import math def solve(): N = int(input()) c = list(map(int, input().split())) # Create a dictionary to check the counts of each digit. input_counts = {str(d+1): c[d] for d in range(9)} # Check if all digits are odd (leading to exponent 0) all_odd = True for d in range(1, 10): if (d % 2 == 0) and c[d-1] > 0: all_odd = False break if all_odd: print(0) return # Check exact power of 2 log2 = math.log10(2) lower_e = max(0, math.ceil((N-1) * log2)) upper_e = math.floor(N * log2) max_e = -1 for e in range(lower_e, upper_e + 1): num = 1 << e s = str(num) if len(s) != N: continue temp_counts = {} for ch in s: temp_counts[ch] = temp_counts.get(ch, 0) + 1 valid = True for d in range(1, 10): key = str(d) if temp_counts.get(key, 0) != input_counts.get(key, 0): valid = False break if valid: max_e = e break # Found the maximum possible e if max_e != -1: print(max_e) return # Fallback to permutations (only feasible for very small N) from itertools import permutations digits = [] for d in range(9): digits.extend([str(d+1)] * c[d]) max_k = 0 seen = set() for perm in permutations(digits): num_str = ''.join(perm) if num_str in seen: continue seen.add(num_str) if perm[-1] in {'1', '3', '5', '7', '9'}: continue num = int(num_str) current_k = 0 while num % 2 == 0: current_k += 1 num //= 2 if current_k > max_k: max_k = current_k print(max_k) solve()