A - Legendary Players

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

AtCoder では、レーティング上位 10 人のハンドルネームには金色の冠が、上位 1 人のハンドルネームには白金色の冠が表示されます。

このコンテストが開始した時点で、アルゴリズム部門での上位 10 人に入っているプレイヤーのハンドルネームとレーティングは以下のようになっています。

tourist 3858
ksun48 3679
Benq 3658
Um_nik 3648
apiad 3638
Stonefeang 3630
ecnerwala 3613
mnbvmar 3555
newbiedmy 3516
semiexp 3481

上記のプレイヤーのハンドルネーム S が与えられるので、その人のレーティングを出力してください。

制約

  • S はアルゴリズム部門でレーティング上位 10 人に入っているプレイヤーのハンドルネームのいずれかと等しい。

入力

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

S

出力

対応するプレイヤーのレーティングを 1 行で出力せよ。


入力例 1

tourist

出力例 1

3858

このコンテストが開始した時点において、 tourist さんのアルゴリズム部門のレーティングは 3858 です。


入力例 2

semiexp

出力例 2

3481

このコンテストが開始した時点において、 semiexp さんのアルゴリズム部門のレーティングは 3481 です。

Score : 100 points

Problem Statement

In AtCoder, the top 10 rated players' usernames are displayed with a gold crown, and the top-rated player's username is displayed with a platinum crown.

At the start of this contest, the usernames and ratings of the top 10 rated players in the algorithm category are as follows:

tourist 3858
ksun48 3679
Benq 3658
Um_nik 3648
apiad 3638
Stonefeang 3630
ecnerwala 3613
mnbvmar 3555
newbiedmy 3516
semiexp 3481

You are given the username S of one of these players. Print that player's rating.

Constraints

  • S is equal to one of the usernames of the top 10 rated players in the algorithm category.

Input

The input is given from Standard Input in the following format:

S

Output

Print the rating of the corresponding player in one line.


Sample Input 1

tourist

Sample Output 1

3858

At the start of this contest, the rating of tourist in the algorithm category is 3858.


Sample Input 2

semiexp

Sample Output 2

3481

At the start of this contest, the rating of semiexp in the algorithm category is 3481.

B - Majority

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

ある提案に対し、N 人の人が賛成か反対かを表明しています。なお、N は奇数です。

i \, (i = 1, 2, \dots, N) 番目の人の意見は文字列 S_i で表され、S_i = For のとき賛成しており、S_i = Against のとき反対しています。

過半数の人がこの提案に賛成しているかどうかを判定してください。

制約

  • N1 以上 99 以下の奇数
  • 全ての i = 1, 2, \dots, N に対し、S_i = For または S_i = Against

入力

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

N
S_1
S_2
\vdots
S_N

出力

N 人のうち過半数が提案に賛成しているならば Yes、そうでなければ No と出力せよ。


入力例 1

3
For
Against
For

出力例 1

Yes

提案に賛成している人数は 2 人であり、これは半数を超えているので Yes と出力します。


入力例 2

5
Against
Against
For
Against
For

出力例 2

No

提案に賛成している人数は 2 人であり、これは半数以下なので No と出力します。


入力例 3

1
For

出力例 3

Yes

Score : 100 points

Problem Statement

There are N people. Each of them agrees or disagrees with a proposal. Here, N is an odd number.

The i-th (i = 1, 2, \dots, N) person's opinion is represented by a string S_i: the person agrees if S_i = For and disagrees if S_i = Against.

Determine whether the majority agrees with the proposal.

Constraints

  • N is an odd number between 1 and 99, inclusive.
  • S_i = For or S_i = Against, for all i = 1, 2, \dots, N.

Input

The input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print Yes if the majority of the N people agree with the proposal; print No otherwise.


Sample Input 1

3
For
Against
For

Sample Output 1

Yes

The proposal is supported by two people, which is the majority, so Yes should be printed.


Sample Input 2

5
Against
Against
For
Against
For

Sample Output 2

No

The proposal is supported by two people, which is not the majority, so No should be printed.


Sample Input 3

1
For

Sample Output 3

