{"raw_statement":[{"iden":"problem statement","content":"There are $N$ items.  \nEach of these is one of a pull-tab can, a regular can, or a can opener.  \nThe $i$\\-th item is described by an integer pair $(T_i, X_i)$ as follows:\n\n*   If $T_i = 0$, the $i$\\-th item is a pull-tab can; if you obtain it, you get a happiness of $X_i$.\n*   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$.\n*   If $T_i = 2$, the $i$\\-th item is a can opener; it can be used against at most $X_i$ cans.\n\nFind the maximum total happiness that you get by obtaining $M$ items out of $N$."},{"iden":"constraints","content":"*   $1 \\leq M \\leq N \\leq 2 \\times 10^5$\n*   $T_i$ is $0$, $1$, or $2$.\n*   $1 \\leq X_i \\leq 10^9$\n*   All input values are integers."},{"iden":"input","content":"The input is given from Standard Input in the following format:\n\n$N$ $M$\n$T_1$ $X_1$\n$T_2$ $X_2$\n$\\vdots$\n$T_N$ $X_N$"},{"iden":"sample input 1","content":"8 4\n0 6\n0 6\n1 3\n1 5\n1 15\n2 1\n2 10\n2 100"},{"iden":"sample output 1","content":"27\n\nIf 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$.  \nThere 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."},{"iden":"sample input 2","content":"5 5\n1 5\n1 5\n1 5\n1 5\n1 5"},{"iden":"sample output 2","content":"0"},{"iden":"sample input 3","content":"12 6\n2 2\n0 1\n0 9\n1 3\n1 5\n1 3\n0 4\n2 1\n1 8\n2 1\n0 1\n0 4"},{"iden":"sample output 3","content":"30"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}