5.3 Pingu plays a game
(RankList for this Question)
Score: 100pts
Time Limit: 1.00 sec
Pingu plays a game with his friend Robby the Seal. They have \(n\) baskets. Basket \(i\) contains \(f_i\) number of fishes in it. Pingu, as we all know, loves fishes! The rules of the game are as follows - both players take turn to take each basket. In one turn, the player can take either the leftmost basket or the rightmost basket. When a player chooses a particular basket, he gets to keep the fishes the basket contains. Pingu always starts first. Once a basket is picked, it gets deleted from the row. The game ends when no baskets remain.
Pingu wants to collect the maximum amount of fishes he can. Can you help him find it? Consider both Pingu and Robby the seal to play the game optimally.

\(1 \leq n \leq 1000\)
\(1 \leq f_i \leq 10^9\)

Input Format
\(f_1 f_2 … f_n\)

Output Format
Where X is the number of fishes Pingu can collect.

Example 1
8 15 1 1 3 7


Pingu first takes the rightmost basket and gets 7 fishes. Robby picks the rightmost basket and gets 3 fishes. Pingu again picks the rightmost basket and gets 1 fish. Robby picks up leftmost basket and gets 8 fishes. Ping picks the leftmost basket and gets 15 fishes. Robby picks the last basket.

Example 2
6 4 8 11


Pingu picks the rightmost basket first and gets 11 fishes. Then Robby the seal picks the rightmost basket and gets 8 fishes. Now Pingu selects the leftmost basket with 6 fishes and thus he has 17 fishes. Now Robby the seal picks the last basket with 4 fishes and the game ends.

Log In to solve the Question