n = int(input()) cnt = [0] + list(map(int, input().split())) tot = 0 two = 8 five = 5 seven = 7 for i, c in enumerate(cnt): if c == n: print(str(i) * n) exit() tot += i * c if c > 0: if i == 8: pass elif i == 4: two = min(two, 4) elif i == 2: two = min(two, 2) else: two = 1 if i != 5: five = 1 if i != 7: seven = 1 ans = two * five * seven if tot % 9 == 0: ans *= 9 elif tot % 3 == 0: ans *= 3 print(ans)