Yes
C - Adjacency Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

N 頂点の単純無向グラフ G があり、グラフの頂点には 1,2,\ldots, N の番号が付けられています。

G の隣接行列 (A_{i,j}) が与えられます。すなわち、GA_{i,j} = 1 であるとき、またそのときに限り頂点 i と頂点 j を結ぶ辺を持ちます。

i = 1, 2, \ldots, N について、頂点 i と直接結ばれている頂点の番号を昇順に出力してください。

ただし、頂点 i と頂点 j が直接結ばれているとは、頂点 i と頂点 j を結ぶ辺が存在することをいいます。

制約

  • 2 \leq N \leq 100
  • A_{i,j} \in \lbrace 0,1 \rbrace
  • A_{i,i} = 0
  • A_{i,j} = A_{j,i}
  • 入力される値はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

出力

N 行出力せよ。 i 行目には頂点 i と直接結ばれている頂点の番号を昇順に空白区切りで出力せよ。


入力例 1

4
0 1 1 0
1 0 0 1
1 0 0 0
0 1 0 0

出力例 1

2 3
1 4
1
2

頂点 1 と直接結ばれている頂点は頂点 2, 3 です。したがって、1 行目には 2, 3 をこの順で出力します。

同様に、2 行目には 1, 4 をこの順に、3 行目には 1 を、4 行目には 2 を出力します。


入力例 2

2
0 0
0 0

出力例 2



G に辺が存在しないこともあります。


入力例 3

5
0 1 0 1 1
1 0 0 1 0
0 0 0 0 1
1 1 0 0 1
1 0 1 1 0

出力例 3

2 4 5
1 4
5
1 2 5
1 3 4

Score: 150 points

Problem Statement

There is a simple undirected graph G with N vertices labeled with numbers 1, 2, \ldots, N.

You are given the adjacency matrix (A_{i,j}) of G. That is, G has an edge connecting vertices i and j if and only if A_{i,j} = 1.

For each i = 1, 2, \ldots, N, print the numbers of the vertices directly connected to vertex i in ascending order.

Here, vertices i and j are said to be directly connected if and only if there is an edge connecting vertices i and j.

Constraints

  • 2 \leq N \leq 100
  • A_{i,j} \in \lbrace 0,1 \rbrace
  • A_{i,i} = 0
  • A_{i,j} = A_{j,i}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

Output

Print N lines. The i-th line should contain the numbers of the vertices directly connected to vertex i in ascending order, separated by a space.


Sample Input 1

4
0 1 1 0
1 0 0 1
1 0 0 0
0 1 0 0

Sample Output 1

2 3
1 4
1
2

Vertex 1 is directly connected to vertices 2 and 3. Thus, the first line should contain 2 and 3 in this order.

Similarly, the second line should contain 1 and 4 in this order, the third line should contain 1, and the fourth line should contain 2.


Sample Input 2

2
0 0
0 0

Sample Output 2



G may have no edges.


Sample Input 3

5
0 1 0 1 1
1 0 0 1 0
0 0 0 0 1
1 1 0 0 1
1 0 1 1 0

Sample Output 3

2 4 5
1 4
5
1 2 5
1 3 4
D - Append

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

空の数列 A があります。クエリが Q 個与えられるので、与えられた順に処理してください。
クエリは次の 2 種類のいずれかです。

  • 1 x: A の末尾に x を追加する。
  • 2 k: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。

制約

  • 1 \leq Q \leq 100
  • 1 種類目のクエリにおいて x1 \leq x \leq 10^9 を満たす整数
  • 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数

入力

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

クエリは以下の 2 つのいずれかの形式である。

1 x
2 k

出力

2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。


入力例 1

5
1 20
1 30
2 1
1 40
2 3

出力例 1

30
20
  • 最初 A は空である。
  • 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
  • 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
  • 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
  • 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
  • 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。

Score: 200 points

Problem Statement

You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:

  • 1 x: Append x to the end of A.
  • 2 k: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.

Constraints

  • 1 \leq Q \leq 100
  • In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
  • In the second type of query, k is a positive integer not greater than the current length of sequence A.

