A - God Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 200

問題文

以下の条件をすべて満たす、長さ A + B の数列 E = (E_1, E_2, \dots, E_{A+B}) を「神の数列」といいます。

  • E_1 + E_2 + \cdots + E_{A+B} = 0 である。
  • E_1, E_2, \dots, E_{A+B} の中に正の整数はちょうど A 個ある。
  • E_1, E_2, \dots, E_{A+B} の中に負の整数はちょうど B 個ある。
  • E_1, E_2, \dots, E_{A+B} はすべて相異なる。
  • すべての i (1 \leq i \leq A+B) について、-10^{9} \leq E_i \leq 10^9, E_i \neq 0 である。

「神の数列」を 1 つ構成してください。

なお、本問題の制約下では、「神の数列」が 1 つ以上存在することが証明できます。

制約

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。

A B

出力

数列の各要素を空白で区切って 1 行で出力してください。

神の数列が複数存在する場合は、どれを出力しても正解となります。

E_1 E_2 \cdots E_{A+B}

入力例 1

1 1

出力例 1

1001 -1001

数列 (1001, -1001) には正の整数が A=1 個、負の整数が B=1 個存在し、総和は 1001+(-1001)=0 です。

その他の問題文中の条件もすべて満たすため、この数列は「神の数列」です。


入力例 2

1 4

出力例 2

-8 -6 -9 120 -97

数列 (-8, -6, -9, 120, -97) には正の整数が A=1 個、負の整数が B=4 個存在し、総和は (-8)+(-6)+(-9)+120+(-97)=0 です。

その他の問題文中の条件もすべて満たすため、この数列は「神の数列」です。


入力例 3

7 5

出力例 3

323 -320 411 206 -259 298 -177 -564 167 392 -628 151

Score : 200 points

Problem Statement

A sequence of length A + B, E = (E_1, E_2, \dots, E_{A+B}), that satisfies all of the conditions below is said to be a god sequence.

  • E_1 + E_2 + \cdots + E_{A+B} = 0 holds.
  • There are exactly A positive integers among E_1, E_2, \dots, E_{A+B}.
  • There are exactly B negative integers among E_1, E_2, \dots, E_{A+B}.
  • E_1, E_2, \dots, E_{A+B} are all distinct.
  • -10^{9} \leq E_i \leq 10^9, E_i \neq 0 holds for every i (1 \leq i \leq A+B).

Construct one god sequence.

We can prove that at least one god sequence exists under Constraints of this problem.

Constraints

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the elements of your sequence in one line, with space as separator.

If there are multiple god sequences, any of them will be accepted.

E_1 E_2 \cdots E_{A+B}

Sample Input 1

1 1

Sample Output 1

1001 -1001

A sequence (1001, -1001) contains A=1 positive integer and B=1 negative integer totaling 1001+(-1001)=0.

It also satisfies the other conditions and thus is a god sequence.


Sample Input 2

1 4

Sample Output 2

-8 -6 -9 120 -97

A sequence (-8, -6, -9, 120, -97) contains A=1 positive integer and B=4 negative integers totaling (-8)+(-6)+(-9)+120+(-97)=0.

It also satisfies the other conditions and thus is a god sequence.


Sample Input 3

7 5

Sample Output 3

323 -320 411 206 -259 298 -177 -564 167 392 -628 151
B - ARC Wrecker

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 400

問題文

AtCoder 街道には N 個のビルが建設されています。最初、左から i 番目のビルは A_i 階建てです。

ARC 解体業者の社長である高橋君は、以下の操作を好きな回数だけ行うことができます。1 回も操作を行わないことも可能です。

  • 好きな正の整数 X を選び、X 階の高さから大砲を発射する。そのとき、現時点で X 階建て以上であるすべてのビルについて、階数が 1 減少する。

あり得る最終的なビルの景観の数を 10^{9} + 7 で割った余りを求めてください。

ただし、景観 A と景観 B が異なるとは、以下のことを指します。

  • 景観 A における、左から i 番目のビルの高さを P_i とする。
  • 景観 B における、左から i 番目のビルの高さを Q_i とする。
  • P_i \neq Q_i となる i1 つでも存在する場合、景観 A と景観 B は異なる。

制約

  • 1 \leq N \leq 100000
  • 1 \leq A_i \leq 10^{9}
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。

N
A_1 A_2 \cdots A_N

出力

答えを出力してください。


入力例 1

2
1 2

出力例 1

4

操作後のビルの高さとしてあり得るものは、以下の 4 通りです。

  • (ビル 1 の階数, ビル 2 の階数) = (0, 0)
  • (ビル 1 の階数, ビル 2 の階数) = (0, 1)
  • (ビル 1 の階数, ビル 2 の階数) = (1, 1)
  • (ビル 1 の階数, ビル 2 の階数) = (1, 2)

入力例 2

6
5 3 4 1 5 2

出力例 2

32

入力例 3

7
314 159 265 358 979 323 846

出力例 3

492018656

全部で 20192492160000 通りの景観があり得ますが、それを 10^{9} + 7 で割った余りである 492018656 を出力すると正解になります。

Score : 400 points

Problem Statement

There are N buildings along AtCoder road. Initially, the i-th building from the left has A_i stories.

Takahashi, the president of ARC Wrecker, Inc., can do the following operation any number of times, possibly zero:

  • Choose a positive integer X that he likes and shoot a cannonball at that height, which decreases by 1 the number of stories in each building with X or more stories.

Find the number of possible final sceneries of buildings, modulo (10^{9} + 7).

We consider two sceneries A and B different when the following holds:

  • let P_i be the number of stories of the i-th building from the left in scenery A;
  • let Q_i be the number of stories of the i-th building from the left in scenery B;
  • we consider sceneries A and B different when P_i \neq Q_i for one or more indices i.

Constraints

  • 1 \leq N \leq 100000
  • 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 \cdots A_N

Output

Print the answer.


Sample Input 1

2
1 2

Sample Output 1

4

There are four possible combinations of heights of the buildings, as follows:

  • (Building 1, Building 2) = (0, 0)
  • (Building 1, Building 2) = (0, 1)
  • (Building 1, Building 2) = (1, 1)
  • (Building 1, Building 2) = (1, 2)

Sample Input 2

6
5 3 4 1 5 2

Sample Output 2

32

Sample Input 3

7
314 159 265 358 979 323 846

Sample Output 3

492018656

There are 20192492160000 possible final sceneries. The correct output is that number modulo 10^{9} + 7, which is 492018656.

C - Tricolor Pyramid

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 600

問題文

N 個のブロックが横一列に並んでおり、それぞれのブロックは青・白・赤のうちいずれかで塗られています。 左から i 番目 (1 \leq i \leq N) のブロックの色は文字 c_i で表され、B は青、W は白、R は赤に対応しています。

この状態から青・白・赤のブロックを積み上げ、N 段のピラミッドの形にします。以下の図がその一例です。

ここでは、ブロックを下から順に、以下の規則で 1 個ずつ置いていきます。

  • 直下にある 2 個のブロックの色が同じ場合、それと同じ色のブロックを置く
  • 直下にある 2 個のブロックの色が異なる場合、そのどちらでもない色のブロックを置く

このとき、一番上のブロックはどの色になるでしょうか?

制約

  • N2 \leq N \leq 400000 を満たす整数
  • c_1, c_2, \dots, c_N はそれぞれ BWR のいずれか

入力

入力は以下の形式で標準入力から与えられます。

N
c_1c_2\cdotsc_N

出力

一番上のブロックの色が青ならば B、白ならば W、赤ならば R を出力してください。


入力例 1

3
BWR

出力例 1

W

