A regional power monitor samples the instantaneous load of n feeders. Each sample is measured to three decimal places (for example 12.345), but the utility's ledger can only store a whole-number reading per feeder. For every feeder you must record either the floor or the ceiling of its true reading, and the recorded integers must sum to exactly a given target total S. Among every combination of floor/ceiling choices whose recorded integers sum to S, report the smallest possible sum of |recorded - actual| taken over all n feeders, printed to exactly three decimal places. If no combination of choices can make the recorded integers sum to S, report that it cannot be done.
The first line contains two integers n and S, separated by a space. The second line contains n space-separated decimal strings, each with exactly three digits after the decimal point, giving the true load of each feeder.
If some combination of roundings makes the recorded integers sum to exactly S, print the minimum total rounding error as a decimal number with exactly three digits after the decimal point. Otherwise print -1.
Example 1
Input
3 5 1.500 0.900 2.100
Expected
0.700
Explanation
Floors are 1, 0, 2 (sum 3); every feeder has a nonzero fractional part, so the ceiling sum is 3+3=6, and target 5 needs exactly 2 feeders rounded up. Rounding up the two largest fractional parts (0.900 -> 1, error 0.100; 1.500 -> 2, error 0.500) and rounding the third down (2.100 -> 2, error 0.100) sums to 1+1+2=5 with total error 0.500+0.100+0.100=0.700, which is minimal.
Example 2
Input
2 100 1.200 1.300
Expected
-1
Explanation
Floors sum to 1+1=2 and both feeders have nonzero fractional parts, so the maximum achievable recorded sum is 2+2=4. Target 100 is far outside the feasible range [2, 4], so it is impossible and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →