Greedy algorithms and dynamic programming