atcoder#ARC130F. [ARC130F] Replace by Average

[ARC130F] Replace by Average

Score : 10001000 points

Problem Statement

Given is a sequence of NN positive integers A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N).

You can do the following operation on this sequence any number of times.

  • Choose integers i,j,ki, j, k such that 1i<j<kN1\leq i < j < k \leq N and j=i+k2j = \frac{i+k}{2}. Replace AjA_j with Ai+Ak2\lfloor\frac{A_i+A_k}{2}\rfloor.

Find the minimum possible value of i=1NAi\sum_{i=1}^N A_i after the operations.

Constraints

  • 3N3×1053\leq N\leq 3\times 10^5
  • 1Ai10121\leq A_i\leq 10^{12}

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

5
2 2 5 5 4
13

The following operations achieves i=1NAi=13\sum_{i=1}^N A_i = 13.

  • Do the operation with (i,j,k)=(1,3,5)(i,j,k) = (1,3,5). The sequence AA is now (2,2,3,5,4)(2,2,3,5,4).
  • Do the operation with (i,j,k)=(3,4,5)(i,j,k) = (3,4,5). The sequence AA is now (2,2,3,3,4)(2,2,3,3,4).
  • Do the operation with (i,j,k)=(2,3,4)(i,j,k) = (2,3,4). The sequence AA is now (2,2,2,3,4)(2,2,2,3,4).
5
3 1 4 1 5
11
3
3 1 3
7
3
3 5 3
9