Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- //#include <bits/stdc++.h>
- #include <cmath>
- #include <queue>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- int n, d;
- ll p;
- ll a[maxn];
- bool check(int k) {
- ll sum = 0;
- priority_queue<ll> pq, greatest;
- for(int i = 0; i < n; i++) {
- pq.push(a[i]);
- }
- for(int i = 1; i <= d; i++) {
- ll max_tree = 0;
- if(!pq.empty()) {
- max_tree = pq.top();
- pq.pop();
- }
- sum += max_tree;
- if(i > k and !greatest.empty()) {
- pq.push(greatest.top());
- greatest.pop();
- }
- greatest.push(max_tree);
- }
- return sum >= p;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n >> p >> d;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- int L = 1, R = d;
- int res = -1;
- while(L <= R) {
- int mid = (L + R) / 2;
- if(check(mid)) {
- L = mid + 1;
- res = mid;
- }
- else {
- R = mid - 1;
- }
- }
- if(res == d) {
- cout << "seedno" << endl;
- }
- else {
- cout << res << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement