stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

using namespace std;

int n;

ll a[20];



ll dfs(int l,int r,int lx,int rx){

	if(l+1==r) return 0;

	ll ans=1e18;

	for(int i=l+1;i<r;i++) ans=min(ans,dfs(l,i,lx,lx+rx)+dfs(i,r,lx+rx,rx)+1ll*(lx+rx)*a[i]);

	return ans;

}



int main(){

	scanf("%d",&n);

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

	printf("%lld\n",dfs(1,n,1,1)+a[1]+a[n]);



	return 0;

}

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

#include <algorithm>

#include <map>



typedef long long LL;

const int MN = 20;



int N, A[MN];



LL F(int l, int r, int cl, int cr) {

	if (l > r) return 0;

	LL ans = 0x3f3f3f3f3f3f3f3f;

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

		ans = std::min(ans, F(l, i - 1, cl, cl + cr) + (LL)A[i] * (cl + cr) + F(i + 1, r, cl + cr, cr));

	return ans;

}



int main() {

	scanf("%d", &N);

	for (int i = 0; i < N; ++i) scanf("%d", &A[i]);

	N -= 2;

	printf("%lld\n", F(1, N, 1, 1) + A[0] + A[N + 1]);

	return 0;

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

#define int long long

#define rep(i,n)for(int i=0;i<(n);i++)

using namespace std;



int base;

int tod(int b,int ka,int t,int w,int ki){

	ka+=b*20;t+=ka*20;w+=18*t;ki+=20*w;

	return base+ki;

}

bool is_uruu(int y){

	return y%4==0&&(y%100!=0||y%400==0);

}

int mn[]{31,28,31,30,31,30,31,31,30,31,30,31};

int calc(int y,int m,int d){

	int res=(y-1)*365+(y-1)/4;

	for(int i=100;i<y;i+=100){

		if(!is_uruu(i))res--;

	}

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

		res+=mn[i-1];if(is_uruu(y)&&i==2)res++;

	}

	return res+d;

}

int tod(int y,int m,int d){

	return calc(y,m,d)-base;

}

void tom(int day){

	int b,ka,t,w,ki;

	ki=day%20;day/=20;

	w=day%18;day/=18;

	t=day%20;day/=20;

	ka=day%20;day/=20;

	b=day%13;

	printf("%lld.%lld.%lld.%lld.%lld\n",b,ka,t,w,ki);

}

void tos(int day){

	int y,m;

	for(y=1;365+is_uruu(y)<day;y++)day-=365+is_uruu(y);

	for(m=1;mn[m-1]+(m==2&&is_uruu(y))<day;m++)day-=mn[m-1]+(m==2&&is_uruu(y));

	printf("%lld.%lld.%lld\n",y,m,day);

}

signed main() {

	base=calc(2012,12,21);

	string s;

	while(cin>>s,s!="#"){

		s+='.';

		vector<int>v;

		string t;

		for(char c:s){

			if(c=='.')v.push_back(stoi(t)),t="";

			else t+=c;

		}

		if(v.size()==3)tom(tod(v[0],v[1],v[2]));

		else tos(tod(v[0],v[1],v[2],v[3],v[4]));

	}

}

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

#define int long long

#define rep(i,n)for(int i=0;i<(n);i++)

using namespace std;



int base;

int tod(int b,int ka,int t,int w,int ki){

	ka+=b*20;t+=ka*20;w+=18*t;ki+=20*w;

	return base+ki;

}

bool is_uruu(int y){

	return y%4==0&&(y%100!=0||y%400==0);

}

int mn[]{31,28,31,30,31,30,31,31,30,31,30,31};

int calc(int y,int m,int d){

	int res=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400;

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

		res+=mn[i-1];if(is_uruu(y)&&i==2)res++;

	}

	return res+d;

}

int tod(int y,int m,int d){

	return calc(y,m,d)-base;

}

