#ARC147C. [ARC147C] Min Diff Sum

[ARC147C] Min Diff Sum

Score : 600600 points

Problem Statement

NN people, numbered 1,2,,N1,2,\ldots ,N, are going to stand on the number line. Let's denote by xix_i the coordinate the Person ii stands at. Then, xix_i should be an integer satisfying LixiRiL_i \leq x_i \leq R_i. Multiple people can occupy the same coordinate.

We define the dissatisfaction level as the following formula:

$\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^{N}|x_j-x_i|$

Find the minimum possible value of the dissatisfaction level.

Constraints

  • 2N3×1052 \leq N \leq 3 \times 10^5
  • 1LiRi107(1iN)1 \leq L_i \leq R_i \leq 10^7 \,(1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

L1L_1 R1R_1

L2L_2 R2R_2

\vdots

LNL_N RNR_N

Output

Print the answer.

3
1 3
2 4
5 6
4

If we let x1=3,x2=4,x3=5x_1=3,x_2=4,x_3=5, we get the dissatisfaction level of 44. We cannot make it 33 or less, so the answer is 44.

3
1 1
1 1
1 1
0
6
1 5
2 4
1 1
4 4
3 6
3 3
15