Powering the Hero (easy version)为英雄提供力量(简单版)

time limit per test

2 seconds

memory limit per test

256 megabytes

This is an easy version of the problem. It differs from the hard one only by constraints on nn and tt.

There is a deck of nn cards, each of which is characterized by its power. There are two types of cards:

  • a hero card, the power of such a card is always equal to 00;
  • a bonus card, the power of such a card is always positive.

You can do the following with the deck:

  • take a card from the top of the deck;
  • if this card is a bonus card, you can put it on top of your bonus deck or discard;
  • if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards.

Your task is to use such actions to gather an army with the maximum possible total power.

Input

The first line of input data contains single integer tt (1≤t≤10001≤t≤1000) --- the number of test cases in the test.

The first line of each test case contains one integer nn (1≤n≤50001≤n≤5000) --- the number of cards in the deck.

The second line of each test case contains nn integers s1,s2,...,sns1,s2,...,sn (0≤si≤1090≤si≤109) --- card powers in top-down order.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

Output

Output tt numbers, each of which is the answer to the corresponding test case --- the maximum possible total power of the army that can be achieved.

Example

Input

Copy

复制代码

5

5

3 3 3 0 0

6

0 3 3 0 0 3

7

1 2 3 0 4 5 0

7

1 2 5 0 4 3 0

5

3 1 0 0 4

Output

Copy

复制代码
6
6
8
9
4

Note

In the first sample, you can take bonuses 11 and 22. Both hero cards will receive 33 power. If you take all the bonuses, one of them will remain unused.

In the second sample, the hero's card on top of the deck cannot be powered up, and the rest can be powered up with 22 and 33 bonuses and get 66 total power.

In the fourth sample, you can take bonuses 11, 22, 33, 55 and skip the bonus 66, then the hero 44 will be enhanced with a bonus 33 by 55, and the hero 77 with a bonus 55 by 44. 4+5=94+5=9.

每次测试的时间限制 2 秒

每次测试的内存限制 256 兆字节

这是问题的简单版。它与困难版的区别仅在于对 n

和 t

的限制。

有一副 n

张牌,每张牌都有其力量。有两种牌:

英雄牌,这种牌的力量始终等于 0

奖励牌,这种牌的力量始终为正。

您可以对牌组执行以下操作:

从牌组顶部取出一张牌;

如果这张牌是奖励牌,您可以将其放在奖励牌组顶部或丢弃;

如果这张牌是英雄牌,则将奖励牌组顶部牌的力量添加到其力量中(如果该牌组不为空),然后将英雄添加到您的军队中,并使用的奖励牌丢弃。

您的任务是使用此类操作来召集一支总力量最大的军队。

输入

输入数据的第一行包含单个整数t

(1≤t≤1000

)------测试中的测试用例数。

每个测试用例的第一行包含一个整数n

(1≤n≤5000

)------牌堆中的牌数。

每个测试用例的第二行包含n

个整数s1,s2,...,sn

(0≤si≤109

)------按自上而下的顺序表示牌的威力。

保证所有测试用例的n

之和不超过5000

输出

输出t

个数字,每个数字都是对应测试用例的答案------军队可能达到的最大总威力。

示例

输入副本

5

5

3 3 3 0 0

6

0 3 3 0 0 3

7

1 2 3 0 4 5 0

7

1 2 5 0 4 3 0

5

3 1 0 0 4

输出副本

6

6

8

9

4

注意

在第一个示例中,您可以获得奖励 1

和 2

。两张英雄卡都将获得 3

力量。如果您获得所有奖励,其中一张将保持未使用状态。

在第二个示例中,牌组顶部的英雄卡无法增强,其余卡可以用 2

和 3

奖励增强并获得 6

总力量。

第四个例子中,你可以选择奖励 1

、2

、3

、5

,而跳过奖励 6

,那么英雄 4

将获得奖励 3

增强 5

,英雄 7

将获得奖励 5

增强 4

。4+5=9

代码:

cpp 复制代码
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void solve() {
    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;
        vector<int> s(n);

        // 读取卡牌力量值
        for (int i = 0; i < n; i++) {
            cin >> s[i];
        }

        // 优先队列用于存储附加卡(大根堆)
        priority_queue<int> bonusDeck;
        long long totalPower = 0;

        for (int i = 0; i < n; i++) {
            if (s[i] == 0) {
                // 如果是英雄卡,尝试使用附加卡(如果有的话)
                if (!bonusDeck.empty()) {
                    totalPower += bonusDeck.top();
                    bonusDeck.pop();
                }
            }
            else {
                // 如果是附加卡,将其加入优先队列(大根堆)
                bonusDeck.push(s[i]);
            }
        }

        cout << totalPower << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
相关推荐
AI进化营-智能译站2 小时前
ROS2 C++开发系列17-多线程驱动多传感器|chrono高精度计时实现机器人同步控制
java·c++·ai·机器人
墨染千千秋5 小时前
C++函数的使用以及主函数
c++
特种加菲猫6 小时前
继承,一场跨越时空的对话
开发语言·c++
WBluuue6 小时前
Codeforces 1093 Div2(ABCD1D2)
c++·算法
玩转单片机与嵌入式7 小时前
玩转边缘AI(TInyML):需要掌握的C++知识汇总!
开发语言·c++·人工智能
历程里程碑7 小时前
4 Git远程协作:从零开始,玩转仓库关联与代码同步(带实操代码讲解)
大数据·c++·git·elasticsearch·搜索引擎·gitee·github
汉克老师8 小时前
GESP5级C++考试语法知识(贪心算法(一)课堂例题精讲)
c++·贪心算法·gesp5级·gesp五级·贪心规律
墨染千千秋8 小时前
C++头文件的使用,和各个头文件与头文件用处
c++
呱呱巨基8 小时前
Linux 基础IO
linux·c++·笔记·学习
旖-旎8 小时前
深搜练习(N皇后)(10)
c++·算法·深度优先·力扣