0
点赞
收藏
分享

微信扫一扫

UVA-11300 - Spreading the Wealth-2014年4月18日173335

Ad大成 2022-07-27 阅读 23


F. Spreading the Wealth 

Problem

A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.

The Input

There is a number of inputs. Each input begins with n(n<1000001), the number of people in the village.n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.

The Output

For each input, output the minimum number of coins that must be transferred on a single line.

Sample Input


3 100 100 100 4 1 2 5 4


Sample Output


0 4




Problem setter: Josh Bao


 

# include<stdio.h>
# include<string.h>
# include<algorithm>
using namespace std;

long long money[1000100],C[1000100],ans,tot,mid,average;

int main()
{
int n,i;
while(~scanf("%d",&n))
{
memset(money,0,sizeof(money));
memset(C,0,sizeof(C));

tot = 0;
for(i = 1; i <= n; i++)
{
scanf("%lld",&money[i]);
tot += money[i]; //tot 金币总数
}
average = tot / n; //mid 平均数
C[0] = 0;
for(i = 1 ; i < n; i++)
{
C[i] = C[i-1] + money[i] - average ;
}
sort(C,C+n);
long long x1 = C[n/2];
ans = 0;
for(i = 0; i < n; i++)
ans += abs(x1 - C[i]);

printf("%lld\n",ans);
}
return 0;
}

 

举报

相关推荐

0 条评论