akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>void __f(const char* name, Arg1&& arg1){cout<<name<<" : "<<arg1<<"\n";}
template <typename Arg1, typename... Args>void __f(const char* names, Arg1&& arg1, Args&&... args)
{const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);} 
#define traceloop(x,a) { cerr<<#a<<": "; for(ll ix=0;ix<x;ix++) cerr<<a[ix]<<" "; cerr<<endl; }

const ll MOD = 1e9+7;
const ll maxn = 1e5+5;

ll f[maxn+2];
ll inv[maxn+2];
ll invf[maxn+2];

void pre()
{
	ll i;
	f[0]=f[1]=1LL;
	invf[0]=invf[1]=1LL;
	inv[0]=inv[1]=1LL;

	// factorial
	for(i=2;i<maxn;++i)
		f[i] = (i * f[i-1]) % MOD;
	
	// inverse
	for(i=2;i<maxn;++i)
		inv[i] = inv[MOD % i] * (MOD - MOD/i) % MOD;
	
	// inverse of factorial
	for(i=2;i<maxn;++i)
		invf[i] = (inv[i] * invf[i-1])% MOD;
}

ll C(ll n, ll r)
{
	if(n < r)
		return 0;
	return (((f[n]*invf[n-r])%MOD)*invf[r])%MOD;
}

int main()
{
	ios::sync_with_stdio(0);
	ll i, j, k, n;

	pre();

	cin >> n >> k;
	vector<ll> a(n);
	for(i=0;i<n;++i)
		cin >> a[i];

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

	ll ans = 0;
	for(i=1;i<n;++i)
	{
		ll temp = (a[i] - a[i-1] + MOD) % MOD;
		ll temp1 = C(n, k);
		temp1 = (temp1 - C(i, k) + MOD) % MOD;
		temp1 = (temp1 - C(n-i, k) + MOD) % MOD;
		temp = temp * temp1 % MOD;

		ans = (ans + temp) % MOD;
	}

	cout << ans << "\n";

	return 0;
}

Measured memory_bytes: 3328
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n; cin >> n;
    vector<int> b(n);
    vector<int> ans;

    for (int i = 0; i < n; i++) { cin >> b[i]; }

    for (int i = 0; i < n; i++) {
        if (b[i] > i+1) {
            cout << -1 << endl;
            return 0;
        }
    }

    while (n--) {
        for (int j = n; j >= 0; j--) {
            if ( b[j] == j+1) {
                ans.push_back(b[j]);
                b.erase(b.begin()+j);
                break;
            }
        }
    }
    reverse(ans.begin(), ans.end());

    for (auto &e: ans) { cout << e << endl; }

    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

#define MAX_K (100000)
#define INF (1000000000000000000L)

long calc_final(const long *as, long k, long n) {
  long ret = n;
  for (int i = 0; i < k; i++) {
    ret = ret / as[i] * as[i];
  }
  return ret;
}

long calc_lb(long b, long e, const long *as, long k, long thresh) {
  if (e - b <= 1) {
    if (calc_final(as, k, b) >= thresh) {
      return b;
    } else {
      return -1;
    }
  }
  const long m = (b + e - 1) / 2, num = calc_final(as, k, m);
  if (num >= thresh) {
    return calc_lb(b, m + 1, as, k, thresh);
  } else {
    return calc_lb(m + 1, e, as, k, thresh);
  }
}

int main(int argc, char *argv[]) {
  // read inputs
  long K, as[MAX_K];
  scanf("%ld", &K);
  for (int i = 0; i < K; i++) {
    scanf("%ld", &as[i]);
  }

  // solve
  const long ans_min = calc_lb(2, INF, as, K, 2);
  const long ans_max = calc_lb(2, INF, as, K, 3) - 1;
  if (calc_final(as, K, ans_min) == 2 && calc_final(as, K, ans_max) == 2) {
    printf("%ld %ld\n", ans_min, ans_max);
  } else {
    printf("-1\n");
  }

  return 0;
}


Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";}; 
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

void solve(){
    ll n,k;
    cin >> n >> k;
    Vi lamp(n);
    rp(i,n) cin >> lamp.at(i);
    rp(i,k){
        Vi add(n+1,0);
        ll sum;
        rp(j,n){
            add[max(0ll,j-lamp[j])]+=1;
            add[min(n,j+lamp[j]+1)]-=1;
        }
        sum=lamp[0]=add[0];
        repi(i,1,n){
            lamp[i]=lamp[i-1]+add[i];
            sum+=lamp[i];
        }
        if(sum==n*n){
            rp(i,n) princ(n);
            cout << endl;
            return;
        }
    }
    rp(i,n) princ(lamp[i]);
    cout << endl;
    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}

Measured memory_bytes: 6328
#include<iostream>
#include<map>
using namespace std;

map<int,int>mark;

int main()
{
    int n,i,temp,counter=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>temp;
        if(mark[temp]==1)
        {
            mark[temp]=0;
            counter--;
        }
        else
        {
            mark[temp]=1;
            counter++;
        }
    }
    cout<<counter<<endl;
    return 0;
}


