Algorithms and Data Structures

Playing a game of removing numbers

Let’s play a game. You are given n positive integers (n is even). On each step, you are allowed to remove a number from the beginning or the very end of the sequence and keep it to yourself. I (the computer) will remove the largest among the first and the last elements in the sequence. In case those are equal, I’ll remove the first one. We will take turns until there are no items left on the list.
If you are the one starting the game, what would be the maximum sum of numbers you can collect?


The first line of the input contains a single integer n (1 ≤ n ≤ 1000).
The next line contains n space-separated integers (1 ≤ ).


The program should print the maximum sum you can collect.


4 3 2 10 4


  1. You take the first 3 → 2 10 4
  1. The computer takes the 4 → 2 10
  1. You take the 10 → 2
  1. The computer takes the 2 ⇒ You’ve collected 3 + 10 = 13


Time limit: 0.2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue