N = int(input()) M = [int(input()) for i in range(N)] INF = 1 << 60 dp = [INF] * (1 << N) dp[0] = 0 for status in range(1 << N): cur_price = 0 for i in range(N): if (status >> i) & 1: cur_price += M[i] discount = cur_price % 1000 for i in range(N): if (status >> i) & 1: continue next_status = status | (1 << i) dp[next_status] = min(dp[next_status], dp[status] + max(0, M[i] - discount)) print(dp[-1])