#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; struct Food { int p; int q; Food(int p, int q) { this->p = p; this->q = q; } }; int N, D; vector foods; bool f(int x) { int val = 0; bool checked[N]; memset(checked, false, sizeof(checked)); for (int d = 0; d < D; ++d) { bool ok = false; for (int i = 0; i < N; ++i) { if (checked[i]) continue; Food &food = foods[i]; if (val - food.p < x) continue; val -= food.p; val += food.q; checked[i] = true; ok = true; break; } if (not ok) return false; } return true; } int main() { cin >> N >> D; for (int i = 0; i < N; ++i) { int p, q; cin >> p >> q; foods.push_back(Food(p, q)); } int ok = -100000000; int ng = 1000000; while (abs(ok - ng) >= 2) { int x = (ok + ng) / 2; if (f(x)) { ok = x; } else { ng = x; } } cout << ok << endl; return 0; }