Under construction

Still being built. Things may change

Joys of Trading

Time limit1sMemory limit1024 MB

Summary
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 NN resources, numbered from 11 to NN, and each village can independently produce any of them, though with different efficiencies. To produce one unit of resource ii, Apolyanka needs AiA_i person-hours, while Büdelsdorf needs BiB_i person-hours. Currently Apolyanka produces UiU_i units of resource ii in each time period, and Büdelsdorf produces WiW_i 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 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. Apolyanka works 4 person-hours: A1U1=2A_1 U_1 = 2 for 2 units of wood, and A2U2=2A_2 U_2 = 2 for 1 unit of food. Büdelsdorf works 16 person-hours: B1W1=4B_1 W_1 = 4 for 1 unit of wood, and B2W2=12B_2 W_2 = 12 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 3A1+0.5A2+0B1+4.5B2=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 has 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. 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 NN (1≤N≤1051 \le N \le 10^5), the number of resources. Each resource is identified by a distinct integer from 11 to NN. The ii-th of the next NN lines contains four integers AiA_i, UiU_i, BiB_i, and WiW_i (1≤Ai,Ui,Bi,Wi≤10001 \le A_i, U_i, B_i, W_i \le 1000 for i=1,2,…,Ni = 1, 2, \dots, N), 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 10−910^{-9}.

Examples2

  1. Example 1

    Input
    2
    1 2 4 1
    2 1 3 4
    
    Expected output
    17.5
    
  2. Example 2

    Input
    3
    1 1 2 1
    2 1 1 1
    1 1 1 1
    
    Expected output
    6