You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string s_{l}s_{l} + 1s_{l} + 2... s_{r}, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.

You have to determine the length of the longest balanced substring of s.


-----Input-----

The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.

The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.


-----Output-----

If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.


-----Examples-----
Input
8
11010111

Output
4

Input
3
111

Output
0



-----Note-----

In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.

In the second example it's impossible to find a non-empty balanced substring.

Source: codeforces | Difficulty: MEDIUM
Tags: Dynamic programming

Implementation


Solutions:
n = int(input())
s = input()
left = list([-100000] * 200005)
right = list([-100000] * 200005)
cur = 0
left[n] = -1
for x in range(n):
	if s[x] == '1':
		cur += 1
	else:
		cur -= 1
	if left[cur + n] == -100000:
		left[cur + n] = x
	right[cur + n] = x
res = 0
for x in range(0, 2 * n):
	res = max(res, right[x] - left[x])
print(res)


n = int(input())
s = input()
d = {0: -1}
(res, e) = (0, 0)
for (i, x) in enumerate(s):
	if x == '0':
		e -= 1
	else:
		e += 1
	if d.get(e) == None:
		d[e] = i
	else:
		res = max(res, i - d[e])
print(res)


import sys

def findMaxLength(s):
	nums = [int(i) for i in s]
	dic = {}
	dic[0] = -1
	flag = 0
	maxL = 0
	for i in range(len(nums)):
		flag += 1 if nums[i] == 1 else -1
		if flag not in dic:
			dic[flag] = i
		maxL = i - dic[flag] if i - dic[flag] > maxL else maxL
	return maxL
count = 0
for line in sys.stdin:
	if count == 0:
		num = int(line.strip())
		count += 1
		continue
	s = line.strip()
print(findMaxLength(s))


n = int(input())
b = list(input())
suma = list((0 for i in range(n + 1)))
equal = list(([] for i in range(2 * (n + 1))))
for i in range(n):
	b[i] = int(b[i])
	if b[i] == 0:
		b[i] = -1
equal[n + 1] += [0]
for i in range(1, n + 1):
	suma[i] = suma[i - 1] + b[i - 1]
	equal[n + 1 + suma[i]] += [i]
l = 0
for i in range(0, 2 * (n + 1)):
	if len(equal[i]) > 1:
		l = equal[i][-1] - equal[i][0] if equal[i][-1] - equal[i][0] > l else l
print(l)


n = int(input())
l = [-2] * (n * 2 + 1)
(l[n], r) = (-1, 0)
for (i, c) in enumerate(input()):
	n += {'0': -1, '1': 1}[c]
	if l[n] == -2:
		l[n] = i
	else:
		i -= l[n]
		if r < i:
			r = i
print(r)


n = int(input())
s = input()
balance = [2 * int(i) - 1 for i in s]
m = 0
dist = {}
dist[0] = -1
sum = 0
for i in range(n):
	sum += balance[i]
	if sum in dist:
		m = max(m, i - dist[sum])
	else:
		dist[sum] = i
print(m)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	s = ' ' + input().rstrip()
	(ma, z, o, a) = (0, 0, 0, {})
	a[0] = 0
	for i in range(1, n + 1):
		if s[i] == '1':
			o += 1
		else:
			z += 1
		if z - o in a:
			ma = max(ma, i - a[z - o])
		else:
			a[z - o] = i
	print(ma)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


input()
g = {0: -1}
z = s = 0
for (i, c) in enumerate(input()):
	s += 2 * int(c) - 1
	g[s] = p = g.get(s, i)
	z = max(z, i - p)
print(z)


n = int(input())
s = input()
d = {0: 0}
acc = 0
lst = [0]
for x in range(len(s)):
	if s[x] == '0':
		a = -1
	else:
		a = 1
	acc += a
	if not acc in d:
		d[acc] = x + 1
	else:
		lst.append(x - d[acc] + 1)
print(max(lst))


_ = input()
string = input()
lasts = {0: -1}
curr_count = 0
best_best = 0
for (idx, x) in enumerate(string):
	if x == '1':
		curr_count += 1
	else:
		curr_count -= 1
	if curr_count in lasts:
		curr_best = idx - lasts[curr_count]
		if curr_best > best_best:
			best_best = curr_best
	else:
		lasts[curr_count] = idx
print(best_best)


from collections import defaultdict as dis
(n, s) = (int(input()), input())
(sumx, l, d) = (0, 0, dis(list))
d[0].append(-1)
for (i, j) in enumerate(s):
	sumx += {'0': -1, '1': 1}[j]
	if len(d[sumx]) < 2:
		d[sumx].append(i)
	else:
		d[sumx][1] = i
for i in d:
	if len(d[i]) == 2:
		l = max(l, d[i][1] - d[i][0])
print(l)


n = int(input())
a = input()
b = [0]
c = 0
d = 0
e = {}
for i in range(n):
	if a[i] == '1':
		c = c + 1
	if a[i] == '0':
		d = d + 1
	b.append(d - c)
for i in range(n + 1):
	e[b[i]] = []
for i in range(n + 1):
	e[b[i]].append(i)
f = []
for i in e:
	if len(e[i]) > 1:
		f.append(max(e[i]) - min(e[i]))
if len(f) == 0:
	print(0)
else:
	print(max(f))


def getZ(pred):
	return 1 if pred else 0
n = int(str(input()))
s = str(input()).strip()
a = [0 if s[i] == '0' else 1 for i in range(n)]
cnt_0 = [0 for i in range(n)]
cnt_1 = [0 for i in range(n)]
cnt_0[0] = getZ(a[0] == 0)
cnt_1[0] = getZ(a[0] == 1)
balance = dict()
balance[0] = -1
for i in range(1, n):
	cnt_0[i] = getZ(a[i] == 0) + cnt_0[i - 1]
	cnt_1[i] = getZ(a[i] == 1) + cnt_1[i - 1]
rt = 0
for i in range(n):
	diff = cnt_0[i] - cnt_1[i]
	if not diff in balance.keys():
		balance[diff] = i
	rt = max(rt, i - balance[diff])
print(rt)


import io, os
n = int(input())
s = [int(x) for x in list(input())]
l = []
z = 0
o = 0
d = {}
d[0] = -1
for i in range(n):
	if s[i] == 0:
		z += 1
	else:
		o += 1
	l.append(z - o)
ans = 0
for i in range(n):
	if l[i] not in d:
		d[l[i]] = i
	else:
		ans = max(ans, i - d[l[i]])
print(ans)


n = int(input())
zo = input()
dr = [0]
ml = 0
mini = {0: 0}
for i in range(n):
	if zo[i] == '0':
		dr.append(dr[i] - 1)
	else:
		dr.append(dr[i] + 1)
	if not dr[i + 1] in mini:
		mini[dr[i + 1]] = i + 1
	else:
		ml = max(ml, i + 1 - mini[dr[i + 1]])
print(ml)


n = input()
s = input()
res = ans = 0
dp = {}
dp[0] = -1
for i in range(int(n)):
	if s[i] == '1':
		res += 1
	else:
		res -= 1
	if res not in dp:
		dp[res] = i
	else:
		ans = max(ans, i - dp[res])
print(ans)


from itertools import accumulate
n = int(input())
s = input().strip()
a = [0] + list(accumulate((2 * int(d) - 1 for d in list(s))))
m = {}
for i in range(len(a)):
	if a[i] not in m:
		m[a[i]] = i
	a[i] = i - m[a[i]]
print(max(a))


from sys import stdin
stdin.readline()
binary = [int(v) for v in stdin.readline().rstrip()]
(cnt0, cnt1) = ([0] * len(binary), [0] * len(binary))
cnt0[0] = abs(binary[0] - 1)
cnt1[0] = binary[0]
balanced = [0] * len(binary)
balanced[0] = cnt0[0] - cnt1[0]
for i in range(1, len(binary)):
	cnt0[i] = cnt0[i - 1] + (1 if binary[i] == 0 else 0)
	cnt1[i] = cnt1[i - 1] + binary[i]
	balanced[i] = cnt0[i] - cnt1[i]
best = 0
for i in range(len(balanced)):
	if balanced[i] == 0:
		best = max(best, i + 1)
last_occurance = dict()
for i in range(len(balanced) - 1, -1, -1):
	if balanced[i] not in last_occurance:
		last_occurance[balanced[i]] = i
for i in range(len(balanced)):
	if balanced[i] in last_occurance:
		best = max(best, last_occurance[balanced[i]] - i)
print(best)


while True:
	try:

		def solve(a, n):
			id = [-2] * (2 * n + 1)
			id[n] = 0
			(i, sstr, indx) = (1, 0, n)
			while i <= n:
				if a[i] == '1':
					indx += 1
				else:
					indx -= 1
				if id[indx] != -2:
					sstr = max(sstr, i - id[indx])
				else:
					id[indx] = i
				i += 1
			print(sstr)
		if __name__ == '__main__':
			n = int(input())
			s = input()
			a = [0] + list(s)
			solve(a, n)
	except EOFError:
		break


n = int(input())
arr = list(input())
maxx = 0
zeros = 0
ones = 0
for i in range(n):
	if arr[i] == '0':
		zeros += 1
	else:
		ones += 1
if zeros == 0 or ones == 0:
	print('0')
else:
	map = {}
	map[0] = -1
	count = 0
	for i in range(n):
		if arr[i] == '1':
			count += 1
		else:
			count -= 1
		if count in map:
			maxx = max(maxx, i - map[count])
		else:
			map[count] = i
	print(maxx)


n = int(input())
s = input()
lefts = [100000 for i in range(n + 1)]
lefts[0] = -1
rights = [-1000000 for i in range(n + 1)]
current = 0
for i in range(n):
	if s[i] == '0':
		current -= 1
	else:
		current += 1
	if i < lefts[current]:
		lefts[current] = i
	if i > rights[current]:
		rights[current] = i
ans = 0
for i in range(0, n + 1):
	if rights[i] - lefts[i] > ans:
		ans = rights[i] - lefts[i]
print(ans)


n = int(input())
s = input().strip()
seen = {}
seen[0] = -1
ans = sm = 0
for i in range(len(s)):
	if s[i] == '1':
		sm += 1
	else:
		sm -= 1
	if sm in seen:
		ans = max(ans, i - seen[sm])
	else:
		seen[sm] = i
print(ans)


n = int(input())
a = list(map(int, list(input())))
for i in range(0, len(a)):
	if a[i] == 0:
		a[i] = -1
hashmap = {0: -1}
s = 0
maximum = 0
for i in range(0, n):
	s += a[i]
	if s in hashmap:
		maximum = max(maximum, i - hashmap[s])
	else:
		hashmap[s] = i
print(maximum)


from itertools import accumulate
n = int(input())
m = {0: -1}
a = 0
for (i, b) in enumerate(accumulate([2 * int(c) - 1 for c in input()])):
	x = m.get(b)
	if x is None:
		m[b] = i
	else:
		a = max(a, i - x)
print(a)


n = int(input().strip())
s = list(input().strip())
for (i, c) in enumerate(s):
	if c == '0':
		s[i] = -1
	else:
		s[i] = 1
sum = [0 for i in range(n)]
for (i, c) in enumerate(s):
	if i == 0:
		sum[i] = c
	sum[i] = sum[i - 1] + c
dic = {0: -1}
maxl = 0
for i in range(n):
	if sum[i] in dic:
		maxl = max(maxl, i - dic[sum[i]])
	else:
		dic[sum[i]] = i
print(maxl)


lens = int(input())
strs = input()
cha_dict = {0: [0, 0]}
cha = 0
max = 0
for i in range(lens):
	if strs[i] == '1':
		cha = cha + 1
	else:
		cha = cha - 1
	if cha in cha_dict:
		cha_dict[cha][1] = i + 1
	else:
		cha_dict[cha] = [i + 1, i + 1]
	if cha_dict[cha][1] - cha_dict[cha][0] > max:
		max = cha_dict[cha][1] - cha_dict[cha][0]
print(max)


n = int(input())
l = [int(i) for i in input()]
d = {}
for i in range(n):
	if l[i] == 0:
		l[i] = -1
	else:
		l[i] = 1
from collections import defaultdict
d = defaultdict(int)
sm = 0
ans = 0
maxi = 0
d = {}
d[0] = -1
for i in range(n):
	sm += l[i]
	if sm in d:
		maxi = max(maxi, i - d[sm])
	if sm not in d:
		d[sm] = i
print(maxi)
exit()
ans = 0
for i in d:
	c = d[i]
	ans += c * (c - 1) // 2
print(ans)


n = int(input())
s = input()
a = [2 * int(c) - 1 for c in s]
r = {0: -1}
h = 0
ans = 0
for i in range(n):
	h += a[i]
	if h in r:
		ans = max(ans, i - r[h])
	else:
		r[h] = i
print(ans)


import sys
inf = float('inf')
(mod, MOD) = (1000000007, 998244353)
from collections import deque, Counter, OrderedDict, defaultdict

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
mydict = dict()
mydict[0] = -1
sum = 0
ans = 0
for (i, c) in enumerate(input()):
	if c == '1':
		sum += 1
	else:
		sum -= 1
	if sum in mydict:
		ans = max(ans, i - mydict[sum])
	else:
		mydict[sum] = i
print(ans)


n = int(input())
s = input()
d = {}
cnt = 0
for i in range(len(s)):
	if s[i] == '1':
		cnt += 1
	if s[i] == '0':
		cnt -= 1
	d[cnt] = i
ans = cnt = 0
for i in range(len(s)):
	if s[i] == '1':
		cnt += 1
		try:
			ans = max(d[cnt - 1] - i + 1, ans)
		except:
			pass
	if s[i] == '0':
		cnt -= 1
		try:
			ans = max(d[cnt + 1] - i + 1, ans)
		except:
			pass
print(ans)


import re

def check(substr):
	if len(re.findall('0', substr)) == len(re.findall('1', substr)):
		return True
	else:
		return False
n = int(input())
s = input()
longest = 0
balance = 0
dic_bal = {0: -1}
for i in range(0, n):
	if s[i] == '1':
		balance -= 1
	else:
		balance += 1
	if not balance in dic_bal:
		dic_bal[balance] = i
	else:
		longest = max(i - dic_bal[balance], longest)
print(longest)


from itertools import accumulate
n = int(input())
l = list(accumulate(map(lambda c: 2 * int(c) - 1, input())))
m = [None] * (2 * n + 1)
m[n] = -1
a = 0
for (i, b) in enumerate(l):
	if m[n + b] is None:
		m[n + b] = i
	else:
		a = max(a, i - m[n + b])
print(a)


n = int(input())
s = input()
one = 0
zero = 0
d = {}
ans = 0
d[0] = -1
for i in range(len(s)):
	if s[i] == '1':
		one += 1
	else:
		zero += 1
	k = one - zero
	if k not in d:
		d[k] = i
	else:
		ans = max(ans, i - d[k])
print(ans)


n = int(input())
s = input()
o = [0] * n
z = [0] * n
for i in range(n):
	if i == 0:
		if s[i] == '1':
			o[i] = 1
		else:
			z[i] = 1
	elif s[i] == '1':
		o[i] = o[i - 1] + 1
		z[i] = z[i - 1]
	else:
		z[i] = z[i - 1] + 1
		o[i] = o[i - 1]
d = []
for i in range(n):
	d.append(o[i] - z[i])
dd = {}
dd.setdefault(0, -1)
for i in range(n):
	if d[i] not in dd.keys():
		dd.setdefault(d[i], i)
ans = 0
for i in range(n):
	ans = max(ans, i - dd[d[i]])
print(ans)


from sys import stdin, stdout
n = int(stdin.readline())
s = stdin.readline().strip()
cnt = [[0, 0] for i in range(n)]
cnt[0][1] = int(s[0])
cnt[0][0] = 1 - int(s[0])
for i in range(1, n):
	cnt[i][1] += cnt[i - 1][1] + int(s[i])
	cnt[i][0] = i + 1 - cnt[i][1]
cnt.append([0, 0])
ans = 0
for i in range(n):
	value = int(s[i])
	current = i + ans
	while current < n:
		if max(cnt[current][1] - cnt[i - 1][1], cnt[current][0] - cnt[i - 1][0]) != min(cnt[current][1] - cnt[i - 1][1], cnt[current][0] - cnt[i - 1][0]):
			current += max(cnt[current][1] - cnt[i - 1][1], cnt[current][0] - cnt[i - 1][0]) - min(cnt[current][1] - cnt[i - 1][1], cnt[current][0] - cnt[i - 1][0])
		else:
			ans = max(ans, current - i + 1)
			current += 1
stdout.write(str(ans))


