Disclaimer: I have no idea if this are indeed the correct answers. I just solved the exercises like this. I think that they are right though.
I have added my own code to this gist. It is ugly as hell, just like you can expect from code created in a contest like this.
It is simple to see that a greedy solution is good enough.