Ordinary Knapsack
InterviewTime limit2sMemory limit512 MB
Given N items with weights and values, pick a subset with total weight at most K to maximize total value.
- Level
Easy3 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
Junseo enlists in a month. He wants to take one trip before then, and since he will be away from the world for a while he wants to enjoy the trip as much as he can, so he packs his backpack for the highest possible value.
Junseo has items he thinks he needs for the trip. Each item has a weight and a value , and carrying that item in the backpack lets Junseo enjoy the trip by . Junseo has never marched before, so he can only carry a backpack whose packed weight sums to at most .
There is only one copy of each item, so the only choice per item is to pack it or leave it. Find the largest total value of the items Junseo can pack.
Input
The first line contains the number of items and the weight that Junseo can bear. (, )
Each of the next lines contains the weight and the value of one item. (, )
Every number in the input is an integer.
Output
Print the largest total value of the items that can be packed, on one line.