n = int(input()) C = list(map(int, input().split())) A = [] for i, c in enumerate(C, 1): A += [i] * c memo = {} def f(bit, tot, t, x, ten): if tot % x != 0: return 0 if t == n: return (tot & -tot).bit_length() - 1 tmp = tot * n + t if tmp in memo: return memo[tmp] ret = t for i in range(n): if not bit >> i & 1: if i > 0 and A[i - 1] == A[i] and not (bit >> (i - 1) & 1): continue ret = max(ret, f(bit ^ (1 << i), A[i] * ten + tot, t + 1, x * 2, ten * 10)) memo[tmp] = ret return ret ans = f(0, 0, 0, 1, 1) print(ans)