local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max local tbl = {} local function prepare(n, cost) local tot = 1 local alltask = {} local stagetask = {} for i = 1, n do tot = tot * 2 stagetask[i] = {} end for i = 1, tot - 1 do alltask[i] = 150000 tbl[i] = 0 end for i = 1, tot - 1 do local ti = i local cnt = 0 for j = 1, n do if ti % 2 == 1 then tbl[i] = tbl[i] + cost[j] cnt = cnt + 1 ti = ti - 1 end ti = ti / 2 end table.insert(stagetask[cnt], i) end -- set first state local tmp = 1 for i = 1, n do alltask[tmp] = cost[i] tmp = tmp * 2 end return tot, alltask, stagetask end local function doall(n, alltask, stagetask, cost) for stage = 1, n - 1 do local stlen = #stagetask[stage] for i_stagetask = 1, stlen do local used = stagetask[stage][i_stagetask] local mul = 1 local tmp = used for j = 1, n do if tmp % 2 == 0 then alltask[used + mul] = mmi(alltask[used + mul], alltask[used] + mma(0, cost[j] - tbl[used] % 1000)) else tmp = tmp - 1 end tmp = tmp / 2 mul = mul * 2 end end end end local n = io.read("*n") local cost = {} for i = 1, n do cost[i] = io.read("*n") end local tot, alltask, stagetask = prepare(n, cost) doall(n, alltask, stagetask, cost) print(alltask[tot - 1])