Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
小数第三位までで表すことのできる実数 X が、小数第三位まで入力されます。
X を小数第一位で四捨五入した結果として得られる整数を出力してください。
制約
- 0 \leq X < 100
- X は小数第三位までで表現可能である。
- X は小数第三位まで与えられる。
入力
入力は以下の形式で標準入力から与えられる。
X
出力
X を小数第一位で四捨五入して得られる整数を出力せよ。
入力例 1
3.456
出力例 1
3
3.456 の小数第一位は 4 であるので、3.456 を小数第一位で四捨五入した値は 3 となります。
入力例 2
99.500
出力例 2
100
入力例 3
0.000
出力例 3
0
Score : 100 points
Problem Statement
You are given a real number X, which is representable using at most three decimal digits, with three decimal digits.
Round X to the nearest integer and print the result.
Constraints
- 0 \leq X < 100
- X is representable using at most three decimal digits.
- X has three decimal digits in input.
Input
Input is given from Standard Input in the following format:
X
Output
Print the integer resulting from rounding X to the nearest integer.
Sample Input 1
3.456
Sample Output 1
3
The digit in the first decimal place of 3.456 is 4, so we should round it down to 3.
Sample Input 2
99.500
Sample Output 2
100
Sample Input 3
0.000
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
5 枚のカードがあり、それぞれのカードには整数 A,B,C,D,E が書かれています。
この 5 枚組は以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。
- 同じ数が書かれたカード 3 枚と、別の同じ数が書かれたカード 2 枚からなる。
5 枚組がフルハウスであるか判定してください。
制約
- 1 \leq A,B,C,D,E\leq 13
- A,B,C,D,E 全てが同じ値ではない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D E
出力
フルハウスである場合 Yes を、そうでないとき No を出力せよ。
入力例 1
1 2 1 2 1
出力例 1
Yes
1 が書かれたカード 3 枚と、2 が書かれたカード 2 枚からなるため、これはフルハウスです。
入力例 2
12 12 11 1 2
出力例 2
No
フルハウスの条件を満たしません。
Score : 100 points
Problem Statement
We have five cards with integers A, B, C, D, and E written on them, one on each card.
This set of five cards is called a Full house if and only if the following condition is satisfied:
- the set has three cards with a same number written on them, and two cards with another same number written on them.
Determine whether the set is a Full house.
Constraints
- 1 \leq A,B,C,D,E\leq 13
- Not all of A, B, C, D, and E are the same.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C D E
Output
If the set is a Full house, print Yes; otherwise, print No.
Sample Input 1
1 2 1 2 1
Sample Output 1
Yes
The set has three cards with 1 written on them and two cards with 2 written on them, so it is a Full house.
Sample Input 2
12 12 11 1 2
Sample Output 2
No
The condition is not satisfied.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる 3 つの文字列 S_1, S_2, S_3 と、1、2、3 のみからなる文字列 T が与えられます。
T の各文字に対応する文字列を連結してできる文字列を出力してください。より厳密には、以下の指示にしたがって文字列を出力してください。
- 1 \leq i \leq |T| を満たす整数 i に対し、文字列 s_i を次のように定める。
- T の i 文字目が
1のとき、S_1 - T の i 文字目が
2のとき、S_2 - T の i 文字目が
3のとき、S_3
- T の i 文字目が
- s_1, s_2, \dots, s_{|T|} をこの順に連結してできる文字列を出力する。
制約
- 1 \leq |S_1|, |S_2|, |S_3| \leq 10
- 1 \leq |T| \leq 1000
- S_1, S_2, S_3 は英小文字からなる。
- T は
1、2、3のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2 S_3 T
出力
答えを出力せよ。
入力例 1
mari to zzo 1321
出力例 1
marizzotomari
s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari であるので、これらを連結してできる文字列である marizzotomari を出力します。
入力例 2
abra cad abra 123
出力例 2
abracadabra
入力例 3
a b c 1
出力例 3
a
Score : 200 points
Problem Statement
You are given three strings S_1, S_2, S_3 consisting of lowercase English letters, and a string T consisting of 1, 2, 3.
Concatenate the three strings according to the characters in T and print the resulting string. Formally, conform to the following instructions.
- For each integer i such that 1 \leq i \leq |T|, let the string s_i be defined as follows:
- S_1, if the i-th character of T is
1; - S_2, if the i-th character of T is
2; - S_3, if the i-th character of T is
3.
- S_1, if the i-th character of T is
- Concatenate the strings s_1, s_2, \dots, s_{|T|} in this order and print the resulting string.
Constraints
- 1 \leq |S_1|, |S_2|, |S_3| \leq 10
- 1 \leq |T| \leq 1000
- S_1, S_2, and S_3 consist of lowercase English letters.
- T consists of
1,2, and3.
Input
Input is given from Standard Input in the following format:
S_1 S_2 S_3 T
Output
Print the answer.
Sample Input 1
mari to zzo 1321
Sample Output 1
marizzotomari
We have s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari. Concatenate these and print the resulting string: marizzotomari.
Sample Input 2
abra cad abra 123
Sample Output 2
abracadabra
Sample Input 3
a b c 1
Sample Output 3
a
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
xy 平面上に N 人の人 1,2,\dots,N がおり、人 i は座標 (X_i,Y_i) にいます。
このうち、 K 人の人 A_1,A_2,\dots,A_K に同じ強さの明かりを持たせます。
座標 (x,y) にいる人が強さ R の明かりを持っている時、その明かりによって中心 (x,y) 、半径 R の円の内部全体(境界を含む)が照らされます。
すべての人が少なくとも 1 つの明かりによって照らされるために必要な明かりの強さの最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K < N \le 1000
- 1 \le A_1 < A_2 < \dots < A_K \le N
- |X_i|,|Y_i| \le 10^5
- i \neq j ならば (X_i,Y_i) \neq (X_j,Y_j)
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_K X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを実数として出力せよ。
出力された解と想定解との絶対誤差または相対誤差が 10^{-5} 以下であるならば、出力は正しいと見なされる。
入力例 1
4 2 2 3 0 0 0 1 1 2 2 0
出力例 1
2.23606797749978969
この入力では人が 4 人おり、そのうち人 2,3 が明かりを持ちます。
R \ge \sqrt{5} \approx 2.236068 である時、すべての人が少なくとも 1 つの明かりによって照らされます。
入力例 2
2 1 2 -100000 -100000 100000 100000
出力例 2
282842.712474619009
入力例 3
8 3 2 6 8 -17683 17993 93038 47074 58079 -57520 -41515 -89802 -72739 68805 24324 -73073 71049 72103 47863 19268
出力例 3
130379.280458974768
Score : 200 points
Problem Statement
There are N people numbered 1, 2, \dots, N in the xy-plane. Person i is at the coordinates (X_i, Y_i).
K of these people, Persons A_1, A_2, \dots, A_K, will receive lights of the same strength.
When a person at coordinates (x, y) has a light of strength R, it lights up the interior of a circle of radius R centered at (x, y) (including the boundary).
Find the minimum strength of the lights needed for every person to be lit by at least one light.
Constraints
- All values in input are integers.
- 1 \le K < N \le 1000
- 1 \le A_1 < A_2 < \dots < A_K \le N
- |X_i|,|Y_i| \le 10^5
- (X_i,Y_i) \neq (X_j,Y_j), if i \neq j.
Input
Input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_K X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print the answer as a real number.
Your output will be considered correct if its absolute or relative error from the judge's output is at most 10^{-5}.
Sample Input 1
4 2 2 3 0 0 0 1 1 2 2 0
Sample Output 1
2.23606797749978969
This input contains four people. Among them, Persons 2 and 3 will have lights.
Every person will be lit by at least one light if R \ge \sqrt{5} \approx 2.236068.
Sample Input 2
2 1 2 -100000 -100000 100000 100000
Sample Output 2
282842.712474619009
Sample Input 3
8 3 2 6 8 -17683 17993 93038 47074 58079 -57520 -41515 -89802 -72739 68805 24324 -73073 71049 72103 47863 19268
Sample Output 3
130379.280458974768
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正の整数 L に対して、 レベル L のダンゴ文字列とは、以下の条件を満たす文字列です。
oと-からなる長さ L+1 の文字列である。- 先頭の文字と末尾の文字のうちちょうど一方が
-であり、そのほかの L 文字はすべてoである。
例えば、ooo- はレベル 3 のダンゴ文字列ですが、-ooo- や oo や o-oo- などはダンゴ文字列ではありません(より正確には、どのような正の整数 L に対してもレベル L のダンゴ文字列ではありません)。
2 種類の文字 o - からなる、長さ N の文字列 S が与えられます。
次の条件を満たすような正整数 X のうち、最大のものを求めてください。
- S の連続する部分文字列であって、レベル X のダンゴ文字列であるものが存在する。
ただし、そのような整数が存在しない場合、-1 と出力してください。
制約
- 1\leq N\leq 2\times10^5
- S は
o-からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S にレベル X のダンゴ文字列が含まれるような最大の X の値を 1 行で出力せよ。
そのような値が存在しない場合、-1 を出力せよ。
入力例 1
10 o-oooo---o
出力例 1
4
たとえば、S の 3 文字目から 7 文字目までに対応する部分文字列 oooo- は、レベル 4 のダンゴ文字列です。
S の部分文字列であってレベル 5 以上のダンゴ文字列であるようなものは存在しないため、4 と出力してください。
入力例 2
1 -
出力例 2
-1
S の連続する部分文字列は空文字列と - の 2 種類だけです。
これらはダンゴ文字列ではないため、-1 と出力してください。
入力例 3
30 -o-o-oooo-oo-o-ooooooo--oooo-o
出力例 3
7
Score : 300 points
Problem Statement
For a positive integer L, a level-L dango string is a string that satisfies the following conditions.
- It is a string of length L+1 consisting of
oand-. - Exactly one of the first character and the last character is
-, and the other L characters areo.
For instance, ooo- is a level-3 dango string, but none of -ooo-, oo, and o-oo- is a dango string (more precisely, none of them is a level-L dango string for any positive integer L).
You are given a string S of length N consisting of the two characters o and -.
Find the greatest positive integer X that satisfies the following condition.
- There is a contiguous substring of S that is a level-X dango string.
If there is no such integer, print -1.
Constraints
- 1\leq N\leq 2\times10^5
- S is a string of length N consisting of
oand-.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the greatest positive integer X such that S contains a level-X dango string, or -1 if there is no such integer.
Sample Input 1
10 o-oooo---o
Sample Output 1
4
For instance, the substring oooo- corresponding to the 3-rd through 7-th characters of S is a level-4 dango string.
No substring of S is a level-5 dango string or above, so you should print 4.
Sample Input 2
1 -
Sample Output 2
-1
Only the empty string and - are the substrings of S.
They are not dango strings, so you should print -1.
Sample Input 3
30 -o-o-oooo-oo-o-ooooooo--oooo-o
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 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.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
非負整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
A の(添え字が相異なる) K 個の項の和として考えられる非負整数の集合を S とします。
S に含まれる D の倍数の最大値を求めてください。ただし、S に D の倍数が含まれない場合、代わりに -1 と出力してください。
制約
- 1 \leq K \leq N \leq 100
- 1 \leq D \leq 100
- 0 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K D a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
4 2 2 1 2 3 4
出力例 1
6
A から 2 個の項を選ぶ方法を列挙すると
- a_1 と a_2 を選ぶ。選ばれた項の和は 1+2=3 となる。
- a_1 と a_3 を選ぶ。選ばれた項の和は 1+3=4 となる。
- a_1 と a_4 を選ぶ。選ばれた項の和は 1+4=5 となる。
- a_2 と a_3 を選ぶ。選ばれた項の和は 2+3=5 となる。
- a_2 と a_4 を選ぶ。選ばれた項の和は 2+4=6 となる。
- a_3 と a_4 を選ぶ。選ばれた項の和は 3+4=7 となる。
となり、S=\{3,4,5,6,7\} となります。S に含まれる 2 の倍数のうち最大のものは 6 なので、6 と出力します。
入力例 2
3 1 2 1 3 5
出力例 2
-1
この例では S=\{1,3,5\} です。S に含まれる非負整数はいずれも 2 の倍数でないため、-1 と出力します。
Score : 400 points
Problem Statement
You are given a sequence of non-negative integers A=(a_1,a_2,\ldots,a_N).
Let S be the set of non-negative integers that can be the sum of K terms in A (with distinct indices).
Find the greatest multiple of D in S. If there is no multiple of D in S, print -1 instead.
Constraints
- 1 \leq K \leq N \leq 100
- 1 \leq D \leq 100
- 0 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K D a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
4 2 2 1 2 3 4
Sample Output 1
6
Here are all the ways to choose two terms in A.
- Choose a_1 and a_2, whose sum is 1+2=3.
- Choose a_1 and a_3, whose sum is 1+3=4.
- Choose a_1 and a_4, whose sum is 1+4=5.
- Choose a_2 and a_3, whose sum is 2+3=5.
- Choose a_2 and a_4, whose sum is 2+4=6.
- Choose a_3 and a_4, whose sum is 3+4=7.
Thus, we have S=\{3,4,5,6,7\}. The greatest multiple of 2 in S is 6, so you should print 6.
Sample Input 2
3 1 2 1 3 5
Sample Output 2
-1
In this example, we have S=\{1,3,5\}. Nothing in S is a multiple of 2, so you should print -1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1 以上 N-1 以下の整数からなる長さ M の数列 A=(A_1,A_2,\dots,A_M) が与えられます。 i=1,2,\dots,M について、以下の質問に答えてください。
- 数列 B=(B_1,B_2,\dots,B_N) がある。最初、各 j について B_j=j である。今から、k=1,2,\dots,i-1,i+1,\dots,M の順に以下の操作を行う
(すなわち、i を除いた 1 以上 M 以下の整数 k について、昇順に以下の操作を行う)。
- B_{A_k} と B_{A_k+1} の値を入れ替える。
- 全ての操作が終了した段階で、B_j=1 を満たす j の値を S_i と定義する。S_i を求めよ。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i \leq N-1\ (1\leq i \leq M)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_M
出力
M 行出力せよ。 i\ (1\leq i \leq M) 行目には、S_i の値を整数として出力せよ。
入力例 1
5 4 1 2 3 2
出力例 1
1 3 2 4
i = 2 のとき、操作によって B は以下のように変化します。
- 最初、B = (1,2,3,4,5)
- k=1 として操作を行う。すなわち B_1 と B_2 の値を入れ替えて、B = (2,1,3,4,5)
- k=3 として操作を行う。すなわち B_3 と B_4 の値を入れ替えて、B = (2,1,4,3,5)
- k=4 として操作を行う。すなわち B_2 と B_3 の値を入れ替えて、B = (2,4,1,3,5)
全ての操作が終了した段階で B_3=1 であるため、S_2 = 3 です。
同様に、
- i=1 のとき:k=2,3,4 の順に操作を行うと B=(1,4,3,2,5) になるので、S_1=1
- i=3 のとき:k=1,2,4 の順に操作を行うと B=(2,1,3,4,5) になるので、S_3=2
- i=4 のとき:k=1,2,3 の順に操作を行うと B=(2,3,4,1,5) になるので、S_4=4
です。
入力例 2
3 3 2 2 2
出力例 2
1 1 1
入力例 3
10 10 1 1 1 9 4 4 2 1 3 3
出力例 3
2 2 2 3 3 3 1 3 4 4
Score : 500 points
Problem Statement
You are given a sequence of length M consisting of integers between 1 and N-1, inclusive: A=(A_1,A_2,\dots,A_M). Answer the following question for i=1, 2, \dots, M.
- There is a sequence B=(B_1,B_2,\dots,B_N). Initially, we have B_j=j for each j. Let us perform the following operation for k=1, 2, \dots, i-1, i+1, \dots, M in this order (in other words, for integers k between 1 and M except i in ascending order).
- Swap the values of B_{A_k} and B_{A_k+1}.
- After all the operations, let S_i be the value of j such that B_j=1. Find S_i.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i \leq N-1\ (1\leq i \leq M)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_M
Output
Print M lines. The i-th line (1\leq i \leq M) should contain the value S_i as an integer.
Sample Input 1
5 4 1 2 3 2
Sample Output 1
1 3 2 4
For i = 2, the operations change B as follows.
- Initially, B = (1,2,3,4,5).
- Perform the operation for k=1. That is, swap the values of B_1 and B_2, making B = (2,1,3,4,5).
- Perform the operation for k=3. That is, swap the values of B_3 and B_4, making B = (2,1,4,3,5).
- Perform the operation for k=4. That is, swap the values of B_2 and B_3, making B = (2,4,1,3,5).
After all the operations, we have B_3=1, so S_2 = 3.
Similarly, we have the following.
- For i=1: performing the operation for k=2,3,4 in this order makes B=(1,4,3,2,5), so S_1=1.
- For i=3: performing the operation for k=1,2,4 in this order makes B=(2,1,3,4,5), so S_3=2.
- For i=4: performing the operation for k=1,2,3 in this order makes B=(2,3,4,1,5), so S_4=4.
Sample Input 2
3 3 2 2 2
Sample Output 2
1 1 1
Sample Input 3
10 10 1 1 1 9 4 4 2 1 3 3
Sample Output 3
2 2 2 3 3 3 1 3 4 4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の品物があります。
これらはそれぞれ、缶切りが不要な缶・缶切りが必要な缶・缶切りのいずれかです。
i 個目の品物は、整数の組 (T_i, X_i) により次のように表されます。
- T_i = 0 ならば、i 個目の品物は缶切りが不要な缶で、入手すると満足度 X_i を得る。
- T_i = 1 ならば、i 個目の品物は缶切りが必要な缶で、入手した上で缶切りを使うと満足度 X_i を得る。
- T_i = 2 ならば、i 個目の品物は缶切りで、X_i 個までの缶に対して使用できる。
N 個の品物から M 個を選んで入手するとき、得られる満足度の合計としてあり得る最大値を求めてください。
制約
- 1 \leq M \leq N \leq 2 \times 10^5
- T_i は 0,1,2 のいずれか
- 1 \leq X_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M T_1 X_1 T_2 X_2 \vdots T_N X_N
出力
答えを整数として出力せよ。
入力例 1
8 4 0 6 0 6 1 3 1 5 1 15 2 1 2 10 2 100
出力例 1
27
1, 2, 5, 7 個目の品物を入手し、7 個目の品物である缶切りを 5 個目の品物に対して使用すると、満足度 6 + 6 + 15 = 27 を得ます。
満足度が 28 以上になる品物の入手方法は存在しませんが、上記の例において 7 個目の品物のかわりに 6 個目の品物や 8 個目の品物を選んでも満足度 27 を得ることができます。
入力例 2
5 5 1 5 1 5 1 5 1 5 1 5
出力例 2
0
入力例 3
12 6 2 2 0 1 0 9 1 3 1 5 1 3 0 4 2 1 1 8 2 1 0 1 0 4
出力例 3
30
Score : 500 points
Problem Statement
There are N items.
Each of these is one of a pull-tab can, a regular can, or a can opener.
The i-th item is described by an integer pair (T_i, X_i) as follows:
- If T_i = 0, the i-th item is a pull-tab can; if you obtain it, you get a happiness of X_i.
- If T_i = 1, the i-th item is a regular can; if you obtain it and use a can opener against it, you get a happiness of X_i.
- If T_i = 2, the i-th item is a can opener; it can be used against at most X_i cans.
Find the maximum total happiness that you get by obtaining M items out of N.
Constraints
- 1 \leq M \leq N \leq 2 \times 10^5
- T_i is 0, 1, or 2.
- 1 \leq X_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M T_1 X_1 T_2 X_2 \vdots T_N X_N
Output
Print the answer as an integer.
Sample Input 1
8 4 0 6 0 6 1 3 1 5 1 15 2 1 2 10 2 100
Sample Output 1
27
If you obtain the 1-st, 2-nd, 5-th, and 7-th items, and use the 7-th item (a can opener) against the 5-th item, you will get a happiness of 6 + 6 + 15 = 27.
There are no ways to obtain items to get a happiness of 28 or greater, but you can still get a happiness of 27 by obtaining the 6-th or 8-th items instead of the 7-th in the combination above.
Sample Input 2
5 5 1 5 1 5 1 5 1 5 1 5
Sample Output 2
0
Sample Input 3
12 6 2 2 0 1 0 9 1 3 1 5 1 3 0 4 2 1 1 8 2 1 0 1 0 4
Sample Output 3
30