#abc373f. F - Knapsack with Diminishing Values

F - Knapsack with Diminishing Values

Score : 550550 points

问题陈述

NN 种物品。第 ii 种物品的重量为 wiw_i,价值为 viv_i。每种物品有 101010^{10} 个可用。

高桥打算选择一些物品并将它们放入容量为 WW 的背包中。他希望在避免选择太多同种物品的同时,最大化所选物品的价值。因此,他定义选择 kik_i 个第 ii 种物品的幸福度kiviki2k_i v_i - k_i^2。他希望在选择物品时最大化所有类型的总幸福度,同时保持总重量不超过 WW。计算他能够实现的最大总幸福度。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are NN types of items. The ii-th type of item has a weight of wiw_i and a value of viv_i. Each type has 101010^{10} items available.

Takahashi is going to choose some items and put them into a bag with capacity WW. He wants to maximize the value of the selected items while avoiding choosing too many items of the same type. Hence, he defines the happiness of choosing kik_i items of type ii as kiviki2k_i v_i - k_i^2. He wants to choose items to maximize the total happiness over all types while keeping the total weight at most WW. Calculate the maximum total happiness he can achieve.

Constraints

  • 1N30001 \leq N \leq 3000
  • 1W30001 \leq W \leq 3000
  • 1wiW1 \leq w_i \leq W
  • 1vi1091 \leq v_i \leq 10^9
  • All input values are integers.

Input

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

NN WW

w1w_1 v1v_1

w2w_2 v2v_2

\vdots

wNw_N vNv_N

Output

Print the answer.

Sample Input 1

2 10
3 4
3 2

Sample Output 1

5

By choosing 22 items of type 11 and 11 item of type 22, the total happiness can be 55, which is optimal.

Here, the happiness for type 11 is 2×422=42 \times 4 - 2^2 = 4, and the happiness for type 22 is 1×212=11 \times 2 - 1^2 = 1.

The total weight is 99, which is within the capacity 1010.

Sample Input 2

3 6
1 4
2 3
2 7

Sample Output 2

14

Sample Input 3

1 10
1 7

Sample Output 3

12
}