A16141 | Earn or Unlock
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Andrea is playing the game Tanto Cuore.
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?
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.
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted