Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- int n, d;
- ll P, 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 m = 0;
- if(!pq.empty()) {
- m = pq.top();
- pq.pop();
- }
- sum += m;
- if(i > K) {
- pq.push(greatest.top());
- greatest.pop();
- }
- greatest.push(m);
- }
- 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;
- int R = d;
- int res = -1;
- while(L <= R) {
- int middle = (L + R) / 2;
- if(check(middle)) {
- res = max(res, middle);
- L = middle + 1;
- }
- else {
- R = middle - 1;
- }
- }
- if(res == d) {
- cout << "seedno" << endl;
- }
- else {
- cout << res << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment