akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

pair<int, int>operator+(const pair<int, int>&p1, const pair<int, int>&p2) {
	return make_pair(p1.first + p2.first, p1.second + p2.second);
}

struct LazySeg {
	vector<pair<int, int>>seg; vector<int> laz;
	int width;
	LazySeg(int n, vector<pair<int, int>>&vec) :width(1) {
		while (width < n)width *= 2;
		seg.resize(width * 2 - 1);
		laz.resize(width * 2 - 1);
		for (int i = 0; i < vec.size(); ++i) {
			seg[i + width - 1] = vec[i];
		}
		for (int i = width - 2; i >= 0; --i) {
			seg[i] = seg[i * 2 + 1] + seg[i * 2 + 2];
		}
	}
	void eval(int k, int l, int r) {
		if (!laz[k]) {
			return;
		}
		if (r - l > 1) {
			laz[k * 2 + 1] += laz[k];
			laz[k * 2 + 2] += laz[k];
		}
		if (laz[k] % 2)swap(seg[k].first, seg[k].second);
		laz[k] = 0;
	}
	void flip(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0)r = width;
		eval(k, l, r);
		if (r <= a||b <= l) {
			return;
		}
		if (a <= l&&r <= b) {
			laz[k]++;
			eval(k, l, r);
			return;
		}
		flip(a, b, k * 2 + 1, l, (l + r) / 2);
		flip(a, b, k * 2 + 2, (l + r) / 2, r);
		seg[k] = seg[k * 2 + 1] + seg[k * 2 + 2];
	}
	pair<int, int> getVal(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0)r = width;
		eval(k, l, r);
		if (r <= a||b <= l) {
			return { 0,0 };
		}
		if (a <= l&&r <= b) {
			return seg[k];
		}
		return
			getVal(a, b, k * 2 + 1, l, (l + r) / 2) +
			getVal(a, b, k * 2 + 2, (l + r) / 2, r);
	}
};

int N, Q;
int par[123456];
vector<int>child[123456];
vector<pair<int, int>>vec;
vector<pair<int, int>>parse;

int indexe[123456][2];
vector<int>tour;
void euler(int x) {
	indexe[x][0] = tour.size();
	tour.push_back(x);
	for (int ch : child[x]) {
		euler(ch);
	}
	indexe[x][1] = tour.size();
	tour.push_back(x);
}

int main(void)
{
	cin >> N >> Q;
	par[0] = -1;
	for (int i = 1; i < N; ++i) {
		cin >> par[i];
		par[i]--;
		child[par[i]].push_back(i);
	}
	for (int i = 0; i < N; ++i) {
		char c;
		cin >> c;
		if (c == 'G') {
			vec.push_back({ 1,0 });
		}
		else {
			vec.push_back({ 0,1 });
		}
	}
	euler(0);
	int M = tour.size();
	for (int i = 0; i < M; ++i) {
		int val = tour[i];
		if (indexe[val][0] == i) {
			parse.push_back(vec[val]);
		}
		else {
			parse.push_back({ 0,0 });
		}
	}
	LazySeg lazy(M, parse);
	for (int q = 0; q < Q; ++q) {
		int vert;
		cin >> vert;
		--vert;
		lazy.flip(indexe[vert][0], indexe[vert][1]);
		auto res = lazy.getVal(indexe[0][0], indexe[0][1]);
		if (res.first > res.second) {
			cout << "broccoli" << endl;
		}
		else {
			cout << "cauliflower" << endl;
		}
	}
	return 0;
}



Measured memory_bytes: 24220
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> v1;
typedef vector<v1> v2;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;

const ll mod = 1000000007;
const ll INF = 1ll << 50;


ll n,m;
string s;
queue<v1> x;
ll pre[1 << 20];
v1 ans;

void error(){
  cout << -1 << endl;
  exit(0);
}

int main(){
  cin >> n >> m;
  cin >> s;
  x.push(v1({0,0}));
  pre[0] = -1;
  for(ll i = 1;i <= n;i++){
    if(x.size() > m){
      x.pop();
      while(!x.empty() && x.front()[0] == INF){
        x.pop();
      }
    }
    if(x.empty()) error();
    if(s[i] == '0'){
      v1 p = x.front();
      x.push(v1({p[0]+1,i}));
      pre[i] = p[1];

    }else{
      x.push(v1({INF,i}));
      pre[i] = -1;
    }



  }
  ll x = n;
  while(x > 0){
    ll y = pre[x];
    ans.push_back(x-y);
    x = y;
  }
  reverse(ans.begin(),ans.end());
  for(ll x : ans){
    cout << x << " ";
  }
  cout << endl;


}


Measured memory_bytes: 6528
#include <cstdio>
#include <cstring>
#include <algorithm>
#define R register
#define Min(_A, _B) (_A < _B ? _A : _B)
int a[200010], cnt, p = 1, ans;
int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	for(R int i = 1; i <= n; i++)
	{
		R int t;
		scanf("%d", &t);
		a[t]++;
	}
	std::sort(a + 1, a + n + 1);
	for(R int i = 1; i <= n; i++) if(a[i]) cnt++;
	while(cnt > k)
	{
		ans += a[p]; 
		if(a[p]) cnt--;
		p++;
	}
	printf("%d", ans);
	return 0;
}

Measured memory_bytes: 896
import sys

sys.setrecursionlimit(10**6)

n, q = map(int, sys.stdin.readline().strip().split())

path = [[] for _ in range(n + 1)]
for i in range(n - 1):
  a, b = map(int, input().split())
  path[a].append(b)
  path[b].append(a)

plus = [0] * (n + 1)

for _ in range(q):
  p, x = map(int, sys.stdin.readline().strip().split())
  plus[p] += x

def calc(num, path, plus, parent):
  for i in path[num]:
    if i == parent:
      continue

    plus[i] += plus[num]
    calc(i, path, plus, num)

calc(1, path, plus, 0)

print(" ".join([str(i) for i in plus[1:]]))


