stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <queue>

#include <algorithm>

using namespace std;

int ans;

int n, k, q, a[2010], ls[2010];

priority_queue <int, vector <int>, greater <int> > q1, q2;

int main () {

	scanf ("%d%d%d", &n, &k, &q);

	for (int i = 1; i <= n; i++) scanf ("%d", &a[i]), ls[i] = a[i];

	sort (ls + 1, ls + n + 1);

	ans = ls[n] - ls[1];

	for (int p = 1; p <= n; p++) {

		for (int i = 1; i <= n; i++) if (a[i] >= ls[p]) {

			int j = i; while (j < n && a[j + 1] >= ls[p]) j++;

			if (j - i + 1 < k) { i = j; continue; }

			for (int x = i; x <= j; x++) q1.push (a[x]);

			for (int x = j - i + 1; x >= k; x--) q2.push (q1.top ()), q1.pop ();

			while (!q1.empty ()) q1.pop ();

			i = j;

		}

		int mx = 0; bool ok = true;

		for (int i = 1; i <= q; i++) {

			if (q2.empty ()) { ok = false; break; }

			mx = q2.top (), q2.pop ();

		}

		while (!q2.empty ()) q2.pop ();

		if (ok) ans = min (ans, mx - ls[p]);

	}

	printf ("%d\n", ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstdio>

#define N 2005

using namespace std;

int a[N],l[N],n,o,q,r[N],s[N],t,x;bool w[N],z[N];

inline bool cmp(int u,int v){return a[u]<a[v];}

int main()

{

	scanf("%d%d%d",&n,&o,&q),x=0x7fffffff,w[n+1]=true;

	for(int i=1;i<=n;i++)scanf("%d",a+i),r[i]=n+1;

	for(int i=1,j,k,u;i<=n;i++)

	{

		u=t=0,j=1;

		while(w[j])j++;

		while(j<=n)

		{

			k=j;

			while(!w[k])s[++t]=k++;

			if(k-j<o)t-=k-j;

			else sort(s+t+1-k+j,s+t+1,cmp),t-=o-1;

			j=k;

			while(w[j])j++;

		}

		if(t<q)break;

		sort(s+1,s+t+1,cmp);

		if(a[s[q]]-a[s[1]]<x)x=a[s[q]]-a[s[1]];

		w[s[1]]=true;

	}

	printf("%d\n",x);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<cstring>

#include<climits>

#include<queue>

#include<vector>

#include<map>

#include<algorithm>

using namespace std;

const int inf = INT_MAX;

int n, k, q;

int A[2005], sorted_A[2005], temp[2005];

int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	cin >> n >> k >> q;

	int ans = inf;

	for (int i = 0; i < n; i++)

	{

		cin >> A[i];

		sorted_A[i] = A[i];

	}

	sort(sorted_A, sorted_A + n);

	for (int i = 0; i < n; i++)

	{

		int begin = 0, end = 0;

		temp[0] = 0;

		while (begin <= end)

		{

			while (A[begin] < sorted_A[i] && begin<n)

			{

				begin++;

			}

			end = begin;

			while (A[end] >= sorted_A[i] && end<n)

			{

				end++;

			}

			if (end - begin >= k) {

				int *tmp = new int[end - begin + 1];

				for (int j = 0; j < end - begin; j++)

					tmp[j] = A[begin + j];

				sort(tmp, tmp + end - begin);

				for (int j = 0; j <= end - begin - k; j++)

					temp[++temp[0]] = tmp[j];

				delete[] tmp;

			}

			begin = end;

			if (end == n)break;

		}

		if (temp[0] >= q) {

			sort(temp + 1, temp + 1 + temp[0]);

			ans = min(ans, temp[q] - sorted_A[i]);

		}

	}

	cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstdio>

#define N 2005

using namespace std;

int a[N],l[N],n,o,q,r[N],s[N],t,x;bool w[N],z[N];

inline bool cmp(int u,int v){return a[u]<a[v];}

int main()

{

	scanf("%d%d%d",&n,&o,&q),x=0x7fffffff,w[n+1]=true;

	for(int i=1;i<=n;i++)scanf("%d",a+i),r[i]=n+1;

	for(int i=1,j,k,u;i<=n;i++)

	{

		u=t=0,j=1;

		while(w[j])j++;

		while(j<=n)

		{

			k=j;

			while(!w[k])s[++t]=k++;

			if(k-j<o)t-=k-j;

			else sort(s+t+1-k+j,s+t+1,cmp),t-=o-1;

			j=k;

			while(w[j])j++;

		}

		if(t<q)break;

		sort(s+1,s+t+1,cmp);

		if(a[s[q]]-a[s[1]]<x)x=a[s[q]]-a[s[1]];

		w[s[1]]=true;

	}

	printf("%d\n",x);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define fi first

#define se second

#define LL long long



using namespace std;

const int maxn = 2e5 + 5;

typedef pair<int, int> P;

int a[maxn];

int n, k, q, ans;

bool check(int o, int p) {

    int ti = 0;

    int x = 0;

    int y = 0;

    for(int i = 0; i <= n; ++i) {

        if(a[i] >= o) {

            if(a[i] <= o + p) {

                x ++;

            } else {

                y ++;

            }

        } else {

            if(y >= k - 1) {

                ti += x;

            } else {

                ti += max(0, x - (k - 1 - y));

            }

            x = 0;

            y = 0;

        }

    }

    return ti >= q;

}

int main() {

#ifdef TT

    freopen("xxx.in", "r", stdin);

#endif // TT

    scanf("%d%d%d", &n, &k, &q);

    for(int i = 0; i < n; ++i) {

        scanf("%d", &a[i]);

    }

    a[n] = -1;

    ans = 1e9 + 7;

    for(int i = 0; i < n; ++i) {

        int L = 0, R = 1e9 + 7;

        while(L < R) {

            int mid = (L + R) >> 1;

            if(check(a[i], mid))

                R = mid;

            else

                L = mid + 1;

        }

        ans = min(ans, L);

    }

    cout << ans << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstdio>

#define N 2005

using namespace std;

int a[N],l[N],n,o,q,r[N],s[N],t,x;bool w[N],z[N];

inline bool cmp(int u,int v){return a[u]<a[v];}

int main()

{

	scanf("%d%d%d",&n,&o,&q),x=0x7fffffff,w[n+1]=true;

	for(int i=1;i<=n;i++)scanf("%d",a+i),r[i]=n+1;

	for(int i=1,j,k,u;i<=n;i++)

	{

		u=t=0,j=1;

		while(w[j])j++;

		while(j<=n)

		{

			k=j;

			while(!w[k])s[++t]=k++;

			if(k-j<o)t-=k-j;

			else sort(s+t+1-k+j,s+t+1,cmp),t-=o-1;

			j=k;

			while(w[j])j++;

		}

		if(t<q)break;

		sort(s+1,s+t+1,cmp);

		if(a[s[q]]-a[s[1]]<x)x=a[s[q]]-a[s[1]];

		w[s[1]]=true;

	}

	printf("%d\n",x);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

//#include<string>

using namespace std;

int n,k,q,y,a[2001],b[2001],t,ll,s[2001],ss,ans=2e9;

bool cmp(int a,int b){

  return a<b;

}

int main(){

  scanf("%d %d %d",&n,&k,&q);

  for(register int i=1;i<=n;i++)

    scanf("%d",&a[i]);

  for(register int j=1;j<=n;j++){

  	y=a[j];//枚举能被删除的最小元素y

  	memset(s,0,sizeof(s));

  	ss=0;

	t=0;

	for(register int i=1;i<=n+1;i++){

  	   if(i==n+1||a[i]<y){//如果a[i]为一段的边界

		 sort(b+1,b+t+1,cmp);

		 ll=t-k+1;//当前这段能删的数的个数 

		 for(register int l=1;l<=ll;l++)

		    s[++ss]=b[l];//把能删的数存在s数组里

		 memset(b,0,sizeof(b));

		 t=0;//清空b数组 

	   }

	   else{

	   	 b[++t]=a[i];//不是边界就加入到当前这段 

	   }

	}

	if(ss<q){//如果能被删的数不足q个，就直接枚举下个y 

	  continue;

	}

	  sort(s+1,s+ss+1,cmp);

	  //printf("%d %d\n",s[q],y);

	  ans=min(ans,s[q]-y);//s[q]就是x,这里是更新ans

  }

  printf("%d\n",ans);

  return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

#include<set>

#define MN 50005

using namespace std;

int n,m,k,val[MN],c[MN][2],id[MN],fa[MN],ht[MN],sz[MN],ans=0x3f3f3f3f;bool vis[MN];

bool cmp(int x,int y){return val[x]>val[y];}

multiset<int> s;

multiset<int>::iterator it;

int merge(int x,int y){

	if((!x)||(!y))return x+y;

	if(val[x]>val[y])swap(x,y);

	c[x][1]=merge(c[x][1],y);

	if(ht[c[x][0]]<ht[c[x][1]])swap(c[x][0],c[x][1]);

	ht[x]=ht[c[x][1]]+1;sz[x]=sz[c[x][0]]+sz[c[x][1]]+1;return x;

}

int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}

int main(){

	scanf("%d%d%d",&n,&m,&k);int x,y,tmp;

	for(int i=1;i<=n;i++)scanf("%d",&val[i]),id[i]=fa[i]=i,sz[i]=1,ht[i]=0;sort(id+1,id+n+1,cmp);

	for(int i=1;i<=n;i++){

		vis[id[i]]=1;

		if(vis[id[i]-1]){

			x=getfa(id[i]),y=getfa(id[i]-1);

			if(val[x]>val[y]&&sz[y])swap(x,y);if(sz[y])fa[y]=x;merge(x,y);

		}

		if(vis[id[i]+1]){

			x=getfa(id[i]),y=getfa(id[i]+1);

			if(val[x]>val[y]&&sz[y])swap(x,y);if(sz[y])fa[y]=x;merge(x,y);

		}tmp=getfa(id[i]);

		while(sz[tmp]>=m)s.insert(val[tmp]),x=merge(c[tmp][0],c[tmp][1]),fa[tmp]=x,fa[x]=x,tmp=x;

		while(s.size()>k)it=s.end(),--it,s.erase(it);

		if(s.size()==k)it=s.end(),--it,ans=min(ans,(*it)-val[id[i]]);

	}printf("%d\n",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

const int N=2000;

int a[N+1];

int main(){

  int n,k,q;

  scanf("%d%d%d",&n,&k,&q); 

  for (int i=0;i<n;i++)

    scanf("%d",&a[i]);

  int ans=1e9;

  for (int& bound:a) {

    vector<int>t;

    vector<int>s;

    for (int i=0;i<=n;i++){

      if (i==n||a[i]<bound){

        sort(t.begin(),t.end());

        for (int i=0;i<=(int)t.size()-k;i++)

          s.push_back(t[i]);

        t.clear();

      }else t.push_back(a[i]);

    }

    if ((int)s.size()<q) continue;

    sort(s.begin(),s.end());

    ans=min(ans,s[q-1]-bound);

  }

  printf("%d\n",ans);

  return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

#include<set>

#define MN 50005

using namespace std;

int n,m,k,val[MN],c[MN][2],id[MN],fa[MN],ht[MN],sz[MN],ans=0x3f3f3f3f;bool vis[MN];

bool cmp(int x,int y){return val[x]>val[y];}

multiset<int> s;

multiset<int>::iterator it;

int merge(int x,int y){

	if((!x)||(!y))return x+y;

	if(val[x]>val[y])swap(x,y);

	c[x][1]=merge(c[x][1],y);

	if(ht[c[x][0]]<ht[c[x][1]])swap(c[x][0],c[x][1]);

	ht[x]=ht[c[x][1]]+1;sz[x]=sz[c[x][0]]+sz[c[x][1]]+1;return x;

}

int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}

int main(){

	scanf("%d%d%d",&n,&m,&k);int x,y,tmp;

	for(int i=1;i<=n;i++)scanf("%d",&val[i]),id[i]=fa[i]=i,sz[i]=1,ht[i]=0;sort(id+1,id+n+1,cmp);

	for(int i=1;i<=n;i++){

		vis[id[i]]=1;

		if(vis[id[i]-1]){

			x=getfa(id[i]),y=getfa(id[i]-1);

			if(val[x]>val[y]&&sz[y])swap(x,y);if(sz[y])fa[y]=x;merge(x,y);

		}

		if(vis[id[i]+1]){

			x=getfa(id[i]),y=getfa(id[i]+1);

			if(val[x]>val[y]&&sz[y])swap(x,y);if(sz[y])fa[y]=x;merge(x,y);

		}tmp=getfa(id[i]);

		while(sz[tmp]>=m)s.insert(val[tmp]),x=merge(c[tmp][0],c[tmp][1]),fa[tmp]=x,fa[x]=x,tmp=x;

		while(s.size()>k)it=s.end(),--it,s.erase(it);

		if(s.size()==k)it=s.end(),--it,ans=min(ans,(*it)-val[id[i]]);

	}printf("%d\n",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#include <iostream>

#include <algorithm>

#define R register

#define MN 2005

typedef long long ll;

namespace IO{

	inline int read(){

		R int x; R bool f; R char c;

		for (f=0; (c=getchar())<'0'||c>'9'; f=c=='-');

		for (x=c^'0'; (c=getchar())>='0'&&c<='9'; x=(x<<1)+(x<<3)+(c^'0'));

		return f?-x:x;

	}

}

namespace sol{

	int n,k,q,vl[MN],seq[MN],ans=0x3f3f3f3f,vn,tmp[MN];

	void solve(int mi){

		vn=0;for (R int l=1,r; l<=n; l=r){

			for (;seq[l]<mi&&l<=n;++l);

			for (r=l;seq[r]>=mi&&r<=n;++r);

			if (r-l>=k){

				for (R int i=l; i<r; ++i)

					tmp[i-l]=seq[i];

				std::sort(tmp,tmp+r-l);

				for (R int i=0; i<r-l-k+1; ++i)

					vl[++vn]=tmp[i];

			}

		}

		if (vn<q) return;

		std::sort(vl+1,vl+vn+1);

		ans=std::min(ans,vl[q]-vl[1]);

	}

	void main(){

		n=IO::read(),k=IO::read(),q=IO::read();

		for (R int i=1; i<=n; ++i) seq[i]=IO::read();

		for (R int i=1; i<=n; ++i) solve(seq[i]);

		printf("%d\n",ans);

	}

}

int main(){sol::main();return 0;}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int n,k,q,mi;

int a[2010],b[2010],ans[2001],cn,Ans=2147483647;

int main(){

	scanf("%d%d%d",&n,&k,&q);

	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

	for(int i=1;i<=n;i++){

		mi=a[i];

		int l=1,r=1;

		while(l<=n){

			while(a[l]<mi&&l<n) l++;

			r=l;

			while(a[r+1]>=mi&&r<n) r++;

			if(r-l+1>=k){

				for(int j=l;j<=r;j++) b[j]=a[j];

				std::sort(b+l,b+r+1);

				for(int j=l;j<=r-k+1;j++) ans[++cn]=b[j];

			}

			l=r+1;

		}

		std::sort(ans+1,ans+cn+1);

		if(cn>=q) if(ans[q]-ans[1]<Ans) Ans=ans[q]-ans[1];

		cn=0;

	}

	printf("%d\n",Ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define F(i,a,b) for(int i=(a);i<=(b);++i)

#define F2(i,a,b) for(int i=(a);i<(b);++i)

#define dF(i,a,b) for(int i=(a);i>=(b);--i)

#define dF2(i,a,b) for(int i=(a);i>(b);--i)

#define dF3(i,a,b) for(int i=(a)-1;i>=(b);--i)

using namespace std;typedef long long ll;typedef double ld;int INF=0x3f3f3f3f;int INF2=0x7fffffff;ll LNF=0x3f3f3f3f3f3f3f3f;ll LNF2=0x7fffffffffffffff;



int n,k,q,Ans=INF;

int a[2001],p[2001],v[2001];

bool cmp(int i,int j){return a[i]<a[j];}

int siz[2001],bel[2001];

int check(){

	int S=0;

	F(i,1,n) bel[i]=0;

	F(i,1,n){

		if(!v[i]){

			if(v[i-1]) ++S, siz[S]=1;

			else ++siz[S];

			bel[i]=S;

		}

	}

	int cnt=0;

	int Amn=INF, Amx=-INF;

	F(i,1,n) if(!v[p[i]]){

		int pos=p[i];

		if(siz[bel[pos]]>=k) Amn=min(Amn,a[pos]), Amx=max(Amx,a[pos]), --siz[bel[pos]], ++cnt;

		if(cnt==q) break;

	}

	if(cnt!=q) return INF;

	else return Amx-Amn;

}

int main(){

	scanf("%d%d%d",&n,&k,&q);

	F(i,1,n) scanf("%d",a+i), p[i]=i;

	sort(p+1,p+n+1,cmp);

	v[0]=1;

	Ans=check();

	F(i,1,n){

		v[p[i]]=1;

		Ans=min(Ans,check());

	} printf("%d",Ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int n,k,q,mi;

int a[2010],b[2010],ans[2001],cn,Ans=2147483647;

int main(){

	scanf("%d%d%d",&n,&k,&q);

	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

	for(int i=1;i<=n;i++){

		mi=a[i];

		int l=1,r=1;

		while(l<=n){

			while(a[l]<mi&&l<n) l++;

			r=l;

			while(a[r+1]>=mi&&r<n) r++;

			if(r-l+1>=k){

				for(int j=l;j<=r;j++) b[j]=a[j];

				std::sort(b+l,b+r+1);

				for(int j=l;j<=r-k+1;j++) ans[++cn]=b[j];

			}

			l=r+1;

		}

		std::sort(ans+1,ans+cn+1);

		if(cn>=q) if(ans[q]-ans[1]<Ans) Ans=ans[q]-ans[1];

		cn=0;

	}

	printf("%d\n",Ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <iostream>

#include <cstdio>

#include <queue>



using namespace std ;



typedef long long LL ;



priority_queue<LL,deque<LL>,greater<LL> > h ;

priority_queue<LL,deque<LL>,greater<LL> > t ;

LL v[2333];

LL a[2333];

LL b[2333];

LL n , k , q ;

LL res;



int main ()

{

	LL i , j ;

	LL x , s ;

	res=9223372036854775807;

	scanf("%lld%lld%lld",&n,&k,&q);

	for ( i=1 ; i<=n ; i++ ) scanf("%lld",a+i),b[i]=a[i];

	sort(b+1,b+n+1);

	for ( s=1 ; s<=n ; s++ ) 

	{

		fill(v+1,v+n+1,0);

		while ( !h.empty() ) h.pop();

		x=b[s];

		LL m=0;

		for ( i=1 ; i<=n ; i++ )

			if ( a[i]<x ) v[++m]=i;

		v[++m]=n+1;

		for ( i=1 ; i<=m ; i++ )

		{

			if ( v[i]-v[i-1]-1<k ) continue;

			for ( j=v[i-1]+1 ; j<v[i] ; j++ ) t.push(a[j]);

			LL tmp=v[i]-v[i-1]-k;

			for ( j=1 ; j<=tmp ; j++ ) h.push(t.top()),t.pop();

			while ( !t.empty() ) t.pop();

		}

		for ( i=1 ; i<q ; i++ )	

		{

			if ( h.empty() ) goto end;

			h.pop();

		}

		if ( h.empty() ) goto end;

		res=min(res,h.top()-x);

		end:;

	}

	printf("%lld\n",res);

	return 0 ;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

int n,k,q,mi;

int a[2010],b[2010],ans[2001],cn,Ans=2147483647;

int main(){

	scanf("%d%d%d",&n,&k,&q);

	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

	for(int i=1;i<=n;i++){

		mi=a[i];

		int l=1,r=1;

		while(l<=n){

			while(a[l]<mi&&l<n) l++;

			r=l;

			while(a[r+1]>=mi&&r<n) r++;

			if(r-l+1>=k){

				for(int j=l;j<=r;j++) b[j]=a[j];

				std::sort(b+l,b+r+1);

				for(int j=l;j<=r-k+1;j++) ans[++cn]=b[j];

			}

			l=r+1;

		}

		std::sort(ans+1,ans+cn+1);

		if(cn>=q) if(ans[q]-ans[1]<Ans) Ans=ans[q]-ans[1];

		cn=0;

	}

	printf("%d\n",Ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

using namespace std;

int main() {

	int n,m,q,a[2005];

	scanf("%d%d%d",&n,&m,&q);

	for (register int i=1;i<=n;++i) {

		scanf("%d",a+i);

	}

	int l=0,r=2000000000;

	while (l<r) {

		int mid=(l+r)>>1;

		bool ok=0;

		for (register int i=1;i<=n;++i) {

			int p=a[i],k=0,tmp=0,cnt=0;

			for (register int j=1;j<=n+1;++j) {

				if (a[j]<p||j==n+1) {

					tmp+=j-k-1<m?0:min(j-k-1-m+1,j-k-1-cnt);

					k=j;

					cnt=0;

				}

				else if (a[j]>p+mid) {

					++cnt;

				}

			}

			if (tmp>=q) {

				ok=1;

				break;

			}

		}

		ok?r=mid:l=mid+1;

	}

	printf("%d",l);

	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define mx 200005

inline int min(int x,int y){return x<y?x:y;}

inline int max(int x,int y){return x>y?x:y;}

int n,k,q,qz[mx],qp[mx],ans=2e9+5;

inline bool ck(int kl,int kr)

{

	int nl=0,nw=0,res=0;

	for(int i=1;i<=n;++i)

	{

		if(qz[i]>=kl&&qz[i]<=kr) ++nl,++nw;

		else if(qz[i]>kr) ++nl;

		else res+=max(0,min(nl-k+1,nw)),nl=nw=0;

	}

	res+=max(0,min(nl-k+1,nw)),nl=nw=0;

	return res>=q;

}

int main()

{

	scanf("%d%d%d",&n,&k,&q);

	for(int i=1;i<=n;++i)

		scanf("%d",&qz[i]),qp[i]=qz[i];

	sort(qp+1,qp+n+1);

	for(int i=1;i<=n;++i)

	{

		int l=i,r=n,mid;

		while(l<=r)

		{

			mid=(l+r)/2;

			if(ck(qp[i],qp[mid])) ans=min(ans,qp[mid]-qp[i]),r=mid-1;

			else l=mid+1;

		}

	}

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define fi first

#define se second

#define sz(x) ((int)x.size())

using namespace std;

typedef long long lli;

typedef pair<int,int> pii;



const int inf=1e9;

int n,k,q,arr[2000];

pii a[2000];

int get(int val) {

	int mx=0;

	priority_queue<int,vector<int>,greater<int>> aq,qq;

	for(int i=0;i<n;i++) if(arr[i]>=val) {

		int j=i;

		while(!aq.empty()) aq.pop();

		for(;j<n && arr[j]>=val;j++) aq.push(arr[j]);

		

		int s=sz(aq)-k+1;

		while(s>0) {

			qq.push(aq.top()); aq.pop();

			s--;

		}

		i=j-1;

	}

	if(sz(qq)<q) return inf;

	for(int i=0;i<q;i++) {

		mx=qq.top(); qq.pop();

	}

	return mx-val;

}

int main() {

	scanf("%d%d%d",&n,&k,&q);

	for(int i=0;i<n;i++) {

		scanf("%d",arr+i);

		a[i].fi=arr[i]; a[i].se=i;

	}

	sort(a,a+n);

	

	int ans=inf;

	for(int i=0;i<n;i++) if(i==0 || a[i].fi!=a[i-1].fi)

		ans=min(ans,get(a[i].fi));



	printf("%d\n",ans);

	

	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define mx 200005

inline int min(int x,int y){return x<y?x:y;}

inline int max(int x,int y){return x>y?x:y;}

int n,k,q,qz[mx],qp[mx],ans=2e9+5;

inline bool ck(int kl,int kr)

{

	int nl=0,nw=0,res=0;

	for(int i=1;i<=n;++i)

	{

		if(qz[i]>=kl&&qz[i]<=kr) ++nl,++nw;

		else if(qz[i]>kr) ++nl;

		else res+=max(0,min(nl-k+1,nw)),nl=nw=0;

	}

	res+=max(0,min(nl-k+1,nw)),nl=nw=0;

	return res>=q;

}

int main()

{

	scanf("%d%d%d",&n,&k,&q);

	for(int i=1;i<=n;++i)

		scanf("%d",&qz[i]),qp[i]=qz[i];

	sort(qp+1,qp+n+1);

	for(int i=1;i<=n;++i)

	{

		int l=i,r=n,mid;

		while(l<=r)

		{

			mid=(l+r)/2;

			if(ck(qp[i],qp[mid])) ans=min(ans,qp[mid]-qp[i]),r=mid-1;

			else l=mid+1;

		}

	}

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define len(x) (int) (x.size())

using namespace std;

typedef long long ll;



int main(){

    int n, k;

    string s;

    cin >> n >> k >> s;



    s[k-1] = s[k-1]+32;

    cout << s << '\n'; 

    

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>



int main(){

	int panjangkata,kecilin;

	scanf("%d %d", &panjangkata,&kecilin);

	char huruf[panjangkata+5];

	scanf("%s",huruf);

	huruf[kecilin-1] += 32;

	printf("%s",huruf);

	

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define ll long long

#define ii pair<int, int>

#define vi vector<int>

#define vl vector<ll>

#define pb push_back

#define eb emplace_back

#define mp make_pair

#define mt make_tuple

#define ff first

#define ss second

#define pi 3.14159265

#define oo 1000000007

#define loo 10000000000000007



#define prime 1000000007



#define W(x) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << "\n";

using namespace std;

    

vi gr[100100];

int ans[100100];

int visi[100100];

deque<int> d;

map<ii, int> m;



int main(){

    ios::sync_with_stdio(false);

    cin.tie(0); cout.tie(0);

    int n, k; cin >> n >> k;

    string s; cin >> s;

    k--;

    s[k] += 'a'-'A';   

    cout << s << endl;





    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>



int main(){

	int a , b;

	char c[100];

	scanf("%d %d %s", &a, &b, &c);

	getchar();

	c[b - 1] = c[b-1] + 32;

	printf("%s", c);

	

	

	

	

	

	

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



const int N = 2e5+5;



int main(){

    string s;

    int k;

    int n;

    cin >> n >> k >> s;

    s[k - 1] = s[k - 1] - 'A' + 'a';

    cout << s << endl;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>



int main(){

	int a , b;

	char c[100];

	scanf("%d %d %s", &a, &b, &c);

	getchar();

	c[b - 1] = c[b-1] + 32;

	printf("%s", c);

	

	

	

	

	

	

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



using ull=unsigned long long;

using vi=vector<int>;

using pii=pair<int, int>;

using ll=long long;



int main()

{

    int n, k;

    string s;



    cin >> n >> k >> s;



    s[k-1] = tolower(s[k-1]);



    cout << s << "\n"; 



    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>



int main(){

	int a , b;

	char c[100];

	scanf("%d %d %s", &a, &b, &c);

	getchar();

	c[b - 1] = c[b-1] + 32;

	printf("%s", c);

	

	

	

	

	

	

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string>



using namespace std;

int main(int argc, char *argv[])

{

	int n,k,i;

	string s;

	cin>>n>>k;

	cin>>s;

	s[k-1]=towlower(s[k-1]);

	cout<<s<<endl;

	

	



	return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>



int main(){

	int a , b;

	char c[100];

	scanf("%d %d %s", &a, &b, &c);

	getchar();

	c[b - 1] = c[b-1] + 32;

	printf("%s", c);

	

	

	

	

	

	

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


#include<bits/stdc++.h>

using namespace std;



typedef long long ll;

int main(){

  int n,k;

  string s;

  cin>>n>>k>>s;

  s[k-1]=s[k-1]+32;

  cout<<s;









}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>



int main(){

	int a , b;

	char c[100];

	scanf("%d %d %s", &a, &b, &c);

	getchar();

	c[b - 1] = c[b-1] + 32;

	printf("%s", c);

	

	

	

	

	

	

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
// Created by Bingoo on 7/8/2020.

#include"bits/stdc++.h"

using namespace std ;

#define ll  long long

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



template <class ...Args>

auto &see(const Args &...args) {return ((cerr << " " << args), ...) << '\n';}

#define bug(...)  cerr << "[ "#__VA_ARGS__" ] = "; see(__VA_ARGS__) << '\n';

//C++17

template<typename... Args>

void read(Args&... args){((cin >> args), ...);}

template<typename... Args>

void write(Args... args){((cout << args << " "), ...);}



ll mod = 1e6 +3 ;

ll power(ll a, ll b)	//a is base, b is exponent

{

    if (b == 0)

        return 1;

    if (b == 1)

        return a;

    if (b % 2 == 1)

        return (power(a, b - 1) * a) % mod;

    ll q = power(a, b / 2);

    return (q * q) % mod;

}



bool comp(pair<int,int>&a ,pair<int,int>&b)

{

    return a.second > b.second ;

}



string s;

int k ,n ;



int main(){

    cin >> n >> k >> s ;



   s[k-1]=tolower(s[k-1]);



   cout << s << endl;





}






// Optimized solution to the same problem (faster):
#include<stdio.h>



int main(){

	

	int panjangkata, lowercase;

	scanf("%d %d", &panjangkata, &lowercase);

	char kata[panjangkata + 3];

	scanf("%s", &kata);

	kata[lowercase - 1] += 32;

	printf("%s", kata);



	

	return 0;

}

// Fastest known solution for this problem: