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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int t, n, x, s1 = -5, s2 = -5;
string s;
vector <pair <int, int>> v;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> t;

	while (t--)
	{
		cin >> n;
		int ile = 0, sum = 0;
		bool czy = true;
		cin >> s;
		if (s[0] == '1')
			czy = false;

		for (int i = 0; i < n; i++)
		{
			if (s[i] == '1') x = 1;
			else x = 0;
			if (x == 1)
			{
				if (ile > 0)
				{
					if (czy)	
						s1 = ile;
					else		
						v.push_back({ ile-2, -2 });
					czy = false;
					sum += ile;
					ile = 0;
				}
			}
			else if (i == n - 1)
			{
				if (x == 0)	ile++;
				if (ile > 0)
				{
					s2 = ile;
					sum += ile;
					ile = 0;
				}
			}

			else
				ile++;
		}
		if (s1 < s2)
		{
			int y = s1; 
			s1 = s2;
			s2 = y;
		}
		if (sum == n)
			cout << "0\n";
		else if (sum == 0)
			cout << n << endl;
		else if (v.size() == 0)
		{
			long long odp = 0;
				odp += s1;
				s2--;
				if (s2 > 0)
					odp += s2;
				cout << n - odp << endl;
		}
		else
		{
			sort(v.rbegin(), v.rend());
			int turn = 0;
			long long wyn = 0;
			bool back = false;
			for (int j = 0; j < v.size(); j++)
			{
				if(!back)
					v[j].first -= v[j].second;
				//cout << v[j].first << " " << v[j].second*turn << endl;
				if (s1 - turn > 0 && v[j].first + (turn+1) * v[j].second <= s1 - turn)
				{
					wyn += s1 - turn; 
					turn++;
					s1 = -5;
					j--;
					back = true;
				}
				else if (s2 - turn > 0 && v[j].first + (turn+1) * v[j].second <= s2 - turn)
				{
					//cout << "TU?";
					//cout << "??\n";
					wyn += s2 - turn; 
					turn++;
					s2 = -5;
					j--;
					back = true;

				}
				else if (v[j].first + turn*v[j].second == 1)
				{
					wyn += 1;
					turn += 1;
					back = false;
				}
				else if (v[j].first + turn * v[j].second > 0)
				{
					if (v[j].first + turn * v[j].second >= 2)
					{
						wyn += v[j].first + turn * v[j].second - 1;
						turn += 2;
						back = false;
					}
					else if (v[j].first + turn * v[j].second > 0)
					{
						wyn += v[j].first + turn * v[j].second;
						turn += 1;
						back = false;
					}
				}
				else if (v[j].first + turn * v[j].second < 0)
				{
					//cout << "wyn: " << wyn << endl;
					break;
				}
				//cout << "wyn: " << wyn << endl;
			}
			//cout << s1 << " " << s2 << "  " << turn << endl;
			if (s1 != -5 && s1 - turn > 0)
			{
				//cout << "?";
				wyn += s1 - turn; 
				turn++;
			}
			if (s2 != -5 && s2 - turn > 0)
			{
				wyn += s2 - turn; 
			}
			cout << n - wyn << endl;
		}
		v.clear();
		s1 = -5; 
		s2 = -5;
	}
	return 0;
}

//195 linijka  poprawna 190
/*
21
0000100000000100000001100
0X111X00000X1111000111111
00111X00000X11111X1111111

1X0000001110000011110
1X000001111X000111111
1X0000X1111X001111111
1X0000X1111X0X

2

1
1000
0000111111111100011010111111001001100011110000011110101010110011101100011010011000010011000101010100100101111011011010101111011001110100011110110011001110101001111101011100010011100111101100001110111011011010000010110101101000010111110001001101110100010100001000110110010100011010011111110110101110111100111110100011000011011100101011010000010011111111010100110000001001010011111100010000000100100011110101010101000000010001011110000010111010110111110000101010110111110011000111001001011101110100011010000011011110011101010100101001001010111000001011100010000000101000011111101000110111101101111011010100010110001011001111100011010010011010010011111010100101001010011100101101010111101110110011011101011100001111001001010010011100001100100110000100001100101010010010001010010110110101111101100101000111111000001110001111000100001010001100110001001011101111011100101011000000110110000101100010100001111010011100100110000011101100111010001111101011011110000000010111101110010011110000110110011001111100
																																																																																																																																																																																																																																												    												
1000
0000100011101110011110001010000000000000100010111010110011010000000111101110101000111111100100111000110001001100011111001111100000011110010010010101011100110101000110101100001010101011000000101010010100000100110011110000010111010011111100101101100100100101001100011111101100010000110100000111110011000110111101110000000100011000001010010100001001001011011001001100001010010110010110101001101001001000001010011001011011101101011011011010011010101001111000011110111010101000010010100000001100110011001111010011101110110110101111111010010011000111110001010011100001100110010000101001100101011100010101100111101001101010101111110100111110000111001000000111101111001100100101000100000001011101001111100000100101001001000111001011111111100000100000001000100100001110110110100101101011100110110010001110100010101010000010011110100011001000011100100001000110000001001100011101101101000111110100001111110101011001111100110100100011011001101001100010101111111111010100110001111000011000111101000000010010101001



*/