Language: C++

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cassert>
#define MAXN 5010
#define int long long
const int INF = (~0ull) >> 1;
int n, k, q, a[MAXN], b[MAXN], ans = INF;
std::vector<std::pair<int, int>> V;

inline int calc(int l, int r, int max){
    int len = r - l + 1;
    int mc = len - k + 1;
    int ret = 0;
    for(int i = l;i <= r;i++)if(a[i] <= max)ret++;
    return std::max(std::min(ret, mc), 0);
}

inline bool judge(int min, int max){
    int L = -1, R = -1;
    V.clear();
    for(int i = 1;i <= n;i++){
        if(a[i] < min){
            if(~L)V.push_back({L, R});
            L = R = -1;
        }else{
            if(!~L)L = i;
            R = i;
        }
    }
    if(~L)V.push_back({L, R});
    int ret = 0;
    for(auto x : V){
        assert(x.first <= x.second);
        ret += calc(x.first, x.second, max);
    }
    return ret >= q;
}
signed main(){
    scanf("%lld%lld%lld", &n, &k, &q);
    for(int i = 1;i <= n;i++)scanf("%lld", &a[i]);
    memcpy(b, a, sizeof(a));
    std::sort(b + 1, b + n + 1);
    for(int i = 1;i <= n;i++){
        if(i + q - 1 > n)break;
        int l = i + q - 1, r = n, tmp = INF;
        while(l <= r){
            int mid = (l + r) >> 1;
            if(judge(b[i], b[mid])){
                tmp = b[mid] - b[i];
                r = mid - 1;
            }else{
                l = mid + 1;
            }
        }
        ans = std::min(ans, tmp);
    }
    printf("%lld\n", ans);
    return 0;
}

Compiler output:
a.cc: In function 'long long int calc(long long int, long long int, long long int)':
a.cc:18:20: error: no matching function for call to 'max(const long long int&, int)'
   18 |     return std::max(std::min(ret, mc), 0);
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:4:
/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:18:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   18 |     return std::max(std::min(ret, mc), 0);
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/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:18:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   18 |     return std::max(std::min(ret, mc), 0);
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~