N = input() M = [input() for i in xrange(N)] total = {0:0} dp = {0:0} for i in xrange(N): tmp_dp = dict() for k,v in dp.iteritems(): for j in xrange(N): jbit = 1 << j if (jbit & k) != 0: continue nk = k|jbit total[nk] = total[k] + M[j] nv = v + max(0,M[j] - total[k]%1000) if not(nk in tmp_dp and nv >= tmp_dp[nk]): tmp_dp[nk] = nv dp = tmp_dp print dp[2**N-1]