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

using namespace std;

#define MAX 3001
 
int N, S, T, a[MAX], b[MAX];
int dp[MAX][MAX], ans;
 
void solve(){
  for(int i = 1 ; i <= N ; i++){
    for(int j = 1 ; j <= T ; j++){
      dp[i][j] = max(dp[i][j-1], dp[i-1][j]);;
      int k = j - b[i];
      if(k >= 0 && !(j-b[i] < S && S < j)){
        dp[i][j] = max(dp[i][j], dp[i-1][k] + a[i]);
      }
    }
  }
  ans = -1;
  for(int i = 1 ; i <= T ; i++){
    ans = max(ans, dp[N][i]);
  }
  cout << ans << endl;
}
 
int main(){
  cin >> N >> T >> S;
  for(int i = 1 ; i <= N ; i++){
    cin >> a[i] >> b[i];
  }
  solve();
 
  return 0;
}

Measured memory_bytes: 36360
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
#define inf 1000000007
#define LINF 1000000000000000007mope
#define ll long long
using namespace std;
signed main(){
	int n,m;
	cin>>n>>m;
	vector< pair<int,int> > v;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		v.pb( mp( y,x ) );
	}
	sort( v.begin(),v.end () );
	int rmc = 0;
	int ans = 0;
	for(int i=0;i<m;i++){
		if( rmc < v[i].second ){
			ans ++ ;
			rmc = v[i].first-1;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 2420
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <limits>
#define INF 100000000
typedef long long int ll;
using vi = std::vector<int>;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n;
    int ans = INF;
    int ret = 0;
    cin >> n;
    vi w(n); rep(i, n) cin>>w[i];
    int sum=0;
    rep(i, n) sum += w[i];
    rep(i, n-1){
        ret += w[i];
        ans = min(ans, abs((sum-ret)-ret));
        }
    cout << ans << endl;
    }





Measured memory_bytes: 256
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int s = input.nextInt();
        int h = s/3600;
        int m = (s-(h*3600))/60;
        s = s-h*3600-m*60;
        System.out.println(h + ":" + m + ":" + s);
    }
}


Measured memory_bytes: 26352
def erathosthenes(n):
    if n < 2:
        return False
    
    prime_list = []
    search_max = n ** 0.5

    cand = [i for i in range(2, n+1)]
    while True:
        prime_cand = cand[0]
        if search_max <= prime_cand:
            return prime_list + cand
        prime_list.append(prime_cand)
        cand = [j for j in cand if j % prime_cand != 0]

X = int(input())
SIZE = 100003
prime_list = erathosthenes(SIZE)

for i in range(len(prime_list)):
    if prime_list[i] >= X:
        print(prime_list[i])
        break

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

using namespace std;

typedef long long ll;
typedef vector<int> pi;
typedef pair<int,int> vi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i=a; i < b; i++)


bool sort_by(vi c, vi d){
	return c.S < d.S;
}

string solve(string n, int sum, int depth, string sol){
	//cout << sum << endl;
	if(depth == 4 && sum == 7) return sol + "=7";
	else if(depth == 4 && sum != 7) return "";
	else{
		int num = n[depth] - '0';
		string p = solve(n, sum+num, depth+1, (depth==0?sol+to_string(num):sol + "+"+  to_string(num)));
		if(p== "")  p = solve(n, sum-num, depth+1, (depth==0?sol+to_string(num):sol + "-" + to_string(num)));
		return p;
	}
}	


int main() {
	string N;
	cin >> N;
	cout << solve(N, 0, 0, "");
}


Measured memory_bytes: 256
#include <iostream>
#include <stack>
#define MAX 101
#define NIL -1

using namespace std;


int matrix[MAX][MAX] = {0};
int d[MAX], f[MAX];
int n, t;

void dfs();
void dfs_visit(int u);
int next(int u);

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> v;
			matrix[u][v] = 1;			
		}
	}
	
//显示邻接矩阵 
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = 0; j < n; j++)
//		{
//			if(j) cout << ' ';
//			cout << matrix[i+1][j+1];		
//		}
//		cout << endl;
//	}
	
	
	dfs();
	
	
	for (int i = 1; i <= n; i++)
	{
		cout << i << ' ';
		cout << d[i] << ' ';	
		cout << f[i] << endl;
	}
} 



void dfs()
{	
	for (int i = 1; i <= n; i++)
	{
		if (d[i] == 0)
			dfs_visit(i);
	}
}



void dfs_visit(int u)
{

	stack<int> S;
	S.push(u);
	d[u] = ++t;
	
	while (!S.empty())
	{
		u = next(S.top());
		if (u)
		{
//			if (d[u] == 0)
			{
			d[u] = ++t;
			S.push(u); 				
			}
		}
		else
		{
			f[S.top()] = ++t;
			S.pop();
		}
		
	}
	
}


int next(int u)
{
	for (int i = 1; i <= n; i++)
	{
		if (matrix[u][i] != 0 && d[i] == 0)
			return i;
	}
	return 0;
}


Measured memory_bytes: 3132
#include<iostream>
#include <list>
#include<stack>
#include<queue>
#include <vector>
#include <set>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string>
#include <functional>
#include<fstream>

#define FOR(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define ll long long
#define CLR(a) memset((a),0,sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<<str<<endl
using namespace std;

const ll MOD = 1000000007;// 10^9+7
const int INF = (1 << 30);
constexpr int MAX_N = 200005;

struct BIT {
	int n;
	vector<ll> bit;