Measured memory_bytes: 4992
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    int n;
    ll c, a[100005] = {}, b[100005] = {}, d[100005] = {}, e[100005] = {}, f[100005] = {}, g[100005] = {}, z = 0;
    cin >> n >> c; a[n + 1] = c;
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for (int i = 1; i <= n; i++) d[i] = d[i - 1] + b[i] - (a[i] - a[i - 1]), f[i] = f[i - 1] + b[i] - (a[i] - a[i - 1]) * 2;
    for (int i = n; i; i--) e[i] = e[i + 1] + b[i] - (a[i + 1] - a[i]), g[i] = g[i + 1] + b[i] - (a[i + 1] - a[i]) * 2;
    for (int i = 1; i <= n; i++) d[i] = max(d[i - 1], d[i]), f[i] = max(f[i - 1], f[i]);
    for (int i = n; i; i--) e[i] = max(e[i], e[i + 1]), g[i] = max(g[i], g[i + 1]);
    for (int i = 0; i <= n; i++) z = max({z, d[i] + g[i + 1], f[i] + e[i + 1]});
    //for (int i = 0; i <= n; i++) cout << d[i] << ' ' << e[i] << ' ' << f[i] << ' ' << g[i] << '\n';
    cout << z;
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main()
{
    ll n,a,b,c,d,e; cin >> n >> a >> b >> c >> d >> e;

    ll x = min(min(a,b),c);
    x = min(min(x,d),e);

    ll ans = 0;
    ll s = (n + (x - 1)) / x;
    ans += s;

    cout << ans + 4 << endl; 

    return 0;
}

Measured memory_bytes: 256
import java.util.*;
import java.io.*;

public class Main {
	
	public static String fix(String s, int h) {
		while(s.length() < h - 1)
			s = "0" + s;
		return s;
	}
	
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		
		int h = sc.nextInt(), w = sc.nextInt(), k = sc.nextInt();
		int[][] mat = new int[h][w];
		for(int i = 0; i < h; i++) {
			String s = sc.next();
			for(int j = 0; j < w; j++) {
				mat[i][j] = Integer.parseInt("" + s.charAt(j));
			}
		}
		
		int min = h - 1 + w - 1;
		big: for(int prm = 0; prm <= (1 << (h - 1)) - 1; prm++) {
			int b = Integer.bitCount(prm);
			String s = fix(Integer.toBinaryString(prm), h);
			int[] count = new int[b + 1];
			
			int c = b;
			int pre = -1;
			for(int i = 0; i < w; i++) {
				int[] newCount = new int[b + 1];
				int idx = 0;
				for(int j = 0; j < h; j++) {
					if(j > 0 && (s.charAt(s.length() - j) == '1'))
						idx++;
					newCount[idx] += mat[j][i];
				}
				boolean flag = true;
				for(int u = 0; u <= b; u++) {
					pre = u - 1;
					count[u] += newCount[u];
					if(count[u] > k) {
						flag = false;
						break;
					}
				}
				
				if(!flag) {
					c++;
					for(int u = 0; u <= b; u++) {
						count[u] = newCount[u];
					}
					flag = true;
					for(int u = 0; u <= b; u++) {
						if(count[u] > k) {
							flag = false;
							break;
						}
					}
					if(!flag) {
						continue big;
					}
				}
			}
			
			min = Math.min(min, c);
		}
		
		pw.println(min);
		pw.flush();
	}

	public static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


Measured memory_bytes: 39284
/*
 * ex 4_2
 * kamecha
 */
#include<stdio.h>
#include<math.h>

int main(void){
    double h_hand, m_hand, hour, minute;
    scanf("%lf %lf %lf %lf", &h_hand, &m_hand, &hour, &minute);
    double time = 60*hour + minute;
    //角速度の差を出しておく
    double theta = time*M_PI*11/360;
    //余弦定理
    double ans = sqrt(h_hand*h_hand + m_hand*m_hand - 2*h_hand*m_hand*cos(theta));
    printf("%.10lf\n", ans);
    return 0;
}


Measured memory_bytes: 2560
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
//const int mo=0;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}


char s[222222];
int main()
{

	int n;
	gs(s+1);
	n=strlen(s+1);
	int cnt=0;
	int ans=0;
	rep(i,1,n+1){
		if(s[i]=='S')cnt++;
		else{
			if(cnt)cnt--;
			else ans++;
		}
	}
	cout<<cnt+ans<<endl;
	return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
#define MOD (1000000007)
using namespace std;

const int N=2e5+10;
int f[N], g[N];
int num[N], p;

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n;
    cin>>n;
    int v;
    for(int i=0; i<n; i++) {
        cin>>v;
        if(num[p]!=v) num[++p]=v;
    }
    f[0]=1;
    for(int i=1; i<=p; i++) {
        f[i]=(f[i-1]+g[num[i]])%MOD;
        (g[num[i]]+=f[i-1])%=MOD;
    }
    cout<<f[p]<<endl;
    return 0;
}

