akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();
//1 (0, 0)
//11 (1, 2), (2, 1)
//121 (2, 4)(3, 3)(4, 2)
//1331 (3, 6)(4, 5)(5, 4)(6, 3)
//14641 (4, 8)(5, 7)(6, 6)(7, 5)(8, 4)
ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result
    x = x % p;  // Update x if it is more than or
                // equal to p
    while (y > 0)
    {

        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p-2, p);
}
ll nCr(ll n, ll m) {
    ll mod = 1e9+7;
    m = min(m, n-m);
    ll num = 1;
    ll dem = 1;
    if (m == 0) return 1;
    for(ll i = 1; i <= m; i++) {
        num *= (n+1-i);
        num %= mod;
        dem *= i;
        dem %= mod;
    }
    return (num * modInverse(dem, 1e9+7)) % mod;
}
int main() {
    ll x, y;
    cin >> x >> y;
    ll mod = 1e9+7;
    if ((x+y)%3 != 0) {
        cout << 0 << endl;
    } else {
        int row = (x+y) / 3;
        if (x >= row && x <= row*2) {
            // nCr(n, m) = nCr(row, x - row)
            cout << nCr(row, x-row) << endl;
        } else {
            cout << 0 << endl;
        }
    }
    return 0;
    // dp[x][y] = (dp[x-2][y-1] + dp[x-1][y-2]) % mod
    /*
    vector<vector<ll>> dp(3, vector<ll>(y_1, 0));
    dp[0][0] = 1;
    for(int i = 0; i <= x; i++) {
        for(int j = 0)
    }*/
    /*
    vector<vector<ll>> dp(x+1, vector<ll>(y+1, 0));
    dp[0][0] = 1;
    for(int i = 0; i <= x; i++) {
        for(int j = 0; j <= y; j++) {
            if ((i-2 >= 0) && (j-1 >= 0)) {
                dp[i][j] += dp[i-2][j-1];
                dp[i][j] %= mod;
            }
            if ((i-1 >= 0) && (j-2 >= 0)) {
                dp[i][j] += dp[i-1][j-2];
                dp[i][j] %= mod;
            }
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }*/
    // for(int i = 0; )

    return 0;
}




Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll h,w;
ll direction2[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
bool valid(int a,int b)
{
    if(a>=1&&a<=h&&b>=1&&b<=w)return true;
    else return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t,x=1000,y=0;
    cin>>t;
    for(int i=1;i<=10;i++){
        y=x*i;
        if(t<=y){
            cout<<y-t<<endl;return 0;
        }
    }
}


Measured memory_bytes: 3604
from collections import deque
 
 
def dfs(field):
    q = deque([(0, 0, 1)])
    visited = [[False] * w for _ in range(h)]
    while q:
        i, j, d = q.popleft()
        if i == h - 1 and j == w - 1:
            return d
        if visited[i][j]:
            continue
        visited[i][j] = True
        if i > 0 and field[i - 1][j] == '.':
            q.append((i - 1, j, d + 1))
        if i < h - 1 and field[i + 1][j] == '.':
            q.append((i + 1, j, d + 1))
        if j > 0 and field[i][j - 1] == '.':
            q.append((i, j - 1, d + 1))
        if j < w - 1 and field[i][j + 1] == '.':
            q.append((i, j + 1, d + 1))
    else:
        return -1
 
 
h, w = map(int, input().split())
field = [input() for _ in range(h)]
init_black = sum(r.count('#') for r in field)
res = dfs(field)
if res == -1:
    print(-1)
else:
    print(h * w - res - init_black)

Measured memory_bytes: 3444
#include<stdio.h>
#include<string.h>

int main(void){
    char w[100];
    scanf("%s",w);
    int n = strlen(w);
    int a[26];
    for(int i=0;i<26;i++){
        a[i]=0;
    }
    char b[26];
    char c;
    int i = 0;
    for(c='a';c<='z';c++){
        b[i]=c;
        i++;
    }
    
    for(int i=0;i<n;i++){
        for(int k=0;k<26;k++){
            if(w[i]==b[k]){
                a[k]++;
                break;
            }
        }
    }
    int d=0;
    for(int i=0;i<26;i++){
        if((a[i]%2)==1){
            d=1;
            break;
        }
    }
    if(d==0) printf("Yes\n");
    if(d==1) printf("No\n");
    return 0;
}


Measured memory_bytes: 128
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
#define Nmax 200010
#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,n)  FOR(i,0,n)
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
typedef long long ll;

int main() {
	int A,B,C,D;
	cin >> A >> B >> C >> D;
	int scheme[110]={};
	scheme[A]++;
	scheme[B+1]--;
	scheme[C]++;
	scheme[D+1]--;
	REP(i,101) scheme[i+1]+=scheme[i];
	int cnt=0;
	REP(i,101) if(scheme[i]==2) cnt++;
	if(cnt!=0) cnt-=1;  
    cout << cnt << endl;
	
	return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main{
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);

      int a=sc.nextInt();
      int b=sc.nextInt();
      int c=sc.nextInt();

      int ans;

      if(a-b>=c){
        ans = 0;
      }
      else{
        ans = c - a + b;
      }

      System.out.println(ans);

    }
}


