Test instructions
N Equidistant villages, each village either buys wine or sells liquor, and the K-units of wine are transported to neighboring villages to require K-units of labor, asking at least how much labor is needed to meet the requirements of all villages;
Ideas:
Last time did a ring, this is a straight line, is a flood problem;
The first village of wine can only come from the second village, so the transfer of a[1] to a[2], now is the equivalent of a sub-problem;
AC Code:
#include <bits/stdc++.h>#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;#definefor (i,j,n) for (int i=j;i<=n;i++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong Longll;template<classT>voidRead (t&num) { CharCH;BOOLf=false; for(Ch=getchar (); ch<'0'|| Ch>'9'; f= ch=='-', ch=GetChar ()); for(num=0; ch>='0'&&ch<='9'; num=num*Ten+ch-'0', ch=GetChar ()); F&& (num=-num);}intstk[ -], Tp;template<classT> Inlinevoidprint (T p) {if(!p) {Puts ("0");return; } while(p) stk[++ TP] = p%Ten, p/=Ten; while(TP) Putchar (stk[tp--] +'0'); Putchar ('\ n');}ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=1e9;Const intn=12e4+Ten;Const intmaxn=1e3+Ten;Const Doubleeps=1e-Ten;intN,a[n];intMain () { while(1) {read (n); if(!n) Break; For (I,1, N) read (a[i]); LL ans=0; For (I,1, N) {ans=ans+ABS (A[i]); A[i+1]=a[i+1]+A[i]; } cout<<ans<<Endl; } return 0;}
UVA-11054 (Scanning method)