Wine Trading in Gergovia
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3187   Accepted: 1454

Description

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). The following line contains n integers ai (−1000 ≤ ai ≤ 1000). If ai ≥ 0, it means that the inhabitant living in the ith house wants to buy ai bottles of wine, otherwise if ai < 0, he wants to sell −ai bottles of wine. You may assume that the numbers ai sum up to 0.

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” or “__int64”, 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 思路:最终每个位置上的数应该是0;
    设now为当前剩余要搬的葡萄酒数目;ans为目前最小工作量。初始时now和ans为0;
    顺序扫描每个房间i(0<=i&&i<n):
      ans+=now;now+=a[i];
     最后得出的ans为最小工作量。
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.141592653589792128462643383279502
#define N 100005
int main(){
//#ifdef CDZSC_June
freopen("in.txt","r",stdin);
//#endif
//std::ios::sync_with_stdio(false);
long long n,a[N],ans;int now;
while(scanf("%lld",&n),n){
ans=;now=;
for(int i=;i<n;i++){
scanf("%lld",a+i);
ans+=abs(now);
now+=a[i];
}
cout <<ans<<endl;
} return ;
}

 

poj 2940的更多相关文章

  1. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  2. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  8. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  9. poj 2352 Stars 数星星 详解

    题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...

随机推荐

  1. 【Foreign】Weed [线段树]

    Weed Time Limit: 20 Sec  Memory Limit: 512 MB Description 从前有个栈,一开始是空的. 你写下了 m 个操作,每个操作形如 k v : 若 k ...

  2. R、Python、Scala和Java,到底该使用哪一种大数据编程语言?

    有一个大数据项目,你知道问题领域(problem domain),也知道使用什么基础设施,甚至可能已决定使用哪种框架来处理所有这些数据,但是有一个决定迟迟未能做出:我该选择哪种语言?(或者可能更有针对 ...

  3. fundamentals of the jQuery library

    1.why is jquery Only 32kB minified and gzipped. Can also be included as an AMD module Supports CSS3 ...

  4. 一个简单插件this传值的跟踪

    <!DOCUTYPE html> <html> <head> <meta charset="UTF-8"> <script s ...

  5. 【遍历集合】Java遍历List,Map,Vector,Set的几种方法

    关于list,map,set的区别参考http://www.cnblogs.com/qlqwjy/p/7406573.html 1.遍历list @Test public void testList( ...

  6. 常见网络命令之traceroute命令一起其他常用命令

    备注:任何命令+/?就可以显示命令帮助,比如:ipconfig /?. traceroute命令 traceroute是UNIX系统中的名字,用来跟踪一个分组从源点到终点的路径.在Windows系统中 ...

  7. Metlnfo cms后台getshell漏洞复现

    整体思路 挖掘伪全局变量 然后找可控参数进行利用#伪全局变量:可理解为全局变量,例部分CMS为了全局过滤SQL注入或者XSS之类的漏洞就会将GET.POST.COOKIE等请求借入全局然后直接过滤.这 ...

  8. shell下在while循环中使用ssh命令的问题

    1 现象描述 最近使用ssh批量执行命令(已经做了密钥互信了),脚本读取配置文件中的主机列表(内容为每行一台主机IP地址),然后执行,可是每次只是执行第一台,就退出循环了. 2 排查思路 由于脚本比较 ...

  9. C++学习之路(三):volatile关键字

    volatile是c++中的一个关键字.用volatile修饰的变量,具有三个性质:易变性 (一)易变性: 由于编译器对代码执行的优化,两条赋值语句,下一条语句可能会直接从上一条语句使用的寄存器中取得 ...

  10. linux运维记

    nmap 127.0.0.1 命令查看当前服务器对外有多少端口,用于检查漏洞 vim ctrl+z ,jobs,fg 切换控制应用程序 vim 执行命令 #!sh aa.sh执行命令 运维系统监控开源 ...