	BIT(int n_) {
		n_++;
		n = n_;
		bit.resize(MAX_N);
		REP(i, n)add(i, 1);
	}
	ll sum(ll i) {
		i++;
		ll s = 0;
		while (i > 0) {
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}
	void add(ll i, ll x) {
		i++;
		while (i <= n) {
			bit[i] += x;
			i += i & -i;
		}
	}
};

//変数
int N;
ll K;
vector<ll> a;






//サブ関数
//入力
void input()
{
	cin >> N >> K;
	a.resize(N + 1);
	REP(i, N)cin >> a[i + 1];
	REP(i, N)a[i + 1] -= K;
	REP(i, N)a[i + 1] += a[i];
}

vector<pair<ll, ll>> make_sample(vector<ll> a)
{
	vector<pair<ll, ll>> sample;
	REP(i, N + 1)sample.push_back({ a[i],i });
	sort(sample.begin(), sample.end());
	return sample;
}


ll count(
	vector<pair<ll, ll>>& sample,
	BIT& bit,
	pair<ll, ll> alpha)
{
	auto itr = lower_bound(sample.begin(), sample.end(), alpha);
	if (alpha != *itr)exit(1);
	int id = distance(sample.begin(), itr);

	int res = bit.sum(N) - bit.sum(id);
	bit.add(id, -1);
	return res;
}

//計算
void calc()
{
	ll res = 0;
	BIT bit(N);
	auto sample = make_sample(a);

	REP(i, N+1)res += count(sample, bit, { a[i],i });

	cout << res << endl;
}


//出力
void output()
{

}


//デバッグ
void debug()
{
	int N;
	cin>>N;
}


//メイン関数
int main()
{
	input();
	calc();
	output();
	debug();
	
	return 0;
}


Measured memory_bytes: 11120
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int n; cin>>n;
    vector<int> a(n);
    rep(i,n) cin>>a[i];
    vector<int> b(n);
    rep(i,n) b[a[i]-1]=i+1;
    rep(i,n) cout<<b[i]<<" ";
}

Measured memory_bytes: 4036
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#define rep(index,num) for(int index=0;index<num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
typedef long long ll;
using namespace std;
typedef pair<int,int> pint;
int main(){
	int N;
	scan(N);
	if(N<105){
		prin("0");
	}
	else if(N<135){
		prin("1");
	}
	else if(N<165){
		prin("2");
	}
	else if(N<189){
		prin("3");
	}
	else if(N<195){
		prin("4");
	}
	else{
		prin("5");
	}
	return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x), y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, q;
  cin >> n >> q;
  UnionFind uf;
  uf.init(n);
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0) {
      uf.unite(u, v);
    } else {
      cout << uf.same(u, v) << endl;
    }
  }
  return 0;
}

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

int grid[2001][2001];
int n, k;

void init(int x, int y) {
  if (x < 0 || x >= 3*k) return;
  if (y < 0 || y >= 3*k) return;
  // 二次元累積和の前処理. 
  int lx = max(0, x-k+1);
  int uy = max(0, y-k+1);
  int rx = min(x+1, 2*k);
  int dy = min(y+1, 2*k);
  // cout << x << " " << y << " " << lx << " " << uy << " " << rx << " " << dy << endl;
  grid[uy][lx] += 1;
  grid[dy][rx] += 1;
  grid[dy][lx] -= 1;
  grid[uy][rx] -= 1;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W') x += k;
    x %= 2*k;
    y %= 2*k;
    init(x, y);
    init(x-k, y-k);
    init(x-k, y+k);
    init(x+k, y-k);
    init(x+k, y+k);
    init(x+2*k, y);
    init(x, y+2*k);
  }

  for (int y = 0; y < 2*k; ++y) {
    for (int x = 1; x < 2*k; ++x) {
      grid[y][x] += grid[y][x-1];
    }
  }
  for (int x = 0; x < 2*k; ++x) {
    for (int y = 1; y < 2*k; ++y) {
      grid[y][x] += grid[y-1][x];
    }
  }
  // for (int y = 0; y < k; ++y) {
  //   for (int x = 0; x < k; ++x) {
  //     cout << grid[y][x];
  //   }
  //   cout << endl;
  // }

  int ans = 0;
  for (int x = 0; x < 2*k; ++x) {
    for (int y = 0; y < 2*k; ++y) {
      ans = max(ans, grid[y][x]);
    }
  }
  cout << ans << endl;
}



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

int main() {
	int n;
	char ch;
	cin>>n;
	vector<int> a(n,0);
	int l=0,r=n-1;
	for(int i=0;i<n;i++){
	    cin>>ch;
	    if(ch=='R') a[i]=1;
	}
	int count=0;
	while(l<r){
	    if(a[l]==0&&a[r]==1){ 
	        count++;
	        l++;
	        r--;
	    }
	    else{
	        if(a[l]==1) l++;
	        if(a[r]==0) r--;
	    
	}}
	cout<<count;
	return 0;
}

Measured memory_bytes: 4048
N=int(input())
X=list(map(int,input().split()))
mid1=sum(X)//N+1
mid2=sum(X)//N


ans=0
ans2=0

for i in range(N):
    ans+=(X[i]-mid1)**2
    
for i in range(N):
    ans2+=(X[i]-mid2)**2
    
print(min(ans,ans2))

Measured memory_bytes: 3064
#define _LIBCPP_DEBUG 0
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1e9 + 7;

int H,W;
char S[22][22];

int DFS(int x, int y, int max){

    return 0;
}


