PTSD

시간 제한1초메모리 제한1024 MB

요약
병사 1번부터 n번을 여러 집단으로 나눌 때, 자기 집단에서 두 번째로 강한 PTSD 병사의 힘 값 합이 최대가 되도록 만든다.
난이도

보통10점 중 6점

유형
그리디, 동적 계획법, 구현
정답자
아직 제출이 없습니다

문제

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

There is a tournament in the kingdom now. The soldiers need to be divided into several groups where each soldier belongs to exactly one group. Note that it's allowed for a group to contain only one single soldier. For some unknown reason, some soldiers have a disease called PTSD (post-traumatic stress disorder). The soldiers with PTSD don't like being the ∗∗second∗∗**second** strongest soldier in their groups. Formally speaking, a soldier with PTSD will be upset if there is exactly one other soldier with a larger power value than him in his group.

JB, the king of JB kingdom, wants to maximize the sum of the power values of the soldiers who feel upset because of PTSD. You are asked to help him divide the soldiers.

입력

There are multiple test cases. The first line of the input contains a positive integer TT, indicating the number of test cases. For each test case:

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

The second line contains a string ss of length nn. It's guaranteed that ss only contains '0' and '1'. The ii-th character describes the ii-th soldier: If s_i=s\_i = '1', the ii-th soldier has PTSD. Otherwise, the ii-th soldier doesn't have PTSD.

It's guaranteed that the sum of nn of all test cases doesn't exceed 10610^6.

출력

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

힌트

For the first test case, a valid division is [1, 2], [3, 4], [5], which makes the 11-st soldier and the 33-rd soldier upset. [1, 2], [3, 5], [4] is also valid.

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

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

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

예제1

  1. 예제 1

    입력
    4
    5
    10101
    8
    11111111
    4
    1100
    4
    0110
    
    예상 출력
    4
    16
    3
    3