CF1854B.Earn or Unlock
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Andrea is playing the game Tanto Cuore.
He has a deck of n cards with values a1,…,an 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:
- 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.
- 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≤n≤105 ) — the number of cards in the deck.
The second line contains n integers a1,…,an ( 0≤a1,…,an≤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.