Joys of Trading
Time limit1sMemory limit1024 MB
Split each resource's output between two villages, staying within each village's current person-hours, to minimize total person-hours.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Binary search
- Solved
- No attempts yet
Statement
Apolyanka and Büdelsdorf are two small neolithic villages that have recently come into contact. There are resources, numbered from to , and each village can independently produce any of them, though with different efficiencies. To produce one unit of resource , Apolyanka needs person-hours, while Büdelsdorf needs person-hours. Currently Apolyanka produces units of resource in each time period, and Büdelsdorf produces units. Each village already works at maximum capacity, so neither can put more person-hours to work than it employs now. Through trade, however, both villages can still produce everything they need while reducing the total person-hours worked, which frees time for rest and games. All that is needed is for the villages to cooperate, coordinate their work, and exchange resources.
For example, suppose , resource 1 is wood, resource 2 is food, , , , , , , , and . Apolyanka works 4 person-hours: for 2 units of wood, and for 1 unit of food. Büdelsdorf works 16 person-hours: for 1 unit of wood, and for 4 units of food. The total output is 3 units of wood and 5 units of food, using 20 person-hours.
A better organization exists. Apolyanka produces 3 units of wood and 0.5 units of food, and Büdelsdorf produces no wood and 4.5 units of food. The totals stay the same, but the work drops to person-hours.
Another example with has , , , , , , and for . Each village works 4 person-hours. With a small reorganization, each can work 3 person-hours while producing the same total resources. Apolyanka makes one less unit of resource 2 and one more unit of resource 1, and Büdelsdorf does the opposite.
Given these values, compute the minimum total number of person-hours the villages must work to produce exactly the same total resources. The person-hours spent on a resource need not be an integer.
Input
The first line contains an integer (), the number of resources. Each resource is identified by a distinct integer from to . The -th of the next lines contains four integers , , , and ( for ), as described in the statement.
Output
Output a single line with the minimum total number of person-hours required to produce the resources. The answer must have an absolute or relative error of at most .