Language: C++

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

#define getBit(x,i) (x&(1<<i))
#define setBit(x,i) (x|(1<<i))
#define mem(A,N) memset(A,N,sizeof(A))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define rep(i, x, y)  for(int i = x; i < y; ++i)
#define Rep(i, x, y)  for(int i = x; i <= y; ++i)
#define srep(i, x, y) for(int i = x; i > y; --i)
#define sRep(i, x, y) for(int i = x; i >= y; --i)
#define all(v) v.begin(),v.end()
#define Sort(x) sort(x.begin(), x.end())
#define f1(i,n) for (int i=1; i<=n; i++)
#define f0(i,n) for (int i=0; i<n; i++)
#define sz(x) ((int)(x).size())
#define len(x) ((int)(x).length())
#define fi first
#define se second

#define linf 0x7FFFFFFFFFFFFFFF
#define inf 0x7FFFFFFF
#define mod 1000000007
#define eps 1e-9
#define Pi 3.14159265358979323846

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define write(x) cerr << #x << " = " << (x) << ", ";
#define writeI0 cerr << "i = " << 0 << ", ";
#define dumpI0 cerr << "i = " << 0 << endl;
#define printSet(s) for(auto it = s.begin(); it!=s.end(); it++) (it==s.begin())? cout << *it : cout << ' ' << *it; cout << endl;
#define printPairSet(s) for(auto it = s.begin(); it!=s.end(); it++) (it==s.begin())? cout << '(' << it->first << ", " << it->second << ')' : cout << " (" << it->first << ", " << it->second << ')'; cout << endl;
#define printVector(x) for(auto it = x.begin(); it!=x.end(); it++) (it==x.begin())? cout << *it : cout << ' ' << *it; cout << endl;
#define printArray(a,x,y) for(auto it = a+x; it != a+y; it++) (it==a+x)? cout << *it : cout << ' ' << *it; cout << endl;
#define PrintArray(a,x,y) for(auto it = a+x; it != a+y+1; it++) (it==a+x)? cout << *it : cout << ' ' << *it; cout << endl;

// ***************** End Of Template ********************

int a[100000];
int n, m, v, p;
ll pre[100000];

ll sum(int i, int j) {
    if (i > 0)
        return pre[j] - pre[i-1];
    return pre[j];
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> n >> m >> v >> p;

    rep(i, 0, n) {
        cin >> a[i];
    }

    sort(a, a+n);

    pre[0] = a[0];
    rep(i, 1, n) {
        pre[i] = pre[i-1] + a[i];
    }

    int ans = 0;

    rep(i, 0, n) {
        int lastSame = upper_bound(a+i+1, a+n, a[i]) - a - 1;
        if (n-lastSame <= p) {
            ans++;
            continue;
        }
        int t = max(0, v - (lastSame+1)); // v is at least 1 in the Problem, so we greedily vote the current Problem i and any small or equal Problem

        if (!t) {
            int movedTo = upper_bound(a+lastSame+1, a+n, a[i]+m) - a - 1;
            if (n-movedTo <= p)
                ans++;
        }
        // The case where t*m Votes remain after voting for (v-t) problem each by the m Judges
        else {
            int movedTo = upper_bound(a+lastSame+1, a+n, a[i]+m) - a - 1;
            int rank = n-movedTo;

            if (movedTo == lastSame) {
                if (rank <= p)
                    ans++;
            }
            else {
                t -= n - (movedTo+1);
                if (t <= 0) {
                    if (rank <= p)
                        ans++;
                }
                else {
                    int range = movedTo - lastSame;
                    ll s = sum(lastSame+1, movedTo);
                    ll target = (ll) (a[i]+m) * range - s;
                    ll votingPower = (ll) t * m;
                    if (target >= votingPower) {
                        ans++;
                    }
                    else if (min(range, (votingPower - target + m-1) / m) + rank <= p) {
                        ans++;
                    }
                }
            }
        }
    }

    cout << ans << '\n';

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:117:33: error: no matching function for call to 'min(int&, ll)'
  117 |                     else if (min(range, (votingPower - target + m-1) / m) + rank <= p) {
      |                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:117:33: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  117 |                     else if (min(range, (votingPower - target + m-1) / m) + rank <= p) {
      |                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:117:33: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  117 |                     else if (min(range, (votingPower - target + m-1) / m) + rank <= p) {
      |                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~