akhauriyash/Code-Regression huggingface dataset:
def f():
    def getMin(A):
        minA = A[0]
        for a in A:
            minA = min(minA,a)
        return minA
    la, lb, lc = [int(s) for s in input().split()]
    A = [int(s) for s in input().split()]
    B = [int(s) for s in input().split()]
    mina = getMin(A)
    minb = getMin(B)
    ans = mina+minb
    for i in range(lc):
        x, y, c = [int(s) for s in input().split()]
        p = A[x-1]+B[y-1]-c
        ans = min(ans,p)
    print(ans)


f()

Measured memory_bytes: 67668
# -*- coding: utf-8 -*-

n = int(input())
v = list(map(int, input().split()))
c = list(map(int, input().split()))

g = 0
for i in range(n):
    if v[i]-c[i] >= 0:
        g += v[i]-c[i]
    else:
        continue

print(g)


Measured memory_bytes: 3060
#include<iostream>
using namespace std;
int n,a,cnt,ans=0x7fffffff;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cnt=0;cin>>a;
		while(a%2==0)a/=2,++cnt;
		ans=min(ans,cnt);
	}
	cout<<ans<<endl;
	return 0;
}

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

using namespace std;
const int maxn = 3010;
const long long mod = 1e9+7;
const int maxx = 2e5+10;

int x, y;
int n;
int a[maxn], b[maxn];
long long dp[maxn];
long long fac[maxx];

long long pot(long long a, long long b) {
    if (b == 0) return 1;
    else if (b & 1) return (a * pot(a, b - 1)) % mod;
    else {
        long long out = pot(a, b / 2);
        return (out * out) % mod;
    }
}

long long inv(long long x) {
    return pot(x, mod - 2);
}

long long divi(long long x, long long y) {
    return (x * inv(y)) % mod;
}

long long choose(int n, int k) {
    assert(k <= n);
    return divi(fac[n], (fac[n - k] * fac[k]) % mod);
}

long long rek(int i) {
    //printf("rek: %d\n", i);
    long long &ret = dp[i];
    if (ret != -1) return ret;

    ret = choose(a[i] + b[i] - 2, a[i] - 1);
    for (int j = 0; j < n; j++) {
        if (i == j) continue;
        if (a[i] >= a[j] && b[i] >= b[j])
            ret -= (rek(j) * choose(a[i] - a[j] + b[i] - b[j], a[i] - a[j])) % mod;
        if (ret < 0) ret += mod;
    }
    return ret;
}

int main() {
    memset(dp, -1, sizeof dp);
    scanf("%d%d%d", &x, &y, &n);
    for (int i = 0; i < n; i++)
        scanf("%d%d", a+i, b+i);
    a[n] = x, b[n] = y;

    fac[0] = 1;
    for (int i = 1; i <= x + y; i++)
        fac[i] = fac[i - 1] * i, fac[i] %= mod;
    printf("%lld\n", rek(n));
    return 0;
}


Measured memory_bytes: 1792
H,W,*L = open(0).read().split()
H,W = map(int, (H,W))
dp = [[0]*W for i in range(H)]
for i in range(1,H):
  if L[i][0]!=L[i-1][0]:
    dp[i][0] = dp[i-1][0]+1
  else:
    dp[i][0] = dp[i-1][0]
for j in range(1,W):
  if L[0][j]!=L[0][j-1]:
    dp[0][j] = dp[0][j-1]+1
  else:
    dp[0][j] = dp[0][j-1]
for i in range(1,H):
  for j in range(1,W):
    if L[i][j]!=L[i-1][j] and L[i][j]!=L[i][j-1]:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1])+1
    elif L[i][j]!=L[i-1][j]:
      dp[i][j] = min(dp[i-1][j]+1,dp[i][j-1])
    elif L[i][j]!=L[i][j-1]:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1]+1)
    else:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1])
if L[0][0]=='.':
  ans = (dp[H-1][W-1]+1)//2
else:
  ans = (dp[H-1][W-1]+2)//2
print(ans)

Measured memory_bytes: 3188
#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++)

using namespace std;

int main() {
   char c;
    cin >> c;
    if(c == 'A')    cout << 'T' << endl;
    if(c == 'T')    cout << 'A' << endl;
    if(c == 'C')    cout << 'G' << endl;
    if(c == 'G')    cout << 'C' << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
    int a,b; /* 数の箱*/
    char c, p; /* 文字の箱*/
    do{
        scanf("%d ",&a);
        scanf("%c",&c);/*文字は %c */
        scanf(" %d",&b);
        switch(c){ /* switchの値で判断して実行 */
        case '+':
            printf("%d\n",a+b);
            break;
        case '-':
            printf("%d\n",a-b);
            break;
        case '*':
            printf("%d\n",a*b);
            break;
        case '/':
            printf("%d\n",a/b);
            break;
        case '%':
            printf("%d\n",a%b);
            break;
        default: /*どれにも当てはまらない場合*/
            break;
        }
    }while(c!='?'); /*doからwhileまででcが'？'になるまで繰り返す*/
    return 0;
}


Measured memory_bytes: 2688
#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <unordered_map>

const int MAX_N = 310;
const int MAX_L = MAX_N * 300;
const int MOD = 998244353;
int a[MAX_N];
int N;

long long dp[MAX_N][MAX_L];
long long dp0[MAX_N][MAX_L];

long long pow_mod(long long x, long long n, long long mod_num) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) { res = (res * x) % mod_num; }
        x = (x * x) % mod_num;
        n = (n >> 1);
    }
    return res;
}