この入力例では、ブロックを以下のように積み上げることになります。

  • 一番下の段の左から 1, 2 番目のブロックはそれぞれ青色・白色なので、その上に赤色のブロックを置く。
  • 一番下の段の左から 2, 3 番目のブロックはそれぞれ白色・赤色なので、その上に青色のブロックを置く。
  • 下から 2 段目のブロックはそれぞれ赤色・青色なので,その上に白色のブロックを置く。

一番上のブロックの色は白となるため、W を出力します。


入力例 2

4
RRBB

出力例 2

W

この入力例では、ブロックを以下のように積み上げることになります。

  • 一番下の段の左から 1, 2 番目のブロックはそれぞれ赤色・赤色なので、その上に赤色のブロックを置く。
  • 一番下の段の左から 2, 3 番目のブロックはそれぞれ赤色・青色なので、その上に白色のブロックを置く。
  • 一番下の段の左から 3, 4 番目のブロックはそれぞれ青色・青色なので、その上に青色のブロックを置く。
  • 下から 2 段目の左から 1, 2 番目のブロックはそれぞれ赤色・白色なので、その上に青色のブロックを置く。
  • 下から 2 段目の左から 2, 3 番目のブロックはそれぞれ白色・青色なので、その上に赤色のブロックを置く。
  • 下から 3 段目のブロックはそれぞれ青色・赤色なので、その上に白色のブロックを置く。

一番上のブロックの色は白となるため、W を出力します。


入力例 3

6
BWWRBW

出力例 3

B

最終的なブロックの並びは、以下の図のように表されます。一番上のブロックの色は青となるため、B を出力します。

なお、これは問題文中に例示したケースと同じものになっています。


入力例 4

8
WWBRBBWB

出力例 4

R

最終的なブロックの並びは、以下の図のように表されます。一番上のブロックの色は赤となるため、R を出力します。


入力例 5

21
BWBRRBBRWBRBBBRRBWWWR

出力例 5

B

Score : 600 points

Problem Statement

We have N blocks arranged in a row, where each block is painted blue, white, or red. The color of the i-th block from the left (1 \leq i \leq N) is represented by a character c_i; B, W, and R stand for blue, white, and red, respectively.

From this situation, we will pile up blue, white, and red blocks to make a pyramid with N steps. The following figure shows an example of this:

Here, we place blocks one by one from bottom to top as follows:

  • if the two blocks immediately below a position have the same color, we will place there a block of the same color;
  • if the two blocks immediately below a position have different colors, we will place there a block of the color different from those two colors.

What will be the color of the block at the top?

Constraints

  • N is an integer satisfying 2 \leq N \leq 400000.
  • Each of c_1, c_2, \dots, c_N is B, W, or R.

Input

Input is given from Standard Input in the following format:

N
c_1c_2\cdotsc_N

Output

If the topmost block will be blue, print B; if it will be white, print W; if it will be red, print R.


Sample Input 1

3
BWR

Sample Output 1

W

In this case, we will pile up blocks as follows:

  • the 1-st and 2-nd blocks from the left in the bottom row are respectively blue and white, so we place a red block on top of it;
  • the 2-nd and 3-rd blocks from the left in the bottom row are respectively white and red, so we place a blue block on top of it;
  • the blocks in the 2-nd row from the bottom are respectively red and blue, so we place a white block on top of it.

Thus, the block at the top will be white; we should print W.


Sample Input 2

4
RRBB

Sample Output 2

W

In this case, we will pile up blocks as follows:

  • the 1-st and 2-nd blocks from the left in the bottom row are both red, so we place a red block on top of it;
  • the 2-nd and 3-rd blocks from the left in the bottom row are respectively red and blue, so we place a white block on top of it;
  • the 3-rd and 4-th blocks from the left in the bottom row are both blue, so we place a blue block on top of it;
  • the 1-st and 2-nd blocks from the left in the 2-nd row from the bottom are respectively red and white, so we place a blue block on top of it;
  • the 2-nd and 3-rd blocks from the left in the 2-nd row from the bottom are respectively white and blue, so we place a red block on top of it;
  • the blocks in the 3-rd row from the bottom are respectively blue and red, so we place a white block on top of it.

