実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個のビルが横一列に並んでいて、左から i 番目のビルの高さは H_i です。
左から 1 番目のビルより高いビルが存在するか判定し、存在する場合その内最も左のビルは左から何番目か求めてください。
制約
- 1\leq N\leq 100
- 1\leq H_i \leq 100
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N H_1 H_2 \ldots H_N
出力
左から 1 番目のビルより高いビルが存在しない場合 -1 を出力せよ。
存在する場合、その内最も左のビルは左から何番目か出力せよ。
入力例 1
4 3 2 5 2
出力例 1
3
左から 1 番目のビルより高いビルは、左から 3 番目のビルです。
入力例 2
3 4 3 2
出力例 2
-1
左から 1 番目のビルより高いビルは存在しません。
入力例 3
7 10 5 10 2 10 13 15
出力例 3
6
左から 1 番目のビルより高いビルは、左から 6 番目のビルと左から 7 番目のビルです。その内最も左のビルは左から 6 番目のビルです。
Score: 100 points
Problem Statement
There are N buildings aligned in a row. The i-th building from the left has a height of H_i.
Determine if there is a building taller than the first one from the left. If such a building exists, find the position of the leftmost such building from the left.
Constraints
- 1 \leq N \leq 100
- 1 \leq H_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 H_2 \ldots H_N
Output
If no building is taller than the first one from the left, print -1.
If such a building exists, print the position (index) of the leftmost such building from the left.
Sample Input 1
4 3 2 5 2
Sample Output 1
3
The building taller than the first one from the left is the third one from the left.
Sample Input 2
3 4 3 2
Sample Output 2
-1
No building is taller than the first one from the left.
Sample Input 3
7 10 5 10 2 10 13 15
Sample Output 3
6
The buildings taller than the first one from the left are the sixth and seventh ones. Among them, the leftmost is the sixth one.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
AtCoder 遊園地には K 人乗りのアトラクションがあります。 現在、このアトラクションの待機列には N グループが並んでいます。
先頭から i 番目 (1\leq i\leq N) のグループは A _ i 人組です。 すべての i (1\leq i\leq N) について、A _ i\leq K です。
高橋君はこのアトラクションのスタッフとして、並んでいるグループを次の手順に従って誘導します。
はじめ、アトラクションには誰も誘導されておらず、空席は K 個あります。
- 待機列に並んでいるグループがない場合、アトラクションをスタートさせ、誘導を終了する。
- アトラクションの空席の数と待機列の先頭に並んでいるグループの人数を比較し、次のどちらかを行う。
- 待機列の先頭に並んでいるグループの人数よりアトラクションの空席の数のほうが少ない場合、アトラクションをスタートさせる。 スタートしたのち、アトラクションの空席が K 個になる。
- そうでない場合、待機列の先頭に並んでいるグループを全員アトラクションへ誘導する。 先頭のグループが待機列から取り出され、アトラクションの空席がグループの人数ぶんだけ減少する。
- 1 に戻る。
ただし、誘導を開始したあとに追加でグループが並ぶことはないとします。 以上の条件のもとで、この手順が有限回で終了することが示せます。
高橋君が誘導を開始してから誘導を終了するまで、何回アトラクションをスタートさせるか求めてください。
制約
- 1\leq N\leq100
- 1\leq K\leq100
- 1\leq A _ i\leq K\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A _ 1 A _ 2 \ldots A _ N
出力
答えを出力せよ。
入力例 1
7 6 2 5 1 4 1 2 3
出力例 1
4
はじめ、7 つのグループは以下のように並んでいます。

高橋君の誘導の様子の一部を以下の図に示します。

- はじめ、先頭に並んでいるグループは 2 人のグループで、空席は 6 個です。よって、高橋君は先頭のグループをアトラクションに誘導し、空席は 4 個になります。
- 次に、先頭に並んでいるグループは 5 人のグループで、空席の個数 4 より多いため、アトラクションをスタートさせます。
- 空席が 6 個になったため、先頭のグループをアトラクションに誘導し、空席は 1 個になります。
- 次に先頭に並んでいるのは 1 人なので、アトラクションに誘導し、空席は 0 個になります。
すべての誘導が終了するまでに、高橋君は 4 回アトラクションをスタートさせることになります。
よって、4 を出力してください。

