#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 NN resources, numbered from 1 to NN, and each village is capable of independently producing any of them, albeit with different efficiencies. In order to produce one unit of resource ii, Apolyanka needs AiA_i person-hours, while Büdelsdorf needs BiB_i person-hours. Currently Apolyanka is producing UiU_i units of resource ii in each given time period, while Büdelsdorf is producing WiW_i 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 N=2N = 2, resource 1 is wood, resource 2 is food, A1=1A_1 = 1, U1=2U_1 = 2, B1=4B_1 = 4, W1=1W_1 = 1, A2=2A_2 = 2, U2=1U_2 = 1, B2=3B_2 = 3, and W2=4W_2 = 4. Then Apolyanka is doing 4 person-hours of work: A1U1=2A_1 \cdot U_1 = 2 for producing U1=2U_1 = 2 units of wood, and A2U2=2A_2 \cdot U_2 = 2 for producing U2=1U_2 = 1 unit of food. Similarly, Büdelsdorf is doing 16 person-hours of work: B1W1=4B_1 \cdot W_1 = 4 for producing W1=1W_1 = 1 unit of wood, and B2W2=12B_2 \cdot W_2 = 12 for producing W2=4W_2 = 4 units of food. Thus, the total production is U1+W1=3U_1 + W_1 = 3 units of wood and U2+W2=5U_2 + W_2 = 5 units of food, requiring 4+16=204 + 16 = 20 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 N=3N = 3 is A1=1A_1 = 1, B1=2B_1 = 2, A2=2A_2 = 2, B2=1B_2 = 1, A3=1A_3 = 1, B3=1B_3 = 1, and Ui=Wi=1U_i = W_i = 1 for i=1,2,3i = 1, 2, 3. 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 NN (1N1051 \le N \le 10^5) indicating the number of resources. Each resource is identified by a distinct integer from 1 to NN.

The ii-th of the next NN lines describes resource ii with four integers AiA_i, UiU_i, BiB_i and WiW_i (1Ai,Ui,Bi,Wi10001 \le A_i, U_i, B_i, W_i \le 1000 for i=1,2,,Ni = 1, 2, \dots, N), 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 10910^{-9}.

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