int main(int argc, char **argv) {
    std::cin >> N;
    for (int i = 1; i <= N; i++) {
        std::cin >> a[i];
    }

    int total_length = 0;
    for (int i = 1; i <= N; i++) {
        total_length += a[i];
    }

    for (int i = 0; i < MAX_N; i++) {
        for (int j = 0; j < MAX_L; j++) {
            dp[i][j] = 0;
        }
    }

    dp[0][0] = 1;
    dp0[0][0] = 1;

    for (int i = 1; i <= N; i++) {
        for (int L = 0; L < MAX_L; L++) {
            dp[i][L] += dp[i-1][L];
            dp0[i][L] += dp0[i-1][L];
        }
        for (int L = 0; L < MAX_L; L++) {
            if (a[i] <= L) {
                dp[i][L] = dp[i][L] + (dp[i-1][L - a[i]] * 2) % MOD;
                dp[i][L] = dp[i][L] % MOD;

                dp0[i][L] = dp0[i][L] + dp0[i-1][L - a[i]] % MOD;
                dp0[i][L] = dp0[i][L] % MOD;
            }
        }
    }

    long long tmp = 0;
    for (int L = 0; L < MAX_L; L++) {
        if (L <= total_length - L) {
            tmp = (tmp + (dp[N][L] * 3)) % MOD;
        }
        if (L == total_length - L) {
            tmp = (tmp - (dp0[N][L] * 3) + MOD) % MOD;
        }
    }

    //std::cout << tmp << std::endl;


    long long ret = pow_mod(3, N, MOD);
    ret = (ret + MOD - tmp) % MOD;
    //ret = (ret + MOD - (tmp * 3) % MOD) % MOD;

    std::cout << ret << std::endl;

    return 0;
}

Measured memory_bytes: 445952
#include<stdio.h>
int main()
{
    int n,i;
    for(n=1;n<=9;n=n+1){
            for(i=1;i<=9;i=i+1){
                 printf("%dx%d=%d\n",n,i,n*i);
    }
    }
    return 0;
    }

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

using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string s, t;
  cin >> s;
  cin >> t;
  int nums = s.length();
  int numt = s.length();

  int count = 0;
  if (nums<=numt){
    for(int i=0; i<nums; ++i){
      count += s[i]!=t[i] ? 1 : 0;
    }
    cout << count + (numt - nums) << endl;
    return 0;
  }

   return 0;
}


Measured memory_bytes: 3732
N = int(input())
digit_sums = []
for i in range(1, N // 2 + 1):
    A_sum = sum([int(c) for c in str(i)])
    B_sum = sum([int(c) for c in str(N - i)])
    digit_sums.append(A_sum + B_sum)
print(min(digit_sums))

Measured memory_bytes: 3480
import sys

n = int(input())
a = list(map(int, input().split()))
b= sorted(a)

ans = 0
for i in range(n):
    w = 0
    cnt = 0
    x = a.index(b[i]) # b[i]: sort未済の中で最小値
    while x != i:
        y = a.index(b[x]) #位置xに来るべき数の位置
        a[x], a[y] = b[x], b[i] #b[i] -> 位置yに移動
        cnt += 1
        w += b[x] + b[i]
        x = y
    if cnt > 0: 
        ans += min(w, (b[i] + b[0]) * 2 + w - (b[i] - b[0]) * cnt) #b[0]が全体の最小値

print(ans)


Measured memory_bytes: 5628
import java.util.Scanner;

public class Main {

	public static void main(String[] args){
		
		//テキスト入力クラス
		Scanner sc = new Scanner(System.in);
		
		//入力された横と縦の値
		int width = sc.nextInt();
		int height = sc.nextInt();
		
		//面積を調べる
		int area_size = (width*height);
		
		//周の長さを調べる
		int circumference = (width*2+height*2);
		
		//出力
		System.out.println(area_size+" "+circumference);
	}
}

Measured memory_bytes: 17160
#include <iostream>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
    ll n;
    cin>>n;
    ll v[n];
    for(int i=0;i<n;++i){
        cin>>v[i];
    }
    ll c[n];
    for(int i=0;i<n;++i){
        cin>>c[i];
    }
    ll ans=0;

    for(int i=0;i<n;++i){
        if(v[i]>c[i]){
            ans+=v[i]-c[i];
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func stringLineScan() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, 1000000009)
	sc.Split(bufio.ScanWords)
	n, _ := strconv.Atoi(stringLineScan())
	k, _ := strconv.Atoi(stringLineScan())
	s := stringLineScan()
	c := make([]int, 1)
	c[0] = 1
	t := 0
	f := 0
	if string(s[0]) == "1" {
		f = 1
	}
	for i := 1; i < n; i++ {
		if s[i] == s[i-1] {
			c[t]++
		} else {
			c = append(c, 1)
			t++
		}
	}
	d := make([]int, len(c)+1)
	d[0] = 0
	ld := len(d)
	for i := 1; i < len(c)+1; i++ {
		d[i] = d[i-1] + c[i-1]
	}
	ans := 0
	for i := 1; i < ld; i++ {
		a := 0
		if i%2 == f {
			a = d[smaller(ld-1, i+2*k)] - d[i-1]
		} else {
			a = d[smaller(ld-1, i+2*k-1)] - d[i-1]
		}
		if ans < a {
			ans = a
		}
	}
	fmt.Println(ans)
}
func smaller(x, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}


Measured memory_bytes: 5632
#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++)

using ll=long long;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int x = 0, y = 0, z = 0;
  rep(i,n){
    int a = 0;
    cin >> a;
    if(a%4 == 0) z++;
    else if(a%2 == 0) y++;
    else x++;
  }
  string ans = "No";
  if(y == 0)if(x <= z+1) ans = "Yes";
  if(y > 0)if(x <= z) ans = "Yes";
  cout << ans << endl;
}


Measured memory_bytes: 256
hoge=input()
if(hoge=="SUN"):print(7)
if(hoge=="MON"):print(6)
if(hoge=="TUE"):print(5)
if(hoge=="WED"):print(4)
if(hoge=="THU"):print(3)
if(hoge=="FRI"):print(2)
if(hoge=="SAT"):print(1)


