stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.238868242637032x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 5.148461626082201x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.1010794702486184x):
#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);

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.1273784902480863x):
#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);

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.001095827514427x):
#include <bits/stdc++.h>

using namespace std;



const int N = 2005;



int a[N], b[N], c[N];



int main(){

	int n, k, q, last, cnt, ans;

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

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

	ans = 2e9;

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

		for (int j = 1; j <= n; j++) b[j] = a[j];

		cnt = 0; last = 0;

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

			if (b[j] < a[i]){

				if (j - last - 1 >= k){

					sort(b + last + 1, b + j);

					for (int p = last + 1; p <= j - k; p++) c[++cnt] = b[p];

				}

				last = j;

			}

		}

		if (n - last >= k){

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

			for (int p = last + 1; p <= n + 1 - k; p++) c[++cnt] = b[p];

		}

		if (cnt >= q){

			sort(c + 1, c + cnt + 1);

			ans = min(ans, c[q] - a[i]);

		}

	}

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

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.0042795362627808x):
#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

const int p=2005;

int n,m,q,a[p],w[p],ans;

int hs(int i,int mid)

{

	int s=0,len=0,sum=0;

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

	if(a[j]<w[i])

	sum+=min(s,max(0,len-m+1)),s=0,len=0;

	else if(a[j]>w[mid])++len;

	else ++len,++s;

	return sum+min(s,max(0,len-m+1));

}

int main()

{

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

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

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

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

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

//for(int i=1;i<=n;++i)printf("%d ",w[i]);printf("\n");

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

{

//printf("i %d\n",i);

	if(hs(i,n)<q)break;

	int l=i+q-1,r=n;

	while(l<r)

	{

		int mid=(l+r)/2;

		if(hs(i,mid)<q)l=mid+1;

		else r=mid;

	}

//printf("l %d\n",l);

	ans=min(ans,w[l]-w[i]);

}

	printf("%d",ans);

	return 0;

}

/*

>=w[i],<=w[mid]

*/
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.7047469874750285x):
#include<bits/stdc++.h>

#include<vector>

#include<queue>

#include<stack>

#include<string>

#define INF 0x3f3f3f3f

using namespace std;

inline int read(){

    int w=0,f=1;

    char ch=getchar();

    while(ch<'0'||ch>'9'){

        if(ch=='-')f=-1;

        ch=getchar();

    }

    while(ch>='0'&&ch<='9'){

        w=(w<<3)+(w<<1)+ch-48;

        ch=getchar();

    }

    return w*f;

}

int n,m,q,a[200010],b[200010],ans,vec[20010];

priority_queue<int> pq;

inline void work(){

    while(!pq.empty()){

        if(pq.size()>=m){

            vec[0]++;vec[vec[0]]=-pq.top();

        }

        pq.pop();

    }

}

signed main(){

    n=read();m=read();q=read();ans=INF;

    for(int i=1;i<=n;i++)a[i]=read();

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

        int now=a[i];

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

            if(a[j]>=now) pq.push(-a[j]);

            else work();

        }

        work();

        if(vec[0]>=1){

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

        }

        if(vec[0]>=q&&vec[1]==now){

            ans=min(ans,vec[q]-now);

        }

        memset(vec,0,sizeof(vec));

    }

    cout<<ans<<endl;

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.0002573328692836x):
#include<bits/stdc++.h>

using namespace std;

int n,k,q,a[2005],ans=0x7fffffff,t[2005],b[2005],c[2005];

inline int min(int x,int y){

	return x<y?x:y;

}

void solve(int mn){

    int l=1,r=1,tot=0;

    while(l<=n){

        int cnt=0;

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

        r=l;

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

        for(int i=l;i<=n&&i<r;i++)

            c[++cnt]=a[i];

        if(cnt>=k){

            sort(c+1,c+cnt+1);

            for(int i=1;i<=cnt-k+1;i++)

                b[++tot]=c[i];

        }

        l=r;

    }

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

    if(tot>=q) ans=min(ans,b[q]-b[1]);

}

int main(){

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

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

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

        t[i]=a[i];

    }

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

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

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

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#include<vector>

#include<queue>

#include<stack>

#include<string>

#define INF 0x3f3f3f3f

using namespace std;

inline int read(){

    int w=0,f=1;

    char ch=getchar();

    while(ch<'0'||ch>'9'){

        if(ch=='-')f=-1;

        ch=getchar();

    }

    while(ch>='0'&&ch<='9'){

        w=(w<<3)+(w<<1)+ch-48;

        ch=getchar();

    }

    return w*f;

}

