require "big" n = read_line.to_i m = (n + 1) // 2 ab = Array.new(n) { a, b = read_line.split.map &.to_big_i; {a, b} } ab << {0.to_big_i, 0.to_big_i} if n.odd? def make_v(a) n = a.size [0, 1].repeated_permutations(a.size).map { |flag| a.zip(flag).sum { |ai, k| ai[k] * (k == 0 ? 1 : -1) } }.sort end v1, v2 = make_v(ab[0, m].to_a), make_v(ab[m, m].to_a) puts v1.min_of { |val1| pos = v2.bsearch_index { |val2| val2 >= -val1 } pos = m - 1 if !pos v2[{0, pos - 1}.max..pos + 1].min_of { |val2| (val1 + val2).abs } }