#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)); dp[0].assign(n, 0); rep(i, d) { int 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) { const int tmp = (dp[i][j] != mx ? mx : mx2); if(tmp - p[j] < x) continue; dp[i + 1][j] = tmp - 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; }