入力例 2
7 10 1 10 1 10 1 10 1
出力例 2
7
入力例 3
15 100 73 8 55 26 97 48 37 47 35 55 5 17 62 2 60
出力例 3
8
Score: 200 points
Problem Statement
The AtCoder amusement park has an attraction that can accommodate K people. Now, there are N groups lined up in the queue for this attraction.
The i-th group from the front (1\leq i\leq N) consists of A_i people. For all i (1\leq i\leq N), it holds that A_i \leq K.
Takahashi, as a staff member of this attraction, will guide the groups in the queue according to the following procedure.
Initially, no one has been guided to the attraction, and there are K empty seats.
- If there are no groups in the queue, start the attraction and end the guidance.
- Compare the number of empty seats in the attraction with the number of people in the group at the front of the queue, and do one of the following:
- If the number of empty seats is less than the number of people in the group at the front, start the attraction. Then, the number of empty seats becomes K again.
- Otherwise, guide the entire group at the front of the queue to the attraction. The front group is removed from the queue, and the number of empty seats decreases by the number of people in the group.
- Go back to step 1.
Here, no additional groups will line up after the guidance has started. Under these conditions, it can be shown that this procedure will end in a finite number of steps.
Determine how many times the attraction will be started throughout the guidance.
Constraints
- 1\leq N\leq 100
- 1\leq K\leq 100
- 1\leq A_i\leq K\ (1\leq i\leq N)
- All input values 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 the answer.
Sample Input 1
7 6 2 5 1 4 1 2 3
Sample Output 1
4
Initially, the seven groups are lined up as follows:

Part of Takahashi's guidance is shown in the following figure:

- Initially, the group at the front has 2 people, and there are 6 empty seats. Thus, he guides the front group to the attraction, leaving 4 empty seats.
- Next, the group at the front has 5 people, which is more than the 4 empty seats, so the attraction is started.
- After the attraction is started, there are 6 empty seats again, so the front group is guided to the attraction, leaving 1 empty seat.
- Next, the group at the front has 1 person, so they are guided to the attraction, leaving 0 empty seats.
In total, he starts the attraction four times before the guidance is completed.
Therefore, print 4.

