

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
長さ の整数列 が与えられます。 また、列 があり、 ははじめは空列です。
の順に、それぞれの について、下記の つの操作のうちちょうど つを行います。
- の末尾に を要素として追加する。
- の末尾の要素を削除する。ただし が空のときは、この操作を行うことを選択できない。
すべての操作を終えた後の、 の要素の総和としてあり得る最大値を出力してください。
制約
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを出力せよ。
入力例 1Copy
6 3 -1 -4 5 -9 2
出力例 1Copy
8
が空列である初期状態から、下記の通りに操作を行うことを考えます。
- について、 の末尾に を追加する。その結果、 となる。
- について、 の末尾に を追加する。その結果、 となる。
- について、 の末尾の要素を削除する。その結果、 となる。
- について、 の末尾に を追加する。その結果、 となる。
- について、 の末尾に を追加する。その結果、 となる。
- について、 の末尾の要素を削除する。その結果、 となる。
このとき、すべての操作を終えた後の の要素の総和は、 であり、これがあり得る最大値です。
入力例 2Copy
1 -1
出力例 2Copy
-1
が空のときは必ず、要素を追加する方の操作を選ばなければならないことに注意してください。
入力例 3Copy
20 -14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16
出力例 3Copy
369
Score : points
Problem Statement
You are given an integer sequence of length : . There is also a sequence , which is initially empty.
For each in this order, you perform exactly one of the following two operations:
- Append as an element to the end of .
- Delete the last element of . You cannot choose this operation if is empty.
Print the maximum possible value of the sum of the elements of after all operations.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
6 3 -1 -4 5 -9 2
Sample Output 1Copy
8
Starting from the initial state where is an empty sequence, consider the following operations:
- For , append to the end of . Now, .
- For , append to the end of . Now, .
- For , delete the last element of . Now, .
- For , append to the end of . Now, .
- For , append to the end of . Now, .
- For , delete the last element of . Now, .
Here, the sum of the elements of after all operations is , which is the maximum possible value.
Sample Input 2Copy
1 -1
Sample Output 2Copy
-1
Note that if is empty, you must choose to append an element.
Sample Input 3Copy
20 -14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16
Sample Output 3Copy
369