Transport

Interview

Time limit1sMemory limit128 MB

Summary
With at most 20 items, choose a subset whose total weight is at most W and whose total value is as large as possible.
Level

Medium4 of 10

Topics
Brute force, Backtracking, Dynamic programming
Solved
No attempts yet

Problem

You have a transport plane that must deliver items to a remote location. You would like to load all of the items, but you cannot exceed the plane's weight capacity WW. Given nn items with known weights w1,w2,…,wnw_1, w_2, \dots, w_n and values v1,v2,…,vnv_1, v_2, \dots, v_n, find the most valuable subset of the items that fits into the plane without exceeding the capacity WW.

Input

The first line contains a positive integer indicating the number of problem sets. Each problem set begins with a line containing two positive integers nn and WW, where nn is the number of items and WW is the capacity of the plane. The next nn lines each contain two integers ww and vv, where ww is the weight and vv is the value of an item. All weights and values are positive integers, and the number of items in any problem set is at most 20.

Output

For each problem set, print on its own line the total value of the most valuable subset that the plane can transport without exceeding its capacity WW.

Examples3

  1. Example 1

    Input
    2
    3 5
    2 3
    2 2
    3 3
    4 10
    7 42
    3 12
    4 40
    5 25
    
    Expected output
    6
    65
    
  2. Example 2

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

    Input
    1
    1 3
    5 100
    
    Expected output
    0