int main(void) {

    vector<int> A(3);
    for (int i = 0; i < 3; ++i) {
        cin>>A[i];
    }
    sort(A.begin(),A.end());
    int ans=0;
    for (int i = 0; i < 2; ++i) {
        ans += A[i+1]-A[i];
    }

    cout<<ans<<endl;

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

struct cmp{
    bool operator()(const array<int,3> &a,const array<int,3> &b){
        return a[1]-a[2]>b[1]-b[2];
    }
};

struct cmp2{
    bool operator()(const array<int,3> &a,const array<int,3> &b){
        return a[1]-a[2]<b[1]-b[2];
    }
};


int32_t main(){ 
    FAST
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        vector<array<int,3>> v(n);
        for(auto &i : v) cin>>i[0]>>i[1]>>i[2];

        map<int,set<int>> mp_to_forward,mp_to_backword;
        for(int i=0;i<n;i++){
            if(v[i][1]>v[i][2]) mp_to_forward[v[i][0]].insert(i);
            else mp_to_backword[v[i][0]].insert(i);
        }

        priority_queue<array<int,3>,vector<array<int,3>>,cmp> pq;
        int ans=0;
        for(int i=1;i<=n;i++){
            for(auto j :  mp_to_forward[i]){
                pq.push(v[j]);
                ans+=v[j][1];           //taking li value
            }
            while(pq.size()>i){
                ans-=pq.top()[1];
                ans+=pq.top()[2];
                pq.pop();
            }
        }


        priority_queue<array<int,3>,vector<array<int,3>>,cmp2> pq2;
        for(int i=n;i>=1;i--){
            for(auto j :  mp_to_backword[i]){
                pq2.push(v[j]);
                ans+=v[j][2];           //taking ri value
            }
            while(pq2.size()>n-i){
                ans-=pq2.top()[2];
                ans+=pq2.top()[1];
                pq2.pop();
            }
        }
        

        cout<<ans<<"\n";

    }
    return 0;
}

Measured memory_bytes: 60232
#include<bits/stdc++.h>
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int n;
long long ans;
string a;
map<string,int>m;
int main() {
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>a;
		sort(a.begin(),a.end());
		ans+=m[a];
		m[a]++;
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 11136
import * as fs from "fs";

const input = (fs.readFileSync("/dev/stdin", "utf8") as string).split("\n");
const n = +input[0];
let result = 0
for(let i=1;i<=n;i++){
	if(i%3===0 || i%5===0)continue;
	result+=i;
}

console.log(result);


Measured memory_bytes: 35164
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define INF 1145141919
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int ny, nx;
typedef long long int ll;
using namespace std;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll m, ll n)
{
    if ((0 == m) || (0 == n))
        return 0;
    return ((m / gcd(m, n)) * n);
}

ll llpow(ll x, ll y)
{
    ll ans = 1;
    REP(i, y)
    ans *= x;
    return ans;
}

int ctoi(char c)
{
    if (c >= '0' && c <= '9')
    {
        return c - '0';
    }
    return 0;
}

int cnt_digits(ll N){
    int ans=0;
    while(N>0){
        N/=10; ans++;
    }
    return ans;
}

////////////////////////////////////////////////////////////////////

int main()
{
    ll N; cin >> N;
    int ans=cnt_digits(N);

    for(ll A=1LL; A*A<=N; A++){
        if(N%A!=0) continue;
        const ll B=N/A;
        const int res = max(cnt_digits(A), cnt_digits(B));
        if(res<ans) ans=res;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int n,sum=0,a=0,f;
  int h[1000];
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>h[i];
    sum+=h[i];
  }
  while(true){
    if(sum==0) break;
    f=0;
    for(int i=0;i<n;i++){
      if(h[i]>0){
        sum--;
        h[i]--;
        f=1;
      }
      else if(f==1) break;
    }
    a++;
  }
  cout<<a<<"\n";
}

Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
#include <cctype>

using namespace std;

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

int gcd(int m, int n)
{
	if(n == 0)
		return m;
	return gcd(n, m%n);
}
int main()
{
	int a, b;

	cin >> a >> b;
	cout << gcd(a, b) << endl;
	return 0;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
#define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;

int main()
{
    Fast;
    long long t,n,m,i,j,k,x=0,y=0,z;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='A'){
            x=1;
        }
        if(s[i]=='B'){
            y=1;
        }
    }
    if(x==1 && y==1)
    {
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
    }

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(a) int((a).size())

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
 
    ll n,i,j;
    cin>>n;
    string s;
    cin>>s;
    ll r,g,b;
    r=g=b=0;
    for(i=0;i<n;i++)
    {
        if(s[i] == 'R') r++;
        if(s[i] == 'G') g++;
        if(s[i] == 'B') b++;
    }
    ll ans = r*g*b;
    for(i=0;i<n;i++)
    {
        for(j=i+1;2*j-i<n;j++)
        {
            if(s[i]!=s[j] && s[i]!=s[2*j-i] && s[j]!=s[2*j-i]) ans--;
        }
    }
    cout<<ans;
}

Measured memory_bytes: 3636
/*
    Ujjawal Pabreja
*/
#include <bits/stdc++.h> 
#include <functional> 
using namespace std;
using ll = long long;
using ld = long double;
#define speed_cubing    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i,a)        for(ll i = 0 ; i < a ; i++)
#define repe(i,a,b)     for(ll i = a ; i < b ; i++)
#define bac(i,a)        for(ll i = a ; i >= 0 ; i--)
#define bace(i,a,b)     for(ll i = a ; i >= b ; i--)
#define pb              push_back
#define mp              make_pair
#define in              insert
#define ff              first
#define ss              second
#define vv              vector
#define vll             vector <ll>
#define pll             pair <ll,ll>
#define vpll            vector <pll>
#define umap            unordered_map
#define uset            unordered_set
#define mset            multiset
#define pqueue          priority_queue
#define setbit(x)       __builtin_popcountll(x)
#define init(c,a)       memset(c,a,sizeof(c))
#define all(c)          c.begin(),c.end()
#define sz(c)           (ll)c.size()
#define lb              lower_bound
#define ub              upper_bound
#define maxe            *max_element
#define mine            *min_element
#define gcd(a,b)        __gcd(a,b)
#define rev             reverse
#define Y				"YES"
#define N 				"NO"
#define nl              "\n"
#define debug(x)        cout << #x << " : " << x << nl;
const ll inf = 1e18;  
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll MAX = 2e5 + 1;

