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
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){
        vector<int> v;
        v.push_back(0);
        v.push_back(0);
        int n,p = 0, k = 0,acc = 0, odp = 0;
        bool czy = 0;
        cin >> n;
        string s;
        cin >> s;
        for(int i = 0; i < n; i++){
            if (s[i] == '1'){
                if (czy == 0) p = acc;
                else v.push_back(acc - 1);
                acc = 0;
                czy = 1;
            }
            else{
                acc++;
                if ( i == n - 1) k = acc;
            }
        }
        sort(v.begin(), v.end(), greater<int>());
        int z = 0;
        int i = 0;

        while(v[i] - z > 0 && i < v.size() - 1){
            odp += v[i] - z;
            z += 2;
            i += 1;
        }
        if(k > p) swap(p,k);
        if (p - z > 0){
            odp += p - z;
            z += 1;
        }
        if (k - z > 0) odp += k - z;
        cout << n - odp <<endl;


    }
    return 0;
}