1. 程式人生 > >Codeforces Round #527 (Div. 3) B. Teams Forming(水題)

Codeforces Round #527 (Div. 3) B. Teams Forming(水題)

題目連結:http://codeforces.com/contest/1092/problem/B

題意:給你n個人,要給他們兩個兩個分組,要儘量讓他們的權值之差最小,問總差值是多少。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, a[105]; cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a,a+n); int ans = 0;
    for(int i = 1; i < n; i += 2)
        ans += a[i] - a[i-1];
    cout << ans << endl;
    return 0;
}