import sequtils,algorithm # import ,math,tables # import sets,intsets,queues,heapqueue,bitops,strutils # import strutils,strformat,sugar,macros # template stopwatch(body) = (let t1 = cpuTime();body;echo "TIME:",(cpuTime() - t1) * 1000,"ms") template times*(n:int,body) = (for _ in 0.." .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': return result = 10 * result + k.ord - '0'.ord let n = scan().int32 let M = newSeqWith(n,scan().int32).sorted(cmp,Descending) const INF = 1e10.int32 var ans = INF var dp : array[^15 + 1,int32] var T : array[^15 + 1,int32] for x in 0..(^n): dp[x] = INF # proc solve(s,total,buyTotal:int) = # if s == ^n - 1: # ans .min= total # return # if dp[s] <= total : return # dp[s] = total # for x in 0.. 0 : continue # solve(s or ^x,total + (M[x] - (buyTotal mod 1000)).max(0),buyTotal+M[x]) # solve(0,0,0) proc solve() = for x in 0..<(^n - 1): for i in 0.. 0 : continue dp[x or ^i] .min= dp[x] + 0.max(M[i] - T[x] mod 1000) T[x or ^i] = T[x] + M[i] dp[0] = 0 solve() echo dp[^n-1]