#abc312f. F - Cans and Openers

F - Cans and Openers

Score : 500500 points

问题描述

NN 件物品。
其中每一件是易拉罐、普通罐头或开罐器之一。
ii 个物品由一个整数对 (Ti,Xi)(T_i, X_i) 描述如下:

  • 如果 Ti=0T_i = 0,则第 ii 个物品是一个易拉罐;如果你获取它,你会得到幸福度为 XiX_i
  • 如果 Ti=1T_i = 1,则第 ii 个物品是一个普通罐头;如果你获取它并用开罐器打开它,你会得到幸福度为 XiX_i
  • 如果 Ti=2T_i = 2,则第 ii 个物品是一个开罐器;它可以最多用来打开 XiX_i 个罐头。

NN 件物品中选取 MM 件,求能获得的最大总幸福度。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN items.
Each of these is one of a pull-tab can, a regular can, or a can opener.
The ii-th item is described by an integer pair (Ti,Xi)(T_i, X_i) as follows:

  • If Ti=0T_i = 0, the ii-th item is a pull-tab can; if you obtain it, you get a happiness of XiX_i.
  • If Ti=1T_i = 1, the ii-th item is a regular can; if you obtain it and use a can opener against it, you get a happiness of XiX_i.
  • If Ti=2T_i = 2, the ii-th item is a can opener; it can be used against at most XiX_i cans.

Find the maximum total happiness that you get by obtaining MM items out of NN.

Constraints

  • 1MN2×1051 \leq M \leq N \leq 2 \times 10^5
  • TiT_i is 00, 11, or 22.
  • 1Xi1091 \leq X_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

T1T_1 X1X_1

T2T_2 X2X_2

\vdots

TNT_N XNX_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 11-st, 22-nd, 55-th, and 77-th items, and use the 77-th item (a can opener) against the 55-th item, you will get a happiness of 6+6+15=276 + 6 + 15 = 27.
There are no ways to obtain items to get a happiness of 2828 or greater, but you can still get a happiness of 2727 by obtaining the 66-th or 88-th items instead of the 77-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

update @ 2024/3/10 08:54:03