void tom(int day){

	int b,ka,t,w,ki;

	ki=day%20;day/=20;

	w=day%18;day/=18;

	t=day%20;day/=20;

	ka=day%20;day/=20;

	b=day%13;

	printf("%lld.%lld.%lld.%lld.%lld\n",b,ka,t,w,ki);

}

void tos(int day){

	int y,m;

	for(y=1;365+is_uruu(y)<day;y++)day-=365+is_uruu(y);

	for(m=1;mn[m-1]+(m==2&&is_uruu(y))<day;m++)day-=mn[m-1]+(m==2&&is_uruu(y));

	printf("%lld.%lld.%lld\n",y,m,day);

}

signed main() {

	base=calc(2012,12,21);

	string s;

	while(cin>>s,s!="#"){

		s+='.';

		vector<int>v;

		string t;

		for(char c:s){

			if(c=='.')v.push_back(stoi(t)),t="";

			else t+=c;

		}

		if(v.size()==3)tom(tod(v[0],v[1],v[2]));

		else tos(tod(v[0],v[1],v[2],v[3],v[4]));

	}

}
// Slower C++ solution:
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;

const int MAXN = 2000;

const int INF = (1<<30);

int A[MAXN + 5];

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

int main() {

	int N, K, Q, ans = INF;

	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++) {

		int le = 1;

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

			if( A[j] < A[i] ) {

				int len = j - le;

				for(int k=K;k<=len;k++) {

					q2.push(q1.top());

					q1.pop();

				}

				le = j+1;

				while( !q1.empty() )

					q1.pop();

			}

			else q1.push(A[j]);

		}

		if( q2.size() >= Q ) {

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

				q2.pop();

			ans = min(ans, q2.top() - A[i]);

		}

		while( !q2.empty() )

			q2.pop();

	}

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

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

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

inline int min(int x,int y){

	return x<y?x:y;

}

int main(){

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

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

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

        b[i]=a[i];

    }

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

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

    	t=b[i],l=1,r=1;

	    c[0]=0;

	    while(l<=n){

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

	        d[0]=0,r=l;

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

	        for(int i=l;i<r&&i<=n;i++) d[++d[0]]=a[i];

	        if(d[0]>=k){

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

	            for(int i=1;i<=d[0]-k+1;i++) c[++c[0]]=d[i];

	        }

	        l=r;

	    }

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

	    if(c[0]>=q) ans=min(ans,c[q]-c[1]);

    }

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

    return 0;

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

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

#define INF 0x7fffffff

using namespace std;

int n,k,q,a[2005],b[2005],ans=INF,c[2005],d[2005],t;

inline int read(){

    int x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}

    return x*f;

}

int check(int x){

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

	int l=0; t=0;

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

		if(c[r]>=x) continue;

		if(r-l-k>0){

			sort(c+l+1,c+r);

			for(int i=l+1;i<=r-k;++i) d[++t]=c[i];

		}

		l=r;

	}

	sort(c+l+1,c+n+1);

	if(n-l-k+1>0) for(int i=l+1;i<=n-k+1;++i) d[++t]=c[i];

	if(t>=q){

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

		return d[q]-x;

	}

	else return INF;

}

int main(){

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

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

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

	for(int i=1;i<=n;++i) ans=min(check(b[i]),ans);

	printf("%d",ans);

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

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

using namespace std;

inline int read(){

	int x=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}

	return x*f;

}

#define MN 2005

#define inf 2000000000

int n,k,q,a[MN],rk[MN],r[MN],len[MN],ans;

bool cho[MN];

bool cmp(int x,int y){return a[x]<a[y];}

inline int solve(){

	register int i,bl=0;

	memset(r,0,sizeof r);

	for(i=1;i<=n;i++)if(!cho[i]){

			if(cho[i-1]) len[++bl]=1;

			else ++len[bl];r[i]=bl;

		}

	int cnt=0,mn=inf,mx=-inf;

	for(i=1;i<=n;i++)if(!cho[rk[i]]){

		if(len[r[rk[i]]]>=k) mn=min(mn,a[rk[i]]),mx=max(mx,a[rk[i]]),--len[r[rk[i]]],++cnt;

		if(cnt==q) break;

	}

	return cnt==q?mx-mn:inf;

}