Measured memory_bytes: 232184
export class Main {
  static main(input): string {
    const tmp: string[] = input.split("\n");
    const [a, p]: number[] = tmp[0].split(" ").map(Number);
    return `${Math.floor((a * 3 + p) / 2)}`;
  }
}
console.log(Main.main(require("fs").readFileSync("/dev/stdin", "utf8")));

Measured memory_bytes: 7372
#include <iostream>
#include "string.h"
#include <algorithm>
#include <vector>
#include <stdio.h>
#include "math.h"
#include <queue>
#include <map>
using namespace std;

int n, m;
vector<pair<int, int> > p[100010];
pair<int, int> c[100010];

int main() {

    cin>>n>>m;
    for (int i=0; i<m; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        c[i] = make_pair(y, -1);
        p[x-1].push_back(make_pair(y, i));
    }

    for (int i=0; i<n; i++) {
        sort(p[i].begin(), p[i].end());
        for (int j=0; j<p[i].size(); j++) {
            c[p[i][j].second] = make_pair(i+1, j+1);
        }
    }

    for (int i=0; i<m; i++) {
        printf("%06d%06d\n", c[i].first, c[i].second);
    }

    return 0;
}

Measured memory_bytes: 7808
x, y = map(int, input().split())
i = 0
if x % y == 0:
    print(-1)
    quit()
while True:
    if x > 10**18:
        print(-1)
        quit()
    elif (x * i) % y != 0:
        print(x * i)
        quit()
    i += 1

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 100;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

int h, w; 
int c_h, d_h, c_w, d_w;
string MP[N];
bool vis[N][N];
int ans = INT_MAX;

struct P{
	int x, y;
	int cnt;
};

bool inside(int x, int y) {
	return x >= 0 and x < h and y >= 0 and y < w;
}

bool legal(int x, int y) {
	return inside(x, y) and MP[x][y] == '.';
}

void bfs() {
	deque<P> dque; 
	dque.push_front({c_h, c_w, 0});
	while (!dque.empty()) {
		auto [x, y, cnt] = dque.front();
		dque.pop_front();
		if (x == d_h and y == d_w) {
			ans = min(ans, cnt);
		}
		if (vis[x][y]) continue;
		vis[x][y] = true;
		for (int i = 0; i < 4; i++) {
			int nx = x + dir[i][0];
			int ny = y + dir[i][1];
			if (legal(nx, ny))
				dque.push_front({nx, ny, cnt});
		}
		for (int nx = x - 2; nx <= x + 2; nx++) {
			for (int ny = y - 2; ny <= y + 2; ny++) {
				if (legal(nx, ny))
					dque.push_back({nx, ny, cnt + 1});
			}
		}
	}
	
}

int main() {
	cin >> h >> w;
	cin >> c_h >> c_w >> d_h >> d_w;
	--c_h, --c_w, --d_h, --d_w;
	for (int i = 0; i < h; i++)
		cin >> MP[i];
	bfs();
	cout << (ans != INT_MAX ? ans : -1) << "\n";
}

Measured memory_bytes: 8160
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define lowbit(x) (x& -x)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define eps 1e-18
inline ll read(){ll tmp=0; char c=getchar(),f=1; for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1; for(;'0'<=c&&c<='9';c=getchar())tmp=tmp*10+c-'0'; return tmp*f;}
inline ll power(ll a,ll b){ll tmp=1; for(;b;b>>=1){if(b&1)tmp=tmp*a%mod; a=a*a%mod;} return tmp;}
using namespace std;
int main()
{
	ll a=read(),b=read(),c=read(),k=read();
	if(a>b&&a>c)printf("%lld\n",(1<<k)*a+b+c);
	else if(b>c)printf("%lld\n",a+(1<<k)*b+c);
	else printf("%lld\n",a+b+(1<<k)*c);
	return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <iomanip>
using namespace std;
 
#define REP(i,n) for(long long (i)=0;(i)<(long long)(n);(i)++)
#define RREP(i,n) for(long long (i)=(long long)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
typedef long long ll;
 
int main() {
 
    string s; cin >> s;
    string st = s;
    
    REMOVE(s, 'x');
 
    REP(i,s.size() / 2) {
    	if (s[i] != s[s.size() - i - 1]) {
    		cout << -1 << endl;
    		return 0;
    	}
    }
 
    REP(i,st.size() / 2) {
    	if (st[i] != st[st.size() - i - 1]) {
    		goto nxt;
    	}
    }
    cout << 0 << endl;
    return 0;
 
    nxt:;
 
    vector < int > ss;
    int cnt = 0;
    REP(i,st.size()) {
    	if (st[i] == 'x') {
    		cnt++;
    		if (i == st.size() - 1) {
    			ss.push_back(cnt);
    		}
    	} else {
    		ss.push_back(cnt);
    		cnt = 0;
    		ss.push_back(-1);
    		if (i == st.size() - 1) {
    			ss.push_back(0);
    		}
    	}
    }
 
 
    int ans = 0;
    REP(i,ss.size() / 2) {
    	if (ss[i] >= 0) {
    		ans += abs(ss[i] - ss[ss.size() - i - 1]);
    	}
    }
 
    cout << ans << endl;
 
    return 0;
}

Measured memory_bytes: 1656
#include <stdio.h>
#include <stdint.h>

int main(void)
{
    uint32_t N, T;
    uint32_t i;
    uint32_t minA = 0xfffffffful;
    uint32_t diffMax = 0;
    uint32_t cnt = 0;

    scanf("%u %u", &N, &T);
    for (i = 0; i < N; i++) {
        uint32_t A;
        scanf("%u", &A);
        if (minA < A) {
            const uint32_t diff = A - minA;
            if (diffMax < diff) {
                cnt = 1;
                diffMax = diff;
            } else if (diffMax == diff) {
                cnt++;
            }
        } else if (minA > A) {
            minA = A;
        }
    }

    printf("%u\n", cnt);

    return 0;
}



Measured memory_bytes: 128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSFJ2019A
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var b = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            Console.WriteLine(a[0] != b[0] ? 1 : 0);
            Console.ReadLine();
        }
    }
}