Measured memory_bytes: 3060
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)
int n,m,q;
vector<array<int,4>> lst;
vector<int> A;
int ans=0;
void gen(int ind, int nxt) {
	if(nxt>m) return ;
	if(ind==n) {
		int cans=0;
		for(auto j:lst) {
			if(A[j[1]-1]-A[j[0]-1]==j[2]) cans+=j[3];
		}
		ans=max(cans, ans);
		return ;
	}
	
	gen(ind,nxt+1);
	
	A.push_back(nxt);
	gen(ind+1,nxt+1);
	gen(ind+1,nxt);
	A.pop_back();
}

int main() {
	IO;
	
	cin>>n>>m>>q;
	lst.resize(q);
	for(int i=0;i<q;++i) {
		for(int j=0;j<4;++j) {
			cin>>lst[i][j];
		}
	}
	
	gen(0,1);
	cout<<ans<<"\n";

	
	return 0;
}


Measured memory_bytes: 3668
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>

typedef  long long ll;
#define mop 1000000007
using namespace std;

int main() {
    

    ll n;
    cin >> n;
    vector <ll> v;
    for (ll i=0;i<n;i++){
        ll tmp;
        cin >> tmp;
        v.push_back(tmp);
    }
    sort(v.begin(), v.end());
    ll ans = 0;
    for (ll i=0;i<n;i++){
        for(ll j=i+1;j<n;j++){
            auto itr = lower_bound(v.begin(), v.end(), v[i]+v[j]);
//            cout << i << j << itr - v.begin() << endl;
            ans += itr - v.begin() - j - 1;
        }
    }
    cout << ans << endl;


}

Measured memory_bytes: 256
#include <cstdio>
#include <string>
using namespace std;

int main(){
	char tmp[11];
	scanf("%s", tmp);
	string str = tmp;
	int a[10];
	for(int i=0; i<str.size(); i++){
		a[i] = str[i] - '0';
	}
	long long int ans = 0;
	for(int i = 0; i < 1 << (str.size() - 1); i++){
//		printf("%d\n", i);
		long long int t = 0;
		t += a[0];
		for(int j=1; j<str.size(); j++){
//			printf("j=%d\n", j);
//			printf("fugo = %d\n", i >> (j - 1) & 1);
			if((i >> (j - 1) & 1) == 1){
				ans += t;
//				printf("%lld\n", ans);
				t = a[j];
			}else{
				t = t * 10 + a[j];
			}
		}
		ans += t;
//		printf("%lld\n", ans);
	}
	printf("%lld\n", ans);
	return 0;
}

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

int main() {
	while(1) {
		string s;
		getline(cin, s);
		stringstream sstr(s);

		int n;
		int one_count = 0, pts = 0;
		sstr >> n;
		if(n == 0)
			break;
		do {
			if(n == 1)
				one_count++;
			else if(n < 10)
				pts += n;
			else
				pts += 10;
		} while(sstr >> n);

		int ret = pts + one_count;
		ret = ret <= 21 ? ret : 0;
		for(int i = 0; i <= one_count; i++) {
			int tmp;
			tmp = pts + i + 11 * (one_count - i);
			tmp = tmp <= 21 ? tmp : 0;

			ret = ret > tmp ? ret : tmp;
		}

		cout << ret << endl;
	}
}

Measured memory_bytes: 1224
#include<iostream>

using namespace std;

int main(){
    int D, G, p[11], c[11], ans = 1<<20;
    cin >> D >> G;
    for(int i = 0; i < D; i++) cin >> p[i] >> c[i];

    for(int i = 0; i < (1 << D); i++){
        int t = 0, n = 0, rest_max = 0;
        for(int j = 0; j < D; j++){
            if( (i>>j) & 1){
                t +=  100 * (j+1) * p[j] + c[j];
                n += p[j];
            }
            else{
                rest_max = j;
            }
        }
        if(t < G){
            int q = 100 * (rest_max + 1);
            int need = (G - t + q - 1) / q;
            if(need >= p[rest_max]){
                continue;
            }
            n += need;
        }
        ans = min(ans, n);
    }
    cout<< ans <<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define double long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define ms(v,x) memset(v,x,sizeof(v))
#define pii pair<int,int>
#define ff first
#define ss second
#define frr(i,n) for(int i=0;i<n;i++)
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define td(v) v.begin(),v.end()
#define inf 1000000000 // 1e9
#define M   1000000007 // 1e9 + 7
using namespace std;
inline int mod(int n, int m){ int ret = n%m; if(ret < 0) ret += m; return ret; }
int gcd(int a, int b){return (b == 0 ? a : gcd(b, a%b));}
int lcm(int a,int b){return (a*b)/gcd(a,b);}
int exp(int a,int b,int m){
    if(b==0) return 1LL;
    if(b==1) return mod(a,m);
    int k = mod(exp(a,b/2,m),m);
    if(b&1){
        return mod(a*mod(k*k,m),m);
    }
    else return mod(k*k,m);
}
void _div(int &a,int b){
	a = (a * exp(b,M-2,M)) % M;
}
void _mul(int &a,int b){
	a =  a * b % M;
}
void _sum(int &a,int b){
	a += b;
	if(a >= M) a-=M;
	if(a < 0) a += M;
}
int n;
vector<int> adj[100100];
pii dfs(int u,int p){
    int a = 1, b = 1;
    for(int v : adj[u]){
        if(v==p) continue;
        pii x = dfs(v,u);
        _mul(a,(x.ff + x.ss) % M);
        _mul(b,x.ff);
    }
    return {a,b};
}
main(){
    fastio;
    cin >> n;
    for(int i=0;i<n-1;i++){
        int a,b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    pii ans = dfs(1,0);
    cout << mod(ans.ff + ans.ss,M) << endl;
}

Measured memory_bytes: 12672
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int[] v = new int[649900];
		for(int i = 0;i < 649900;i++){
			v[i] = 1;
		}
		int p = 3;
		while(true){
			if(p*p > 1299800){
				break;
			}else{
				if(v[(p-1)/2] == 0){
					p += 2;
				}else{
					int t = p*p;
					while(t < 1299800){
						v[(t-1)/2] = 0;
						t += 2*p;
					}
					p += 2;
				}
			}
		}
		while(scan.hasNext()){
			int n = scan.nextInt();
			if(n == 0){
				break;
			}
			long sum = 2;
			for(int i = 1;i < 649900;i++){
				if(v[i] == 0){
					continue;
				}
				if(n < 2){
					break;
				}
				sum += (2*i+1);
				n--;
			}
			System.out.println(sum);
		}
	}
}

