同上一题

def solve(l, n):
    m = {0:-1}
    a = ans = 0
    for i in range(n):
        a += 1 if l[i] else -1
        if a not in m.keys():
            m[a] = i
        else:
            ans = max(ans, i - m[a])
    return ans

while True:
    try:
        n = int(input())
        l = list(map(int, input().split()))
        print(solve(l, n))
    except EOFError:
        break