// ====== int_1024t begins ======
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// ====== int_1024t ends ======

// ====== pbds begins ======
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, 
//         tree_order_statistics_node_update> 
//         pbds; 
// ====== pbds ends ======

// ====== Useful functions begin ======
inline ll add(ll a,ll b) {return ((a%mod)+(b%mod))%mod;}
inline ll sub(ll a,ll b) {return ((a%mod)-(b%mod)+mod)%mod;}
inline ll mul(ll a,ll b) {return ((a%mod)*(b%mod))%mod;}
ll pwr(ll x,ll n){
    if(!n) return 1;
    if(n&1) return mul(x, pwr(mul(x,x),(n-1)/2));
    else return pwr(mul(x,x),n/2);
}
ll modinv(ll n){ 
    return pwr(n,mod - 2); 
}
// ====== Useful functions end ======
 
// ====== Variables begin ======

// ====== Variables end ======
void speed_solve(){
    ll n;
    cin >> n;
    cout << (1000 - n % 1000) % 1000;
}

int main(){
    speed_cubing;
    ll test_cases = 1;
    // cin >> test_cases;
    rep(i,test_cases){
        // cout << "Case #" << i + 1 << ": ";
        speed_solve();
    }
    return 0;
}

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

int main()
{
    set<string> dict;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string s,t;
        cin >> s >> t;
        if (s == "insert")
        {
            dict.insert(t);
        }
        else
        {
            if (dict.count(t) == 0)
            {
                cout << "no" << endl;
            }
            else
            {
                cout << "yes" << endl;
            }
        }
    }
    return 0;
}



Measured memory_bytes: 32180
package main

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

var (
	sc = bufio.NewScanner(os.Stdin)
	w  = bufio.NewWriter(os.Stdout)
)

func readInts(n int) []int {
	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		sc.Scan()
		a[i], _ = strconv.Atoi(sc.Text())
	}
	return a
}

func main() {
	sc.Split(bufio.ScanWords)
	var n int
	fmt.Scan(&n)
	a := readInts(n)

	b := make([]int, len(a))
	for i := n; i >= 1; i-- {
		sum := 0
		for j := i + i; j <= n; j += i {
			sum ^= b[j]
		}
		b[i] = sum ^ a[i]
	}
	ans := make([]int, 0, len(a))
	for i, e := range b {
		if e == 1 {
			ans = append(ans, i)
		}
	}

	fmt.Fprintln(w, len(ans))
	for i, e := range ans {
		fmt.Fprint(w, e)
		if i == len(ans)-1 {
			fmt.Fprintf(w, "\n")
		} else {
			fmt.Fprintf(w, " ")
		}
	}
	w.Flush()
}


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

bool dp[2][16001][16001] = {};
int main() {
	string s;
	cin >> s;
	int x,y;
	cin >> x >> y;
	
	s += "T";
	int cou = 0;
	int muki = 0;
	int t[2] = {};
	
	dp[0][0][8000] = 1;
	dp[1][0][8000] = 1;
	int isf = 1;
	for(auto ch : s){
		if(ch == 'T'){
			
			if(isf){
				dp[0][1][8000+cou] = 1;
			}
			else{
				for(int i = 1; 16000 > i; i++){
					dp[muki][t[muki] + 1][i + cou] |= dp[muki][t[muki]][i];
					dp[muki][t[muki] + 1][i - cou] |= dp[muki][t[muki]][i];
				}
			}
			isf = 0;
			
			t[muki]++;
			cou = 0;
			muki = 1-muki;
		}
		else
			cou++;
	}
	
	if(dp[0][t[0]][x+8000] && dp[1][t[1]][y+8000])
		cout << "Yes" << endl;
	else
	cout <<"No"<<endl;
	
	return 0;
}

Measured memory_bytes: 127232
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<numeric>
#include<algorithm>
#include<complex>
#include<queue>
#include<stdlib.h>
#define ll long long
#define for(i,n) for(int i=0;i<n;i++)

using namespace std;

int main(){
    int N;
    cin >> N;
    string S;
    int K;
    cin >> S;
    cin >> K;
    for(i,N){
        if(S[i]!=S[K-1]){
            S[i]='*';
        }
    }
    cout << S <<endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        int a = 0;
        int z = 0;
        boolean ba = true;
        boolean bb = true;
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == 'A' && ba){
                a = i;
                ba = false;
            }
            if(s.charAt(s.length() - i - 1) == 'Z' && bb){
                z = s.length() - i - 1;
                bb = false;
            }
            if(!ba && !bb){
                break;
            }
        }
        System.out.println(z - a + 1);
    }
}

