A - Six Characters

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

英小文字からなる文字列 S が与えられます。 S の長さは 1 以上かつ 3 以下です。

S を繰り返して得られる文字列であって、長さが 6 のものを出力してください。

本問題の制約下で、そのような文字列はただ一つ存在することが示せます。

制約

  • S は英小文字からなる長さ 1 以上 3 以下の文字列

入力

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

S

出力

答えとなる長さ 6 の文字列を出力せよ。


入力例 1

abc

出力例 1

abcabc

S = abc を繰り返してできる文字列として、abc 、abcabc 、abcabcabc 、abcabcabcabc などがあります。 そのうち、長さが 6 のものは abcabc です。よって、abcabc と出力します。


入力例 2

zz

出力例 2

zzzzzz

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English characters. The length of S is between 1 and 3, inclusive.

Print the string of length 6 that is a repetition of S.

It can be shown that there uniquely exists such a string under the Constraints of this problem.

Constraints

  • S is a string consisting of lowercase English characters of length between 1 and 3, inclusive.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer string, which is of length 6.


Sample Input 1

abc

Sample Output 1

abcabc

These are strings that are repetitions of S = abc: abc, abcabc, abcabcabc, abcabcabcabc, and so on. Among them, abcabc has the length of 6, so abcabc should be printed.


Sample Input 2

zz

Sample Output 2

zzzzzz
B - A Substring

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

英小文字からなる N 文字の文字列 S が与えられます。

S の先頭から A 文字、末尾から B 文字取り除いた N-A-B 文字の文字列を出力してください。

制約

  • 1\le N\le20
  • 0\le A
  • 0\le B
  • A+B\lt N
  • S は英小文字からなる N 文字の文字列
  • N,A,B はすべて整数

入力

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

N A B
S

出力

S の先頭から A 文字、末尾から B 文字取り除いた文字列を出力せよ。


入力例 1

7 1 3
atcoder

出力例 1

tco

atcoder の先頭から 1 文字、末尾から 3 文字取り除くと tco になります。


入力例 2

1 0 0
a

出力例 2

a

出力すべき文字列が S と等しい場合もあります。


入力例 3

20 4 8
abcdefghijklmnopqrst

出力例 3

efghijkl

Score : 100 points

Problem Statement

You are given an N-character string S consisting of lowercase English letters.

Output the string of N-A-B characters obtained by removing the first A characters and the last B characters from S.

Constraints

  • 1\le N\le20
  • 0\le A
  • 0\le B
  • A+B\lt N
  • S is a string of N characters consisting of lowercase English letters.
  • N, A, and B are all integers.

Input

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

N A B
S

Output

Output the string obtained by removing the first A characters and the last B characters from S.


Sample Input 1

7 1 3
atcoder

Sample Output 1

tco

Removing the first one character and the last three characters from atcoder gives tco.


Sample Input 2

1 0 0
a

Sample Output 2

a

The string to be output may be equal to S.


Sample Input 3

20 4 8
abcdefghijklmnopqrst

Sample Output 3

efghijkl
C - Vacation Together

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

1 から N までの番号がついた N 人の人がいます。
N 人の人の今後 D 日間の予定が与えられます。人 i の予定は長さ D の文字列 S_i で表されて、S_i の j 文字目が o ならば j 日目は暇であることを、x ならばそうでないことを意味します。

D 日間のうち全員が暇であるような 連続する 何日かを選ぶことを考えます。
選べる日数は最大で何日ですか?ただし、選べる日が存在しない場合は 0 日と答えてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq D \leq 100
  • N, D は整数
  • S_i は o と x からなる長さ D の文字列

入力

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

N D
S_1
S_2
\vdots
S_N

出力

選べる日数の最大値を出力せよ。選べる日が存在しない場合は 0 を出力せよ。


入力例 1

3 5
xooox
oooxx
oooxo

出力例 1

2

2 日目と 3 日目は全員が暇な日なので選ぶことができます。
この 2 日間を選ぶと、連続する日にちを選ぶ方法の中で日数を最大にすることができます。


入力例 2

3 3
oxo
oxo
oxo

出力例 2

1

選ぶ日にちは連続している必要があるのに注意してください。(1 日目と 3 日目は全員が暇な日なので選ぶことができますが、この 2 つを同時に選ぶことはできません)


入力例 3

3 3
oox
oxo
xoo

