CF1854B.Earn or Unlock

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Andrea is playing the game Tanto Cuore.

He has a deck of nn cards with values a1,,ana_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 vv — and performs exactly one of the following two operations:

  1. Unlock the first vv locked cards in the deck from the top. If there are less than vv locked cards in the deck, then unlock all the locked cards.
  2. Earn vv 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 nn ( 1n1051 \leq n \leq 10^5 ) — the number of cards in the deck.

The second line contains nn integers a1,,ana_1, \ldots, a_n ( 0a1,,ann0 \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 22 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=94+5=9 victory points.

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

首页