from itertools import accumulate
n = int(input())
d = {0: -1}
a = 0
for (i, b) in enumerate(accumulate(map(lambda c: 2 * int(c) - 1, input()))):
	d[b] = x = d.get(b, i)
	a = max(a, i - x)
print(a)


import math
import collections
import bisect
import heapq
import time
import random
n = int(input())
s = input()
ones = [0] * (n + 1)
for i in range(1, n + 1):
	ones[i] = ones[i - 1]
	ones[i] += s[i - 1] == '1'
ones = [2 * x for x in ones]
ones = [(x - i, i) for (i, x) in enumerate(ones)]
ds = collections.defaultdict(list)
for (d, i) in ones:
	ds[d].append(i)
ans = 0
for (k, v) in ds.items():
	ans = max(ans, max(v) - min(v))
print(ans)


input()
d = {0: -1}
a = b = 0
for (i, c) in enumerate(input()):
	b += 2 * int(c) - 1
	d[b] = x = d.get(b, i)
	a = max(a, i - x)
print(a)


def balanced(word):
	length = 0
	difference = 0
	first_index = {0: -1}
	for (index, num) in enumerate(word):
		if num == '1':
			difference += 1
		elif num == '0':
			difference -= 1
		if difference in first_index:
			length = max(length, index - first_index[difference])
		else:
			first_index[difference] = index
	return length
n = input()
print(balanced(input()))


def main():
	n = int(input())
	s = input()
	stats = {0: -1}
	prv = 0
	res = 0
	for i in range(len(s)):
		c = s[i]
		nxt = prv + 1 if c == '1' else prv - 1
		if nxt in stats:
			res = max(res, i - stats[nxt])
		else:
			stats[nxt] = i
		prv = nxt
	print(res)
main()


n = int(input())
strs = input()
counta = []
countb = []
no1 = 0
no0 = 0
i = 0
while i < len(strs):
	if strs[i] == '1':
		no1 += 1
	else:
		no0 += 1
	counta.append(no1)
	countb.append(no0)
	i += 1
hashd = dict()
hashd[0] = [-1]
i = 0
maxm = 0
while i < len(strs):
	if counta[i] - countb[i] in hashd:
		hashd[counta[i] - countb[i]].append(i)
		if i - hashd[counta[i] - countb[i]][0] > maxm:
			maxm = i - hashd[counta[i] - countb[i]][0]
	else:
		hashd[counta[i] - countb[i]] = [i]
	i += 1
print(maxm)


from collections import defaultdict
n = int(input())
s = [int(i) for i in input().strip()]
balance = 0
ans = 0
seen = defaultdict(lambda : -2)
seen[0] = -1
for (j, i) in enumerate(s):
	if i:
		balance += 1
	else:
		balance -= 1
	if seen[balance] == -2:
		seen[balance] = j
	ans = max(ans, j - seen[balance])
print(ans)


n = int(input())
res = []
s = input()
heights = {0: [-1]}
current = 0
for i in range(n):
	current += (-1) ** int(s[i])
	if current not in heights:
		heights[current] = [i]
	else:
		heights[current].append(i)
for i in heights.values():
	res.append(max(i) - min(i))
print(max(res))


n = input()
s = input()
max_ = 0
A = {}
mid = 0
for i in range(len(s)):
	if s[i] == '0':
		mid -= 1
		A[mid + 1] = min(A.get(mid + 1, float('inf')), i)
	else:
		mid += 1
		A[mid - 1] = min(A.get(mid - 1, float('inf')), i)
	if mid in A:
		max_ = max(max_, i - A[mid] + 1)
print(max_)


import bisect
from itertools import accumulate, count
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
from sys import maxsize
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return sorted(fac)

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
n = int(input())
s = input()
dic = {}
dic[0] = -1
count1 = 0
count0 = 0
ans = 0
for i in range(0, len(s)):
	if s[i] == '1':
		count1 += 1
	else:
		count0 += 1
	if count1 - count0 in dic:
		ans = max(ans, i - dic[count1 - count0])
	else:
		dic[count1 - count0] = i
print(ans)


n = int(input())
dic = {0: -1}
balance = 0
length = 0
for (i, x) in enumerate(input()):
	if x == '1':
		balance += 1
	else:
		balance -= 1
	if balance in dic:
		length = max(length, i - dic[balance])
	else:
		dic[balance] = i
print(length)


import math as mt
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
d = defaultdict(int)
e = defaultdict(int)
s = input().strip()
x = [0]
for i in range(n):
	w = int(s[i])
	if w == 0:
		w -= 1
	x.append(x[-1] + w)
for i in range(n + 1):
	if d[x[i]] == 0 and x[i] != 0:
		d[x[i]] = i
m = 0
for j in range(n, -1, -1):
	if e[x[j]] == 0:
		e[x[j]] = j
		if d[x[j]] or x[j] == 0:
			m = max(m, e[x[j]] - d[x[j]])
print(m)


import sys
n = int(sys.stdin.readline().strip('\n'))
num = sys.stdin.readline().strip('\n')

def lengest01SubStr(s):
	count = [0, 0]
	B = [0] * len(s)
	dic = {}
	lengest = 0
	for i in range(len(s)):
		count[int(s[i])] += 1
		B[i] = count[0] - count[1]
		if B[i] == 0:
			lengest = i + 1
			continue
		if B[i] in dic:
			lengest = max(lengest, i - dic[B[i]])
		else:
			dic[B[i]] = i
	return lengest
print(lengest01SubStr(num))


n = int(input())
x = input()

def get_max(s):
	net_count = 0
	current_max = 0
	mp = {0: -1}
	for (i, item) in enumerate(s):
		if item == '0':
			net_count -= 1
		elif item == '1':
			net_count += 1
		if mp.get(net_count, None) == None:
			mp[net_count] = i
		else:
			dist = i - mp[net_count]
			if dist > current_max:
				current_max = dist
	return current_max
print(get_max(x))


inp = lambda : map(int, input().split())
n = int(input())
s = input()
a0 = [0] * (10 ** 5 + 1)
a1 = [0] * (10 ** 5 + 1)
if s[0] == '0':
	a0[0] = 1
else:
	a1[0] = 1
for i in range(1, n):
	if s[i] == '0':
		a0[i] = a0[i - 1] + 1
		a1[i] = a1[i - 1]
	else:
		a0[i] = a0[i - 1]
		a1[i] = a1[i - 1] + 1
lab = [-2] * (2 * 10 ** 5 + 1)
m = [-1] * (2 * 10 ** 5 + 1)
lab[10 ** 5] = -1
for i in range(n):
	if lab[a0[i] - a1[i] + 10 ** 5] >= -1:
		m[a0[i] - a1[i] + 10 ** 5] = max(m[a0[i] - a1[i] + 10 ** 5], i - lab[a0[i] - a1[i] + 10 ** 5])
	else:
		lab[a0[i] - a1[i] + 10 ** 5] = i
t = -1
for i in range(2 * 10 ** 5 + 1):
	t = max(t, m[i])
print(max(t, 0))


n = int(input())
line = input()
first = [-2] * (n * 2 + 1)
last = [0] * (n * 2 + 1)
sm = 0
ans = 0
first[n] = -1
for i in range(n):
	elem = line[i]
	if elem == '0':
		sm -= 1
	else:
		sm += 1
	if first[sm + n] == -2:
		first[sm + n] = i
	else:
		last[sm + n] = i
		ans = max(ans, last[sm + n] - first[sm + n])
print(ans)


n = int(input())
d = {0: -1}
a = b = 0
for (i, c) in enumerate(input()):
	if int(c) == 0:
		b -= 1
	else:
		b += 1
	d[b] = x = d.get(b, i)
	a = max(a, i - x)
print(a)


n = int(input())
heights = {0: (0, 0)}
ch = 0
count = 0
for i in input():
	count += 1
	if i == '1':
		ch += 1
	else:
		ch -= 1
	if ch not in heights:
		heights[ch] = (count, count)
	heights[ch] = (heights[ch][0], count)
ml = 0
for i in heights:
	delta = heights[i][1] - heights[i][0]
	ml = max(ml, delta)
print(ml)


n = int(input())
s = input()
a = []
for i in range(n):
	if s[i] == '1':
		a.append(1)
	else:
		a.append(-1)
d = {}
d[0] = -1
d[a[0]] = 0
k = 0
for i in range(1, n):
	a[i] += a[i - 1]
	if a[i] not in d:
		d[a[i]] = i
	else:
		k = max(k, i - d[a[i]])