Measured memory_bytes: 23040
#include<bits/stdc++.h>
#include<unistd.h>
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define All(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;

int main(){
    int N,M;cin>>N>>M;
    vector<int> a(M),b(M);
    vector<int> cnt(N);
    REP(i,M){
        cin>>a[i]>>b[i];
        a[i]--;b[i]--;
        cnt[a[i]]++;
        cnt[b[i]]++;
    }
    bool ok=true;
    REP(i,N) if(cnt[i]&1) ok=false;
    if(ok) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}


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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define Eunique(v) v.erase(unique(all(v)),v.end())
#define Eback(s) s.erase(s.end()-1,s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v),0LL)
#define mapmin(v) v.rbegin()->first
#define mapmax(v) v.begin()->first
#define pb push_back
#define pf push_front
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK cerr<<"OK\n"
#define OK1 cerr<<"OK1\n"
#define OK2 cerr<<"OK2\n"
#define SIZE(s) (int)s.size()
#define INF ((1LL<<62)-(1LL<<31))
#define zero(x,n) setw(x) << setfill('0') << n
#define endl '\n'

typedef long long ll;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<pair<ll,ll>> vpll;

const double pi = acos(-1.0);
const ll mod=1000000007;
const ll mod2=998244353;

template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>&p) {
	ost << "{" << p.first << ", " << p.second << "} ";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost, const vector<T>&v) {
	ost << "{";
	for (int i = 0; i<(int)v.size(); i++) {
		if (i)ost << " ";
		ost << v[i];
	}
	ost << "} \n";
	return ost;
}

template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>&v) {
	ost << "{";
	for (auto p:v) {
		ost << "{" << p.first << ", " << p.second << "} ";
	}
	ost << "} ";
	return ost;
}

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

void YES(bool b) {cout << ((b)?"YES\n":"NO\n");}
void Yes(bool b) {cout << ((b)?"Yes\n":"No\n");}
void yes(bool b) {cout << ((b)?"yes\n":"no\n");}
void Yay(bool b) {cout << ((b)?"Yay!\n":":(\n");}

template<typename T>
void VIN(vector<T> &v,int n){for(int i=0;i<n;i++) cin >> v[i];}

template<typename T>
void VOUT(vector<T> &v){
    ll n=(int)v.size();
    for(int i=0;i<n;i++) cout << v[i] << (i==n-1?"\n":" ");
}

template<typename T>
void VOUTL(vector<T> &v){
    ll n=(int)v.size();
    for(int i=0;i<n;i++) cout << v[i] << endl;
}

ll powmod(ll a,ll b){ll c=1; while(b>0){ if(b&1){ c=a*c%mod; } a=a*a%mod; b>>=1; } return c;}
ll gcd(ll x, ll y) {return __gcd(x,y);}
ll lcm(ll x, ll y) {return x/__gcd(x,y)*y;}

int main()
{
	ll h,w;
	while(1){
		cin >> h >> w;
		if(h==0 && w==0) break;
		rep(i,h){
			rep(j,w){
				if(j%2==0) cout << (i%2?'.':'#');
				else cout << (i%2?'#':'.');
			}
			cout << "\n";
		}
		cout << "\n";
	}
	return 0;
}


Measured memory_bytes: 3060
#include<iostream>

using namespace std;

int main(void){

  int A, B, C;
  
  cin >> A >> B >> C;

  int cand = C - A;

  if(C < A){
    cout << "NO" << endl;
    return 0;
  }

  if(cand <= B){
    cout << "YES" << endl;
  }else{
    cout << "NO" << endl;
  }
  
  return 0;
}


Measured memory_bytes: 256
import sys

input = lambda: sys.stdin.readline().rstrip()


class SegTree:

    def __init__(self, init_val, segfunc, ide_ele):

        n = len(init_val)
        self.ide_ele = ide_ele
        self.segfunc = segfunc
        self.num = 2**(n - 1).bit_length()
        self.seg = [self.ide_ele] * 2 * self.num

        for i in range(n):
            self.seg[i + self.num - 1] = init_val[i]

        for i in range(self.num - 2, -1, -1):
            self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])

    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k:
            k = (k - 1) // 2
            self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])

    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num - 1
        q += self.num - 2
        res = self.ide_ele
        while q - p > 1:
            if p & 1 == 0:
                res = self.segfunc(res, self.seg[p])
            if q & 1 == 1:
                res = self.segfunc(res, self.seg[q])
                q -= 1
            p = p // 2
            q = (q - 1) // 2
        if p == q:
            res = self.segfunc(res, self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
        return res


def solve():

    N = int(input())
    S = list(input())
    Q = int(input())
    seg = [[0 for _ in range(N)] for _ in range(26)]

    for i in range(N):
        al = ord(S[i]) - ord('a')
        seg[al][i] = 1

    segtree = []
    segfunc = lambda a, b: a | b

    for i in range(26):
        segtree.append(SegTree(seg[i], segfunc, 0))

    ans = []
    for i in range(Q):
        a, b, c = input().split()
        a = int(a)

        if a == 1:
            b = int(b) - 1
            ps = S[b]
            S[b] = c
            segtree[ord(ps) - ord('a')].update(b, 0)
            segtree[ord(c) - ord('a')].update(b, 1)
        elif a == 2:
            b, c = int(b) - 1, int(c)
            count = 0
            for i in range(26):
                count += segtree[i].query(b, c)
            ans.append(count)

    print('\n'.join(map(str, ans)))


if __name__ == '__main__':
    solve()


Measured memory_bytes: 441976
#include <cstdio>
#include <algorithm>

int n,q;
int d[500050];
int s[500050];
int r[500050];

int main(void)
{
    int i,k;

    //freopen("x.in","r",stdin);

    scanf("%d %d",&n,&s[0]);
    for(i = 1;i <= n;++i)
        scanf("%d",&d[i]),s[i] = std::min(s[i - 1],std::abs(s[i - 1] - d[i]));

    for(i = n;i >= 1;--i)
        if(d[i] < 2 * (r[i + 1] + 1))
            r[i] = r[i + 1] + d[i];
        else
            r[i] = r[i + 1];

    scanf("%d",&q);
    while(q--)
        scanf("%d",&k),puts((s[k - 1] <= r[k + 1]) ? "NO" : "YES");

    return 0;
}

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

bool judge(int n){
    int i;
    for(i=2;i<=sqrt(n);i++){
        if(n%i==0) break;
    }
    if(i>sqrt(n) && n!=1) return 1;
    else return 0;
}
int main()
{
    int n;
    cin>>n;
    for(int i=n;;i++){
        if(judge(i)){
            cout<<i;
            break;
        }
    }
    return 0;
}


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

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL),cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair <ll, int> ii;
typedef pair <ii,int> iii;

