INF = 10000 N = gets.to_i S = 1 << N B = N.times.map { gets.split.map(&:to_i) } dp = Array.new(N){ Array.new(S, INF) } # dp[i][s]はsを使用して最後の本がiの時の栞の最大間隔 N.times do |i| dp[i][1 << i] = 0 end (1 ... S).each do |s| used, unused = N.times.partition{ |i| (s & (1 << i)) > 0 } unused.each do |j| t = s | (1 << j) dp[j][t] = used.map{|i| [dp[i][s], B[i][1] - B[i][0] + B[j][0]].max }.min end end puts N.times.map{|i| dp[i][S - 1] }.min