Charm Bracelet
InterviewTime limit1sMemory limit128 MB
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 () available charms. Charm has a weight () and a desirability (), and each charm may be used at most once. The bracelet can support a total weight of at most ().
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 and .
- Lines 2 to : line contains two space-separated integers and describing charm .
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 , , and gives a desirability of for a total weight of , which does not exceed the limit.