PC/UVa:110401/10041
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
using namespace std;
int main()
{
int T = 0;
cin >> T;
for (int t = 0; t < T; t++)
{
int r = 0;
cin >> r;
vector<int> viS(r, 0);
vector<vector<int>> vviDis(r, vector<int>(r, 0));
for (int i = 0; i < r; i++)
{
cin >> viS[i];
}
for (int i = 0; i < r; i++)
{
for (int j = i + 1; j < r; j++)
{
vviDis[i][j] = abs(viS[i] - viS[j]);
vviDis[j][i] = abs(viS[i] - viS[j]);
}
}
int min = INT_MAX;
for (int i = 0; i < r; i++)
{
int sum = 0;
for (int j = 0; j < r; j++)
{
sum += vviDis[i][j];
}
if (sum < min) min = sum;
}
cout << min << endl;
}
return 0;
}
/*
2
2 2 4
3 2 4 6
*/