This page is still under construction.

Parts of this page are still being built. What you see may change.

Manure Teleporter

Time limit2sMemory limit512 MB

Summary
Choose y to minimize the total driving distance when each pile can be hauled directly or via a teleporter from 0 to y.
Level

Medium7 of 10

Topics
Greedy, Math, Sorting, Implementation
Solved
No attempts yet

Problem

The farm chore Farmer John dislikes most is hauling large amounts of manure. To cut the work down he builds a manure teleporter. Instead of driving a cart behind his tractor between two points, he sends manure from one location to another instantly.

Farmer John's farm lies along a single straight road, so every location on it is described by one coordinate along that road, a point on the number line. A teleporter is described by two numbers xx and yy: manure brought to location xx is transported instantly to location yy.

Farmer John puts one endpoint at x=0x = 0 and has to choose where the other endpoint yy goes. There are NN piles of manure on the farm (1≤N≤100,0001 \le N \le 100{,}000). Pile ii must be moved from position aia_i to position bib_i, and each pile is transported separately. Let did_i be the distance Farmer John drives with pile ii in his tractor. Hauling it directly gives di=∣ai−bi∣d_i = |a_i - b_i|. Using the teleporter means driving from aia_i to xx and then from yy to bib_i, which gives di=∣ai∣+∣y−bi∣d_i = |a_i| + |y - b_i|. Farmer John takes the shorter of the two routes. The same yy is used while every pile is transported.

Choose yy so the sum of the did_i is as small as possible, and report that sum.

Input

The first line contains NN. Each of the next NN lines contains aia_i and bib_i, both integers in the range −108-10^8 to 10810^8. The values are not necessarily distinct.

Output

Print the minimum sum of the did_i as a single integer. The value can exceed the range of a 32-bit integer, so use a 64-bit integer type in languages that need one.

Hint

In the first example, setting y=8y = 8 gives d1=2d_1 = 2, d2=5d_2 = 5 and d3=3d_3 = 3. Any yy between 77 and 1010 gives the same sum.

Examples2

  1. Example 1

    Input
    3
    -5 -7
    -3 10
    -2 7
    
    Expected output
    10
    
  2. Example 2

    Input
    2
    0 20
    0 -20
    
    Expected output
    20