print(k)


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
n = int(input())
a = input()
dp = {0: -1}
s = 0
ans = 0
for i in range(n):
	if a[i] == '1':
		s += 1
	else:
		s -= 1
	if s in dp:
		ans = max(ans, i - dp[s])
	else:
		dp[s] = i
print(ans)


n = int(input())
a = list(input())
d = {0: [-1]}
r = 0
for i in range(n):
	if a[i] == '1':
		r += 1
	else:
		r -= 1
	d[r] = d.get(r, []) + [i]
ans = 0
for i in d.keys():
	ans = max(d[i][-1] - d[i][0], ans)
print(ans)


n = int(input())
string = input()
diff = [0] * len(string)
diff[0] = 1
if string[0] == '0':
	diff[0] = -1
minIndex = {}
minIndex[0] = 0
minIndex[diff[0]] = 1
for i in range(1, len(string)):
	if string[i] == '0':
		diff[i] = diff[i - 1] - 1
	else:
		diff[i] = diff[i - 1] + 1
	if not diff[i] in minIndex:
		minIndex[diff[i]] = i + 1
ans = 0
for i in range(len(string)):
	ans = max(ans, i - minIndex[diff[i]] + 1)
print(ans)


n = int(input())
L = input()
maxi = 0
s = 0
d = {0: -1}
for i in range(len(L)):
	if L[i] == '1':
		s += 1
	else:
		s -= 1
	if s not in d:
		d[s] = i
	else:
		long = i - d[s]
		if long > maxi:
			maxi = long
print(maxi)


import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
s = input()[:-1]
(zero, one) = (0, 0)
d = defaultdict(int)
d[0] = -1
ans = 0
for i in range(n):
	if s[i] == '0':
		zero += 1
	else:
		one += 1
	if zero - one in d:
		ans = max(ans, i - d[zero - one])
	else:
		d[zero - one] = i
print(ans)


input()
s = input()

def solution(s):
	s = list(s)
	l = len(s)
	m = {}
	r = 0
	sum = 0
	for x in range(l):
		s[x] = 1 if s[x] == '1' else -1
	for x in range(l):
		sum += s[x]
		if sum in m:
			r = max(r, x - m[sum])
		else:
			m[sum] = x
		if sum == 0:
			r = max(r, x + 1)
	return r
print(solution(s))


n = int(input())
l = list(input())
bz = [n + 1] * (n + 1)
bo = [n + 1] * (n + 1)
b = 0
bo[0] = 0
bz[0] = 0
ans = 0
for i in range(n):
	if l[i] == '1':
		b += 1
	else:
		b -= 1
	if b < 0:
		bz[abs(b)] = min(i + 1, bz[abs(b)])
		ans = max(i + 1 - bz[abs(b)], ans)
	else:
		bo[b] = min(i + 1, bo[b])
		ans = max(i + 1 - bo[b], ans)
print(ans)


n = int(input())
s = input()
a = [2 * int(c) - 1 for c in s]
d = {0: -1}
(h, ans) = (0, 0)
for t in range(n):
	h += a[t]
	if h in d:
		ans = max(ans, t - d[h])
	else:
		d[h] = t
print(ans)


from collections import defaultdict
d = defaultdict(list)
n = int(input())
s = input()
counter = 0
d[0].append(-1)
for (pos, c) in enumerate(s):
	if c == '1':
		counter += 1
	else:
		counter -= 1
	d[counter].append(pos)
ans = 0
for (k, v) in d.items():
	if v[-1] - v[0] > ans:
		ans = v[-1] - v[0]
print(ans)


n = int(input())
s = input()
d = {}
d[0] = 0
t = 0
ans = 0
for i in range(1, n + 1):
	if s[i - 1] == '0':
		t -= 1
	else:
		t += 1
	if t in d:
		ans = max(ans, i - d[t])
	else:
		d[t] = i
print(ans)


n = int(input())
s = input()
a = [2 * int(i) - 1 for i in s]
ans = 0
dic = {}
dic[0] = -1
h = 0
for t in range(n):
	h += a[t]
	if h in dic:
		ans = max(ans, t - dic[h])
	else:
		dic[h] = t
print(ans)


n = int(input())
s = input()
dic = dict()
dic[0] = -1
res = 0
mx = 0
for i in range(len(s)):
	if s[i] == '0':
		res += 1
	else:
		res -= 1
	if res not in dic:
		dic[res] = i
	else:
		mx = max(mx, i - dic[res])
print(mx)


n = int(input())
s = input()
a = list(s)
for i in range(n):
	if a[i] == '0':
		a[i] = -1
	else:
		a[i] = 1
sum = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	sum[i] = sum[i - 1] + a[i - 1]
ans = 0
index = {}
for i in range(n):
	if sum[i + 1] in index:
		ans = max(ans, i + 1 - index[sum[i + 1]])
	if sum[i] not in index:
		index[sum[i]] = i
print(ans)


n = int(input())
s = input()
from collections import defaultdict
hash = {0: -1}
b = 0
a = 0
for (j, i) in enumerate(s):
	b += 2 * int(i) - 1
	if b in hash:
		a = max(a, j - hash[b])
	else:
		hash[b] = j
print(a)


n = int(input())
s = [1 if i == '1' else -1 for i in input()]
(ml, ss) = (0, 0)
dic = {}
for i in range(0, n):
	ss += s[i]
	if ss == 0:
		ml = max(ml, i + 1)
	if ss in dic:
		ml = max(ml, i - dic[ss])
	else:
		dic[ss] = i
print(ml)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	l = input()
	sumsofar = 0
	d = dict()
	d[0] = -1
	ans = 0
	for (j, i) in enumerate(l):
		if i == '1':
			sumsofar += 1
		else:
			sumsofar -= 1
		if sumsofar not in d:
			d[sumsofar] = j
		else:
			ans = max(ans, j - d[sumsofar])
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
s = input()
l = []
for i in s:
	if i == '1':
		l.append(1)
	else:
		l.append(-1)
dp = [0 for i in range(n)]
dp[0] = l[0]
for i in range(1, len(l)):
	dp[i] = dp[i - 1] + l[i]
d = {}
maxlen = 0
for i in range(len(dp)):
	if dp[i] == 0:
		maxlen = max(maxlen, i + 1)
	elif dp[i] not in d:
		d[dp[i]] = i
	else:
		maxlen = max(maxlen, i - d[dp[i]])
print(maxlen)


(n, s) = (int(input()), input())
(d, o, z) = (dict([((0, 0), -1)]), 0, 0)
ans = 0
for i in range(len(s)):
	o += int(s[i] == '1')
	z += int(s[i] == '0')
	tp = (o - min(o, z), z - min(o, z))
	if tp in d.keys():
		ans = max(ans, i - d[tp])
	else:
		d[tp] = i
print(ans)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
lsi = lambda : [int(i) for i in ls()]
let = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(1):
	d = dict()
	n = ii()
	l = lsi()
	tot = 0
	d[0] = -1
	ans = 0
	for i in range(n):
		tot += 1 if l[i] == 1 else -1
		if not tot in d:
			d[tot] = i
		else:
			ans = max(ans, i - d[tot])
	print(ans)


n = int(input())
s = input()
a = [2 * int(x) - 1 for x in s]
d = {0: -1}
(h, ans) = (0, 0)
for i in range(n):
	h += a[i]
	if h in d:
		ans = max(ans, i - d[h])
	else:
		d[h] = i
print(ans)


from collections import defaultdict
N = int(input())
S = input()
List = [0]
count = 0
for i in range(N):
	if S[i] == '0':
		List.append(count - 1)
		count -= 1
	else:
		List.append(count + 1)
		count += 1
Dict = defaultdict(list)
for i in range(N + 1):
	Dict[List[i]].append(i)
Ans = 0
for i in Dict.keys():
	Ans = max(Ans, max(Dict[i]) - min(Dict[i]))
print(Ans)


from functools import lru_cache
from sys import stdin
one = '1'

def precount_ones(s):
	one_counts = [0] * len(s)
	for (i, c) in enumerate(s):
		one_counts[i] = int(c == one)
		if i > 0:
			one_counts[i] += one_counts[i - 1]
	one_counts.append(0)
	return tuple(one_counts)

def is_balanced(l, r, one_counts):
	length = r - l + 1
	if length % 2 != 0:
		return False
	n_ones = one_counts[r] - one_counts[l - 1]
	return n_ones == length / 2

def longest_balanced_len_my(l, r, one_counts):
	length = r - l + 1
	if length == 2:
		if is_balanced(l, r, one_counts):
			return length
		else:
			return 0
	n_ones = one_counts[r] - one_counts[l - 1]
	if n_ones == 0 or n_ones == length:
		return 0
	if is_balanced(l, r, one_counts):
		return length
	res = 0
	for l1 in range(l + 1, r):
		if r - l1 + 1 <= res:
			continue
		for r1 in range(r - 1, l1, -1):
			if r1 - l1 + 1 > res:
				if is_balanced(l1, r1, one_counts):
					res = r1 - l1 + 1
	return res

def longest_balanced_len(l, r, one_counts):
	return longest_balanced_len_tut(l, r, one_counts)

def longest_balanced_len_tut(l, r, one_counts):
	balance_to_imin = {0: -1}
	res = 0
	balances = []
	for i in range(l, r + 1):
		length = i - l + 1
		n_ones = one_counts[i] - one_counts[l - 1]
		b = n_ones - (length - n_ones)
		if b not in balance_to_imin:
			balance_to_imin[b] = i
		balances.append(b)
		longest = i - balance_to_imin[b]
		if longest > res:
			res = longest
	return res

def main():
	n = int(stdin.readline())
	s = stdin.readline()[:n]
	print(longest_balanced_len(0, len(s) - 1, precount_ones(s)))

def test():
	s = '011010101110111101'
	print(precount_ones(s))
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 8
	print('passed {}'.format(s))
	s = '11010111'
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 4
	print('passed {}'.format(s))
	s = '111'
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 0
	print('passed {}'.format(s))
	s = 3456 * '0'
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 0
	print('passed {}'.format(s))
	s = 3456 * '1'
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 0
	print('passed {}'.format(s))
	s = '00001000100'
	print(precount_ones(s))
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 2
	print('passed {}'.format(s))
	s = '0' * 9999 + '1'
	print(precount_ones(s))
	assert longest_balanced_len(0, len(s) - 1, precount_ones(s)) == 2
	print('passed {}'.format(s))
main()


n = int(input())
s = input()
a = [-2 for i in range(n + 1)]
count = 0
max1 = 0
a[0] = -1
for i in range(n):
	if s[i] == '0':
		count -= 1
	else:
		count += 1
	if a[count] == -2:
		a[count] = i
	else:
		max1 = max(max1, i - a[count])
print(max1)


from bisect import bisect, bisect_left

def linp():
	return list(map(int, input().split(' ')))

def slinp():
	return sorted(linp())

def inp():
	return int(input())

def fnp():
	return float(input())

def dinp():
	s = linp()
	return (s[0], s[1])

def tinp():
	s = linp()
	return (s[0], s[1], s[2])

def main():
	n = inp()
	s = input()
	dc = dict()
	z = 0
	o = 0
	ans = 0
	dc[0] = -1
	for i in range(n):
		if s[i] == '1':
			o += 1
		else:
			z += 1
		df = o - z
		if df in dc.keys():
			ans = max(ans, i - dc[df])
		else:
			dc[df] = i
	print(ans)
t = 1
for i in range(t):
	main()


n = int(input())
s = input()
max_subs = 0
num0 = 0
num1 = 0
diffs = {0: 0}
for (i, x) in enumerate(s):
	if x == '0':
		num0 += 1
	else:
		num1 += 1
	diff = num0 - num1
	if diff in diffs:
		subs = i - diffs[diff] + 1
		if subs > max_subs:
			max_subs = subs
	else:
		diffs[diff] = i + 1
print(max_subs)


from collections import defaultdict
n = int(input())
s = input()
l = [0] * (n + 1)
d = defaultdict(list)
c = 0
for i in range(n):
	if s[i] == '0':
		c -= 1
	else:
		c += 1
	l[i + 1] = c
for i in range(len(l)):
	d[l[i]].append(i)
cs = 0
for i in d:
	xs = d[i]
	if len(xs) != 1:
		a = xs[0]
		b = xs[len(xs) - 1]
		ds = b - a
		cs = max(cs, ds)
print(cs)


import sys
import itertools

def main():
	n = int(input())
	s = input()
	ar = [1 if ch == '1' else -1 for ch in s]
	res = 0
	first = {}
	first[0] = -1
	for (i, v) in enumerate(list(itertools.accumulate(ar))):
		if v in first:
			res = max(res, i - first[v])
		else:
			first[v] = i
	print(res)
main()


n = int(input())
s = input()
rec = dict()
sum = 0
res = 0
rec[0] = 0
for i in range(n):
	if s[i] == '0':
		sum -= 1
	else:
		sum += 1
	if sum in rec:
		res = max(res, i + 1 - rec[sum])
	else:
		rec[sum] = i + 1
print(res)


n = int(input())
s = input()
visited = [[] for i in range(2 * n + 1)]
count = 0
visited[count + n].append(0)
for i in range(n):
	if s[i] == '1':
		count += 1
	else:
		count -= 1
	visited[count + n].append(i + 1)
ans = 0
for i in range(2 * n + 1):
	if len(visited[i]) > 1:
		visited[i].sort()
		ans = max(ans, visited[i][-1] - visited[i][0])
print(ans)


Ln = int(input())
Str = input()
diff = [0] * Ln
diff[0] = -1
if Str[0] == '1':
	diff[0] = 1
minIndex = {0: 0}
minIndex[diff[0]] = 1
for i in range(1, Ln):
	if Str[i] == '1':
		diff[i] = diff[i - 1] + 1
	else:
		diff[i] = diff[i - 1] - 1
	if not diff[i] in minIndex:
		minIndex[diff[i]] = i + 1
maxLen = 0
for i in range(Ln):
	maxLen = max(i - minIndex[diff[i]] + 1, maxLen)
print(maxLen)


n = int(input())
x = str(input())
(count0, count1, ans, mi, ma) = (0, 0, 0, [-2] * 200001, [0] * 200001)
mi[100000] = -1
for i in range(len(x)):
	if x[i] == '0':
		count0 += 1
	else:
		count1 += 1
	if mi[count1 - count0 + 100000] == -2:
		mi[count1 - count0 + 100000] = i
	else:
		ma[count1 - count0 + 100000] = max(ma[count1 - count0 + 100000], i)
for i in range(200000):
	if ans < ma[i] - mi[i] and ma[i] != 0:
		ans = ma[i] - mi[i]
print(ans)


import traceback
import math
from collections import defaultdict
from functools import lru_cache

def main():
	N = int(input())
	nums = input()
	dp = [0] * N
	tot = 0
	for i in range(N):
		tot += 1 if nums[i] == '1' else -1
		dp[i] = tot
	loc = {}
	loc[0] = -1
	ans = 0
	for i in range(N):
		loc.setdefault(dp[i], i)
		ans = max(ans, i - loc[dp[i]])
	return ans
try:
	ans = main()
	print(ans)
except Exception as e:
	traceback.print_exc()


def process(list_):
	raw = 0
	res = 0
	dict_ = {}
	dict_[0] = -1
	for (index, num) in enumerate(list_):
		if num == '1':
			raw += 1
		else:
			raw -= 1
		if int(raw) in dict_:
			cur_len = index - dict_[int(raw)]
			res = res if res > cur_len else cur_len
		else:
			dict_[raw] = index
	return res
n = input().strip()
str_ = input().strip()
res = process(str_)
print(res)


n = int(input())
s = input()
sm = ans = 0
hmap = {0: -1}
for i in range(n):
	if s[i] == '0':
		sm -= 1
	else:
		sm += 1
	if sm not in hmap:
		hmap[sm] = i
	else:
		ans = max(ans, i - hmap[sm])
print(ans)


input()
s = input()
cs = [0] * (1 + len(s))
i = 0
S = len(s)
while i < S:
	if s[i] == '1':
		cs[i + 1] = cs[i] + 1
	else:
		cs[i + 1] = cs[i] - 1
	i += 1
xyu = {}
i = 0
while i < S + 1:
	if cs[i] not in xyu:
		xyu[cs[i]] = [i]
	else:
		xyu[cs[i]] += [i]
	i += 1
cnt = 0
for i in xyu.values():
	piska = i[0]
	siska = i[-1]
	cnt = max(cnt, siska - piska)
print(cnt)