Input

The input is given from Standard Input in the following format:

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 x
2 k

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.


Sample Input 1

5
1 20
1 30
2 1
1 40
2 3

Sample Output 1

30
20
  • Initially, A is empty.
  • The first query appends 20 to the end of A, making A=(20).
  • The second query appends 30 to the end of A, making A=(20,30).
  • The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
  • The fourth query appends 40 to the end of A, making A=(20,30,40).
  • The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
E - Index × A(Continuous ver.)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

長さ MA の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。

注記

数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。

例えば (2, 3)(1, 2, 3)(1, 2, 3, 4) の連続部分列ですが、(1, 3)(3,2,1)(1, 2, 3, 4) の連続部分列ではありません。

制約

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 2
5 4 -1 8

出力例 1

15

B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。

B=(A_1,A_4) などを選ぶことができないことに注意してください。


入力例 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

出力例 2

31

Score : 300 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.

Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.

Notes

A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.

For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).

Constraints

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 2
5 4 -1 8

Sample Output 1

15

When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.

Note that you are not allowed to choose, for instance, B=(A_1,A_4).


Sample Input 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

Sample Output 2

31
F - Circular Playlist

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 曲からなるプレイリストがあり、曲には 1, \dots, N の番号が付けられています。
i の長さは A_i 秒です。

プレイリストを再生すると、曲 1、曲 2\ldots、曲 N の順に流れます。曲 N が流れ終わると、再び曲 1 から順に流れていきます。ある曲の途中で次の曲が流れることはなく、曲が流れ終わると、その瞬間に次の曲が流れ始めます。

プレイリストを再生してから T 秒後に流れているのはどの曲ですか?また、その曲が流れ始めてから何秒の時点ですか?
ただし、T 秒後ちょうどに曲が切り替わるような入力は与えられません。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • プレイリストを再生して T 秒後ちょうどに曲が切り替わることはない
  • 入力される値は全て整数

入力

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

N T
A_1 \ldots A_N

出力

プレイリストを再生してから T 秒後に流れている曲の番号と、その曲が流れ始めてから何秒たったかを表す整数を空白区切りで出力せよ。


入力例 1

3 600
180 240 120

出力例 1

1 60

プレイリストを再生してからの様子は次のようになります。

  • 0 秒後から 180 秒後まで曲 1 が流れる。
  • 180 秒後から 420 秒後まで曲 2 が流れる。
  • 420 秒後から 540 秒後まで曲 3 が流れる。
  • 540 秒後から 720 秒後まで曲 1 が流れる。
  • 720 秒後から 960 秒後まで曲 2 が流れる。
  • \qquad\vdots

600 秒後の時点で流れているのは曲 1 であり、流れ始めて 60 秒の時点です。


入力例 2

3 281
94 94 94

出力例 2

3 93

入力例 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

6 678912340

Score : 300 points

Problem Statement

We have a playlist with N songs numbered 1, \dots, N.
Song i lasts A_i seconds.

When the playlist is played, song 1, song 2, \ldots, and song N play in this order. When song N ends, the playlist repeats itself, starting from song 1 again. While a song is playing, the next song does not play; when a song ends, the next song starts immediately.

At exactly T seconds after the playlist starts playing, which song is playing? Also, how many seconds have passed since the start of that song?
There is no input where the playlist changes songs at exactly T seconds after it starts playing.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • The playlist does not change songs at exactly T seconds after it starts playing.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N T
A_1 \ldots A_N

Output

Print an integer representing the song that is playing at exactly T seconds after the playlist starts playing, and an integer representing the number of seconds that have passed since the start of that song, separated by a space.


Sample Input 1

3 600
180 240 120

Sample Output 1

1 60

When the playlist is played, the following happens. (Assume that it starts playing at time 0.)

  • From time 0 to time 180, song 1 plays.
  • From time 180 to time 420, song 2 plays.
  • From time 420 to time 540, song 3 plays.
  • From time 540 to time 720, song 1 plays.
  • From time 720 to time 960, song 2 plays.
  • \qquad\vdots