Measured memory_bytes: 13268
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,h,a[100000],b[100000],ans=0;
    cin>>n>>h;
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    sort(a,a+n);
    sort(b,b+n);
    for(int i=n-1;i>=0;i--){
        if(a[n-1]<b[i]){
            h-=b[i];
            ans++;
            if(h<=0){
                cout<<ans<<endl;
                return 0;
            }
        }
        else{
            ans+=(h+a[n-1]-1)/a[n-1];
            cout<<ans<<endl;
            return 0;
        }
    }
    ans+=(h+a[n-1]-1)/a[n-1];
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define REP(i, a, b) for(int i=(int)(a); i<(int)(b); i++)
#define RREP(i, a, b) for(int i=(int)(a); i>=(int)(b); i--)
#define ALL(v) v.begin(), v.end()
#define INF 2e9
typedef long long ll;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return 1;}return 0;}

using namespace std;

int main()
{
    int N, D;
    vector<vector<int>> X;
    cin >> N >> D;
    REP(i,0,N){
        vector<int> xx;
        REP(k,0,D){
            int x;
            cin >> x;
            xx.push_back(x);
        }
        X.push_back(xx);
    }
    int c=0;
    REP(i, 0, N-1){
        REP(k, i+1, N){
            double distance=0;
            REP(l, 0, D){
                distance += pow(abs(X[i][l]-X[k][l]), 2);
            }
            distance = sqrt(distance);
            if(ceil(distance)==floor(distance)){
                c++;
            }

        }
    }
    cout << c << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;

ll sqrt(ll in){
    ll ok = 0;
    ll ng = 1e9 + 1;
    while(ng - ok > 1){
        ll mid = (ok + ng) / 2;
        if(mid * mid < in){
            ok = mid;
        } else {
            ng = mid;
        }
    }
    return ok;
}

int main() {
    //cout.precision(10);
    int n;
    cin >> n;
    for(int time = 0; time < n; time++){
        ll a, b;
        cin >> a >> b;
        if(a > b) swap(a, b);
        if((a == 1) && (b == 1)){
            cout << 0 << endl;
            continue;
        }
        ll checker = sqrt(a * b);
        ll ans = 2 * checker;
        if(checker >= a){
            ans--;
        }
        if(checker * (checker + 1) >= (a * b)){
            ans--;
        }
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(void){
 	
  	int N,M;
  	int array[10000];
  
  	scanf("%d %d",&N,&M);
  	for(int i = 0;i < M;i ++){
     	scanf("%d",&array[i]);
      	N -= array[i];
      	if(N < 0){
         	printf("-1");
          	return 0;
        }
    }
  
  	printf("%d",N);
  
  	return 0;
}

Measured memory_bytes: 1764
#include <stdio.h>

int main()
{
    int a,b;
    int c,d;
    while(1){
    scanf("%d %d",&a,&b);
    if(a==0 && b==0) return 0;
    for(c=1;c<=a;c++)
    {
        if(c%2 == 1)
        {
        for(d=1;d<=b;d++)
            {
                if(d%2 == 1) printf("#");
                if(d%2 == 0) printf(".");
                if(d==b) printf("\n");
            }
        }else{
        for(d=1;d<=b;d++)
            {
                if(d%2 == 1) printf(".");
                if(d%2 == 0) printf("#");
                if(d==b) printf("\n");
            }
            }
    }
    printf("\n");
    }
    return 0;
}

Measured memory_bytes: 572
#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b;
  int count=0;
  cin>>a>>b;
  while(1){
    a=a/b;
    count++;
  
    if(a<b&&a>=1){
      count=count+1;
    
      break;
    }
    else if(a==0) break;
    
  }
  cout<<count<<endl;

  
}


Measured memory_bytes: 256
#include <iostream>
#include <math.h>
using namespace std;

int main(void) {
    
  int N;
  int A;
  int B;
  
  cin >> N;
  cin >> A;
  cin >> B;
  
  if ( N * A <= B ) {
    
    cout << N * A << endl;
  }
  
  if ( N * A > B ) {
    
    cout << B << endl;
    
    return 0;
    
  }
}


Measured memory_bytes: 3548
n,q=map(int,input().split())
s=input()
a=[0]*n
for i in range(n):
    if i:
        a[i]=a[i-1]+(s[i-1:i+1]=='AC')
for _ in range(q):
    l,r=map(int,input().split())
    print(a[r-1]-a[l-1])

Measured memory_bytes: 7748
#include<stdio.h>

int main(void){
	int S;
	int h,m,s;
	scanf("%d",&S);
	h=S/3600;
	m=(S-3600*h)/60;
	if(m>=60){
		m=59;
	}
	s=(S-3600*h-m*60)%60;
	if(s>=60){
		s=59;
	}
	printf("%d:%d:%d\n",h,m,s);
	
	return 0;
}

Measured memory_bytes: 592
// D.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <numeric>

using namespace std;

typedef long long LL;

int main(int argc, char* argv[]) {
#ifdef _MSC_VER
	while (true)
#endif
	{
		const int INF = 1 << 30;
		int n = -1, ans = INF;
		cin >> n;
		if (n < 0) return 0;
		string s;
		cin >> s;
		vector<int> r(n + 1), w(n + 1);
		for (int i = 0; i < n; ++i) {
			r[i + 1] = r[i] + (s[i] == 'R');
			w[i + 1] = w[i] + (s[i] == 'W');
		}
		for (int i = 0; i <= n; ++i) {
			int right = r[n] - r[i];
			int cost = w[i] + right - min(w[i], right);
			ans = min(ans, cost);
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 5236
al=[chr(ord('a') + i) for i in range(26)]
def dfs(N, f):
    if len(f) == N:
        return f
    else:
        result = ""
        f_index = [al.index(i) for i in f]
        for i in range(max(f_index) +1 + 1):#追加できるのは(f+1) + 1
            result +=dfs(N,  f + al[i])
        return result

N = int(input())
ans = dfs(N, 'a')
for i in range(int(len(ans) / N)):
    print(ans[N*i : N * (i+1)])

Measured memory_bytes: 79452
#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include "ext/pb_ds/tree_policy.hpp"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/rope"

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define f first
#define s second

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    vector<pair<ll, ll>> sch;
    ll ax, bx;
    F0R(i, n)
    {
        cin >> ax >> bx;
        sch.pb(make_pair(bx, ax));
    }
    sort(sch.begin(), sch.end());
    ll t = 0;
    F0R(i, n)
    {
        t += sch[i].s;
        if(t > sch[i].f)
        {
            cout << "No";
            return 0;
        }
    }
    cout << "Yes";
    return 0;
}

Measured memory_bytes: 6384
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10; 

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   f(i,0,n){
      cin >> a[i];
   }
   sort(all(a),greater<int>());
   int ans = 0;
   f(i,k,n){
      ans += a[i];
   }
   cout << ans << '\n';
   return 0;
}

Measured memory_bytes: 1792
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = map(int, input().split())
p = defaultdict(lambda :defaultdict(list))
e = [[int(i) for i in input().split()] for i in range(m)] 
p_index = defaultdict(int)
for i in e:
    pp, yy = i[0], i[1]
    p[pp][yy] = 0

for i in p:
    count = 0
    l = sorted(p[i])
    for j in l:
        count += 1
        p[i][j] += count

for i in e:
    print("{:0=6}".format(i[0]) + "{:0=6}".format(p[i[0]][i[1]]))

Measured memory_bytes: 61536
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int h1,w1,h2,w2,K; cin >> h1 >> w1 >> h2 >> w2 >> K;
    int p1 = h1*60+w1;
    int p2 = h2*60+w2;
    cout << p2 - p1 - K << ln;
}

Measured memory_bytes: 3664
x,y=map(int,input().split())
i=x
l=False
if x==y or x%y==0:
    print(-1)
else:
    while 1<=i and i<=x*y:
        if i%x==0 and i%y!=0:
            print(i)
            l=True
            break
        else:
            i+=x

Measured memory_bytes: 38384
#include <stdio.h>

int main(void){
    int H, W, i, j;
    //scan H and W until 0 0
    while(1){
        scanf("%d %d", &H, &W);
        if (H == 0 && W == 0 )
        break;

    for ( i = 1; i <= H; i++ ){
        for ( j = 1; j <= W; j++ ){
            if(i > 1 && i <= H - 1 && j > 1 && j <= W - 1){
                printf(".");
            } else {
                printf("#");
            }
        }
        printf("\n");
    }
        printf("\n");
    }
}

    


Measured memory_bytes: 2032
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i=0; i<n;i++) {
			a[i] = sc.nextInt();
		}

		Arrays.sort(a);

		System.out.println(Math.abs(a[0]-a[n-1]));

	}
}

