QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#22387. Payback

Statistics

"Never a borrower nor a lender be." O how Bessie wishes she had taken that advice! She has borrowed from or lent money to each of $N$ $(1 \le N \le 100,000)$ friends, conveniently labeled $1,\ldots,N$.

Payback day has finally come. She knows she is owed more money than she owes to the other cows. They have all lined up in a straight line, cow $i$ standing $i$ meters from the barn. Bessie is going to traverse the line collecting money from those who owe her and reimbursing money to those she owes.

As she moves down the line, she can request any cow who owes her money to give her the money. When she has enough money to pay off any or all of her debts, she can pay the (recently collected) money to those she owes. Cow $i$ owes Bessie $D_{i}$ money $(-1,000 \le D_{i} \le 1,000; D_{i} \ne 0)$. A negative debt means that Bessie owes money to the cow instead of vice-versa.

Bessie starts at the barn, location 0. What is the minimum distance she must travel to collect her money and pay all those she owes? She must end her travels at the end of the line.

Input

  • Line 1: A single integer: $N$
  • Lines $2,\ldots,N+1$: Line $i+1$ contains a single integer: $D_{i}$

Output

  • Line 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.

Examples

Input 1

5
100
-200
250
-200
200

Output 1

9

Three cows owe Bessie; she owes two. When she's done, Bessie will have 150 money.

Barn  100  -200  250 -200  200
 |     |     |    |    |    |
 ***>**+**>*****>**+
                   *            < Bessie has 350
             -**<***
             *                  < Bessie has 150
             ***>****>****>**+
                             *  < Bessie has 350
                       -**<***
                       *     
                       ***>***  < Bessie ends her travels with 150*

Problem credits: Jarrah Lacko, 2009

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.