At time 600, song 1 is playing, and 60 seconds have passed since the start of that song.


Sample Input 2

3 281
94 94 94

Sample Output 2

3 93

Sample Input 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

6 678912340
G - All Assign Point Add

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N) が与えられます。

Q 個のクエリが与えられるので、順番にすべて処理してください。 q 番目 (1\leq q\leq Q) のクエリは以下の 3 つのいずれかの形式で、それぞれ次のようなクエリを表します。

  • 1\ x _ qA のすべての要素に x _ q を代入する。
  • 2\ i _ q\ x _ qA _ {i _ q}x _ q を加える。
  • 3\ i _ qA _ {i _ q} の値を出力する。

制約

  • 1 \leq N \leq 2\times10^5
  • 1 \leq Q \leq 2\times10^5
  • 0 \leq A _ i \leq 10^9\ (1\leq i\leq N)
  • q 番目 (1\leq q\leq Q) のクエリが 2 番目もしくは 3 番目の形式のとき、1 \leq i _ q \leq N
  • q 番目 (1\leq q\leq Q) のクエリが 1 番目もしくは 2 番目の形式のとき、0 \leq x _ q \leq 10^9
  • 3 番目の形式のクエリが存在する
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N
Q
\operatorname{query}_1
\operatorname{query}_2
\vdots
\operatorname{query}_Q

ただし、\operatorname{query}_qq 番目のクエリであり、1 x, 2 i x, 3 i の形式のいずれかで与えられる。

出力

\operatorname{query}_q3 番目の形式であるような q\ (1\leq q\leq Q) の個数を X として、X 行出力せよ。 j\ (1\leq j\leq X) 行目にはそのようなクエリのうち j 番目のクエリに対する答えを出力せよ。


入力例 1

5
3 1 4 1 5
6
3 2
2 3 4
3 3
1 1
2 3 4
3 3

出力例 1

1
8
5

はじめ、A=(3,1,4,1,5) です。 それぞれのクエリでは、以下のような処理が行われます。

  • A_2=1 なので、1 を出力します。
  • A_34 を加えます。A=(3,1,8,1,5) となります。
  • A_3=8 なので、8 を出力します。
  • A の要素すべてに 1 を代入します。A=(1,1,1,1,1) となります。
  • A_34 を加えます。A=(1,1,5,1,1) となります。
  • A_3=5 なので、5 を出力します。

入力例 2

1
1000000000
8
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
3 1

出力例 2

8000000000

A の要素の値が 32\operatorname{bit} 整数に収まらない可能性があることに注意してください。


入力例 3

10
1 8 4 15 7 5 7 5 8 0
20
2 7 0
3 7
3 8
1 7
3 3
2 4 4
2 4 9
2 10 5
1 10
2 4 2
1 10
2 3 1
2 8 11
2 3 14
2 1 9
3 8
3 8
3 1
2 6 5
3 7

出力例 3

7
5
7
21
21
19
10

Score : 400 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N.

Given Q queries, process all of them in order. The q-th (1\leq q\leq Q) query is in one of the following three formats, which represents the following queries:

  • 1\ x _ q: assign x_q to every element of A.
  • 2\ i _ q\ x _ q: add x_q to A _ {i _ q}.
  • 3\ i _ q: print the value of A _ {i _ q}.

Constraints

  • 1 \leq N \leq 2\times10^5
  • 1 \leq Q \leq 2\times10^5
  • 0 \leq A _ i \leq 10^9\ (1\leq i\leq N)
  • If the q-th (1\leq q\leq Q) query is in the second or third format, 1 \leq i _ q \leq N.
  • If the q-th (1\leq q\leq Q) query is in the first or second format, 0 \leq x _ q \leq 10^9.
  • There exists a query in the third format.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N
Q
\operatorname{query}_1
\operatorname{query}_2
\vdots
\operatorname{query}_Q

Here, \operatorname{query}_q denotes the q-th query, which is in one of following formats: 1 x, 2 i x, and 3 i.

Output

Print X lines, where X is the number of q's (1\leq q\leq Q) such that \operatorname{query}_q is in the third format. The j-th (1\leq j\leq X) line should contain the answer to the j-th such query.