Thus, the block at the top will be white; we should print W.


Sample Input 3

6
BWWRBW

Sample Output 3

B

The figure below shows the final arrangement of blocks. The block at the top will be blue; we should print B.

Note that this is the case shown as an example in Problem Statement.


Sample Input 4

8
WWBRBBWB

Sample Output 4

R

The figure below shows the final arrangement of blocks. The block at the top will be red; we should print R.


Sample Input 5

21
BWBRRBBRWBRBBBRRBWWWR

Sample Output 5

B
D - Miracle Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 600

問題文

N 頂点の木があり、頂点には 1, 2, \dots, N と番号が振られています。i 番目 (1 \leq i \leq N-1) の辺は頂点 A_i と頂点 B_i を結んでいます。

木を見つけた E869120 君は、N 個の頂点それぞれに整数を書き込み、square1001 君を驚かせようとしています。彼を驚かせるためには、頂点 i に書かれた整数を E_i とするとき、次の条件を満たす必要があります。

条件1 E_i \geq 1 (1 \leq i \leq N) を満たす。
条件2 すべての組 (i, j) (1 \leq i < j \leq N) について、|E_i - E_j| \geq dist(i, j) を満たす。
条件3 条件 1・条件 2 を満たす中で、\max(E_1, E_2, \dots, E_N) の値が最小になる。

ただし、dist(i, j) は次の値を指します。

  • 頂点 i から j への単純パス(同じ頂点を 2 度通らない経路)の長さ。
  • つまり、単純パスを q_0 \to q_1 \to q_2 \to \cdots \to q_Lq_0 = i, q_L = j)とするときの L の値。

square1001 君を驚かせるような整数の書き方を 1 つ構成してください。

制約

  • 2 \leq N \leq 200000
  • 1 \leq A_i < B_i \leq N
  • 与えられるグラフは木である
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

出力

木に書き込む整数 E_1, E_2, \cdots, E_N を順に、空白で区切って 1 行で出力してください。

問題文の条件を満たす整数の書き込み方が複数存在する場合は、どれを出力しても正解となります。

E_1 E_2 \cdots E_{N}

入力例 1

2
1 2

出力例 1

2 1

頂点 1 に整数 2 を、頂点 2 に整数 1 を書き込んだ場合、dist(1, 2) = 1|E_1 - E_2| = 1 であるため、問題文中の条件 2 を満たします。

その他の条件もすべて満たすため、この書き込み方は square1001 君を驚かせることができます。

他にも、(E_1, E_2) = (1, 2) は正解となります。


入力例 2

4
1 2
1 4
2 3

出力例 2

3 2 1 4

他にも、(E_1, E_2, E_3, E_4) = (2, 3, 4, 1) は正解となります。

Score : 600 points

Problem Statement

We have a tree with N vertices, numbered 1, 2, \dots, N. The i-th edge (1 \leq i \leq N-1) connects Vertex A_i and Vertex B_i.

A boy E869120 found this tree and wants to write an integer in each vertex to surprise another boy square1001. For that, the following conditions need to be satisfied, where E_i is the integer written on Vertex i.

Condition 1: E_i \geq 1 (1 \leq i \leq N) holds.
Condition 2: |E_i - E_j| \geq dist(i, j) holds for every pair (i, j) (1 \leq i < j \leq N).
Condition 3: the value \max(E_1, E_2, \dots, E_N) should be minimized while satisfying Conditions 1 and 2.

Here, dist(i, j) is:

  • the length of the simple path (the path without repetition of the same vertex) from Vertex i to j.
  • In other words, it is the value L where the simple path is q_0 \to q_1 \to q_2 \to \cdots \to q_L (q_0 = i, q_L = j).