Measured memory_bytes: 25684
#pragma GCC optime(3)
#pragma GCC optime(2)
#pragma GCC optime(1)
#include<bits/stdc++.h>
using namespace std;
long long n,l=1,r=3,mn=INT_MAX,a[200001];
inline long long read()
{
    long long sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
	{
        if (ch=='-')
		  x=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9')
	{
        sum=sum*10+ch-'0';
        ch=getchar();
    }
    return sum*x;
}
inline void write(long long x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');  
    return;
}
int main()
{
	n=read();
	for (register int i=1;i<=n;++i)
	{
		int x=read();
		a[i]=a[i-1]+x;
	}
	for (register int i=2;i<=n;++i)
	{
		while (l+1<i&&abs(a[i]-a[l]*2)>abs(a[i]-a[l+1]*2))
		  ++l;
		while (r+1<n&&abs(a[n]-a[r]-(a[r]-a[i]))>abs(a[n]-a[r+1]-(a[r+1]-a[i])))
		  ++r;
		long long b=max(a[l],max(a[i]-a[l],max(a[r]-a[i],a[n]-a[r])));
		long long c=min(a[l],min(a[i]-a[l],min(a[r]-a[i],a[n]-a[r])));
		mn=min(mn,b-c);
	}
	write(mn);
	return 0;
}

Measured memory_bytes: 5172
#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    char s1[10000];
    while(cin>>s)
    {
        int len=s.size();
        int j=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]!='B')
                s1[j++]=s[i];
            else
            {
                j--;
                if(j<0)
                    j=0;
            }
        }
        s1[j]='\0';
        cout<<s1<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
import math
import sys
sys.setrecursionlimit(10**7)
#X=int(input())
N,M,Q=map(int,input().split())
S=[[]for i in range(Q)]
for i in range(Q):
    a=list(map(int,input().split()))
    S[i]=a
ans=0
T=0
def solve(l):
    global ans
    if len(l)>=N:
        tmp=0
        for k in range(Q):
            if l[S[k][1]-1]-l[S[k][0]-1]==S[k][2]:
                tmp+=S[k][3]
        ans=max(ans,tmp)
        return
    for i in range(l[-1],M+1):
        solve(l+[i])
solve([1])
print(ans)

Measured memory_bytes: 72260
n,m=map(int,input().split())

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

cash=[0 for i in range(n+1)]

for i in range(1,n+1):
  for j in range(m):
    if (i-c[j])>=0:
      coins=cash[i-c[j]]+1
      if cash[i]==0:
        cash[i]=coins
      elif cash[i]>coins:
        cash[i]=coins

print(cash[n])


Measured memory_bytes: 7376
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define M 1000000009
#define maxN 1000000

void solve()
{
	lli a;
	cin >> a;
	lli ans = a + (a*a) + (a*a*a);
	cout << ans << "\n";
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int t;
	t = 1;
	while(t--){
		solve();		
	}
	return 0;
}


Measured memory_bytes: 3688
#include <iostream>
#include <string>


int main() {
   int n; std::cin >> n;
   std::string s; std::cin >> s;
  
   for (int i = 0; i < (int)s.size(); ++i) {
      char c = s[i] - 'A'; // 0~25に変換
      c = (c+n) % 26;
      c += 'A';
      std::cout << c;
   }
   std::cout << std::endl;

   return 0;
}


Measured memory_bytes: 3620
#define  _CRT_SECURE_NO_WARNINGS
	#include <algorithm>
	#include <iostream>
	#include <iterator>
	#include <numeric>
	#include <sstream>
	#include <fstream>
	#include <cassert>
	#include <climits>
	#include <cstdlib>
	#include <cstring>
	#include <string>
	#include <cstdio>
#include<algorithm>
	#include <vector>
	#include <math.h>
	#include <queue>
	#include<memory>
	#include <deque>
	#include <stack>
	#include <list>
	#include <map>
	#include <set>
	#include <cmath>
	#include <bitset>
	#include<list>
	#include <iomanip>
	using namespace std;
	typedef long long ll;
	int m[200005];
	void go()
	{
		m[0] = 1;
		m[1] = 1;
		for (int i = 2; i < 40000; i++)
		{
			for (int j = i * i; j < 10000; j += i)
			{
				m[j] = 1;
			}
		}
	}
	long long  a[(int)1e5+10][4];
	int main()
	{	
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)for (int j = 1; j <= 3; j++)cin >> a[i][j];
		for (int i = n; i > 0; i--)
		{
			for (int j = 1; j <= 3; j++) {
				long long m = 0;
				for (int k = 1; k <= 3; k++) {
					if(j!=k)m = max(a[i + 1][k] + a[i][j],m);
				}
				a[i][j] = m;
			}
		}
		long long ans = 0;
		for (int i = 1; i <= 3; i++)ans = max(ans, a[1][i]);
		cout << ans << endl;
		return 0;
	}

Measured memory_bytes: 3328
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll solve(ll a){
    return a * (a + 1) / 2;
}

int main() {
    ll n;
    cin >> n;
    ll ans = 0;
    REPO(i, n){
        ans += i * solve(n / i);
    }
    cout << ans << endl;
}


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

#define REP(i,n) for (int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()

using namespace std;

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

    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0) break;
        vector<int> a(n);
        REP(i, n) cin >> a[i];

        sort(all(a));

        int result = 0;

        REP(i, n) {
            auto ub = upper_bound(a.begin(), a.begin() + i, m - a[i]);
            if (ub != a.begin()) {
                result = max(result, a[i] + *(ub - 1));
            }
        }

        if (result) {
            cout << result << '\n';
        } else {
            cout << "NONE\n";
        }
    }
    cout << flush;

    return 0;
}


Measured memory_bytes: 3200
#include<stdio.h>

int main(void){
    int x;
    scanf("%d", &x);
    int max, hasu, num, ans=0;
    max = (int)x/100;
    hasu = x%100;

    num = 0;
    for(int i=5; i>0; i--){
        num += (int) (hasu/i);
        hasu = hasu%i;
    }
    //printf("max = %d, num = %d\n", max, num);
    if(num <= max)printf("1\n");
    else printf("0\n");

    return 0;
}

