Problem:
Score : 1000 points

Problem Statement

AtCoDeer the deer found N rectangle lying on the table, each with height 1.
If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure:

AtCoDeer will move these rectangles horizontally so that all the rectangles are connected.
For each rectangle, the cost to move it horizontally by a distance of x, is x.
Find the minimum cost to achieve connectivity.
It can be proved that this value is always an integer under the constraints of the problem.

Constraints

All input values are integers.

1≤N≤10^5

1≤l_i<r_i≤10^9

Partial Score

300 points will be awarded for passing the test set satisfying 1≤N≤400 and 1≤l_i<r_i≤400.

Input

The input is given from Standard Input in the following format:

N
l_1 r_1
l_2 r_2
:
l_N r_N

Output

Print the minimum cost to achieve connectivity.

Sample Input 1

3
1 3
5 7
1 3

Sample Output 1

2

The second rectangle should be moved to the left by a distance of 2.

Sample Input 2

3
2 5
4 6
1 4

Sample Output 2

0

The rectangles are already connected, and thus no move is needed.

Sample Input 3

5
999999999 1000000000
1 2
314 315
500000 500001
999999999 1000000000

Sample Output 3

1999999680

Sample Input 4

5
123456 789012
123 456
12 345678901
123456 789012
1 23

Sample Output 4

246433

Sample Input 5

1
1 400

Sample Output 5

0

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;

class Solver
{
    static Scanner cin;
    public Solver() {}
    public static int Main()
    {
        cin = new Scanner();
        new Solver().solve();
        return 0;
    }
 
    public void solve()
    {
        int n;
        n = cin.nextInt();
        long[] l, r, mid, width;
        l = new long[n];
        r = new long[n];
        mid = new long[n];
        width = new long[n];
        for (int i = 0; i < n; ++i) {
            l[i] = cin.nextLong();
            r[i] = cin.nextLong();
            l[i] *= 2;
            r[i] *= 2;
            width[i] = r[i] - l[i];
            mid[i] = (r[i] + l[i]) / 2;
        }
        long leftOffset, rightOffset;
        leftOffset = rightOffset = 0;
        long minValue = 0;
        
        var left = new PriorityQueue<long>();
        var right = new PriorityQueue<long>((long x, long y) => x > y);
        
        left.Push(mid[0]);
        right.Push(mid[0]);
        
        for (int i = 1; i < n; ++i) {
            long myWidth = (width[i] + width[i-1]) / 2;
            leftOffset -= myWidth;
            rightOffset += myWidth;
            long maxLeft = left.Top();
            long minRight = right.Top();
            
            if (maxLeft + leftOffset <= mid[i] && mid[i] <= minRight + rightOffset) {
                left.Push(mid[i] - leftOffset);
                right.Push(mid[i] - rightOffset);
            } else {
                if (mid[i] < maxLeft + leftOffset) {
                    minValue += maxLeft + leftOffset - mid[i];
                    
                    right.Push(maxLeft + leftOffset - rightOffset);
                    left.Pop();
                    left.Push(mid[i] - leftOffset);
                    left.Push(mid[i] - leftOffset);
                } else {
                    minValue += mid[i] - (minRight + rightOffset);
                    
                    left.Push(minRight + rightOffset - leftOffset);
                    right.Pop();
                    right.Push(mid[i] - rightOffset);
                    right.Push(mid[i] - rightOffset);
                }
            }
        }
        
        Console.WriteLine(minValue / 2);
    }
}

class PriorityQueue<T> where T: IComparable, new()
{
    private List<T> _heap;
    private Func<T, T, bool> _compare;
    
    public PriorityQueue(Func<T, T, bool> compare)
    {
        this.Init();
        _compare = compare;
    }
    
    public PriorityQueue()
    {
        this.Init();
        this._compare = (T x, T y) => x.CompareTo(y) < 0;
    }
    
    public void Init()
    {
        _heap = new List<T>();
        _heap.Add(new T());
    }
    
    public void Push(T x)
    {
        _heap.Add(x);
        for (int i = _heap.Count - 1; i > 1; i /= 2) {
            if (_compare(_heap[i], _heap[i / 2])) {
                return;
            } else {
                Swap(_heap, i, i / 2);
            }
        }
    }
    
    public void Pop()
    {
        int _size = _heap.Count - 1;
        _heap[1] = _heap[_size];
        _heap.RemoveAt(_size);
        _size -= 1;
        for (int i = 1; ;) {
            int j = 2 * i;
            int k = 2 * i + 1;
            if (j > _size) {
                return;
            }
            if (k > _size) {
                if (_compare(_heap[j], _heap[i])) {
                    return;
                }
                Swap(_heap, i, j);
                i = j;
            }
            if (_compare(_heap[j], _heap[i]) && _compare(_heap[k], _heap[i])) {
                return;
            }
            if (_compare(_heap[j], _heap[k])) {
                Swap(_heap, i, k);
                i = k;
            } else {
                Swap(_heap, i, j);
                i = j;
            }
        }
    }
    
    public T Top()
    {
        if (this.IsEmpty())
        {
            throw new InvalidOperationException("A priority queue is empty.");
        }
        return _heap[1];
    }
    
    public int GetSize()
    {
        return _heap.Count - 1;
    }
    
    public bool IsEmpty()
    {
        return this.GetSize() == 0;
    }
    
    public void Swap(ref T x, ref T y)
    {
        
        T tmp = x;
        x = y;
        y = tmp;
    }
    
    public void Swap(List<T> list, int indexA, int indexB)
    {
        
        T tmp = list[indexA];
        list[indexA] = list[indexB];
        list[indexB] = tmp;
    }
}

class Scanner
{
    string[] s;
    int i;
 
    char[] cs = new char[] { ' ' };
 
    public Scanner()
    {
        s = new string[0];
        i = 0;
    }
 
    public string next()
    {
        while (i >= s.Length)
        {
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
        }
        return s[i++];
    }
 
    public int nextInt()
    {
        return int.Parse(next());
    }
 
    public long nextLong()
    {
        return long.Parse(next());
    }
 
    public double nextDouble()
    {
        return double.Parse(next());
    }
}