Test instructions: There are N (2<=n<=100000) equidistant villages in the line, and each village buys wine or sells liquor. The demand for wine in the first village is Ai ( -1000<=ai<=1000), of which ai>0 means to buy wine and ai<0 to sell alcohol. The balance of supply and demand in all villages, i.e. all Ai sum equals 0. Transporting K-units of wine from one village to the neighboring village requires a workforce of k units. Calculating at least how much labor is needed to meet the needs of all villages.
Analysis: From the left-hand village, whether he buys wine or sells alcohol, there is A0 traffic relative to his neighboring village, so the traffic is constantly accumulating or offsetting, counting to the right-most village.
#pragma COMMENT (linker, "/stack:102400000, 102400000") #include <cstdio> #include <cstring> #include < cstdlib> #include <cctype> #include <cmath> #include <iostream> #include <sstream> #include <iterator> #include <algorithm> #include <string> #include <vector> #include <set># include<map> #include <stack> #include <deque> #include <queue> #include <list> #define Min (A, B) ((a < b) a:b) #define MAX (A, B) ((a < b) b:a) typedef long Long ll;typedef unsigned long long Ull;co NST int int_inf = 0x3f3f3f3f;const int int_m_inf = 0x7f7f7f7f;const ll ll_inf = 0x3f3f3f3f3f3f3f3f;const ll LL_M_INF = 0x7 f7f7f7f7f7f7f7f;const int dr[] = {0, 0,-1, 1,-1,-1, 1, 1};const int dc[] = {-1, 1, 0, 0,-1, 1,-1, 1};const int MOD = 1e9 + 7;const Double pi = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 100000 + 10;const int maxt = 10000 + 10;usin G namespace Std;int main () {int n; while (scanf ("%d", &n) = = 1) { if (!n) return 0; LL x, ans = 0, last = 0; for (int i = 0; i < n; ++i) {scanf ("%lld", &x); Ans + = ABS (last); Last + = x; } printf ("%lld\n", ans); } return 0;}
UVA-11054 Wine Trading in Gergovia (Gergovia's liquor trading) (greedy + analog)