Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define INF (ll)(1e18)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }





ll n, m, v;
int p;
vector<int> a;

bool check(int x) { // x番目の問題を採用できるか
    if (x + 1 <= p) return 1;
    if (a[x] + m < a[p - 1]) return 0;
    
    ll sum = m * (p - 1 + n - x);
    rep3(i, p - 1, x) {
        sum += a[x] + m - a[i];
    }
    if (sum >= m * v) return 1;
    return 0;
}

template<typename T>
void binary_search(T ok, T ng) {
    while (abs(ng - ok) > 1) {
        T mid = (ng + ok) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    cout << ok + 1 << endl;
}

void solve() {
    cin >> n >> m >> v >> p;
    a.resize(n);
    rep(i, n) cin >> a[i];
    sort(all(a), greater<int>());
    binary_search(0, n);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int T;
    // cin >> T;
    T = 1;
    while (T--) {
        solve();
        // cout << (solve() ? "YES" : "NO") << endl;
    }
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:56:18: error: no matching function for call to 'binary_search(int, ll&)'
   56 |     binary_search(0, n);
      |     ~~~~~~~~~~~~~^~~~~~
a.cc:43:6: note: candidate: 'template<class T> void binary_search(T, T)'
   43 | void binary_search(T ok, T ng) {
      |      ^~~~~~~~~~~~~
a.cc:43:6: note:   template argument deduction/substitution failed:
a.cc:56:18: note:   deduced conflicting types for parameter 'T' ('int' and 'long long int')
   56 |     binary_search(0, n);
      |     ~~~~~~~~~~~~~^~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:2194:5: note: candidate: 'template<class _FIter, class _Tp> bool std::binary_search(_FIter, _FIter, const _Tp&)'
 2194 |     binary_search(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2194:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:2228:5: note: candidate: 'template<class _FIter, class _Tp, class _Compare> bool std::binary_search(_FIter, _FIter, const _Tp&, _Compare)'
 2228 |     binary_search(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2228:5: note:   candidate expects 4 arguments, 2 provided