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.

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.

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

算法竞赛入门经典训练指南上有题意与分析,不懂的可以去翻阅。

 //Asimple
#include <bits/stdc++.h>
#define INF (1<<20)
#define mod 10007
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, res, ans, len, T, k, num, sum;
ll x, y;
ll a[maxn], c[maxn]; /*
假设 An表示n个人原有的金币数
xn表示第n个人给n-1个人xn枚金币(x1表示1给n)
M表示最终每个人拥有的金币数
则可推出通式: xn = x1-Cn 其中 Cn = A1+A2+...+An-n*M
由于要求是最小的金币数,所以x1应当取C数组的中位数
*/ void input() {
ios_base::sync_with_stdio(false);
while( cin >> n ) {
sum = ;
for(int i=; i<=n; i++) {
cin >> a[i];
sum += a[i];
}
ll M = sum / n;
c[] = ;
for(int i=; i<n; i++)
c[i] = c[i-]+a[i]-M;
sort(c, c+n);
ll x1 = c[n/];
ans = ;
for(int i=; i<n; i++)
ans += abs(x1-c[i]);
cout << ans << endl;
}
} int main(){
input();
return ;
}

算法竞赛入门经典训练指南——UVA 11300 preading the Wealth的更多相关文章

  1. 算法竞赛入门经典-训练指南(10881-Piotr's Ants)

    题目大意: 一根长度为L的木棍一堆蚂蚁爬,向左或向右,速度都为1,若两蚂蚁碰撞则同时转头(转身时间忽略不计),问T时间之后每只蚂蚁的位置: 输入:t,(t个样例),每个样例输入 L,T,n,接下来是n ...

  2. 算法竞赛入门经典 LA 4329(树状数组)

    题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...

  3. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  4. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  5. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  6. [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...

  7. [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...

  8. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

  9. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...

随机推荐

  1. python线程中的join(转)

    Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: 知识点一:当一个进程启动之后,会默认产生一个主线程,因为线程是程序执行流的最小单元,当设置多 ...

  2. python-类属性和对象属性(转)

    class People(object): # 类属性是指定义在类的内部而且在方法的外部的属性 money = 10000 def __init__(self,name,age,gender=1): ...

  3. https://github.com/Lushenggang/show-pdf在线浏览pdf文件在线浏览pdf文件

    在线浏览pdf文件 https://github.com/Lushenggang/show-pdf https://github.com/Lushenggang/show-pdf

  4. Python 全栈开发一 python初识

    1.Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  5. NeuroNER+brat工具学习

    1.Brat:http://brat.nlplab.org/ 能够进行直觉标注.命名实体识别.关系标注.分块.共存标注.二元关系标注等(药物与药物).时间标注. 但是这个安装好麻烦啊... 2.

  6. 10.25 AITalkUat部署

    准备: git clone AITalkUat工程,在本地跑: 可以直接浏览器访问127.0.0.1:5000() 如果带上参数,可以得到访问的结果127.0.0.1:5000/autoservice ...

  7. Apache与Tomcat整合的配置

    下载jk连接器地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/ 把jk连接器即“mod_j ...

  8. gerrit上的commit msg中关联jira单号(含gerrit的安装)

    这个问题折腾了很久,前期后后大概一个月吧,终于搞定了,查了很多资料,有的不完整,有的完全就不能用,有的没说完整,所以一定要记录下来,我的心血啊. 由于公司在用的gerrit的是正式环境中的,大家都在使 ...

  9. [LeetCode] 506. Relative Ranks_Easy tag: Sort

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  10. 10 Free Image Hosting Sites for Your Photos

    https://www.lifewire.com/free-image-hosting-sites-3486329 Wondering if there are there any good site ...