Measured memory_bytes: 2560
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<climits>
using namespace std;
const int N=3e5+7;
int n,a[N];
char s[N];
int q[N],h[N];
int main(){
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i = 1;i<=n;i++){
		q[i]=q[i-1]+(s[i]=='W'?1:0);
	}
	for(int i = n;i>=1;i--){
		h[i]=h[i+1]+(s[i]=='E'?1:0);
	}
	int ans=INT_MAX;
	for(int i = 1;i<=n;i++){
		a[i]=q[i-1]+h[i+1];
		ans=min(a[i],ans);
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 4096
def dfs(node, visited, g):
    visited[node] = True
    for neib in g[node]:
        if not visited[neib]:
            dfs(neib, visited,g)


N, M = map(int, input().split()) 
abl = []
for _ in range(M):
    a, b = map(int, input().split()) 
    abl.append([a,b])

ans = 0
for i in range(M):
    g = [ [] for _ in range(N+1)]
    for j, ab in enumerate(abl):
        if j == i: continue
        a,b = ab
        g[a].append(b)
        g[b].append(a)
    visited = [False] * (N+1)
    visited[0] = True
    dfs(1, visited, g)
    if False in visited:
        ans+=1 

print(ans)


Measured memory_bytes: 3064
/*
* @author:  codancer
*/
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
#define pb push_back
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef pair<int,int> pii;
const int N = 2e5+100;
int par[N],siz[N];
void init(int n){
	for(int i=1;i<=n;i++){
		par[i]=i;
		siz[i]=1;
	}
}
int find(int x){
	return x==par[x]?x:par[x]=find(par[x]);
}
void unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y) return ;
	par[x]=y;
	siz[y]+=siz[x];
	siz[x]=0;
}
bool same(int x,int y){
	return find(x)==find(y); 
}
int adj[N];
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	init(n);
	int u,v; 
	vector<int> ans(n+1);
	for(int i=1;i<=m;i++){
		cin>>u>>v;
		adj[u]++;
		adj[v]++;
		if(!same(u,v)){
			unite(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		ans[i]=siz[find(i)]-adj[i]-1;
	}
	for(int i=1;i<=k;i++){
		cin>>u>>v;
		if(find(u)==find(v)){
			ans[u]--;
			ans[v]--;
		}
	}
	cout<<ans[1];
	for(int i=2;i<=n;i++){
		cout<<' '<<ans[i];
	}
	cout<<endl;
	return 0;
}


Measured memory_bytes: 3072
def main():
    n,m=map(int,input().split())
    ab=[list(map(int,input().split())) for _ in [0]*m]
    g=[set() for _ in [0]*n]
    g2=[set() for _ in [0]*n]
    [g[a-1].add(b-1) for a,b in ab]
    [g2[b-1].add(a-1) for a,b in ab]

    def bfs(start,graph):
        temp=[[start,{start}]]
        while temp:
            temp2=[]
            for j,k in temp:
                for i in graph[j]:
                    if i in k:
                        return j,k
                    else:
                        temp2.append([i,k|{i}])
            temp=temp2
        return None,None

    return_set_pre=set(range(n))
    for i in range(n):
        now,return_set=bfs(i,g)
        if return_set!=None:
            break
    if return_set==None:
        print(-1)
        return 0
    while True:
        for i in return_set_pre-return_set:
            g[i]={}
            g2[i]={}
        for i in return_set:
            g[i]=g[i]&return_set
            g2[i]=g2[i]&return_set
        l1=len([1 for i in range(n) if len(g[i])==1])
        l2=len([1 for i in range(n) if len(g2[i])==1])
        l=len(return_set)
        if l==l1 and l==l2:
            break
        return_set_pre=return_set
        now,return_set=bfs(now,g2)
        for i in return_set_pre-return_set:
            g[i]={}
            g2[i]={}
        for i in return_set:
            g[i]=g[i]&return_set
            g2[i]=g2[i]&return_set
        l1=len([1 for i in range(n) if len(g[i])==1])
        l2=len([1 for i in range(n) if len(g2[i])==1])
        l=len(return_set)
        if l==l1 and l==l2:
            break
        return_set_pre=return_set
        now,return_set=bfs(now,g)
    print(len(return_set))
    for i in return_set:
        print(i+1)
main()

Measured memory_bytes: 4716
#include <iostream>
using namespace std;
int main () {
	int a, b;

	cin >> a >> b;

	if ( ((a >= -1000) && (a <= 1000)) && ((b >= -1000) && (b <= 1000)) ) {
		if (a < b) {
			cout << "a < b\n";
		} else if (a > b) {
			cout << "a > b\n";
		} else {
			cout << "a == b\n";
		}
	} else {
		cout << "Input number is invalid.\n";
	}
	return 0;
}

Measured memory_bytes: 1124
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE 

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef pair<int, int> P;
typedef long long ll;

string field[20];
int id[20][20];
int dist[11][11];
int vis[20][20];

const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, 1, 0, -1 };

int dp[11][1<<11];
int n;
int rec(int pos, int visit){
	if (visit + 1 == 1 << n) return 0;

	int &res = dp[pos][visit];
	if (res >= 0) return res;

	res = 1<<30;
	for (int i = 1; i < n; ++i){
		if (visit&(1 << i)) continue;
		res = min(res, dist[pos][i] + rec(i, visit | (1 << i)));
	}

	return res;
}

