QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 512 MB Points totaux : 100

#7993. Hamiltonian

Statistiques

Given $n$ pairs $(a_i, b_i)$.

Consider a weighted directed complete graph $G$ with $n$ nodes, where the weight of the edge from $i$ ($1 \le i \le n$) to $j$ ($1 \le j \le n$) is $|a_i - b_j|$.

Find a Hamiltonian cycle in $G$ such that the sum of the weights of the edges it traverses is maximized, and output this maximum value.

Input

The input is read from standard input.

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

Output

The output is written to standard output.

Output a single integer representing the maximum Hamiltonian cycle edge weight sum.

Examples

Input 1

3
1 10
8 2
4 5

Output 1

10

Note 1

Consider the Hamiltonian cycle $1 \to 2 \to 3 \to 1$. The sum of its edge weights is $|1 - 2| + |8 - 5| + |4 - 10| = 10$. It can be proven that no Hamiltonian cycle has an edge weight sum exceeding $10$, so the answer is $10$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.