#P2345. [USACO04OPEN] MooFest G

[USACO04OPEN] MooFest G

Description

John’s nn cows attend “MooFest” every year.

MooFest is a grand event in the cow world. There are many activities at the gathering, such as stacking hay, jumping over fences, and patting cowboys on the butt.

When they participate in activities, they gather together. The ii-th cow is located at coordinate xix_i, and no two cows share the same coordinate.

The cows are very loud. When the ii-th and jj-th cows communicate, they produce a volume of max{vi,vj}×xixj\max\{v_i, v_j\} \times |x_i - x_j|, where viv_i and vjv_j are the hearing abilities of cows ii and jj, respectively.

Assume that every pair of cows is talking at the same time. Please compute the sum of the volume produced by all cows.

Input Format

  • The first line: a single integer nn, 1n2×1041 \le n \le 2 \times 10^4.
  • Lines 22 to n+1n + 1: the (i+1)(i + 1)-th line contains two integers viv_i and xix_i (1vi,xi2×1041 \le v_i, x_i \le 2 \times 10^4).

Output Format

A single integer: the total volume produced by all cows.

4
3 1
2 5
2 6
4 3
57

Hint

Translated by ChatGPT 5