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 speci?c amount of wine. They are clever enough to ?gure 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 i-th 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 ful?lled. You may assume that this number ?ts 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
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 i-th 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 ful?lled. You may assume that this number ?ts 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
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 100005
#define MAX 500005
#define ms memset
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴栈用的int n,seq[maxn];
/*
题目大意:给定一个n个元素的线性结构,
其中包含n个数。
已知操作只能运行在相邻元素上,
操作的代价就是搬运的数量。
问操作代价的最小值。首先末尾元素别无选择,肯定是往一侧倒酒的,
那么本题的核心就是维护两端,
至于如何维护,先猜测每次挑两端绝对值最小的搬运。
这是贪心的算法,因为如果有一次我们选大的先搬运了,
那么从小的也可以达到效果,但目前来看搬大的代价比搬小的大。
这么个贪心性质*/int main()
{while(scanf("%d",&n) && n){for(int i=0;i<n;i++) scanf("%d",&seq[i]);int i=0,j=n-1;ll ans=0;while(i<j){if(abs(seq[i])<abs(seq[j])){ans+=abs(seq[i]);seq[i+1]+=seq[i];i++;}else{ans+=abs(seq[j]);seq[j-1]+=seq[j];j--;}}printf("%lld\n",ans);}return 0;
}