const int mod = 1e9 + 7;
const int N = 200005;

char v[N];

void solve(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> v[i];
	int l = 0, r = n-1, ans = 0;
	while(l < r){
		while(l < r and v[l] == 'R')l++;
		while(r > l and v[r] == 'W')r--;
		if(l != r)ans++;
		swap(v[l], v[r]);
	}
	cout << ans << endl;
}

int main(){
	fastio;
	int t = 1;
	//cin >> t;
	while(t--)
		solve();
	return 0;
}


Measured memory_bytes: 3768
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint MOD = 1e9+7;

signed main(){
	lint N; cin >> N;
	vector<char> s(2 * N);
	for(int i = 0; i < s.size(); i++) cin >> s[i];
	vector<char> t(s.size());
	t[0] = 'L';
	int l = 1, r = 0;
	for(int i = 1; i < s.size(); i++){
		if(s[i] == s[i - 1]){
			if(t[i - 1] == 'L') t[i] = 'R', r++;
			else t[i] = 'L', l++;
		}
		else{
			if(t[i - 1] == 'L') t[i] = 'L', l++;
			else t[i] = 'R', r++;
		}
	}	
	if(s[0] == 'W' || s[s.size() - 1] == 'W' || l != r){
		cout << 0 << endl;
		return 0;
	}
	
	lint ans = 1, num = 0;
	vector<lint> counter(s.size(), 0);
	for(int i = 1; i < counter.size(); i++) counter[i] = counter[i - 1] + (t[i] == 'R');
	for(lint i = 2; i <= N; i++) ans *= i, ans %= MOD;
	for(int i = t.size() - 1; i >= 0; i--){
		if(t[i] == 'L'){
			ans *= (r - counter[i] - num);
			ans %= MOD;
			num++;
		}
	}
	cout << ans << endl;
}

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

int main()
{
    int a=1,b=1,c=1,sum=0,hcf,st,k;
    scanf("%d",&k);
    for(a=1;a<=k;a++){
        for(b=1;b<=k;b++)
        {
            for(c=1;c<=k;c++){
        st=a<b?(a<c?a:c):(b<c?b:c);
        for(hcf=st;hcf>=1;hcf--)
    {
        if(a%hcf==0 && b%hcf==0 && c%hcf==0)
            break;
    }
        sum=sum+hcf;

            }
        }
    }
    printf("%d\n",sum);


    return 0;
}


Measured memory_bytes: 2128
#include<iostream>
#include<cstdio>
#include<cstdlib>
#define N 4
using namespace std;

int mas[N][N];
int cpy[N][N];

void input() {

  for(int i = 0; i < N; i++)
    for(int j = 0; j < N; j++)
      cin>>mas[i][j];
}
void output() {

  for(int i = 0; i < N; i++)
    for(int j = 0; j < N; j++) {
      printf("%3d", mas[i][j]);
      cout<<endl;
    }
}
void copy(int m[N][N], int t[N][N]) {

  for(int i = 0; i < N; i++)
    for(int j = 0; j < N; j++) m[i][j] = t[i][j];
}
int calc() {
  int sum = 0;
  for(int i = 0; i < N*N; i++) {
    int v = mas[i/N][i%N];
    if(v) {
      v--;
      sum += abs(i/N-v/N)+abs(i%N-v%N);
    }
  }
  return sum;
}

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

void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}
int is_in(int x, int y) {
  return 0<=x && x<N && 0<=y && y<N;
}
int dfs(int x, int y, int p, int dep, const int lim) {
  int h = calc();

  if(h == 0) return dep;
  if(dep+h > lim) return -1;

  for(int i = 0; i < N; i++) {
    int nx = x+dx[i], ny = y+dy[i], res;
    if(~p && i == (p+2)%N) continue;
    if(!is_in(nx, ny)) continue;
    swap(&mas[y][x], &mas[ny][nx]);
    res = dfs(nx, ny, i, dep+1, lim);
    if(~res) return res;
    swap(&mas[y][x], &mas[ny][nx]);
  }
  return -1;
}
void get_empty(int *x, int *y) {

  for(int i = 0; i < N; i++) {
    for(int j = 0; j < N; j++) {
      if(!mas[i][j]) {
    *x = j;
    *y = i;
    return;
      }
    }
  }
}
int solve() {
  int x, y, lim, ans;
  get_empty(&x, &y);
  for(lim = 0; lim < 50; lim++) {
    copy(cpy, mas);
    if((ans = dfs(x, y, -1, 0, lim)) != -1) return ans;
    copy(mas, cpy);
  }
  return -1;
}
int main() {
  input();
  cout<<solve()<<endl;
  return 0;
}




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

