Earn or Unlock

题意翻译

有一长度为 $n$ 的一副牌,每张牌上都有一个数字,设第 $i$ 张牌上的数字为 $a_i$。初始时,你手里只有第一张牌。对于每一张牌,你有两种选择: - 如果剩余的牌数量 $< a_i$,则将牌摸完,否则继续往下摸 $a_i$ 张牌。摸牌完成后,这张牌会被丢弃。 - 获得 $a_i$ 的分数,并丢弃这张牌。 求你能获得的最大分数。 对于所有数据,保证 $1 \le n \le 10 ^ 5$,$0 \le a_i \le n$。

题目描述

Andrea is playing the game Tanto Cuore. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1854B/1661c88808ceec929a74ca91720280905e810592.png)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?

输入输出格式

输入格式


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 a single integer — the maximum number of victory points Andrea can earn.

输入输出样例

输入样例 #1

2
1 2

输出样例 #1

2

输入样例 #2

5
2 4 5 0 1

输出样例 #2

9

输入样例 #3

4
0 4 4 4

输出样例 #3

0

说明

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.