Knapsack of Artifacts

MEDIUM
G
Time limit: 2.0sMemory limit: 256MB
dpknapsack

Knapsack of Artifacts

An expedition has found n artifacts, artifact i weighing w_i and worth ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌​‌​‌‌​​‌​‌​​‌‌‌​​‌​​‌‌​​​‌​​‌‌​‌‌​​​‌‌​‌​​​‌‌​​‌​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌​​​​‌‌​​‌​​​‌‌​‌‌‌v_i. Your bag can carry a total weight of at most W. Each artifact can either be taken whole or left behind (no partial artifacts). Maximize the total value you can carry.

Input

Line ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌​‌​‌‌​​‌​‌​​‌‌‌​​‌​​‌‌​​​‌​​‌‌​‌‌​​​‌‌​‌​​​‌‌​​‌​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌​​​​‌‌​​‌​​​‌‌​‌‌‌1: two integers n W (1 ≤ n ≤ 500, 0 ≤ W ≤ 10^4) Next n lines: two integers w v (1 ≤ w ≤ 10^4, 1 ≤ v ≤ 10^4)

Output

A single integer: the maximum achievable total value.

Example

Input:

​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌​‌​‌‌​​‌​‌​​‌‌‌​​‌​​‌‌​​​‌​​‌‌​‌‌​​​‌‌​‌​​​‌‌​​‌​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌​​​​‌‌​​‌​​​‌‌​‌‌‌4 7
2 3
3 4
4 5
5 6

Output:

9

Constraints

  • 1 ≤ n ≤ 500, 0 ≤ W ≤ 10^4 ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌​‌​‌‌​​‌​‌​​‌‌‌​​‌​​‌‌​​​‌​​‌‌​‌‌​​​‌‌​‌​​​‌‌​​‌​​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌​​​​‌‌​​‌​​​‌‌​‌‌‌

Sample Testcases

Case 1

Input
4 7
2 3
3 4
4 5
5 6
Expected Output
9

Case 2

Input
3 50
10 60
20 100
30 120
Expected Output
220
Loading Cloud IDE...
Ln 1, Col 1Spaces: 4UTF-8
pythonAuto-Saved

Input

4 7
2 3
3 4
4 5
5 6

Expected Output

9

Run or submit your code to see output here.