diff --git a/Luogu/T263132/T263132.cpp b/Luogu/T263132/T263132.cpp new file mode 100644 index 00000000..d286d1e7 --- /dev/null +++ b/Luogu/T263132/T263132.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 50005; + +int n, k; +long long m, sum; +std::pair a[N]; +std::priority_queue, std::greater> q; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> k >> m; + + for (int i = 1; i <= n; i++) { + cin >> a[i].first >> a[i].second; + } + + std::sort(a + 1, a + n + 1, [](auto a, auto b) { + return a.second < b.second; + }); + + for (int i = 1; i <= k; i++) { + sum += a[i].second; + + q.push(a[i].first - a[i].second); + + if (sum > m) { + cout << i - 1 << endl; + + exit(0); + } else if (i == n) { + cout << n << endl; + + exit(0); + } + } + + std::sort(a + k + 1, a + n + 1, [](auto a, auto b) { + return a.first < b.first; + }); + + for (int i = k + 1; i <= n; i++) { + auto x = q.top(); + + sum += a[i].first; + + if (a[i].second + x < a[i].first) { + q.pop(); + + auto y = a[i].first - a[i].second; + q.push(y); + sum += x - y; + } + + if (sum > m) { + cout << i - 1 << endl; + + exit(0); + } + } + + cout << n << endl; + + return 0; +}