Baltic OI '15 P5 - Hacker

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Java 2.0s
Memory limit: 256M

Problem type
Baltic Olympiad in Informatics: 2015 Day 2, Problem 2

Byteasar the hacker has qualified for this year's IHO, the International Hacking Olympiad. One of the tasks in the Olympiad involves competing against a system operator. There are n computers numbered from 1 to n, connected in a ring topology, i.e. computers i and i+1 are connected (for i = 1, \dots, n-1), and also computers n and 1 are connected.

The competition is performed as a game between the hacker and the system operator:

  • Byteasar moves first. Afterwards, the operator and Byteasar move alternately.
  • In his first move, Byteasar chooses any computer and hacks it (for instance, by exploiting some operating system vulnerabilities).
  • In his first move, the operator chooses any non-hacked computer and protects it (for instance, by installing latest security upgrades).
  • In all his following moves, Byteasar either (a) does nothing or (b) chooses any computer that is neither hacked nor protected and is directly linked to any hacked computer, and hacks it.
  • In all his following moves, the operator either (a) does nothing or (b) chooses any computer that is neither hacked nor protected and is directly linked to any protected computer, and protects it.
  • The game ends as soon as both have done nothing in two subsequent moves.

At the beginning of the game none of the computers are hacked or protected.

Every computer i has a certain value v_i which specifies the value of the data which is stored on it. For each hacked computer i, Byteasar scores its value v_i. Byteasar is quite a good hacker, but has no idea of algorithms. That is why he asks you to write a program that computes his maximum possible score, assuming that the operator plays optimally.

Input Specification

The first line of input contains a positive integer n (n \ge 2), specifying the number of computers. The second line contains a sequence of n integers v_1, v_2, \dots, v_n (1 \le v_i \le 2000); number v_i specifies the value of the data stored on computer i.

Output Specification

In the first and only line of output your program should write one integer: Byteasar's maximum possible score against an optimally playing operator.

Constraints

SubtaskConditions (in each test case)Points
1n \le 30020
2n \le 500020
3n \le 500\,000, hacking computer 1 is optimal as Byteasar's first move.20
4n \le 500\,00040

Sample Input 1

4
7 6 8 4

Sample Output 1

13

Explanation for Sample 1

In the first example, Byteasar in his first move should hack computer 2 (scoring 6). The operator's response will be protecting computer 3. In the next move Byteasar can hack computer 1 (scoring 7). Finally, the operator will protect computer 4.

Sample Input 2

5
1 1 1 1 1

Sample Output 2

3

Comments

There are no comments at the moment.