Problem:
Score : 1000 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

Snuke received N intervals as a birthday present.
The i-th interval was [-L_i, R_i].
It is guaranteed that both L_i and R_i are positive.
In other words, the origin is strictly inside each interval.

Snuke doesn't like overlapping intervals, so he decided to move some intervals.
For any positive integer d, if he pays d dollars, he can choose one of the intervals and move it by the distance of d.
That is, if the chosen segment is [a, b], he can change it to either [a+d, b+d] or [a-d, b-d].

He can repeat this type of operation arbitrary number of times.
After the operations, the intervals must be pairwise disjoint (however, they may touch at a point).
Formally, for any two intervals, the length of the intersection must be zero.

Compute the minimum cost required to achieve his goal.

Constraints

1 ≤ N ≤ 5000

1 ≤ L_i, R_i ≤ 10^9

All values in the input are integers.

Input

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

N
L_1 R_1
:
L_N R_N

Output

Print the minimum cost required to achieve his goal.

Sample Input 1

4
2 7
2 5
4 1
7 5

Sample Output 1

22

One optimal solution is as follows:

Move the interval [-2, 7] to [6, 15] with 8 dollars

Move the interval [-2, 5] to [-1, 6] with 1 dollars

Move the interval [-4, 1] to [-6, -1] with 2 dollars

Move the interval [-7, 5] to [-18, -6] with 11 dollars

The total cost is 8 + 1 + 2 + 11 = 22 dollars.

Sample Input 2

20
97 2
75 25
82 84
17 56
32 2
28 37
57 39
18 11
79 6
40 68
68 16
40 63
93 49
91 10
55 68
31 80
57 18
34 28
76 55
21 80

Sample Output 2

7337

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.io.*;
import java.util.*;

class P implements Comparable<P>{
    long a,b;
    P(long x,long y){a=x;b=y;}
    @Override
    public int compareTo(P o){
        return Long.compare(a,o.a);
    }
}
class Main {
    static final long I=1L<<48;
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        P[]lr=new P[n];
        for(int i=0;i<n;++i){
            long l=sc.nextInt();
            long r=sc.nextInt();
            lr[i]=new P(l+r,l);
        }
        Arrays.sort(lr);
        int nn=n/2*2;
        long sl=n%2==1?lr[0].b:0;
        long sr=n%2==1?lr[0].a-lr[0].b:0;
        long[][]dp=new long[nn+1][nn/2+1];
        Arrays.fill(dp[0],I);
        dp[0][0]=0;
        for(int i=0;i<nn;++i){
            int u=i;
            int nxt=i+1;
            long l=lr[i+n%2].b;
            long r=lr[i+n%2].a-l;
            Arrays.fill(dp[nxt],I);
            for(int j=0;j<=Math.min(i+1,nn/2);++j){
                int lft=j,rgt=i+1-j;
                if(lft>nn/2||rgt>nn/2)continue;
                if(rgt>0)
                    dp[nxt][j]=Math.min(dp[nxt][j],dp[u][j]+(nn/2-rgt+1)*l+(nn/2-rgt)*r);
                if(lft>0)
                    dp[nxt][j]=Math.min(dp[nxt][j],dp[u][j-1]+(nn/2-lft)*l+(nn/2-lft+1)*r);
            }
        }
        if(n%2==1)throw new Error();
        //System.err.println(Arrays.deepToString(dp));
        out.println(dp[nn][nn/2]+(sl+sr)*(nn/2));
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}