Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
髙橋君はサッカーの試合で N 回ペナルティキックを蹴ります。
髙橋君は i 回目のペナルティキックでは、i が 3 の倍数の場合は失敗しそれ以外の場合は成功します。
髙橋君がペナルティキックを蹴ったときの結果を出力してください。
制約
- 1 \leq N \leq 100
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
髙橋君のペナルティキックの結果を表す長さ N の文字列を出力せよ。結果を表す文字列の i (1 \leq i \leq N) 文字目は髙橋君が i 回目のペナルティキックで成功した場合は o 、失敗した場合は x とする。
入力例 1
7
出力例 1
ooxooxo
髙橋君は 3 回目と 6 回目のペナルティキックに失敗するので、3 文字目と 6 文字目が x となります。
入力例 2
9
出力例 2
ooxooxoox
Score: 100 points
Problem Statement
Takahashi will have N penalty kicks in a soccer match.
For the i-th penalty kick, he will fail if i is a multiple of 3, and succeed otherwise.
Print the results of his penalty kicks.
Constraints
- 1 \leq N \leq 100
- All inputs are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print a string of length N representing the results of Takahashi's penalty kicks. The i-th character (1 \leq i \leq N) should be o if Takahashi succeeds in the i-th penalty kick, and x if he fails.
Sample Input 1
7
Sample Output 1
ooxooxo
Takahashi fails the third and sixth penalty kicks, so the third and sixth characters will be x.
Sample Input 2
9
Sample Output 2
ooxooxoox
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は N 歳の誕生日を迎えました。この時の彼の身長は T cmです。
また、以下のことが分かっています。
- 高橋君は 0 歳の誕生日(生まれた当日)から X 歳の誕生日までの間、毎年身長が D cmずつ伸びた。より厳密に書くと、i=1,2,\ldots,X それぞれに対し、i-1 歳の誕生日から i 歳の誕生日までの間に身長が D cm伸びた。
- 高橋君は X 歳の誕生日から N 歳の誕生日までの間、身長が変化していない。
高橋君の M 歳の誕生日の時の身長が何cmだったかを求めてください。
制約
- 0 \leq M \lt N \leq 100
- 1 \leq X \leq N
- 1 \leq T \leq 200
- 1 \leq D \leq 100
- 高橋君の 0 歳の誕生日の時の身長は 1 cm以上である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X T D
出力
答えを整数として出力せよ。
入力例 1
38 20 17 168 3
出力例 1
168
この例では、高橋君の 38 歳の誕生日の時の身長が 168 cmです。また、17 歳の誕生日から 38 歳の誕生日までの間、身長が変化していません。
このことから、20 歳の誕生日の時の身長は 168 cmだったと言え、これが答えになります。
入力例 2
1 0 1 3 2
出力例 2
1
この例において、高橋君は 0(=M) 歳の誕生日の時の身長が 1 cmで、1(=N) 歳の誕生日の時の身長が 3(=T) cmです。
入力例 3
100 10 100 180 1
出力例 3
90
Score : 100 points
Problem Statement
Takahashi had his N-th birthday, when he was T centimeters tall.
Additionally, we know the following facts:
- In each year between Takahashi's birth (0-th birthday) and his X-th birthday, his height increased by D centimeters. More formally, for each i = 1, 2, \ldots, X, his height increased by D centimeters between his (i-1)-th birthday and his i-th birthday.
- Between Takahashi's X-th birthday and his N-th birthday, his height did not change.
Find Takahashi's height on his M-th birthday, in centimeters.
Constraints
- 0 \leq M \lt N \leq 100
- 1 \leq X \leq N
- 1 \leq T \leq 200
- 1 \leq D \leq 100
- Takahashi was at least 1 centimeter tall at his birth.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M X T D
Output
Print the answer as an integer.
Sample Input 1
38 20 17 168 3
Sample Output 1
168
In this sample, Takahashi was 168 centimeters tall on his 38-th birthday. Also, his height did not change between his 17-th birthday and 38-th birthday.
From these facts, we find that he was 168 centimeters tall on his 20-th birthday, so the answer is 168.
Sample Input 2
1 0 1 3 2
Sample Output 2
1
In this sample, Takahashi was 1 centimeter tall on his 0(=M)-th birthday and 3(=T) centimeters tall on his 1(=N)-st birthday.
Sample Input 3
100 10 100 180 1
Sample Output 3
90
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英大文字と数字からなる文字列 S が与えられるので、S が以下の条件を満たすか判定してください。
- S は次の文字または文字列をこの順番で連結して得られる。
- 一文字の英大文字
- 100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列
- 一文字の英大文字
制約
- S は英大文字と数字からなる
- S の長さは 1 以上 10 以下
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が問題文中の条件を満たすなら Yes と、満たさないなら No と出力せよ。
入力例 1
Q142857Z
出力例 1
Yes
S は Q、142857、Z をこの順に連結して得られます。
Q、Z は英大文字であり、142857 は 100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列なので、S は条件を満たします。
入力例 2
AB912278C
出力例 2
No
AB は一文字の英大文字ではないため、S は条件を満たしません。
入力例 3
X900000
出力例 3
No
S の末尾の一文字が英大文字ではないため、S は条件を満たしません。
入力例 4
K012345K
出力例 4
No
012345 は 100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列ではないため、S は条件を満たしません。
Score : 200 points
Problem Statement
You are given a string S consisting of uppercase English letters and digits. Determine whether S satisfies the following condition.
- S is a concatenation of the following characters and string in the order listed.
- An uppercase English letter
- A string of length 6 that is a decimal representation of an integer between 100000 and 999999, inclusive
- An uppercase English letter
Constraints
- S consists of uppercase English letters and digits.
- The length of S is between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
If S satisfies the condition in the problem statement, print Yes; otherwise, print No.
Sample Input 1
Q142857Z
Sample Output 1
Yes
S is a concatenation of Q, 142857, and Z in this order.
Q and Z are uppercase English letters, and 142857 is a string of length 6 that is a decimal representation of an integer between 100000 and 999999, so S satisfies the condition.
Sample Input 2
AB912278C
Sample Output 2
No
AB is not an uppercase English letter, so S does not satisfy the condition.
Sample Input 3
X900000
Sample Output 3
No
The last character of S is not an uppercase English letter, so S does not satisfy the condition.
Sample Input 4
K012345K
Sample Output 4
No
012345 is not a string of length 6 that is a decimal representation of an integer between 100000 and 999999, so S does not satisfy the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, \dots, N の番号が付けられており、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
以下の条件を全て満たす整数 a, b, c の組の総数を求めてください。
- 1 \leq a \lt b \lt c \leq N
- 頂点 a と頂点 b を結ぶ辺が存在する。
- 頂点 b と頂点 c を結ぶ辺が存在する。
- 頂点 c と頂点 a を結ぶ辺が存在する。
制約
- 3 \leq N \leq 100
- 1 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 \vdots U_M V_M
出力
答えを出力せよ。
入力例 1
5 6 1 5 4 5 2 3 1 4 3 5 2 5
出力例 1
2
(a, b, c) = (1, 4, 5), (2, 3, 5) が条件を満たします。
入力例 2
3 1 1 2
出力例 2
0
入力例 3
7 10 1 7 5 7 2 5 3 6 4 7 1 5 2 4 1 3 1 6 2 7
出力例 3
4
Score : 200 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertex U_i and Vertex V_i.
Find the number of tuples of integers a, b, c that satisfy all of the following conditions:
- 1 \leq a \lt b \lt c \leq N
- There is an edge connecting Vertex a and Vertex b.
- There is an edge connecting Vertex b and Vertex c.
- There is an edge connecting Vertex c and Vertex a.
Constraints
- 3 \leq N \leq 100
- 1 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M U_1 V_1 \vdots U_M V_M
Output
Print the answer.
Sample Input 1
5 6 1 5 4 5 2 3 1 4 3 5 2 5
Sample Output 1
2
(a, b, c) = (1, 4, 5), (2, 3, 5) satisfy the conditions.
Sample Input 2
3 1 1 2
Sample Output 2
0
Sample Input 3
7 10 1 7 5 7 2 5 3 6 4 7 1 5 2 4 1 3 1 6 2 7
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。
- レベル n の赤い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
- レベル n の青い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。
高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?
制約
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y
出力
答えを出力せよ。
入力例 1
2 3 4
出力例 1
12
次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。
- まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
- 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
- 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
- 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
- これ以上変換を行うことはできません。
12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。
入力例 2
1 5 5
出力例 2
0
高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。
入力例 3
10 5 5
出力例 3
3942349900
答えが 32 bit 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.
- Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
- Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".
Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?
Constraints
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y
Output
Print the answer.
Sample Input 1
2 3 4
Sample Output 1
12
Takahashi can obtain 12 blue jewels of level 1 by the following conversions.
- First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
- After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
- Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
- After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
- He cannot perform a conversion anymore.
He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.
Sample Input 2
1 5 5
Sample Output 2
0
Takahashi may not be able to obtain a blue jewel of level 1.
Sample Input 3
10 5 5
Sample Output 3
3942349900
Note that the answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。
グラフに含まれる連結成分の個数を求めてください。
注釈
単純無向グラフ とは、単純で辺に向きの無いグラフのことをいいます。
グラフが 単純 であるとは、グラフが自己ループや多重辺を含まないことをいいます。
あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。
制約
- 1 \leq N \leq 100
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq u_i, v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
5 3 1 2 1 3 4 5
出力例 1
2
与えられるグラフに含まれる連結成分は次の 2 個です。
- 頂点 1, 2, 3 および辺 1, 2 からなる部分グラフ
- 頂点 4, 5 および辺 3 からなる部分グラフ

