CF1854B.Earn or Unlock
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:512MB
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?
安德烈正在玩《Tanto Cuore》这款游戏。

他拥有一副由 n 张卡片组成的牌堆,从上到下依次为 a1,…,an。每张卡片处于“锁定”或“未锁定”状态之一。初始时,仅有最顶端的卡片是未锁定的。
游戏以回合制进行。在每一回合中,安德烈需从牌堆中选择一张未锁定的卡片——该卡片上的数值为 v——并恰好执行以下两种操作之一:
- 从牌堆顶部开始,解锁前 v 张锁定的卡片;若牌堆中锁定的卡片总数少于 v 张,则解锁所有锁定的卡片;
- 获得 v 点胜利点数。
无论执行哪一种操作,在完成该操作后,这张被选中的卡片都会从牌堆中移除。
当牌堆中剩余的所有卡片均处于锁定状态,或牌堆中已无任何卡片时,游戏结束。
安德烈最多能获得多少点胜利点数?
输入格式
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.
第一行包含一个整数 n(1≤n≤105)—— 表示牌组中卡片的数量。
第二行包含 n 个整数 a1,…,an(0≤a1,…,an≤n)—— 表示牌组中各张卡片的数值。
输出格式
Output a single integer — the maximum number of victory points Andrea can earn.
输出一个整数——Andrea 能获得的最高胜利点数。
输入输出样例
输入#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.
在第一个样例测试中,卡组初始状态为 [未解锁,已锁定]。随后,安德烈使用第一张卡牌解锁第二张卡牌,再使用第二张卡牌获得 2 点胜利分数。
在第二个样例测试中,安德烈可以使用第一张卡牌解锁第二张和第三张卡牌,然后使用第二张和第三张卡牌获得 4+5=9 点胜利分数。
在第三个样例测试中,安德烈无法用第一张卡牌解锁任何卡牌,也无法获得任何胜利分数。
输入解题思路,AI测评打分。不知道怎么写?