Measured memory_bytes: 128
q = int(input())
for it in range(q):
    a, b = map(int, input().split())
    if a * b <= 2:
        print(0)
        continue
    lo, hi = 1, a*b - 1
    while lo < hi:
        x = (lo + hi + 1) // 2
        #l = list(range(max(1, x // 2 - 2), x // 2 + 3))
        #r = list(range(max(1, x // 2 - 2), x // 2 + 3))
        if x % 2 == 0:
            r1, r2 = x // 2, x // 2 + 1
            l1, l2 = x // 2, x // 2 + 1
        else:
            l, r = (x + 1) // 2, (x + 1) // 2 
            if a <= l:
                l += 1
            if b <= r:
                r += 1
            if l * r < a * b:
                lo = x
            else:
                hi = x - 1
            continue
        if b <= r1:
            r1 += 1
            r2 += 1
        #elif b >= r2 and b <= x:
        #    r1 -= 1
        #    r2 -= 1
        if a <= l1:
            l1 += 1
            l2 += 1
        #elif a >= l2 and a <= x:
        #    l1 -= 1
        #    l2 -= 1
        if l1 * r2 < a*b and l2 * r1 < a*b:
            lo = x
        else:
            hi = x - 1
    print(lo)


Measured memory_bytes: 3064
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>

using namespace std;

int main(void)
{	

	
	for (;;)
	{
		int num, val[1000];
		cin >> num;
		double sum = 0, ave = 0;

		if (num == 0)
			break;


		for (int i = 0; i < num; i++)
		{
			cin >> val[i];
			ave += val[i];
		}

		ave = ave/num;

		for (int i = 0; i < num; i++)
		{
			sum += ((double)val[i] - ave) * ((double)val[i] - ave);
		}

		cout << sqrt(sum /num) << endl;


	}
	

}

Measured memory_bytes: 1236
#include <iostream>
#include <string>
#include <vector>


using namespace std;


int main()
{
    string s;
    cin >> s;
    
    
    vector<int> ans;
    int len = s.length();
    for( int i = 0; i < len; ++i ){
        switch( s.c_str()[i] ){
            case '0': ans.push_back(0); break; 
            case '1': ans.push_back(1); break;
            case 'B': if( !ans.empty() ) ans.pop_back(); break;
        }
    }
    
    
    for( auto itr = ans.begin(); itr != ans.end(); ++itr ){
        cout << *itr;
    }
    cout << endl;
    
    
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String t = sc.next();
        for (int i = 0 ; i < s.length() ; i++) {
            String u = s.substring(i, s.length()) + s.substring(0, i);
            if (u.equals(t)) {
                System.out.println("Yes");
                return;
            }
        }
        System.out.println("No");
    }

}


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

using namespace std;

int N;
vector<vector<int>> edges;

void dfs(vector<int> &D, int v) {
  for (auto e : edges[v]) {
    if (D[e] != -1) continue;
    D[e] = D[v] + 1;
    dfs(D, e);
  }
}

int main() {
  cin >> N;
  edges.assign(N, vector<int>());
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vector<int> F(N, -1), S(N, -1);
  F[0] = 0;
  S[N - 1] = 0;
  dfs(F, 0);
  dfs(S, N - 1);
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (F[i] > S[i]) cnt++;
  }
  if (cnt >= N - cnt) {
    cout << "Snuke" << endl;
  } else {
    cout << "Fennec" << endl;
  }

  return 0;
}
/* vim:set fdm=marker: */


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

int main() {
    int D; cin >> D; D = 25 - D;
    cout << "Christmas";
    rep(i, D) cout << " Eve";
    cout << endl;

    return 0;
}

Measured memory_bytes: 3568
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<queue>
#include<numeric>
#include<string>
#define all(x) (x).begin(),(x).end()
typedef long long ll;
const ll MOD = 1000000007;
 
using namespace std;

int main()
{
    ll N, K;   cin >> N >> K;
    vector<ll> p(N), c(N), cycle(N);
    vector<int> rot(N);
    ll m[N][min(N,K)];
    for(int i=0; i<N; i++)  cin >> p[i];
    for(int i=0; i<N; i++)  cin >> c[i];

    ll tmp, ans;
    int now, r;
    ans = -MOD;
    for(int i=0; i<N; i++){
        now = i;
        tmp = 0;
        for(int j=0; j<min(K,N); j++){
            now = p[now] - 1;
            tmp += c[now];
            r = j;
            m[i][j] = tmp;
            if(now == i)    break;
        }
        cycle[i] = tmp;
        rot[i] = r+1;
    }
    ans = -MOD;
    for(int i=0; i<N; i++){
        for(int j=0; j<rot[i]; j++){
            tmp = m[i][j];
            if(cycle[i] > 0 && K-j-1>0)    tmp += (K-j-1)/rot[i]*cycle[i];
            ans = max(ans,tmp);
        }
    }
    cout << ans;
}

Measured memory_bytes: 199072
N, C, K = map(int, input().split())
T = []
for i in range(N):
    T.append(int(input()))

T.sort()
i = 0
start = 0
ans = 0
limit = C
for i in range(N - 1):
    if T[start] + K >= T[i + 1] and limit > 1 :
        limit -= 1
    elif T[start] + K < T[i + 1] and limit > 1:
        ans += 1
        limit = C
        start = i + 1
    elif limit == 1:
        ans += 1
        limit = C
        start = i + 1
if start != N:
    ans += 1
print(ans)

Measured memory_bytes: 7388
#include<iostream>
#include<cstdio>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std;

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

	int a,b,c,d,day;

	cin >> day >> a >> b >> c >> d;

	while(1){
		a -= c;
		b -= d;
		day--;
		if(a <= 0 && b <= 0) break;
	}

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

	return 0;
}

