1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;

priority_queue<int> pq;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--) {
        char ch;
        int n, l=0, czas=0, bez=0;
        bool b=1;
        cin>>n;
        for(int i=1; i<=n; i++) {
            cin>>ch;
            if(ch=='0') l++;
            else {
                if(b) {
                    b=0;
                    pq.push(2*l);
                }
                else {
                    pq.push(l);
                    pq.push(l);
                }
                l=0;
            }
        }
        pq.push(2*l);
        while(!pq.empty()) {
            if(pq.top()-czas<=0) break;
            bez+=pq.top()-czas;
            czas+=2;
            pq.pop();
        }
        cout<<n-(bez+1)/2<<"\n";
        while(!pq.empty()) pq.pop();
    }
}