Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
ある提案に対し、N 人の人が賛成か反対かを表明しています。なお、N は奇数です。
i \, (i = 1, 2, \dots, N) 番目の人の意見は文字列 S_i で表され、S_i = For のとき賛成しており、S_i = Against のとき反対しています。
過半数の人がこの提案に賛成しているかどうかを判定してください。
制約
- N は 1 以上 99 以下の奇数
- 全ての i = 1, 2, \dots, N に対し、S_i =
Forまたは S_i =Against
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 人のうち過半数が提案に賛成しているならば Yes、そうでなければ No と出力せよ。
入力例 1
3 For Against For
出力例 1
Yes
提案に賛成している人数は 2 人であり、これは半数を超えているので Yes と出力します。
入力例 2
5 Against Against For Against For
出力例 2
No
提案に賛成している人数は 2 人であり、これは半数以下なので No と出力します。
入力例 3
1 For
出力例 3
Yes
Score : 100 points
Problem Statement
There are N people. Each of them agrees or disagrees with a proposal. Here, N is an odd number.
The i-th (i = 1, 2, \dots, N) person's opinion is represented by a string S_i: the person agrees if S_i = For and disagrees if S_i = Against.
Determine whether the majority agrees with the proposal.
Constraints
- N is an odd number between 1 and 99, inclusive.
- S_i =
Foror S_i =Against, for all i = 1, 2, \dots, N.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print Yes if the majority of the N people agree with the proposal; print No otherwise.
Sample Input 1
3 For Against For
Sample Output 1
Yes
The proposal is supported by two people, which is the majority, so Yes should be printed.
Sample Input 2
5 Against Against For Against For
Sample Output 2
No
The proposal is supported by two people, which is not the majority, so No should be printed.
Sample Input 3
1 For
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。S の末尾の文字を出力してください。
制約
- N は整数
- 1 ≤ N ≤ 1000
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S の末尾の文字を出力せよ。
入力例 1
5 abcde
出力例 1
e
S = {}abcde です。S の末尾の文字は e なので、e を出力します。
入力例 2
1 a
出力例 2
a
Score : 100 points
Problem Statement
Given a string S of length N consisting of lowercase English alphabets, print the last character of S.
Constraints
- N is an integer.
- 1 ≤ N ≤ 1000
- S is a string of length N consisting of lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the last character of S.
Sample Input 1
5 abcde
Sample Output 1
e
The last character of S = {}abcde is e, so e should be printed.
Sample Input 2
1 a
Sample Output 2
a
Time Limit: 2 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数 A,B が K 進法表記で与えられます。
A \times B を 10 進法表記で出力してください。
注記
K 進法表記については、Wikipedia「位取り記数法」 を参照してください。
制約
- 2 \leq K \leq 10
- 1 \leq A,B \leq 10^5
- A,B は K 進法表記で与えられる
入力
入力は以下の形式で標準入力から与えられる。
K A B
出力
答えを出力せよ。
入力例 1
2 1011 10100
出力例 1
220
2 進法表記の 1011 を 、10 進法表記すると 11 です。
2 進法表記の 10100 を、 10 進法表記すると 20 です。
11 \times 20 = 220 なので 220 を出力します。
入力例 2
7 123 456
出力例 2
15642
7 進法表記の 123 を 、10 進法表記すると 66 です。
7 進法表記の 456 を、 10 進法表記すると 237 です。
66 \times 237 = 15642 なので 15642 を出力します。
Score : 200 points
Problem Statement
You are given integers A and B, in base K.
Print A \times B in decimal.
Notes
For base-K representation, see Wikipedia's article on Positional notation.
Constraints
- 2 \leq K \leq 10
- 1 \leq A,B \leq 10^5
- A and B are in base-K representation.
Input
Input is given from Standard Input in the following format:
K A B
Output
Print the answer.
Sample Input 1
2 1011 10100
Sample Output 1
220
1011 in base 2 corresponds to 11 in base 10.
10100 in base 2 corresponds to 20 in base 10.
We have 11 \times 20 = 220, so print 220.
Sample Input 2
7 123 456
Sample Output 2
15642
123 in base 7 corresponds to 66 in base 10.
456 in base 7 corresponds to 237 in base 10.
We have 66 \times 237 = 15642, so print 15642.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 人の生徒が 4 日間にわたる試験を受けています。
それぞれの日に行われる試験は 300 点満点です。すなわち、4 日間を通した試験の満点は 1200 点です。
現在 3 日目までの試験が終わり、これから 4 日目の試験が行われようとしています。i \, (1 \leq i \leq N) 番目の生徒は j \, (1 \leq j \leq 3) 日目の試験で P_{i, j} 点獲得しました。
それぞれの生徒について、4 日目の試験後に上位 K 位以内に入っていることがあり得るかどうか判定してください。
ただし、4 日目の試験後の生徒の順位は、その生徒よりも 4 日間の合計点が高い生徒の人数に 1 を加えた値として定めます。
制約
- 1 \leq K \leq N \leq 10^5
- 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}
出力
N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 番目の生徒が 4 日目の試験後に上位 K 位以内に入っていることがあり得るならば Yes と、そうでないならば No と出力せよ。
入力例 1
3 1 178 205 132 112 220 96 36 64 20
出力例 1
Yes Yes No
4 日目に全員が 100 点を取ると、1 番目の生徒が 1 位になります。 4 日目に 2 番目の生徒が 100 点を取り、それ以外の生徒が 0 点を取ると、2 番目の生徒が 1 位になります。 3 番目の生徒が 1 位になることはあり得ません。
入力例 2
2 1 300 300 300 200 200 200
出力例 2
Yes Yes
入力例 3
4 2 127 235 78 192 134 298 28 56 42 96 120 250
出力例 3
Yes Yes No Yes
Score : 300 points
Problem Statement
N students are taking a 4-day exam.
There is a 300-point test on each day, for a total of 1200 points.
The first three days of the exam are already over, and the fourth day is now about to begin. The i-th student (1 \leq i \leq N) got P_{i, j} points on the j-th day (1 \leq j \leq 3).
For each student, determine whether it is possible that he/she is ranked in the top K after the fourth day.
Here, the rank of a student after the fourth day is defined as the number of students whose total scores over the four days are higher than that of the student, plus 1.
Constraints
- 1 \leq K \leq N \leq 10^5
- 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if it is possible that the i-th student is ranked in the top K after the fourth day, and No otherwise.
Sample Input 1
3 1 178 205 132 112 220 96 36 64 20
Sample Output 1
Yes Yes No
If every student scores 100 on the fourth day, the 1-st student will rank 1-st.
If the 2-nd student scores 100 and the other students score 0 on the fourth day, the 2-nd student will rank 1-st.
The 3-rd student will never rank 1-st.
Sample Input 2
2 1 300 300 300 200 200 200
Sample Output 2
Yes Yes
Sample Input 3
4 2 127 235 78 192 134 298 28 56 42 96 120 250
Sample Output 3
Yes Yes No Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder社ではカードを使った 1 人ゲームが流行っています。
ゲームで使う各カードには、英小文字 1 文字または @ の文字が書かれており、いずれのカードも十分多く存在します。
ゲームは以下の手順で行います。
- カードを同じ枚数ずつ 2 列に並べる。
@のカードを、それぞれa,t,c,o,d,e,rのいずれかのカードと置き換える。- 2 つの列が一致していれば勝ち。そうでなければ負け。
このゲームに勝ちたいあなたは、次のようなイカサマをすることにしました。
- 手順 1 以降の好きなタイミングで、列内のカードを自由に並び替えてよい。
手順 1 で並べられた 2 つの列を表す 2 つの文字列 S,T が与えられるので、イカサマをしてもよいときゲームに勝てるか判定してください。
制約
- S,T は英小文字と
@からなる - S,T の長さは等しく 1 以上 2\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
イカサマをしてもよいとき、ゲームに勝てるなら Yes、勝てないなら No と出力せよ。
入力例 1
ch@ku@ai choku@@i
出力例 1
Yes
@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。
入力例 2
ch@kud@i akidu@ho
出力例 2
Yes
イカサマをし、@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。
入力例 3
aoki @ok@
出力例 3
No
イカサマをしても勝つことはできません。
入力例 4
aa bb
出力例 4
No
Score : 300 points
Problem Statement
A single-player card game is popular in AtCoder Inc.
Each card in the game has a lowercase English letter or the symbol @ written on it. There is plenty number of cards for each kind.
The game goes as follows.
- Arrange the same number of cards in two rows.
- Replace each card with
@with one of the following cards:a,t,c,o,d,e,r. - If the two rows of cards coincide, you win. Otherwise, you lose.
To win this game, you will do the following cheat.
- Freely rearrange the cards within a row whenever you want after step 1.
You are given two strings S and T, representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.
Constraints
- S and T consist of lowercase English letters and
@. - The lengths of S and T are equal and between 1 and 2\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S T
Output
If it is possible to win with cheating allowed, print Yes; otherwise, print No.
Sample Input 1
ch@ku@ai choku@@i
Sample Output 1
Yes
You can replace the @s so that both rows become chokudai.
Sample Input 2
ch@kud@i akidu@ho
Sample Output 2
Yes
You can cheat and replace the @s so that both rows become chokudai.
Sample Input 3
aoki @ok@
Sample Output 3
No
You cannot win even with cheating.
Sample Input 4
aa bb
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
A, B, ? からなる N 文字の文字列 S が与えられます。
正整数 K が与えられます。
A, B からなる文字列 T が次の条件を満たすとき、T は良い文字列であるということにします。
- T の長さ K の連続する部分文字列で、回文であるものが存在しない。
S に含まれる ? の個数を q 個とします。
q 個の ? をそれぞれ A, B のどちらかに置き換えて得られる文字列は 2 ^ q 通りありますが、その中に良い文字列がいくつあるか求めてください。
ただし、答えは非常に大きくなる場合があるので、998244353 で割った余りを求めてください。
制約
- 2\leq K\leq N\leq 1000
- K\leq 10
- S は
A,B,?からなる文字列 - S の長さは N
- N,K は整数
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
7 4 AB?A?BA
出力例 1
1
与えられた文字列の中に ? は 2 個あります。
2 個の ? をそれぞれ A, B のどちらかに置き換えて得られる文字列は次の 4 通りあります。
ABAAABAABAABBAABBAABAABBABBA
このうち、最初の ABAAABA 以外の 3 つの文字列は、長さ 4 の回文 ABBA を連続する部分文字列として含むため、良い文字列ではありません。
よって、1 を出力してください。
入力例 2
40 7 ????????????????????????????????????????
出力例 2
116295436
良い文字列の個数を 998244353 で割った余りを求めることに注意してください。
入力例 3
15 5 ABABA??????????
出力例 3
0
? をどのように置き換えても良い文字列にならないこともあります。
入力例 4
40 8 ?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA
出力例 4
259240
Score : 450 points
Problem Statement
You are given a string S of length N consisting of characters A, B, and ?.
You are also given a positive integer K.
A string T consisting of A and B is considered a good string if it satisfies the following condition:
- No contiguous substring of length K in T is a palindrome.
Let q be the number of ? characters in S.
There are 2^q strings that can be obtained by replacing each ? in S with either A or B. Find how many of these strings are good strings.
The count can be very large, so find it modulo 998244353.
Constraints
- 2 \leq K \leq N \leq 1000
- K \leq 10
- S is a string consisting of
A,B, and?. - The length of S is N.
- N and K are integers.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
7 4 AB?A?BA
Sample Output 1
1
The given string has two ?s.
There are four strings obtained by replacing each ? with A or B:
ABAAABAABAABBAABBAABAABBABBA
Among these, the last three contain the contiguous substring ABBA of length 4, which is a palindrome, and thus are not good strings.
Therefore, you should print 1.
Sample Input 2
40 7 ????????????????????????????????????????
Sample Output 2
116295436
Ensure to find the number of good strings modulo 998244353.
Sample Input 3
15 5 ABABA??????????
Sample Output 3
0
It is possible that there is no way to replace the ?s to obtain a good string.
Sample Input 4
40 8 ?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA
Sample Output 4
259240
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
H 行 W 列の格子状の区画に区切られた街があります。上から i 行目、左から j 列目の区画は、S_{i,j} が . のとき道、# のとき塀です。
高橋君は自分の家から魚屋に買い物に行くことにしました。高橋君の家は街の左上隅の区画にあり、魚屋は街の右下隅の区画にあります。
高橋君は、自分がいる区画から上下左右に隣接する道の区画に移動することができます。街の外に出ることはできません。
塀の区画に移動することはできませんが、高橋君は非常に腕力が強いため、パンチを 1 回繰り出すことで任意の 2\times 2 の区画内の塀を壊して道にすることができます。
高橋君が魚屋にたどり着くためには、最低何回パンチを繰り出せばよいか求めてください。
制約
- 2 \leq H,W \leq 500
- H,W は整数
- S_{i,j} は
.または# - S_{1,1} と S_{H,W} は
.
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1} \ldots S_{1,W}
\vdots
S_{H,1} \ldots S_{H,W}
出力
答えを出力せよ。
入力例 1
5 5 ..#.. #.#.# ##.## #.#.# ..#..
出力例 1
1
例えば、以下の * で表す 2\times 2 の区画にある塀を破壊すると魚屋にたどり着くことができます。
..#.. #.**# ##**# #.#.# ..#..
破壊対象の 2 \times 2 の区画の全てが塀である必要はありません。
入力例 2
5 7 ....... ######. ....... .###### .......
出力例 2
0
遠回りが必要ですが、塀を破壊することなく魚屋にたどり着くことができます。
入力例 3
8 8 .####### ######## ######## ######## ######## ######## ######## #######.
出力例 3
5
Score : 500 points
Problem Statement
There is a town divided into a grid of cells with H rows and W columns. The cell at the i-th row from the top and j-th column from the left is a passable space if S_{i,j} is . and a block if S_{i,j} is #.
Takahashi will go from his house to a fish market. His house is in the cell at the top-left corner, and the fish market is in the cell at the bottom-right corner.
Takahashi can move one cell up, down, left, or right to a passable cell. He cannot leave the town. He cannot enter a block, either. However, his physical strength allows him to destroy all blocks in a square region with 2\times 2 cells of his choice with one punch, making these cells passable.
Find the minimum number of punches needed for Takahashi to reach the fish market.
Constraints
- 2 \leq H,W \leq 500
- H and W are integers.
- S_{i,j} is
.or#. - S_{1,1} and S_{H,W} are
..
Input
Input is given from Standard Input in the following format:
H W
S_{1,1} \ldots S_{1,W}
\vdots
S_{H,1} \ldots S_{H,W}
Output
Print the answer.
Sample Input 1
5 5 ..#.. #.#.# ##.## #.#.# ..#..
Sample Output 1
1
He can reach the fish market by, for example, destroying the blocks in the square region with 2\times 2 cells marked * below.
..#.. #.**# ##**# #.#.# ..#..
It is not required that all of the 2\times 2 cells in the region to punch are blocks.
Sample Input 2
5 7 ....... ######. ....... .###### .......
Sample Output 2
0
He can reach the fish market without destroying blocks, though he has to go a long way around.
Sample Input 3
8 8 .####### ######## ######## ######## ######## ######## ######## #######.
Sample Output 3
5
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