実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
二つの文字 x と y は以下の 3 つの条件のうちどれか 1 つを満たすとき、似た文字と呼ばれます。
- x と y は同じ文字
- x と y の片方が
1で、もう片方がl - x と y の片方が
0で、もう片方がo
また、長さ N の文字列 S と T は以下の条件を満たすとき、似た文字列と呼ばれます。
- 任意の i\ (1\leq i\leq N) について、 S の i 番目の文字と T の i 番目の文字は似た文字
英小文字及び数字からなる長さ N の文字列 S,T が与えられます。 S と T が似た文字列か判定してください。
制約
- N は 1 以上 100 以下の整数
- S,T は英小文字及び数字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
S と T が似た文字列の場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
3 l0w 1ow
出力例 1
Yes
S の 1 文字目は lで、T の 1 文字目は 1です。これらは似た文字です。
S の 2 文字目は 0で、T の 2 文字目は oです。これらは似た文字です。
S の 3 文字目は wで、T の 3 文字目は wです。これらは似た文字です。
よって S と T は似た文字列です。
入力例 2
3 abc arc
出力例 2
No
S の 2 文字目は bで、T の 2 文字目は rです。これらは似た文字ではありません。
よって S と T は似た文字列ではありません。
入力例 3
4 nok0 n0ko
出力例 3
Yes
Score : 100 points
Problem Statement
Two characters x and y are called similar characters if and only if one of the following conditions is satisfied:
- x and y are the same character.
- One of x and y is
1and the other isl. - One of x and y is
0and the other iso.
Two strings S and T, each of length N, are called similar strings if and only if:
- for all i\ (1\leq i\leq N), the i-th character of S and the i-th character of T are similar characters.
Given two length-N strings S and T consisting of lowercase English letters and digits, determine if S and T are similar strings.
Constraints
- N is an integer between 1 and 100.
- Each of S and T is a string of length N consisting of lowercase English letters and digits.
Input
The input is given from Standard Input in the following format:
N S T
Output
Print Yes if S and T are similar strings, and No otherwise.
Sample Input 1
3 l0w 1ow
Sample Output 1
Yes
The 1-st character of S is l, and the 1-st character of T is 1. These are similar characters.
The 2-nd character of S is 0, and the 2-nd character of T is o. These are similar characters.
The 3-rd character of S is w, and the 3-rd character of T is w. These are similar characters.
Thus, S and T are similar strings.
Sample Input 2
3 abc arc
Sample Output 2
No
The 2-nd character of S is b, and the 2-nd character of T is r. These are not similar characters.
Thus, S and T are not similar strings.
Sample Input 3
4 nok0 n0ko
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
AtCoder社のオフィスには加湿器が 1 つあります。現在は時刻 0 であり、加湿器には水が入っていません。
あなたはこの加湿器にこれから N 回水を追加します。i 回目 (1\leq i \leq N)の水の追加は時刻 T_i に行い、水を V_i リットル追加します。なお、 T_i < T_{i+1} (1 \leq i \leq N-1) を満たすことが保証されます。
ただし、加湿器には穴が空いていて、加湿器に水が入っている間は 1 単位時間につき水が 1 リットル減り続けます。
時刻 T_N に水を追加し終えたとき、加湿器に残っている水の量を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq T_i \leq 100 (1 \leq i \leq N)
- 1 \leq V_i \leq 100 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T_1 V_1 T_2 V_2 \vdots T_N V_N
出力
答えを出力せよ。
入力例 1
4 1 3 3 1 4 4 7 1
出力例 1
3
時系列で以下のように水が追加されます。
- 時刻 1 : 水を追加する前は 0 リットル入っており、3 リットル追加して加湿器には 3 リットルの水が入っています。
- 時刻 3 : 水を追加する前は 1 リットル入っており、1 リットル追加して加湿器には 2 リットルの水が入っています。
- 時刻 4 : 水を追加する前は 1 リットル入っており、4 リットル追加して加湿器には 5 リットルの水が入っています。
- 時刻 7 : 水を追加する前は 2 リットル入っており、1 リットル追加して加湿器には 3 リットルの水が入っています。
時刻 7 に水を追加し終えたとき、加湿器に残っている水の量は 3 リットルです。よって答えは 3 です。
入力例 2
3 1 8 10 11 21 5
出力例 2
5
入力例 3
10 2 1 22 10 26 17 29 2 45 20 47 32 72 12 75 1 81 31 97 7
出力例 3
57
Score : 150 points
Problem Statement
There is one humidifier in the AtCoder company office. The current time is 0, and the humidifier has no water inside.
You will add water to this humidifier N times. The i-th addition of water (1 \leq i \leq N) takes place at time T_i, and you add V_i liters of water. It is guaranteed that T_i < T_{i+1} for all 1 \leq i \leq N-1.
However, the humidifier has a leak, and as long as there is water inside, the amount of water decreases by 1 liter per unit time.
Find the amount of water remaining in the humidifier immediately after you finish adding water at time T_N.
Constraints
- 1 \leq N \leq 100
- 1 \leq T_i \leq 100 (1 \leq i \leq N)
- 1 \leq V_i \leq 100 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T_1 V_1 T_2 V_2 \vdots T_N V_N
Output
Print the answer.
Sample Input 1
4 1 3 3 1 4 4 7 1
Sample Output 1
3
At each point in time, water is added as follows:
- Time 1: Before adding, the humidifier has 0 liters. After adding 3 liters, it has 3 liters.
- Time 3: Before adding, it has 1 liter. After adding 1 liter, it has 2 liters total.
- Time 4: Before adding, it has 1 liter. After adding 4 liters, it has 5 liters total.
- Time 7: Before adding, it has 2 liters. After adding 1 liter, it has 3 liters total.
After finishing the addition at time 7, the humidifier contains 3 liters. Thus, the answer is 3.
Sample Input 2
3 1 8 10 11 21 5
Sample Output 2
5
Sample Input 3
10 2 1 22 10 26 17 29 2 45 20 47 32 72 12 75 1 81 31 97 7
Sample Output 3
57
実行時間制限: 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
配点 : 200 点
問題文
正整数 n の桁和を、 n を十進法で表したときの各桁の和と定めます。例えば 2026 の桁和は 2+0+2+6=10 です。
N 以下の正整数のうち、桁和が K であるものの個数を求めてください。
制約
- 1\leq N,K \leq 10^5
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
答えを出力せよ。
入力例 1
30 4
出力例 1
3
30 以下の正整数のうち、桁和が 4 であるものは、4,13,22 の 3 個です。
入力例 2
2026 10
出力例 2
121
入力例 3
99999 45
出力例 3
1
Score : 200 points
Problem Statement
The digit sum of a positive integer n is defined as the sum of the digits when n is represented in decimal. For example, the digit sum of 2026 is 2+0+2+6=10.
Find the number of positive integers not exceeding N whose digit sum is K.
Constraints
- 1\leq N,K \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K
Output
Output the answer.
Sample Input 1
30 4
Sample Output 1
3
Among the positive integers not exceeding 30, there are three integers whose digit sum is 4: 4,13,22.
Sample Input 2
2026 10
Sample Output 2
121
Sample Input 3
99999 45
Sample Output 3
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
H 行 W 列のマス目があります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす 2 つの整数 i, j について、 上から i 行目、左から j 列目のマス(以下、(i, j) と表す)には、整数 A_{i, j} が書かれています。
いま、高橋君は (1, 1) にいます。 これから高橋君は「いまいるマスから右または下に隣接するマスに移動する」ことを繰り返して、(H, W) まで移動します。 ただし、その過程でマス目の外部に移動することは出来ません。
その結果、高橋君が通ったマス(始点 (1, 1) と終点 (H, W) を含む)に書かれた整数がすべて異なるとき、高橋君は嬉しくなります。 高橋君の移動経路として考えられるもののうち、高橋君が嬉しくなるものの個数を出力してください。
制約
- 2 \leq H, W \leq 10
- 1 \leq A_{i, j} \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}
出力
答えを出力せよ。
入力例 1
3 3 3 2 2 2 1 3 1 5 4
出力例 1
3
高橋君の移動経路として考えられるものは下記の 6 通りです。
- (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 2, 3, 4 であり、高橋君は嬉しくなりません。
- (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません。
- (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります。
- (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません。
- (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります。
- (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります。
よって、高橋君が嬉しくなる移動経路は、上で 3, 5, 6 番目に述べた 3 個です。
入力例 2
10 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
出力例 2
48620
この例では、高橋君は考えられるどの経路を通っても嬉しくなります。
Score : 300 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. For two integers i and j such that 1 \leq i \leq H and 1 \leq j \leq W, the square at the i-th row from the top and j-th column from the left (which we denote by (i, j)) has an integer A_{i, j} written on it.
Takahashi is currently at (1,1). From now on, he repeats moving to an adjacent square to the right of or below his current square until he reaches (H, W). When he makes a move, he is not allowed to go outside the grid.
Takahashi will be happy if the integers written on the squares he visits (including initial (1, 1) and final (H, W)) are distinct. Find the number of his possible paths that make him happy.
Constraints
- 2 \leq H, W \leq 10
- 1 \leq A_{i, j} \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}
Output
Print the answer.
Sample Input 1
3 3 3 2 2 2 1 3 1 5 4
Sample Output 1
3
There are six possible paths:
- (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 2, 3, 4, so he will not be happy.
- (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
- (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
- (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
- (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
- (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
Thus, the third, fifth, and sixth paths described above make him happy.
Sample Input 2
10 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Sample Output 2
48620
In this example, every possible path makes him happy.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君と青木君が選挙で戦っています。
選挙区は N 個あります。i 番目の選挙区には X_i + Y_i 人の有権者がいて、そのうち X_i 人が高橋派、Y_i 人が青木派です。(X_i + Y_i はすべて奇数です)
それぞれの区では、多数派がその区の Z_i 議席を全て獲得します。そして、N 個の選挙区全体として過半数の議席を獲得した方が選挙に勝利します。(\displaystyle \sum_{i=1}^N Z_i は奇数です)
高橋君が選挙で勝利するには最低で何人を青木派から高橋派に鞍替えさせる必要がありますか?
制約
- 1 \leq N \leq 100
- 0 \leq X_i, Y_i \leq 10^9
- X_i + Y_i は奇数
- 1 \leq Z_i
- \displaystyle \sum_{i=1}^N Z_i \leq 10^5
- \displaystyle \sum_{i=1}^N Z_i は奇数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
出力
答えを出力せよ。
入力例 1
1 3 8 1
出力例 1
3
選挙区が 1 個しかないので、1 番目の選挙区で議席を獲得した人が選挙に勝利します。
1 番目の選挙区の青木派 3 人を高橋派に鞍替えさせると、1 番目の選挙区にいる有権者のうち高橋派は 6 人、青木派は 5 人になり、高橋君は議席を獲得できます。
入力例 2
2 3 6 2 1 8 5
出力例 2
4
1 番目の選挙区の議席数よりも 2 番目の選挙区の議席数の方が多いため、高橋君が選挙に勝つには 2 番目の選挙区で高橋派を多数派にする必要があります。
2 番目の選挙区の青木派の 4 人を鞍替えさせると高橋君は 5 議席を獲得できます。このとき青木君の獲得する議席は 2 議席なので、高橋君は選挙に勝利できます。
入力例 3
3 3 4 2 1 2 3 7 2 6
出力例 3
0
青木派から高橋派に鞍替えする人が 0 人でも高橋君が選挙で勝つ場合は 0 人が答えになります。
入力例 4
10 1878 2089 16 1982 1769 13 2148 1601 14 2189 2362 15 2268 2279 16 2394 2841 18 2926 2971 20 3091 2146 20 3878 4685 38 4504 4617 29
出力例 4
86
Score : 400 points
Problem Statement
Takahashi and Aoki are competing in an election.
There are N electoral districts. The i-th district has X_i + Y_i voters, of which X_i are for Takahashi and Y_i are for Aoki. (X_i + Y_i is always an odd number.)
In each district, the majority party wins all Z_i seats in that district. Then, whoever wins the majority of seats in the N districts as a whole wins the election. (\displaystyle \sum_{i=1}^N Z_i is odd.)
At least how many voters must switch from Aoki to Takahashi for Takahashi to win the election?
Constraints
- 1 \leq N \leq 100
- 0 \leq X_i, Y_i \leq 10^9
- X_i + Y_i is odd.
- 1 \leq Z_i
- \displaystyle \sum_{i=1}^N Z_i \leq 10^5
- \displaystyle \sum_{i=1}^N Z_i is odd.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
Output
Print the answer.
Sample Input 1
1 3 8 1
Sample Output 1
3
Since there is only one district, whoever wins the seat in that district wins the election.
If three voters for Aoki in the district switch to Takahashi, there will be six voters for Takahashi and five for Aoki, and Takahashi will win the seat.
Sample Input 2
2 3 6 2 1 8 5
Sample Output 2
4
Since there are more seats in the second district than in the first district, Takahashi must win a majority in the second district to win the election.
If four voters for Aoki in the second district switch sides, Takahashi will win five seats. In this case, Aoki will win two seats, so Takahashi will win the election.
Sample Input 3
3 3 4 2 1 2 3 7 2 6
Sample Output 3
0
If Takahashi will win the election even if zero voters switch sides, the answer is 0.
Sample Input 4
10 1878 2089 16 1982 1769 13 2148 1601 14 2189 2362 15 2268 2279 16 2394 2841 18 2926 2971 20 3091 2146 20 3878 4685 38 4504 4617 29
Sample Output 4
86
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点の根付き木があります。頂点には 1 から N の番号がついており、根は頂点 1 です。
i 番目の辺は頂点 A_i と B_i を結んでいます。
頂点 i には整数 X_i が書かれています。
Q 個のクエリが与えられます。i 番目のクエリでは整数の組 (V_i,K_i) が与えられるので、次の問題に答えてください。
- 問題:頂点 V_i の部分木に含まれる頂点に書かれた整数のうち、大きい方から K_i 番目の値を求めよ
制約
- 2 \leq N \leq 10^5
- 0\leq X_i\leq 10^9
- 1\leq A_i,B_i\leq N
- 1\leq Q \leq 10^5
- 1\leq V_i\leq N
- 1\leq K_i\leq 20
- 与えられるグラフは木である
- 頂点 V_i の部分木は頂点を K_i 個以上持つ
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N Q
X_1 \ldots X_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 K_1
\vdots
V_Q K_Q
出力
Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
5 2 1 2 3 4 5 1 4 2 1 2 5 3 2 1 2 2 1
出力例 1
4 5
この入力において与えられる木は下図のようなものです。

1 番目のクエリでは、頂点 1 の部分木に含まれる頂点 1,2,3,4,5 に書かれた数のうち大きい方から 2 番目である 4 を出力します。
2 番目のクエリでは、頂点 2 の部分木に含まれる頂点 2,3,5 に書かれた数のうち大きい方から 1 番目である 5 を出力します。
入力例 2
6 2 10 10 10 9 8 8 1 4 2 1 2 5 3 2 6 4 1 4 2 2
出力例 2
9 10
入力例 3
4 4 1 10 100 1000 1 2 2 3 3 4 1 4 2 3 3 2 4 1
出力例 3
1 10 100 1000
Score : 500 points
Problem Statement
We have a rooted tree with N vertices. The vertices are numbered 1 through N, and the root is Vertex 1.
The i-th edge connects Vertices A_i and B_i.
Vertex i has an integer X_i written on it.
You are given Q queries. For the i-th query, given a pair of integers (V_i,K_i), answer the following question.
- Question: among the integers written on the vertices in the subtree rooted at Vertex V_i, find the K_i-th largest value.
Constraints
- 2 \leq N \leq 10^5
- 0\leq X_i\leq 10^9
- 1\leq A_i,B_i\leq N
- 1\leq Q \leq 10^5
- 1\leq V_i\leq N
- 1\leq K_i\leq 20
- The given graph is a tree.
- The subtree rooted at Vertex V_i has K_i or more vertices.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
X_1 \ldots X_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 K_1
\vdots
V_Q K_Q
Output
Print Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
5 2 1 2 3 4 5 1 4 2 1 2 5 3 2 1 2 2 1
Sample Output 1
4 5
The tree given in this input is shown below.

For the 1-st query, the vertices in the subtree rooted at Vertex 1 are Vertices 1, 2, 3, 4, and 5, so print the 2-nd largest value of the numbers written on these vertices, 4.
For the 2-nd query, the vertices in the subtree rooted at Vertex 2 are Vertices 2, 3, and 5, so print the 1-st largest value of the numbers written on these vertices, 5.
Sample Input 2
6 2 10 10 10 9 8 8 1 4 2 1 2 5 3 2 6 4 1 4 2 2
Sample Output 2
9 10
Sample Input 3
4 4 1 10 100 1000 1 2 2 3 3 4 1 4 2 3 3 2 4 1
Sample Output 3
1 10 100 1000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
N 個の整数 A_1,A_2,\dots,A_N が与えられます。
以下の条件を満たす整数の組 (i,j,k) の個数を求めてください。
- 1 \le i,j,k \le N
- A_i \times A_j = A_k
制約
- 1 \le N \le 1000
- \color{red}{1 \le A_i < 10^{1000}}
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \vdots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 6 12 24
出力例 1
6
問題文中の条件を満たす (i,j,k) の組は以下の 6 通りです。
- (1,2,3)
- (1,3,4)
- (1,4,5)
- (2,1,3)
- (3,1,4)
- (4,1,5)
入力例 2
11 1 2 3 4 5 6 123456789123456789 123456789123456789 987654321987654321 987654321987654321 121932631356500531347203169112635269
出力例 2
40
各整数 A_i の値が非常に大きくなりうることに注意してください。
入力例 3
9 4 4 4 2 2 2 1 1 1
出力例 3
162
A_i の値に重複がありうることに注意してください。
Score: 550 points
Problem Statement
You are given N integers A_1, A_2, \dots, A_N.
Find the number of triples of integers (i, j, k) that satisfy the following conditions:
- 1 \le i, j, k \le N
- A_i \times A_j = A_k
Constraints
- 1 \le N \le 1000
- \color{red}{1 \le A_i < 10^{1000}}
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \vdots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 6 12 24
Sample Output 1
6
The following six triples (i, j, k) satisfy the conditions in the problem statement:
- (1, 2, 3)
- (1, 3, 4)
- (1, 4, 5)
- (2, 1, 3)
- (3, 1, 4)
- (4, 1, 5)
Sample Input 2
11 1 2 3 4 5 6 123456789123456789 123456789123456789 987654321987654321 987654321987654321 121932631356500531347203169112635269
Sample Output 2
40
Note that the values of each integer A_i can be huge.
Sample Input 3
9 4 4 4 2 2 2 1 1 1
Sample Output 3
162
Note that there may be duplicates among the values of A_i.