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 C++14 (GCC 5.4.1) (judge verdict: Memory Limit Exceeded)

#include <bits/stdc++.h>

using namespace std;

int N;
pair<int, int> A[5000];
long long dp1[5000][2501];
long long dp2[5000][5001];

long long prefix(int pos, int typeA)
{
    if(pos==-1)
        return 0;
    long long& ret=dp1[pos][typeA];
    if(ret!=-1)
        return ret;
    ret=0x3f3f3f3f3f3f3f3fLL;
    int typeB=pos-typeA+1;
    int len=A[pos].first+A[pos].second;
    if(typeA>0)
        ret=min(ret, 1LL*len*(typeA-1)+A[pos].second+prefix(pos-1, typeA-1));
    if(typeB>0)
        ret=min(ret, 1LL*len*(typeB-1)+A[pos].first+prefix(pos-1, typeA));
    return ret;
}

long long suffix1(int pos, int typeA)
{
    if(pos==N)
    {
        if(typeA==(N-1)/2)
            return 0;
        return 0x3f3f3f3f3f3f3f3fLL;
    }
    long long& ret=dp2[pos][typeA];
    if(ret!=-1)
        return ret;
    ret=0x3f3f3f3f3f3f3f3fLL;
    int typeB=pos-typeA-1;
    int len=A[pos].first+A[pos].second;
    ret=min(ret, 1LL*len*typeA+A[pos].second+suffix1(pos+1, typeA+1));
    ret=min(ret, 1LL*len*typeB+A[pos].first+suffix1(pos+1, typeA));
    return ret;
}

long long suffix2(int pos, int typeA)
{
    if(pos==N)
    {
        if(typeA==(N-1)/2+1)
            return 0;
        return 0x3f3f3f3f3f3f3f3fLL;
    }
    long long& ret=dp2[pos][typeA];
    if(ret!=-1)
        return ret;
    ret=0x3f3f3f3f3f3f3f3fLL;
    int typeB=pos-typeA-1;
    int len=A[pos].first+A[pos].second;
    ret=min(ret, 1LL*len*typeA+A[pos].second+suffix2(pos+1, typeA+1));
    ret=min(ret, 1LL*len*typeB+A[pos].first+suffix2(pos+1, typeA));
    return ret;
}

int main()
{
    scanf("%d", &N);
    for(int i=0; i<N; i++)
        scanf("%d%d", &A[i].first, &A[i].second);
    sort(A, A+N, [](const pair<int, int>& a, const pair<int, int>& b) {
        return a.first+a.second>b.first+b.second;
    });
    memset(dp1, -1, sizeof dp1);
    memset(dp2, -1, sizeof dp2);
    long long ans=0x3f3f3f3f3f3f3f3fLL;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<=min((N-1)/2, i); j++)
            ans=min(ans, 1LL*A[i].first*((N-1)/2)+1LL*A[i].second*(N-1-(N-1)/2)+
                         prefix(i-1, j)+suffix1(i+1, j));
    }
    memset(dp2, -1, sizeof dp2);
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<=min((N-1)/2+1, i); j++)
            ans=min(ans, 1LL*A[i].first*((N-1)/2+1)+1LL*A[i].second*(N-1-((N-1)/2+1))+
                         prefix(i-1, j)+suffix2(i+1, j));
    }
    printf("%lld\n", ans);
    return 0;
}