COCI '07 Regional #2 Nikola
View as PDFAgainst his own will, Nikola has become the main character in a game. The game is played on a row of
 squares, numbered 
 to 
. Nikola is initially in square 
 and can jump to other squares. Nikola's first
jump must be to square 
. Each subsequent jump must satisfy two constraints:
- If the jump is in the forward direction, it must be one square longer than the preceding jump.
 - If the jump is in the backward direction, it must be exactly the same length as the preceding jump.
 
For example, after the first jump (when on square ), Nikola can jump back to square 
 or forwards to
square 
.
Every time he enters a square, Nikola must pay an entry fee. Nikola's goal is to get from square  to
square 
 as cheaply as possible. Write a program that determines the smallest total cost for Nikola to
get to square 
.
Input Specification
The first line contains the integer , 
, the number of squares.
Each of the following  lines contains the entry fee for one square, a positive integer less than 
.
The entry fees will be given in order for squares 
 to 
.
Output Specification
Output the smallest total cost for Nikola to get to square .
Sample Input 1
6
1
2
3
4
5
6
Sample Output 1
12
Sample Input 2
8
2
3
4
3
1
6
1
4
Sample Output 2
14
In the first example, after jumping to square , Nikola jumps back to square 
. From there he can jump
to square 
 and then to 
.
Comments
Note that Nikola does not pay to enter the starting square.
My code passes with Clang++ but not g++. I tested with official test data and got the correct output but when I use C++11 on dmoj i WA