ソースコード
n = int(input())
a = list(map(int, input().split()))
tot = sum(a)
f = [[],[],[]]
for v in a:
f[v%3].append(v)
f[0].sort()
f[1].sort()
f[2].sort()
if tot % 3 == 0:
print(tot)
elif tot % 3 == 1:
ans = 0
if len(f[1]) >= 1:
ans = max(ans, tot - f[1][0])
if len(f[2]) >= 2:
ans = max(ans, tot - f[2][0] - f[2][1])
if ans == 0:
print(-1)
else:
print(ans)
else:
ans = 0
if len(f[2]) >= 1:
ans = max(ans, tot - f[2][0])
if len(f[1]) >= 2:
ans = max(ans, tot - f[1][0] - f[1][1])
if ans == 0:
print(-1)
else:
print(ans)