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
#include<bits/stdc++.h>
using namespace std;
#define all(X) (X).begin(), (X).end()

int32_t main(){
    ios::sync_with_stdio(false);
    int TC;
    cin >> TC;
    for(int TCC=1;TCC<=TC;TCC++) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        if(count(all(s),'1') == 0) {cout<<0<<"\n"; continue;}

        vector<int> tab, es;
        for(int i=0;i<n;i++) {
            if(s[i] == '0') {
                int j = i;
                while(j < n && s[j] == '0') j++;
                if(i == 0 || j == n)
                    es.push_back(j-i);
                else
                    tab.push_back(j-i);
                i = j-1;
            }
        }
        sort(all(tab));
        reverse(all(tab));
        int res = 0;
        for(int c=0;c<(1<<(es.size()));c++) {
            int x = 0, saved = 0;
            for(int j=0;j<(int)es.size();j++) {
                if((c&(1<<j)) && es[j]-x > 0) {
                    saved += es[j]-x;
                    x++;
                }
            }
            for(auto a:tab) {
                if(a-2*x > 0) {
                    saved += max(1,a-2*x-1);
                    x+=2;
                }
            }
            res = max(res,saved);
        }
        cout<<n-res<<"\n";
    }
}