Measured memory_bytes: 21844
n = int(input())
X = [0]*n
Y = [0]*n
for i in range(n):
  X[i],Y[i] = map(int,input().split())  
  
dic = {}
for i in range(n):
  for j in range(n):
    if i!=j:
      x = X[i]-X[j]
      y = Y[i]-Y[j]
      dic[(x,y)] = dic.get((x,y),0) + 1

if n == 1:
  print(1)
else:  
  print(n-max(dic.values()))

Measured memory_bytes: 9316
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>

using namespace std;

int N;

int main() {
	cin >> N;
	int ans = 0;
	if (!(N % 2)) {
		ans = (N / 2) * (N / 2);
	}
	else {
		ans = floor(N / 2) * (floor(N / 2) + 1);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}

int main(){
  int N;
  cin >> N;
  for(int i = 0;i < 100; i++){
    for(int j = 0;j < 100; j++){
      if( N == (i*4 + j*7) ){
        cout << "Yes" << endl;
        return 0;
      }else if(N < i*4+j*7) break;
    }
  }
  cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <thread>
#include <chrono>
#include <random>
#include <fstream>
#include <time.h>

#define ll long long int
#define ull unsigned long long int
#define pr(x) (std::cout << x << std::endl)



int main()
{
    ull n, m, q;
    std::cin >> n >> m >> q;
    std::vector<int> ts(m), td(m), qs(q), qd(q);
    std::vector<std::vector<int> > dic;
    dic = std::vector<std::vector<int> >(n, std::vector<int>(n, 0));
    for(int i=0; i<m; i++){
        std::cin >> ts[i] >> td[i];
        int s = ts[i]-1, d = td[i]-1;
        for(int j=0; j<=s; j++){
            for(int k=d; k<n; k++){
                dic[j][k]++;
            }
        }
    }

    for(int i=0; i<q; i++){
        std::cin >> qs[i] >> qd[i];
    }
    for(int i=0 ;i<q; i++) pr(dic[qs[i]-1][qd[i]-1]);


    return 0;
}

Measured memory_bytes: 4224
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
	int n,r;
	int data[100];
	while(1){
		cin >> n >> r;
		if(n==0&&r==0) break;
		REP(i,100) data[i] = n-i+1;
		REP(i,r){
			int p,c;
			cin >> p >> c;
			int newdata[100];
			REP(j,c){
				newdata[j+1] = data[p+j];
			}
			REP(j,p){
				newdata[j+c+1] = data[j+1];
			}
			FOR(j,p+c,n+1){
				newdata[j] = data[j];
			}
			REP(i,100) data[i] = newdata[i];
		}
		cout << data[1] << endl;
	}
}

Measured memory_bytes: 1160
N = int(input())
city = {}
for i in range(N):
    s,p = map(str,input().split())
    if s not in city:
        city[s] = [[int(p),i+1]]
    else:
        city[s].append([int(p),i+1])
        city[s].sort(reverse=True)

city = sorted(city.items())

for i in city:
    for j in i[1:]:
        for k in j:
            print(k[1])

Measured memory_bytes: 9196
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i,s,n) for(int i = s; i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n';
#define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;

int main(void)
{
    IOS
    int n, m;
    cin >> n >> m;
    int ml = 1, mr = n;
    REP(i,m) {
        int l, r;
        cin >> l >> r;
        ml = max(ml,l);
        mr = min(mr,r);
    }

    OUT(max(mr-ml+1,0))

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector <int> a;

vector <ll> calc(vector <int> &a){
    multiset <int> s;
    ll sum = 0;
    vector <ll> ret(3 * n);
    for(int i = 0 ; i < 3 * n ; i++){
        s.insert(a[i]);
        sum += a[i];
        if((int)s.size() > n){
            sum -= *s.begin();
            s.erase(s.begin());
        }
        if((int)s.size() == n) ret[i] = sum;
    }
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    a.resize(3 * n);
    for(auto &i : a) cin >> i;
    vector <ll> pref = calc(a);
    reverse(a.begin(), a.end());
    for(auto &i : a) i *= -1;
    vector <ll> suff = calc(a);
    reverse(suff.begin(), suff.end());
    ll ans = -1e18;
    for(int i = n - 1 ; i < 2 * n ; i++){
        ans = max(ans, pref[i] + suff[i + 1]);
    }
    cout << ans << endl;
}


Measured memory_bytes: 13732
using System;
using System.Collections.Generic;
using System.Linq;


public class Contest
{
	public static void Main()
	{

/*
		int n = a[0];
		int[][] e = new int[n][];
		for(int i=0; i<n; i++) MyRead.Array(out e[i]);

		int[] a;
		MyRead.Array(out a);

        long a,b,c,d,e,f;
        MyRead.Multi(out a, out b, out c, out d, out e, out f);

		Console.WriteLine(string.Join(' ', Array.ConvertAll(ans, x => x.ToString())));
*/

        int n;
		MyRead.Multi(out n);
		int[] h;
		MyRead.Array(out h);



		string ans = "Yes";
		h[0]--;
		for(int i=0;i<n-1; i++){
			if(h[i]<h[i+1])h[i+1]--;
			if(h[i]>h[i+1]){
				ans = "No";
				break;
			}

		}

		Console.WriteLine(ans);
	}


}

public static class MyRead
{
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) => eq<T, int>()    ? ct<T, int>(int.Parse(s))
	                                  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	                                  : eq<T, double>() ? ct<T, double>(double.Parse(s))
	                                  : eq<T, char>()   ? ct<T, char>(s[0])
	                                                    : ct<T, string>(s);
	public static string[] Array<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); 												
		a = System.Array.ConvertAll(ar, new Converter<string,T>(cv<T>));
		return ar;
	}										
	public static string[] Multi<T>(out T a) {
		var ar = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] Multi<T, U>(out T a, out U b) {
		var ar = Multi<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] Multi<T, U, V>(out T a, out U b, out V c) {
		var ar = Multi<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] Multi<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = Multi<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = Multi<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = Multi<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
}