Measured memory_bytes: 22356
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
const long double PI=(acos(-1));
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main(){
  int n,y;
  cin>>n>>y;
  
  rep(i,n+1){
    rep(j,n-i+1){
      int z=n-i-j;
      if(y==10000*i+5000*j+1000*z){
        cout<<i<<" "<<j<<" "<<z<<endl;
        return 0;
      }
    }
  }
  cout<<-1<<" "<<-1<<" "<<-1<<endl;
    
}

Measured memory_bytes: 256
s = gets.chomp
flag = true
ans = [-1,-1]
n = s.size
n.times do |i|
	ans = [i+1,i+2] if i < n - 1 && s[i] == s[i+1]
	ans = [i+1,i+3] if i < n - 2 && s[i] == s[i+2]
end

puts ans.join(" ")

Measured memory_bytes: 1916
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

int main(void) {
    int X, Y;
    cin >> X >> Y;

    bool ok = false;
    for (int i = 0; i <= X; i++) {
        int j = X - i;
        int foot = i * 2 + j * 4;
        if (foot == Y) {
            ok = true;
            break;
        }
    }

    if (ok) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

    return 0;
}


Measured memory_bytes: 3588
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy

T=int(input())
ABCD=[list(map(int,input().split())) for i in range(T)]
for A,B,C,D in ABCD:
    if B>D or A<B:
        print("No")
    elif C>=B:
        print("Yes")
    else:
        F=D-B
        _gcd=gcd(D,B)
        st=(A-B)%_gcd
        #if A==14 and B==10 and C==8 and D==12:
        #    print(B-C-1)
        if B-_gcd+A%_gcd>C:
            print("No")
        else:
            print("Yes")        


Measured memory_bytes: 64492
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int main() {
    double N,K;
    cin >> N >> K;
    double ans = 0;
    for(int i=1; i<N+1; i++){
        double count = 0;
        int j = i;
        while(j < K){
            j *= 2;
            count += 1.0;     
        }
        ans += 1.0/(N * pow(2.0,count));
    }
    printf("%.12f\n",ans);
}



Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <sstream>

using namespace std;

int gcd(int x, int y){
    return y ? gcd(y, x % y) : x;
}

int main(){
    int N;
    cin >> N;

    vector<int> A(N);

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

    int ans = A[0];
    
    for(int i = 0; i < N - 1; i++){
        ans = min(ans, gcd(A[i], A[i + 1]));
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 640
#include<iostream>
using namespace std;
#define rep(i, a, b) for(int i=(a); i<(b); i++)
int main(){
  int N,L,room = 0, sum = 0;
  cin >> N >> L;
  rep(i,0,N){
    int temp = L+i;
    if(abs(room) > abs(temp)||i==0){
      sum += room;
      room = temp;
    }else{
      sum += temp;
    }
  }

  cout<< sum <<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <array>
#include <limits>

using namespace std;

struct MatrixDims {
    int row;
    int col;
    MatrixDims() {}
    MatrixDims(int row, int col) : row(row), col(col) {}
};

array<MatrixDims, 100> matrix_dims;
array<array<int, 100>, 100> dp;

int get_min(int row, int col) {
    int min = numeric_limits<int>::max();
    for (int j = row; j < col; ++j) {
        int sum = dp[row][j] + dp[j+1][col] + matrix_dims[row].row * matrix_dims[j].col * matrix_dims[col].col;
        if (sum < min) min = sum;
    }
    return min;
}

int main()
{
    int n;
    cin >>  n;
    for (int i = 0; i < n; ++i) {
        int row, col;
        cin >> row >> col;
        matrix_dims[i] = MatrixDims(row, col);
    }
    dp.fill({});
    
    for (int endpt = 1; endpt < n; ++endpt) {
        for (int diag_i = 0; (endpt + diag_i) < n; ++diag_i) {
            dp[diag_i][diag_i + endpt] = get_min(diag_i, endpt + diag_i);
        }
    }
    cout << dp[0][n-1] << endl;
}


Measured memory_bytes: 3132
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		long K = sc.nextLong();
		if (K==1) {
			System.out.println(s.charAt(0));
			return;
		}
		for (int i=0;i<s.length()&&i<K;i++) {
			if (s.charAt(i)!='1') {
				System.out.println(s.charAt(i));
				return;
			}
		}
		System.out.println(1);
	}
}

Measured memory_bytes: 23764
import sys
N, A, B, C, D = map(int, input().split())
s = input()
if C < D:
    for x, y in zip(s[A:], s[A+1:D]):
        if x == '#' and y == '#':
            print('No')
            sys.exit()
    print('Yes')
else:
    for x, y in zip(s[A:], s[A+1:C]):
        if x == '#' and y == '#':
            print('No')
            sys.exit()
    for x, y, z in zip(s[B-2:], s[B-1:], s[B:D+1]):
        if x == '.' and y == '.' and z == '.':
            print('Yes')
            sys.exit()
    print('No')

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n;cin>>n;
    int arr[n];
    for (int i=0;i<n;i++)cin>>arr[i];
    sort(arr,arr+n);
    cout<<arr[n-1]-arr[0];
    return 0;
}


Measured memory_bytes: 256
oper = True
po = ""
result = int(input())

while True:
    if oper:
        po = input()
        if po == "=":
            break
        oper = False
    else:
        inp = int(input())
        if po == "+":
            result += inp
        elif po == "-":
            result -= inp
        elif po == "*":
            result *= inp
        elif po == "/":
            result //= inp
        oper = True
print(result)

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

#define ll long long

