507

 

10667

 

10074

 

108

 

836

This problem also can be solved using Dynamic Programming.

Similar problems are : uva 108, uva 10074,uva 10667; These can be solved using the same algorithm.