A good question(FSEATS on spoj) involving dynamic programming