using static System.Math; using System.Linq; using System.Collections.Generic; using System; public class P { public int ended { get; set; } public int pre { get; set; } } public class Hello { public static int n; static void Main() { n = int.Parse(Console.ReadLine().Trim()); var input = new int[n, 2]; for (int i = 0; i < n; i++) { string[] line = Console.ReadLine().Trim().Split(' '); input[i, 0] = int.Parse(line[0]); input[i, 1] = int.Parse(line[1]); } getAns(input); } static int[,] makeArray(int[,] input) { var res = new int[n, n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) res[i, j] = input[i, 1] - input[i, 0] + input[j, 0]; return res; } static void getAns(int[,] input) { var aa = makeArray(input); var imax = 1 << n; var dp = new int[imax, n]; for (int i = 0; i < imax; i++) for (int j = 0; j < n; j++) dp[i, j] = int.MaxValue; var q = new Queue
(); for (int i = 0; i < n; i++) { var t = 1 << i; dp[t, i] = 0; q.Enqueue(new P { ended = t, pre = i }); } while (q.Count() > 0) { var w = q.Dequeue(); for (int i = 0; i < n; i++) { if (((w.ended >> i) & 1) == 0) { var ndp = Max(dp[w.ended, w.pre], aa[w.pre, i]); var nended = (w.ended | (1 << i)); if (ndp < dp[nended, i]) { dp[nended, i] = ndp; q.Enqueue(new P { ended = nended, pre = i }); } } } } var ans = int.MaxValue; for (int i = 0; i < n; i++) ans = Min(ans, dp[imax - 1, i]); Console.WriteLine(ans); } }