int main(){
	int w, h;
	while (cin >> w >> h, w){
		vector<string> v(h);
		rep(i, h) cin >> v[i];

		MEMSET(id, -1);
		n = 1;
		rep(i, h) rep(j, w){
			if (v[i][j] == 'o') id[i][j] = 0, v[i][j] = '*';
			else if (v[i][j] == '*') id[i][j] = n++;
		}
		MEMSET(dist, -1);
		rep(i, h) rep(j, w){
			if (v[i][j] != '*') continue;
			P pos = MP(j, i);
			queue<tuple<int, int, int>> q;
			q.push(MT(j, i, 0));
			MEMSET(vis, 0);
			vis[i][j] = 1;
			while (!q.empty()){
				int x, y, d;
				tie(x, y, d) = q.front();
				q.pop();

				if (id[y][x] >= 0) dist[id[i][j]][id[y][x]] = d;

				rep(dir, 4){
					int nx = x + dx[dir], ny = y + dy[dir];
					if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
					if (v[ny][nx] == 'x') continue;
					if (vis[ny][nx]) continue;
					vis[ny][nx] = 1;
					q.push(MT(nx, ny, d + 1));
				}
			}
		}

		rep(i, n) rep(j, n){
			if (dist[i][j] < 0) goto NG;
		}

		MEMSET(dp, -1);
		cout << rec(0, 1) << endl;

		continue;
	NG:
		cout << -1 << endl;
	}


	return 0;
}

Measured memory_bytes: 1324
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
signed main(){
  ll a,b,c,d, ac, ad, bc, bd, acad, bcbd, r;
  
  cin >> a >> b >> c >> d;
  ac = a * c;
  ad = a * d;
  bc = b * c;
  bd = b * d;
  acad = (ac>ad)?ac:ad;
  bcbd = (bc>bd)?bc:bd;
  r = (acad>bcbd)?acad:bcbd;
  cout << r << endl;
}

Measured memory_bytes: 3632
//  Pyon-Pyon River Crossing.cpp
//  2013/01/02.

#if 1

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

const int X = 1005;
const int Y = 155;
const int MM = 80;
const int INF = 1 << 30;

int N, M;
int dp[3][X][MM];

int main()
{
	for(; cin >> N >> M && (N || M); )
	{
		vector<vector<vint> > dp(3, vector<vint>(X, vint(MM, INF)));
		dp[0] = vector<vint>(X, vint(MM, 0));
		rep(x, X) rep(m, M) dp[1][x][m] = 0;
		
		vector<vector<pint> > river;
		
		rep(y, N)
		{
			int k;
			cin >> k;
			
			river.push_back(vector<pint>(k));
			
			rep(i, k) cin >> river[y][i].first >> river[y][i].second;
		}
		
		river.push_back(vector<pint>(X));
		river.push_back(vector<pint>(X));
		rep(i, X) river[N][i] = mp(i, 0), river[N+1][i] = mp(i, 0);
		
		rep(y, N)
		{
			rep(i, river[y].size())
			{
				rep(m, M+1)
				{
					rep(j, river[y+1].size())
					{
						int d = (river[y+1][j].second + river[y][i].second) * abs(river[y+1][j].first - river[y][i].first);
						chmin(dp[1][river[y+1][j].first][m], dp[0][river[y][i].first][m] + d);
					}
					
					if(m > 0) rep(j, river[y+2].size())
					{
						int d = (river[y+2][j].second + river[y][i].second) * abs(river[y+2][j].first - river[y][i].first);
						chmin(dp[2][river[y+2][j].first][m-1], dp[0][river[y][i].first][m] + d);
					}
				}
			}
			
			dp[0] = dp[1];
			dp[1] = dp[2];
			rep(x, X) rep(m, MM) dp[2][x][m] = INF;
		}
		
		int ans = INF;
		rep(x, X) rep(m, MM) chmin(ans, dp[0][x][m]);

		cout << ans << endl;
	}
}

#endif

Measured memory_bytes: 2588
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    a,b = a-1, b-1
    g[a].append(b)
    g[b].append(a)

from collections import deque
q = deque([])
q.append(0)
visit = [-1]*n
visit[0] = 0
while q:
    v = q.popleft()
    for u in g[v]:
        if visit[u] == -1:
            visit[u] = v
            q.append(u)
print('Yes')
for i in range(1, n):
    print(visit[i]+1)


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


int H, W;

int main(){
	cin >> H >> W;
	char C[H+1][W+1];
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin >> C[i][j];
		}
	}
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cout << C[i][j] ;
		}
		cout << endl;
		for(int j=0;j<W;j++){
			cout << C[i][j];
		}
		cout << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 10000000000000000

long long gcd(long long a, long long b){
	if (b == 0) return a;
	return gcd(b, a % b);
}
 
long long lcm(long long a, long long b){
	if (b > a) swap(a, b);
	return a / gcd(a, b) * b;
}

int main(){
	
	int T;
	cin>>T;
	
	for(int i=0;i<T;i++){
		long long A,B,C,D;
		cin>>A>>B>>C>>D;
		
		if(A<B||D<B){
			cout<<"No"<<endl;
			continue;
		}
		if(C>=B){
			cout<<"Yes"<<endl;
			continue;
		}
		
		long long G = gcd(B,D);
		
		A%=D,B%=D,C%=D;
		long long X = (D+C+1)%D,Y = (D+D+B-1)%D;
		
		if(X>Y){
			cout<<"Yes"<<endl;
			continue;
		}
		
		X = (X+D-A)%D,Y = (Y+D-A)%D;
		
		if(Y<X){
			cout<<"No"<<endl;
			continue;
		}
		X = (X+G-1)/G,Y = Y/G;
		
		//cout<<G<<','<<X<<','<<Y<<endl;
		if(X<=Y)cout<<"No"<<endl;
		else cout<<"Yes"<<endl;
	}
		
	
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,-1,0,1},dy={1,0,-1,0};