ll s, cache[2000], mod = 1e9 + 7;

ll bt(ll nw){
	if (nw > s){
		return 0;
	}else if (nw == s){
		return 1;
	}
	if (cache[nw] != -1) return cache[nw];
	ll ret = 0;
	for (ll a = s; a >= 3; a --){
		ret += bt(nw + a);
		ret %= mod;
	}
	return cache[nw] = ret;
}

int main(){
	cin >> s;
	memset(cache, -1, sizeof cache);
	cout << bt(0) << "\n";
}

Measured memory_bytes: 3616
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,x,y;
    while (cin>>a>>b>>c>>x>>y)
    {
        int n;
        if (((a+b)/2)>=c)
        {
            if (x>y)
            {
                if (a>c*2)
                {
                    n=c*y*2+(x-y)*c*2;
                }
                else
                {
                    n=y*2*c+(x-y)*a;
                }
                cout<<n<<endl;
            }
            else
            {
                if (b>c*2)
                {
                    n=c*x*2+(y-x)*c*2;
                }
                else
                {
                    n=x*2*c+(y-x)*b;
                }
                cout<<n<<endl;
            }
        }
        else
        {
            n=a*x+b*y;
            cout<<n<<endl;
        }

    }
    return 0;
}


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

int main(){
	int n,m,a=0,sum=0;
	char c1,c2;
	
	cin>>n>>m;
	
	cin>>c2;
	
	for(int i=0;i<n-1;i++){
		c1=c2;
		cin>>c2;
		if(c1==c2) sum++;
		else a++;
	}
	
	if(a>2*m) cout<<sum+2*m;
	else cout<<sum+a;
	
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <cmath>
#include <numeric>
#include <functional>
#include <stack>
#include <iomanip>
#include <map>
#include <cstdio>

int main() {
	std::map<std::string, int> m;

	std::string a, b;

	std::cin >> a >> b;

	int c, d;

	std::cin >> c >> d;

	m[a] = c;
	m[b] = d;

	std::string e;

	std::cin >> e;

	if (a == e) {
		m[a]--;
	}
	else {
		m[b]--;
	}

	std::cout << m[a] << " " << m[b] << std::endl;

	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<iomanip>


#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()


using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

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

map<ll, vector<ll>> memo;
vector<bool> ok;
v yak(ll N){
    v a;
    for(ll i = 2;i*i<=N;i++){
        if(N%i == 0 && i<N){
            a.PB(i);
            a.PB(N/i);
        }
    }
    a.PB(N);
    return a;
}

int main(){
    ll N;
    cin>>N;
    if(N == 2){
        cout<<1;
        return 0;
    }
    set<ll> s;
    v a = yak(N-1);
    for(ll now: a){
        s.insert(now);
    }
    a = yak(N);
    for(ll now: a){
        ll n=N;
        while(n%now ==0){
            n/=now;
        }
        if(n%now == 1)s.insert(now);
    }

    cout<<s.size();
    return 0;
}

Measured memory_bytes: 384
#include "bits/stdc++.h"
#include <math.h>
using namespace std;
void solve(){
    int n, h, w;
    cin >> n, cin >> h, cin >> w;
    cout << (n - h + 1) * (n - w + 1) << endl;
}
int main(){
    solve();
    return 0;
}

Measured memory_bytes: 256
# coding: utf-8
#69

A=[]
B=[]
c=0
n,m=map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]
for j in range(m):
    B.append(int(input()))

#print(A,B)

for k in range(n):
    for z in range(m):
        c += A[k][z]*B[z]
    print(c)
    c=0


Measured memory_bytes: 6024
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

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

  int ans = 0;

  for (int i = 1; i < N; i++) {
    string L, R;
    set<char> left;
    int tmp = 0;

    L = S.substr(0, i);
    R = S.substr(i, S.length()-i);

    for (char elem : L) {
      left.insert(elem);
    }
    sort(all(R));

    for (char elem : left) {
      if (binary_search(all(R), elem)) tmp++;
    }

    ans = max(ans, tmp);
  }

  cout << ans << endl;

  return 0;

}

Measured memory_bytes: 256
import math
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = 0
r = a[-1] + 1

while r - l > 1:
    mid = (r + l) // 2
    cnt = 0
    for i in a:
        cnt += (i - 1) // mid
    if cnt <= K:
        r = mid
    else:
        l = mid
print(r)

Measured memory_bytes: 30840
# coding: utf-8
# Your code here!

S = input()
sl = list(S)
ans = 0
for i in range(1 << len(S)): # 階乗(1,2,4,8...)
    string = str(0)
    tmp = 0
    for j in range(len(S)):
        if ((i >> j) & 1) == 1: # j,j+1桁目の間が+なら
            tmp+=int(string)
            string = "0"
        else:
            string += str(S[j])
    tmp=tmp+int(string)
    ans+=tmp
print(ans)
    

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

int main()
{
	int a, b;
	
	cin >> a >> b;

	if (a <= 8 && b <= 8)
	{ 
		cout << "Yay!" << endl;
	}
	else
	{
		cout << ":(" << endl;
	}

	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using namespace std;
int n,ans;
int t[20];

void solve(int x,int a,int b){
  if(x==n){
    ans=min(ans,max(a,b)-min(a,b));
  }else{
    solve(x+1,a+t[x],b);
    solve(x+1,a,b+t[x]);
  }
}

int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++)cin>>t[i];
    ans=(1e9);
    solve(0,0,0);
    cout<<ans<<endl;
  }
  return 0;
}

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

