spoj#MLK. Milk Trading

Milk Trading

Hareedi and Hanadt live in a village with N houses built next to each other along one

straight roadway with equal distances between adjacent houses. Some people own cows

and sell the milk their cows produce, while others do not own cows and may need to buy

milk.

Every morning, Hareedi and Hanadi transport milk around the village such that everyone

buys or sells the exact number of bottles they want. Transporting one bottle of milk from

one house to an adjacent house results in one unit of work. Fortunately, the number of

milk bottles needed by people is always equal to the number of milk bottles sold by

cow owners.

 

Task

write a program that is given the number of milk bottles that people at each house needs

to buy or sell, represented as an integer, finds the minimum work necessary to transport

the bottles from sellers to buyers.

 

Constraints

2 ≤ N ≤ 1,000,000     The number of houses.

-1000 Bi 1000      Integer representing the number of bottles to be bought from or

                              sold to people at the house at position i.

Input

  • Line 1 contains the integer N, the number of houses.
  • Each of the next N lines contains an integer Bi. If Bi > 0 then people in house i are cow

  owners and want to sell Bi bottles of milk, If Bi ≤ 0 then people at house i are not cow owners

     and want to buy Bi bottles of milk.

 Output

  • A single line containing a single integer, the minimum work units necessary to

  transport the bottle of milk.

Example

Input:
5
5
-4
1
-3
1
Output:
9
There are 5 houses as shown in the table.

Position

1

2

3

4

5

Bottles

5

-4

1

-3

1

4 bottles are transported from house 1 to house 2, 1 bottle from house 1 to
house 4, 1 bottle from house 3 to house 4, 1 bottle from house 5 to house 4.

  • Number of test-cases is 28.