QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 256 MB Puntuación total: 100 Hackeable ✓

#18961. Please, Don't Climb the Tower Again 2

Estadísticas

Asrit is playing Slay the Spire 2. He has only $1$ HP left, and the monster is about to attack him, but the hand he drew consists entirely of attack cards.

Asrit has $n$ cards in hand and $m$ energy points. Each card has two attributes: energy cost and damage. There are only $3$ possible energy costs: 1, 2, or X. The exact rules are as follows:

  • If a card's energy cost is 1 and its damage is $d$, it can be played when the remaining energy is $\ge 1$. After playing it, $1$ energy is consumed and it deals $d$ damage.
  • If a card's energy cost is 2 and its damage is $d$, it can be played when the remaining energy is $\ge 2$. After playing it, $2$ energy is consumed and it deals $d$ damage.
  • If a card's energy cost is X and its damage is $d$, it can be played when the remaining energy is $> 0$. After playing it, all remaining energy is consumed, and each $1$ energy consumed deals $d$ damage (i.e., the total damage is $d \times \text{amount of consumed energy}$).

Asrit's game has not been taken over by Waku, so he can play any cards satisfying the conditions in any order, and cards that have already been played will no longer remain in his hand.

Please tell Asrit the maximum damage his hand can deal, so that he can determine whether he will return to the bottom of the Spire.

Input

The first line contains two integers $n, m$ $(1 \le n \le 10^6, 1 \le m \le 2 \times 10^6)$, representing the number of cards in hand and the initial energy.

The next $n$ lines each contain a character $op$ and an integer $d$ $(op \in \{\texttt{1}, \texttt{2}, \texttt{X}\}, 1 \le d \le 10^{12})$, representing a card's energy cost type and damage.

Output

Output a single line containing an integer, the maximum damage that can be dealt.

Examples

Input 1

5 5
1 6
1 6
1 13
2 12
2 13

Output 1

38

Input 2

5 3
1 6
1 6
1 9
2 21
X 11

Output 2

33

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.