using edge = struct { int to; ll cost; };

vector<edge> g[100010];
ll depth[100010];

void dfs(int v, int p, ll d) {
    depth[v] = d;
    for (auto & e : g[v]) {
        if (e.to == p) continue;
        dfs(e.to, v, d + e.cost);
    }
}

int main() {
    int n;
    cin >> n;
    rep(i, n-1) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }

    int q, k;
    cin >> q >> k;
    k--;

    dfs(k, -1, 0);

    rep(i, q) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        cout << depth[x] + depth[y] << endl;
    }
}


Measured memory_bytes: 14336
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n, u, v;
vector<vector<int>> tr;

void f(vector<int>& d, int s)
{
	queue<pair<int,int>> q;
	q.push(pair<int,int>(s,0));
	d[s] = 0;
	while (!q.empty())
	{
		pair<int,int>& f = q.front();
		for (auto& i : tr[f.first]) {
			if (d[i] == -1) {
				q.push(pair<int,int>(i, f.second + 1));
				d[i] = f.second + 1;
			}
		}
		q.pop();
	}
}

int main()
{
	cin >> n >> u >> v;
	tr.resize(n + 1);

	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		tr[a].push_back(b);
		tr[b].push_back(a);
	}

	vector<int> td(n + 1, -1);
	f(td, u);
	vector<int> ad(n + 1, -1);
	f(ad, v);

	int ret = 0;
	for (int i = 1; i <= n; ++i) {
		if (td[i] < ad[i]) {
			if (ret < ad[i] - 1) {
				ret = ad[i] - 1;
			}
		}
	}

	cout << ret << endl;

	return 0;
}

Measured memory_bytes: 6784
a = input()
b = input()
c = input()
def turn(x ,a_hand ,b_hand ,c_hand):
    if not a_hand and x == "a":
        print("A")
        return
    if not b_hand and x =="b":
        print("B")
        return
    if not c_hand and x =="c":
        print("C")
        return
    if x == "a":
        x = a_hand[0]
        a_hand = a_hand[1:]
        turn(x , a_hand , b_hand , c_hand)
    elif x == "b":
        x = b_hand[0]
        b_hand = b_hand[1:]
        turn(x , a_hand , b_hand , c_hand)
    else:
        x = c_hand[0]
        c_hand = c_hand[1:]
        turn(x , a_hand , b_hand , c_hand)
turn("a",a,b,c)

Measured memory_bytes: 3188
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

