您的位置:首页 > 其它

uva11054

2016-06-11 11:44 281 查看
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21418

/*
solution:
由于路费和距离相关,所以需要让路程越小越好,那么就可以让每人之和相邻的人交易。
不用考虑他们是要买还是要卖。假设a1要买5个,a2要卖2个,那么就让a1向a2买5个,
不用管a2有多少,不够可以打欠条,就变成-3了,这时a3就要向a3买3个。如此算下去,
只需要从第一个枚举到最后一个便可得到最小的答案。

note:
等价转换的思路.

date:
2016/5/20
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
const int maxn = 100000 + 5;

int main()
{
freopen("input.txt", "r", stdin);
int n;
while(~scanf("%d", &n) && n) {
long long ans = 0, last = 0, a;
for(int i = 0; i < n; i++) {
cin >> a;
ans += abs(last);
last += a;
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: