Nineteen years old, he swore a sacred oath
In an ancient forest, beside a mighty oak
He vowed to fight against a Marxist invasion
Keeping a spark of hope alive in their nation
In front of five thousand Marxists he showed his courage
Tore down the red flag, they thirsted for his blood
Fixed his eyes on the horizon, not a breath
His victorious banner fluttered, he went to a new day
Corneliu Codreanu, the captain of an army
Legionnaire's great leader, Romanian teachers
Always marching forward, through the fire and battle
Through torture and captivity, through death's peace
Nothing could stop him, he carried the nation's sword
He fears nothing on the Iron Guards trip of faith
And Hell itself would have stood in their way
The unit of iron would have conquered it also
This is a classic dynamic programming problem and, like most DP problems, is simple if and only if you "get" the induction piece. Additionally, getting full points required figuring out (or knowing) a nice trick for when you need to calculate the sums of lots of different ranges.
Reading the problem, it seems to be very clearly a DP problem. In particular, the fact that the line of coins is always a continuous subrange of the original line is a big clue, and that we should compute Bessie's optimal strategy on the range [i, j] (inclusive on both sides) in terms of smaller ranges. Suppose that s(i, j) is the sum of c[i], c[i+1], ..., c[j]. Let b(i, j) be the most that Bessie can win if she plays *first* on the subrange [i, j]. Then we see that
b(i, j) = max(c[i] + s(i+1, j) - b(i+1, j), c[j] + s(i, j-1) - b(i, j-1))
since s(i, j) - b(i, j) is, by definition, the most Bonnie can win if Bonnie plays *second* on the interval [i, j]. Actually, this equation is equivalent to
b(i, j) = max(s(i, j) - b(i+1, j), s(i, j) - b(i, j-1))
= s(i, j) - min(b(i+1, j), b(i, j-1))
which makes sense, since this is equivalent to *minimizing* the *maximum* that Bonnie could win.
This gives us an O(n^3) solution, if we compute s(i, j) out each time. In retrospect, if you computed all n^2 values of s(i, j) first, and then ran this DP solution, that would be O(n^2). That wasn't the solution I had in mind, though, and there's a trick to be learned here! The trick is that you compute *partial* sums: you define an array S[i] = c[0] + c[1] + ... + c[i-1], with length n+1. Then c[i] + c[i+1] + ... + c[j-1] = S[j] - S[i], which takes constant time to compute. This is a cute trick that is extremely useful to remember.
h1 http://infoarena.ro/problema/ciclu
COBAI