Measured memory_bytes: 28512
'use strict'
function main(input) {
  // お決まりの処理
  const tmp = input.split('\n');
  const N = parseInt(tmp[0], 10);
  const h = tmp[1].split(' ').map((elem) => parseInt(elem, 10));
  
  // 動的計画法にお決まりの必要な配列を作って満たす処理
  let dp = new Array(N).fill(Infinity);
  // 最初なので0
  dp[0] = 0;
  // 原則はdpと他の条件を比較して小さいほうが勝ち
  for(let i = 1; i < N; i++){
  	dp[i] = Math.min(dp[i],dp[i-1] + Math.abs(h[i] - h[i - 1])); 
    if(i > 1){
      dp[i] = Math.min(dp[i],dp[i-2] + Math.abs(h[i] - h[i - 2])); 
    }
  } 
  console.log(dp[N - 1]);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));


Measured memory_bytes: 27556
### constant

K2CHS = ["ld", "mb", "mp", "nc", "nd", "ng", "nt", "nw", "ps", "qu", "cw", "ts"]
K1CHS = ('a'..'z').to_a
KCHS = K1CHS + K2CHS

### subroutines

def split_word(word)
  chs = []
  i = 0
  wlen = word.length
  while i < wlen
    ch2 = word[i, 2]
    if $kcodes[ch2]
      chs << ch2
      i += 2
      next
    end
    chs << word[i]
    i += 1
  end
  chs
end

### main

$kcodes = {}
KCHS.each_with_index{|c, i| $kcodes[c] = i}
#p $kcodes; exit

pairs = KCHS.map{KCHS.map{0}}

n = gets.to_i

n.times do
  for word in gets.strip.split
    pcc = nil
    for ch in split_word(word)
      cc = $kcodes[ch]
      if ! pcc.nil?
        pairs[pcc][cc] += 1
      end
      pcc = cc
    end
  end
end

for pcc in (0...KCHS.length)
  max_pn = 0
  max_cc = 0

  for cc in (0...KCHS.length)
    pn = pairs[pcc][cc]
    if max_pn < pn
      max_pn = pn
      max_cc = cc
    end
  end

  puts [KCHS[pcc], KCHS[max_cc], max_pn].join(' ')
end

Measured memory_bytes: 6132
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <time.h>
#include <tuple>
#define all(c) ((c).begin(),(c).end())
#define rall(c) ((c).rbegin(),(c).rend())
//#define sort(v,n) sort(v,v+n)
#define ll long long
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int a, b, c;

