#P2507. [SCOI2008] 配对
[SCOI2008] 配对
Description
You are given integers and integers . You need to pair them, that is, each is matched to exactly one . The goal is to minimize the sum of absolute differences over all pairs, but pairing two equal numbers is not allowed. For example, if and , then an optimal pairing is , , , with absolute differences , summing to . Note that , , are not allowed because equal numbers may not be paired.
Input Format
The first line contains a positive integer . Then follow lines, each containing two integers and . It is guaranteed that all are pairwise distinct, and all are also pairwise distinct.
Output Format
Output a single integer, the minimum possible sum of absolute differences of the paired integers. If it is impossible to pair, output -1.
3
3 65
45 10
60 25
32
3
5 5
6 7
8 8
5
Hint
Constraints:
of the testdata satisfies: .
of the testdata satisfies: , and are integers between and .
Translated by ChatGPT 5
京公网安备 11011102002149号