Wine trading in Gergovia

As you may know from the comic “Asterix and the Chieftain’s Shield”, Gergovia consists of one street, and every inhabitant of the city is a wine salesman. You wonder how this economy works? Simple enough: everyone buys wine from other inhabitants of the city. Every day each inhabitant decides how much wine he wants to buy or sell. Interestingly, demand and supply is always the same, so that each inhabitant gets what he wants.

There is one problem, however: Transporting wine from one house to another results in work. Since all wines are equally good, the inhabitants of Gergovia don’t care which persons they are doing trade with, they are only interested in selling or buying a specific amount of wine. They are clever enough to figure out a way of trading so that the overall amount of work needed for transports is minimized.

In this problem you are asked to reconstruct the trading during one day in Gergovia. For simplicity we will assume that the houses are built along a straight line with equal distance between adjacent houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work.

Input

The input consists of several test cases. Each test case starts with the number of inhabitants n(2≤n≤100000)n(2 \le n \le 100000). The following line contains nn integers ai(−1000≤ai≤1000)a_i(-1000 \le a_i \le 1000). If ai≥0a_i \ge 0, it means that the inhabitant living in the ii-th house wants to buy aia_i bottles of wine, otherwise if ai<0a_i, he wants to sell aia_i bottles of wine. You may assume that the numbers aia_i sum up to 00.

The last test case is followed by a line containing ‘0’.

Output

For each test case print the minimum amount of work units needed so that every inhabitant has his demand fulfilled. You may assume that this number fits into a signed 64-bit integer (in C/C++ you can use the data type “long long”, in JAVA the data type “long”).

Sample Input

5
5 -4 1 -3 1
6
-1000 -1000 -1000 1000 1000 1000
0

Sample Output

9
9000

直线上有n(2≤n≤100000)n(2≤n≤100000)个等距的村庄,每个村庄要么买酒,要么卖酒。设第ii个村庄对酒的需求为ai(−1000≤ai≤1000)a_i(-1000≤a_i≤1000),其中ai>0a_i>0表示买酒,ai<0a_i表示卖酒。所有村庄供需平衡,即所有aia_i之和等于00。

思路非常简单,就是从左到右,将酒累加到下一个上去,总移动单位则为每次累加的绝对值之和。

其证明过程为,nn个村庄,无论是买酒还是卖酒,最左边的a1a_1都需要从a2a_2获取到∣∣a1∣∣\left| a_1 \right|个移动单位,这时a2=a1+a2a_2 = a_1 + a_2,这样就等价于n−1n-1个村庄的问题了。

#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
int main() {int n;while(cin >> n && n) {LL sum = 0;LL ans = 0;LL num;for(int i = 0; i < n; i++) {cin >> num;sum += num;ans += abs(sum);}cout << ans << endl;}return 0;
}

11054 - Wine trading in Gergovia相关推荐

  1. UVA 11054 Wine trading in Gergovia 葡萄酒交易 贪心+模拟

    题意:一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求 ...

  2. Uva 11054 - Wine trading in Gergovia(模拟)

    As you may know from the comic "Asterix andthe Chieftain's Shield", Gergovia consists of o ...

  3. uva 11054——Wine trading in Gergovia

    题意:有n个村庄,每个村庄要么买酒(+),要么卖酒(-),要求供需平衡,求最小代价(代价k为把k个单位的酒运到相邻的村庄). 思路:贪心.可以把第1个村庄的所有酒都从第2个村庄运来,那么12可以合二为 ...

  4. UVA 11054 Wine trading in Gergovia

    题意: 一条街上住着连续的n户人家,没相邻的两户人相隔一个单位.街上的每户人都需要买一定数量的葡萄酒或者卖掉葡萄酒,保证所有人家买进的总量与卖出的数量一致.每户可以选择与其他任何家交易.但是因为相隔路 ...

  5. POJ2940 HDU1489 UVA11054 Wine Trading in Gergovia【Ad Hoc】

    Wine Trading in Gergovia Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3541   Accepte ...

  6. Wine Trading in Gergovia (贪心)

    As you may know from the comic "Asterix and the Chieftain's Shield", Gergovia consists of ...

  7. uva11054 - Wine trading in Gergovia(等价转换,贪心法)

    这个题看上去麻烦,实际上只要想清楚就很简单.关键是要有一种等价转换的思维方式.其实题意就是个一排数,最后通过相邻的互相移动加减使得所有数都变成零,移动过程中每次都耗费相应值,让耗费的值最小.虽然从实际 ...

  8. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  9. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

最新文章

  1. arcgis弧段怎么加很多点_ArcGIS小技巧——提取面要素的质心点
  2. 零基础自学python的建议-零基础学python是学2还是3好
  3. Alpha 冲刺(2/10)
  4. 【Android 安装包优化】WebP 应用 ( 4.0 以下兼容 WebP | Android Studio 中使用 libwebp.so 库向下兼容版本 | libwebp 库测试可用性 )
  5. 使用CSDN的Markdown编辑器
  6. OpenCV形态morphology的实例(附完整代码)
  7. Entity Framework Core 2.0 全局查询过滤器
  8. 3dmax模型转换为*.FLT格式的建模要点
  9. php之time的用法,php中time()与$_SERVER[REQUEST_TIME]用法区别
  10. 基于图像的小麦真菌病害深度学习识别(数据+平台)
  11. 两个变量相乘_自动控制原理-信号流图与系统状态变量传递函数之间联系如此紧密...
  12. java 排序,选择排序、插入排序、冒泡排序
  13. window10 下面固定本地 ip
  14. windows11,Windows10,服务器centos7安装docker,docker compose
  15. stalk词组_let.stalk是什么意思
  16. SEO文章配图自动生成工具软件
  17. 声音(音乐)分类综述
  18. 信息安全工程师(软考中级)
  19. 幽默感七个技巧_如何让自己变得幽默-16个聊天幽默技巧
  20. 面阵相机靶面详解and镜头选择andFA镜头视野计算

热门文章

  1. ERR! sharp EACCES: permission denied, mkdir ‘/root/.npm/_libvips‘......
  2. npm 开发中 常用依赖包
  3. 【Java】编写Java程序,完成从键盘输入两个运算数据,计算两数之和并输出结果...
  4. 使用Visual Studio Code调试.net控制台应用程序的方法
  5. C#LeetCode刷题之#258-各位相加(Add Digits)
  6. 微信小程序本地存储存储_如何利用本地存储构建快速的应用程序
  7. R画地图,并标注所需点
  8. linux内存段页,linux内存管理-段式和页式管理
  9. python语言有什么作用_python“ with”语句的用途是什么?
  10. qt2-无边框窗口创建、拖拽、阴影