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

using namespace std;

pair<int,int> select(string &str) {
    char prev = '1';
    int longest = 0, length, start, ind1 = -1, ind2 = -1;

    for (int i = 0; i < str.length(); ++i) {
        if ((prev == '2' || prev == '1') && str[i] == '0') {
            start = (i == 0 || prev == '2' ? -1 : i);
            length = 1;
        }
        else if (prev == '0' && str[i] == '1') {
            if (length > longest) {
                longest = length;
                ind1 = start;
                ind2 = i - 1;
            }
        }
        else if (prev == '0' && str[i] == '2') {
            length = 0;
        }
        else if (str[i] == '0'){
            ++length;
        }
        prev = str[i];
    }

    if (str.back() == '0' && length > longest) {
        ind1 = start;
        ind2 = -1;
    }

    return {ind1, ind2};
}

void infect(string &str) {
    char prev = 'x';

    for (int i = 0; i < str.length(); ++i) {
        if (prev == '1' && str[i] == '0') {
            prev = 'x';
            str[i] = '1';
        }
        else if (prev == '0' && str[i] == '1') {
            prev = '1';
            str[i - 1] = '1';
        }
        else {
            prev = str[i];
        }
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    cin >> n;

    vector<string> data(n);

    int num;
    string bitmap;
    for (int i = 0; i < n; ++i) {
        cin >> num >> data[i];
    }

    int ind1, ind2;
    for (string &cities: data) {
        while (true) {
            std::tie(ind1, ind2) = select(cities);
            if (ind1 == -1 && ind2 == -1) {
                cout << std::count(cities.begin(), cities.end(), '1') << '\n';
                break;
            }
            else {
                cities[ind1 == -1 ? ind2 : ind1] = '2';
            }
            infect(cities);
        }
    }

    return 0;
}