int main(){

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

	register int i;

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

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

	ans=inf;rk[0]=0;

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

		cho[rk[i]]=1;

		ans=min(ans,solve());

	}

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

	return 0;

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

}
// Slower C++ solution:
#include<map>

#include<set>

#include<queue>

#include<stack>

#include<cmath>

#include<ctime>

#include<bitset>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<complex>

#include<iostream>

#include<algorithm>

#define INF 0x3f3f3f3f

#define LL long long

#define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v

#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)

char ch;bool fs;void re(int& x)

{

	while(ch=getchar(),ch<33);

	if(ch=='-')fs=1,x=0;else fs=0,x=ch-48;

	while(ch=getchar(),ch>33)x=x*10+ch-48;

	if(fs)x=-x;	

}

using namespace std;

int ans=INF;

int w,c[2001];

int m,b[2001];

int n,k,q,a[2001];

int main()

{

	re(n);re(k);re(q);

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

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

	{

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

			if(a[j]>a[i])

				b[++m]=a[j];

			else

			{

				if(m>=k)

				{

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

					for(int l=1;l<=m-k+1;++l)

						c[++w]=b[l];

				}

				m=0;

			}

		if(m>=k)

		{

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

			for(int l=1;l<=m-k+1;++l)

			c[++w]=b[l];

		}

		m=0;

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

/*		for(int j=1;j<=w;++j)

			printf("%d ",c[j]);

		puts("");*/

		if(w>=q)

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

		w=0;

	}

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

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<iostream>

#include<cstdlib>

#include<queue>

#define re register 

#define rep(i,s,t) for(re int i=s;i<=t;++i)

using namespace std;

const int N=1e6+11;

int n,k,q,cnt,ans;

int a[N],b[N],c[N],bl[N],sz[N],id[N];

inline bool cmp(int x,int y){

	return a[x]<a[y];

}

inline void build(){

	cnt=1;

	rep(i,1,n)

		sz[i]=0;

	rep(i,1,n)

		if(c[i])

			++cnt;

		else{

			bl[i]=cnt;

			++sz[cnt];

		}

}

int main(){

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

	rep(i,1,n)

		scanf("%d",a+i),id[i]=i;

	ans=1e9+1;

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

	rep(i,1,n){

		build();

		int top=i,j;

		for(j=1;j<=q;++j){

			while(top<=n&&sz[bl[id[top]]]<k)

				++top;

			if(top>n)break;

			--sz[bl[id[top]]];

			++top;

		}

		--top;

		//printf("%d %d %d\n",top,a[id[top]],a[id[i]]);

		if(j==q+1&&top<=n){

			

			ans=min(ans,a[id[top]]-a[id[i]]);

		}

		c[id[i]]=1;

	}

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

	return 0;

} 

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

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

using namespace std;

const int M=2e3+5;

int que[M],order[M],tmp[M],now[M],tot,top,n,k,q;

void in()

{

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

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

}

void add(int le,int ri)

{

    tot=0;

    if(le>ri||ri-le+1<k)return;

    for(int i=le;i<=ri;++i)tmp[++tot]=que[i];

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

    for(int i=ri-le+2-k;i>=1;--i)now[++top]=tmp[i];

}

int test(int mn)

{

    int p=1;top=0;

    for(int i=1;i<=n;++i)if(que[i]<mn)add(p,i-1),p=i+1;

    if(top<q)return INT_MAX; 

    sort(now+1,now+1+top);

    return now[q]-mn;

}

void ac()

{

    int ans=INT_MAX;

    sort(order+1,order+1+n);que[++n]=-INT_MAX;

    for(int i=1;i<n;++i)ans=min(test(order[i]),ans);

    printf("%d",ans);

}

int main()

{

    in();ac();

    return 0;

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

}
// Slower C++ solution:
#include <algorithm>

#include <cstdio>

#include <cstring>

#include <queue>



const int MaxN = 2000 + 5;

const int INF = 0x7F7F7F7F;



int N, K, Q, V;

int A[MaxN], D[MaxN];



void init() {

  scanf("%d %d %d", &N, &K, &Q);

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

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

    D[i] = A[i];

  }

  std::sort(D + 1, D + 1 + N);

}



