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.

Difficulty: interview

Starter code:

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

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 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())
l=list(accumulate([2*int(c)-1 for c in 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)


from itertools import accumulate

n=int(input())
l=accumulate([2*int(c)-1 for c in 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()

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)

# -*- coding: utf-8 -*-

import math
import collections
import bisect
import heapq
import time
import random

"""
created by shhuan at 2017/10/12 22:12

"""

n = int(input())
s = input()
# n = 100000
# s = ''
# for i in range(n):
#     x = random.randint(1, 10)
#     if x >= 5:
#         s += '1'
#     else:
#         s += '0'
#
# t0 = time.time()

# n = 8
# s = '11010111'

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)

# print(ones)
# print(ds)
ans = 0
for k, v in list(ds.items()):
    ans = max(ans, max(v)-min(v))

print(ans)
# print(time.time() - t0)




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)


'''input
8
00001111
'''

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)


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


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


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


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 list(d.keys()):
        ans = max(ans, i - d[tp])
    else:
        d[tp] = i
print(ans)


n=int(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)


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)


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)

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)

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(i ,current_sum,dict,ans)

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))



# coding: utf-8

# In[2]:


import sys


# In[3]:


n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()


# In[53]:


balance_max_pos = {}

balance = 0
for i, c in enumerate(s):
    if c == '1':
        balance += 1
    else:
        balance -= 1
    
    balance_max_pos[balance] = i


# In[58]:


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)


# In[59]:


print(ans)


# In[46]:

# In[ ]:






_ = 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)

"""
First try - Try to use something like binary search to find the length of the substring.
Sadly, this does not work,
    as knowing that the maximum possible substring length is 8 and having a valid substring of length 4 does not guarantee that we have a substring of length 6
    e.g 00111100
"""
"""
_ = input()
string = input()
str_len = len(string)
one_count, zero_count = 0, 0
counts_by_idx = {}
for idx, x in enumerate(string):
    if x == '1':
        one_count += 1
    else:
        zero_count += 1
    counts_by_idx[idx] = {'0': zero_count, '1': one_count}

maximum_substring_length = min(zero_count, one_count) * 2
print(maximum_substring_length)
curr_best = 0

lower_bound = 0
current_search = maximum_substring_length // 2
upper_bound = maximum_substring_length


def srch(lower_bound, current_search, upper_bound):
    nonlocal curr_best
    for start_idx in range(str_len-current_search):
        end_idx = start_idx + current_search
        zero_count = counts_by_idx[end_idx]['0'] - counts_by_idx[start_idx]['0']
        one_count = counts_by_idx[end_idx]['1'] - counts_by_idx[start_idx]['1']
        if one_count == zero_count:
            # Try to go up
            curr_best = zero_count * 2
            new_search = current_search + ((upper_bound - current_search) // 2)
            return current_search, new_search, upper_bound

    new_search = lower_bound + ((current_search - lower_bound) // 2)
    return lower_bound, new_search, current_search


while True:
    lower_bound, current_search, upper_bound = srch(lower_bound, current_search, upper_bound)
"""

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)


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

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())
x = input()
s = ''.join(['0', x, '1'])
s1 = ''.join(['1', x, '0'])
# s = input()

def get_max(s):
	net_count = 0
	current_max = 0
	mp = {}
	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(min(get_max(s), get_max(s1)))