if len(s) == 1:
    print(k//2)
    exit()

li = []
point = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        point += 1
        if i == len(s)-2:
            li.append(point)
    else:
        li.append(point)
        point = 1

if s[-1] != s[-2]:
    li.append(1)

if len(li) == 1:
    print(li[0]*k//2)
    exit()

if s[0] != s[-1]:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

else:
    co = 0
    for i in range(len(li)):
        co += li[i]//2
    co *= k
    co -= ((li[0]//2+li[-1]//2)-(li[0]+li[-1])//2)*(k-1)
    print(co)

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

using namespace std;
typedef long long ll;

int R, C, N;
vector <pair <int, int> > v;
vector <int> res;

int conv (int r, int c)
{
    if (r != 0 && r != R && c != 0 && c != C) return -1;
    if (c == 0) return r;
    if (r == R) return R + c;
    if (c == C) return R + C + (R - r);
    return R + C + R + (C - c);
}

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> R >> C >> N;
    for (int i = 0; i < N; i++)
    {
        int x, y, x2, y2;
        cin >> x >> y >> x2 >> y2;
        int lv = conv (x, y), rv = conv (x2, y2);
        if (lv == -1 || rv == -1) continue;
        v.push_back(make_pair (lv, i));
        v.push_back(make_pair (rv, i));
    }

    sort (v.begin(), v.end());
    for (auto p : v)
    {
        //cout << p.second << "\n";
        if (res.size() > 0 && res.back() == p.second)
            res.pop_back();
        else
            res.push_back(p.second);
    }

    if (res.size() > 0)
        cout << "NO\n";
    else
        cout << "YES\n";
}

Measured memory_bytes: 2672
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

int n,m;
ll b[100010];
ll lc=1;

ll gcd(ll a,ll b){
    if(a<b) swap(a,b);
    if(b==0) return a;
    return gcd(b,a%b);
}

void solve(){
    cin >> n >> m;
    bool zero=false;
    int k=0;
    rep(i,n){
        cin >> b[i];
        b[i]/=2;
        int s=b[i];
        if(i==0){
            while(s%2==0){
                s/=2;
                k+=1;
            }
        }
        else{
            int t=0;
            while(s%2==0){
                s/=2;
                t+=1;
            }
            if(k!=t) {
                zero=true; 
                break;
            }
        }
    }
    if(zero){
        cout << 0 << endl;
        return;
    }
    rep(i,n){
        lc=lc*b[i]/gcd(lc,b[i]);
        if(lc>m){
            cout << 0 << endl;
            return;
        }
    }
    ll ans=1+m/lc;
    cout << ans/2 << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}

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

const int dig[10]={6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

string maxstr(string s1, string s2){
  if(s1.size()<s2.size())return s2;
  else if(s1.size()==s2.size()){
    if(s1<s2)return s2;
    else return s1;
  }
  else return s1;
}

int main(){
  int n, m;
  cin >> n >> m;
  vector<int>a(m);
  for(int i=0; i<m; i++)cin >> a[i];
  sort(a.begin(), a.end());
  vector<vector<string>>dp(2*n+10,vector<string>(m, "0"));
  for(int i=0; i<m; i++)dp[0][i]="";
  for(int i=0; i<n; i++){
    for(int j=0; j<m; j++){
      if(dp[i][j]!="0"){
        for(int k=0; k<m; k++){
          string tmp=dp[i][j];
          tmp.push_back(a[k]+'0');
          if(maxstr(dp[i+dig[a[k]]][k], tmp)==tmp)dp[i+dig[a[k]]][k]=tmp;
        }
      }
    }
  }
  string ret=dp[n][0];
  for(int i=1; i<m; i++)ret=maxstr(ret, dp[n][i]);
  cout << ret;
  return 0;
}

Measured memory_bytes: 350336
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using pii = pair<int, int>;

//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;

int		main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	string a, b;
	cin >> a >> b;

	int n = stoi(a + b);
	int i = 1;
	while (i * i < n) {
		i++;
	}
	if (i * i == n) cout << "Yes" << endl;
	else cout << "No" << endl;
}


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

#define SIZE 1000

int stack[SIZE];
int pos = 0;

int empty()
{
	return pos == SIZE;
}

void push(int x)
{
	if (empty())
		printf("ERROR\n");
	else
		stack[pos++] = x;
}

int pop()
{
	int x;

	if (pos == 0)
	{
		printf("ERROR\n");
		exit(1);
	}
	else
		x = stack[--pos];

	return x;
}

void swap(int *a, int *b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

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

int binary_search(int A[], int N, int key)
{
	int left, right, middle;

	left = 0, right = N - 1;

	while (left <= right)
	{
		middle = (left + right) / 2;

		if (A[middle] == key)
			return 1;
		else if (A[middle] < key)
			left = middle + 1;
		else
			right = middle - 1;
	}

	return 0;
}

int dfs(int A[], int n, int k, int num, int sum)
{
	if (k == n)
	{
		if (num == sum)
			return 1;
		else 
			return 0;
	}
	else
	{
		if (dfs(A, n, k + 1, num + A[k], sum))
			return 1;
		if (dfs(A, n, k + 1, num, sum))
			return 1;
	}
	return 0;
}

int main()
{
	int n,q;
	int A[100];
	int i;
	int num;

	scanf("%d", &n);

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

	scanf("%d", &q);

	for (i = 0; i < q; i++)
	{
		scanf("%d", &num);
		if (dfs(A, n, 0, 0, num))
			printf("yes\n");
		else
			printf("no\n");
	}

	return 0;
}


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

int main() {
    long long X;  cin >> X;
    long long now = 100;
    long long ans = 0;
    while (true) {
        if (now >= X) {
            cout << ans << endl;
            return 0;
        }
        else {
            now += now / 100;
            ++ans;
        }
    }
}


Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
const int maxn=2e6+100;
struct tree
{
    ll x,y,u,v;
}a[maxn];
int main()
{
    ll n,u1=1e18,v1=1e18,u2=-1e18,v2=-1e18;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].x>>a[i].y;
        a[i].u=a[i].x+a[i].y;
        a[i].v=a[i].x-a[i].y;
        u1=min(u1,a[i].u);
        u2=max(u2,a[i].u);
        v1=min(v1,a[i].v);
        v2=max(v2,a[i].v);
    }
    cout<<max({u2-u1,v2-v1});
    return 0;
}


Measured memory_bytes: 9812
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
const int MAX_Q = 150, MAX_X = 1000;
int q, dp[MAX_X + 1][MAX_X + 1];
string x[MAX_X], y[MAX_X];

void solve() {
    for (int i = 0; i < q; i++) {
        for (int j = 0; j < x[i].length(); j++) {
            for (int k = 0; k < y[i].length(); k++) {
                if (x[i][j] == y[i][k]) {
                    dp[j + 1][k + 1] = dp[j][k] + 1;
                } else {
                    dp[j + 1][k + 1] = max(dp[j][k + 1], dp[j + 1][k]);
                }
            }
        }
        cout << dp[x[i].length()][y[i].length()] << endl;
    }
}

int main() {
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x[i] >> y[i];
    }
    solve();
    return 0;
}


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

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		var L = new List<long>();
		List<long> D = new List<long>();
		List<long> Q = new List<long>();
		for(long i=1;i * i <= N;i++){
			D.Add(i);
			Q.Add(N / i);
			if(N / i == N % i) L.Add(i);
		}
		for(int i=D.Count-1;i>=0;i--){
			if(D[i] == Q[i]) continue;
			D.Add(Q[i]);
			Q.Add(D[i]);
		}
		
		int M = D.Count;
		for(int i=M-1;i>0;i--){
			if(D[i] <= Q[i]) break;
			long dmax = D[i];
			long dmin = D[i-1] + 1;
			long q = Q[i];
			long modmin = N % dmax;
			long modmax = N % dmin;
			if( modmin <= q && q <= modmax){
				if(modmin != 0) continue;
				L.Add(dmax - 1);
			}
		}
		Console.WriteLine(L.Sum());
	}
	long N;
	public Sol(){
		N = rl();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


Measured memory_bytes: 50156
import sys

#A = input('Aを入力してください')
#B = input('Bを入力してください')
#K = input('Cを入力してください')
#print('A= ', A)
#print('B= ', B)
#print('K= ', K)

#A = 1
#B = 10
#K = 6

#S = input('ABKを入力してください')
S = input()
S = S.split(' ')

#for i in range(len(S)):
#    print(S[i])

#sys.exit()

A = int(S[0])
B = int(S[1])
K = int(S[2])

ANS = []
for i in range(K):
    if A + i <= B:
        ANS.append(A + i)


for j in range(K):
    if B - j not in ANS and B - j >= A:
        ANS.append(B - j)

ANS.sort()

#print(ANS)

for i in range(len(ANS)):
    print(ANS[i])


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

int main() {
  int N, a;
  string S, T;
  cin >> N;
  cin >> S;
  cin >> T;
  string out = "";
  for (int i = 0; i < N; i++) {
    out += S.at(i);
    out += T.at(i);
  }
  cout << out;
  return 0;
}

Measured memory_bytes: 256
res_list = []
n,k = map(int, input().split()) 
for i in range(k):
    d = int(input())
    a_list = list(map(int, input().split())) 
    for a in a_list:
        res_list.append(a)

res_list_set = list(set(res_list))
print( n - len(res_list_set))

Measured memory_bytes: 9280
#include <bits/stdc++.h>
using namespace std;
#define ll          long long
#define vl          vector<ll>
#define vi          vector<int>
#define pi          pair<int,int>
#define pl          pair<ll,ll>
#define all(a)      a.begin(),a.end()
#define mem(a,x)    memset(a,x,sizeof(a))
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define endl 		"\n"
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define fast_io     std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define trace(...) ZZ(#__VA_ARGS__, __VA_ARGS__);
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1) {std::cerr << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr(names + 1, ',');
	std::cerr.write(names, comma - names) << " : " << arg1;
	ZZ(comma, args...);
}
ll MOD = 2019;
void solve() {
	string s;
	cin >> s;
	vi ma(2020, 0);
	int  n = s.size();
	ll suf = 0, pot = 1, ans = 0;
	ma[0]++;
	for (int i = n - 1; i >= 0; i--) {
		int val = s[i] - '0';
		suf = (suf + (pot * val) % MOD) % MOD;
		pot = (pot * 10) % 2019;
		ans += (ma[suf]);
		ma[suf]++;
	}
	cout << ans << endl;
}
int main() {
	fast_io;
	int t = 1;
	// cin>>t;
	while (t--) {
		solve();
	}
	// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}

Measured memory_bytes: 3768
#include<bits/stdc++.h>
#define ll long long
#define ii pair<int,int>
#define x first
#define y second

using namespace std;

bool check(vector<int>&a,vector<int>&b){
    for(int i = 0;i <a.size();i++){
        if(a[i]!= b[i]){
            return false;
        }
    }
    return true;
}

int main(){
    int n;
    cin >> n;
    vector<int>p(n),q(n);
    for(int i= 0;i <n;i++){
        cin >> p[i];
    }
    for(int i = 0;i <n;i++){
        cin >> q[i];
    }
    vector<int>a(n);
    for(int i = 0;i<n;i++){
        a[i] = i+1;
    }
    int n1,n2;
    n1 = n2 = -1;
    int  c = 0;
    do{
        if(n1 == -1 && check(p,a)){
            n1 = c;
        }
        if( n2 == -1 && check(q,a)){
            n2 = c;
        }
        if(n1 == n2 && n1 != -1){
            break;
        }
        c++;
    }while(next_permutation(a.begin(),a.end()));
    cout << abs(n1-n2);
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main
{

	public static void main(String[] args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String str;
		long[] ans=new long[2];
		while((str=br.readLine())!=null)
		{
			String[] aa=str.split(" ");
			long[] a=new long[aa.length];
			for(int i=0;i<aa.length;i++)
			{
				a[i]=Long.parseLong(aa[i]);
			}
			ans[0]=gcd(a[0],a[1]);
			ans[1]=(a[0]/ans[0])*a[1];

			System.out.printf("%d %d\n",ans[0],ans[1]);


		}
	}

	static long gcd(long a,long b)
	{
		if(b>0)
		{
			return gcd(b,a%b);
		}
		else
		{
			return a;
		}
	}
}

Measured memory_bytes: 24248
#include <bits/stdc++.h>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define int long long
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 

vector<int> init(int size){
  vector<int> par(size,-1);
  return par;
}

int root(int x, vector<int>& par){
  if(par[x] < 0){
    return x;
  }else{
    return par[x] = root(par[x],par);
  }
}

bool same(int x, int y, vector<int>& par){
  return root(x, par) == root(y, par);
}

void unite(int x, int y, vector<int>& par){
  x = root(x, par), y = root(y, par);
  if(same(x,y, par))return;
  par[x] += par[y];
  par[y] = x;
}

int size(int x, vector<int>& par){
  return -par[root(x, par)];
}

int pow(int x, int n){
  if(n == 0)return 1;
  if(n % 2 == 0)return pow(x * x % MOD, n/2);
  else return x * pow(x, n-1) % MOD;
}

struct edge { int u, v, cost; };
edge es[110000];int v,e;
int kruskal() {
  sort(es, es+e, [](const edge& e1, const edge& e2){
    return e1.cost < e2.cost;
  });
  vector<int> tree = init(v);
  int res = 0;
  rep(i,e){
    edge e = es[i];
    if(!same(e.u, e.v, tree)){
      unite(e.u, e.v, tree);
      res += e.cost;
    }
  }
  return res;
}

signed main(){
  int n,k;string s;cin>>n>>k;cin>>s;
  vector<pair<int,bool>> v;int c = 1;
  rep(i,n){
    if(i == n-1){
      v.push_back(make_pair(c,s[i]-'0'));
      break;
    }
    if(s[i] == s[i+1]){
      c++;
    }else{
      v.push_back(make_pair(c,s[i]-'0'));
      c = 1;
    }
  }
  //rep(i,v.size()-1)v[i+1].first += v[i].first;
  //rep(i,v.size())cout << v[i] << " ";
  int ans = 0, cur = 0,zc = 0, last = 0;
  rep(i,v.size()){
    last = i;
    cur += v[i].first;
    if(!v[i].second){
      zc++;
    }else if(zc == k){
      break;
    }
  }
  if(last == v.size()-1){
    cout << cur << endl;
    return 0;
  }
  ans = cur;int head = 0;
  while(true){
    if(last == v.size()-1)break;
    if(last == v.size()-2){
      cur += v[last+1].first;
      last++;
    }else{
      cur += v[last+1].first + v[last+2].first;
      last += 2;
    }
    if(head == 0 && s[0] == '0'){
      cur -= v[head].first;
      head++;
    }else{
      cur -= v[head].first + v[head+1].first;
      head+=2;
    }
    ans = max(cur, ans);
  }
  cout << ans << endl;
  //cout << zc << " " << last << " " << cur << endl;
}


Measured memory_bytes: 2548
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	int i,j,n,m,s;
	while(cin>>n)
	{
		if(n==0)break;
		s=0;
		m=1000-n;
		s=s+(m/500);
		m=m%500;
		s=s+(m/100);
		m=m%100;
		s=s+(m/50);
		m=m%50;
		s=s+(m/10);
		m=m%10;
		s=s+(m/5);
		m=m%5;
		s=s+(m/1);
		cout<<s<<endl;
	}
	return 0;
}

Measured memory_bytes: 3052
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
#define dump(a) cout << (a) << endl;
typedef long long ll;

bool node[100010];
ll location[100010];
vector<pair<int, int>> graph[100005];

bool dfs( int x , int cost ) {
    if( node[x] ) return location[x] != cost;
    node[x] = true;
    location[x] = cost;
    for( int i = 0; i < (int)graph[x].size(); i++ ) {
        if ( dfs( graph[x][i].first, cost+graph[x][i].second ) ) {
            return true;
        }
    }
    return false;
}   

int main() {
    int n, m;
    cin >> n >> m;
    for( int i = 0; i < m; i++ ) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a].push_back(make_pair(b, c));
        graph[b].push_back(make_pair(a, -c));
    }
    for( int i = 1; i <= n; i++ ) node[i] = false;
    bool res = true;
    for( int i = 1; i <= n; i++ ) {
        if( node[i] ) continue;
        if( dfs(i, 0) ) res = false;
    }
    dump( res ? "Yes" : "No" );
    return 0;  
}

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