int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;cin >> n;
	vector<pii> a(n);
	rep(i,n) cin >> a.at(i).first >> a.at(i).second;
	sort(a.begin(),a.end());
	int ans=n;
	map<pii,int> m;
	rep(i,n){
		for(int j=i+1;j<n;++j){
			int x=a.at(i).first-a.at(j).first,y=a.at(i).second-a.at(j).second;
			m[mp(x,y)]++;
		}
	}
	for(auto i:m){
		ans=min(ans,n-i.second);
	}
	cout << ans << endl;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    while(1)
    {
        c=c-b;
        if(c<=0)
        {
            cout<<"Yes"<<endl;
            break;
        }
        a=a-d;
        if(a<=0)
        {
            cout<<"No"<<endl;
            break;
        }
    }
    return 0;
}



Measured memory_bytes: 3592
#include <iostream>
#include <set>
using namespace std;
long long int A[100000];
int main(void){
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) cin >> A[i];
    
    multiset<long long int> color;
    color.insert(A[N-1]);
    for (int i = N-2; i >= 0; i--) {
        auto c = color.upper_bound(A[i]);
        if (c == color.end()) {
            color.insert(A[i]);
        }
        else {
            color.erase(c);
            color.insert(A[i]);
        }
    }
    
    cout << color.size() << endl;
}

Measured memory_bytes: 5760
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

void print(int A[], int N){
    for(int i=0;i<N;i++){
        if(i>0)printf(" ");
        printf("%d",A[i]); 
    }
    printf("\n");
}

void insert(int A[], int N){
    int v,j;
    for(int i=1;i<N;i++){
        v = A[i];
        j = i - 1;
        while(j>=0 && A[j]>v){
            A[j+1] = A[j];
            j--;
        }
        A[j+1] = v;
        print(A, N);
    }
}
int main(){
   int N;
   int A[100];

   cin >> N;
   for(int i = 0;i<N;i++) cin >> A[i];
   print(A, N);
   insert(A, N);
 return 0;

}



