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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>

using namespace std;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    for(int o=0;o<t;o++) {
        int n;
        cin>>n;
        vector <pair<int,bool>> vec;
        int akt = 0, ile = 0, pocz = 0;
        for(int i=0;i<n;i++) {
            char x;
            cin>>x;
            if(x=='1') {
                pocz ++;
                if(ile == 0) {
                    vec.push_back({2*akt,1});
                }
                else {
                    vec.push_back({akt,0});
                }
                ile++;
                akt = 0;
            }
            else {
                akt ++;
            }
        }
        vec.push_back({2*akt,1});
        sort(vec.begin(),vec.end());
        /*for(auto ele : vec) {
            cout<<ele.first<<' '<<ele.second<<'\n';
        }*/
        int wyn = 0;
        int dod = 0;
        for(int i = ile;i>=0;i--) {
            if(dod*2 >= vec[i].first) {
                if(vec[i].second) 
                    wyn += vec[i].first/2;
                else
                    wyn += vec[i].first;
                continue;
            } 
            if(!vec[i].second) {   
                //cout<<"i: "<<i<<'\n';
                if(vec[i].first - dod*2 == 1)
                    wyn += dod*2;
                else
                    wyn += dod*2 + 1;
                dod += 2;
            }
            else {
                /*if(dod >= vec[i].first) {
                    wyn += vec[i].first;
                    continue;
                }*/
                wyn += dod;
                dod += 1;
            }
        }
        cout<<wyn+pocz<<'\n';
    }
}