PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000036. Những bước nhảy của Nikola (Nikola)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những bước nhảy của Nikola (Nikola)

Nikola

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Nikola moves along NN squares in a row, numbered 11 through NN. He starts at square 11 and his first jump must land on square 22.

After each jump he may jump forward or backward. A forward jump must be exactly one square longer than the previous jump; a backward jump must have the same length as the previous jump. He may land only on squares in [1,N][1,N]. Landing on square ii costs cic_i, while the initial position costs nothing.

Find the minimum total cost needed to reach square NN.

Input

The first line contains NN, the number of squares.

Each of the next NN lines contains an integer cic_i, the landing cost of square ii.

Output

Print the minimum total landing cost to reach square NN.

Subtasks

  • Subtask 1 (20 points): 2≤N≤202\le N\le 20; 1≤ci≤4991\le c_i\le 499.
  • Subtask 2 (30 points): 2≤N≤2002\le N\le 200; 1≤ci≤4991\le c_i\le 499.
  • Subtask 3 (50 points): 2≤N≤10002\le N\le 1000; 1≤ci≤4991\le c_i\le 499.

Examples

Input

6
1
2
3
4
5
6

Output

12

Explanation

Nikola starts at square 11 and must first jump to square 22. Later forward jumps increase in length by one, while backward jumps retain the previous length. One optimal route is 1→2→1→3→61\to2\to1\to3\to6: jumps of lengths 11 forward, 11 backward, 22 forward, and 33 forward. The landing costs are c2+c1+c3+c6=2+1+3+6=12c_2+c_1+c_3+c_6=2+1+3+6=12. No cheaper route exists.