#!/usr/bin/python # -*- coding: utf-8 -*- # import from sys import stdin, setrecursionlimit setrecursionlimit(10**8) def int_map(): return map(int,input().split()) def int_list(): return list(map(int,input().split())) def int_mtx(N): x = [list(map(int, stdin.readline().split())) for _ in range(N)] return x def str_mtx(N): x = [list(stdin.readline()[:-1]) for _ in range(N)] return x def print_space(l): return print(" ".join([str(x) for x in l])) """ main code """ N = int(input()) M = [] for _ in range(N): Mi = int(input()) M.append(Mi) dp = [[float("INF"),0] for _ in range(2**N)] dp[0][0] = 0 for s in range(2**N): for u in range(N): if s >> u & 1 == 0: dp[s+2**u][0] = min(dp[s+2**u][0],dp[s][0]+max(0,M[u]-dp[s][1])) dp[s+2**u][1] = (dp[s][1]+M[u])%1000 print(dp[2**N-1][0])