Language: C++

#include <algorithm>
#include <cstdio>
#define LL long long
#define int long long

using namespace std;

const int N = 1e5 + 5;

int n, m, v, p;
int d[N];

bool cmp(int a, int b) {
    return a > b;
}

bool check(int mid) {
    if (mid <= p) return 1;
    if (d[mid] + m < d[p]) return 0;
    if (v < p && d[mid] + m >= d[p]) return 1;
    LL ans = (LL)m * p;
    for (int i = n, j = v - p; i > mid && j; i--, j--) {
        ans += m;
    }
    LL ned = (LL)m * v - ans;
    if (ned < 0) return 1;
    int cd;
    for (int i = p; i < mid; ++i) {
        int x = min(m, max(0, d[mid] + m - d[i]));
        if (x > ned) x = ned;
        ned -= x;
        if (i == p) {
            cd = d[p] + x;
        }
    }
    return cd <= m + d[mid] && ned == 0;
}

int main() {
    scanf("%d%d%d%d", &n, &m, &v, &p);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &d[i]);
    }
    sort(d + 1, d + n + 1, cmp);
    int l = 1, r = n + 1;
    int ans = 0;
    while (l + 1 < r) {
        int mid = (l + r) >> 1;
        if (check(mid)) {
            l = mid;
            ans = mid;
        } else {
            r = mid ;
        }
    }
    printf("%d\n", ans);
    return 0;
}


Compiler output:
a.cc: In function 'bool check(long long int)':
a.cc:29:27: error: no matching function for call to 'max(int, long long int)'
   29 |         int x = min(m, max(0, d[mid] + m - d[i]));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:29:27: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   29 |         int x = min(m, max(0, d[mid] + m - d[i]));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:29:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   29 |         int x = min(m, max(0, d[mid] + m - d[i]));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~
At global scope:
cc1plus: error: '::main' must return 'int'