Sample Input 1

5
3 1 4 1 5
6
3 2
2 3 4
3 3
1 1
2 3 4
3 3

Sample Output 1

1
8
5

Initially, A=(3,1,4,1,5). The queries are processed as follows:

  • A_2=1, so print 1.
  • Add 4 to A_3, making A=(3,1,8,1,5).
  • A_3=8, so print 8.
  • Assign 1 to every element of A, making A=(1,1,1,1,1).
  • Add 4 to A_3, making A=(1,1,5,1,1).
  • A_3=5, so print 5.

Sample Input 2

1
1000000000
8
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
3 1

Sample Output 2

8000000000

Note that the elements of A may not fit into a 32-bit integer type.


Sample Input 3

10
1 8 4 15 7 5 7 5 8 0
20
2 7 0
3 7
3 8
1 7
3 3
2 4 4
2 4 9
2 10 5
1 10
2 4 2
1 10
2 3 1
2 8 11
2 3 14
2 1 9
3 8
3 8
3 1
2 6 5
3 7

Sample Output 3

7
5
7
21
21
19
10
H - Apple Baskets on Circle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1,2,\ldots,N の番号がついた N 個のかごが円状に置かれています。
1\leq i \leq N-1 についてかご i の右隣にはかご i+1 があり、かご N の右隣にはかご 1 があります。

かご i の中には A_i 個りんごが入っています。

高橋君は最初かご 1 の前におり、以下の行動を繰り返します。

  • 目の前にあるかごの中にりんごがあれば 1 個かごから取り出して食べる。その後、りんごを食べたかどうかにかかわらず、右隣のかごの前に移動する。

高橋君がちょうど K 個のりんごを食べた時点で、各かごの中に残っているりんごの個数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{12}
  • 1 \leq K \leq 10^{12}
  • りんごは全部で K 個以上ある。すなわち、\sum_{i=1}^{N}A_i\geq K
  • 入力に含まれる値は全て整数である

入力

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

N K
A_1 A_2 \ldots A_N

出力

N 個の整数を空白区切りで出力せよ。
i 番目には、高橋君がちょうど K 個のりんごを食べた時点で、かご i の中に残っているりんごの個数を出力せよ。


入力例 1

3 3
1 3 0

出力例 1

0 1 0 

高橋君は次のように行動します。

  • 目の前にあるかご 1 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 2 の前に移動する。この時点で各かごの中のりんごの個数は (0,3,0) である。
  • 目の前にあるかご 2 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 3 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 3 の中にりんごはない。かご 1 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 1 の中にりんごはない。かご 2 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 2 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 3 の前に移動する。この時点で各かごの中のりんごの個数は (0,1,0) である。

入力例 2

2 1000000000000
1000000000000 1000000000000

出力例 2

500000000000 500000000000 

Score : 500 points

Problem Statement

There are N baskets numbered 1, 2, \ldots, N arranged in a circle.
For each 1\leq i \leq N-1, basket i+1 is to the immediate right of basket i, and basket 1 is to the immediate right of basket N.

Basket i now contains A_i apples.

Takahashi starts in front of basket 1 and repeats the following action.

  • If the basket he is facing contains an apple, take one and eat it. Then, regardless of whether he has eaten an apple now, go on to the next basket to the immediate right.

Find the number of apples remaining in each basket when Takahashi has eaten exactly K apples in total.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{12}
  • 1 \leq K \leq 10^{12}
  • There are at least K apples in total. That is, \sum_{i=1}^{N}A_i\geq K.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print N integers, with spaces in between.
The i-th integer should be the number of apples remaining in basket i when Takahashi has eaten exactly K apples in total.


Sample Input 1

3 3
1 3 0

Sample Output 1

0 1 0 

Takahashi will do the following.

  • Basket 1, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 2. Now, the baskets have 0,3,0 apples.
  • Basket 2, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 3. Now, the baskets have 0,2,0 apples.
  • Basket 3, which he is facing, contains no apple. Then, he goes on to basket 1. Now, the baskets have 0,2,0 apples.
  • Basket 1, which he is facing, contains no apple. Then, he goes on to basket 2. Now, the baskets have 0,2,0 apples.
  • Basket 2, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 3. Now, the baskets have 0,1,0 apple(s).