void solve() {

  int ans = INF;



  for (int x = 1; x <= N; ++x) {

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

      if (A[i] < D[x]) A[i] = -1;

    std::priority_queue<int> pq, pq2;

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

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

    int num = 0, num2 = 0;

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

      if (A[i] == -1) {

        while (num >= K) {

          pq2.push(pq.top());

          pq.pop();

          num--; num2++;

        }

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

        num = 0;

      } else pq.push(-A[i]), num++;

    }

    while (num >= K) {

      pq2.push(pq.top());

      pq.pop();

      num--; num2++;

    }

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

    num = 0;



    int y = 0;

    if (num2 < Q) continue;

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

      y = std::max(y, -pq2.top());

      pq2.pop();

    }

    ans = std::min(ans, y - D[x]);

  }



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

}



int main() {

  init();

  solve();

  return 0;

}

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

#include<algorithm>

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

int ls[2005],st[2005],con;

int work(int mini){

	int last=-1;

	con=0;

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

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

		if(a[i]>=mini&&last==-1) last=i;

		if(a[i]<mini&&~last){

			std::sort(ls+last,ls+i);

			for(int j=last;j<=i-k;j++) st[con++]=ls[j];

			last=-1;

		}

	}

	if(con<q) return 0x3f3f3f3f;

	std::sort(st,st+con);

	return st[q-1]-mini;

}

int main(){

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

	a[n+1]=-1;

	int ans=0x3f3f3f3f;

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

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

		ans=std::min(ans,work(a[i]));

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

}
// Slower C++ solution:
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>

#define INF 0x3FFFFFFF

#define MN 2005

using namespace std;

priority_queue <int,vector<int>,greater<int> > q,Q;

int a[MN],pos[MN];

int n,m,p,las,ans,mn,mx;

bool u[MN];



inline int read()

{

	int n=0,f=1; char c=getchar();

	while (c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}

	while (c>='0' && c<='9') {n=n*10+c-'0'; c=getchar();}

	return n*f;

}



bool cmp(int x,int y) {return a[x]<a[y];}



int main()

{

	register int i,j,k;

	n=read(); m=read(); p=read();

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

	sort(pos+1,pos+n+1,cmp); ans=INF; u[n+1]=true;

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

	{

		las=0;

//		puts("sb");	

//		printf("%d %d\n",Q.size(),q.size());

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

			if (u[j])

			{

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

				for (k=1;k<=j-las-m;++k) Q.push(q.top()),q.pop();

				while (!q.empty()) q.pop(); las=j;

			}

			else q.push(a[j]);

		if (Q.size()<p) break;

		mn=Q.top(); for (j=1;j<=p;++j) mx=Q.top(),Q.pop();

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

		ans=min(ans,mx-mn);

		u[pos[i]]=true;

	}

	printf("%d",ans);

}

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

#include<algorithm>

#include<cstring>

#include<set>

#define MN 2005

using namespace std;

int n,m,k,val[MN],c[MN][2],fa[MN],id[MN],ht[MN],sz[MN],ans=0x3f3f3f3f;bool now[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]),ht[i]=0,sz[i]=1,fa[i]=i,id[i]=i;sort(id+1,id+n+1,cmp);

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

		now[id[i]]=1;

		if(now[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(now[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]),y=merge(c[tmp][0],c[tmp][1]),fa[tmp]=y,fa[y]=y,tmp=y;

		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);

}

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

}

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

}