#ARC117F. [ARC117F] Gateau

[ARC117F] Gateau

Score : 900900 points

Problem Statement

Mr. AtCoder made a round chocolate cake for his 2N2N friends. It is radially cut from the center into 2N2N equal pieces, numbered 00 through 2N12N-1 in clockwise order.

To give the final touches, he is going to put strawberries on each piece. He has heard what his friends want about it. The friends are numbered 00 through 2N12N-1, and Friend ii (0i2N1)(0 \leq i \leq 2N-1) wants the following:

  • Piece i,i+1,,i+N1i, i+1, \dots, i+N-1 have a total of at least AiA_i strawberries on them (for x2Nx \geq 2N, consider Piece xx as Piece x2Nx-2N).

To fulfill the requests of all his friends, at least how many strawberries should be put on the whole cake?

Constraints

  • 1N1500001 \leq N \leq 150000
  • $0 \leq A_i \leq 5 \times 10^8 \ (0 \leq i \leq 2N-1)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A0A_0 A1A_1 \cdots A2N1A_{2N-1}

Output

Print the minimum number of strawberries that should be put on the whole cake to fulfill the requests of the friends.

3
2 5 7 4 2 1
8

Consider the case we put 1,0,1,4,2,01, 0, 1, 4, 2, 0 strawberries on Pieces 0,1,2,3,4,50, 1, 2, 3, 4, 5, respectively.

In this case, all requests of the friends are fulfilled, as follows:

  • Friend 00: Pieces 0,1,20, 1, 2 have a total of 22 strawberries on them, which is not less than A0=2A_0 = 2.
  • Friend 11: Pieces 1,2,31, 2, 3 have a total of 55 strawberries on them, which is not less than A1=5A_1 = 5.
  • Friend 22: Pieces 2,3,42, 3, 4 have a total of 77 strawberries on them, which is not less than A2=7A_2 = 7.
  • Friend 33: Pieces 3,4,53, 4, 5 have a total of 66 strawberries on them, which is not less than A3=4A_3 = 4.
  • Friend 44: Pieces 4,5,04, 5, 0 have a total of 33 strawberries on them, which is not less than A4=2A_4 = 2.
  • Friend 55: Pieces 5,0,15, 0, 1 have a total of 11 strawberry on them, which is not less than A5=1A_5 = 1.

Thus, we can fulfill all requests of the friends by putting a total of 88 strawberries on the cake. This is the minimum number of strawberries needed.

3
8 0 6 0 9 0
12

Consider the case we put 6,0,2,1,3,06, 0, 2, 1, 3, 0 strawberries on Pieces 0,1,2,3,4,50, 1, 2, 3, 4, 5, respectively.

In this case, all requests of the friends are fulfilled, as follows:

  • Friend 00: Pieces 0,1,20, 1, 2 have a total of 88 strawberries on them, which is not less than A0=8A_0 = 8.
  • Friend 11: Pieces 1,2,31, 2, 3 have a total of 33 strawberries on them, which is not less than A1=0A_1 = 0.
  • Friend 22: Pieces 2,3,42, 3, 4 have a total of 66 strawberries on them, which is not less than A2=6A_2 = 6.
  • Friend 33: Pieces 3,4,53, 4, 5 have a total of 44 strawberries on them, which is not less than A3=0A_3 = 0.
  • Friend 44: Pieces 4,5,04, 5, 0 have a total of 99 strawberries on them, which is not less than A4=9A_4 = 9.
  • Friend 55: Pieces 5,0,15, 0, 1 have a total of 66 strawberries on them, which is not less than A5=0A_5 = 0.

Thus, we can fulfill all requests of the friends by putting a total of 1212 strawberries on the cake. This is the minimum number of strawberries needed.

5
3 1 5 7 0 8 4 6 4 9
12

We can put 0,0,0,4,0,1,1,1,0,50, 0, 0, 4, 0, 1, 1, 1, 0, 5 strawberries on Pieces 0,1,,90, 1, \dots, 9, respectively, to fulfill all requests of the friends.

In this case, there is a total of 1212 strawberries on the cake, which is the minimum number of strawberries needed.

1
267503 601617
869120

We can put 267503267503 strawberries on Piece 00 and 601617601617 strawberries on Piece 11 to fulfill all requests of the friends.

8
418940906 38034755 396064111 43044067 356084286 61548818 15301658 35906016 20933120 211233791 30314875 25149642 42550552 104690843 81256233 63578117
513119404