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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>

using namespace std;

#ifdef GGDEBUG
#define dbg printf
#else 
#define dbg //printf
#endif

int main() {
    int Z;
    scanf("%d", &Z);
    while(Z--) {
        int n;
        scanf("%d", &n);
        char input[110000];
        scanf("%s", input);

        bool all_ok = true;
        for (int i = 0; i < n; ++i) {
            if (input[i] == '1') {
                all_ok = false;
                break;
            }
        }
        if (all_ok) {
            printf("0\n");
            continue;
        }

        int start = 0, end = n;
        std::vector<int> edges;

        if (input[0] == '0') {
            // first edge
            int i = 0;
            while (input[i] == '0') i++;
            edges.push_back(i);
            start = i;
        }

        if (input[n-1] == '0') {
            // first edge
            int i = n-1;
            while (input[i] == '0') i--;
            edges.push_back(n-i-1);
            end = i+1;
        }

        int count_alive = 0;
        std::vector<int> batches;

        for(int i = start; i < end; ++i) {
            if (input[i] == '0') {
                // alive, count it
                count_alive++;
            } else {
                // virus, make new batch
                if (count_alive > 0) {
                    batches.push_back(count_alive);
                    count_alive = 0;
                }
            }
        }
        
        // sort batches
        std::sort(edges.rbegin(), edges.rend());
        std::sort(batches.rbegin(), batches.rend());

#ifdef GGDEBUG
        cout << "edges: ";
        for(auto x: edges) {
            cout << x << " ";
        }
        cout << endl;
        cout << "batches: ";
        for(auto x: batches) {
            cout << x << " ";
        }
        cout << endl;
#endif

        auto calculate = [&](int accumulated_drop){
            // count result without taking edges
            unsigned long long saved = 0;
            int edges_used = 0;
            int batches_used = 0;

            while(true) {
                auto b = batches[batches_used];
                int e = 0;
                if (edges_used < edges.size())
                    e = edges[edges_used];

                auto alive = b - accumulated_drop;
                auto alive_edge = e - accumulated_drop/2;

                if (alive_edge > 0 && alive_edge > alive) {
                    dbg("Take edge %d\n", alive_edge);
                    saved += alive_edge;
                    accumulated_drop += 2;
                    edges_used++;
                    continue;
                }

                if (alive == 1) {
                    dbg("Saved %d\n", 1);
                    saved += 1;
                    accumulated_drop += 2;
                } else if (alive >= 2) {
                    dbg("Saved %d\n", alive-1);
                    saved += alive-1;
                    accumulated_drop += 4;
                } else {
                    break;
                }
                batches_used++;
            }
            dbg("saved: %d\n", saved);
            return saved;
        };

        auto calculate_batch = [&](int accumulated_drop){
            // count result without taking edges
            unsigned long long saved = 0;
            int batches_used = 0;

            while(true) {
                auto b = batches[batches_used];
                auto alive = b - accumulated_drop;

                if (alive == 1) {
                    dbg("Saved %d\n", 1);
                    saved += 1;
                    accumulated_drop += 2;
                } else if (alive >= 2) {
                    dbg("Saved %d\n", alive-1);
                    saved += alive-1;
                    accumulated_drop += 4;
                } else {
                    break;
                }
                batches_used++;
            }
            dbg("saved: %d\n", saved);
            return saved;
        };

        std::function<int(int, int,int)> calculate2 = [&](int accumulated_drop, int edges_used, int batches_used) {
            int saved_edge = 0;
            int saved_batch = 0;
            dbg("(%d %d %d)\n", accumulated_drop, edges_used, batches_used);

            // 1) take edge
            if (edges_used < edges.size()) {
                saved_edge = edges[edges_used] - accumulated_drop/2;
                if ( saved_edge > 0 ) {
                    dbg("take edge: %d\n", saved_edge);
                    saved_edge += calculate2(accumulated_drop + 2, edges_used+1, batches_used);
                }
            }
            // 2) take batch
            if (batches_used < batches.size()) {
                saved_batch = batches[batches_used] - accumulated_drop;
                if ( saved_batch == 1 ) {
                    dbg("take single batch: %d\n", saved_batch);
                    saved_batch += calculate2(accumulated_drop + 4, edges_used, batches_used+1);
                } else if ( saved_batch > 1 ) {
                    dbg("take batch: %d\n", saved_batch-1);
                    saved_batch += -1+calculate2(accumulated_drop + 4, edges_used, batches_used+1);
                }                
            }
            dbg("saved: %d %d\n", saved_edge, saved_batch);
            return max(0, max(saved_edge, saved_batch));
        };

        int without_edges = 0;
        int with_edge_1 = 0;
        int with_edge_12 = 0;

        without_edges = calculate2(0, 0, 0);
        // if (edges.size() == 1) {
        //     with_edge_1 = edges[0] + calculate(2);
        // }
        // if (edges.size() == 2) {
        //     with_edge_12 = edges[0] + edges[1] - 1 + calculate(4);
        // }

        dbg("result: %d %d %d\n", without_edges, with_edge_1, with_edge_12);

        auto maxi = std::max({without_edges, with_edge_1, with_edge_12});
        cout << n-maxi << endl;


        

        // if (count_alive > 0) {
        //     count.push_back(count_alive);
        //     rate.push_back(2);
        //     count_alive = 0;
        // }

        // if (input[0] == '0') {
        //     rate[0] = 1;
        // }
        // if (input[n-1] == '0') {
        //     *rate.rbegin() = 1;
        // }

        // int batches = count.size();

        // // for (auto x: count) {
        // //     if (x == -1) {
        // //         cout << 'x';
        // //     } else {
        // //         cout << x;// << ' ';
        // //     }
        // // }
        // // cout << endl;
        // // for (auto x: rate) {
        // //     cout << x << ' ';
        // // }
        // // cout << endl;
        // // cout << endl;
        // // cout << endl;


        // int cumulative_rate = accumulate(rate.begin(), rate.end(), 0);
        // // printf("cumulative: %d\n", cumulative_rate);

        // bool stopped = false;

        // auto spread = [&]() {
        //     // now spread the disease
        //     stopped = true;
        //     for (int i = 0; i < count.size(); ++i) {
        //         assert(count[i] >= 0);
        //         if (rate[i] > 0 && count[i] > 0) {
        //             stopped = false;
        //             auto rate_now = min(rate[i], count[i]);

        //             count[i] -= rate_now;
        //             // printf("%d:: %d -= %d\n", i, count[i], rate_now);
        //             if (count[i] == 0) {
        //                 rate[i] == 0;
        //             }
        //         }
        //     }
        // };
        // while (!stopped) {
        //     // cout << endl;
        //     // cout << endl;
        //     // cout << endl;
        //     // for (auto x: count) {
        //     //     if (x == -1) {
        //     //         cout << 'x';
        //     //     } else {
        //     //         cout << x;// << ' ';
        //     //     }
        //     // }
        //     // cout << endl;
        //     // for (auto x: rate) {
        //     //     cout << x;// << ' ';
        //     // }
        //     // cout << endl;


            
        //     // find largest one
        //     auto maxi = max_element(count.begin(), count.end());
        //     auto index = maxi - count.begin();
        //     // printf("now max: %d at %d with rate %d\n", *maxi, index, rate[index]);

        //     if (count[index] == 0) {
        //         // nothing else to do
        //         break;
        //     }

        //     dbg("%d %d %d\n", count[0], count[index], count[batches-1]);
        //     dbg("r: %d %d %d\n", rate[0], rate[index], rate[batches-1]);
        //     int edge_index = -1;

        //     if (count[0] > 0 && rate[0] == 1 && count[0] + 1 >= count[index]) {
        //         // prefer first edge
        //         dbg("Prefer first edge!\n");
        //         edge_index = 0;
        //     }

        //     if (count[batches-1] > 0 && rate[batches-1] == 1 && count[batches-1] + 1 >= count[index]) {
        //         // prefer last edge
        //         dbg("Prefer last edge!\n");
        //         edge_index = batches - 1;
        //     }
        //     if (edge_index >= 0) {
        //         index = edge_index;
        //     }

        //     if (rate[index] == 1) {
        //         // edge, will take only one move
        //         saved += count[index];
        //         dbg("Saved edge: %d at %d\n", count[index], index);
        //         count[index] = 0;
        //         rate[index] = 0;
                
        //         spread();
        //         continue;
        //     }

        //     // single town, vaccinate it and that's it
        //     if (count[index] == 1) {
        //         dbg("Saved single: %d\n", count[index]);
        //         saved += 1;
        //         count[index] = 0;
        //         rate[index] = 0;
                
        //         spread();
        //         continue;
        //     }

        //     // two towns, vaccinate one and let second one die
        //     if (count[index] == 2) {
        //         dbg("Saved twins: %d\n", count[index]);
        //         saved += 1;
        //         count[index] = 0;
        //         rate[index] = 0;

        //         spread();
        //         continue;
        //     }


        //     // more than 2 towns in this batch - vaccinate batch
        //     dbg("Saved batch: %d\n", count[index]);
        //     saved += count[index] - 1;
        //     count[index] = 0;
        //     rate[index] = 0;

        //     spread();
        //     spread();
        // }

        // // printf("Result:\n");
        // // cout << result << endl;
        // // cout << "saved: " << saved << endl;
        // cout << n-saved << endl;
    }
    return 0;
}