Sample Input 2
7 10 1 10 1 10 1 10 1
Sample Output 2
7
Sample Input 3
15 100 73 8 55 26 97 48 37 47 35 55 5 17 62 2 60
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正整数 x,y に対して f(x,y) を「(x+y) を 10^8 で割ったあまり」として定義します。
長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を求めてください。
制約
- 2\leq N\leq 3\times 10^5
- 1\leq A_i < 10^8
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 3 50000001 50000002
出力例 1
100000012
- f(A_1,A_2)=50000004
- f(A_1,A_3)=50000005
- f(A_2,A_3)=3
なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 100000012 です。
総和を 10^8 で割ったあまりを求めるわけではないことに注意してください。
入力例 2
5 1 3 99999999 99999994 1000000
出力例 2
303999988
Score: 300 points
Problem Statement
For positive integers x and y, define f(x, y) as the remainder of (x + y) divided by 10^8.
You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression:
Constraints
- 2 \leq N \leq 3\times 10^5
- 1 \leq A_i < 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 3 50000001 50000002
Sample Output 1
100000012
- f(A_1,A_2)=50000004
- f(A_1,A_3)=50000005
- f(A_2,A_3)=3
Thus, the answer is f(A_1,A_2) + f(A_1,A_3) + f(A_2,A_3) = 100000012.
Note that you are not asked to compute the remainder of the sum divided by 10^8.
Sample Input 2
5 1 3 99999999 99999994 1000000
Sample Output 2
303999988
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
正整数 x,y に対して f(x,y) を以下で定義します。
- 十進表記の x,y をそれぞれ文字列として解釈しこの順に連結して得られる文字列を z とする。z を十進表記の整数として解釈したときの値を f(x,y) とする。
例えば f(3,14)=314, f(100,1)=1001 です。
長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を 998244353 で割ったあまりを求めてください。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 3 14 15
出力例 1
2044
- f(A_1,A_2)=314
- f(A_1,A_3)=315
- f(A_2,A_3)=1415
なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 2044 です。
入力例 2
5 1001 5 1000000 1000000000 100000
出力例 2
625549048
式の値を 998244353 で割ったあまりを求めることに注意してください。
Score: 400 points
Problem Statement
For positive integers x and y, define f(x, y) as follows:
- Interpret the decimal representations of x and y as strings and concatenate them in this order to obtain a string z. The value of f(x, y) is the value of z when interpreted as a decimal integer.
For example, f(3, 14) = 314 and f(100, 1) = 1001.
You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression modulo 998244353:
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 3 14 15
Sample Output 1
2044
- f(A_1, A_2) = 314
- f(A_1, A_3) = 315
- f(A_2, A_3) = 1415
Thus, the answer is f(A_1, A_2) + f(A_1, A_3) + f(A_2, A_3) = 2044.
Sample Input 2
5 1001 5 1000000 1000000000 100000
Sample Output 2
625549048
Be sure to calculate the value modulo 998244353.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
文字列 x,y に対して f(x,y) を以下で定義します。
- x,y の最長共通接頭辞の長さを f(x,y) とする。
英小文字からなる N 個の文字列 (S_1,\ldots,S_N) が与えられます。次の式の値を求めてください。
制約
- 2\leq N\leq 3\times 10^5
- S_i は英小文字からなる文字列
- 1\leq |S_i|
- |S_1|+|S_2|+\ldots+|S_N|\leq 3\times 10^5
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N S_1 \ldots S_N
出力
答えを出力せよ。
入力例 1
3 ab abc arc
出力例 1
4
- f(S_1,S_2)=2
- f(S_1,S_3)=1
- f(S_2,S_3)=1
なので、答えは f(S_1,S_2)+f(S_1,S_3)+f(S_2,S_3) = 4 です。
入力例 2
11 ab bb aaa bba baba babb aaaba aabbb a a b
出力例 2
32
Score: 500 points
Problem Statement
For strings x and y, define f(x, y) as follows:
- f(x, y) is the length of the longest common prefix of x and y.
You are given N strings (S_1, \ldots, S_N) consisting of lowercase English letters. Find the value of the following expression:
Constraints
- 2 \leq N \leq 3\times 10^5
- S_i is a string consisting of lowercase English letters.
- 1 \leq |S_i|
- |S_1|+|S_2|+\ldots+|S_N|\leq 3\times 10^5
- All input numbers are integers.
Input
The input is given from Standard Input in the following format:
N S_1 \ldots S_N
Output
Print the answer.
Sample Input 1
3 ab abc arc
Sample Output 1
4
- f(S_1,S_2)=2
- f(S_1,S_3)=1
- f(S_2,S_3)=1
Thus, the answer is f(S_1,S_2) + f(S_1,S_3) + f(S_2,S_3) = 4.
Sample Input 2
11 ab bb aaa bba baba babb aaaba aabbb a a b
Sample Output 2
32
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
座標平面上にタイルが敷き詰められています。 1\times1 の大きさの小タイルと K\times K の大きさの大タイルの 2 種類があり、次の規則に従って敷き詰められています。
- 整数の組 (i,j) に対し、正方形 \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つの小タイルもしくは 1 つの大タイルに含まれる。
- \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor が偶数のとき、小タイルに含まれる。
- そうでないとき、大タイルに含まれる。
ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。
例えば、K=3 のとき、タイルは以下のようになります。

高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。
高橋君は、次の移動を好きなだけ繰り返します。
- 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。
高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。
高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。
制約
- 1\leq K\leq10 ^ {16}
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
K S _ x S _ y T _ x T _ y
出力
高橋君が支払わなければならない通行料の最小値を出力せよ。
入力例 1
3 7 2 1 6
出力例 1
5
例えば、以下のように移動することで支払う通行料を 5 にすることができます。

- 上に 3 進む。通行料を 1 支払う。
- 左に 2 進む。通行料を 1 支払う。
- 上に 1 進む。通行料を 1 支払う。
- 左に 4 進む。通行料を 2 支払う。
支払う通行料を 4 以下にすることはできないので、5 を出力してください。
入力例 2
1 41 42 13 56
出力例 2
42

