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: Accepted)

#include <bits/stdc++.h>

using namespace std;

const long long inf = (long long) 1e18;

const int N = 5010;

long long L[N], R[N], new_L[N], new_R[N];
int id[N];
long long f[N][N];
long long g[2][N][3];

inline bool cmp(int i, int j) {
  return (L[i] + R[i] < L[j] + R[j]);
}

inline void upd(long long &a, long long b) {
  if (b < a) {
    a = b;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int foo, bar;
    scanf("%d %d", &foo, &bar);
    L[i] = foo;
    R[i] = bar;
    id[i] = i;
  }
  sort(id, id + n, cmp);
  for (int i = 0; i < n; i++) {
    new_L[i] = L[id[i]];
    new_R[i] = R[id[i]];
  }
  for (int i = 0; i < n; i++) {
    L[i] = new_L[i];
    R[i] = new_R[i];
  }
  long long ans = inf;
  {
    for (int a = 0; a <= n; a++) {
      for (int b = 0; a + b <= n; b++) {
        f[a][b] = inf;
      }
    }
    f[0][0] = 0;
    for (int a = 0; a < n; a++) {
      for (int b = 0; a + b < n; b++) {
        long long ft = f[a][b];
        int i = n - 1 - a - b;
        upd(f[a + 1][b], ft + R[i] + (L[i] + R[i]) * a);
        upd(f[a][b + 1], ft + L[i] + (L[i] + R[i]) * b);
      }
    }
    for (int a = 0; a <= n; a++) {
      ans = min(ans, f[a][n - a]);
    }
  }
  if (n % 2 == 1) {
    for (int b = 0; 0 + b <= n; b++) {
      for (int c = 0; c <= 1 && 0 + b + c <= n; c++) {
        g[0][b][c] = inf;
      }
    }
    g[0][0][0] = 0;
    int half = (n - 1) / 2;
    for (int a = 0; a < n; a++) {
      for (int b = 0; (a + 1) + b <= n; b++) {
        for (int c = 0; c <= 1 && (a + 1) + b + c <= n; c++) {
          g[1][b][c] = inf;
        }
      }
      for (int b = 0; a + b <= n; b++) {
        for (int c = 0; c <= 1 && a + b + c <= n; c++) {
          long long ft = g[0][b][c];
          if (a + b + c == n) {
            ans = min(ans, ft);
            continue;
          }
          int i = n - 1 - a - b - c;
          upd(g[1][b][c], ft + R[i] + (L[i] + R[i]) * a);
          upd(g[0][b + 1][c], ft + L[i] + (L[i] + R[i]) * b);
          if (c == 0) {
            upd(g[0][b][c + 1], ft + (L[i] + R[i]) * half);
          }
        }
      }
      for (int b = 0; (a + 1) + b <= n; b++) {
        for (int c = 0; c <= 1 && (a + 1) + b + c <= n; c++) {
          g[0][b][c] = g[1][b][c];
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}