#P6163. [Cnoi2020] 领域极限

[Cnoi2020] 领域极限

Description

Cirno has nn integers, denoted as a1,a2,a3,...,ana_1,a_2,a_3,...,a_n.

For each number aia_i, there is a constraint pair (li,ri)(l_i,r_i).

Cirno wants to know:

$$\min_{\forall t, a_t \in [l_t,r_t]}\big\{\sum_{i=1}^{n}\sum_{j=1}^{n}\left| a_i - a_j \right|\big\}$$

Input Format

The first line contains an integer nn.

The next nn lines each contain a constraint pair (li,ri)(l_i,r_i).

Output Format

One line containing an integer, which is the answer.

3
1 2
2 3
3 4
4
8
39 42
53 55
51 52
46 47
52 54
33 38
2 7
32 34
910

Hint

Sample 1 Explanation

When (a1,a2,a3)=(2,3,3)(a_1,a_2,a_3)=(2,3,3), the answer reaches its minimum.

Constraints and Notes

"This problem uses bundled testdata."

  • Subtask 1 ( 20%20\% ): n10n \le 10, and rili5r_i - l_i \le 5.
  • Subtask 2 ( 20%20\% ): n20n \le 20.
  • Subtask 3 ( 20%20\% ): n103n \le 10^3.
  • Subtask 4 ( 40%40\% ): n105n \le 10^5.

For 100%100\% of the testdata: n(0,105]n \in (0,10^5], 0liri1090 \le l_i \le r_i \le 10^9, and the answer is within [0,4×1018][0,4 \times 10^{18}].

Translated by ChatGPT 5