int main() {

  string s; cin >> s;
  vector<string> vec(0);
  for(int i=0; i<s.size(); i++){
    if (s.at(i) == '0'){
      vec.push_back("0");
    } else if (s.at(i) == '1'){
      vec.push_back("1");
    } else {
       if (vec.size() > 0) vec.pop_back();
    }
  }
  
  for(int i=0; i<vec.size(); i++){
    cout << vec.at(i);
  }
  cout << endl;
}


Measured memory_bytes: 256
n = readline()
numbers = tryparse.(Int,split(readline()))

count = 0
num = 0
while num == 0 
  for i = 1:length(numbers)
      if numbers[i]&1 == 1
        global num = 1
        break
      end
      numbers[i] >>= 1
  end
  if num == 1
    break
  end
  global count += 1
end

println(count)

Measured memory_bytes: 177136
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
int main() {
	int n, k, ans = 0; cin >> n >> k;
	for (int i = 0; i < n; i++) {
		int x; cin >> x;
		ans += min(x, abs(k - x));
	}
	cout << ans * 2 << endl;
}

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

int main(){
    int A, B, K;
    cin >> A >> B >> K;

    int N=0;

    for(int i=100; i>0; i--){
        if(A%i==0 && B%i==0){
            N++;
            if(N==K){
                cout << i << endl;
                break;
            }
        }
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define emp emplace

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

int h, w;
int pd[1010][1010];
char m[1010][1010];

const int MOD = 1000 * 1000 * 1000 + 7;

int main()
{
	scanf("%d %d", &h, &w);
	for (int i = 0; i < h; i++)
		scanf("%s", m[i]);
	
	pd[h-1][w-1] = 1;
	for (int i = h-1; i >= 0; i--)
	{
		for (int j = w-1; j >= 0; j--)
		{
			if (i == h-1 and j == w-1) continue;
			if (m[i][j] == '#')
				pd[i][j] = 0;
			else
			{	
				pd[i][j] = (pd[i+1][j] + pd[i][j+1])%MOD;
			}
		}
	}
	printf("%d\n", pd[0][0]);
	return 0;
}



Measured memory_bytes: 5120
#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
// using namespace atcoder;

struct Edge {
    ll from;
    ll to;
    ll cost;
    Edge() { from = to = cost = 0; }

    Edge(ll f, ll t, ll c) {
        from = f;
        to = t;
        cost = c;
    }
};


void Main()
{
    ll n, m;
    cin >> n >> m;

    vector<ll> a(m), b(m), c(m);
    rep(i, m) cin >> a[i] >> b[i] >> c[i];

    vector<Edge> edges(m);
    rep(i, m) {
        edges[i] = Edge(a[i]-1, b[i]-1, -c[i]);
    }

    ll INF = 1e18;
    vector<ll> d(n, INF);
    d[0] = 0;

    rep(_i, n) {
        for (auto& e: edges) {
            ll v = d[e.from] + e.cost;
            if (d[e.from] != INF && d[e.to] > v) {
                d[e.to] = v;
                // n-1を含む無限ループがあるか
                if (_i == n - 1 && e.to == n - 1) {
                    // negative loop
                    put("inf");
                    return;
                }
            }
        }
    }

    put(-d[n-1]);
}
signed main(){ Main();return 0;}

Measured memory_bytes: 3680
(lambda T,S: (lambda O,N,K :(lambda
P:(lambda U :print ( sum(map(lambda
i: (lambda j :(0<=j<N)and(U[i]==U[j
]))(i-1+(U[i]=="R")*2),range(N)))))
(''.join( [ ( lambda X,V :[X,("R"if
X[0] == "L" else "L" )*len(X) ][ V%
2 and V//2<P] ) ( O[k],k ) for k in
range( len(O) ) ] ) ))( min( len(O)
//2,K )))(__import__("re").findall(
r"(R+|L+)",S),T[0],T[1]))(tuple(map
( int,input().split() )), input() )

Measured memory_bytes: 6336
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main()
{
  cout << fixed << setprecision(10);
  int A,B;
  cin >> A >> B;
  if(A<9&&B<9)
  {
    cout << "Yay!" << endl;
  }
  else
  {
    cout << ":(" << endl;
  }
  return 0;
}


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

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<int> id(m, -1); // iが何回目に登場したか
  vector<int> list;
  
  // 周期を得るまで回す
  ll x1 = x;
  int len = 0;
  ll total = 0;
  while (id[x1] == -1) {
    list.push_back(x1);
    id[x1] = len;
    len++;
    total += x1;
    x1 = x1 * x1 % m;
  }
  int c = len - id[x1]; // 周期のノード数
  ll c_total = 0; // 周期の合計点数 
  for (int i=id[x1]; i<len; i++) c_total += list[i];
  
  ll ans = 0;
  ll x2 = x;
  if (n <= len) {
    for (int i=1; i<=n;i++) {
      ans += x2;
      x2 = x2*x2%m;
    }
  } else {
    // 周期を使える時
    ans += total;
    n -= len;  //残り回数
    ans += (n / c) * c_total;
    n %= c;
    for (int i=0; i<n; i++) ans += list[ id[x1] + i];
  }
  
  cout << ans;
}

Measured memory_bytes: 3916
#include<stdio.h>
#include<stdlib.h>
#define MAX 20000

typedef struct{
  int top;
  int S[MAX];
}Stack;


void initialize(Stack *st)
{
  st->top = 0;
  st->S[st->top] = 0;
}

int isEmpty(Stack *st)
{
  return st->top == 0;
}

int isFull(Stack *st)
{  
  return st->top >= MAX - 1;
}

void push(int x, Stack *st)
{
  if (isFull(st)) 
    exit(1);

  st->top++;
  st->S[st->top] = x;
}

int pop(Stack *st) {
  if (isEmpty(st)) 
    exit(1);

  st->top--;
  return st->S[st->top+1];
}


int main(void)
{
  Stack st1;     // '\' の開始位置を格納するスタック
  Stack st2_j;   // 各水たまりの開始位置を格納するスタック
  Stack st2_a;   // 各水たまりの水量を格納するスタック

  // スタック初期化
  initialize(&st1);
  initialize(&st2_j);
  initialize(&st2_a);

  char c;
  int sum = 0;  // 水たまりの総水量

  // 水たまりの水量を計算
  for (int i = 0; (c = getchar()) != EOF; i++) {
    if (c == '\\')                                  // '\' を発見
      push(i, &st1);
    else if (!isEmpty(&st1) && c == '/') {          // '/' を発見
      int j = pop(&st1);
      sum += i - j;
      int a = i - j;
      while (!isEmpty(&st2_j) && st2_j.S[st2_j.top] > j) {
        pop(&st2_j);
        a += pop(&st2_a);
      }
      push(j, &st2_j);
      push(a, &st2_a);
    }
  }

  // 水たまりの水量を出力
  printf("%d\n", sum);
  printf("%d", st2_a.top);
  for (int i = 1; i <= st2_a.top; i++)
    printf(" %d", st2_a.S[i]);
  printf("\n");

  return 0;
}



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

using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long int

int main(){
  int N;
  string S;
  cin>>N>>S;
  int count=0;
  rep(i,10){
    rep(j,10){
      rep(k,10){
	for(int l=0;l<N;l++){
	  if(('0'+k)==S[l]){
	    for(int n=l+1;n<N;n++){
	      if(('0'+j)==S[n]){
		for(int m=n+1;m<N;m++){
		  if(('0'+i)==S[m]){
	       
		    // cout<<k<<j<<i<<endl;
		    // cout<<l<<n<<m<<endl;
		    count++;
		    
		   
		    m=N;
		    
		  }
		}
		n=N;    }
	    }
	    l=N; }
	}
     
      }    
    }
  }
 
  cout<<count<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef vector<llong> V;

#define _ cin.tie(0);ios::sync_with_stdio(false);
llong  n;
llong  t,a,tt,aa,x;
llong  ans=0;
int main(){ _;
  cin>>n;
  REP(i,n){
    cin>>t>>a;
    x=1;
    if(i==0){
      tt = t;aa = a;
      continue;
    }

    x = max((tt+t-1)/t,(aa+a-1)/a);
    tt = x*t; aa = x*a;
  }
  cout<<x*(t+a)<<endl;
  return 0;
}


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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m, cnt = 0;
    cin >> n >> m;
    int ans = 0;
    ans += n * (n - 1) / 2;
    ans += m * (m - 1) / 2;
    cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 3580
x1, y1, x2, y2 = gets.strip.split.map(&:to_i)
x3 = x2-(y2-y1)
y3 = y2+x2-x1
x4 = x3-(x2-x1)
y4 = y3-(y2-y1)
puts "#{x3} #{y3} #{x4} #{y4}"


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

const int ind = 1e5 + 5;





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

    int arr[n];

    for (int i = 0; i < n; i++) cin >> arr[i];

    int val = arr[0];
    int zor = 0;

    for(int i = 0; i < n; i++) {
        zor ^= (val ^ arr[i]);
    }

    for (int i = 0; i < n; i++)
        cout << (zor ^ arr[i]) << " ";
    cout << "\n";
    


    return 0;
}

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

typedef long long ll;

const int maxn = 10005, maxd = 110, mod = 1e9 + 7;
string K;
int D, n;
vector<int> digits;
ll dp[maxn][maxd][2];

ll rec(int pos, int sum, int flag) {
    if (pos == n) {
        if (sum % D == 0) {
            return 1;
        }
        return 0;
    }
    if (dp[pos][sum][flag] != -1) {
        return dp[pos][sum][flag];
    }
    int limit = (flag == 1)? digits[pos] : 9;
    ll tot = 0;
    for (int i = 0; i <= limit; i++) {
        if (i == digits[pos]) {
            tot = (tot + rec(pos + 1, (sum + i) % D, flag)) % mod;
        } else {
            tot = (tot + rec(pos + 1, (sum + i) % D, 0)) % mod;
        }
    }
    return dp[pos][sum][flag] = tot;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> K >> D;
    for (char it : K) {
        digits.push_back(it - '0');
    }
    n = digits.size();
    memset(dp, -1, sizeof(dp));
    ll answer = (rec(0, 0, 1) - 1 + mod) % mod;
    cout << answer;

    return 0;
}

Measured memory_bytes: 21880
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>

#define FOR(i,a,b) for(lint i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()

typedef long long lint;

using namespace std;

int main(){
  lint N,K;
  cin>>N>>K;
  int A[N];
  REP(i,N){
    cin>>A[i];
    A[i]--;
  }
  int D[N];
  REP(i,N)D[i]=-1;
  int p=0;
  D[0]=0;
  bool f=true;
  while(K>0){
    // cerr<<p<<endl;
    if(f and D[A[p]]>=0){
      // cerr<<K<<" "<<(D[p]-D[A[p]]+1)<<endl;
      K%=(D[p]-D[A[p]]+1);
      f=false;
      if(K==0)break;
    }
    D[A[p]]=D[p]+1;
    p=A[p];
    K--;
  }
  cout<<p+1<<endl;;
  return 0;
}


Measured memory_bytes: 5172
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(int i=0; i<n; i++)
using namespace std;

//6桁未満の数字を0でパディングして表示
void convert(int n){
  int array[] = {0, 0, 0, 0, 0, 0};
  int i = 0;
  //逆順の方が入れやすい．後で補正する
  while(n != 0){
    if(n%10 != 0)
      array[i++] = n%10;
    else
      array[i++] = 0;
    n /= 10;
  }
  //最後に逆順に表示
  for(int i=5; i>=0; i--)
    cout << array[i];
}


int main(){
  int N, M;
  cin >> N >> M;
  int P[M], Y[M];
  rep(i, M)
    cin >> P[i] >> Y[i];
  //ここまで入力

  //県iに年の情報を持たせる
  vector<vector<int> > v(N);
  rep(i, M)
    v[P[i]-1 ].push_back(Y[i] );

  //年でソート
  rep(i, N)
    sort(v[i].begin(), v[i].end() );

  rep(i, M){
    //県iをパディング表示
    convert(P[i]);
    int pre = P[i]-1;
    //ソート済みの配列において，年が何番目か
    int num = lower_bound(v[pre].begin(), v[pre].end(), Y[i] )-v[pre].begin();
    //何番目かのパディング表示
    convert(num+1);
    cout << endl;
  }

  return 0;
}


Measured memory_bytes: 7680
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#define rep(i,n) for(int i=0; i<(n); i++)
typedef long long ll;
using namespace std;



int main(){
    int a,b;
    cin >> a >> b;
    
    int ans = 0;
    ans = a + b;
    ans = max(ans, a-b);
    ans = max(ans, a*b);
    
    cout << ans << endl;
    


}



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

int N, node;

void check(){
  
  for(int i=1;i<100000;i++){
    
    if( i * ( i - 1 ) / 2 == N ) node = i;
    
  }
  
}

vector<int> ans[100000];

signed main(){
  
  cin>>N;
  
  check();
  
  if( node == 0 ){
    cout<<"No"<<endl;
    return 0;
  }
  
  cout<<"Yes"<<endl;
  
  cout<<node<<endl;
  
  int num = 1;
  
  for(int i=0;i<node;i++){
    
    for(int j=i+1;j<node;j++){
      
      ans[i].push_back( num );
      
      ans[j].push_back( num );
      
      num++;
      
    }
    
  }
  
  for(int i=0;i<node;i++){
    cout<<node-1;
    for(int j=0;j<(int)ans[i].size();j++){
      cout<<' '<<ans[i][j];
    }
    cout<<endl;
  }
    
  return 0;
}


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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

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


    lint n;
    cin >> n;

    lint ans, tmp;

    queue<string> q;

    q.push("a");

    REP(i,n-1){

        while(1){
            int max_s = 0;
            string s = q.front();
            if (SZ(s)==i+2){
                break;
            }
            q.pop();
            REP(j,SZ(s)){
                max_s = max((int)s[j], max_s);
                //cout << max_s -'a' << "q";
            }
            REP(j,max_s -'a' +2){
                string s_push = s + (char)('a' + j);
                q.push(s_push);
                //cout << s_push << "\n";
            }

        }
    }

    while(!q.empty()){
        string s = q.front();
        q.pop();
        cout << s << "\n";
    }
    
    //cout << ans <<"\n";

    return 0;
}

Measured memory_bytes: 9728
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <iomanip>
#include <math.h> 
#include <stack>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <tuple>
#include <cctype>
#include <ctype.h>
#include <set>
#include <sstream>
#include <time.h>
#include <numeric>
using namespace std;
//#define int long long
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
#define rrep(i,s,n) for(int i = (n)-1;i>=(s);i--)
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a,b) a=min((a),(b))
#define chmax(a,b) a=max((a),(b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i,j) ((i<0 && j<0) || (i>0 && j>0)) ? true : false 
typedef long long ll;
typedef pair<int, int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265359;
int ca[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };



int main(void) {
	const int inf = 100000000;


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

	int a[1000], b[1000], c[1000], dist[100][100];

	for (int i = 0; i < m; ++i) {
		cin >> a[i] >> b[i] >> c[i];
		a[i]--, b[i]--;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (i == j)
				dist[i][j] = 0;
			else
				dist[i][j] = inf;
		}
	}

	for (int i = 0; i < m; ++i) {
		dist[a[i]][b[i]] = min(dist[a[i]][b[i]], c[i]);
		dist[b[i]][a[i]] = min(dist[b[i]][a[i]], c[i]);
	}

	for (int k = 0; k < n; ++k) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}

	int ans = m;
	for (int i = 0; i < m; ++i) {
		bool shortest = false;
		for (int j = 0; j < n; ++j) if (dist[j][a[i]] + c[i] == dist[j][b[i]]) shortest = true;
		if (shortest == true) {
			ans = ans - 1;
		}
	}

	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    vi x(m);
    rep(i, m) cin >> x[i];
    
    if (n >= m) {
        cout << 0 << endl;
        return 0;
    }
    sort(x.begin(), x.end());
    vi d(m-1);
    rep(i, m-1) d[i] = x[i+1] - x[i];
    sort(d.begin(), d.end());
    ll ans = accumulate(d.begin(), d.begin()+m-n, 0LL);
    cout << ans << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define LL               long long
#define PB               push_back
using namespace std;
int main (){
  int n; cin>>n;
  if (n<600) cout<< 8 ;
  else if (n<800) cout<<7  ;
  else if (n<1000) cout<<6  ;
  else if (n<1200) cout<< 5 ;
  else if (n<1400) cout<< 4 ;
  else if (n<1600) cout<< 3;
  else if (n<1800) cout<<2  ;
  else cout<< 1 ;
}

Measured memory_bytes: 3592
#define _USE_MATH_DEFINES
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n), dp(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = n - 2; i >= 0; --i) {
    dp[i] = 2e9;
    if (i + 1 < n) {
      dp[i] = min(dp[i], dp[i + 1] + abs(a[i] - a[i + 1]));
    }
    if (i + 2 < n) {
      dp[i] = min(dp[i], dp[i + 2] + abs(a[i] - a[i + 2]));
    }
  }
  printf("%d\n", dp[0]);
  return 0;
}


Measured memory_bytes: 1024