Max Pair Matching
Time limit1sMemory limit1024 MB
Given 2n pairs (a_i, b_i), find a perfect matching that maximizes the total of max(|a_i-a_j|, |a_i-b_j|, |b_i-a_j|, |b_i-b_j|) over matched pairs, and print that sum.
Problem
You are given pairs of integers. Consider a complete graph on vertices and define the weight of the edge as .
Determine the maximum weight of a matching in this graph. That is, choose edges so that no two chosen edges share an endpoint, and find the largest possible total weight of those edges.
Input
The first line contains a single integer ().
Each of the next lines contains two integers and ().
Output
Print a single integer, the maximum weight of a matching in this graph.
Hint
The adjacency matrix is: