Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- //#include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- int n, d;
- ll p;
- int v[maxn];
- bool check(int x) {
- priority_queue<int> pq, greatest;
- for(int i = 0; i < n; i++) {
- pq.push(v[i]);
- }
- ll sum = 0;
- for(int i = 1; i <= d; i++) {
- int max_element = 0;
- if(!pq.empty()) {
- max_element = pq.top();
- pq.pop();
- }
- sum += max_element;
- if(i > x) {
- pq.push(greatest.top());
- greatest.pop();
- }
- greatest.push(max_element);
- }
- return sum >= p;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n >> p >> d;
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- int L = 1, R = d;
- int res = -1;
- while(L <= R) {
- int mid = (L + R) / 2;
- if(check(mid)) {
- L = mid + 1;
- res = max(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