#abc251e. E - Takahashi and Animals

E - Takahashi and Animals

Score : 500500 points

问题描述

高桥有 NN 只动物。这 NN 只动物分别被称为动物 11、动物 22\ldots、动物 NN

高桥将执行以下 NN 种类型的行动,每种行动可以执行任意次(可能为零次)。

  • 花费 A1A_1 日元(日本货币)喂养动物 1122
  • 花费 A2A_2 日元喂养动物 2233
  • 花费 A3A_3 日元喂养动物 3344
  • \cdots
  • 花费 AiA_i 日元喂养动物 ii(i+1)(i+1)
  • \cdots
  • 花费 AN2A_{N-2} 日元喂养动物 (N2)(N-2)(N1)(N-1)
  • 花费 AN1A_{N-1} 日元喂养动物 (N1)(N-1)NN
  • 花费 ANA_N 日元喂养动物 NN11

注意上述的第 NN 个行动是喂养“动物 NN11”。

请输出至少喂养每只动物一次所需的最小总费用。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

5
2 5 3 2 5

Sample Output 1

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.

Sample Input 2

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

Sample Output 2

426

update @ 2024/3/10 10:43:24