高橋君が最短距離で移動するとき、どのように移動しても通行料を 42 だけ支払います。
支払う通行料を 41 以下にすることはできないので、42 を出力してください。
入力例 3
100 100 99 199 1
出力例 3
0
通行料を支払わなくてよい場合もあります。
入力例 4
96929423 5105216413055191 10822465733465225 1543712011036057 14412421458305526
出力例 4
79154049
Score: 550 points
Problem Statement
Tiles are laid out on a coordinate plane. There are two types of tiles: small tiles of size 1\times1 and large tiles of size K\times K, laid out according to the following rules:
- For each pair of integers (i,j), the square \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained within either one small tile or one large tile.
- If \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor is even, it is contained within a small tile.
- Otherwise, it is contained within a large tile.
Tiles include their boundaries, and no two different tiles have a positive area of intersection.
For example, when K=3, tiles are laid out as follows:

Takahashi starts at the point (S_x+0.5,S_y+0.5) on the coordinate plane.
He can repeat the following movement any number of times:
- Choose a direction (up, down, left, or right) and a positive integer n. Move n units in that direction.
Each time he crosses from one tile to another, he must pay a toll of 1.
Determine the minimum toll Takahashi must pay to reach the point (T_x+0.5,T_y+0.5).
Constraints
- 1\leq K\leq10^{16}
- 0\leq S_x\leq2\times10^{16}
- 0\leq S_y\leq2\times10^{16}
- 0\leq T_x\leq2\times10^{16}
- 0\leq T_y\leq2\times10^{16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
K S_x S_y T_x T_y
Output
Print the minimum toll Takahashi must pay.
Sample Input 1
3 7 2 1 6
Sample Output 1
5
For example, he can move as follows, paying a toll of 5.

- Move up by 3. Pay a toll of 1.
- Move left by 2. Pay a toll of 1.
- Move up by 1. Pay a toll of 1.
- Move left by 4. Pay a toll of 2.
The toll paid cannot be 4 or less, so print 5.
Sample Input 2
1 41 42 13 56
Sample Output 2
42

When he moves the shortest distance, he will always pay a toll of 42.
The toll paid cannot be 41 or less, so print 42.
Sample Input 3
100 100 99 199 1
Sample Output 3
0
There are cases where no toll needs to be paid.
Sample Input 4
96929423 5105216413055191 10822465733465225 1543712011036057 14412421458305526
Sample Output 4
79154049
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
AtCoder 王国には町 1, 町 2,\ldots, 町 N の N 個の町があります。 町 i から町 j まで移動するには通行料が C\times|i-j| 円かかります。
商人である高橋君は、これから開催される M 回の市場のうち 0 回以上に参加しようと思っています。
i 回目 (1\leq i\leq M) の市場の情報は整数の組 (T _ i,P _ i) で表され、i 回目の市場が町 T _ i で行われ、高橋君が参加すると P _ i 円が得られることを意味します。
すべての 1\leq i\lt M について、i 回目の市場が終了してから i+1 回目の市場が開始します。 高橋君が移動するのにかかる時間は無視できるものとします。
高橋君は、はじめ 10 ^ {10 ^ {100}} 円持っており、町 1 にいます。 参加する市場をうまく選び、うまく移動することによって高橋君が得られる儲けの最大値を求めてください。
厳密には、M 回の市場が終わったあとの所持金を最大化するように高橋君が行動した場合の最終的な高橋君の所持金を 10 ^ {10 ^ {100}}+X として、X を求めてください。
制約
- 1\leq N\leq2\times10 ^ 5
- 1\leq C\leq10 ^ 9
- 1\leq M\leq2\times10 ^ 5
- 1\leq T _ i\leq N\ (1\leq i\leq M)
- 1\leq P _ i\leq10 ^ {13}\ (1\leq i\leq M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C M T _ 1 P _ 1 T _ 2 P _ 2 \vdots T _ M P _ M
出力
答えを出力せよ。
入力例 1
6 3 4 5 30 2 10 4 25 2 15
出力例 1
49
たとえば、高橋君が次のように行動することで、所持金を 49 円増やすことができます。
- 町 5 に移動する。所持金が 10 ^ {10 ^ {100}}-12 円になる。
- 1 回目の市場に参加する。所持金が 10 ^ {10 ^ {100}}+18 円になる。
- 町 4 に移動する。所持金が 10 ^ {10 ^ {100}}+15 円になる。
- 3 回目の市場に参加する。所持金が 10 ^ {10 ^ {100}}+40 円になる。
- 町 2 に移動する。所持金が 10 ^ {10 ^ {100}}+34 円になる。
- 4 回目の市場に参加する。所持金が 10 ^ {10 ^ {100}}+49 円になる。
所持金を 10 ^ {10 ^ {100}}+50 円以上にすることはできないため、49 を出力してください。
入力例 2
6 1000000000 4 5 30 2 10 4 25 2 15
出力例 2
0
通行料が高すぎるので、高橋君は町 1 から動かないのが最適です。
入力例 3
50 10 15 37 261 28 404 49 582 19 573 18 633 3 332 31 213 30 377 50 783 17 798 4 561 41 871 15 525 16 444 26 453
出力例 3
5000
入力例 4
50 1000000000 15 30 60541209756 48 49238708511 1 73787345006 24 47221018887 9 20218773368 34 40025202486 14 28286410866 24 82115648680 37 62913240066 14 92020110916 24 20965327730 32 67598565422 39 79828753874 40 52778306283 40 67894622518
出力例 4
606214471001
出力すべき値が 32\operatorname{bit} 整数の範囲に収まらない場合があることに注意してください。
Score: 550 points
Problem Statement
The Kingdom of AtCoder has N towns: towns 1, 2, \ldots, N. To move from town i to town j, you must pay a toll of C \times |i-j| yen.
Takahashi, a merchant, is considering participating in zero or more of M upcoming markets.
The i-th market (1 \leq i \leq M) is described by the pair of integers (T_i, P_i), where the market is held in town T_i and he will earn P_i yen if he participates.
For all 1 \leq i < M, the i-th market ends before the (i+1)-th market begins. The time it takes for him to move is negligible.
He starts with 10^{10^{100}} yen and is initially in town 1. Determine the maximum profit he can make by optimally choosing which markets to participate in and how to move.
Formally, let 10^{10^{100}} + X be his final amount of money if he maximizes the amount of money he has after the M markets. Find X.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C \leq 10^9
- 1 \leq M \leq 2 \times 10^5
- 1 \leq T_i \leq N (1 \leq i \leq M)
- 1 \leq P_i \leq 10^{13} (1 \leq i \leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C M T_1 P_1 T_2 P_2 \vdots T_M P_M
Output
Print the answer.
Sample Input 1
6 3 4 5 30 2 10 4 25 2 15
Sample Output 1
49
For example, Takahashi can increase his money by 49 yen by acting as follows:
- Move to town 5. His money becomes 10^{10^{100}} - 12 yen.
- Participate in the first market. His money becomes 10^{10^{100}} + 18 yen.
- Move to town 4. His money becomes 10^{10^{100}} + 15 yen.
- Participate in the third market. His money becomes 10^{10^{100}} + 40 yen.
- Move to town 2. His money becomes 10^{10^{100}} + 34 yen.
- Participate in the fourth market. His money becomes 10^{10^{100}} + 49 yen.
It is impossible to increase his money to 10^{10^{100}} + 50 yen or more, so print 49.
Sample Input 2
6 1000000000 4 5 30 2 10 4 25 2 15
Sample Output 2
0
The toll fee is so high that it is optimal for him not to move from town 1.
Sample Input 3
50 10 15 37 261 28 404 49 582 19 573 18 633 3 332 31 213 30 377 50 783 17 798 4 561 41 871 15 525 16 444 26 453
Sample Output 3
5000
Sample Input 4
50 1000000000 15 30 60541209756 48 49238708511 1 73787345006 24 47221018887 9 20218773368 34 40025202486 14 28286410866 24 82115648680 37 62913240066 14 92020110916 24 20965327730 32 67598565422 39 79828753874 40 52778306283 40 67894622518
Sample Output 4
606214471001
Note that the output value may exceed the range of a 32-bit integer.