题库练习 Earn or Unlock
← 上一题 下一题 →

A16141 | Earn or Unlock

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Andrea is playing the game Tanto Cuore.

![](/uploads/acgo/image/63f0b14f3749ad5c_2326ed834832.jpeg)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
C++ 编辑器
输入
输出