#include using namespace std; int main() { int N,D; cin >> N >> D; vectorP(N),Q(N); for(int i = 0; i < N; i++) { cin >> P[i] >> Q[i]; } int l = -1001001001,r = 0; while (l+1 < r) { int mid = (l+r)/2; vector>dp(D+1,vector(N,-1001001001)); for(int i = 0; i < N; i++) { dp[0][i] = 0; } for(int i = 0; i < D; i++) { vectorlmx(N+1,-1001001001),rmx(N+1,-1001001001); for(int j = 0; j < N; j++) { lmx[j+1] = max(lmx[j],dp[i][j]); } for(int j = N; j >= 1; j--) { rmx[j-1] = max(rmx[j],dp[i][j-1]); } for(int j = 0; j < N; j++) { if(max(lmx[j],rmx[j+1])-P[j] >= mid) { dp[i+1][j] = max(dp[i+1][j],max(lmx[j],rmx[j+1])-P[j]+Q[j]); } } } int mx = *max_element(dp[D].begin(),dp[D].end()); if(mx >= mid) { l = mid; } else { r = mid; } } cout << l << endl; }