#d2971. 奶牛集会

奶牛集会

Cow Gathering

Problem Source

Original problem: USACO04OPEN MooFest G.

Although an O(N2)O(N^2) simulation can pass, a divide and conquer or Fenwick tree approach is recommended for the full challenge.

Related (harder) problem where O(N2)O(N^2) fails: P5094.

Problem Description

John's nn cows attend the "Moo Moo Gathering" every year. It is a grand event in the cow world, with many activities such as stacking hay, jumping fences, and patting cowboys on the butt.

When they gather, the ii-th cow stands at coordinate xix_i, and no two cows share the same coordinate.

The cows are very loud. When cow ii and cow jj communicate, they produce a volume of

max⁡{vi,vj}×∣xi−xj∣,\max\{v_i, v_j\} \times |x_i - x_j|,

where viv_i and vjv_j are the hearing abilities of cow ii and cow jj respectively.

Assuming every pair of cows is talking simultaneously, compute the sum of the volumes produced by all cows.

Input Format

The first line contains a single integer nn, 1≤n≤3×1041 \le n \le 3 \times 10^4.

Lines 22 through n+1n + 1: the (i+1)(i + 1)-th line contains two integers viv_i and xix_i (1≤vi,xi≤3×1041 \le v_i, x_i \le 3 \times 10^4).

Output Format

Output a single integer: the total volume produced by all cows.

Sample Input

4
3 1
2 5
2 6
4 3

Sample Output

57