出力例 3

0

選べる日が存在しない場合は 0 を出力してください。


入力例 4

1 7
ooooooo

出力例 4

7

入力例 5

5 15
oxooooooooooooo
oxooxooooooooox
oxoooooooooooox
oxxxooooooxooox
oxooooooooxooox

出力例 5

5

Score : 200 points

Problem Statement

There are N people numbered 1 to N.
You are given their schedule for the following D days. The schedule for person i is represented by a string S_i of length D. If the j-th character of S_i is o, person i is free on the j-th day; if it is x, they are occupied that day.

From these D days, consider choosing some consecutive days when all the people are free.
How many days can be chosen at most? If no day can be chosen, report 0.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq D \leq 100
  • N and D are integers.
  • S_i is a string of length D consisting of o and x.

Input

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

N D
S_1
S_2
\vdots
S_N

Output

Print the maximum number of days that can be chosen, or 0 if no day can be chosen.


Sample Input 1

3 5
xooox
oooxx
oooxo

Sample Output 1

2

All the people are free on the second and third days, so we can choose them.
Choosing these two days will maximize the number of days among all possible choices.


Sample Input 2

3 3
oxo
oxo
oxo

Sample Output 2

1

Note that the chosen days must be consecutive. (All the people are free on the first and third days, so we can choose either of them, but not both.)


Sample Input 3

3 3
oox
oxo
xoo

Sample Output 3

0

Print 0 if no day can be chosen.


Sample Input 4

1 7
ooooooo

Sample Output 4

7

Sample Input 5

5 15
oxooooooooooooo
oxooxooooooooox
oxoooooooooooox
oxxxooooooxooox
oxooooooooxooox

Sample Output 5

5
D - Pasta

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

高橋君の家には N 本の麺からなるパスタがあり、i 本目の麺の長さは A_i です。
高橋君はこれから M 日間の食事計画を立てており、 i 日目にはパスタの麺のうち長さがちょうど B_i であるようなものを 1 本選び、食べようと考えています。 もし、1 日目から M 日目の間に 1 日でもそのような麺が無い日があれば、食事計画は失敗となります。 また、同じ麺を複数の日に食べることはできません。

高橋君が食事計画を最後まで実行することは可能ですか?

制約

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数である。

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

高橋君が食事計画を最後まで実行できる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3 2
1 1 3
3 1

出力例 1

Yes

1 日目に 3 本目の麺を、2 日目に 1 本目の麺を食べれば良いので、高橋君の食事計画は実行可能です。


入力例 2

1 1
1000000000
1

出力例 2

No

長さがちょうど 1 の麺が存在する必要があります。


入力例 3

5 2
1 2 3 4 5
5 5

出力例 3

No

長さが 5 の麺は 1 本しか存在しないため、2 日目に食事をとる事が出来ません。

Score : 200 points

Problem Statement

There is pasta consisting of N noodles at Takahashi's home. The length of the i-th noodle is A_i.
Takahashi has a meal plan for the next M days. On the i-th day, he is going to choose a pasta noodle of length exactly B_i and eat it. If no such noodle is available on any day, his plan fails. Additionally, he cannot eat the same noodle on multiple days.

Can Takahashi accomplish his meal plan?

Constraints

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

If Takahashi can accomplish his meal plan, print Yes; otherwise, print No.


Sample Input 1

3 2
1 1 3
3 1

Sample Output 1

Yes

He can eat the 3-rd noodle on the 1-st day and the 1-st noodle on the 2-nd day, so his meal plan is feasible.


Sample Input 2

1 1
1000000000
1

Sample Output 2

No

A noodle of length exactly 1 is needed.


Sample Input 3

5 2
1 2 3 4 5
5 5

Sample Output 3

No

Since there are only 1 noodle of length 5, he cannot have a meal on the 2-nd day.

E - Adjacent Sums (easy)

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

※ C 問題の問題文は E 問題と同じです。赤字で示された制約のみが異なります。

0 以上 M-1 以下の整数からなる整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_{N-1}) が与えられます。 A,B の長さはそれぞれ N,N-1 です。
A に対して以下の操作を好きな回数行うことができます。

  • 1 以上 N 以下の整数 i を 1 つ選び、A_i に 1 を加える。

