UVa 10891 - Game of Sum

10891 - Game of Sum

 * http://acm.uva.es/p/v108/10891.html

Summary
This is a variation of a classic Dynamic Programming problem, only in this one, you can take any number of numbers from either end.

Explanation
In the classic problem, the recurrence is:

$$best( i, j ) = max( value( i ) - best( i + 1, j ), value( j ) - best( i, j - 1 )$$

Now we just add an iteration over it:

$$best( i, j ) = max_{k=0}^{j-i}( max( values( i, i + k ) - best( i + k, j ), values( j - k, j ) - best( i, j - k ) )$$

And the corresponding recursive memoization is trivial to implement.

Input
4 4 -10 -20 7 4 1 2 3 4 6 1 1 1 1 1 1 5 -1 0 0 0 5

Output
7 10 6 6