PlanttPastes

Дрва со гумени бомбони

Dec 10th, 2025
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. #define int long long
  5.  
  6. int arr[100005], pref[100005];
  7.  
  8. signed main() {
  9.     #ifdef ONLINE_JUDGE
  10.     ios::sync_with_stdio(0);
  11.     cin.tie(0); cout.tie(0);
  12.     #endif
  13.  
  14.     int n, p, d, l = 1, r, m, k = -1;
  15.     cin >> n >> p >> d;
  16.     r = d;
  17.     for (int i = 0; i < n; i++) cin >> arr[i];
  18.     sort(arr, arr + n);
  19.     reverse(arr, arr + n);
  20.     for (int i = 0; i < n; i++) {
  21.         pref[i + 1] = pref[i] + arr[i];
  22.         // cerr << "pref[" << i + 1 << "] = " << pref[i + 1] << '\n';
  23.     }
  24.     auto f = [&](int x) { ++x; return pref[min(x, n)] * (d / x) + pref[min(d % x, n)]; };
  25.     while (l <= r) {
  26.         m = (l + r) / 2;
  27.         // cerr << "m = " << m << "\nf(m) = " << f(m) << endl;
  28.         if (f(m) < p) {
  29.             // mnogu malce
  30.             // cerr << "mnogu malce\n";
  31.             r = m - 1;
  32.         }
  33.         else {
  34.             // cerr << "dovolno\n";
  35.             // dovolno
  36.             if (m == d && f(d + 1) >= p) {
  37.                 // se edno
  38.                 cout << "seedno";
  39.                 return 0;
  40.             }
  41.             l = m + 1;
  42.             k = m;
  43.         }
  44.     }
  45.     cout << k;
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment