def minBuy(M,dp,st): minSum=10**9 i=0 while True: if st&2**i!=0: k=0 tmpSum=0 while True: if st<2**k: break if i==k: k+=1 continue if st&2**k!=0: tmpSum+=M[k] k+=1 minSum=min(minSum,dp[st-2**i]+max(0,M[i]-tmpSum%1000)) i+=1 if st<2**i: break return minSum N=int(raw_input()) M=[] for _ in xrange(N): M.append(int(raw_input())) dp=[10**9]*(2**N) dp[0]=0 for i in xrange(1,2**N): dp[i] = minBuy(M,dp,i) print dp[2**N-1]