Poj 2940 wine trading in Gergovia (simple greedy)

Source: Internet
Author: User
Wine Trading in Gergovia
Time limit:1000 ms   Memory limit:65536 K
Total submissions:2765   Accepted:1245

Description

As you may know from the comic "Asterix and the chieftain's shield", Gergovia consists of one street, and every inhabitant of the city is a wine salesman. you wonder how this economy works? Simple enough: Everyone buys wine from other inhabitants of the city. every day each inhabitant decides how much wine he wants to buy or tables. interestingly, demand and supply is always the same, so that each inhabitant gets what he wants.

There is one problem, however: transporting wine from one house to another results in work. since all wines are equally good, the inhabitants of Gergovia don't care which persons they are doing trade with, they are only interested in selling or buying a specific amount of wine. they are clever enough to figure out a way of trading so that the overall amount of work needed for transports is minimized.

In this problem you are asked to reconstruct the trading during one day in Gergovia. for simplicity we will assume that the houses are built along a straight line with equal distance between adjacent houses. transporting one bottle of wine from one house to an adjacent house results in one unit of work.

Input

The input consists of several test cases.

Each test case starts with the number of inhabitantsN(2 ≤N≤ 100000). The following line contains N IntegersAI(− 1000 ≤AI≤ 1000). IfAI≥0, it means that the inhabitant living inITh House wants to buyAIBottles of wine, otherwise ifAI<0, he wants to wang−AIBottles of wine. You may assume that the numbersAISum up to 0.

The last test case is followed by a line containing 0.

Output

For each test case print the minimum amount of work units needed so that every inhabitant has his demand fulfilled. you may assume that this number fits into a signed 64-bit integer (in C/C ++ you can use the data type "long" or "_ int64 ", in java the data type "long ").

Sample Input

 
55-4 1-3 16-1000-1000-1000 1000 1000

Sample output

 
99000

Source

Ulm Local 2006 It seems complicated. It is actually a greedy question. Only one company can calculate. Accumulate from the first one.
 /*  Poj 2940 simple greedy  */  # Include <Stdio. h> # Include < String . H> # Include <Iostream>Using   Namespace  STD;  Int  Main (){  Int  N;  Int  A;  Long   Long  Ans;  Long   Long  SUM;  While (Scanf ( " % D  " ,& N), n) {sum = Ans = 0  ;  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % D  " ,& A); sum + = A; If (Sum> 0 ) Ans + = SUM;  Else Ans-= SUM;} printf (  "  % I64d \ n  "  , ANS );}  Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.