Codeforces Round 889 (Div. 1)
B
Earn or Unlock
Codeforces
2200
原题
← A2C →
翻译中…

Andrea is playing the game Tanto Cuore.

翻译中…

image

翻译中…

He has a deck of $n$ cards with values $a_1, \ldots, a_n$ from top to bottom. Each card can be either locked or unlocked. Initially, only the topmost card is unlocked.

翻译中…

The game proceeds in turns. In each turn, Andrea chooses an unlocked card in the deck — the value written on the card is $v$ — and performs exactly one of the following two operations:

翻译中…
  1. Unlock the first $v$ locked cards in the deck from the top. If there are less than $v$ locked cards in the deck, then unlock all the locked cards.
  2. Earn $v$ victory points.
翻译中…

In both cases, after performing the operation, he removes the card from the deck.

翻译中…

The game ends when all the cards remaining in the deck are locked, or there are no more cards in the deck.

翻译中…

What is the maximum number of victory points Andrea can earn?

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of cards in the deck.

翻译中…

The second line contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_1, \ldots, a_n \leq n$) — the values of the cards in the deck.

翻译中…

Output

翻译中…

Output a single integer — the maximum number of victory points Andrea can earn.

翻译中…

Note

翻译中…

In the first sample test, the deck starts as [unlocked, locked]. Then, Andrea uses the first card to unlock the second card. Then he uses the second card to earn $2$ victory points.

翻译中…

In the second sample test, Andrea can use the first card to unlock the second and the third card. Then he uses the second and the third card to earn $4+5=9$ victory points.

翻译中…

In the third sample test, Andrea can't unlock any cards or gain any victory points with the first card.

翻译中…

样例

Input #1
2
1 2
Output #1
2
InputCopy OutputCopy
Input #2
5
2 4 5 0 1
Output #2
9
InputCopy OutputCopy
Input #3
4
0 4 4 4
Output #3
0
InputCopy OutputCopy
题目信息
题号
1854B
时间限制
3 s
内存限制
512 MB
标签
位运算
暴力
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。