/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は商店街でお店を経営しています。
お店では N 日間の売上データを記録しており、i 日目(1 \leq i \leq N)の損益は V_i 円でした。V_i が正のときはその日に V_i 円の利益があったことを、V_i が負のときはその日に |V_i| 円の損失があったことを、V_i が 0 のときは利益も損失もなかったことを意味します。
高橋君は、最も調子が良かった時期を把握するために、連続する 1 日以上の期間を 1 つ選び、その期間の損益の合計を最大化したいと考えています。
具体的には、1 \leq L \leq R \leq N を満たすすべての整数の組 (L, R) について、L 日目から R 日目までの損益の合計
\displaystyle\sum_{i=L}^{R} V_i
を考えたとき、その最大値を求めてください。
ただし、期間は必ず 1 日以上選ばなければなりません。すべての日が損失である場合でも、損失が最も小さい(V_i が最も大きい)1 日間を選んだ場合の値(負の値)を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq V_i \leq 10^9
- 入力はすべて整数
入力
N V_1 V_2 \cdots V_N
- 1 行目には、記録した日数を表す整数 N が与えられる。
- 2 行目には、各日の損益を表す N 個の整数 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。
出力
連続する 1 日以上の期間における損益の合計の最大値を、整数で 1 行に出力してください。
入力例 1
5 3 -2 5 -1 2
出力例 1
7
入力例 2
8 -3 4 -1 2 1 -5 4 3
出力例 2
8
入力例 3
12 -2 -3 4 -1 -2 1 5 -3 2 -1 4 -2
出力例 3
9
Score : 333 pts
Problem Statement
Takahashi runs a shop in a shopping district.
The shop has recorded sales data over N days, and the profit/loss on day i (1 \leq i \leq N) was V_i yen. When V_i is positive, it means there was a profit of V_i yen on that day; when V_i is negative, it means there was a loss of |V_i| yen on that day; when V_i is 0, it means there was neither profit nor loss.
To identify the period when business was at its best, Takahashi wants to select one contiguous period of at least 1 day and maximize the total profit/loss over that period.
Specifically, for all pairs of integers (L, R) satisfying 1 \leq L \leq R \leq N, consider the total profit/loss from day L to day R:
\displaystyle\sum_{i=L}^{R} V_i
Find the maximum value of this sum.
Note that the selected period must contain at least 1 day. Even if every day results in a loss, you must output the value (which will be negative) obtained by selecting the single day with the smallest loss (the largest V_i).
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq V_i \leq 10^9
- All input values are integers
Input
N V_1 V_2 \cdots V_N
- The first line contains an integer N representing the number of recorded days.
- The second line contains N integers V_1, V_2, \ldots, V_N separated by spaces, representing the profit/loss for each day.
Output
Print the maximum total profit/loss over a contiguous period of at least 1 day, as an integer on a single line.
Sample Input 1
5 3 -2 5 -1 2
Sample Output 1
7
Sample Input 2
8 -3 4 -1 2 1 -5 4 3
Sample Output 2
8
Sample Input 3
12 -2 -3 4 -1 -2 1 5 -3 2 -1 4 -2
Sample Output 3
9