import sys readline = sys.stdin.readline N = int(readline()) M = [int(readline()) for i in range(N)] INF = 1 << 60 dp = [INF] * (1 << N) dp[0] = 0 for status in range(1 << N): # 現状態でのdiscount額を求める discount = 0 for i in range(N): if (status >> i) & 1: discount += M[i] discount %= 1000 for target in range(N): if (status >> target) & 1: continue next_status = status | (1 << target) price = max(0, M[target] - discount) if dp[next_status] > dp[status] + price: dp[next_status] = dp[status] + price print(dp[-1])