Construct one way to write integers that surprises square1001.

Constraints

  • 2 \leq N \leq 200000
  • 1 \leq A_i < B_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

Output

Print a line containing integers E_1, E_2, \cdots, E_N to write on the vertices, in this order, with space as separator.

If there are multiple ways to write integers that satisfy the conditions in the Problem Statement, any of them will be accepted.

E_1 E_2 \cdots E_{N}

Sample Input 1

2
1 2

Sample Output 1

2 1

If we write an integer 2 on Vertex 1 and an integer 1 on Vertex 2, we have dist(1, 2) = 1 and |E_1 - E_2| = 1, satisfying Condition 2.

The other conditions are also satisfied, so we can surprise square1001 this way.

(E_1, E_2) = (1, 2) will also be accepted.


Sample Input 2

4
1 2
1 4
2 3

Sample Output 2

3 2 1 4

(E_1, E_2, E_3, E_4) = (2, 3, 4, 1) will also be accepted.

E - Zero-Sum Ranges 2

Time Limit: 5 sec / Memory Limit: 1024 MB

配点: 900

問題文

以下の条件をともに満たす長さ 2N の数列 A = (A_1, A_2, \dots, A_{2N}) は、何種類あるでしょうか?

  • 数列 A は、N 個の +1N 個の -1 で構成される。
  • A_l + A_{l+1} + \cdots + A_r = 0 となる l, r の組み合わせ (1 \leq l \leq r \leq 2N) はちょうど K 個ある。

制約

  • 1 \leq N \leq 30
  • 1 \leq K \leq N^2
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。

N K

出力

問題文中の条件を満たす数列が何種類あるかを出力してください。
ただし、答えは必ず 64 ビット符号付き整数型の範囲に収まります。


入力例 1

1 1

出力例 1

2

N = 1, K = 1 のとき、条件を満たす数列は次の 2 種類です。

  • A = (+1, -1)
  • A = (-1, +1)

入力例 2

2 3

出力例 2

2

N = 2, K = 3 のとき、条件を満たす数列は次の 2 種類です。

  • A = (+1, -1, -1, +1)
  • A = (-1, +1, +1, -1)

入力例 3

3 7

出力例 3

6

N = 3, K = 7 のとき、条件を満たす数列は次の 6 種類です。

  • A = (+1, -1, +1, -1, -1, +1)
  • A = (+1, -1, -1, +1, +1, -1)
  • A = (+1, -1, -1, +1, -1, +1)
  • A = (-1, +1, +1, -1, +1, -1)
  • A = (-1, +1, +1, -1, -1, +1)
  • A = (-1, +1, -1, +1, +1, -1)

入力例 4

8 24

出力例 4

568

N = 8, K = 24 のとき、条件を満たす数列は 568 種類あります。


入力例 5

30 230

出力例 5

761128315856702

N = 30, K = 230 のとき、条件を満たす数列は 761128315856702 種類あります。


入力例 6

25 455

出力例 6

0

N = 25, K = 455 のとき、条件を満たす数列はありません。

Score : 900 points

Problem Statement

How many different sequences of length 2N, A = (A_1, A_2, \dots, A_{2N}), satisfy both of the following conditions?

  • The sequence A contains N occurrences of +1 and N occurrences of -1.
  • There are exactly K pairs of l and r (1 \leq l \leq r \leq 2N) such that A_l + A_{l+1} + \cdots + A_r = 0.

Constraints

  • 1 \leq N \leq 30
  • 1 \leq K \leq N^2
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of different sequences that satisfy the conditions in Problem Statement. The answer always fits into the signed 64-bit integer type.


Sample Input 1

1 1

Sample Output 1

2

For N = 1, K = 1, two sequences below satisfy the conditions:

  • A = (+1, -1)
  • A = (-1, +1)