Measured memory_bytes: 3192
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_(i, n, m) for (int i = n; i < (int)(m); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define stoi stoll
//#define _GLIBCXX_DEBUG

vector<int> s(vector<int> b, vector<int> a) {
  if(b.size() == 0) return a;
  bool k = false;
  for(int i=b.size()-1; i>=0; i--) {
    if(b[i]-1 == i) {
      k = true;
      vector<int> bb = b;
      bb.erase(bb.begin()+i);
      vector<int> aa = a;
      aa.push_back(i+1);
      return s(bb, aa);
      //if(ss.size() > 0) return ss;
    }
  }
  if(!k) return {};
}

signed main() {
  int N; cin >> N;
  vector<int> b(N);
  rep(i, N) cin >> b[i];
  
  vector<int> a = {};
  vector<int> ss = s(b, a);
  if(ss.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  reverse(all(ss));
  rep(i, N) cout << ss[i] << endl;
  
}


Measured memory_bytes: 3840
#include <bits/stdc++.h>
/* attention */
#define rep(i, n) for (int i = 1; i < (n); ++i)
/* attention */
using namespace std;
using ll = long long;

vector<vector<int>> GCD(201, vector<int>(201, -1));

int calc_gcd (int a, int b) {
    if (b > a) swap(a, b);
    if (a % b == 0) {
        GCD.at(a).at(b) = b;
    } else {
        GCD.at(a).at(b) = calc_gcd(b, a%b);
    }
    return GCD.at(a).at(b);
}
int main (void){
    ll ans = 0;
    int K;
    cin >> K;
    rep(i, K+1) rep(j, K+1) rep(k, K+1) ans += calc_gcd(calc_gcd(i, j), k);
    cout << ans << endl;
}


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

int main(){
  int N, count = 0;
  cin >> N;
  for(int i = 1; i <= N; i++){
  	if(to_string(i).length() % 2 == 1){
    	count++;
    }
  }
  cout << count << endl;
  return 0;
}

Measured memory_bytes: 256
//↓template↓

#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n)  n.begin(),n.end()
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
const int SIZE = 1 << 17;
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 }, alp[30];
long long fac[200005], finv[200005], inv[200005];
vector<long long>dij;
struct edge { long long to, cost; };
vector<vector<edge> >G;

long long mod_pow(long long a, long long b) {
	long long res = 1, mul = a;
	for (int i = 0; i < 31; ++i) {
		if (b >> i & 1) {
			res *= mul;
			res %= MOD;
		}
		mul = (mul * mul) % MOD;
	}

	return res;
}

void addedge(int from, int to, int cost) {
	G[from].push_back({ to,cost });
	G[to].push_back({ from,cost });
}

//↑template↑


int dif = 10000;
int main() {
	string h;
	cin >> h;
	for (int i = 2; i < h.length(); ++i) {
		dif = min(dif, abs(stoi(h.substr(i - 2, 3)) - 753));
	}
	cout << dif << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define per(a, b, c) for(int a = (b); a >= (c); -- a)
using namespace std;
template <class T> void rd(T &x){
  x = 0; int 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();
  x *= f;
}

const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
int n, m;
int s[2][maxn];
int mi[maxn << 2][2];

struct node{ int l, r;
  bool operator < (const node &b) const {
	return l == b.l ? r < b.r : l < b.l;
  }
}q[maxn];

#define lson u << 1, l, mid
#define rson u << 1 | 1, mid + 1, r

void update(int u, int l, int r, int x, int v, int op){
  mi[u][op] = min(mi[u][op], v);
  if(l == r) return ;
  int mid = l + r >> 1;
  if(x <= mid) update(lson, x, v, op);
  else update(rson, x, v, op);
}

int query(int u, int l, int r, int ql, int qr, int v, int op){
  if(ql <= l && r <= qr) return mi[u][op] + v;
  int mid = l + r >> 1, res = inf;
  if(ql <= mid) res = min(res, query(lson, ql, qr, v, op));
  if(qr > mid) res = min(res, query(rson, ql, qr, v, op));
  return res;
}

void sol(){
  int ans = s[0][1];
  update(1, 0, n, 0, s[1][1], 1);
  update(1, 0, n, 0, s[0][1], 0);
  rep(i, 1, m){
	int l = q[i].l, r = q[i].r;
	int t = query(1, 0, n, l, r, -s[1][r + 1], 1);
	t = min(t, query(1, 0, n, 0, l - 1, -s[0][l] + s[1][l] - s[1][r + 1], 0));
	update(1, 0, n, r, t + s[1][r + 1], 1);
	update(1, 0, n, r, t + s[0][r + 1], 0);
	ans = min(ans, t + s[0][r + 1]);
  }
  printf("%d\n", ans);
}

int main(){
  memset(mi, 0x3f, sizeof(mi));
  rd(n);
  rep(i, 1, n){
	int x; rd(x);
	s[1 - x][i] = 1;
  }
  per(i, n, 1) rep(j, 0, 1) s[j][i] += s[j][i + 1];
  rd(m);
  rep(i, 1, m) rd(q[i].l), rd(q[i].r);
  sort(q + 1, q + m + 1);
  sol();
  return 0;
}


Measured memory_bytes: 9600
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;


class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        this.inpt = Reader.ReadLine();
        long ans = this.GetAns(0, "");
        Console.WriteLine(ans);
    }

    string inpt;

    private long GetAns(int idx, string tmp)
    {
        if (idx >= inpt.Length)
        {
            return tmp.Split('+').Sum(a => long.Parse(a));
        }
        long ans = 0;
        ans += this.GetAns(idx + 1, tmp + inpt[idx]);
        if (idx > 0)
        {
            ans += this.GetAns(idx + 1, tmp + "+" + inpt[idx]);
        }
        return ans;
    }


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


9999999999

";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}

Measured memory_bytes: 3416
#include <iostream>
using namespace std;
using ll = long long;

ll gcd(ll a, ll b) {
  if (min(a, b) == 0) {
    return max(a, b);
  }
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

bool solve(ll a, ll b, ll c, ll d) {
  if (a < b) {
    return false;
  }
  if (b > d) {
    return false;
  }
  if (b <= c + 1) {
    return true;
  }
  if (b == d) {
    if (a - b * (a / b) < c) {
      return true;
    } else {
      return false;
    }
  }
  if (a % b > c) {
    return false;
  }
  ll t = gcd(d - b, b);
  ll s = a - t * (a / t);
  ll x = s + t * ((c + t - s) / t);
  if (x < b) {
    return false;
  }
  return true;
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if (solve(a, b, c, d)) {
      cout << "Yes" << '\n';
    } else {
      cout << "No" << '\n';
    }
  }
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main() {
    ll n, m;
    cin >> n >> m;
    vector<pair<ll, ll>> v;
    rep(i, 0, n) {
        ll a;
        cin >> a;
        v.emplace_back(a, 1);
    }
       
    rep(i, 0, m) {
        ll b, c;
        cin >> b >> c;
        v.emplace_back(c, b);
    }
    sort(v.begin(), v.end());

    ll ans = 0, cnt = 0;
    while(cnt < n) {
        auto p = v.back();
        v.pop_back();
        if (p.second > n - cnt) ans += p.first * (n - cnt);
        else ans += p.first * p.second;
        cnt += p.second;
    }
    cout << ans << endl;
}

Measured memory_bytes: 6384
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] arr = new int[n];
        boolean[] used = new boolean[n];
        for (int i = 0; i < m; i++) {
            int s = sc.nextInt() - 1;
            int c = sc.nextInt();
            if (used[s] && c != arr[s]) {
                System.out.println(-1);
                return;
            }
            if (s == 0 && c == 0 && n != 1) {
                System.out.println(-1);
                return;
            }
            used[s] = true;
            arr[s] = c;
        }
        if (n != 1 && arr[0] == 0) {
            arr[0] = 1;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(arr[i]);
        }
		System.out.println(sb);
   }

}


