10041 - Vito's Family

#include<bits/stdc++.h>

using namespace std;
int store[505];
int main()
{
    int test,r,i,sum,mid;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&r);
        for(i=0; i<r; i++)
        {
            scanf("%d",&store[i]);

        }
        sort(store,store+r);
        sum=0;
        mid=r/2;
        mid=store[mid];
        for(i=0; i<r; i++)
        {
            sum+=abs(mid-store[i]);
        }
        printf("%d\n",sum);
    }
    return 0;
}

0 comments: (+add yours?)