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;
}
相关推荐
C++ 老炮儿的技术栈6 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl6 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu628886 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
波特率1152007 小时前
const关键字与函数的重载
开发语言·c++·函数重载
干啥啥不行,秃头第一名7 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
2301_807367197 小时前
C++中的解释器模式变体
开发语言·c++·算法
2301_819414309 小时前
C++与区块链智能合约
开发语言·c++·算法
不想看见4049 小时前
Valid Parentheses栈和队列--力扣101算法题解笔记
开发语言·数据结构·c++
老约家的可汗10 小时前
C/C++内存管理探秘:从内存分布到new/delete的底层原理
c语言·c++
天赐学c语言10 小时前
Linux - 应用层自定义协议与序列/反序列化
linux·服务器·网络·c++