Under construction

Still being built. Things may change

PTSD

Time limit1sMemory limit1024 MB

Summary
Soldiers numbered 1 to n are split into groups so that the sum of power values of PTSD soldiers who are second strongest in their group is maximized.
Level

Medium7 of 10

Topics
Greedy, Dynamic programming
Solved
No attempts yet

Problem

There are nn soldiers in the JB kingdom, numbered 1,2,⋯ ,n1, 2, \cdots, n. The ii-th soldier has power value ii.

A tournament is held in the kingdom. The soldiers must be divided into several groups so that each soldier belongs to exactly one group. A group may contain only one soldier.

Some soldiers have post-traumatic stress disorder (PTSD). A soldier with PTSD is upset if exactly one other soldier in his group has a larger power value than him, that is, he is the second strongest in the group.

JB, the king, wants to maximize the sum of the power values of the upset soldiers. Divide the soldiers to find this maximum.

Input

The first line contains a positive integer TT, the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤1061 \le n \le 10^6), the number of soldiers.

The second line contains a string ss of length nn consisting of '0' and '1'. The ii-th character describes the ii-th soldier. If sis_i is '1', the ii-th soldier has PTSD. Otherwise, the ii-th soldier does not have PTSD.

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

Output

For each test case, output one line containing the maximum sum of the power values of the upset soldiers.

Hint

In the first test case, a valid division is [1, 2], [3, 4], [5]. It makes soldiers 1 and 3 upset. The division [1, 2], [3, 5], [4] is also valid.

In the second test case, a valid division is [1, 2], [3, 4], [5, 6], [7, 8].

In the third test case, a valid division is [1, 3], [2, 4].

In the fourth test case, a valid division is [1, 2, 3, 4].

Examples1

  1. Example 1

    Input
    4
    5
    10101
    8
    11111111
    4
    1100
    4
    0110
    
    Expected output
    4
    16
    3
    3