QOJ.ac

QOJ

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

#22387. 返済

Statistics

「借り手にも貸し手にもなるな。」Bessie は助言に従えばよかったと思います!$N$ 人、$(1 \le N \le 100,000)$ の友達と貸し借りがあり、番号は $1,\ldots,N$ です。

返済日です。受け取る総額は返す総額より多いと知っています。牛は直線に並び、牛 $i$ は納屋から $i$ メートル。Bessie は列を歩き、貸した金を回収し、借りた金を返します。

歩きながら貸した牛に返済を頼めます。一部またはすべての債務を返せる金があれば、最近受け取った金で返せます。牛 $i$ は Bessie に $D_{i}$ の借りがあり、$(-1,000 \le D_{i} \le 1,000; D_{i} \ne 0)$。負なら逆に Bessie が借りています。

納屋の位置 0 から出発し、全額回収と返済に必要な最小距離は?列の末端で終えなければなりません。

入力

  • 1 行目:整数 $N$。
  • $2,\ldots,N+1$ 行目:$i+1$ 行目に整数 $D_{i}$。

出力

  • 1 行目:各牛との回収または返済に必要な総距離をメートルで表す整数。

入出力例

入力 1

5
100
-200
250
-200
200

出力 1

9

注記

三頭が Bessie に借り、Bessie は二頭から借りています。終了後 150 が残ります。

      100  -200  250 -200  200
 |     |     |    |    |    |
 ***>**+**>*****>**+
                   *            < Bessie の所持金 350
             -**<***
             *                  < Bessie の所持金 150
             ***>****>****>**+
                             *  < Bessie の所持金 350
                       -**<***
                       *     
                       ***>***  < Bessie の終了時の所持金 150*

左端は納屋。

問題作成者: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.