A16182 | Card Game
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ cards stacked in a deck. Initially, $a_{i}$ is written on the $i$ -th card from the top. The value written on a card does not change.
You will play a game. Initially your score is $0$ . In each turn, you can do one of the following operations:
- Choose an odd $^{\dagger}$ positive integer $i$ , which is not greater than the number of cards left in the deck. Remove the $i$ -th card from the top of the deck and add the number written on the card to your score. The remaining cards will be reindexed starting from the top.
- Choose an even $^{\ddagger}$ positive integer $i$ , which is not greater than the number of cards left in the deck. Remove the $i$ -th card from the top of the deck. The remaining cards will be reindexed starting from the top.
- End the game. You can end the game whenever you want, you do not have to remove all cards from the initial deck.
What is the maximum score you can get when the game ends?
$^{\dagger}$ An integer $i$ is odd, if there exists an integer $k$ such that $i = 2k + 1$ .
$^{\ddagger}$ An integer $i$ is even, if there exists an integer $k$ such that $i = 2k$ .
You will play a game. Initially your score is $0$ . In each turn, you can do one of the following operations:
- Choose an odd $^{\dagger}$ positive integer $i$ , which is not greater than the number of cards left in the deck. Remove the $i$ -th card from the top of the deck and add the number written on the card to your score. The remaining cards will be reindexed starting from the top.
- Choose an even $^{\ddagger}$ positive integer $i$ , which is not greater than the number of cards left in the deck. Remove the $i$ -th card from the top of the deck. The remaining cards will be reindexed starting from the top.
- End the game. You can end the game whenever you want, you do not have to remove all cards from the initial deck.
What is the maximum score you can get when the game ends?
$^{\dagger}$ An integer $i$ is odd, if there exists an integer $k$ such that $i = 2k + 1$ .
$^{\ddagger}$ An integer $i$ is even, if there exists an integer $k$ such that $i = 2k$ .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 10^{4}$ ). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^{5}$ ).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-10^{9} \le a_i \le 10^{9}$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^{5}$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^{5}$ ).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-10^{9} \le a_i \le 10^{9}$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^{5}$ .
输出格式
For each test case, print a single integer — the maximum score you can get when the game ends.
输入输出样例
输入 #1
4 4 -4 1 -3 5 4 1 -2 3 -4 3 -1 3 -5 1 -1
输出 #1
5 4 2 0
In the first test case, one can get the score of $5$ as follows:
1. In the first turn, choose $i=2$ . Your score remains $0$ and the numbers written on the cards from the top will become $[-4, -3, 5]$ .
2. In the second turn, choose $i=3$ . Your score will become $5$ and the numbers written on the cards from the top will become $[-4, -3]$ .
3. In the third turn, end the game with the score of $5$ .
In the second test case, one can get the score of $4$ as follows:
1. In the first turn, choose $i=3$ . Your score will become $3$ and the numbers written on the cards from the top will become $[1, -2, -4]$ .
2. In the second turn, choose $i=1$ . Your score will become $4$ and the numbers written on the cards from the top will become $[-2, -4]$ .
3. In the third turn, end the game with the score of $4$ .
In the third test case, one can get the score of $2$ as follows:
1. In the first turn, choose $i=1$ . Your score will become $-1$ and the numbers written on the cards from the top will become $[3, -5]$ .
2. In the second turn, choose $i=1$ . Your score will become $2$ and the numbers written on the cards from the top will become $[-5]$ .
3. In the third turn, end the game with the score of $2$ .
1. In the first turn, choose $i=2$ . Your score remains $0$ and the numbers written on the cards from the top will become $[-4, -3, 5]$ .
2. In the second turn, choose $i=3$ . Your score will become $5$ and the numbers written on the cards from the top will become $[-4, -3]$ .
3. In the third turn, end the game with the score of $5$ .
In the second test case, one can get the score of $4$ as follows:
1. In the first turn, choose $i=3$ . Your score will become $3$ and the numbers written on the cards from the top will become $[1, -2, -4]$ .
2. In the second turn, choose $i=1$ . Your score will become $4$ and the numbers written on the cards from the top will become $[-2, -4]$ .
3. In the third turn, end the game with the score of $4$ .
In the third test case, one can get the score of $2$ as follows:
1. In the first turn, choose $i=1$ . Your score will become $-1$ and the numbers written on the cards from the top will become $[3, -5]$ .
2. In the second turn, choose $i=1$ . Your score will become $2$ and the numbers written on the cards from the top will become $[-5]$ .
3. In the third turn, end the game with the score of $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted