N = int(input()) M = [int(input()) for _ in range(N)] INF = 2 * 10 ** 5 N2 = 1 << N dp = [INF] * N2 X = [0] * N2 dp[0] = 0 for i in range(N2): tmp = 0 for j in range(N): if i & (1 << j): X[i] += M[j] X[i] %= 1000 for i in range(N): for j in range(N): for k in reversed(range(N2)): if not (k & (1 << j)): dp[k | (1 << j)] = min(dp[k | (1 << j)], dp[k] + max(0, M[j] - X[k])) print(dp[(N2 - 1)])