373 传统题

[ABC373F] 收益递减背包 (Knapsack with Diminishing Values)

时间限制
3000ms
内存限制
1024MiB
登顶 / 尝试
1/ 1
难度
6
    上传者 标签AtCoderABC373提高+动态规划堆背包

[ABC373F] 收益递减背包 (Knapsack with Diminishing Values)

Score : 550550 points

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 kivi−ki2k_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

  • 1≤N≤30001 \leq N \leq 3000

  • 1≤W≤30001 \leq W \leq 3000

  • 1≤wi≤W1 \leq w_i \leq W

  • 1≤vi≤1091 \leq v_i \leq 10^9

  • All input values are integers.

Input

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

$N$ $W$
$w_1$ $v_1$
$w_2$ $v_2$
$\vdots$
$w_N$ $v_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×4−22=42 \times 4 - 2^2 = 4, and the happiness for type 22 is 1×2−12=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