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

void solve() {
	int n; cin >> n;
	
	string status;
	cin >> status;
	
	int days = 0;
	int saved = 0;
	
	vector<pair<int,int>> spans;
	
	int i = 0;
	int len = 0;
	int no = 0;
	while (i < n) {
		if (status[i] == '0') len++;
		else {
			int l = len;
			int nd = 2;
			
			if (no == 0) {
				nd = 1;
				l = l * 2;
			}
			 
			spans.emplace_back(l, nd);
			len = 0;
			no++;
		}
		i++;
	}
	
	if (no == 0) {
		// nie ma zarażonych miast
		cout << 0 << endl;
		return;
	}
	
	spans.emplace_back(2 * len, 1);
	sort(spans.begin(), spans.end());

	for (int j = spans.size() - 1; j >= 0; j--) {
		int len = spans[j].first;
		int nd = spans[j].second;
		
		if (nd == 1) {
			len /= 2;
			if (len > days) {
				saved += len - days;
				days += 1;
			} else break;
		} else {
			if (len == 2 * days + 1) {
				saved += 1;
				break;
			} else if (len == 2 * days + 2) {
				saved += 1;
				break;
			} else if (len >= 2 * days + 3) {
				saved += len - 2 * days - 1;
				days += 2;
			} else break;
		}
	}
	
	cout << n - saved << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	int t; cin >> t; while (t--) solve();
	
	return 0;
}