A - Operations on a Stack Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

長さ NN の整数列 (A1,A2,,AN)(A_1, A_2, \ldots, A_N) が与えられます。 また、列 SS があり、SS ははじめは空列です。

i=1,2,,Ni = 1, 2, \ldots, N の順に、それぞれの ii について、下記の 22 つの操作のうちちょうど 11 つを行います。

  • SS の末尾に AiA_i を要素として追加する。
  • SS の末尾の要素を削除する。ただし SS が空のときは、この操作を行うことを選択できない。

すべての操作を終えた後の、SS の要素の総和としてあり得る最大値を出力してください。

制約

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 109Ai109-10^9 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 \ldots ANA_N

出力

答えを出力せよ。


入力例 1Copy

Copy
6
3 -1 -4 5 -9 2

出力例 1Copy

Copy
8

SS が空列である初期状態から、下記の通りに操作を行うことを考えます。

  • i=1i = 1 について、SS の末尾に A1=3A_1 = 3 を追加する。その結果、S=(3)S = (3) となる。
  • i=2i = 2 について、SS の末尾に A2=1A_2 = -1 を追加する。その結果、S=(3,1)S = (3, -1) となる。
  • i=3i = 3 について、SS の末尾の要素を削除する。その結果、S=(3)S = (3) となる。
  • i=4i = 4 について、SS の末尾に A4=5A_4 = 5 を追加する。その結果、S=(3,5)S = (3, 5) となる。
  • i=5i = 5 について、SS の末尾に A5=9A_5 = -9 を追加する。その結果、S=(3,5,9)S = (3, 5, -9) となる。
  • i=6i = 6 について、SS の末尾の要素を削除する。その結果、S=(3,5)S = (3, 5) となる。

このとき、すべての操作を終えた後の SS の要素の総和は、3+5=83 + 5 = 8 であり、これがあり得る最大値です。


入力例 2Copy

Copy
1
-1

出力例 2Copy

Copy
-1

SS が空のときは必ず、要素を追加する方の操作を選ばなければならないことに注意してください。


入力例 3Copy

Copy
20
-14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16

出力例 3Copy

Copy
369

Score : 400400 points

Problem Statement

You are given an integer sequence of length NN: (A1,A2,,AN)(A_1, A_2, \ldots, A_N). There is also a sequence SS, which is initially empty.

For each i=1,2,,Ni = 1, 2, \ldots, N in this order, you perform exactly one of the following two operations:

  • Append AiA_i as an element to the end of SS.
  • Delete the last element of SS. You cannot choose this operation if SS is empty.

Print the maximum possible value of the sum of the elements of SS after all operations.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 109Ai109-10^9 \leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.


Sample Input 1Copy

Copy
6
3 -1 -4 5 -9 2

Sample Output 1Copy

Copy
8

Starting from the initial state where SS is an empty sequence, consider the following operations:

  • For i=1i = 1, append A1=3A_1 = 3 to the end of SS. Now, S=(3)S = (3).
  • For i=2i = 2, append A2=1A_2 = -1 to the end of SS. Now, S=(3,1)S = (3, -1).
  • For i=3i = 3, delete the last element of SS. Now, S=(3)S = (3).
  • For i=4i = 4, append A4=5A_4 = 5 to the end of SS. Now, S=(3,5)S = (3, 5).
  • For i=5i = 5, append A5=9A_5 = -9 to the end of SS. Now, S=(3,5,9)S = (3, 5, -9).
  • For i=6i = 6, delete the last element of SS. Now, S=(3,5)S = (3, 5).

Here, the sum of the elements of SS after all operations is 3+5=83 + 5 = 8, which is the maximum possible value.


Sample Input 2Copy

Copy
1
-1

Sample Output 2Copy

Copy
-1

Note that if SS is empty, you must choose to append an element.


Sample Input 3Copy

Copy
20
-14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16

Sample Output 3Copy

Copy
369


2025-03-13 (Thu)
14:34:54 +00:00