import sys INF = float('inf') #10**20,2**63,float('inf') MOD = 10**9 + 7 MOD2 = 998244353 #from collections import defaultdict def solve(): def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LC(): return list(input()) def IC(): return [int(c) for c in input()] def MI(): return map(int, sys.stdin.readline().split()) N = II() M = [0]*N for m in range(N): M[m] = II() DP = [INF for _ in range(2 ** N)] DP[0] = 0 for S in range(2**N): if DP[S] == INF: continue Sum = 0 for n in range(N): if S >> n & 1: Sum += M[n] for t in range(N): if (S >> t) & 1: # 行先はもう行った状態だったらスキップ continue if DP[S | (1 << t)] > DP[S] + max(M[t] - Sum%1000,0): DP[S | (1 << t)] = DP[S] + max(M[t] - Sum%1000,0) else: continue #print(DP) print(DP[S]) return solve() #sys.setrecursionlimit(10 ** 6)#再帰関数ではコメントにしないこと!!