简单题
View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 100005
int n, f[maxn];
void input()
{
for (int i = 0; i < n; i++)
scanf("%d", &f[i]);
}
void work()
{
int i = 0, j = 0;
long long ans = 0;
while (1)
{
while (i < n && f[i] <= 0)
i++;
while (j < n && f[j] >= 0)
j++;
if (j >= n || i >= n)
break;
int x = min(f[i], -f[j]);
f[i] -= x;
f[j] += x;
ans += x * abs(i - j);
}
printf("%lld\n", ans);
}
int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n)
{
input();
work();
}
return 0;
}