PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000460. Khớp răng đều (Smooth Occlusion)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khớp răng đều (Smooth Occlusion)

Smooth Occlusion

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Takahashi has NN upper and NN lower teeth, numbered from left to right. Their initial lengths at position ii are UiU_i and DiD_i, respectively.

Let ui,diu_i,d_i denote the final lengths. A good fit requires both of these conditions:

  • There is an integer HH such that ui+di=Hu_i+d_i=H for every 1≤i≤N1\le i\le N.
  • Adjacent upper teeth satisfy ∣ui−ui+1∣≤X|u_i-u_{i+1}|\le X for every 1≤i<N1\le i<N.

For one yen, he may shorten a single tooth of positive length by one unit. He may repeat this operation freely; no other change is allowed. Zero final length is permitted.

Determine the minimum total cost of achieving a good fit.

Input

The first line contains integers N,XN,X.

Each of the next NN lines contains Ui,DiU_i,D_i for the corresponding position.

Output

Print the minimum cost as an integer.

Subtasks

  • Subtask 1 (20 points): 2≤N≤202\le N\le 20; 1≤i≤N1\le i\le N; 1≤Ui,Di≤1001\le U_i,D_i\le 100; 1≤X≤1091\le X\le10^9.
  • Subtask 2 (30 points): 2≤N≤20002\le N\le 2000; 1≤i≤N1\le i\le N; 1≤Ui,Di≤1091\le U_i,D_i\le 10^9; 1≤X≤1091\le X\le10^9.
  • Subtask 3 (50 points): 2≤N≤2000002\le N\le 200000; 1≤i≤N1\le i\le N; 1≤Ui,Di≤1091\le U_i,D_i\le 10^9; 1≤X≤1091\le X\le10^9.

Examples

Input 1

4 3
3 1
4 1
5 9
2 6

Output 1

15

Explanation 1

Upper lengths 3,3,3,2 and lower lengths 1,1,1,2 give height 4 and satisfy the adjacent bound. The initial sum is 31, the final sum is 16, and the first pair bounds H by 4.

Tooth lengths before and after adjustment

Input 2

4 1000000000
3 3
3 3
3 3
3 3

Output 2

0

Explanation 2

All pairs already sum to 6 and the upper lengths agree.

Input 3

4 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1 1

Output 3

5999999994

Explanation 3

The last pair bounds H by 2. Setting all lengths to 1 achieves it; the initial and final sums are 6000000002 and 8.

Input 4

15 128
748 169
586 329
972 529
432 519
408 587
138 249
656 114
632 299
984 755
404 772
155 506
832 854
353 465
387 374
567 385

Output 4

9460

Explanation 4

Height 380 is feasible with upper lengths 380, 380, 380, 380, 266, 138, 266, 380, 380, 283, 155, 283, 353, 380, 380. Lower lengths are H minus the corresponding upper length. The total reduction is 9460. The adjacent bound from position 6 limits upper length at position 7 to 266, so H cannot exceed 380.