実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 点
問題文
高橋くんはレストランで、 品からなる奇妙なフルコースを楽しむことにしました。
このコースのうち 番目の料理は以下の通りです。
- の場合、美味しさが の 解毒剤入り の料理
- の場合、美味しさが の 毒入り の料理
高橋くんが料理を食べると、高橋くんの状態は以下のように変化します。
- 最初、高橋くんはお腹を壊していない。
- 高橋くんが お腹を壊していない 時、
- 解毒剤入り の料理を食べても、高橋くんは お腹を壊していないまま である。
- 毒入り の料理を食べると、高橋くんは お腹を壊す 。
- 高橋くんが お腹を壊している 時、
- 解毒剤入り の料理を食べると、高橋くんは お腹を壊していない状態になる 。
- 毒入り の料理を食べると、高橋くんは 死ぬ 。
コースは以下の流れで進行します。
- についてこの順に、以下の処理を繰り返す。
- まず、 番目の料理が高橋くんに提供される。
- 次に、 高橋くんはこの料理に対し「食べる」か「下げてもらう」かを選択する。
- 「食べる」を選択した場合、高橋くんは 番目の料理を食べる。食べた料理に応じて高橋くんの状態も変化する。
- 「下げてもらう」を選択した場合、高橋くんは 番目の料理を食べない。この料理を後で提供してもらったり何らかの手段で保存したりすることはできない。
- 最後に、 (状態が変化するなら変化後の時点で) 高橋くんが死んでいない場合、
- なら次の料理に進む。
- なら高橋くんは生きて退店する。
高橋くんはこのあと重要な仕事があるため、高橋くんは生きて退店しなければなりません。
この条件の下で高橋くんが各料理に対し「食べる」「下げてもらう」を選択したとき、高橋くんが 食べた料理の美味しさの総和として考えられる最大値 ( 但し、何も食べなかった場合は ) を出力してください。
制約
- 入力は全て整数
- つまり、 は のどちらかである。
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを整数として出力せよ。
入力例 1Copy
5 1 100 1 300 0 -200 1 500 1 300
出力例 1Copy
600
以下のように選択することで食べた料理の美味しさの総和を にでき、これが考えられる最大値です。
- 番目の料理を下げてもらう。高橋くんはお腹を壊していません。
- 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は となります。
- 番目の料理を食べる。高橋くんはお腹を壊していない状態に戻り、食べた料理の美味しさの総和は となります。
- 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は となります。
- 番目の料理を下げてもらう。高橋くんはお腹を壊しています。
- 最終的に高橋くんは死んでいないので、高橋くんは生きて退店する。
入力例 2Copy
4 0 -1 1 -2 0 -3 1 -4
出力例 2Copy
0
この入力の場合何も食べないことが最善ですが、この場合答えは となります。
入力例 3Copy
15 1 900000000 0 600000000 1 -300000000 0 -700000000 1 200000000 1 300000000 0 -600000000 1 -900000000 1 600000000 1 -100000000 1 -400000000 0 900000000 0 200000000 1 -500000000 1 900000000
出力例 3Copy
4100000000
答えが bit 符号付き整数に収まらない可能性があります。
Score : points
Problem Statement
Takahashi has decided to enjoy a wired full-course meal consisting of courses in a restaurant.
The -th course is:
- if , an antidotal course with a tastiness of ;
- if , a poisonous course with a tastiness of .
When Takahashi eats a course, his state changes as follows:
- Initially, Takahashi has a healthy stomach.
- When he has a healthy stomach,
- if he eats an antidotal course, his stomach remains healthy;
- if he eats a poisonous course, he gets an upset stomach.
- When he has an upset stomach,
- if he eats an antidotal course, his stomach becomes healthy;
- if he eats a poisonous course, he dies.
The meal progresses as follows.
- Repeat the following process for in this order.
- First, the -th course is served to Takahashi.
- Next, he chooses whether to "eat" or "skip" the course.
- If he chooses to "eat" it, he eats the -th course. His state also changes depending on the course he eats.
- If he chooses to "skip" it, he does not eat the -th course. This course cannot be served later or kept somehow.
- Finally, (if his state changes, after the change) if he is not dead,
- if , he proceeds to the next course.
- if , he makes it out of the restaurant alive.
An important meeting awaits him, so he must make it out of there alive.
Find the maximum possible sum of tastiness of the courses that he eats (or if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition.
Constraints
- All input values are integers.
- In other words, is either or .
Input
The input is given from Standard Input in the following format:
Output
Print the answer as an integer.
Sample Input 1Copy
5 1 100 1 300 0 -200 1 500 1 300
Sample Output 1Copy
600
The following choices result in a total tastiness of the courses that he eats amounting to , which is the maximum possible.
- He skips the -st course. He now has a healthy stomach.
- He eats the -nd course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to .
- He eats the -rd course. He now has a healthy stomach again, and the total tastiness of the courses that he eats amounts to .
- He eats the -th course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to .
- He skips the -th course. He now has an upset stomach.
- In the end, he is not dead, so he makes it out of the restaurant alive.
Sample Input 2Copy
4 0 -1 1 -2 0 -3 1 -4
Sample Output 2Copy
0
For this input, it is optimal to eat nothing, in which case the answer is .
Sample Input 3Copy
15 1 900000000 0 600000000 1 -300000000 0 -700000000 1 200000000 1 300000000 0 -600000000 1 -900000000 1 600000000 1 -100000000 1 -400000000 0 900000000 0 200000000 1 -500000000 1 900000000
Sample Output 3Copy
4100000000
The answer may not fit into a -bit integer type.