#include using namespace std; #define rep(i, x) for(int i = 0; i < (x); i++) const int inf = 1 << 29; int main() { int n, d; cin >> n >> d; vector p(n), q(n); rep(i, n) { cin >> p[i] >> q[i]; } auto f = [&](int x) { vector dp(d + 1, vector(n, -inf)); rep(i, n) { if(-p[i] < x) continue; dp[1][i] = q[i] - p[i]; } for(int i = 1; i < d; i++) { auto vec = dp[i]; auto mx = -inf, mx2 = -inf; rep(j, n) { if(mx < dp[i][j]) { mx2 = mx, mx = dp[i][j]; } else if(mx2 < dp[i][j]) { mx2 = dp[i][j]; } } rep(j, n) { if(dp[i][j] != mx) { if(mx - p[j] < x) continue; dp[i + 1][j] = mx - p[j] + q[j]; } else { if(mx2 - p[j] < x) continue; dp[i + 1][j] = mx2 - p[j] + q[j]; } } } return *max_element(dp.back().begin(), dp.back().end()) > -inf; }; int ok = -inf, ng = inf; while(abs(ok - ng) > 1) { int mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } cout << ok << endl; return 0; }