Problem:
Score : 600 points

Problem Statement

You are given an integer sequence A of length N and an integer K.
You will perform the following operation on this sequence Q times:

Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).

Let X and Y be the values of the largest and smallest element removed in the Q operations. You would like X-Y to be as small as possible.
Find the smallest possible value of X-Y when the Q operations are performed optimally.

Constraints

1 \leq N \leq 2000

1 \leq K \leq N

1 \leq Q \leq N-K+1

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 ... A_N

Output

Print the smallest possible value of X-Y.

Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

In the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1.
Thus, the first operation removes A_3=1 and now we have A=(4,3,5,2).
In the second operation, it is optimal to choose (A_2,A_3,A_4)=(3,5,2) as the contiguous subsequence of length 3 and remove A_4=2.
In this case, the largest element removed is 2, and the smallest is 1, so their difference is 2-1=1.

Sample Input 2

10 1 6
1 1 2 3 5 8 13 21 34 55

Sample Output 2

7

Sample Input 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

Sample Output 3

451211184

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}


const int X = 2010;

//head

int n, k, q;
int a[X];
map<int, vi> mp;
int ans = INF*10;
set<P> se;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k >> q;
  rep(i, n) cin >> a[i];

  rep(i, n) mp[a[i]].emplace_back(i);
  se.emplace(0, n);
  for(auto &ne: mp) {
    vi &v = ne.second;
    vi H;
    for(P p:se) {
      multiset<int> ss;
      for(int i = p.first; i < p.second; i++) ss.emplace(a[i]);
      while(ss.size() != k-1) {
        H.emplace_back(*ss.begin());
        ss.erase(ss.begin());
      }
    }
    if(H.size() < q) break;
    sort(all(H));
    chmin(ans, H[q-1]-H[0]);
    rep(i, v.size()) {
      auto itr = se.lower_bound(P(v[i]+1, 0));
      if(itr == se.begin()) continue;
      itr = prev(itr);
      P U = *itr;
      if(U.first > v[i] or U.second < v[i]) continue;
      se.erase(itr);
      if(U.second-v[i] > k) se.emplace(v[i]+1, U.second);
      if(v[i]-U.first >= k) se.emplace(U.first, v[i]);
    }
  }

  cout << ans << endl;
}