def func(lista):
	tam = 0
	difer = 0
	first = {0: -1}
	for (index, num) in enumerate(lista):
		if num == '0':
			difer += 1
		else:
			difer += -1
		if difer in first:
			tam = max(tam, index - first[difer])
		else:
			first[difer] = index
	return tam
n = input()
print(func(input()))


n = int(input())
l = list(input())
l = list((-1 if val == '0' else int(val) for (idx, val) in enumerate(l)))
d = {0: -1}
max_len = h = 0
for _ in range(n):
	h += l[_]
	if h in d:
		max_len = max(max_len, _ - d[h])
	else:
		d[h] = _
print(max_len)


dp = {}
input()
sum = 0
ans = 0
dp[0] = -1
for (i, c) in enumerate(input()):
	if c == '0':
		sum += 1
	else:
		sum -= 1
	if sum in dp:
		ans = max(ans, i - dp[sum])
	else:
		dp[sum] = i
print(ans)


def f(a):
	a = list(a)
	for i in range(len(a)):
		if a[i] == '0':
			a[i] = -1
		else:
			a[i] = 1
	a = [None] + a
	dc = {0: 0}
	pref = 0
	ans = 0
	for i in range(1, len(a)):
		pref += a[i]
		if dc.get(pref, None) != None:
			ans = max(i - dc.get(pref), ans)
		dc[pref] = min(dc.get(pref, float('inf')), i)
	return ans
s = input()
s = input()
print(f(s))


n = int(input())
lis = list(input())
ind = [0] * n
pp = [0] * (n + 1)
pos = [[-1, -1] for i in range(n + 1)]
neg = [[-1, -1] for i in range(n + 1)]
for i in range(1, n + 1):
	if lis[i - 1] == '0':
		pp[i] = pp[i - 1] - 1
	else:
		pp[i] = pp[i - 1] + 1
pp = pp
for i in range(n + 1):
	if pp[i] >= 0:
		pos[pp[i]][0] = max(i, pos[pp[i]][0])
		if pos[pp[i]][1] == -1:
			pos[pp[i]][1] = i
	else:
		neg[abs(pp[i])][0] = max(i, neg[abs(pp[i])][0])
		if neg[abs(pp[i])][1] == -1:
			neg[abs(pp[i])][1] = i
ans = 0
for i in range(n + 1):
	if pos[i][1] >= 0 and pos[i][0] >= 0:
		ans = max(ans, pos[i][0] - pos[i][1])
	if neg[i][1] >= 0 and neg[i][0] >= 0:
		ans = max(ans, neg[i][0] - neg[i][1])
print(ans)


l = int(input())
d = input()
sums = []
dp = [-1] * (l + 1)
dp[0] = 0
sums.append(0)
mlen = 0
for i in range(1, l + 1):
	sums.append(sums[-1] + 2 * int(d[i - 1]) - 1)
	if dp[sums[-1]] == -1:
		dp[sums[-1]] = i
	else:
		mlen = max(mlen, i - dp[sums[-1]])
print(mlen)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

def array():
	return list(map(int, sys.stdin.readline().split()))

def intt():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(input())
from itertools import permutations as pmt
from collections import Counter
from collections import Counter
from collections import deque
n = int(input())
s = input()
var = 0
d = {}
for i in range(n):
	if s[i] == '0':
		var -= 1
	else:
		var += 1
	if var in d:
		d[var].append(i)
	else:
		d[var] = [i]
cnt = 0
ans = 0
for i in range(n):
	if cnt in d:
		val = max(d[cnt])
		ans = max(ans, val - i + 1)
	if s[i] == '0':
		cnt -= 1
	else:
		cnt += 1
print(ans)


n = int(input())
s = input()
bal = n
firstseen = [-1] * (n * 2 + 1)
firstseen[n] = 0
ans = 0
for i in range(1, n + 1):
	if s[i - 1] == '1':
		bal += 1
	else:
		bal -= 1
	if firstseen[bal] != -1:
		ans = max(ans, i - firstseen[bal])
	else:
		firstseen[bal] = i
print(ans)


import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
balance_max_pos = {}
balance = 0
for (i, c) in enumerate(s):
	if c == '1':
		balance += 1
	else:
		balance -= 1
	balance_max_pos[balance] = i
ans = 0
balance = 0
if balance in balance_max_pos:
	ans = max(balance_max_pos[balance] + 1, ans)
for (i, c) in enumerate(s):
	if c == '1':
		balance += 1
	else:
		balance -= 1
	if balance in balance_max_pos:
		ans = max(balance_max_pos[balance] - i, ans)
print(ans)


n = int(input())
s = input()
d = {0: 0}
cb = 0
ans = 0
for i in range(n):
	if s[i] == '1':
		cb += 1
	else:
		cb -= 1
	if cb not in d:
		d[cb] = i + 1
	else:
		ans = max(ans, i + 1 - d[cb])
print(ans)


from collections import defaultdict
dd = defaultdict(int)
n = int(input())
s = input()
v = set()
cnt = 0
v.add(0)
dd[0] = 0
ans = 0
for i in range(1, n + 1):
	if s[i - 1] == '0':
		cnt -= 1
	else:
		cnt += 1
	if not cnt in v:
		dd[cnt] = i
		v.add(cnt)
	else:
		ans = max(ans, i - dd[cnt])
print(ans)


n = int(input())
d = {0: -1}
s = 0
mx = 0
ch = input()
for i in range(len(ch)):
	if ch[i] == '1':
		s += 1
	else:
		s += -1
	if s not in d:
		d[s] = i
	else:
		mx = max(i - d[s], mx)
print(mx)


input()
instring = input()
balance = [0]
num0 = 0
num1 = 0
for c in instring:
	if c == '0':
		num0 += 1
	else:
		num1 += 1
	balance.append(num0 - num1)
d = {}
for i in range(len(balance)):
	if balance[i] not in d:
		d[balance[i]] = i
ans = 0
for i in range(len(balance)):
	ans = max(ans, i - d[balance[i]])
print(ans)


from sys import stdin, stdout

def INI():
	return int(stdin.readline())

def INL():
	return [int(_) for _ in stdin.readline().split()]

def INS():
	return stdin.readline()

def MOD():
	return pow(10, 9) + 7

def OPS(ans):
	stdout.write(str(ans) + '\n')

def OPL(ans):
	[stdout.write(str(_) + ' ') for _ in ans]
	stdout.write('\n')
n = INI()
S = INS()
D = dict()
x = 0
ans = float('-inf')
D[0] = [-1, -1]
for _ in range(n):
	if S[_] == '0':
		x -= 1
	else:
		x += 1
	if x not in D:
		D[x] = [_, _]
	else:
		D[x][1] = _
	ans = max(ans, D[x][1] - D[x][0])
OPS(ans)


n = int(input())
(cnt0, cnt1) = (0, 0)
balance = []
dict = {0: -1}
l = 0
for (i, ch) in enumerate(input()):
	if ch == '1':
		cnt1 += 1
	else:
		cnt0 += 1
	balance.append(cnt1 - cnt0)
	if dict.get(cnt1 - cnt0) == None:
		dict[cnt1 - cnt0] = i
le = []
for i in range(len(balance) - 1, -1, -1):
	le += [i - dict.get(balance[i])]
print(max(le))


n = int(input())
T = input()
d = {0: -1}
s = 0
mix = 0
for i in range(n):
	s += 1 - (T[i] == '0') * 2
	if s not in d:
		d[s] = i
	elif i - d[s] > mix:
		mix = i - d[s]
print(mix)


import itertools as it
n = int(input())
ss = input()
sss = [int(x) for x in ss]
cnt_1 = it.accumulate(sss)
s_n = [x for x in range(1, len(sss) + 1)]
balance = [0] + [2 * x - y for (x, y) in zip(cnt_1, s_n)]
res = 0
tab = {}
for (i, v) in enumerate(balance):
	if v in tab:
		res = max(res, i - tab[v])
	else:
		tab[v] = i
print(res)


balance = dict()
n = int(input())
s = input()
b = 0
ans = 0
balance[0] = -1
for i in range(n):
	if s[i] == '1':
		b += 1
	else:
		b -= 1
	if b in balance:
		ans = max(i - balance[b], ans)
	else:
		balance[b] = i
print(ans)


n = int(input())
s = input()
dp = {0: -1}
ans = 0
p = 0
for (kd, i) in enumerate(s):
	if i == '1':
		p += 1
	else:
		p -= 1
	dp.setdefault(p, kd)
	ans = max(ans, kd - dp[p])
