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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

struct SlopeSet {
    heap: BinaryHeap<i64>,
    add: i64
}

impl SlopeSet {
    fn new() -> Self {
        SlopeSet { heap: BinaryHeap::new(), add: 0 }
    }

    fn add(&mut self, x: i64) {
        self.add += x;
    }

    fn push_left(&mut self, x: i64) {
        let tx = x - self.add;
        self.heap.push(tx);
    }

    fn push_right(&mut self, x: i64) {
        let tx = x - self.add;
        self.heap.push(-tx);
    }

    fn pop_left(&mut self) -> i64 {
        let x = self.heap.pop().unwrap();
        x + self.add
    }

    fn pop_right(&mut self) -> i64 {
        let x = self.heap.pop().unwrap();
        -x + self.add
    }

    fn peek_left_pair(&mut self) -> (i64, i64) {
        let one = self.heap.pop().unwrap();
        let two = self.heap.pop().unwrap();
        self.heap.push(one);
        self.heap.push(two);
        (two + self.add, one + self.add)
    }

    fn peek_right_pair(&mut self) -> (i64, i64) {
        let one = self.heap.pop().unwrap();
        let two = self.heap.pop().unwrap();
        self.heap.push(one);
        self.heap.push(two);
        (-one + self.add, -two + self.add)
    }
}

const INF: i64 = 1e16 as i64;

fn main() {
    let n: usize = read();
    let rects: Vec<(i64, i64)> = readn(n);


    let mut from = rects[0].0;
    let mut to = rects[0].0;
    let mut bottom = 0;

    let mut left = SlopeSet::new();
    left.push_left(-INF);
    left.push_left(from);

    let mut right = SlopeSet::new();
    right.push_right(INF);
    right.push_right(from);


    for i in 1..n {
        let fl = rects[i-1].0;
        let fr = rects[i-1].1;
        let tl = rects[i].0;
        let tr = rects[i].1;
        from -= tr-tl;
        left.add(-tr+tl);
        to += fr-fl;
        right.add(fr-fl);

        let (l0, l1) = left.peek_left_pair();
        let (r0, r1) = right.peek_right_pair();

        // println!("{}={}({} {}){}={}", l0, l1, from, to, r0, r1);
        // println!("{}", bottom);

        let position = tl;
        if position <= l1 {
            from = max(l0, position);
            to = l1;
            bottom += (l1 - position).abs();
            left.push_left(position);
            left.push_left(position);
            let l0 = left.pop_left();
            right.push_right(l0);
        } else if position < r0 {
            from = position;
            to = position;
            left.push_left(position);
            right.push_right(position);
        } else {
            from = r0;
            to = min(r1, position);
            bottom += (position - r0).abs();
            right.push_right(position);
            right.push_right(position);
            let r0 = right.pop_right();
            left.push_left(r0);
        }
    }

    println!("{}", bottom);
}