Measured memory_bytes: 22612
#include<bits/stdc++.h>
#define F(i, l, r) for(long long i =l; i<=r; ++i)
#define REP(i, r) for(int i = 0; i<(int)r; ++i)
#define Fr(i, l, r) for(long long i= l; i>=r; --i)
#define RP (i, u) for(i = u-1; i>=0; --i)
#define pb push_back
#define X first
#define Y second
#define mk make_pair
#define sz size()
#define DEBUG(x) {cerr << #x << " = " << x << '\n';}
#define PR(A, n) {cerr << #A << " = "; FOR(i, 1, n) cerr << A[i] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; REP(i, n) cerr << A[i] << ' '; cerr << '\n';}
#define Nmax 100009
#define BASE 1000000007
#define name "  "
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int inf = 1e9;
template<class T> int getbit(T x, int pos) {return (x >> (pos-1)) & 1;}
template<class T> void turn_on(T &x, int pos) {x = x | ((T)1 << (pos-1));}
template<class T> void turn_off(T &x, int pos) {x = x & ~((T)1 << (pos-1));}
template<class T> T sqr(T a) {return a*a;}
ll n, m, t;
ll gt(ll n)
{
  ll  r = 1;
  F(i, 2, n) r = (r*i)%BASE;
  return r;
}
int main()
{
  //  freopen(name".INP", "r", stdin);
  //  freopen(name".OUT", "w", stdout);
cin >> n  >> m;
if(abs(n-m) > 1) { cout<<0; return 0; }
 if(n == m) t = 2; else t =1;
 if(n < m) swap(n, m);
 n = gt(n)%BASE;
 m = ((gt(m)*t)%BASE + BASE)%BASE;
 ll r = ((n*m)%BASE+BASE)%BASE;
 cout<<r;
}


Measured memory_bytes: 256
#include <stdio.h>

int main() {
    int r, c, sps[100][100], rsum = 0, csum = 0;
    scanf("%d %d", &r, &c);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            scanf("%d", &sps[i][j]);
            printf("%d ", sps[i][j]);
            rsum += sps[i][j];
        }
        printf("%d\n", rsum);
        rsum = 0;
    }

    for (int j = 0; j < c; j++) {
        for (int i = 0; i < r; i++) {
            csum += sps[i][j];
        }
        printf("%d ", csum);
        rsum += csum;
        csum = 0;
    }
    printf("%d\n", rsum);
}


Measured memory_bytes: 2048
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define ll long long int
#define lim 100010
#define INF 1e9
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)<(y)?(y):(x))
#define ABS(x) ((x)>0?(x):-(x))

typedef struct{
	ll did,fin;
}time;

int cmp(const void *x, const void *y){
	return (((time*)x)->fin - ((time*)y)->fin);
}

int main(void){
		int n;
	scanf("%d",&n);
	ll FinishTime=0;
	time task[n];
	for(int i=0;i<n;i++){
		scanf("%lld%lld",&task[i].did,&task[i].fin);
	}
	qsort(task,n,sizeof(time),cmp);
	
	bool finish=true;
	int t=0;
	while(t<n){
		FinishTime+=task[t].did;
		if(FinishTime>task[t].fin){
			finish=false;
			break;
		}
		t++;
	}
	if(finish)printf("Yes");
	else printf("No");

	return 0;
}

Measured memory_bytes: 6396
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(),v.end()
typedef long long ll;