以下の条件を満たすようにするために必要な操作回数の最小値を求めてください。
なお、問題の制約下では、必ず条件を満たすようにできることが証明できます。

  • i=1,2,\dots,N-1 について、A_i+A_{i+1} を M で割った余りは B_i に等しい。

制約

  • 2 \leq N \leq 2 \times 10^5
  • M=2
  • 0 \leq A_i \leq M-1
  • 0 \leq B_i \leq M-1
  • 入力される値はすべて整数

入力

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

N M  
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_{N-1}

出力

答えを 1 行で出力せよ。


入力例 1

3 2
1 1 1
1 1

出力例 1

1

1 回目の操作で i=2 を選ぶと、A=(1,2,1) となります。
A_1+A_2=1+2=3,\;A_2+A_3=2+1=3 なので、条件を満たします。
また、A=(1,1,1) は条件を満たさないため、答えは 1 です。


入力例 2

2 2
1 1
0

出力例 2

0

入力例 3

10 2
0 0 0 1 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0

出力例 3

4

Score : 300 points

Problem Statement

The problem statement of Problem C is the same as Problem E. Only the constraint shown in red differ.

You are given integer sequences A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_{N-1}) consisting of integers between 0 and M-1, inclusive. The lengths of A and B are N and N-1, respectively.
You can perform the following operation on A any number of times.

  • Choose an integer i with 1 \leq i \leq N, and add 1 to A_i.

Find the minimum number of operations required to satisfy the following condition.
It can be proved that the condition can always be satisfied under the constraints of this problem.

  • For i=1,2,\dots,N-1, the remainder when A_i+A_{i+1} is divided by M equals B_i.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • M=2
  • 0 \leq A_i \leq M-1
  • 0 \leq B_i \leq M-1
  • All input values are integers.

Input

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

N M  
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_{N-1}

Output

Output the answer on one line.


Sample Input 1

3 2
1 1 1
1 1

Sample Output 1

1

If we choose i=2 for the first operation, we get A=(1,2,1).
Since A_1+A_2=1+2=3 and A_2+A_3=2+1=3, the condition is satisfied.
A=(1,1,1) does not satisfy the condition, so the answer is 1.


Sample Input 2

2 2
1 1
0

Sample Output 2

0

Sample Input 3

10 2
0 0 0 1 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0

Sample Output 3

4
F - Word Ladder

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

英小文字からなる文字列 S, T が与えられます。ここで、S と T の長さは等しいです。

X を空の配列とし、以下の操作を S と T が等しくなるまで繰り返します。

  • S の文字を 1 文字書き換え、X の末尾に S を追加する。

こうして得られる文字列の配列 X のうち要素数最小のものを求めてください。要素数最小のものが複数考えられる場合は、そのうち辞書順最小のものを求めてください。

文字列の配列の辞書順とは

長さ N の文字列 S = S_1 S_2 \ldots S_N が長さ N の文字列 T = T_1 T_2 \ldots T_N より辞書順で小さいとは、ある整数 1 \leq i \leq N が存在して下記の 2 つがともに成り立つことをいいます。

  • S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
  • S_i が T_i よりアルファベット順で早い。

