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

using namespace std;
typedef long long LL;
const int INF = 1e9;

struct V {
    int val;
    bool two;
};

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;

    deque<V> q1;

    int last1 = -1;
    int ones = 0;
    for(int i = 0; i < n; ++i) ones += s[i] == '1';

    int i = 0;
    while (s[i] == '1' && i < n) last1 = i++;
    // virus everywhere
    if(i == n) {
        cout << n << endl;
        return;
    }
    if(i == 0) {
        while(s[i] == '0' && i < n) ++i;
        // no virus
        if(i == n) {
            cout << 0 << endl;
            return;
        }
        q1.push_back({i, false});
        last1 = i++;
    }
    while(i < n) {
        if(s[i-1] == '1' && s[i] == '1') {
            last1 = i++;
        } else if (s[i-1] == '0' && s[i] == '0') {
            i++;
        } else if (s[i-1] == '1' && s[i] == '0') {
            i++;
        } else {
            int x = i-last1-1;
            q1.push_back({x, true});
            last1 = i++;
        }
    }
    if(s[n-1] == '0') {
        q1.push_back({n-last1-1, false});
    }


    sort(q1.begin(), q1.end(), [](V a, V b) {
        int va = a.two ? a.val : a.val*2;
        int vb = b.two ? b.val : b.val*2;
        return va > vb;
    });

    LL res = ones;
    ones = 0;
    int twos = 0;
    for(int i = 0; i < q1.size(); ++i) {
        if(q1[i].two) ++twos;
        else ++ones;
    }
    int step = 0;
    while(!q1.empty()) {
        V top = q1.front();
        q1.pop_front();
        if(top.two) {
            if(top.val-2*step == 1) {
                --twos;
            } else {
                q1.push_front({top.val-step-1, false});
                --twos;
                ++ones;
            }
        } else {
            --ones;
        }
        
        while(!q1.empty()) {
            V b = q1.back();
            if(b.two) {
                if(b.val-2*step <= 1) q1.pop_back(), --twos, ++res;
                else break;
            } else {
                if(b.val-step <= 1) q1.pop_back(), --ones, ++res;
                else break;
            }
        }

        ++step;
        res += twos*2+ones;
        
        while(!q1.empty()) {
            V b = q1.back();
            if(b.two) {
                if(b.val-2*step <= 0) q1.pop_back(), --twos;
                else break;
            } else {
                if(b.val <= step) q1.pop_back(), --ones;
                else break;
            }
        }
        
        
    }
    cout << res << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
}