print(ans)


def longest_ballanced(s: str) -> int:
	running_totals = [0]
	total = 0
	for c in s:
		if c == '0':
			total -= 1
		elif c == '1':
			total += 1
		else:
			assert False, c
		running_totals.append(total)
	earliest = {}
	latest = {}
	for (i, t) in enumerate(running_totals):
		earliest.setdefault(t, i)
		latest[t] = i
	longest = 0
	for (t, first_index) in earliest.items():
		last_index = latest[t]
		length = last_index - first_index
		if length > longest:
			longest = length
	return longest
_ = input()
s = input()
print(longest_ballanced(s))


import sys
sys.setrecursionlimit(20000)
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	n = int(input())
	s = input().strip()
	d = {0: -1}
	mx = 0
	count = 0
	for i in range(n):
		if s[i] == '1':
			count += 1
		else:
			count -= 1
		if count in d:
			mx = max(mx, i - d[count])
		else:
			d[count] = i
	print(mx)
main()


n = int(input())
bal = [0] * (n + 1)
s = input()
zero = 0
one = 0
for i in range(n):
	if s[i] == '0':
		zero += 1
	else:
		one += 1
	bal[i + 1] = zero - one
minInd = {}
for i in range(n + 1):
	x = bal[i]
	y = minInd.get(x, n + 1)
	minInd[x] = min(y, i)
ans = 0
for i in range(n + 1):
	ans = max(ans, i - minInd[bal[i]])
print(ans)


import sys
n = int(input())
a = list(map(int, input()))
(odd, even) = (dict(), {0: 0})
ans = 0
(acc_odd, acc_even) = (0, 0)
for (i, x) in enumerate(a, start=1):
	acc_odd += x
	acc_even += x
	if i & 1:
		acc_odd -= 1
		if acc_odd in odd:
			ans = max(ans, i - odd[acc_odd])
		else:
			odd[acc_odd] = i
	else:
		acc_even -= 1
		if acc_even in even:
			ans = max(ans, i - even[acc_even])
		else:
			even[acc_even] = i
print(ans)


n = int(input())
l = input()
d = {0: -1}
s = 0
m = 0
for i in range(len(l)):
	if l[i] == '1':
		s += 1
	else:
		s += -1
	if s not in d:
		d[s] = i
	else:
		m = max(i - d[s], m)
print(m)


n = int(input())
s = input()
l0 = [0] * n
l1 = [0] * n
ls = [0] * n
a = s[0]
if a == '0':
	l0[0] = 1
	ls[0] = -1
else:
	l1[0] = 1
	ls[0] = 1
for i in range(1, n):
	if s[i] == '1':
		ls[i] = ls[i - 1] + 1
	else:
		ls[i] = ls[i - 1] - 1
dp = {}
dp[0] = -1
ans = 0
for i in range(n):
	e = ls[i]
	if e not in dp:
		dp[e] = i
	else:
		ans = max(ans, i - dp[e])
print(ans)


n = int(input())
s = input()
dicti = {}
dicti[0] = -1
ans = 0
sumi = 0
for i in range(n):
	if s[i] == '1':
		sumi += 1
	else:
		sumi -= 1
	if sumi in dicti:
		ans = max(ans, i - dicti[sumi])
	else:
		dicti[sumi] = i
print(ans)


def f(s):
	if len(s) == 1:
		return 0
	i = len(s) // 2
	n = get_max(s, i)
	return max(n, max(f(s[0:i]), f(s[i:len(s)])))

def get_max(s, i):
	j = i - 1
	n0 = 0
	n1 = 0
	length = 0
	lmap = {}
	while j >= 0:
		if s[j] == '0':
			n0 += 1
		if s[j] == '1':
			n1 += 1
		length += 1
		lmap[n1 - n0] = length
		j -= 1
	j = i
	n0 = 0
	n1 = 0
	length = 0
	m = 0
	while j < len(s):
		if s[j] == '0':
			n0 += 1
		if s[j] == '1':
			n1 += 1
		length += 1
		if n0 - n1 in lmap:
			m = max(m, length + lmap[n0 - n1])
		j += 1
	return m
n = input()
s = input()
print(f(s))


n = int(input())
a = list(map(int, input()))
bal = mx = 0
balan = {i: float('+inf') for i in range(-200000, 200001)}
balan[0] = -1
for i in range(n):
	bal -= a[i] == 0
	bal += a[i] == 1
	balan[bal] = min(balan[bal], i)
	mx = max(mx, i - balan[bal])
print(mx)


from collections import Counter
N = int(input())
nums = input().strip()
current_sum = 0
dict = {0: 0}
ans = 0
for i in range(1, N + 1):
	current_sum += 1 if nums[i - 1] == '1' else -1
	if current_sum not in dict:
		dict[current_sum] = i
	else:
		l = i - dict[current_sum]
		ans = max(ans, l)
print(ans)


n = int(input())
s = [2 * int(x) - 1 for x in input()]
d = {0: [-1]}
check = 0
for i in range(n):
	check += s[i]
	if check not in d:
		d[check] = [i]
	elif len(d[check]) == 1:
		d[check].append(i)
	else:
		d[check][1] = i
maxi = 0
ans = 0
for i in range(-len(d), len(d) + 1):
	if i in d:
		if len(d[i]) == 2:
			ans = d[i][1] - d[i][0]
			if ans > maxi:
				maxi = ans
print(maxi)


R = lambda : map(int, input().split())
n = int(input())
acc = [0]
for c in input():
	acc.append(acc[-1] + (-1 if c == '0' else 1))
res = 0
dp = {}
for (i, cnt) in enumerate(acc[1:]):
	if not cnt:
		res = max(res, i + 1)
	elif cnt not in dp:
		dp[cnt] = i
	else:
		res = max(res, i - dp[cnt])
print(res)


n = int(input())
w = input()
zc = cd = 0
jg = {0: -1}
for i in range(0, n):
	if w[i] == '1':
		zc += 1
	else:
		zc -= 1
	if jg.get(zc, -2) != -2:
		cd = max(cd, i - jg[zc])
	else:
		jg[zc] = i
print(cd)


from collections import defaultdict
import sys
input = sys.stdin.readline
n = int(input())
s = input()
m = defaultdict(lambda : -1)
m[0] = 0
ans = 0
sum = 0
for i in range(1, n + 1):
	sum += 1 if s[i - 1] == '1' else -1
	if sum == 0:
		ans = i
	elif m[sum] != -1:
		ans = max(ans, i - m[sum])
	else:
		m[sum] = i
print(ans)


n = int(input())
s = input()
bal = 0
arr = [0] * (n + 1)
lastseen = {0: 0}
arr[0] = 0
for i in range(1, n + 1):
	if s[i - 1] == '1':
		bal += 1
	else:
		bal -= 1
	lastseen[bal] = i
	arr[i] = bal
ans = 0
for i in range(n + 1):
	if lastseen[arr[i]] != i:
		ans = max(ans, lastseen[arr[i]] - i)
print(ans)


(n, s) = (input(), input())
d = {0: -1}
(diff, ans) = (0, 0)
for i in range(len(s)):
	diff -= 1 if '0' == s[i] else -1
	t = d.get(diff, -2)
	if t != -2:
		ans = max(ans, i - t)
	else:
		d[diff] = i
print(ans)


n = int(input())
s = input()
a = [-1 if c == '0' else 1 for c in s]
summ = 0
maxlen = 0
m = {}
for i in range(n):
	summ += a[i]
	if summ == 0:
		maxlen = i + 1
	if summ in m:
		maxlen = max(maxlen, i - m[summ])
	else:
		m[summ] = i
print(maxlen)


n = int(input())
s = input()
p1 = [0] * (n + 1)
p2 = [0] * (n + 1)
c1 = 0
c0 = 0
for i in range(n):
	if s[i] == '0':
		c0 += 1
	else:
		c1 += 1
	p1[i + 1] = c1
	p2[i + 1] = c0
count = [0] * 1000000
val = [0] * 10 ** 6
ans = [0]
for i in range(1, n + 1):
	if p1[i] == p2[i]:
		ans.append(p1[i] + p2[i])
	elif count[10 ** 5 + p1[i] - p2[i]] != 0:
		ans.append(p1[i] + p2[i] - val[10 ** 5 + p1[i] - p2[i]])
	else:
		count[10 ** 5 + p1[i] - p2[i]] += 1
		if count[10 ** 5 + p1[i] - p2[i]] == 1:
			val[10 ** 5 + p1[i] - p2[i]] = p1[i] + p2[i]
