local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max local n = io.read("*n") local a, b, c = {}, 0, {} local tot = 1 local alltask = {} local stagetask = {} for i = 1, n do a[i], b = io.read("*n", "*n") c[i] = b - a[i] tot = tot * 2 alltask[i] = {} stagetask[i] = {} end local inf = 3000 for i = 1, n do for j = 1, tot - 1 do alltask[i][j] = inf end end for i = 1, tot - 1 do local ti = i local cnt = 0 for j = 1, n do if ti % 2 == 1 then cnt = cnt + 1 ti = ti - 1 end ti = ti / 2 end table.insert(stagetask[cnt], i) end local function dotask(stage) local stlen = #stagetask[stage] for i_stagetask = 1, stlen do local used = stagetask[stage][i_stagetask] for i = 1, n do local val = alltask[i][used] if val ~= inf then local mul = 1 local tmp = used for j = 1, n do if tmp % 2 == 0 then alltask[j][used + mul] = mmi(alltask[j][used + mul], mma(c[i] + a[j], val)) else tmp = tmp - 1 end tmp = tmp / 2 mul = mul * 2 end end end end end local tmp = 1 for i = 1, n do alltask[i][tmp] = 0 tmp = tmp * 2 end for i = 1, n - 1 do dotask(i) end local min = inf for i = 1, n do min = mmi(min, alltask[i][tot - 1]) end print(min)