入力例 2
5 0
出力例 2
5
入力例 3
4 6 1 2 1 3 1 4 2 3 2 4 3 4
出力例 3
1
Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i.
Find the number of connected components in this graph.
Notes
A simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.
A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.
Constraints
- 1 \leq N \leq 100
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq u_i, v_i \leq N
- The given graph is simple.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
5 3 1 2 1 3 4 5
Sample Output 1
2
The given graph contains the following two connected components:
- a subgraph formed from vertices 1, 2, 3, and edges 1, 2;
- a subgraph formed from vertices 4, 5, and edge 3.

Sample Input 2
5 0
Sample Output 2
5
Sample Input 3
4 6 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
以下を全て満たす整数の 3 つ組 (i,j,k) がいくつあるか求めてください。
- 1 \le i,j,k \le N
- A_i : A_j : A_k = 7:5:3
- \min(i,j,k) = j または \max(i,j,k) = j
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
10 3 10 7 10 7 6 7 6 5 14
出力例 1
7
条件を満たす整数の 3 つ組 (i,j,k) は以下の 7 個です。
- (3,9,1)
- A_i : A_j : A_k = 7:5:3 であり、 \max(i,j,k) = j です。
- (5,9,1)
- A_i : A_j : A_k = 7:5:3 であり、 \max(i,j,k) = j です。
- (7,9,1)
- A_i : A_j : A_k = 7:5:3 であり、 \max(i,j,k) = j です。
- (10,2,6)
- A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
- (10,2,8)
- A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
- (10,4,6)
- A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
- (10,4,8)
- A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
入力例 2
6 210 210 210 210 210 210
出力例 2
0
入力例 3
21 49 30 50 21 35 15 21 70 35 9 50 70 21 49 30 50 70 15 9 21 30
出力例 3
34
Score : 425 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the number of triples of integers (i,j,k) that satisfy all of the following:
- 1 \le i,j,k \le N
- A_i : A_j : A_k = 7:5:3
- \min(i,j,k) = j or \max(i,j,k) = j.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Output the answer.
Sample Input 1
10 3 10 7 10 7 6 7 6 5 14
Sample Output 1
7
The seven triples of integers (i,j,k) that satisfy the conditions are:
- (3,9,1)
- A_i : A_j : A_k = 7:5:3, and \max(i,j,k) = j.
- (5,9,1)
- A_i : A_j : A_k = 7:5:3, and \max(i,j,k) = j.
- (7,9,1)
- A_i : A_j : A_k = 7:5:3, and \max(i,j,k) = j.
- (10,2,6)
- A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
- (10,2,8)
- A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
- (10,4,6)
- A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
- (10,4,8)
- A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
Sample Input 2
6 210 210 210 210 210 210
Sample Output 2
0
Sample Input 3
21 49 30 50 21 35 15 21 70 35 9 50 70 21 49 30 50 70 15 9 21 30
Sample Output 3
34
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
高橋君と青木君がすごろくをします。
高橋君ははじめ地点 A、青木君ははじめ地点 B にいて、交互にサイコロを振ります。
高橋君が振るサイコロは 1, 2, \ldots, P の出目が一様ランダムに出るサイコロで、青木君が振るサイコロは 1, 2, \ldots, Q の出目が一様ランダムに出るサイコロです。
地点 x にいるときに自分の振ったサイコロの出目が i であるとき、地点 \min(x + i, N) に進みます。
地点 N に先に着いた人をすごろくの勝者とします。
高橋君が先にサイコロを振るとき、高橋君が勝つ確率を \text{mod }998244353 で求めてください。
確率 \text{mod }998244353 とは
この問題で求める確率は必ず有理数になることが証明できます。また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。
このとき xz \equiv y \pmod {998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。
制約
- 2 \leq N \leq 100
- 1 \leq A, B < N
- 1 \leq P, Q \leq 10
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A B P Q
出力
答えを出力せよ。
入力例 1
4 2 3 3 2
出力例 1
665496236
高橋君が最初の手番で 2 あるいは 3 の出目を出すと、高橋君は地点 4 に進んで高橋君が勝利します。
高橋君が最初の手番で 1 の出目を出すと、高橋君は地点 3 に進み、青木君は次の手番で必ず地点 4 に進んで青木君が勝利します。
よって、高橋君が勝つ確率は \frac{2}{3} です。
入力例 2
6 4 2 1 1
出力例 2
1
サイコロの出目は常に 1 です。
このとき高橋君が地点 5 に進み、次いで青木君が地点 3 に進み、次いで高橋君が地点 6 に進むので、高橋君は必ず勝ちます。
入力例 3
100 1 1 10 10
出力例 3
264077814
Score : 500 points
Problem Statement
Takahashi and Aoki will play a game of sugoroku.
Takahashi starts at point A, and Aoki starts at point B. They will take turns throwing dice.
Takahashi's die shows 1, 2, \ldots, P with equal probability, and Aoki's shows 1, 2, \ldots, Q with equal probability.
When a player at point x throws his die and it shows i, he goes to point \min(x + i, N).
The first player to reach point N wins the game.
Find the probability that Takahashi wins if he goes first, modulo 998244353.
How to find a probability modulo 998244353
It can be proved that the sought probability is always rational. Additionally, the constraints of this problem guarantee that, if that probability is represented as an irreducible fraction \frac{y}{x}, then x is indivisible by 998244353.
Here, there is a unique integer z between 0 and 998244352 such that xz \equiv y \pmod {998244353}. Report this z.
Constraints
- 2 \leq N \leq 100
- 1 \leq A, B < N
- 1 \leq P, Q \leq 10
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A B P Q
Output
Print the answer.
Sample Input 1
4 2 3 3 2
Sample Output 1
665496236
If Takahashi's die shows 2 or 3 in his first turn, he goes to point 4 and wins.
If Takahashi's die shows 1 in his first turn, he goes to point 3, and Aoki will always go to point 4 in the next turn and win.
Thus, Takahashi wins with the probability \frac{2}{3}.
Sample Input 2
6 4 2 1 1
Sample Output 2
1
The dice always show 1.
Here, Takahashi goes to point 5, Aoki goes to point 3, and Takahashi goes to point 6, so Takahashi always wins.
Sample Input 3
100 1 1 10 10
Sample Output 3
264077814
Time Limit: 5 sec / Memory Limit: 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.