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;
}
相关推荐
cookies_s_s1 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
不想编程小谭1 小时前
力扣LeetCode: 2506 统计相似字符串对的数目
c++·算法·leetcode
曼巴UE52 小时前
UE5.3 C++ TArray系列(一)
开发语言·c++·ue5
阿巴~阿巴~3 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
CoderCodingNo4 小时前
【GESP】C++二级真题 luogu-b3924, [GESP202312 二级] 小杨的H字矩阵
java·c++·矩阵
刃神太酷啦5 小时前
堆和priority_queue
数据结构·c++·蓝桥杯c++组
Heris995 小时前
2.22 c++练习【operator运算符重载、封装消息队列、封装信号灯集】
开发语言·c++
----云烟----5 小时前
C/C++ 中 volatile 关键字详解
c语言·开发语言·c++
ChoSeitaku6 小时前
12.重复内容去重|添加日志|部署服务到Linux上(C++)
linux·c++·windows
挣扎与觉醒中的技术人6 小时前
网络安全入门持续学习与进阶路径(一)
网络·c++·学习·程序人生·安全·web安全