atcoder#ABC251E. [ABC251E] Takahashi and Animals

[ABC251E] Takahashi and Animals

Score : 500500 points

Problem Statement

Takahashi is with NN animals. The NN animals are called Animal 11, Animal 22, \ldots, Animal NN.

Takahashi will perform the following NN kinds of action. Each action can be performed any number of (possibly zero) times.

  • Pay A1A_1 yen (the currency in Japan) to feed Animals 11 and 22.
  • Pay A2A_2 yen to feed Animals 22 and 33.
  • Pay A3A_3 yen to feed Animals 33 and 44.
  • \cdots
  • Pay AiA_i yen to feed Animals ii and (i+1)(i+1).
  • \cdots
  • Pay AN2A_{N-2} yen to feed Animals (N2)(N-2) and (N1)(N-1).
  • Pay AN1A_{N-1} yen to feed Animals (N1)(N-1) and NN.
  • Pay ANA_N yen to feed Animals NN and 11.

Note that the NN-th action above feeds "Animals NN and 11."

Print the minimum possible total cost to feed every animal at least once.

Constraints

  • 2N3×1052 \leq N \leq 3 \times 10^5
  • 1Ai1091 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the minimum possible total cost to feed every animal at least once.

5
2 5 3 2 5
7

If Takahashi performs the 11-st, 33-rd, and 44-th actions once each, Animals 11, 22, 33, 44, and 55 are fed once, once, once, twice, once, respectively, so every animal is fed at least once. The total cost to do so is A1+A3+A4=2+3+2=7A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 yen, which is the minimum possible.

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62
426