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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <unistd.h>
#include <cstdlib>

using namespace std;

int main() {

    int t;
    cin >> t;

    for (size_t i = 0; i < t; ++i)
    {
        int n;
        cin >> n;
        char in[n];
        cin >> in;

        int j = 1;

        while(in[j-1] == '0'){
            ++j;
        }
        if (j>0) {
            int count2 = 0;
            bool b = true;
            do {
                int l = 0;
                while (l < n) {
                    //cout << in[l];
                    if(in[l] == '2') {
                        in[l] = '1';
                        --count2;
                    }
                    ++l;
                }
                //cout << endl;
                l = 0;
                bool first = true;
                int longest = 0;
                int longestPlace = 0;
                char start = ' ';
                int front = 0;
                int back = 0;
                int len = 0;
                while(l < n) {
                    //cout << l << ":" << first << endl;
                    if(in[l] == '1' || l == n-1 || l == 0 || in[l] == '3') {
                        if(first) {
                            start = in[l];
                            l = len;
                            first = false;
                        } else {
                            //cout << start << endl;;
                            int leng = l - len - 1;
                            //cout << leng << endl;;
                            if(leng > longest && l != n-1) {
                                longest = leng;
                                longestPlace = len;
                            }
                            if (len == 0 && in[0] != '1' && in[l] != '3') front = leng+1;
                            if (l == n-1 && start != '3' && in[n-1] != '1') back = leng+1;
                            len = l;
                            start = in[l];
                        }
                    }
                    if(in[l] == '1') {
                        
                        if(l > 0 && in[l-1] == '0') {
                            ++count2;
                            in[l-1] = '2';
                        }
                        if (l < n - 1 && in[l+1] == '0') {
                            ++count2;
                            in[l+1] = '2';
                        }
                    }
                    ++l;
                }
                //cout << back << endl;
                if(back >=2 || front >= 2) {
                    if(back > front) {
                        longestPlace = n - 1 - back;
                        back = 0;
                    } else {
                        longestPlace = 0;
                        front = 0;
                    }

                }
                //cout << longest << ";";
                //cout << longestPlace << endl;
                l = 0;
                // while (l < n) {
                //     cout << in[l];
                //     ++l;
                // }
                // cout << endl;
                bool check = true;
                l = longestPlace;
                while(check && l < n) {
                    //cout << l << endl;;
                    if(in[l] == '2') {
                        if(l > 0 && l < n - 1) {
                            //cout << l << endl;
                            if(((in[l-1] == '0' || in[l-1] == '3') && in[l+1] == '1') || (in[l-1] == '1' && (in[l+1] == '0' || in[l+1] == '3'))) {
                                in[l] = '3';
                                --count2;
                                check = false;
                            } else if ((in[l-1] == '1' || in[l+1] == '1')) {
                                in[l] = '3';
                                --count2;
                                check = false;
                            }
                        } else if(l == 0) {
                            if(in[l] == '2') {
                                in[l] == '3';
                                --count2;
                                check = false;
                            }
                        } else if(l == n-1) {
                            if(in[l] == '2') {
                                in[l] == '3';
                                --count2;
                                check = false;
                            }
                        }
                    }
                    ++l;
                }
            } while(count2 > 0);

            int c = 0;

            int count = 0;

            while(c < n) {
                //cout << in[c];
                if(in[c] == '1') ++count;
                ++c;
            }

            cout << count << endl;
            
        } else {
            cout << '0' << endl;
        }
        

    }
    

    return 0;
}