int main() {
  int N ,Big=0,big=0,alice=0,bob=0,count=0;
  cin >> N;
  
  vector<int> vec1 ( N) ;
  for (int i = 0; i < N; i++) {
    cin >> vec1.at(i);
  } 
  for (int j = 0; j < N; j++) {
     for (int k = (j+1); k < N; k++) {
          if (vec1.at(j)<vec1.at(k)){
            Big=k;
            big=vec1.at(j);
            vec1.at(j)=vec1.at(k);
            vec1.at(k)=big;
          }
     }
       count ++;
       if (count%2 !=0){
       alice = alice + vec1.at(j);
       }else{
        bob = bob + vec1.at(j);
          }
         vec1.at(j) = 0;
     
  }
  cout << alice - bob << endl;
  
}


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

#define OnlineJudge1

#define loop(x,a,n) for(int x = a;x < n;x++)
#define NumofDigits(n) ((int)log10(n)+1)
#define CountofNumber(array,n,x)  (upper_bound(array, array+n, x)-lower_bound(array, array+n, x))
#define readline(s) getline(cin,s)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define EPS 1e-7
#define modulo ll (1e9 + 7)
const long double PI = 3.141592653589793238L;


typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,ll> pill;
typedef pair<ll,ll> pll;

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

#define time__(d) \
    for ( \
        auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
        blockTime.second; \
        debug("%s: %lld ms\n", d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false \
    )

void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
#ifdef OnlineJudge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
}