int main() {
	cin >> a >> b >> c;
	int cont = 0;
	int firsttime = 0;
	int lasttime = a;
	int nowc = c;
	if (c > a && a+b==60) {
		cout << -1 << endl;
		return 0;
	}
	while (1) {
		cont++;
		if (cont > 1000000) {
			cout << -1 << endl;
			return 0;
		}
		if (firsttime <= nowc && lasttime >= nowc) {
			cout << nowc << endl;
			break;
		}
		else {
			firsttime = lasttime + b;
			lasttime = firsttime + a;
			if (firsttime > nowc) {
				nowc += 60;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3088
#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll power(ll x,ll n)
{
	ll res =1;
	while(n>0)
	{
		if(n%2==1)
	res=res*x;
	x=x*x;
	n=n/2;
	}
	return res;
	
}

int main(){

ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    string s;
    cin>>n>>s;
    set<int>a[26];
    for(int i=0;i<n;i++)
    {
    	a[s[i]-'a'].insert(i+1);
	}
    int t;
    cin>>t;
    while(t--){
    	int x,l,r,i;
    	char c;
    	cin>>x;
    	if(x==1)
    	{
    		cin>>l>>c;
    		a[s[l-1]-'a'].erase(a[s[l-1]-'a'].find(l));
    		s[l-1]=c;
    		a[c-'a'].insert(l);
		}
		else
		{
			cin>>l>>r;
			ll ans=0;
			set<int>::iterator it,jt;
			for(i=0;i<26;i++)
			{
				it=a[i].lower_bound(l);
				if(it!=a[i].end()&&(*it)<=r)
				ans++;
			}
			cout<<ans<<"\n";
		}
    	

    }

}


Measured memory_bytes: 24220
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
vector<P> ps(100001);
vector<vector<int> > xl(100001);
vector<vector<int> > yl(100001);
vector<bool> xa(100001);
vector<bool> ya(100001);
vector<bool> arrived(100001);

ll lx = 0;
ll ly = 0;

ll dfs(int _index) {
	if(arrived[_index]) return 0;
	arrived[_index] = true;
	int x = ps[_index].first;
	int y = ps[_index].second;
	if(!xa[x]) {
		lx++;
		xa[x] = true;
	}
	if(!ya[y]) {
		ly++;
		ya[y] = true;
	}
	ll temp_sum = 0;
	for(int i = 0; i < xl[x].size(); i++) {
		temp_sum += dfs(xl[x][i]);
	}
	for(int i = 0; i < yl[y].size(); i++) {
		temp_sum += dfs(yl[y][i]);
	}
	return temp_sum + 1LL;
}

ll calc(int _index) {
	int x = ps[_index].first;
	int y = ps[_index].second;
	lx = 0;
	ly = 0;
	ll p_num = dfs(_index);
	if(lx>1LL&&ly>1LL) return (lx*ly-p_num);
	return 0;
}

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) {
		int x,y;
		cin >> x >> y;
		xl[x].push_back(i);
		yl[y].push_back(i);
		ps[i].first = x;
		ps[i].second = y;
	}
	ll ans = 0;
	for(int i = 0; i < n; i++) {
		ans += calc(i);
	}
	cout << ans << endl;
}


Measured memory_bytes: 13568
#include <iostream>
#include<iomanip>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>
#include<math.h>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<cstdlib>
#define INF long long (int)1e18+1
#define MAX 200001
#define rep(i,n,m) for(int i=n;i<m;i++)

using namespace std;
	
int main() {
	int n;
	cin >> n;
	vector<int>a(n + 1, 0);
	rep(i, 1, n + 1) {
		rep(j, 1, n + 1) {
			if (i * j > n)break;
			a[i * j]++;
		}
	}
	long long ans = 0;
	rep(i, 1, n) {
		ans += a[i];
	}
	cout << ans << endl;
}


Measured memory_bytes: 7112
#include <bits/stdc++.h>

using namespace std;

int main(void) {
  
  string S;
  cin >> S;

  int count = 0;
  int max = 0;
  for (int i =0; i<S.size(); i++) {
    if (S.at(i) == 'R') {
      count += 1;
      if (count > max) {
        max = count;
      }
    } else {
      if (count > max) {
        max = count;
      }
      count = 0;
    }
  }

  cout << max  << endl;

}


Measured memory_bytes: 3620
#include<iostream>
#include<iomanip>

using namespace std;

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

int main(){
  std::cin.tie(nullptr);                  // 標準入出力高速化
  std::ios_base::sync_with_stdio(false);  // SとS++の入出力出力を非同期
  int n,m,X,Y;
  cin>>n>>m>>X>>Y;
  int x[n];
  rep(i,0,n) cin>>x[i];
  int y[m];
  rep(i,0,m) cin>>y[i];
  int u=100,d=-100;
  if(d<X) d=X;
  if(u>Y) u=Y;
  rep(i,0,n)
      if(d<x[i])
          d=x[i];
  rep(i,0,m)
      if(u>=y[i])
          u=y[i];
  u>d ? cout<<"No War"<<endl : cout<<"War"<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstring>
using namespace std;

int main() {
    int N;
    cin >> N;
    char W[100][20];
    for (int i = 0; i < N; i++) cin >> W[i];

    int last_i = strlen(W[0]) - 1;
    bool result = true;
    for (int i = 1; i< N && result; i++) {
        result *= (W[i - 1][last_i] == W[i][0]);
        last_i = strlen(W[i]) - 1;

        for (int j = 0; j < i && result; j++) {
            if (strcmp(W[i], W[j]) == 0) result = false;
        }
    }

    if (result) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int s;
char a[15],b[15];
int main(){
	scanf("%s %s",a+1,b+1);
	for(int i=1;i<=3;++i){
		if(a[i]==b[i])s++;
	}
	cout<<s;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#define MAX 101
using namespace std;


int main(){

	int adjMar[MAX][MAX];
	int n, i, j, vertice, degree, adjVertice;
	cin >> n;

	for (i = 0; i <= n; i++)
		for (j = 0; j <= n; j++)
			adjMar[i][j] = 0;

	for (i = 1; i <= n; i ++) {
		cin >> vertice;
		cin >> degree;
		for (j = 0; j < degree; j++){
			cin >> adjVertice;
			adjMar[vertice][adjVertice] = 1;
		}
	}

	for (i = 1; i <= n; i++){
		for (j = 1; j <= n; j++){
			cout << adjMar[i][j];
			if (j != n) 
				cout <<' ';
		}
		cout << endl;
	}
}

Measured memory_bytes: 3096
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;

  string res = "";
  for (int k = 0; k < 1 << 2; k++) {
    string str = "";
    for (int i = 0; i < 2; i++) {
      if (k >> i & 1) {
        str += "W";
      } else {
        str += "S";
      }
    }
    for (int i = 1; i < n - 1; i++) {
      if (str[i] == 'S') {
        if (s[i] == 'o') {
          str += str[i - 1];
        } else {
          str += (str[i - 1] == 'S' ? "W" : "S");
        }
      } else {
        if (s[i] == 'o') {
          str += (str[i - 1] == 'S' ? "W" : "S");
        } else {
          str += str[i - 1];
        }
      }
    }
    string check = str + str + str;
    bool flag = true;
    for (int i = n; i < n + n; i++) {
      if (check[i] == 'S') {
        if (s[i - n] == 'o') {
          if (check[i - 1] != check[i + 1]) flag = false;
        } else {
          if (check[i - 1] == check[i + 1]) flag = false;
        }
      } else {
        if (s[i - n] == 'o') {
          if (check[i - 1] == check[i + 1]) flag = false;
        } else {
          if (check[i - 1] != check[i + 1]) flag = false;
        }
      }
    }
    if (flag) res = str;
  }
  if (res.size()) {
    cout << res << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}

Measured memory_bytes: 1344
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

struct UnionFind{
  vector<int> v;
  UnionFind(int n) : v(n, -1) {}
  void init(){ for(int i = 0;i < v.size();i++)v[i]=-1; }
  int find(int x) { return v[x] < 0 ? x : v[x] = find(v[x]); }
  bool unite(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return false;
    if (-v[x] < -v[y]) swap(x, y);
    v[x] += v[y]; v[y] = x;
    return true;
  }
  bool root(int x) { return v[x] < 0; }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};


ll n,K,Q;
ll a[2001];
bool used[2001];

int main(){
  cin>>n>>K>>Q;
  rep(i,n)cin>>a[i];
  ll res=INF;
  if(K==1){
    sort(a,a+n);
    rep(i,n-Q+1){
      minch(res,a[i+Q-1]-a[i]);
    }
    cout<<res<<endl;
    return 0;
  }
  rep(i,n){
    UnionFind uf(n);
    memset(used,0,sizeof(used));
    rep(j,n-1){
      if(a[j]>=a[i]&&a[j+1]>=a[i])uf.unite(j,j+1);
    }
    ll sz=0;
    rep(j,n){
      if(a[j]>=a[i]&&uf.size(j)>=K){
        if(used[uf.find(j)])continue;
        used[uf.find(j)]=true;
        sz+=uf.size(j)-K+1;
      }
    }
    if(sz<Q)continue;
    vector<int> b;
    rep(j,n){
      if(a[j]>=a[i]&&uf.size(j)>=K){
        vector<int> c;
        repl(k,j,n){
          if(uf.find(k)!=uf.find(j))break;
          c.push_back(a[k]);
        }
        sort(all(c));
        rep(k,uf.size(j)-K+1){
          b.push_back(c[k]);
        }
        j+=c.size()-1;
      }
    }
    sort(all(b));
    minch(res,b[Q-1]-a[i]);
  }
  cout<<res<<endl;
  return 0;
}


Measured memory_bytes: 384
#include<iostream>
using namespace std;
int a[100];
int n,k;
main()
{
    while(cin>>n,n)
    {
        for(int i=0;i<n;i++)cin>>a[i];
        int l=0,f=n-1;
        cin>>k;
        int cnt=0;
        while(f>=l)
        {
            int m=(f+l)/2;
            cnt++;
            if(a[m]>k)f=m-1;
            else if(a[m]<k)l=m+1;
            else break;
        }
        cout<<cnt<<endl;
    }
}



Measured memory_bytes: 3032
#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

#define DEBUG 0

#if DEBUG
  #define DLOG(...) fprintf(stderr, __VA_ARGS__)
  #define DFUNC(fname, ...) fname(__VA_ARGS__)
#else
  #define DLOG(...)
  #define DFUNC(fname, ...)
#endif

ll MOD = 998244353;

constexpr ll eGCD(ll a, ll b, ll& s, ll& t) {
  if (a == 0) {
    s = 0;
    t = 1;
    return b;
  }
  ll u = 0;
  ll g = eGCD(b % a, a, t, u);
  s = u - (b / a) * t;
  return g;
}


struct Fp {
  ll val;

  Fp(ll t = 0) :
    val(t < 0 ? MOD + t % MOD : t >= MOD ? t % MOD : t) {}

  Fp& operator +=(const Fp& t) {
    val += t.val;
    if (val > MOD) val -= MOD;
    return *this;
  }

  Fp& operator -=(const Fp& t) {
    val -= t.val;
    if (val < 0) val += MOD;
    return *this;
  }

  Fp& operator *=(const Fp& t) {
    val = (val * t.val) % MOD;
    return *this;
  }

  Fp inv() const {
    if (val == 0) {
      cerr << "inv() is called for zero." << endl;
      exit(1);
    }
    ll u = 0;
    ll v = 0;
    eGCD(val, MOD, u, v);
    return Fp(u);
  }

  Fp& operator /=(const Fp& t) {
    return (*this) *= t.inv();
  }

  Fp operator +(const Fp& t) const { return Fp(val) += t; }
  Fp operator -(const Fp& t) const { return Fp(val) -= t; }
  Fp operator *(const Fp& t) const { return Fp(val) *= t; }
  Fp operator /(const Fp& t) const { return Fp(val) /= t; }
  Fp operator -() const { return Fp(-val); }

  bool operator ==(const Fp& t) const { return val == t.val; }
  bool operator !=(const Fp& t) const { return val != t.val; }
  
  operator ll() const { return val; }

};

ll N, A, B, K;
vector<Fp> fact(N+1);
vector<Fp> invFact(N+1);

Fp combi(ll n, ll r) {
  Fp c = fact.at(n) * invFact.at(r) * invFact.at(n-r);
  DLOG("combi(%lld, %lld) = %lld\n", (ll)n, (ll)r, (ll)c);
  return c;
}

int main() {
#if !DEBUG
  ios_base::sync_with_stdio(false);
#endif

  cin >> N >> A >> B >> K;
  ll p = 0;
  ll q = 0;
  ll g = eGCD(A, B, p, q);
  if (K % g != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll c = K / g;
  if (p <= 0) {
    swap(A, B);
    swap(p, q);
  }

  fact.resize(N+1);
  invFact.resize(N+1);
  fact.at(0) = invFact.at(0) = 1;
  for (int i = 1; i <= N; i++) {
    fact.at(i) = fact.at(i-1) * Fp(i);
    invFact.at(i) = fact.at(i).inv();
  }

  // Apc + Bqc = K, p > 0, q <= 0
  // A(pc-Bt) + B(qc+At) = K, t \in [ (A - 1 - qc) / A, pc / B ]
  Fp cnt = 0;
  ll uA = A / g;
  ll uB = B / g;
  for (ll t = (uA-1 - q*c) / uA; t <= p*c / uB; t++) {
    ll nA = p*c - uB*t;
    ll nB = q*c + uA*t;
    DLOG("nA, nB = %lld, %lld\n", nA, nB);
    if (nA > N || nB > N) continue;
    cnt += combi(N, nA) * combi(N, nB);
  }
  cout << cnt << endl;


  return 0;
}



Measured memory_bytes: 4992
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

int main() {
  int n, m;
  cin >> n >> m;
  int s[m], c[m];
  rep(i, m) cin >> s[i] >> c[i];

  rep(i, 1000) {
    string str = to_string(i);

    bool now = true;
    rep(j, m) {
      int p = s[j] - 1, q = c[j];
      if (str[p] != q + '0') {
        now = false;
        continue;
      }
    }

    if (str.size() != n) continue;

    if (now) {
      cout << i << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#define _USE_MATH_DEFINES
#include<math.h>
#include<vector>
#include<array>
#include<algorithm>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vector<ll>> mat;
typedef pair<ll,ll> P;
typedef map<ll,ll> MP;
typedef priority_queue<ll> PQ;
typedef stack<ll> ST;
typedef queue<ll> QUE;
#define pb push_back
#define st first
#define nd second
#define SZ(x) ((int)(x).size())
#define FOR(i,s,n) for(int i=(int)(s); i< (int)(n); i++)
#define rep(i,n) FOR(i,0,n)
#define all(x) (x).begin(),(x).end()
#define each(x,c) for(auto x: (c))
#define LB(x,a) lower_bound(all(x), a)
#define UB(x,a) upper_bound(all(x), a)
const ll mod=1e9+7;
const ll INF=1e18;
const double EPS=1.0e-10;
const double PI=M_PI;

int main(){
    int N; scanf("%d", &N);
    string S; cin>>S;
    ll r = 0, g = 0, b = 0;
    rep(i,N){
        if(S[i] == 'R') r++;
        if(S[i] == 'G') g++;
        if(S[i] == 'B') b++;
    }
    ll ans = r * g * b;
    rep(i,N){
        FOR(j,i+1,N){
            int k = 2 * j -i;
            if(k < N && S[i] != S[j] && S[i] != S[k] && S[j] != S[k]){
                ans--;
            }
        }
    }
    printf("%lld\n", ans);
}

Measured memory_bytes: 3760
#include "bits/stdc++.h"
using namespace std;


typedef long long ll;
typedef pair <int, int> ii;
typedef pair <int, ii> iii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

#define iINF (1<<30)
#define INF (1LL<<59)
#define forn(i) for(int i = 0;i < n; ++i)
#define form(i,n) for(int i = 0 ;i < n; ++i)
#define NIL -1

const ll mod = 998244353;
int n;
int m;
int ans;
string str;

int main(){
    int a,b,q;
    cin>>a>>b>>q;
    ll s[100010], t[100010];
    ll st[100010], ts[100010];
    for(int i=0;i<a;i++){
        cin>>s[i];
    }
    s[a]=INF;
    for(int i=0;i<b;i++){
        cin>>t[i];
    }
    t[b]=INF;
    sort(s,s+a+1);
    sort(t,t+b+1);
    int id;
    for(int i=0;i<a;i++){
        id=(int)(upper_bound(t,t+b+1,s[i])-t);
        if(id==0){
            st[i]=abs(s[i]-t[0]);
        }else{
            st[i]=min(abs(s[i]-t[id]),abs(s[i]-t[id-1]));
        }
    }
    for(int i=0;i<b;i++){
        id=(int)(upper_bound(s,s+a+1,t[i])-s);
        if(id==0){
            ts[i]=abs(t[i]-s[0]);
        }else{
            ts[i]=min(abs(t[i]-s[id]),abs(t[i]-s[id-1]));
        }
    }
    for(int _=0;_<q;_++){
        ll x;
        cin>>x;
        ll sans, tans;
        id=(int)(upper_bound(t,t+b+1,x)-t);
        if(id==0){
            tans=abs(t[0]-x)+ts[0];
        }else{
            tans=min(abs(t[id]-x)+ts[id],abs(t[id-1]-x)+ts[id-1]);
        }
        id=(int)(upper_bound(s,s+a+1,x)-s);
        if(id==0){
            sans=abs(s[0]-x)+st[0];
        }else{
            sans=min(abs(s[id]-x)+st[id],abs(s[id-1]-x)+st[id-1]);
        }
        cout<<min(tans,sans)<<endl;
    }
    return 0;
}

Measured memory_bytes: 4480
using System;
using System.Linq;

namespace ProblemA
{
    class Program
    {
        static void Main()
        {
            var temp = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int cardA = temp[0];
            int cardB = temp[1];
            int cardC = temp[2];
            int K = temp[3];

            int ans = 0;
            if(K <= cardA)
            {
                ans = K;
            }
            else if(K <= cardA + cardB)
            {
                ans = cardA;
            }
            else
            {
                ans = cardA - (K - cardA - cardB);
            }

            Console.WriteLine(ans);
        }
    }
}


Measured memory_bytes: 28208
#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
	int H[10];
	
	for(int i = 0; i < 10; i++){
		scanf("%d", H + i);
	}
	
	sort(H, H + 10);
	
	for(int i = 1; i <= 3; i++){
		printf("%d\n", H[10 - i]);
	}
	
	return 0;
}

Measured memory_bytes: 1044