Ferry Loading V
Time limit1sMemory limit128 MB
Given distinct vehicle weights, split them between two lanes so the totals differ as little as possible. Output the minimum difference.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Prefix sum, Math
- Solved
- No attempts yet
Problem
Vehicles are loaded onto a ferry in two lanes, a port (left) lane and a starboard (right) lane. Every vehicle has a distinct weight, and you want to split them between the two lanes so that the total weight on each lane is as close as possible. Compute the minimum possible difference between the total weight of the two lanes.
Input
The input contains several test cases. Each test case begins with a line containing the number of vehicles (). The next lines each give the weight of one vehicle in tonnes, one per line. Each weight is a positive number not exceeding , given to one digit after the decimal point (that is, in multiples of tonnes). Within a single test case all weights are distinct. A line containing a single follows the last test case.
Output
For each test case, print on its own line the minimum possible difference between the total weight of the two lanes, in tonnes, with exactly one digit after the decimal point.