int n,m,q,a[200010],b[200010],ans,vec[20010];

priority_queue<int> pq;

inline void work(){

    while(!pq.empty()){

        if(pq.size()>=m){

            vec[0]++;vec[vec[0]]=-pq.top();

        }

        pq.pop();

    }

}

signed main(){

    n=read();m=read();q=read();ans=INF;

    for(int i=1;i<=n;i++)a[i]=read();

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

        int now=a[i];

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

            if(a[j]>=now) pq.push(-a[j]);

            else work();

        }

        work();

        if(vec[0]>=1){

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

        }

        if(vec[0]>=q&&vec[1]==now){

            ans=min(ans,vec[q]-now);

        }

        memset(vec,0,sizeof(vec));

    }

    cout<<ans<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1733455741910113x):
#include<bits/stdc++.h>

using namespace std;

int n,k,q,a[2005],ans=0x7fffffff,t[2005],b[2005],c[2005];

inline int min(int x,int y){

	return x<y?x:y;

}

void solve(int mn){

    int l=1,r=1,tot=0;

    while(l<=n){

        int cnt=0;

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

        r=l;

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

        for(int i=l;i<=n&&i<r;i++)

            c[++cnt]=a[i];

        if(cnt>=k){

            sort(c+1,c+cnt+1);

            for(int i=1;i<=cnt-k+1;i++)

                b[++tot]=c[i];

        }

        l=r;

    }

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

    if(tot>=q) ans=min(ans,b[q]-b[1]);

}

int main(){

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

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

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

        t[i]=a[i];

    }

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

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

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

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.932862227254709x):
#include<bits/stdc++.h>

using namespace std;

int n,k,q,tag[2005],a[2005],ans=2e9;

pair<int,int>lis[2005];

int solve()

{

	vector<int>vec,cnt,vec2,vec3;

	for(int i=0;i<=n+1;++i)if(tag[i])vec.push_back(i);

	int tot=0,mi=2e9;

	for(int i=0;i<vec.size()-1;++i)tot+=max(0,vec[i+1]-vec[i]-k),cnt.push_back(max(0,vec[i+1]-vec[i]-k));

	if(tot<q)return 1;

	for(int i=0;i<cnt.size();++i)

	{

		if(cnt[i])

		{

			vec2.clear();

			for(int j=vec[i]+1;j<vec[i+1];++j)mi=min(mi,a[j]),vec2.push_back(a[j]);sort(vec2.begin(),vec2.end());

			for(int j=0;j<cnt[i];++j)vec3.push_back(vec2[j]);

		}

	}

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

	//for(int i=0;i<vec3.size();++i)cout<<vec3[i]<<" ";cout<<endl;

	ans=min(ans,vec3[q-1]-vec3[0]);

	return 0;

}

int main()

{

	ios::sync_with_stdio(0);cin.tie(0);

	cin>>n>>k>>q;for(int i=1;i<=n;++i)cin>>a[i];

	for(int i=1;i<=n;++i)lis[i]={a[i],i};

	sort(lis+1,lis+1+n);tag[0]=tag[n+1]=1;

	ans=lis[q].first-lis[1].first;

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

	{

		//cout<<i<<" "<<ans<<endl;

		tag[lis[i].second]=1;

		if(solve())break;

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#include<vector>

#include<queue>

#include<stack>

#include<string>

#define INF 0x3f3f3f3f

using namespace std;

inline int read(){

    int w=0,f=1;

    char ch=getchar();

    while(ch<'0'||ch>'9'){

        if(ch=='-')f=-1;

        ch=getchar();

    }

    while(ch>='0'&&ch<='9'){

        w=(w<<3)+(w<<1)+ch-48;

        ch=getchar();

    }

    return w*f;

}

int n,m,q,a[200010],b[200010],ans,vec[20010];

priority_queue<int> pq;

inline void work(){

    while(!pq.empty()){

        if(pq.size()>=m){

            vec[0]++;vec[vec[0]]=-pq.top();

        }

        pq.pop();

    }

}

signed main(){

    n=read();m=read();q=read();ans=INF;

    for(int i=1;i<=n;i++)a[i]=read();

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

        int now=a[i];

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

            if(a[j]>=now) pq.push(-a[j]);

            else work();

        }

        work();

        if(vec[0]>=1){

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

        }

        if(vec[0]>=q&&vec[1]==now){

            ans=min(ans,vec[q]-now);

        }

        memset(vec,0,sizeof(vec));

    }

    cout<<ans<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1338117864150337x):
