local mmi, mma = math.min, math.max local mab = math.abs local bls, brs = bit.lshift, bit.rshift local mfl = math.floor local function comp(a, b) return a < b end local function lower_bound(ary, x) local num = #ary if num == 0 then return 1 end if not comp(ary[1], x) then return 1 end if comp(ary[num], x) then return num + 1 end local min, max = 1, num while 1 < max - min do local mid = mfl((min + max) / 2) if comp(ary[mid], x) then min = mid else max = mid end end return max end local n = io.read("*n") local t = {} local tgt = 0 for i = 1, n do local a, b = io.read("*n", "*n") tgt = tgt + a t[i] = a + b end if n <= 16 then local ret = tgt local tot = 2^n for it = 0, tot - 1 do local z = 0 for i = 1, n do if it % 2 == 1 then z = z + t[i] end it = brs(it, 1) end ret = mmi(ret, mab(tgt - z)) end print(ret) else local first = {} for it = 0, 65535 do local z = 0 for i = 1, 16 do if it % 2 == 1 then z = z + t[i] end it = brs(it, 1) end table.insert(first, z) end local ret = tgt table.sort(first) local remtot = 2^(n - 16) for it = 0, remtot - 1 do local z = 0 for i = 17, n do if it % 2 == 1 then z = z + t[i] end it = brs(it, 1) end local pos = lower_bound(first, tgt - z) if pos <= #first then ret = mmi(ret, mab(tgt - first[pos] - z)) end if 1 < pos then ret = mmi(ret, mab(tgt - first[pos - 1] - z)) end end print(ret) end