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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;

int main() {

    cin.tie(0); ios_base::sync_with_stdio(false);
    int t; cin >> t;

    while(t--) {

        int n; cin >> n;
        string inp; cin >> inp;

        vector<int> not_virused;

        int i = 0;
        while(inp[i] == '0') i++;

        int left_zeros = (i != 0) ? i : -1, right_zeros = -1, saved = 0;
        int streak = 0;

        for(; i < inp.size(); i++) {
            if(inp[i] == '0') {
                streak++;
            }
            else {
                not_virused.push_back(streak);
                streak = 0;
            }
        }

        if(streak) right_zeros = streak;

        int day = 0;

        if(right_zeros > left_zeros) swap(left_zeros, right_zeros);

        not_virused.push_back(0);

        sort(not_virused.begin(), not_virused.end(), greater<int>());

        if(left_zeros != -1 && left_zeros > not_virused[0]) {
            saved += left_zeros;
            left_zeros = -1;
            day++;
        }
        if(right_zeros != -1 && right_zeros-1 > not_virused[0]-2) {
            saved += right_zeros-1;
            right_zeros = -1;
            day++;
        }

        for(int i = 0; i < not_virused.size(); i++) {
            int cnt = not_virused[i];
            if(cnt - 2*day < 1) {
                break;
            }
            else if(cnt - 2*day == 1 || cnt - 2*day == 2) {
                saved++;
                day++;
            }
            else if(cnt - 2*day > 2) {
                saved += cnt - 2*day - 1;
                day += 2;
            }
        }

        if(left_zeros > day) {
            saved += left_zeros - day;
            day++;
        }

        if(right_zeros > day) {
            saved += right_zeros - day;
            day++;
        }
        
        cout << n-saved << '\n';
        
    }
    
}