If you understand what I was saying then the implementation should be obvious. But I may not have been very clear, so I'll try again.
If the input is 6 elements with values a,b,c,d,e,f, then you read them into the array like this (with a 0 at the beginning and end):
Code:
0 1 2 3 4 5 6 7
0 a b c d e f 0
Then you figure out the optimal values from element 7 back to element 0:
Code:
. optimal value
7 0 0
6 f f
5 e e + (optimal value of elem 6 or 7, whichever's best)
4 d d + (opt val of elem 5 or 6, whichever's best)
3 c c + (opt val of elem 4 or 5, ...)
2 b ...
1 a ...
0 0 0 + (opt val of elem 1 or 2, whichever is best)
And the optimal value for element 0 is the final answer.
Working it on your example input: 2 -1 3 –2 -1 6 -5
Code:
8 0 0
7 -5 -5
6 6 6 ( 6 + 0)
5 -1 5 (-1 + 6)
4 -2 4 (-2 + 6)
3 3 8 ( 3 + 5)
2 -1 7 (-1 + 8)
1 2 10 ( 2 + 8)
0 0 10 ( 0 + 10)