#include<bits/stdc++.h>

using namespace std;

int n,k,q,tag[2005],a[2005],ans=2e9;

pair<int,int>lis[2005];

int solve()

{

	vector<int>vec,cnt,vec2,vec3;

	for(int i=0;i<=n+1;++i)if(tag[i])vec.push_back(i);

	int tot=0,mi=2e9;

	for(int i=0;i<vec.size()-1;++i)tot+=max(0,vec[i+1]-vec[i]-k),cnt.push_back(max(0,vec[i+1]-vec[i]-k));

	if(tot<q)return 1;

	for(int i=0;i<cnt.size();++i)

	{

		if(cnt[i])

		{

			vec2.clear();

			for(int j=vec[i]+1;j<vec[i+1];++j)mi=min(mi,a[j]),vec2.push_back(a[j]);sort(vec2.begin(),vec2.end());

			for(int j=0;j<cnt[i];++j)vec3.push_back(vec2[j]);

		}

	}

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

	//for(int i=0;i<vec3.size();++i)cout<<vec3[i]<<" ";cout<<endl;

	ans=min(ans,vec3[q-1]-vec3[0]);

	return 0;

}

int main()

{

	ios::sync_with_stdio(0);cin.tie(0);

	cin>>n>>k>>q;for(int i=1;i<=n;++i)cin>>a[i];

	for(int i=1;i<=n;++i)lis[i]={a[i],i};

	sort(lis+1,lis+1+n);tag[0]=tag[n+1]=1;

	ans=lis[q].first-lis[1].first;

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

	{

		//cout<<i<<" "<<ans<<endl;

		tag[lis[i].second]=1;

		if(solve())break;

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.008897012524884x):
#include <cstdio>

#include <set>

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <algorithm>

#include <cstring>

#include <queue>

#include <map>

#include<numeric>

#include <list>

#define fo(a, b, c) for (register int a = b; a <= c; ++a)

 

namespace IO

{

    inline char gc()

    {

        static char s[1<<20|1]={0},*p1=s,*p2=s;

        return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);

    }

//	inline char gc() { return getchar(); }

	inline long long read()

	{

		long long ret=0;bool flag=0;char c=gc();

		while ((c<'0')|(c>'9')) flag ^= !(c^'-'),c=gc();

		while ((c>='0')&(c<='9')) ret=(ret<<1)+(ret<<3)+(c^'0'),c=gc();

		return flag?-ret:ret;

 	}

 	char OutputAns[1<<20|1],*OutputCur = OutputAns;

 	inline void output()

 	{

 		OutputCur -= fwrite(OutputAns,1,OutputCur - OutputAns,stdout);

	}

	inline void print(long long ans)

	{

		char s[20]={0};

		if (OutputCur - OutputAns + sprintf(s,"I64d",ans) >> 20) output();

		OutputCur += sprintf(OutputCur,"%I64d",ans);

	}

	inline void printc(char c)

	{

		if (OutputCur - OutputAns + 1 >> 20) output();

		*(OutputCur++) = c;

	}

}

 

using IO::read;

using IO::print;

using IO::printc;

using IO::output;

 

using namespace std;

 

typedef long long int64;

 

const int M = 2e3 + 11;

 

int lim, n, q, ans = 2e9;



int A[M], R[M], Ok[M], C[M];



inline void Input()

{

	n = read(), lim = read(), q = read();

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

		A[i] = R[i] = read();

	std::sort(R + 1, R + 1 + n);

}



inline void Solve(int Min)

{

	Ok[0] = 0;

	int l = 1, r = 1;

	while (l <= n)

	{

		C[0] = 0;

		while (A[l] < Min && l <= n) ++l;

		r = l;

		while (A[r] >= Min && r <= n) ++r;

		if (r - l < lim) goto end;

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

			C[++C[0]] = A[i];

		std::sort(C + 1, C + 1 + C[0]);

		for (int i = 1;i <= C[0] - lim + 1; ++i)

			Ok[++Ok[0]] = C[i];

		end:; l = r;

	}

	if (Ok[0] < q) return ;

	std::sort(Ok + 1, Ok + 1 + Ok[0]);

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

}



int main(void)

{

	Input();

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

		Solve(R[i]);

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

	return 0;

}