Description
Zhu Ge is a very clever boy. One day, he discovered 2*n numbers. He wanted to divide them into n groups, each group contains 2 integers, and minimize the sum of the absolute value of the difference of the numbers in each group.
The problem is too difficult to Zhu Ge, so he turned to you. He hopes you can calculate the minimum of the sum of absolute value of the difference among different division strategies.
Input
There are several test cases.
For each test case, there is an integer n (n < 10,000) at the first line. The second line contains 2*n integers. The input ends up with EOF.
Output
For each test case, output the minimum of sum.
Sample Input
3 10 3 4 5 1 6 5 64 5 63 63 23 63 54 64 3 54
Sample Output
7 42
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string.h>
#include <cmath>
using namespace std;
long long int a[20005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
long long int sum=0;
for(int i=0; i<2*n; i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+(2*n));
for(int i=0; i<2*n; i=i+2)
{
sum+=abs(a[i]-a[i+1]);
}
cout<<sum<<endl;
}
return 0;
}