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

int main() {
	ios_base::sync_with_stdio(0);
	int n, zera, i, l, p, roznica, t, temp;
	string s;
	cin >> t;
	while(t--){
		cin >> n >> s;
		s = '2' + s + '2';
		vector<pair<int,int> > luki;
		i = 1;
		zera = 0;
		while(i <= n && s[i]=='0') {
			zera++;
			i++;
		}
		if(zera == n) {
			cout << 0 << endl;
			continue;
		}
		if (zera > 0) luki.push_back(make_pair(2*zera,1));
		l = i;
		i = n;
		zera = 0;
		while(i > l && s[i]=='0') {
			zera++;
			i--;
		}
		if (zera > 0) luki.push_back(make_pair(2*zera,1));
		p = i;
		zera = 0;
		for(i = l; i <= p; i++) {
			if(s[i] =='0') zera++;
			else {
				if(zera >0) luki.push_back(make_pair(zera,0));
				zera = 0;
			}
		}
		sort(luki.rbegin(),luki.rend());
		//for(i = 0; i < luki.size();i++) cout << luki[i].first <<" "<<luki[i].second << endl;
		zera = 0; roznica = 0;
		for(i = 0; i < luki.size();i++){
			if(luki[i].second == 0) {
				temp = luki[i].first - roznica;
				if(temp < 1) break;
				zera += 1;
				temp -= 2;
				temp = max(temp, 0);
				roznica += 4;
			}
			else{
				temp = luki[i].first - roznica;
				temp /= 2;
				if(temp < 1) break;	
				roznica += 2;				
			}
			zera += temp;
			
			//cout << zera << endl;	
		}
		cout << n - zera << endl;
	}
	
	
	
	return 0;
}