要素数 M の文字列の配列 X = (X_1,X_2,\ldots,X_M) が要素数 M の文字列の配列 Y = (Y_1,Y_2,\ldots,Y_M) より辞書順で小さいとは、ある整数 1 \leq j \leq M が存在して下記の 2 つがともに成り立つことをいいます。

  • (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
  • X_j が Y_j より辞書順で小さい。

制約

  • S, T は英小文字からなる長さ 1 以上 100 以下の文字列
  • S と T の長さは等しい

入力

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

S
T

出力

答えの要素数を M として M + 1 行出力せよ。

1 行目には M の値を出力せよ。

i + 1 (1 \leq i \leq M) 行目には答えの i 番目の要素を出力せよ。


入力例 1

adbe
bcbc

出力例 1

3
acbe
acbc
bcbc

はじめ、S = adbe です。

以下のように操作することで、X = ( acbe , acbc , bcbc ) とすることができます。

  • S を acbe に書き換え、X の末尾に acbe を追加する。

  • S を acbc に書き換え、X の末尾に acbc を追加する。

  • S を bcbc に書き換え、X の末尾に bcbc を追加する。


入力例 2

abcde
abcde

出力例 2

0

入力例 3

afwgebrw
oarbrenq

出力例 3

8
aawgebrw
aargebrw
aarbebrw
aarbebnw
aarbebnq
aarbeenq
aarbrenq
oarbrenq

Score : 300 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters. Here, S and T have equal lengths.

Let X be an empty array, and repeat the following operation until S equals T:

  • Change one character in S, and append S to the end of X.

Find the array of strings X with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.

What is lexicographical order on arrays of strings?

A string S = S_1 S_2 \ldots S_N of length N is lexicographically smaller than a string T = T_1 T_2 \ldots T_N of length N if there exists an integer 1 \leq i \leq N such that both of the following are satisfied:

  • S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
  • S_i comes earlier than T_i in alphabetical order.

An array of strings X = (X_1,X_2,\ldots,X_M) with M elements is lexicographically smaller than an array of strings Y = (Y_1,Y_2,\ldots,Y_M) with M elements if there exists an integer 1 \leq j \leq M such that both of the following are satisfied:

  • (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
  • X_j is lexicographically smaller than Y_j.

Constraints

  • S and T are strings consisting of lowercase English letters with length between 1 and 100, inclusive.
  • The lengths of S and T are equal.

Input

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

S
T

Output

Let M be the number of elements in the desired array. Print M + 1 lines.

The first line should contain the value of M.

The i + 1-th line (1 \leq i \leq M) should contain the i-th element of the array.


Sample Input 1

adbe
bcbc

Sample Output 1

3
acbe
acbc
bcbc

Initially, S = adbe.

We can obtain X = ( acbe , acbc , bcbc ) by performing the following operations:

  • Change S to acbe and append acbe to the end of X.

  • Change S to acbc and append acbc to the end of X.

  • Change S to bcbc and append bcbc to the end of X.


Sample Input 2

abcde
abcde

Sample Output 2

0

Sample Input 3

afwgebrw
oarbrenq

Sample Output 3

8
aawgebrw
aargebrw
aarbebrw
aarbebnw
aarbebnq
aarbeenq
aarbrenq
oarbrenq
G - LR insertion

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

1 個の 0 のみからなる数列 A=(0) があります。
また、L と R のみからなる長さ N の文字列 S=s_1s_2\ldots s_N が与えられます。

i=1,2,\ldots ,N の順番で、次の操作を行います。

  • s_i が L のとき、A 内にある i-1 のすぐ左に i を挿入する
  • s_i が R のとき、A 内にある i-1 のすぐ右に i を挿入する

最終的な A を求めてください。

制約

  • 1\leq N \leq 5\times 10^5
  • N は整数である
  • |S| = N
  • s_i は L か R のいずれかである

入力

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

N
S

出力

最終的な A を空白区切りで出力せよ。


入力例 1

5
LRRLR

出力例 1

1 2 4 5 3 0

はじめ、A=(0) です。
s_1 が L なので、A=(1,0) となります。
s_2 が R なので、A=(1,2,0) となります。
s_3 が R なので、A=(1,2,3,0) となります。
s_4 が L なので、A=(1,2,4,3,0) となります。
s_5 が R なので、A=(1,2,4,5,3,0) となります。


入力例 2

7
LLLLLLL

出力例 2

7 6 5 4 3 2 1 0

Score : 400 points

Problem Statement

There is a sequence that contains one 0, A=(0).
Additionally, you are given a string of length N, S=s_1s_2\ldots s_N, consisting of L and R.

For each i=1, 2, \ldots, N in this order, the following will be done.

  • If s_i is L, insert i to the immediate left of i-1 in A.
  • If s_i is R, insert i to the immediate right of i-1 in A.

Find the final contents of A.

Constraints

  • 1\leq N \leq 5\times 10^5
  • N is an integer.
  • |S| = N
  • s_i is L or R.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final contents of A, separated by spaces.


Sample Input 1

5
LRRLR

Sample Output 1

1 2 4 5 3 0

Initially, A=(0).
S_1 is L, which makes it A=(1,0).
S_2 is R, which makes it A=(1,2,0).
S_3 is R, which makes it A=(1,2,3,0).
S_4 is L, which makes it A=(1,2,4,3,0).
S_5 is R, which makes it A=(1,2,4,5,3,0).


Sample Input 2

7
LLLLLLL

Sample Output 2

7 6 5 4 3 2 1 0
H - Complete Binary Tree

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450 点

問題文

1 から N までの番号が付けられた N 頂点からなる木があります。 各 i\ (2 \leq i \leq N) について、頂点 i と頂点 \lfloor \frac{i}{2} \rfloor を結ぶ辺が張られています。 逆に、これら以外の辺は存在しません。

この木において、頂点 X との距離が K である頂点の数を求めてください。 ただし、2 頂点 u,v の距離は、頂点 u,v を結ぶ単純パスに含まれる辺の個数として定義されます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 10^5
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq N
  • 0\leq K \leq N-1
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。 ここで、\mathrm{test}_i は i 番目のテストケースを意味する。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N X K 

出力

T 行出力せよ。

i\ (1 \leq i \leq T) 行目には、i 番目のテストケースに対する答えを整数として出力せよ。


入力例 1

5
10 2 0
10 2 1
10 2 2
10 2 3
10 2 4

出力例 1

1
3
4
2
0

N=10 のとき、木は以下の図のようになります。

このとき、

  • 頂点 2 との距離が 0 である頂点は 2 の 1 つです。
  • 頂点 2 との距離が 1 である頂点は 1,4,5 の 3 つです。
  • 頂点 2 との距離が 2 である頂点は 3,8,9,10 の 4 つです。
  • 頂点 2 との距離が 3 である頂点は 6,7 の 2 つです。
  • 頂点 2 との距離が 4 である頂点は存在しません。

入力例 2

10
822981260158260522 52 20
760713016476190629 2314654 57
1312150450968417 1132551176249851 7
1000000000000000000 1083770654 79
234122432773361868 170290518806790 23
536187734191890310 61862 14
594688604155374934 53288633578 39
1000000000000000000 120160810 78
89013034180999835 14853481725739 94
463213054346948152 825589 73

出力例 2

1556480
140703128616960
8
17732923532771328
65536
24576
2147483640
33776997205278720
7881299347898368
27021597764222976

Score : 450 points

Problem Statement

There is a tree with N vertices numbered 1 to N. For each i\ (2 \leq i \leq N), there is an edge connecting vertex i and vertex \lfloor \frac{i}{2} \rfloor. There are no other edges.

In this tree, find the number of vertices whose distance from vertex X is K. Here, the distance between two vertices u and v is defined as the number of edges in the simple path connecting vertices u and v.

You have T test cases to solve.

Constraints

  • 1\leq T \leq 10^5
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq N
  • 0\leq K \leq N-1
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i represents the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N X K 

Output

Print T lines.

The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case as an integer.


Sample Input 1

5
10 2 0
10 2 1
10 2 2
10 2 3
10 2 4

Sample Output 1

1
3
4
2
0

The tree for N=10 is shown in the following figure.

Here,

  • There is 1 vertex, 2, whose distance from vertex 2 is 0.
  • There are 3 vertices, 1,4,5, whose distance from vertex 2 is 1.
  • There are 4 vertices, 3,8,9,10, whose distance from vertex 2 is 2.
  • There are 2 vertices, 6,7, whose distance from vertex 2 is 3.
  • There are no vertices whose distance from vertex 2 is 4.

Sample Input 2

10
822981260158260522 52 20
760713016476190629 2314654 57
1312150450968417 1132551176249851 7
1000000000000000000 1083770654 79
234122432773361868 170290518806790 23
536187734191890310 61862 14
594688604155374934 53288633578 39
1000000000000000000 120160810 78
89013034180999835 14853481725739 94
463213054346948152 825589 73

Sample Output 2

1556480
140703128616960
8
17732923532771328
65536
24576
2147483640
33776997205278720
7881299347898368
27021597764222976
I - Clearance

実行時間制限: 5 sec / メモリ制限: 1024 MiB

配点 : 525 点

問題文

AtCoder 社のオンラインショップでは現在 N 個の商品を取り扱っており、商品 i の在庫は残り A_i 個です。

以下の Q 件の注文を順に処理してください。そのうち i 件目は次の通りです。

  • 商品 l_i,l_i+1,\dots,r_i を k_i 個ずつ買う。但し、 k_i 個未満の商品はあるだけ全て買う。この注文で買われた商品の個数の合計を答えよ。

i<Q について、 i 件目の注文で買われた商品の在庫を減らした状態で i+1 件目の注文に進むことに注意してください。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^{15}
  • 1 \le Q \le 3 \times 10^5
  • 1 \le l_i \le r_i \le N
  • 1 \le k_i \le 10^9

入力

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

N
A_1 A_2 \dots A_N
Q
l_1 r_1 k_1
l_2 r_2 k_2
\vdots
l_Q r_Q k_Q

出力

Q 行出力せよ。
そのうち i 行目には、 i 件目の注文で買われた商品の個数の合計を答えよ。


入力例 1

6
2 6 4 5 7 5
5
1 6 1
3 5 4
4 4 1
2 5 1
1 6 100

出力例 1

6
11
0
2
10

この入力には 5 件の注文が含まれます。

  • はじめ、各商品の在庫は (商品 1 から順に) 2,6,4,5,7,5 個です。
  • 1 件目の注文は l_1 = 1, r_1 = 6, k_1 = 1 です。
    • この注文で各商品は 1,1,1,1,1,1 個、合計 6 個買われます。
    • その後、各商品の在庫は 1,5,3,4,6,4 個になります。
  • 2 件目の注文は l_2 = 3, r_2 = 5, k_2 = 4 です。
    • この注文で各商品は 0,0,3,4,4,0 個、合計 11 個買われます。
    • その後、各商品の在庫は 1,5,0,0,2,4 個になります。
  • 3 件目の注文は l_3 = 4, r_3 = 4, k_3 = 1 です。
    • この注文で各商品は 0,0,0,0,0,0 個、合計 0 個買われます。
    • その後、各商品の在庫は 1,5,0,0,2,4 個になります。
  • 4 件目の注文は l_4 = 2, r_4 = 5, k_4 = 1 です。
    • この注文で各商品は 0,1,0,0,1,0 個、合計 2 個買われます。
    • その後、各商品の在庫は 1,4,0,0,1,4 個になります。
  • 5 件目の注文は l_5 = 1, r_5 = 6, k_5 = 100 です。
    • この注文で各商品は 1,4,0,0,1,4 個、合計 10 個買われます。
    • その後、各商品の在庫は 0,0,0,0,0,0 個になります。

Score : 525 points

Problem Statement

AtCoder Inc.'s online shop currently handles N products, and the stock of product i is A_i units remaining.

Process the following Q orders in order. The i-th order is as follows:

  • Buy k_i units each of products l_i,l_i+1,\dots,r_i. For products with fewer than k_i units, buy all available units. Report the total number of products bought in this order.

Note that for i<Q, the stock of products bought in the i-th order is reduced before proceeding to the (i+1)-th order.

Constraints

  • All input values are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^{15}
  • 1 \le Q \le 3 \times 10^5
  • 1 \le l_i \le r_i \le N
  • 1 \le k_i \le 10^9

Input

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

N
A_1 A_2 \dots A_N
Q
l_1 r_1 k_1
l_2 r_2 k_2
\vdots
l_Q r_Q k_Q

Output

Output Q lines.
The i-th line should contain the total number of products bought in the i-th order.


Sample Input 1

6
2 6 4 5 7 5
5
1 6 1
3 5 4
4 4 1
2 5 1
1 6 100

Sample Output 1

6
11
0
2
10

This input contains 5 orders.

  • Initially, the stocks of the products are (from product 1 onward) 2,6,4,5,7,5 units.
  • The first order is l_1 = 1, r_1 = 6, k_1 = 1.
    • In this order, 1,1,1,1,1,1 units of the products are bought, for a total of 6 units.
    • After this, the stocks of the products become 1,5,3,4,6,4 units.
  • The second order is l_2 = 3, r_2 = 5, k_2 = 4.
    • In this order, 0,0,3,4,4,0 units of the products are bought, for a total of 11 units.
    • After this, the stocks of the products become 1,5,0,0,2,4 units.
  • The third order is l_3 = 4, r_3 = 4, k_3 = 1.
    • In this order, 0,0,0,0,0,0 units of the products are bought, for a total of 0 units.
    • After this, the stocks of the products become 1,5,0,0,2,4 units.
  • The fourth order is l_4 = 2, r_4 = 5, k_4 = 1.
    • In this order, 0,1,0,0,1,0 units of the products are bought, for a total of 2 units.
    • After this, the stocks of the products become 1,4,0,0,1,4 units.
  • The fifth order is l_5 = 1, r_5 = 6, k_5 = 100.
    • In this order, 1,4,0,0,1,4 units of the products are bought, for a total of 10 units.
    • After this, the stocks of the products become 0,0,0,0,0,0 units.