102514: [AtCoder]ABC251 E - Takahashi and Animals

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

Takahashi is with $N$ animals. The $N$ animals are called Animal $1$, Animal $2$, $\ldots$, Animal $N$.

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

  • Pay $A_1$ yen (the currency in Japan) to feed Animals $1$ and $2$.
  • Pay $A_2$ yen to feed Animals $2$ and $3$.
  • Pay $A_3$ yen to feed Animals $3$ and $4$.
  • $\cdots$
  • Pay $A_i$ yen to feed Animals $i$ and $(i+1)$.
  • $\cdots$
  • Pay $A_{N-2}$ yen to feed Animals $(N-2)$ and $(N-1)$.
  • Pay $A_{N-1}$ yen to feed Animals $(N-1)$ and $N$.
  • Pay $A_N$ yen to feed Animals $N$ and $1$.

Note that the $N$-th action above feeds "Animals $N$ and $1$."

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

Constraints

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

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\ldots$ $A_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 $1$-st, $3$-rd, and $4$-th actions once each, Animals $1$, $2$, $3$, $4$, and $5$ are fed once, once, once, twice, once, respectively, so every animal is fed at least once. The total cost to do so is $A_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

Input

题意翻译

有 $n$ 只动物围成一圈,你可以花费 $a[i]$ 喂食动物 $i$ 和 $i+1$。特别地,你可以花费 $a[n]$ 喂食动物 $n$ 和 $1$。 输出喂食所有动物需要的最小花费。

Output

分数:500分

问题描述

高桥君和$N$只动物在一起。 这$N$只动物被称为Animal $1$, Animal $2$, $\ldots$, Animal $N$。

高桥君将执行以下$N$种操作。 每个操作可以执行任意次数(包括零次)。

  • 支付$A_1$日元(日本的货币)来喂食Animals $1$和$2$。
  • 支付$A_2$日元来喂食Animals $2$和$3$。
  • 支付$A_3$日元来喂食Animals $3$和$4$。
  • $\cdots$
  • 支付$A_i$日元来喂食Animals $i$和$(i+1)$。
  • $\cdots$
  • 支付$A_{N-2}$日元来喂食Animals $(N-2)$和$(N-1)$。
  • 支付$A_{N-1}$日元来喂食Animals $(N-1)$和$N$。
  • 支付$A_N$日元来喂食Animals $N$和$1$。

请注意,上面的第$N$个操作是喂食"Animals $N$和$1$。"

输出至少喂食每只动物一次的最低总成本。

约束

  • $2 \leq N \leq 3 \times 10^5$
  • $1 \leq A_i \leq 10^9$
  • 输入中的所有值都是整数。

输入

输入以标准输入的以下格式给出:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

输出

输出至少喂食每只动物一次的最低总成本。


样例输入1

5
2 5 3 2 5

样例输出1

7

如果高桥君分别执行第1个,第3个和第4个操作一次,那么Animals $1$,$2$,$3$,$4$和$5$分别被喂食一次,一次,一次,两次和一次,因此每只动物都被喂食至少一次。 这样做的总成本是$A_1 + A_3 + A_4 = 2 + 3 + 2 = 7$日元,这是最低可能的。


样例输入2

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

样例输出2

426

加入题单

算法标签: