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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int findMaxiumum(vector<int> & vec, vector<bool> & seal)
{
    int maks = -1;
    for(int i = 0; i < vec.size(); i++){
        if(!seal[i] && vec[i] > 0){
            if(maks == -1)
                maks = i;
            else if(vec[maks] <= vec[i])
                maks = i;
        }
    }
    if(maks != -1)
        seal[maks] = true;
    return maks;
}
int main()
{
    int t;
    cin>>t;
    for(int zestaw = 0; zestaw < t; zestaw++){
        int n;
        cin>>n;
        string cities;
        cin>>cities;
        int islands = 0, prev = 0;
        vector<int> gap(1);
        vector<bool> seal;
        int infected = 0;
        for(int i = 0; i < cities.size(); i++){
            if(cities[i] == '1'){
                gap.push_back(0);
                int j = i;
                while(j < cities.size() && cities[j] == '1'){
                    j++;
                    infected++;
                }
                i = j;
            }
            if(i < cities.size() && cities[i] == '0')
                gap[gap.size()-1]++;
        }
        if(infected == 0){
            cout<<0<<endl;
            continue;
        }
        if(infected == n){
            cout<<n<<endl;
            continue;
        }
        if(gap.size() == 1 && cities[0] == '1' && cities[cities.size()-1] == '1' && gap[0] != 0){
            cout<<infected+2<<endl;
            continue;
        }
        seal.assign(gap.size(),0);
        for(int i = 0; ;i++){
            int found = findMaxiumum(gap,seal);
            if(found == -1){
                cout<<infected<<endl;
                break;
            }
            if(!seal[0] && gap[0] > 0){
                infected++;
                gap[0]--;
            }
            if(!seal[gap.size()-1] && gap[gap.size()-1] > 0){
                infected++;
                gap[gap.size()-1]--;
            }
            for(int x = 1; x < gap.size()-1; x++){
                if(x == found){
                    infected += 1;
                }
                else if(!seal[x]){
                    infected += min(2,gap[x]);
                    gap[x] = max(0,gap[x]-2);
                }
            }
        }
        //cout<<"inf "<<infected<<endl;


    }
    return 0;
}