Measured memory_bytes: 1192
package main

import (
	"fmt"
)

func main() {
	var a, b, c int
	fmt.Scanf("%d %d %d", &a, &b, &c)

	fmt.Println(logic(a, b, c))
}

func logic(a, b, c int) int {
	if b/a < c {
		return b / a
	}
	return c
}


Measured memory_bytes: 512
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define var(Type, ...)  Type __VA_ARGS__; input(__VA_ARGS__)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
inline i64  sigma(i64 n)            { return (n * (n + 1) >> 1); }
inline i64  updiv(i64 a, i64 b)     { return (a + b - 1) / b; }
inline i64  sqr(i64 n)              { return n * n; }
inline string to_string(char c)     { return string(1, c); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> make_v(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}
template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...));
}

inline void input() {}
template <class Head, class... Tail>
inline void input(Head &head, Tail&... tail) { cin >> head; input(tail...); }

inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}
// }}} End Header

signed main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);

    var(int, N);

    vector<int> index(N + 1);
    vector<int> p(N);

    rep(i, N) {
        input(p[i]);
        index[p[i]] = i;
    }

    multiset<i64> S { -1, -1, N, N };
    sort(all(p), greater<>());

    i64 ans = 0;
    for (const int e : p) {
        const auto cur = S.insert(index[e]);
        const i64 leftR = *prev(cur);
        const i64 leftL = *prev(cur, 2);
        const i64 rightL = *next(cur);
        const i64 rightR = *next(cur, 2);
        const i64 l = e * (leftR - leftL) * (rightL - index[e]);
        const i64 r = e * (rightR - rightL) * (index[e] - leftR);
        ans += l + r;
    }

    print(ans);
    return 0;
}


Measured memory_bytes: 5760
#include <bits/stdc++.h>
#include <time.h>
#include <stdlib.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ull unsigned long long
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define clr(x) x.clear()
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vi>
#define mp make_pair
#define hell 1000000007
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(),a.end()
#define ign cin.ignore();

#define o_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define o_setll tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set

using namespace std;
using namespace __gnu_pbds;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int main()
{
    IOS
    int n;
    cin>>n;
    map<int,int> m;
    vi v(n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        m[v[i]]++;
    }
    ll res=0;
    for(auto u:m){
        ll temp=u.second;
        res=res+(temp*(temp-1))/(ll)2;
    }
    for(int i=0;i<n;i++)
    {
        ll temp=m[v[i]];
        ll out=res-(temp*(temp-1))/(ll)2;
        temp--;
        out=out+(temp*(temp-1))/(ll)2;
        cout<<out<<"\n";
    }
    return 0;
}


Measured memory_bytes: 8320
#include<cstdio>
#include<cmath>
using namespace std;
#define N 100005
int f[N], rank[N], n, m;

void init(){
    for(int i=0; i<=n; ++i)
        f[i]=i, rank[i]=0;
}
int find(int x){
    if(x==f[x]) return f[x];
    int t=f[x];
    f[x] = find(f[x]);
    rank[x] += rank[t];
    return f[x];
}
bool Union(int x,int y, int m){
    int a=find(x), b=find(y);
    if(a==b){
        if(rank[x]+m!=rank[y])
            return false;
        return true;
    }
    f[b] = a;
    rank[b] = rank[x]+m-rank[y];
    return true;
}

int main(){
    int a,b,x;
    scanf("%d%d",&n,&m);
        init();
        int cnt=0;
        for(int i=0; i<m; ++i){
            scanf("%d%d%d",&a,&b,&x);
            if(!Union(a, b, x)){
                ++cnt;
            }
        }
        if(cnt>0) printf("No\n");
        else printf("Yes\n");
    
    return 0;
}

