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

using namespace std;

int t, n;
char c;
int zero, zPref, zSuff, expansion, it, zCur;
int savedCities;
vector <int> z;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    zero = 0; zPref = -1; zSuff = -1;
    z.clear();
    for (int i = 0; i < n; i++) {
      cin >> c;
      if (c == '1') {
        if (zPref == -1) {
          zPref = zero;
        }
        else {
          z.push_back(zero);
        }
        zero = 0;
      }
      else {
        zero++;
      }
    }
    if (zPref == -1) {
      cout << "0\n";
      continue;
    }
    zSuff = zero;
    sort(z.begin(), z.end(), greater<int>());

    expansion = 0;
    it = 0;
    savedCities = 0;
    if (zSuff > zPref) swap(zSuff, zPref);

    while (it < z.size() && z[it] - expansion * 2 > 0) {
      zCur = z[it] - expansion * 2;
      if (zPref * 2 >= zCur) {
        savedCities += zPref;
        zPref = -1;
        expansion++;
      } else if (zSuff * 2 >= zCur) {
        savedCities += zSuff;
        zSuff = -1;
        expansion++;
      } else {
        savedCities += max(1, zCur - 1);
        expansion += 2;
        zSuff--;
        zPref--;
        it++;
      }
      zPref--;
      zSuff--;
    }

    if (zPref > 0) {
      savedCities += zPref;
      zSuff--;
    }
    
    if (zSuff > 0) {
      savedCities += zSuff;
    }
    cout << n - savedCities << "\n";
  }
  
}