int main(){
    int A,B,C;
    cin >> A >> B >> C;
    bool kawaisou = false;
    if (A == B && B != C) kawaisou = true;
    else if (A == C && B != C) kawaisou = true;
    else if (B == C && A != C) kawaisou = true;
    if (kawaisou) cout << "Yes" << endl;
    else cout << "No" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX= 100005 ;
const long long mod = 1.0e9+7;
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t;
    t=1;
    while(t--)
    {
        int n,k,i,x,y,z,j;
        cin>>n>>k;
        int arr[n+1];
        for(i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        int vis[n+1],pos=1;
        memset(vis,0,sizeof(vis));
        vector<int> v;
        while(vis[pos]!=1)
        {
            v.push_back(pos);
            vis[pos]=1;
            pos=arr[pos-1];
        }
        //cout<<v.size()<<endl;
        if((k)<v.size())
        {
            cout<<v[k]<<endl;
        }
        else
        {
            for(i=0;i<v.size();i++)
            {
                if(v[i]==pos)
                    break;
            }
            vector<int> v1;
            for(j=i;j<v.size();j++)
            {
                v1.push_back(v[j]);
            }
            //cout<<"HH"<<v1.size()<<endl;
            int len=v1.size();
            k=k-i;
            //cout<<k<<" "<<len<<endl;
            cout<<v1[k%len]<<endl;
        }
        
    }
    return 0;
}

Measured memory_bytes: 10748
#include<stdio.h>

#define MAX 1000
#define VMAX 10000

int A[MAX] ,n,s;
int B[MAX] , T[VMAX+1];

void sort(int a[]){
	int x=1;
	int copy;
	int j;
	
	while( x== 1 ){
		x = 0;
		
		for( j = n - 1; j >= 1; j-- ){
			if( a[j] < a[ j - 1 ] ){
				copy = a[j];
				a[j] = a[j - 1];
				a[j - 1] = copy;
				x = 1;
			}
		}
	}
	
}

int min( int a , int b ){
	if( a < b ){ 
		return a; 
	}else{ 
		return b; 
	}
}

int solve(){
	int ans = 0;
	int V[MAX];
	
	for( int i = 0; i < n; i++ ){
		B[i] = A[i];
		V[i] = 0;
	}
	
	sort(B);
	
	for( int i = 0; i < n; i++ ) T[B[i]] = i;
	for( int i = 0; i < n; i++ ){
		if( V[i] ) continue;
		
		int cur = i;
		int S = 0;
		int m = VMAX;
		int an = 0;
		
		while(1){
			V[cur] = 1;
			an++;
			int v = A[cur];
			m = min(m,v);
			S += v;
			cur = T[v];
			if( V[cur] ){ break; }
		}
		ans += min( S+(an-2) * m , m + S + (an + 1 ) * s );
	}
	
	return ans;
}

int main( void ){
	int ans,i;
	scanf("%d",&n );
	
	s = VMAX;
	
	for(i=0; i<n;i++ ){
		scanf( "%d", &A[i] );
		s = min(s,A[i]);
	}
	
	ans = solve();
	
	printf( "%d\n", ans );
	
	return 0;
}



Measured memory_bytes: 2108
e=[]
while 1:
  l=[list(map(int,input().split())) for i in range(int(input()))]
  if not l:break
  d={}
  for i in l:
    if i[0] not in d:
      d[i[0]]=i[1]*i[2]
    else:
      d[i[0]]+=i[1]*i[2]
  e.append(d)
f=0
for i in e:
  f=0
  for j in i.items():
    if j[1]>=1000000:
      print(j[0])
      f=1
  if not f:
    print("NA")


Measured memory_bytes: 5988
#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<string>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack> 
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
//const int MAX_V=105;

int main(){
  int a,b,h;
  cin >> a >> b >> h;
  cout << (a+b)*h/2 << endl;

    return 0;
}
 

Measured memory_bytes: 256
using System;
class Problem02{
	static void Main(string []args){
		string t=Console.ReadLine();
		int X=int.Parse(t);
		for(int i=X;i>=1;i--){
			if(beki(i)==true){
				Console.WriteLine(i);
				break;
			}
		}
	}
	static bool beki(int x){
			if(x==1) return true;
		for(int i=2;i<=x/2;i++){
			if(x%i!=0) continue;
			int n=x;
			for(int j=2;n>=0;j++){
				n /= i;
				if(n==1)return true;
				else if(n%i!=0)break;
			}
		}
		return false;
	}
}

Measured memory_bytes: 11092
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

int main(){
    long long a,b,c,d;
    cin>>a>>b>>c>>d;
    long long ans;
    ans=max(a*c,max(a*d,max(b*c,b*d)));
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000000000000
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define pb push_back
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int kaijo[2000010];
struct edge{int to,cost;};
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
void init_fact(){
    kaijo[0]=1;
    for(int i=1;i<=2000000;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=MOD;
    }
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%MOD;
    int memo=modpow(a,b/2);
    return memo*memo%MOD;
}
int comb(int a,int b){
    if(!kaijo[0])init_fact();
    return kaijo[a]*modpow(kaijo[a-b],MOD-2)%MOD*modpow(kaijo[b],MOD-2)%MOD;
}
int inv(int x){
    x=modpow(x,MOD-2);
    return x;
}
bool kosa(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy){
    double ta=(cx-dx)*(ay-cy)+(cy-dy)*(cx-ax);
    double tb=(cx-dx)*(by-cy)+(cy-dy)*(cx-bx);
    double tc=(ax-bx)*(cy-ay)+(ay-by)*(ax-cx);
    double td=(ax-bx)*(dy-ay)+(ay-by)*(ax-dx);
    return tc*td<0&&ta*tb<0;
}
double dist(double ax,double ay,double bx,double by){
    return sqrt((bx-ax)*(bx-ax)+(ay-by)*(ay-by));
}
int a,b,c;
signed main(){
	cin>>a>>b>>c;
	if(a+b+c>=22)puts("bust");
	else puts("win");
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int main(void){
    int w,h,n,x,y,a,max1=0,min2,max3=0,min4,ans;
    scanf("%d%d%d",&w,&h,&n);
    min2=w;
    min4=h;
    for(int i=0;i<n;i++){
        scanf("%d%d%d",&x,&y,&a);
        if(a==1&&max1<x)max1=x;
        else if(a==2&&min2>x)min2=x;
        else if(a==3&&max3<y)max3=y;
        else if(a==4&&min4>y)min4=y;
    }
    if(min2!=w&&min4!=h&&(max1>=min2||max3>=min4))ans=0;
    else ans=(min2-max1)*(min4-max3);
    printf("%d\n",ans);
}


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

int main() {
	int n, k;
	cin >> n >> k;
	long long a = n / k, b = (n + k / 2) / k;
	cout << (k % 2 ? a * a * a : a * a * a + b * b * b) << endl;
	return 0;
}

Measured memory_bytes: 256
#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
#define mp make_pair
signed main(){
    string s; cin>>s;
    if(s.size()==2&&s[0]==s[1])return puts("1 2")*0;
    if(s.size()==2)return puts("-1 -1")*0;
    int n=(int)s.size();
    for(int i=0; i<s.size();i++){
        if(i+1!=n){
            if(s[i]==s[i+1]){
                if(i==0){
                    cout<<1<<" "<<3<<endl;
                }else if(i+1==n-1){
                    cout<<i<<" "<<n<<endl;
                }else{
                    cout<<i+1<<" "<<i+3<<endl;
                }
                return 0;
            }
        }
    }
    for(int i=0; i<n;i++){
        if(i+2>n-1)continue;
        if(s[i]==s[i+2]){
            cout<<i+1<<" "<<i+3<<endl;
            return 0;
        }
    }
    puts("-1 -1");
    return 0;
}


Measured memory_bytes: 512
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 = sc.nextInt();
		if(n % 500 > a) {
			System.out.println("No");
		} else {
			System.out.println("Yes");
		}
	}
}


Measured memory_bytes: 21844