CF1800C2.Powering the Hero (hard version)

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is a hard version of the problem. It differs from the easy 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.

这是一个该问题的困难版本,与简单版本的唯一区别在于对 nn 和 tt 的约束条件。

有一副包含 nn 张卡牌的牌堆,每张牌均具有一个“力量值”。卡牌分为两类:

  • 英雄牌:其力量值恒为 00;
  • 增益牌:其力量值恒为正数。

你可对牌堆执行以下操作:

  • 从牌堆顶部取出一张牌;
  • 若该牌为增益牌,则你可以将其置于你的增益牌堆顶部,或直接弃掉;
  • 若该牌为英雄牌,则(若你的增益牌堆非空)将增益牌堆顶部那张牌的力量值加到该英雄牌的力量值上;随后,将该英雄牌加入你的军队,且已使用的那张增益牌被弃掉。

你的任务是通过上述操作,组建一支总力量值尽可能大的军队。

输入格式

The first line of input data contains single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases in the test.

The first line of each test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of cards in the deck.

The second line of each test case contains nn integers s1,s2,…,sns_1, s_2, \dots, s_n (0≤si≤1090 \le s_i \le 10^9) — card powers in top-down order.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示牌组中卡片的数量。

每个测试用例的第二行包含 nn 个整数 s1,s2,…,sns_1, s_2, \dots, s_n(0≤si≤1090 \le s_i \le 10^9)—— 表示从上到下的各张卡片的点数。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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.

输出 tt 个数字,每个数字对应一个测试用例的答案——即所能达到的军队总战力的最大值。

输入输出样例

  • 输入#1

    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

    输出#1

    6
    6
    8
    9
    4

说明/提示

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.

在第一个样例中,你可以选择奖励 11 和 22。两张英雄卡都将获得 33 点力量值。若你选择全部奖励,则其中有一个奖励将无法使用。

在第二个样例中,牌堆顶部的英雄卡无法被增强,其余英雄卡可分别用奖励 22 和 33 进行增强,总共获得 66 点力量值。

在第四个样例中,你可以选择奖励 11、22、33、55,跳过奖励 66;此时英雄 44 将通过奖励 33 获得 55 点力量值增强,英雄 77 将通过奖励 55 获得 44 点力量值增强。4+5=94+5=9。

输入解题思路,AI测评打分。不知道怎么写?

首页