int main(){
    init();

    int n, m;   cin >> n >> m;

    ll arr[n];
    loop(i,0,n) cin >> arr[i];

    partial_sum(arr, arr + n, arr);

    map<int,int> mp;
    loop(i,0,n)
        mp[arr[i] % m]++;

    ll ans = 0;
    for(auto p : mp){
        ans += (ll)p.S * (p.S - 1) / 2;
    }

    ans += mp[0];

    cout << ans;
}

Measured memory_bytes: 3968
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];
		int[] b = new int[n-1];
		for(int i =0;i < n-1;i++) {
			b[i] = sc.nextInt();
		}
		a[0] = b[0];
		long answer = 0;
		for(int i =1; i < n;i++) {
			a[i] = b[i-1];
		}
		for(int i=1;i < n;i++) {
			if(a[i]< a[i-1]) {
				a[i-1] = a[i];
			}
		}

		for(int i =0;i < n;i++) {
			answer += a[i];
		}

		System.out.println(answer);
	}
}

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

using namespace std;

int const N = 4e6 + 5;

int n;

bitset < N > ans, cur;

int main()
{
   // freopen("inp.txt", "r", stdin);
    cin >> n;
    int x;
    ans[0] = 1;
    int sum = 0;
    for(int i=1; i<=n; i++){
        cin >> x;
        //x = 2000;
        cur = (ans << x);
        ans |= cur;
        sum += x;
    }


    for(int i=(sum + 1) / 2; i<N; i++){
        if(ans[i] == 1){
            cout << i;
            break;
        }
    }


}


