UVa Spreading the Wealth 11300 (递归)

来源:互联网 发布:js判断android版本号 编辑:程序博客网 时间:2024/06/14 15:09

Spreading the Wealth

A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyonearound 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, eachperson 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 everyonehas the same number of coins.

Input

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

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

#include<stdio.h>#include<string.h>#include<algorithm>#define ll long long#define N 1000010using namespace std;ll a[N];ll b[N]; int main(){int n,i,j;ll m;while(scanf("%d",&n)!=EOF){ll sum=0;for(i=1;i<=n;i++){scanf("%lld",&a[i]);sum+=a[i];}m=sum/n;b[0]=0;for(i=1;i<n;i++){b[i]=b[i-1]+a[i]-m;}sort(b,b+n);ll x=b[n/2],ans=0;for(i=0;i<n;i++)ans+=abs(x-b[i]);printf("%lld\n",ans);}return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 啤酒喝醉了难受怎么办 喝啤酒喝醉头疼怎么办 运动鞋鞋子大了怎么办 运动鞋买大一号怎么办 跑鞋买大了怎么办 鞋子大一个手指怎么办 一体式手机死机怎么办 ivvi手机声音小怎么办 单位欠缴社保怎么办 哆点错误代码1怎么办 忘记密码怎么办简单一点 智校园忘记密码怎么办 wps图片显示空白怎么办 dr.com闪退怎么办 联想笔记本连不上无线网络怎么办 长时间不用电池休眠怎么办? 校园电信卡欠费怎么办 校园联通卡欠费怎么办 校园网等不上怎么办 app显示尚未授权怎么办 手机屏有气泡怎么办 平板老是闪退怎么办 挖机环保不合格怎么办 摩托车高压火弱怎么办 助力转向机漏油怎么办 淘宝运费险不够怎么办 iphonex锁屏延迟怎么办 淘宝不能评价了怎么办 微商卖香烟不发货怎么办? 三诺血糖仪不准怎么办 如果点球平了怎么办 a2期胃溃疡怎么办水肿 末次月经记不清怎么办 身体弱容易生病怎么办 用复方沙棘出血怎么办 服了苯扎氯铵溶液怎么办 药监局查到过期药品怎么办 血管粥样硬化怎么办 化疗后血管硬化怎么办 牛油果没熟切开了怎么办 切开的牛油果没熟怎么办