#P10046. [CCPC 2023 北京市赛] 哈密顿

[CCPC 2023 北京市赛] 哈密顿

Problem Description

Given nn pairs (ai,bi)(a_i, b_i).

Consider a weighted complete directed graph GG with nn nodes, where the edge from i (1in)i \ (1 \le i \le n) to j (1jn)j \ (1 \le j \le n) has weight aibj|a_i - b_j|.

Find a Hamiltonian cycle in GG that maximizes the sum of the weights of the edges it traverses, and output this maximum value.

Input Format

The first line contains an integer n(2n105)n (2 \le n \le 10^5), representing the number of pairs. The next nn lines each contain two integers ai,bi (0ai,bi109)a_i, b_i \ (0 \le a_i, b_i \le 10^9), representing each pair. It is guaranteed that among the 2n2n numbers in the nn pairs, all numbers are pairwise distinct.

Output Format

Output one integer on a single line, representing the maximum total edge weight of a Hamiltonian cycle.

3
1 10
8 2
4 5
10

Hint

Consider the Hamiltonian cycle 12311 \to 2 \to 3 \to 1. The sum of edge weights is 12+85+410=10|1 - 2| + | 8 - 5| + |4 - 10| = 10. It can be proven that no Hamiltonian cycle has a total edge weight exceeding 1010, so the answer is 1010.

Translated by ChatGPT 5