Measured memory_bytes: 2176
n,k = map(int,input().split())
A = list(map(int,input().split()))

d = [-1]*n
city = []
d[0] = 0
now = 0
start = 0
for i in range(n):
    city.append(now+1)
    if d[A[now]-1] != -1:
        loop = d[now] + 1 - d[A[now]-1]
        start = A[now]-1
        break
    d[A[now]-1] = d[now] + 1
    now = A[now]-1

if d[start] >= k:
    print(city[k])
else:
    print(city[d[start] + (k-d[start])%loop])


Measured memory_bytes: 102904
import bisect

def ctoi(char):
  return ord(char) - 97


#素材
ingredients = input()
ingredients_len = len(ingredients)
#つくりたいもの
recipe = input()
recipe_len = len(recipe)

alphabets = [[] for _ in range(26)]

# 素材のアルファベット分布リストをつくる
for i in range(ingredients_len):
  alpha_idx = ctoi(ingredients[i])
  alphabets[alpha_idx].append(i)

ingredients_counter = [0] * 26
for i in range(26):
  ingredients_counter[i] = len(alphabets[i])
  
ingredients_idx_list = []
flag = True
ingredients_pointer = -1

for i in range(recipe_len):
  alpha_idx = ctoi(recipe[i])
  if ingredients_counter[alpha_idx] == 0:
    flag = False
    break
  #今のingredients_pointerより右側に使える素材があるか探す
  pointer_candidate_idx = bisect.bisect_right(alphabets[alpha_idx], ingredients_pointer)
  #右側に無かった。ループして先頭のを使ってどうぞ
  if pointer_candidate_idx == ingredients_counter[alpha_idx]:
    ingredients_pointer = alphabets[alpha_idx][0]
  else:
    ingredients_pointer = alphabets[alpha_idx][pointer_candidate_idx]
  ingredients_idx_list.append(ingredients_pointer)

if flag == False:
  print(-1)
else: 
  loop = 0
  idx = ingredients_idx_list[0]
  #idxのリセット分ループを数える。左より右が小さかったらループ
  for i in range(1, recipe_len):
    if ingredients_idx_list[i] <= idx:
      loop += 1
    idx = ingredients_idx_list[i]
  # ループx素材の長さ + つくりたいものの最後の文字のインデックス(に、+１)
  print(loop * len(ingredients) + ingredients_idx_list[-1] + 1)

Measured memory_bytes: 8572
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (1e+18) + 7;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-6;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

ll mod_pow(ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%mod;
		a = a * a%mod; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 500;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

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

void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	rep(i, n - 1)if (a[i] == a[i + 1]) {
		cout << "NO" << endl; return;
	}
	cout << "YES" << endl;
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f(); init();
	//int t; cin >> t; rep(i, t)solve();
	//while(cin>>n,n)solve();
	solve();
	stop
		return 0;
}

Measured memory_bytes: 1024
#import pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

s=input().rstrip()
n=int(input().rstrip())
for _ in range(n):
    c,*x=input().split()
    if c=="print":
        print(s[int(x[0]):int(x[1])+1])
    elif c=="reverse":
        s=s[:int(x[0])]+s[int(x[0]):int(x[1])+1][::-1]+s[int(x[1])+1:]
    else:
        s=list(s)
        t=x[2]
        l=0
        for i in range(int(x[0]),int(x[1])+1):
            s[i]=t[l]
            l+=1
        s="".join(s)




Measured memory_bytes: 5996
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()


def main():
    n = II()
    t = [LI() for _ in range(n)]
    r = 0

    t.reverse()
    c = 0
    for a,b in t:
        ac = a+c
        if ac % b == 0:
            continue
        c += b - ac%b
    return c


print(main())


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


int main(){
    ll n,c=0,b=0; cin >> n;
    string s;
    vector<string> a(n);
    for(ll i=0;i<n;i++){
        cin >>s;
        sort(s.begin(),s.end());
        a.at(i)=s;
    }
    
    sort(a.begin(),a.end());
   
    for(int i=0;i<n-1;i++){
        
    if(a.at(i)==a.at(i+1)){
        c++;
        if(i==n-2)
        b+=(c*(c+1))/2;
    }
    else{
        b+=(c*(c+1))/2;
        c=0;
    }
    }
    cout << b << endl;
    
  }


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

int main() {
	long long a,b;
	string s;
	
	while(cin>>s){
		cin>>a>>b;
		cout<<s<<" "<<a+b<<" "<<a*200+b*300<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 2960