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. elemet-paging

    <template> <card-layout :title="L('Prosuct')" :actions="actions" @click ...

  2. sap 类的左侧导航栏

  3. [dj]django常用设置

    关于django版本说明: Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6(长期支持版本 LTS) 最后一个支持 Python 2.7 的版本 Django 2 ...

  4. 【Java】-NO.16.EBook.4.Java.1.005-【疯狂Java讲义第3版 李刚】- 枚举

    1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.005-[疯狂Java讲义第3版 李刚]- 枚举 Style:EBook Series:Java Si ...

  5. 【Tools】-NO.4.Tools.1.VM.1.001-【VMware Workstation PRO 12 Install CentOS 7.1】-

    1.0.0 Summary Tittle:[Tools]-NO.4.Tools.1.VM.1.001-[VMware Workstation PRO 12 Install CentOS 7.1]- S ...

  6. Pytorch快速入门及在线体验

    本文搭配了Pytorch在线环境,可以直接在线体验. Pytorch是Facebook 的 AI 研究团队发布了一个基于 Python的科学计算包,旨在服务两类场合: 1.替代numpy发挥GPU潜能 ...

  7. 安装程序无法打开注册表项 UNKNOWN\Components\…的简单解决办法(转)

    安装程序无法打开注册表项 UNKNOWN\Components\…的简单解决办法 2018年04月16日 16:41:32 super_star_贤 阅读数:7193   在安装软件时(比如安装SQL ...

  8. spring对JDBC的整合支持

    参考网址:https://blog.csdn.net/u013821825/article/details/51606171 springMVC,目前用到的jar包 spring IOC 5个包  + ...

  9. SSH--完全分布式主机设置【克隆过安装过Hadoop的主机后】

    ====准备完全分布式主机的ssh==== 2018-12-21 14:27:47 1.删除所有主机上.ssh下所有文件 2.在s250主机上生成密钥对 $>ssh-keygen -t rsa ...

  10. Container(容器)与 Injector(注入)

    Container(容器): History: containerd于2014年出生于Docker,最初是Docker引擎的低层运行时管理器.继2017年3月被CNCF接受之后,containerd已 ...