p1002 solution

dp + greedy
p1003