D - Bonus EXP Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

高橋君は NN 匹のモンスターに順に出会います。ii 匹目 (1iN)(1\leq i\leq N) のモンスターの強さは AiA_i です。

高橋君はそれぞれのモンスターについて逃がすか倒すか選ぶことができます。
高橋君はそれぞれの行動によって次のように経験値を得ます。

  • モンスターを逃がした場合、得られる経験値は 00 である。
  • 強さが XX のモンスターを倒したとき、経験値を XX 得る。
    ただし、モンスターを倒すのが偶数回目(22, 44, \ldots 回目)であるとき、さらに追加で経験値を XX 得る。

NN 匹から高橋君が得た経験値の合計としてあり得る最大の値を求めてください。

制約

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

入力

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

NN
A1A_1 A2A_2 \ldots ANA_N

出力

NN 匹から高橋君が得た経験値の合計としてあり得る最大の値を整数で出力せよ。


入力例 1Copy

Copy
5
1 5 3 2 7

出力例 1Copy

Copy
28

1,2,3,51,2,3,5 匹目のモンスターを倒して、44 匹目のモンスターを逃したとき、高橋君は次のように経験値を得ます。

  • 強さが A1=1A_1=1 のモンスターを倒す。高橋君は 11 の経験値を得る。
  • 強さが A2=5A_2=5 のモンスターを倒す。高橋君は 55 の経験値を得る。モンスターを倒すのは 22 回目であるため、追加で経験値 55 を得る。
  • 強さが A3=3A_3=3 のモンスターを倒す。高橋君は 33 の経験値を得る。
  • 44 匹目のモンスターを逃がす。高橋君は経験値を得ない。
  • 強さが A5=7A_5=7 のモンスターを倒す。高橋君は 77 の経験値を得る。モンスターを倒すのは 44 回目であるため、追加で経験値 77 を得る。

よって、このとき 1+(5+5)+3+0+(7+7)=281+(5+5)+3+0+(7+7)=28 の経験値を得ます。
出会ったモンスターであっても、逃がした場合は倒した回数には数えられないことに注意してください。

高橋君がどのように行動しても得られる経験値は 2828 以下であるため、2828 を出力します。
なお、この場合においてすべてのモンスターを倒した時に得られる経験値は 1+(5+5)+3+(2+2)+7=251+(5+5)+3+(2+2)+7=25 となります。


入力例 2Copy

Copy
2
1000000000 1000000000

出力例 2Copy

Copy
3000000000

答えが 3232bit 整数型に収まらない可能性があることに注意してください。

Score : 400400 points

Problem Statement

Takahashi will encounter NN monsters in order. The ii-th monster (1iN)(1\leq i\leq N) has a strength of AiA_i.

For each monster, he can choose to either let it go or defeat it.
Each action awards him experience points as follows:

  • If he lets a monster go, he gains 00 experience points.
  • If he defeats a monster with strength XX, he gains XX experience points.
    If it is an even-numbered defeated monster (2nd, 4th, ...), he gains an additional XX experience points.

Find the maximum total experience points he can gain from the NN monsters.

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 1Ai1091\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 maximum total experience points he can gain from the NN monsters as an integer.


Sample Input 1Copy

Copy
5
1 5 3 2 7

Sample Output 1Copy

Copy
28

If Takahashi defeats the 1st, 2nd, 3rd, and 5th monsters, and lets the 4th monster go, he gains experience points as follows:

  • Defeats a monster with strength A1=1A_1=1. He gains 11 experience point.
  • Defeats a monster with strength A2=5A_2=5. He gains 55 experience points. As it is the 2nd defeated monster, he gains an additional 55 points.
  • Defeats a monster with strength A3=3A_3=3. He gains 33 experience points.
  • Lets the 4th monster go. Takahashi gains no experience points.
  • Defeats a monster with strength A5=7A_5=7. He gains 77 experience points. As it is the 4th defeated monster, he gains an additional 77 points.

Therefore, in this case, he gains 1+(5+5)+3+0+(7+7)=281+(5+5)+3+0+(7+7)=28 experience points.
Note that even if he encounters a monster, if he lets it go, it does not count as defeated.

He can gain at most 2828 experience points no matter how he acts, so print 2828.
As a side note, if he defeats all monsters in this case, he would gain 1+(5+5)+3+(2+2)+7=251+(5+5)+3+(2+2)+7=25 experience points.


Sample Input 2Copy

Copy
2
1000000000 1000000000

Sample Output 2Copy

Copy
3000000000

Beware that the answer may not fit in a 3232-bit integer.



2024-09-15 (Sun)
08:41:15 +09:00