Joys of Trading

시간 제한1초메모리 제한1024 MB

요약
두 마을이 자원별 단위당 작업 시간과 현재 생산량을 가질 때, 각 자원의 총생산량을 그대로 유지하면서 필요한 최소 총 작업 시간을 분수 생산을 허용해 구한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 수학, 구현
정답자
아직 제출이 없습니다

문제

Apolyanka and Büdelsdorf are two small neolithic villages that have recently come into contact. There are NN resources, numbered from 11 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 A_iA\_i person-hours, while Büdelsdorf needs B_iB\_i person-hours. Currently Apolyanka is producing U_iU\_i units of resource ii in each given time period, while Büdelsdorf is producing W_iW\_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 work and exchange resources among them.

For example, suppose N=2N = 2, resource 11 is wood, resource 22 is food, A_1=1A\_1 = 1, U_1=2U\_1 = 2, B_1=4B\_1 = 4, W_1=1W\_1 = 1, A_2=2A\_2 = 2, U_2=1U\_2 = 1, B_2=3B\_2 = 3, and W_2=4W\_2 = 4. Then Apolyanka is doing 44 person-hours of work: A_1⋅U_1=2A\_1 \cdot U\_1 = 2 for producing U_1=2U\_1 = 2 units of wood, and A_2⋅U_2=2A\_2 \cdot U\_2 = 2 for producing U_2=1U\_2 = 1 unit of food. Similarly, Büdelsdorf is doing 1616 person-hours of work: B_1⋅W_1=4B\_1 \cdot W\_1 = 4 for producing W_1=1W\_1 = 1 unit of wood, and B_2⋅W_2=12B\_2 \cdot W\_2 = 12 for producing W_2=4W\_2 = 4 units of food. Thus, the total production is U_1+W_1=3U\_1 + W\_1 = 3 units of wood and U_2+W_2=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 33 units of wood and 0.50.5 units of food, while Büdelsdorf could produce no wood and 4.54.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.53A\_1+0.5A\_2+0B\_1+4.5B\_2 = 3 + 1 + 13.5 = 17.5 person-hours.

Another example with N=3N = 3 is A_1=1A\_1 = 1, B_1=2B\_1 = 2, A_2=2A\_2 = 2, B_2=1B\_2 = 1, A_3=1A\_3 = 1, B_3=1B\_3 = 1, and U_i=W_i=1U\_i = W\_i = 1 for i=1,2,3i = 1, 2, 3. In this case, each village is currently working 44 person-hours. With a slight reorganization however, they can each work 33 person-hours while producing the exact same total resources! All that is required is for Apolyanka to produce one less unit of resource 22 and one more of resource 11, 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 (1≤N≤1051 ≤ N ≤ 10^5) indicating the number of resources. Each resource is identified by a distinct integer from 11 to NN.

The ii-th of the next NN lines describes resource ii with four integers A_iA\_i, U_iU\_i, B_iB\_i and W_iW\_i (1≤A_i,U_i,B_i,W_i≤10001 ≤ A\_i , U\_i , B\_i , W\_i ≤ 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 10−910^{-9 }.

예제2

  1. 예제 1

    입력
    2
    1 2 4 1
    2 1 3 4
    
    예상 출력
    17.5
    
  2. 예제 2

    입력
    3
    1 1 2 1
    2 1 1 1
    1 1 1 1
    
    예상 출력
    6