All problems

Problem 756

Unbounded Knapsack

Medium · 20 pts · Dynamic Programming, Knapsack

Problem

You have N kinds of items, each available in unlimited quantity; kind i has weight W[i] and value V[i]. Choose items with total weight at most CAP and maximise the total value. Unlike 0/1 knapsack, the same kind may be taken many times. Let best[c] be the largest value with capacity c; for every c from 1 to CAP, try adding one item of each kind that fits: best[c] = max(best[c], best[c - W[i]] + V[i]).

Input

The first line contains N and CAP. The second line contains the N weights. The third line contains the N values.

Output

Print the maximum total value.

Constraints

1 <= N <= 1000; 1 <= CAP <= 1000; 1 <= W[i], V[i] <= 1000

Example input

3 10
2 4 6
5 11 13

Example output

27

Explanation

With weights 2 4 6, values 5 11 13 and CAP = 10, take two items of weight 4 (value 22) and one of weight 2 (value 5), for a total value of 27 at weight 10.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Subsets with Sum K Next: Print Longest Common Subsequence