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

#FT0000128. Lễ hội tiếng bò (MooFest)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lễ hội tiếng bò (MooFest)

MooFest

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

John's nn cows are attending MooFest. Cow ii stands at integer coordinate xix_i and has hearing value viv_i. All cow positions are distinct.

The volume of communication between cows ii and jj is max⁡(vi,vj)∣xi−xj∣\max(v_i,v_j)|x_i-x_j|. Every unordered pair of distinct cows communicates. Compute the sum of all these volumes, counting each pair exactly once. Use the maximum hearing value, not the minimum or the sum.

Input

  • First line: nn.
  • Next nn lines: two integers vi,xiv_i,x_i in that order.

Output

Print the total volume over all unordered pairs of cows.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤vi,xi≤200001\le v_i,x_i\le20000; all positions xix_i are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤40001\le n\le 4000; 1≤vi,xi≤200001\le v_i,x_i\le20000; all positions xix_i are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤200001\le n\le 20000; 1≤vi,xi≤200001\le v_i,x_i\le20000; all positions xix_i are pairwise distinct.

Examples

Example 1

Input

4
3 1
2 5
2 6
4 3

Output

57

Explanation

The six pairwise volumes are 12,15,8,2,8,1212,15,8,2,8,12 (in lexicographic pair order). Their sum is 12+15+8+2+8+12=5712+15+8+2+8+12=\mathbf{57}.

Example 2

Input

3
1 2
3 5
2 8

Output

30

Explanation

For cows (v,x)=(1,2),(3,5),(2,8)(v,x)=(1,2),(3,5),(2,8), the pairwise volumes are 3⋅3=93\cdot3=9, 2⋅6=122\cdot6=12, and 3⋅3=93\cdot3=9, totaling 30.