Sample Input 2

2 3

Sample Output 2

2

For N = 2, K = 3, two sequences below satisfy the conditions:

  • A = (+1, -1, -1, +1)
  • A = (-1, +1, +1, -1)

Sample Input 3

3 7

Sample Output 3

6

For N = 3, K = 7, six sequences below satisfy the conditions:

  • A = (+1, -1, +1, -1, -1, +1)
  • A = (+1, -1, -1, +1, +1, -1)
  • A = (+1, -1, -1, +1, -1, +1)
  • A = (-1, +1, +1, -1, +1, -1)
  • A = (-1, +1, +1, -1, -1, +1)
  • A = (-1, +1, -1, +1, +1, -1)

Sample Input 4

8 24

Sample Output 4

568

Sample Input 5

30 230

Sample Output 5

761128315856702

Sample Input 6

25 455

Sample Output 6

0

For N = 25, K = 455, no sequences satisfy the conditions.

F - Gateau

Time Limit: 3 sec / Memory Limit: 1024 MB

配点: 900

問題文

AtCoder さんは 2N 人の友人のために、円形のチョコレートケーキを作りました。このケーキは中心から放射状に 2N 個のピースに等分されており、各ピースには時計回りの順番で 0 から 2N-1 までの番号が付けられています。

いま、最後の仕上げとして、AtCoder さんはこのケーキの各ピースの上にいちごを乗せようとしており、そのために友人に希望を聞きました。友人には 0 から 2N-1 までの番号が付けられており、友人 i \ (0 \leq i \leq 2N-1) の希望は以下の通りです。

  • ピース i, i+1, \dots, i+N-1 には、合計で A_i 個以上のいちごが乗せられていてほしい(ただし、x \geq 2N に対しては、ピース x はピース x-2N のことを指すものとする)

友人全員の希望を叶えるためには、ケーキ全体に最小で何個のいちごを乗せる必要があるでしょうか?

制約

  • 1 \leq N \leq 150000
  • 0 \leq A_i \leq 5 \times 10^8 \ (0 \leq i \leq 2N-1)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。

N
A_0 A_1 \cdots A_{2N-1}

出力

友人全員の希望を叶えるために乗せる必要のあるいちごの個数の最小値を出力してください。


入力例 1

3
2 5 7 4 2 1

出力例 1

8

ピース 0, 1, 2, 3, 4, 5 の上に置くいちごの個数を、それぞれ 1, 0, 1, 4, 2, 0 とする場合を考えます。

そのとき、それぞれの友人の希望は、以下のようにすべて叶います。

  • 友人 0:ピース 0, 1, 2 にはいちごが合計 2 個あり、これは A_0 = 2 個以上である
  • 友人 1:ピース 1, 2, 3 にはいちごが合計 5 個あり、これは A_1 = 5 個以上である
  • 友人 2:ピース 2, 3, 4 にはいちごが合計 7 個あり、これは A_2 = 7 個以上である
  • 友人 3:ピース 3, 4, 5 にはいちごが合計 6 個あり、これは A_3 = 4 個以上である
  • 友人 4:ピース 4, 5, 0 にはいちごが合計 3 個あり、これは A_4 = 2 個以上である
  • 友人 5:ピース 5, 0, 1 にはいちごが合計 1 個あり、これは A_5 = 1 個以上である

したがって、チョコレートケーキ全体で 8 個のいちごを置くことで、友人全員の希望を叶えることができます。 これが最小値となります。


入力例 2

3
8 0 6 0 9 0

出力例 2

12

ピース 0, 1, 2, 3, 4, 5 の上に置くいちごの個数を、それぞれ 6, 0, 2, 1, 3, 0 とする場合を考えます。

