链接
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1995
题解
这题有点递推的味道
有一件事情是显然的,假设村庄的酒可以是负数,那么你没必要从不相邻的村庄运酒过来,因为即使从不相邻的村庄运过来,还是必须经过相邻的村庄,那么我们只从相邻的村庄运酒就行了
第一个村庄只能第二个运过来,所以直接执行这步操作就好了,运完之后,以后在也没必要把酒运到1或者从1往右运了,一次现在第二个村庄就相当于最左边的村庄,那么这个问题就和之前一样了。。。一步一步执行下去,每次看看当前村庄是多酒还是少酒,多酒就往右运,少就从右边运过来。
代码
//水题
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#define ll long long
#define maxn 100010
using namespace std;
ll ans, n, a[maxn];
ll read(ll x=0)
{
char c, f=1;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-48;
return f*x;
}
void solve()
{
int i;
for(i=1;i<=n;i++)a[i]=read();
for(ans=0,i=1;i<=n;i++)ans+=abs(a[i]), a[i+1]+=a[i];
printf("%lld\n",ans);
}
int main()
{
for(n=read();n;n=read())solve();
return 0;
}