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