Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define int long long
- int arr[100005], pref[100005];
- signed main() {
- #ifdef ONLINE_JUDGE
- ios::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- #endif
- int n, p, d, l = 1, r, m, k = -1;
- cin >> n >> p >> d;
- r = d;
- for (int i = 0; i < n; i++) cin >> arr[i];
- sort(arr, arr + n);
- reverse(arr, arr + n);
- for (int i = 0; i < n; i++) {
- pref[i + 1] = pref[i] + arr[i];
- // cerr << "pref[" << i + 1 << "] = " << pref[i + 1] << '\n';
- }
- auto f = [&](int x) { ++x; return pref[min(x, n)] * (d / x) + pref[min(d % x, n)]; };
- while (l <= r) {
- m = (l + r) / 2;
- // cerr << "m = " << m << "\nf(m) = " << f(m) << endl;
- if (f(m) < p) {
- // mnogu malce
- // cerr << "mnogu malce\n";
- r = m - 1;
- }
- else {
- // cerr << "dovolno\n";
- // dovolno
- if (m == d && f(d + 1) >= p) {
- // se edno
- cout << "seedno";
- return 0;
- }
- l = m + 1;
- k = m;
- }
- }
- cout << k;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment