実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
整数 R,C と 2 行 2 列からなる行列 A が与えられるので、 A_{R,C} を出力してください。
制約
- 入力は全て整数
- 1 \le R,C \le 2
- 0 \le A_{i,j} \le 100
入力
入力は以下の形式で標準入力から与えられる。
R C
A_{1,1} A_{1,2}
A_{2,1} A_{2,2}
出力
答えを整数として出力せよ。
入力例 1
1 2 1 0 0 1
出力例 1
0
A_{1,2}=0 です。
入力例 2
2 2 1 2 3 4
出力例 2
4
A_{2,2}=4 です。
入力例 3
2 1 90 80 70 60
出力例 3
70
A_{2,1}=70 です。
Score : 100 points
Problem Statement
Given integers R, C, and a 2 \times 2 matrix A, print A_{R,C}.
Constraints
- All values in input are integers.
- 1 \le R,C \le 2
- 0 \le A_{i,j} \le 100
Input
Input is given from Standard Input in the following format:
R C
A_{1,1} A_{1,2}
A_{2,1} A_{2,2}
Output
Print the answer as an integer.
Sample Input 1
1 2 1 0 0 1
Sample Output 1
0
We have A_{1,2}=0.
Sample Input 2
2 2 1 2 3 4
Sample Output 2
4
We have A_{2,2}=4.
Sample Input 3
2 1 90 80 70 60
Sample Output 3
70
We have A_{2,1}=70.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 以上 3 以下の整数 X が与えられます。
1 以上 3 以下の整数であって X と異なる整数を 1 つ出力してください。
制約
- X は 1 以上 3 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
1 以上 3 以下の整数であって X と異なる整数を 1 つ出力せよ。
条件を満たす整数が複数存在する場合、どれを出力しても正答となる。
入力例 1
1
出力例 1
2
1 以上 3 以下の整数で X(=1) と異なる整数は 2,3 です。
したがって、2 または 3 のいずれかを出力すると正答となります。
入力例 2
2
出力例 2
3
入力例 3
3
出力例 3
2
Score : 100 points
Problem Statement
You are given an integer X between 1 and 3, inclusive.
Output an integer between 1 and 3, inclusive, that is different from X.
Constraints
- X is an integer between 1 and 3, inclusive.
Input
The input is given from Standard Input in the following format:
X
Output
Output an integer between 1 and 3, inclusive, that is different from X.
If there are multiple integers satisfying the condition, any of them will be accepted.
Sample Input 1
1
Sample Output 1
2
The integers between 1 and 3, inclusive, that are different from X(=1) are 2 and 3.
Thus, outputing either 2 or 3 will be accepted.
Sample Input 2
2
Sample Output 2
3
Sample Input 3
3
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の学生が試験を受けました。学生には学生 1, 学生 2, \dots, 学生 N と番号がついていて、学生 i は a_i 点を取りました。
P 点未満の点数を取った学生は "不可" となり単位を取得できません。 "不可" となった学生の人数を答えてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq P \leq 100
- 0 \leq a_i \leq 100 (1 \leq i \leq N)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N P a_1 a_2 \dots a_N
出力
"不可" となった学生の人数を出力せよ。
入力例 1
4 50 80 60 40 0
出力例 1
2
学生 1 は 80 点、学生 2 は 60 点と、 50 点以上の点数を取っているので "不可" とならず単位を取得できています。
一方、学生 3 は 40 点、学生 4 は 0 点で、 50 点を下回る点数を取っているので "不可" となります。よって答えは 2 人です。
入力例 2
3 90 89 89 89
出力例 2
3
入力例 3
2 22 6 37
出力例 3
1
Score : 200 points
Problem Statement
N students took an exam. The students are labeled as Student 1, Student 2, \dots, Student N, and Student i scored a_i points.
A student who scored less than P points are considered to have failed the exam and cannot earn the credit. Find the number of students who failed the exam.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq P \leq 100
- 0 \leq a_i \leq 100 (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P a_1 a_2 \dots a_N
Output
Print the number of students who failed the exam.
Sample Input 1
4 50 80 60 40 0
Sample Output 1
2
Students 1 and 2, who scored 80 and 60 points, respectively, succeeded in scoring at least 50 points to earn the credit.
On the other hand, Students 3 and 4, who scored 40 and 0 points, respectively, fell below 50 points and failed the exam. Thus, the answer is 2.
Sample Input 2
3 90 89 89 89
Sample Output 2
3
Sample Input 3
2 22 6 37
Sample Output 3
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
3 以上の奇数 N が与えられます。
N 行 N 列のマス目があり、はじめどのマスも空白です。 今から、以下の手順に従ってこのマス目の各マスに整数を書き込みます。 なお、上から i+1 行目、左から j+1 列目 (0\leq i<N, 0\leq j<N) のマスを (i,j) と表記することとします。
- マス (0,\frac{N-1}{2}) に 1 を書き込む。
- 次の操作を N^2-1 回繰り返す。
- 前回整数を書き込んだマスを (r,c)、書き込んだ整数を k としたとき、マス ((r-1) \bmod N, (c+1) \bmod N) が空白ならばそのマスに、そうでなければマス ((r+1) \bmod N,c) に k+1 を書き込む。 ここで、x \bmod N は x を N で割ったあまりを表す。
この手順においてそれぞれのマスに書き込まれる整数を求めてください。 なお、どのマスもちょうど 1 回だけ整数を書き込まれることが証明できます。
制約
- 3\leq N \leq 99
- N は奇数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
マス (i,j) に書き込まれる整数を a_{i,j} として、以下の形式で出力せよ。
a_{0,0} a_{0,1} \dots a_{0,N-1}
\vdots
a_{N-1,0} a_{N-1,1} \dots a_{N-1,N-1}
入力例 1
3
出力例 1
8 1 6 3 5 7 4 9 2
以下のように各マスに整数が書き込まれていきます。
- マス (0,\frac{3-1}{2})=(0,1) に 1 を書き込む。
- マス ((0-1) \bmod 3, (1+1) \bmod 3)=(2,2) は空白なので、そこに 2 を書き込む。
- マス ((2-1) \bmod 3, (2+1) \bmod 3)=(1,0) は空白なので、そこに 3 を書き込む。
- マス ((1-1) \bmod 3, (0+1) \bmod 3)=(0,1) は空白ではないので、マス ((1+1) \bmod 3,0)=(2,0) に 4 を書き込む。
- \vdots
入力例 2
5
出力例 2
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Score : 200 points
Problem Statement
You are given an odd number N that is at least 3.
There is a grid with N rows and N columns, where all cells are initially empty. Now, you will write integers in each cell of this grid according to the following procedure. Let (i,j) denote the cell at the (i+1)-th row from the top and (j+1)-th column from the left (0\leq i<N, 0\leq j<N).
- Write 1 in cell (0,\frac{N-1}{2}).
- Repeat the following operation N^2-1 times:
- Let (r,c) be the cell where an integer was written last time, and k be the integer written. If cell ((r-1) \bmod N, (c+1) \bmod N) is empty, write k+1 in that cell; otherwise, write k+1 in cell ((r+1) \bmod N,c). Here, x \bmod N denotes the remainder when x is divided by N.
Find the integer that will be written in each cell in this procedure. It can be proved that each cell will have an integer written in it exactly once.
Constraints
- 3\leq N \leq 99
- N is an odd number.
Input
The input is given from Standard Input in the following format:
N
Output
Let a_{i,j} be the integer written in cell (i,j), and print it in the following format:
a_{0,0} a_{0,1} \dots a_{0,N-1}
\vdots
a_{N-1,0} a_{N-1,1} \dots a_{N-1,N-1}
Sample Input 1
3
Sample Output 1
8 1 6 3 5 7 4 9 2
Integers are written in each cell as follows:
- Write 1 in cell (0,\frac{3-1}{2})=(0,1).
- Cell ((0-1) \bmod 3, (1+1) \bmod 3)=(2,2) is empty, so write 2 there.
- Cell ((2-1) \bmod 3, (2+1) \bmod 3)=(1,0) is empty, so write 3 there.
- Cell ((1-1) \bmod 3, (0+1) \bmod 3)=(0,1) is not empty, so write 4 in cell ((1+1) \bmod 3,0)=(2,0).
- \vdots
Sample Input 2
5
Sample Output 2
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
2 次元座標平面があります。x 軸正方向を右向き、y 軸正方向を上向きとします。
この平面上に自己交差のない四角形があります。
4 つの頂点の座標は反時計回りに (A_x,A_y),(B_x,B_y),(C_x,C_y),(D_x,D_y) です。
この四角形が凸であるか判定してください。
なお、四角形の 4 つの内角が全て 180 度未満であるとき、かつ、その時に限り、その四角形は凸であるといいます。
制約
- -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
- 入力に含まれる値は全て整数である
- 与えられる 4 点は四角形の 4 頂点を反時計回りに並べたものである
- 与えられる 4 点のなす四角形は自己交差がなく退化していない。すなわち
- どの 2 頂点も同じ座標にない
- どの 3 頂点も同一直線上にない
- 隣接しない 2 辺は共有点を持たない
入力
入力は以下の形式で標準入力から与えられる。
A_x A_y B_x B_y C_x C_y D_x D_y
出力
与えられる四角形が凸なら Yes、凸でないなら No を出力せよ。
入力例 1
0 0 1 0 1 1 0 1
出力例 1
Yes
与えられた四角形は正方形であり、4 つの内角は全て 90 度です。したがって、この四角形は凸です。

入力例 2
0 0 1 1 -1 0 1 -1
出力例 2
No
角 A が 270 度です。したがって、この四角形は凸ではありません。

Score : 300 points
Problem Statement
Consider a two-dimensional coordinate plane, where the x-axis is oriented to the right, and the y-axis is oriented upward.
In this plane, there is a quadrilateral without self-intersection.
The coordinates of the four vertices are (A_x,A_y), (B_x,B_y), (C_x,C_y), and (D_x,D_y), in counter-clockwise order.
Determine whether this quadrilateral is convex.
Here, a quadrilateral is convex if and only if all four interior angles are less than 180 degrees.
Constraints
- -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
- All values in input are integers.
- The given four points are the four vertices of a quadrilateral in counter-clockwise order.
- The quadrilateral formed by the given four points has no self-intersection and is non-degenerate. That is,
- no two vertices are at the same coordinates;
- no three vertices are colinear; and
- no two edges that are not adjacent have a common point.
Input
Input is given from Standard Input in the following format:
A_x A_y B_x B_y C_x C_y D_x D_y
Output
If the given quadrilateral is convex, print Yes; otherwise, print No.
Sample Input 1
0 0 1 0 1 1 0 1
Sample Output 1
Yes
The given quadrilateral is a square, whose four interior angles are all 90 degrees. Thus, this quadrilateral is convex.

Sample Input 2
0 0 1 1 -1 0 1 -1
Sample Output 2
No
The angle A is 270 degrees. Thus, this quadrilateral is not convex.

実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A があり、最初 A_i = i です。 以下のクエリを全部で Q 個処理してください。
- タイプ 1 : A_p を x に変更する。
- タイプ 2 : A_p を出力する。
- タイプ 3 : 「 A の先頭の要素を末尾にする」という操作を k 回繰り返す。
- 厳密には、 A を (A_2,A_3,\dots,A_N,A_1) に置き換えることを k 回繰り返す。
制約
- 入力は全て整数
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- 全てのクエリは以下の制約を満たす
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
但し、 \rm{Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 p x
タイプ 2 のクエリは以下の形式で与えられる。
2 p
タイプ 3 のクエリは以下の形式で与えられる。
3 k
出力
タイプ 2 のクエリが現れる度に、その解答を 1 行に出力せよ。
入力例 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
出力例 1
3 1 1000000
この入力には 5 個のクエリが含まれます。
- 最初、 A=(1,2,3,4,5) です。
- 1 番目のクエリはタイプ 2 で、 A_3=3 を出力します。
- 2 番目のクエリはタイプ 1 で、 A_2=1000000 に置き換えます。
- クエリの結果、 A=(1,1000000,3,4,5) となります。
- 3 番目のクエリはタイプ 3 で、「 A の先頭の要素を末尾にする」という操作を 4 回繰り返します。
- クエリの結果、 A=(5,1,1000000,3,4) となります。
- 4 番目のクエリはタイプ 2 で、 A_2=1 を出力します。
- 5 番目のクエリはタイプ 2 で、 A_3=1000000 を出力します。
入力例 2
1000000 2 1 1000000 999999 3 1000000000
出力例 2
出力が空である場合もあります。
Score : 300 points
Problem Statement
There is an integer sequence A of length N, initially A_i = i. Process a total of Q queries of the following types:
- Type 1: Change A_p to x.
- Type 2: Output A_p.
- Type 3: Repeat the operation "move the first element of A to the end" k times.
- Formally, replace A with (A_2,A_3,\dots,A_N,A_1) k times.
Constraints
- All input values are integers.
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- All queries satisfy the following constraints:
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
Input
The input is given from Standard Input in the following format:
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
Here, \rm{Query}_i represents the i-th query.
Type 1 queries are given in the following format:
1 p x
Type 2 queries are given in the following format:
2 p
Type 3 queries are given in the following format:
3 k
Output
For each Type 2 query, output the answer on a line.
Sample Input 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
Sample Output 1
3 1 1000000
This input contains 5 queries.
- Initially, A=(1,2,3,4,5).
- The 1st query is Type 2: output A_3=3.
- The 2nd query is Type 1: replace A_2 with 1000000.
- After the query, A=(1,1000000,3,4,5).
- The 3rd query is Type 3: repeat the operation "move the first element of A to the end" 4 times.
- After the query, A=(5,1,1000000,3,4).
- The 4th query is Type 2: output A_2=1.
- The 5th query is Type 2: output A_3=1000000.
Sample Input 2
1000000 2 1 1000000 999999 3 1000000000
Sample Output 2
The output may be empty.
実行時間制限: 5 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
整数 N と M が与えられます。
以下の条件をすべて満たす長さ N の整数列 (A_1, A_2, \ldots, A_N) を辞書順にすべて出力してください。
- 1 \leq A_i
- 2 以上 N 以下の各整数 i に対して A_{i - 1} + 10 \leq A_i
- A_N \leq M
数列の辞書順とは
長さ 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 より(数として)小さい。
制約
- 2 \leq N \leq 12
- 10N - 9 \leq M \leq 10N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たす長さ N の整数列の個数を X として X + 1 行出力せよ。
1 行目には X の値を出力せよ。
i + 1 (1 \leq i \leq X) 行目には条件を満たす整数列のうち辞書順で i 番目に小さいものを空白区切りで出力せよ。
入力例 1
3 23
出力例 1
10 1 11 21 1 11 22 1 11 23 1 12 22 1 12 23 1 13 23 2 12 22 2 12 23 2 13 23 3 13 23
(1, 11, 21), (1, 11, 22), (1, 11, 23), (1, 12, 22), (1, 12, 23), (1, 13, 23), (2, 12, 22), (2, 12, 23), (2, 13, 23), (3, 13, 23) の 10 個の数列が条件を満たします。
Score: 425 points
Problem Statement
You are given integers N and M.
Print all integer sequences (A_1, A_2, \ldots, A_N) of length N that satisfy all of the following conditions, in lexicographical order.
- 1 \leq A_i
- A_{i - 1} + 10 \leq A_i for each integer i from 2 through N
- A_N \leq M
What is lexicographical order?
A sequence S = (S_1, S_2, \ldots, S_N) of length N is smaller in lexicographical order than a sequence T = (T_1, T_2, \ldots, T_N) of length N if and only if there exists an integer 1 \leq i \leq N such that both of the following hold:
- (S_1, S_2, \ldots, S_{i-1}) = (T_1, T_2, \ldots, T_{i-1})
- S_i is less than T_i (as a number).
Constraints
- 2 \leq N \leq 12
- 10N - 9 \leq M \leq 10N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Let X be the number of integer sequences that satisfy the conditions, and print X + 1 lines.
The first line should contain the value of X.
The (i + 1)-th line (1 \leq i \leq X) should contain the i-th smallest integer sequence in lexicographical order, with elements separated by spaces.
Sample Input 1
3 23
Sample Output 1
10 1 11 21 1 11 22 1 11 23 1 12 22 1 12 23 1 13 23 2 12 22 2 12 23 2 13 23 3 13 23
(1, 11, 21), (1, 11, 22), (1, 11, 23), (1, 12, 22), (1, 12, 23), (1, 13, 23), (2, 12, 22), (2, 12, 23), (2, 13, 23), (3, 13, 23) are the 10 sequences that satisfy the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
0 と 1 からなる長さ N の文字列 S が与えられます。
S は長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) の情報を表しており、S の i 文字目 (1\leq i\leq N) が 0 のとき A _ i=0 、1 のとき A _ i=1です。
\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]
を求めてください。
より厳密には、次のように定められる f(i,j)\ (1\leq i\leq j\leq N) に対して \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) を求めてください。
\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]
ただし、否定論理積 \barwedge は次を満たす二項演算子です。
\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0\]
制約
- 1\leq N\leq10^6
- S は
0と1からなる長さ N の文字列 - 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを 1 行で出力せよ。
入力例 1
5 00110
出力例 1
9
1\leq i\leq j\leq N を満たすすべての組 (i,j) に対して、f(i,j) の値は以下のようになります。
- f(1,1)=0=0
- f(1,2)=0\barwedge0=1
- f(1,3)=(0\barwedge0)\barwedge1=0
- f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
- f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
- f(2,2)=0=0
- f(2,3)=0\barwedge1=1
- f(2,4)=(0\barwedge1)\barwedge1=0
- f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
- f(3,3)=1=1
- f(3,4)=1\barwedge1=0
- f(3,5)=(1\barwedge1)\barwedge0=1
- f(4,4)=1=1
- f(4,5)=1\barwedge0=1
- f(5,5)=0=0
これらの総和は 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9 なので、9 を出力してください。
\barwedge は結合法則を満たさないことに注意してください。 例えば、(1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0) です。
入力例 2
30 101010000100101011010011000010
出力例 2
326
Score : 450 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
It describes a length-N sequence A=(A _ 1,A _ 2,\ldots,A _ N). If the i-th character of S (1\leq i\leq N) is 0, then A _ i=0; if it is 1, then A _ i=1.
Find the following:
\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]
More formally, find \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) for f(i,j)\ (1\leq i\leq j\leq N) defined as follows:
\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]
Here, \barwedge, NAND, is a binary operator satisfying the following:
\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0.\]
Constraints
- 1\leq N\leq10^6
- S is a string of length N consisting of
0and1. - All input values are integers.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer in a single line.
Sample Input 1
5 00110
Sample Output 1
9
Here are the values of f(i,j) for the pairs (i,j) such that 1\leq i\leq j\leq N:
- f(1,1)=0=0
- f(1,2)=0\barwedge0=1
- f(1,3)=(0\barwedge0)\barwedge1=0
- f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
- f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
- f(2,2)=0=0
- f(2,3)=0\barwedge1=1
- f(2,4)=(0\barwedge1)\barwedge1=0
- f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
- f(3,3)=1=1
- f(3,4)=1\barwedge1=0
- f(3,5)=(1\barwedge1)\barwedge0=1
- f(4,4)=1=1
- f(4,5)=1\barwedge0=1
- f(5,5)=0=0
Their sum is 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9, so print 9.
Note that \barwedge does not satisfy the associative property. For instance, (1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0).
Sample Input 2
30 101010000100101011010011000010
Sample Output 2
326
実行時間制限: 2.5 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
店で N 個の商品が売られています。 i 個目の商品の価格は P_i 円、効用は U_i 、色は C_i です。
あなたは、これらの N 個の商品から何個か( 0 個でもよい)を選んで購入します。 このとき、購入した品物の合計価格は X 円以下でなければなりません。
あなたの満足度は、購入した商品の効用の合計を S、購入した商品の色の種類数を T としたとき、S+T \times K です。 ここで、K は入力で与えられる定数です。
あなたの満足度を最大化するように購入する商品を選んだとき、満足度を求めてください。
制約
- 1 \leq N \leq 500
- 1 \leq X \leq 50000
- 1 \leq K \leq 10^9
- 1 \leq P_i \leq X (1 \leq i \leq N)
- 1 \leq U_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_i \leq N (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X K P_1 U_1 C_1 P_2 U_2 C_2 \vdots P_N U_N C_N
出力
答えを出力せよ。
入力例 1
3 10 5 1 3 1 7 4 2 4 5 1
出力例 1
17
1 個目、2 個目の商品を購入したとき、効用の合計 S は 7 で、色の種類数 T は 2 です。よって、満足度は 7+2 \times 5 = 17 です。また、満足度が 18 以上になるような購入の仕方は存在しないため、答えは 17 です。
入力例 2
5 30 3 5 4 3 11 20 1 9 10 4 7 5 2 16 15 4
出力例 2
44
2 個目、3 個目、4 個目の商品を購入したとき、効用の合計 S は 35 で、色の種類数 T は 3 です。よって、満足度は 35+3 \times 3 = 44 です。また、満足度が 45 以上になるような購入の仕方は存在しないため、答えは 44 です。
入力例 3
22 75 6426 9 309 9 5 470 5 17 481 12 27 352 14 1 191 18 7 353 20 9 99 15 20 401 17 46 434 19 11 459 22 10 317 19 15 440 18 17 438 19 25 461 22 5 320 22 1 476 21 11 315 3 8 112 9 11 438 13 19 362 8 10 422 13 10 152 21
出力例 3
67717
Score : 525 points
Problem Statement
There are N products for sale in a store. The i-th product has a price of P_i yen, a utility value of U_i, and a color C_i.
You will choose some subset of these N products to buy (possibly none). The total price of the chosen products must be at most X yen.
Your satisfaction is S + T \times K, where S is the sum of utilities of the chosen products, and T is the number of distinct colors among the chosen products. Here, K is a given constant.
You will choose products to maximize your satisfaction. Find the maximized satisfaction.
Constraints
- 1 \leq N \leq 500
- 1 \leq X \leq 50000
- 1 \leq K \leq 10^9
- 1 \leq P_i \leq X (1 \leq i \leq N)
- 1 \leq U_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_i \leq N (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X K P_1 U_1 C_1 P_2 U_2 C_2 \vdots P_N U_N C_N
Output
Print the answer.
Sample Input 1
3 10 5 1 3 1 7 4 2 4 5 1
Sample Output 1
17
If you buy the 1st and 2nd products, the total utility S is 7, and the number of distinct colors T is 2. Thus, your satisfaction is 7 + 2 \times 5 = 17. No purchase plan makes your satisfaction 18 or greater, so the answer is 17.
Sample Input 2
5 30 3 5 4 3 11 20 1 9 10 4 7 5 2 16 15 4
Sample Output 2
44
If you buy the 2nd, 3rd, and 4th products, the total utility S is 35, and the number of distinct colors T is 3. Thus, your satisfaction is 35 + 3 \times 3 = 44. No purchase plan makes your satisfaction 45 or greater, so the answer is 44.
Sample Input 3
22 75 6426 9 309 9 5 470 5 17 481 12 27 352 14 1 191 18 7 353 20 9 99 15 20 401 17 46 434 19 11 459 22 10 317 19 15 440 18 17 438 19 25 461 22 5 320 22 1 476 21 11 315 3 8 112 9 11 438 13 19 362 8 10 422 13 10 152 21
Sample Output 3
67717