Candy
InterviewTime limit1sMemory limit128 MB
Split multiset candies with counts and calorie values into two groups so the two calorie totals differ as little as possible.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation, Greedy, Math
- Solved
- No attempts yet
Problem
You and a friend share a big bag of candy, and you both want to stay slim. To be fair, you want to split all of the candy into two groups so that the two groups are as equal as possible in total calories.
The bag holds kinds of candy. For kind you have identical pieces, and every piece of that kind has calories. You assign each individual piece to one of the two groups (pieces of the same kind may be placed in different groups). Find the smallest possible difference between the total calories of the two groups.
Input
The first line contains the number of kinds of candy ().
Each of the next lines contains two integers and : the number of pieces of that kind () and the calories of each such piece ().
Output
Print one integer: the minimum possible difference in total calories between the two groups.
Hint
In the sample, one group takes the two -calorie candies (total ) and the other group keeps the remaining candies (total ). The difference is , which is the smallest achievable.