Box of Bricks
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10779 Accepted Submission(s): 3656
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
Output a blank line between each set.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
int a[60];
int k=0;
while(cin>>n&&n)
{
if(k)
{
cout<<endl;
}
int i;
int sum=0,ave=0,suma=0;
for(i=0;i<n;i++)
{
cin>>a[i];
sum += a[i];
}
ave = sum/n;
for(i=0;i<n;i++)
{
suma+=abs(a[i]-ave);
}
cout<<suma/2<<endl;
k++;
}
return 0;
}