#P15124. [ICPC 2024 LAC] Joys of Trading
[ICPC 2024 LAC] Joys of Trading
题目描述
Apolyanka and Büdelsdorf are two small neolithic villages that have recently come into contact. There are resources, numbered from 1 to , and each village is capable of independently producing any of them, albeit with different efficiencies. In order to produce one unit of resource , Apolyanka needs person-hours, while Büdelsdorf needs person-hours. Currently Apolyanka is producing units of resource in each given time period, while Büdelsdorf is producing units.
Each village is currently working at maximum capacity, that is, there is no way they can put more person-hours to work than they are employing now. However, through the recently discovered benefits of trade, it is possible for both villages to produce all the resources they need while reducing the total person-hours worked, and thus becoming able to spend those freed person-hours resting and playing some games. All that is needed is that the villages cooperate, coordinate and exchange resources among them.
For example, suppose , resource 1 is wood, resource 2 is food, , , , , , , , and . Then Apolyanka is doing 4 person-hours of work: for producing units of wood, and for producing unit of food. Similarly, Büdelsdorf is doing 16 person-hours of work: for producing unit of wood, and for producing units of food. Thus, the total production is units of wood and units of food, requiring person-hours.
However, a better organization is possible: Apolyanka could produce 3 units of wood and 0.5 units of food, while Büdelsdorf could produce no wood and 4.5 units of food. The total production of each resource would be the same, but requiring only $3A_1 + 0.5A_2 + 0B_1 + 4.5B_2 = 3 + 1 + 13.5 = 17.5$ person-hours.
Another example with is , , , , , , and for . In this case, each village is currently working 4 person-hours. With a slight reorganization however, they can each work 3 person-hours while producing the exact same total resources! All that is required is for Apolyanka to produce one less unit of resource 2 and one more of resource 1, while Büdelsdorf does the opposite.
Given all of these values, can you compute what is the minimum total number of person-hours that the villages have to work, in order to produce exactly the same total resources? Note that the number of person-hours invested in producing a resource is not required to be integer.
输入格式
The first line contains an integer () indicating the number of resources. Each resource is identified by a distinct integer from 1 to .
The -th of the next lines describes resource with four integers , , and ( for ), as explained in the statement.
输出格式
Output a single line with the minimum total number of person-hours required to produce the resources. The output must have an absolute or relative error of at most .
2
1 2 4 1
2 1 3 4
17.5
3
1 1 2 1
2 1 1 1
1 1 1 1
6