print(max(ans))


n = int(input())
s = input().strip()
x = [0]
y = [0]
tmp = [0]
mem = {}
mem[0] = 0
for (i, el) in enumerate(s):
	x.append(x[-1])
	y.append(y[-1])
	if el == '0':
		x[-1] += 1
	else:
		y[-1] += 1
	tmp.append(x[-1] - y[-1])
	if tmp[-1] not in mem:
		mem[tmp[-1]] = i + 1
res = 0
for i in range(len(s) + 1):
	res = max(res, i - mem[tmp[i]])
print(res)


n = int(input())
s = input()
l = []
c = 0
for i in range(n):
	if s[i] == '1':
		c += 1
	else:
		c -= 1
	l.append(c)
from collections import defaultdict
d = defaultdict(list)
ans = 0
for i in range(n):
	d[l[i]].append(i)
if len(d[0]) != 0:
	ans = d[0][-1] + 1
for i in range(1, n):
	a = l[i - 1]
	if len(d[a]) != 0:
		ans = max(ans, d[a][-1] - i + 1)
print(ans)


n = int(input())
s = input()
(z, o, d, ans) = (0, 0, {}, 0)
for i in range(n):
	if s[i] == '0':
		z += 1
	else:
		o += 1
	x = z - o
	if x == 0:
		ans = max(ans, i + 1)
	if x not in d:
		d[x] = i
	else:
		ans = max(ans, i - d[x])
print(ans)


def main():
	n = int(input())
	s = list(input())
	for i in range(n):
		if s[i] == '0':
			s[i] = -1
		else:
			s[i] = 1
	curr = 0
	sums = {}
	for i in range(n):
		curr += s[i]
		if curr not in sums.keys():
			sums[curr] = [i]
		else:
			sums[curr].append(i)
	ans = 0
	for i in sums.keys():
		if i == 0:
			ans = max(ans, sums[i][-1] + 1)
		else:
			ans = max(ans, sums[i][-1] - sums[i][0])
	print(ans)
main()


n = int(input())
s = input()
mp = {0: -1}
ans = bal = 0
for i in range(n):
	if s[i] == '0':
		bal -= 1
	else:
		bal += 1
	if bal not in mp:
		mp[bal] = i
	else:
		ans = max(ans, i - mp[bal])
print(ans)


a = int(input())
s = [int(c) for c in input()]
vm = {}
v = 0
ma = 0
for i in range(a):
	if s[i] == 0:
		s[i] = -1
vm[0] = -1
for t in range(a):
	v += s[t]
	if v in vm:
		if ma < t - vm[v]:
			ma = t - vm[v]
	else:
		vm[v] = t
print(ma)


def solution():
	n = int(input())
	s = input()
	cnt1 = [0] * (n + 1)
	cnt0 = [0] * (n + 1)
	balance = [0]
	for i in range(n):
		if s[i] == '1':
			cnt1[i + 1] += cnt1[i] + 1
			cnt0[i + 1] = cnt0[i]
		else:
			cnt1[i + 1] = cnt1[i]
			cnt0[i + 1] += cnt0[i] + 1
		x = cnt1[i + 1] - cnt0[i + 1]
		balance.append(x)
	d = {}
	ans = 0
	for i in range(n + 1):
		if d.get(balance[i], -1) == -1:
			d[balance[i]] = i
		else:
			ans = max(ans, i - d[balance[i]])
	print(ans)
solution()


l = int(input())
s = input()
first = {0: -1}
last = {0: -1}
z = 0
o = 0
for index in range(l):
	if s[index] == '0':
		z += 1
	elif s[index] == '1':
		o += 1
	if o - z not in first:
		first[o - z] = index
	last[o - z] = index
m = 0
for key in first:
	m = max(m, last[key] - first[key])
print(m)


n = int(input())
s = input()
d = {0: -1}
mx = 0
sm = 0
for i in range(n):
	sm += 1 - (s[i] == '0') * 2
	if sm not in d:
		d[sm] = i
	elif i - d[sm] > mx:
		mx = i - d[sm]
print(mx)


a = int(input())
s = input()
ans = []
score = 0
from collections import defaultdict
al = defaultdict(list)
for i in range(len(s)):
	if s[i] == '0':
		score -= 1
		ans.append(score)
		al[score].append(i)
	else:
		score += 1
		ans.append(score)
		al[score].append(i)
ans = list(set(ans))
maxa = 0
for i in range(len(ans)):
	if len(al[ans[i]]) > 1:
		te = al[ans[i]][-1] - al[ans[i]][0]
		maxa = max(maxa, te)
if len(al[0]) > 0:
	print(max(maxa, al[0][-1] + 1))
else:
	print(maxa)


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
n = int(input())
a = input().strip()
pf = [0] * n
cnt0 = 0
cnt1 = 0
for i in range(n):
	pf[i] = cnt1 - cnt0
	cnt1 += a[i] == '1'
	cnt0 += a[i] == '0'
pf.append(cnt1 - cnt0)
mp = defaultdict(list)
for (i, v) in enumerate(pf):
	mp[v].append(i)
res = 0
for (i, v) in mp.items():
	res = max(res, v[-1] - v[0])
print(res)


n = int(input())
s = input()
sig = [0 for i in range(n + 1)]
sig[1] = int(s[0])
for k in range(1, n):
	sig[k + 1] = sig[k] + int(s[k])
ans = [0 for i in range(n + 1)]
for k in range(n + 1):
	ans[k] = sig[k] - k / 2
u = {}
for pro in range(n + 1):
	if ans[pro] in u:
		u[ans[pro]].append(pro)
	else:
		u[ans[pro]] = [pro]
maxx = 0
for k in u:
	if len(u[k]) > 1:
		maxx = max(u[k][-1] - u[k][0], maxx)
print(maxx)


n = int(input())
arr = list(map(int, list(input())))
cnt0 = 0
cnt1 = 0
gg = {0: -1}
ans = 0
for i in range(n):
	if arr[i] == 0:
		cnt0 += 1
	else:
		cnt1 += 1
	ans = max(ans, i - gg.get(cnt0 - cnt1, i))
	gg[cnt0 - cnt1] = min(i, gg.get(cnt0 - cnt1, i + 1))
print(ans)


from collections import defaultdict as dt
from bisect import bisect_left as bl
from bisect import bisect_right as br
for tc in range(1):
	n = int(input())
	s = input()
	cnt0 = 0
	cnt1 = 0
	d = dict()
	map = dt(lambda : -1)
	for i in range(n):
		if s[i] == '0':
			cnt0 += 1
		else:
			cnt1 += 1
		d[i] = (cnt0, cnt1)
	for i in range(n):
		if map[d[i][0] - d[i][1]] == -1:
			map[d[i][0] - d[i][1]] = i
	ans = 0
	for i in range(n - 1, -1, -1):
		if d[i][0] == d[i][1]:
			ans = max(ans, i + 1)
		else:
			ind = map[d[i][0] - d[i][1]]
			ans = max(ans, i - ind)
	print(ans)


n = int(input())
s = input()
a = []
for i in range(2 * n + 1):
	a.append(-1)
a[n] = 0
tot = n
ans = 0
for i in range(n):
	if s[i] == '0':
		tot -= 1
	else:
		tot += 1
	if a[tot] == -1:
		a[tot] = i + 1
	else:
		ans = max(ans, i - a[tot] + 1)
print(ans)


n = int(input())
s = input()
a = [-1] * (n + 1)
a[0] = 0
count = [0, 0]
res = 0
for (idx, c) in enumerate(s):
	count[int(c)] += 1
	diff = count[0] - count[1]
	if a[diff] == -1:
		a[diff] = idx + 1
	elif idx + 1 - a[diff] > res:
		res = idx + 1 - a[diff]
print(res)


n = int(input())
ss = input()
l = []
for i in range(len(ss)):
	l.append(int(ss[i]))
for i in range(len(l)):
	if l[i] == 0:
		l[i] = -1
s = [0]
for i in l:
	s.append(s[-1] + i)
j = 1
ans = 0
dic = {}
for i in range(0, len(s)):
	if s[i] in dic:
		ans = max(ans, i - dic[s[i]])
	if s[i] not in dic:
		dic[s[i]] = i
print(ans)