This page is still under construction.

Parts of this page are still being built. What you see may change.

Charm Bracelet

Interview

Time limit1sMemory limit128 MB

Summary
Choose a subset of N charms, each with a weight and a desirability, so that total weight stays within M and total desirability is maximized.
Level

Medium5 of 10

Topics
Dynamic programming, Array, Greedy, Brute force
Solved
No attempts yet

Problem

Bessie is at the mall's jewelry store and spots a charm bracelet. She would like to fill it with the best possible selection from the NN (1≤N≤34021 \le N \le 3402) available charms. Charm ii has a weight WiW_i (1≤Wi≤4001 \le W_i \le 400) and a desirability DiD_i (1≤Di≤1001 \le D_i \le 100), and each charm may be used at most once. The bracelet can support a total weight of at most MM (1≤M≤128801 \le M \le 12880).

Given the weight limit and the list of charms with their weights and desirabilities, determine the maximum possible sum of desirabilities.

Input

  • Line 1: two space-separated integers NN and MM.
  • Lines 2 to N+1N+1: line i+1i+1 contains two space-separated integers WiW_i and DiD_i describing charm ii.

Output

  • A single integer: the greatest total desirability that can be achieved without exceeding the weight limit.

Hint

In the sample, the optimal choice skips the second charm. Picking the charms of weight 11, 33, and 22 gives a desirability of 4+12+7=234 + 12 + 7 = 23 for a total weight of 1+3+2=61 + 3 + 2 = 6, which does not exceed the limit.

Examples3

  1. Example 1

    Input
    4 6
    1 4
    2 6
    3 12
    2 7
    
    Expected output
    23
    
  2. Example 2

    Input
    1 1
    1 100
    
    Expected output
    100
    
  3. Example 3

    Input
    1 5
    6 42
    
    Expected output
    0