Scales
InterviewTime limit1sMemory limit1024 MB
Given N weights where each one is at least the sum of the two before it, find the largest subset sum that does not exceed the limit C.
- Level
Medium5 of 10
- Topics
- Greedy, Backtracking
- Solved
- No attempts yet
Problem
Farmer John has a balance for weighing cows. He also has a set of weights () with known masses. Every mass fits in 31 bits. To weigh a cow, he places the cow on one side of the balance and adds weights to the other side until the two sides balance. Weights cannot go on the same side as the cow, because cows kick weights in his face whenever they can.
The balance has a maximum mass rating. If the total mass on one side exceeds (), the balance breaks.
The weights have a special property. When sorted from smallest to largest, each weight from the third one onward has a mass at least as large as the sum of the previous two weights.
Find the largest mass that can be measured exactly with these weights. The total mass used must not exceed , so he may not be able to place every weight on the scale. Given the list of weights and the maximum mass , output the largest legal mass that can be weighed exactly.
Input
The first line contains two positive integers and , separated by a space.
Each of the next lines contains one positive integer, the mass of one weight. The masses are given in non-decreasing order.
Output
Print one integer on the first line: the largest mass that can be measured accurately and safely.