CF1800C1.Powering the Hero (easy version)

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Powering the Hero (easy version)

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 (1t10001 \le t \le 1000) — the number of test cases in the test.

The first line of each test case contains one integer nn (1n50001 \le n \le 5000) — 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 (0si1090 \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 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.

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.

Samples

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

在线编程 IDE

建议全屏模式获得最佳体验