Some code for greedy algorithm and dynamic