Measured memory_bytes: 1024
def submit():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [0] + a

    # k < nの場合は普通にO(n)で実行可能
    if k < n:
        curr = 1
        while k:
            curr = a[curr]
            k -= 1
        print(curr)
        return
            
    # n < kの場合はループするまで回ってみる
    attach = {i: -1 for i in range(1, n + 1)}
    curr = 1
    cnt = 0
    while True:
        if attach[curr] < 0:
            attach[curr] = cnt
            curr = a[curr]
            cnt += 1
        else:
            break
    
    loop_st = curr
    loop_time = cnt - attach[curr]
    
    skip = ((k - attach[loop_st]) // loop_time) * loop_time
    k -= skip

    curr = 1
    while k:
        curr = a[curr]
        k -= 1
    print(curr)
    return
    

submit()

Measured memory_bytes: 40712
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#include <random>
#include <list>
using namespace std;
 
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
 
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define pb push_back
 
int INF = INT_MAX / 10;
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

vector<int> z_algorithm(string s) {
	int N = s.length();
	vector<int> a(N + 1); a[0] = N;
	int j = 0;
	for (int i = 1; i <= N;) {
		for (; i + j < N && s[i + j] == s[j]; j++);
		if (a[i] = j) {
			int k;
			for (k = 1; k + a[k] < a[i]; k++)
				a[i + k] = a[k];
			i += k; j -= k;
		}
		else i++;
	}
	return a;
}

int main() {
	string t; cin >> t;
	string s; cin >> s;
	int N = s.length(), M = t.length();
	string st = s + '$' + t;
	vector<int> a = z_algorithm(st);
	for (int j = 0; j <= M; j++)
		if (a[N + 1 + j] == N)
			printf("%d\n", j);
}

Measured memory_bytes: 10048
/*+lmake
 * STD = c++14
 * DEFINE += WAAUTOMATON
 */
#include <bits/stdc++.h>
using namespace std;
using LL=long long;
#ifdef WAAUTOMATON
#define debug(args...)            {dbg,args; cerr<<endl;}
#else
#define debug(args...)              // Just strip off all debug tokens
#endif

struct debugger
{
	template<typename T> debugger& operator , (const T& v)
	{
		cerr<<v<<" ";
		return *this;
	}
} dbg;
#define MAXN 2000
struct Edge
{
	int to,next;
}e[2*MAXN+10];
int head[MAXN+10];
void addEdge(int a,int b)
{
	static int c=0;
	e[++c]=(Edge){b,head[a]};
	head[a]=c;
	e[++c]=(Edge){a,head[b]};
	head[b]=c;
}
bool b[MAXN+10];
void dfs(int now,int d,int fa)
{
	if (d<0) return;
	b[now]=true;
	for(int i=head[now]; i!=0; i=e[i].next) {
		int v=e[i].to;
		if (v==fa) continue;
		dfs(v,d-1,now);
	}
}
void solve(long long N, long long K, vector<long long> A, vector<long long> B){
	for(int i=0; i<N-1; ++i) {
		addEdge(A[i],B[i]);
	}
	int ans=0;
	if (K%2==0) {
		for(int i=1; i<=N; ++i) {
			memset(b,false,sizeof(b));
			dfs(i,K/2,0);
			int cnt=0;
			for(int j=1; j<=N; ++j) {
				if (b[j]) ++cnt;
			}
			ans=max(ans,cnt);
		}
	} else {
		for(int i=0; i<N-1; ++i) {
			memset(b,false,sizeof(b));
			dfs(A[i],K/2,0);
			dfs(B[i],K/2,0);
			int cnt=0;
			for(int j=1; j<=N; ++j) {
				if (b[j]) ++cnt;
			}
			ans=max(ans,cnt);
			debug(cnt);
		}
	}
	cout<<N-ans<<endl;
}

int main(){
	//freopen("in.txt","r",stdin);
	long long N;
	long long K;
	scanf("%lld",&N);
	vector<long long> B((N-1)-1+1);
	vector<long long> A((N-1)-1+1);
	scanf("%lld",&K);
	for(int i = 0 ; i <= (N-1)-1 ; i++){
		scanf("%lld",&A[i]);
		scanf("%lld",&B[i]);
	}
	solve(N, K, A, B);
	return 0;
}



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

#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define rep2(i, s, n) for(long long i=(s);i<(long long)(n);i++)
#define repi(i, n) for(int i=0;i<(int)(n);i++)
#define rep2i(i, s, n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define chmax(s, t) s=max(s,t);
#define chmin(s, t) s=min(s,t); 

#define deg2rad(deg) (((deg)/360)*2*M_PI)
#define rad2deg(rad) (((rad)/2/M_PI)*360)

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

const ll INF = (1LL<<60);
const int INFi = (1<<29);

/*素数判定*/
bool is_prime(ll n){
    if(n==1) return false;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0) return false;
    }
    return true;
}

/*約数列挙*/
vll enum_divisors(ll n){
    vll l;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            l.push_back(i);
            if(n/i != i) l.push_back(n/i);
        }
    }
   sort(all(l));
   return l;
}

/*素因数分解*/
vector<P> prime_factorize(ll n){
    vector<P> l;
    for(ll i=2;i*i<=n;i++){
        if(n%i!=0) continue;
        ll e = 0;
        while(n%i==0){
            e++;
            n /= i;
        }
        l.push_back({i, e});
    }
    if(n!=1) l.push_back({n, 1});
    return l;
}

/*最小公倍数*/
ll lcm(ll a, ll b){
    return a*b/__gcd(a,b);
}

/*最大公約数*/
ll gcd(ll a, ll b){
    return __gcd(a,b);
}

/*組み合わせ(Combination)*/
const ll CMAX = 1010000;
const ll CMOD = 1e9+7;
ll fac[CMAX], finv[CMAX], inv[CMAX];
// テーブルを作る前処理
void combinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (ll i = 2; i < CMAX; i++) {
		fac[i] = fac[i - 1] * i % CMOD;
		inv[i] = CMOD - inv[CMOD%i] * (CMOD / i) % CMOD;
		finv[i] = finv[i - 1] * inv[i] % CMOD;
	}
}
// 二項係数計算
ll comb(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % CMOD) % CMOD;
}

/*階乗*/
ll factorial(ll n){
    const ll FMOD = 1e9+7;
    ll ret = n;
    for(ll i=n-1;i>0;i--){
        ret*=i;
        ret%=FMOD;
    }
    return (ret?ret:1);
}


int main(){

    ll n, m; cin >> n >> m;
    vll x(n);
    ll in;
    rep(i, n){
        cin >> in;
        x[i] = abs(in-m);
    }

    ll ans = x[0];
    rep2(i, 1, n){
        ans = gcd(ans, x[i]);
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 4036
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
ll n,m[100][4],r,cld[100];
ll w(int c){
	if(c<0)return 1;
	ll l=m[c][0],r=m[c][1],wl=w(m[c][2]),wr=w(m[c][3]),nl=wr*r,nr=wl*l;
	return (wl*nl+wr*nr)/__gcd(nl,nr);
}
int dfs(int c){
	if(c<0)return 0;
	return cld[c]?cld[c]:(cld[c]=dfs(m[c][2])+dfs(m[c][3])+1);
}
int main(){
	while(cin>>n,n){
		rep(i,n)rep(j,4){
			cin>>m[i][j];if(j>1)m[i][j]--;
		}
		fill(cld,cld+n,0);
		rep(i,n)if(dfs(r=i)==n)break;
		cout<<w(r)<<endl;
	}
	return 0;
}

Measured memory_bytes: 0