Sample Input 2

2 1000000000000
1000000000000 1000000000000

Sample Output 2

500000000000 500000000000 
I - Spices

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

スパイス屋には、スパイス 1 、スパイス 2\ldots 、スパイス 2^N-12^N-1 種類のスパイスがそれぞれ 1 個ずつ売られています。 i = 1, 2, \ldots, 2^N-1 について、スパイス i の値段は c_i 円です。 高橋君はそれらを好きなだけ買うことができます。

高橋君は帰宅後、買ったスパイスのうち 1 つ以上を選び、それらを組み合わせてカレーを作る予定です。
高橋君がスパイス A_1 、スパイス A_2\ldots、スパイス A_kk 個のスパイスを組み合わせると、完成するカレーの辛さは A_1 \oplus A_2 \oplus \cdots \oplus A_k になります。 ここで、\oplus はビットごとの排他的論理和を表します。

高橋君は、作るカレーの辛さを帰宅後の気分で決めたいので、とりあえず 1 以上 2^N-1 以下の任意の整数の辛さのカレーを作れるように、購入するスパイスの組み合わせを決定します。高橋君が支払う金額としてあり得る最小値を出力してください。

制約

  • 2 \leq N \leq 16
  • 1 \leq c_i \leq 10^9
  • 入力はすべて整数

入力

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

N
c_1 c_2 \ldots c_{2^N-1}

出力

高橋君が支払う金額としてあり得る最小値を出力せよ。


入力例 1

2
4 5 3

出力例 1

7

高橋君がスパイス 1 とスパイス 3 を買うと、下記の通り、1 以上 3 以下の任意の整数の辛さのカレーを作ることができます。

  • 辛さ 1 のカレーを作るには、スパイス 1 のみを使い、
  • 辛さ 2 のカレーを作るには、スパイス 1 とスパイス 3 を組み合わせ、
  • 辛さ 3 のカレーを作るには、スパイス 3 のみを使えば良いです。

このとき高橋君が支払う金額は c_1 + c_3 = 4 + 3 = 7 円であり、これが高橋君が支払う金額としてあり得る最小値です。


入力例 2

4
9 7 9 7 10 4 3 9 4 8 10 5 6 3 8

出力例 2

15

Score : 500 points

Problem Statement

The shop supaisu-ya sells 2^N-1 kinds of spices: Spice 1, Spice 2, \ldots, Spice 2^N-1. One pack of each spice is in stock. For each i = 1, 2, \ldots, 2^N-1, Spice i costs c_i yen. Takahashi can buy any of these spices.

He plans to make curry after getting home by choosing one or more of the bought spices and mixing them.
If k spices, Spice A_1, Spice A_2, \ldots, Spice A_k, are mixed, the hotness of the resulting curry is A_1 \oplus A_2 \oplus \cdots \oplus A_k, where \oplus denotes the bitwise XOR.

Takahashi wants to decide the hotness of the curry based on his feeling after getting home. For now, he will buy a set of spices that allows him to make curry of any hotness from 1 through 2^N-1. Print the minimum possible amount of money Takahashi pays.

Constraints

  • 2 \leq N \leq 16
  • 1 \leq c_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
c_1 c_2 \ldots c_{2^N-1}

Output

Print the minimum possible amount of money Takahashi pays.


Sample Input 1

2
4 5 3

Sample Output 1

7

If Takahashi buys Spice 1 and 3, he can make curry of any hotness from 1 through 3, as follows.

  • To make curry of hotness 1, use just Spice 1.
  • To make curry of hotness 2, mix Spice 1 and Spice 3.
  • To make curry of hotness 3, use just Spice 3.

Here, Takahashi pays c_1 + c_3 = 4 + 3 = 7 yen, which is the minimum possible amount he pays.


Sample Input 2

4
9 7 9 7 10 4 3 9 4 8 10 5 6 3 8

Sample Output 2

15