同上一题,正数看作1,负数看作-1,k=0
m存储前i项和对应下标,a存储前i项和,ans最大长,即求第i项时m[a-k]是否存在

def solve(l, n):
    m = {0:-1}
    a = ans = 0
    for i in range(n):
        if l[i] > 0:
            a += 1
        elif l[i] < 0:
            a -= 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