using static System.Math; using System; public class Hello { static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var d = int.Parse(line[1]); var p = new int[n ]; var q = new int[n ]; for (int i = 0; i < n; i++) { line = Console.ReadLine().Trim().Split(' '); p[i] = int.Parse(line[0]); q[i] = int.Parse(line[1]); } getAns(n, d, p, q); } static void getAns (int n, int d, int [] p, int[] q) { var ok = -100000000; var ng = 0; while (ng -ok > 1) { var mid = ok + (ng - ok) / 2; if (check(n, d, p, q, mid)) ok = mid; else ng = mid; } Console.WriteLine(ok); } static bool check (int n, int d, int[]p, int[]q , int t) { var dp = new int[d + 1, n]; for (int i = 0; i < d + 1; i++) for (int j = 0; j < n; j++) dp[i, j] = int.MinValue; for (int i = 0; i < n; i++) if (-p[i] >= t) dp[1, i] = -p[i] + q[i]; for (int i = 2; i <= d; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (j == k) continue; if (dp[i - 1, j] == int.MinValue) continue; var temp = dp[i - 1, j] - p[k]; if (temp < t) continue; temp += q[k]; dp[i, k] = Max(dp[i, k], temp); } } } for (int i = 0; i < n; i++) if (dp[d,i] != int.MinValue) { return true; } return false; } }