そのとき、それぞれの友人の希望は、以下のようにすべて叶います。

  • 友人 0:ピース 0, 1, 2 にはいちごが合計 8 個あり、これは A_0 = 8 個以上である
  • 友人 1:ピース 1, 2, 3 にはいちごが合計 3 個あり、これは A_1 = 0 個以上である
  • 友人 2:ピース 2, 3, 4 にはいちごが合計 6 個あり、これは A_2 = 6 個以上である
  • 友人 3:ピース 3, 4, 5 にはいちごが合計 4 個あり、これは A_3 = 0 個以上である
  • 友人 4:ピース 4, 5, 0 にはいちごが合計 9 個あり、これは A_4 = 9 個以上である
  • 友人 5:ピース 5, 0, 1 にはいちごが合計 6 個あり、これは A_5 = 0 個以上である

したがって、チョコレートケーキ全体で 12 個のいちごを置くことで、友人全員の希望を叶えることができます。 これが最小値となります。


入力例 3

5
3 1 5 7 0 8 4 6 4 9

出力例 3

12

ピース 0, 1, \dots, 9 の上に置くいちごの個数を、それぞれ 0, 0, 0, 4, 0, 1, 1, 1, 0, 5 とすると、友人全員の希望を叶えることができます。

このとき、チョコレートケーキ全体で 12 個のいちごを置くことになり、これが最小値となります。


入力例 4

1
267503 601617

出力例 4

869120

ピース 0 の上に 267503 個、ピース 1 の上に 601617 個のいちごを置くと、友人全員の希望を叶えることができます。


入力例 5

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

出力例 5

513119404

Score : 900 points

Problem Statement

Mr. AtCoder made a round chocolate cake for his 2N friends. It is radially cut from the center into 2N equal pieces, numbered 0 through 2N-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 0 through 2N-1, and Friend i (0 \leq i \leq 2N-1) wants the following:

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

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

Constraints

  • 1 \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:

N
A_0 A_1 \cdots A_{2N-1}

Output

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


Sample Input 1

3
2 5 7 4 2 1

Sample Output 1

8

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

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

  • Friend 0: Pieces 0, 1, 2 have a total of 2 strawberries on them, which is not less than A_0 = 2.
  • Friend 1: Pieces 1, 2, 3 have a total of 5 strawberries on them, which is not less than A_1 = 5.
  • Friend 2: Pieces 2, 3, 4 have a total of 7 strawberries on them, which is not less than A_2 = 7.
  • Friend 3: Pieces 3, 4, 5 have a total of 6 strawberries on them, which is not less than A_3 = 4.
  • Friend 4: Pieces 4, 5, 0 have a total of 3 strawberries on them, which is not less than A_4 = 2.
  • Friend 5: Pieces 5, 0, 1 have a total of 1 strawberry on them, which is not less than A_5 = 1.

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


Sample Input 2

3
8 0 6 0 9 0

Sample Output 2

12

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

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

  • Friend 0: Pieces 0, 1, 2 have a total of 8 strawberries on them, which is not less than A_0 = 8.
  • Friend 1: Pieces 1, 2, 3 have a total of 3 strawberries on them, which is not less than A_1 = 0.
  • Friend 2: Pieces 2, 3, 4 have a total of 6 strawberries on them, which is not less than A_2 = 6.
  • Friend 3: Pieces 3, 4, 5 have a total of 4 strawberries on them, which is not less than A_3 = 0.
  • Friend 4: Pieces 4, 5, 0 have a total of 9 strawberries on them, which is not less than A_4 = 9.
  • Friend 5: Pieces 5, 0, 1 have a total of 6 strawberries on them, which is not less than A_5 = 0.

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


Sample Input 3

5
3 1 5 7 0 8 4 6 4 9

Sample Output 3

12

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

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


Sample Input 4

1
267503 601617

Sample Output 4

869120

We can put 267503 strawberries on Piece 0 and 601617 strawberries on Piece 1 to fulfill all requests of the friends.


Sample Input 5

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

Sample Output 5

513119404