N = int(input()) M = [int(input()) for i in range(N)] DP = [10 ** 9] * 2 ** N X = [0] * 2 ** N DP[0] = 0 from itertools import combinations for i in range(1, N + 1): for C in combinations(range(N), r=i): x = sum([2 ** c for c in C]) X[x] = X[x - 2 ** C[0]] + M[C[0]] DP[x] = min([DP[x - 2 ** c] + max(0, M[c] - X[x - 2 ** c] % 1000) for c in C]) print(DP[-1])