akhauriyash/Code-Regression huggingface dataset:
/******************************************
* NICK : nikhilsinghh59 *
******************************************/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"

#define PB push_back
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
const int M = 1e9+7;

ll last;

void dfs(vector<ll>adj[] , ll src , vector<ll>&v ,  bool vis[]) {
    vis[src] = true;

    for(auto it : adj[src]) {
        if(vis[it]) {
            last = it;
            return ;
        }
        else {
            v.push_back(it);
            dfs(adj , it , v , vis);
        }
    }
}

int main()
{   
    

    ll n , k;
    cin >> n >> k;

    vector<ll>adj[n+1];
    bool vis[n+1] = {false};

    for(ll i=1;i<=n;i++){
        ll x;
        cin >> x;
        adj[i].push_back(x);
    }

    vector<ll>v;
    v.push_back(1);
    dfs(adj , 1 , v , vis);

    ll ind;

    for(ll i=0;i<(ll)v.size();i++){
        if(v[i] == last) {
            ind = i;
            break;
        }
    }

    if(k <= ind) {
        cout << v[k];
        return 0;
    }
    k-=ind;
    ll sz = (ll)v.size() - ind;
    k = k%sz;

    cout << v[ind + k];
    
    return 0;
}

Measured memory_bytes: 31392
#include <bits/stdc++.h>
        #define endl '\n'
        #define modulo 1000000007
        #define int long long
        //#define __int128 long long
        #pragma GCC optimize("-Ofast")
        #define float double
        #define PI 3.141592653589793238462643383279502884
        #define sinDegrees(x) sin((x) * PI / 180.0)
        #define tanDegrees(x) tan((x) * PI / 180.0)
        #define atanDegrees(x) atan(x)* 180.0 / PI
        #define f first
        #define s second
        using namespace std;
        int power(int x,int y,int m)
        {
        int temp;
        if(y == 0)
        return 1;
        temp = (power(x, y/2,m))%m;
        if (y%2 == 0)
        return ((temp%m)*temp);
        else
        return ((x*temp%m)*temp%m)%m;
        }
        int inv(int x)
        {
        return (power(x,modulo-2,modulo))%modulo;
        }
        bool comp(pair<int,int>a,pair<int,int>b)
        {
            if(a.second==b.second)
            return a.first<b.first;
            return a.second<b.second;
        }
        ///Solution created by:Ahmadsm2005
        int DP[2002],visited[2002];
        int32_t main()
        {
            //freopen("output.txt","w", stdout);
            //freopen("input0.txt","r", stdin);
            //iostream::sync_with_stdio(0);
            cin.tie(0);
            cout.tie(0);
            int n,x,y;
            cin>>n>>x>>y;
            vector<vector<int>>lolz(n);
            for(int i=0;i<n-1;i++){
            lolz[i].push_back(i+1);
            lolz[i+1].push_back(i);
            }
            lolz[x-1].push_back(y-1);
            lolz[y-1].push_back(x-1);
            for(int qq=0;qq<n;qq++){
            priority_queue<pair<int,int>>lol;
            lol.push({0,qq});
            visited[qq]=1;
            while(lol.size()){
            pair<int,int>topper=lol.top();
            lol.pop();
            if(topper.second>qq)
            DP[-topper.first]++;
            if(lolz[topper.second].size())
            for(int i=0;i<lolz[topper.second].size();i++){
            if(!visited[lolz[topper.second][i]]){
            lol.push({topper.first-1,lolz[topper.second][i]});
            visited[lolz[topper.second][i]]=1;
            }
            }
            }
            for(int i=0;i<2002;i++)
            visited[i]=0;
            }
            for(int i=1;i<n;i++)
            cout<<DP[i]<<endl;
            return 0;
        }


Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#define pi 3.14159265358979323846264338327950

int main() {
	int N; std::cin >> N;
	int ans = 0;
	int sum = -1;
	for (int x = 1; x <= N; ++x) {
		int count = 0;
		int i = x;
		while (i % 2 == 0) {
			i = i / 2;
			++count;
		}
		if (count > sum) {
			sum = count;
			ans = x;
		}
	}
	std::cout << ans << std::endl;

	return 0;
}

Measured memory_bytes: 3624
main(require("fs").readFileSync("/dev/stdin", "utf8").replace(/\r?\n$/, "").split(/\r?\n/));

function main(lines){
	var a, i;
	
	a = lines.shift().split(" ").map(Number);
	var n = a[0], m = a[1];

	var aRows = [];
	for(i=0;i<n;i++) aRows.push(lines.shift().split(" ").map(Number));

	var bs = [];
	for(i=0;i<m;i++) bs.push(lines.shift().split(" ").map(Number));

	aRows.forEach(function(r){
		console.log(r.map(function(a,i){return a*bs[i];}).reduce(function(a,b){return a+b;}));
	});
}

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

int main()
{
    int n;
    cin >> n;
    int a[n];

    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = n - 1; i >= 0; i--)
    {
        cout << a[i];
        if (i != 0)
        {
            cout << " ";
        }
    }
    cout << endl;
}


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

int main(){
    ll D;cin>>D;
    vector<int> c(26);rep(i,26)cin>>c[i];
    vector<vector<int>> s(D,vector<int>(26));
    rep(i,D)rep(j,26) cin>>s[i][j];
    rep(i,D){
        int it = max_element(s[i].begin(),s[i].end())-s[i].begin();
        cout<<it+1<<endl;
    }
}


Measured memory_bytes: 3684
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <deque>

#define INF_LL 1e18
#define INF 1e9

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

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

const ll MOD = 1e9+7;

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

template<typename T>
void chmax(T &a, T &b){
	a = max(a, b);
}

template<typename T>
void chmin(T &a, T &b){
	a = min(a, b);
}

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

class RMQ{
	int n;
	vector<int> dat;
public:
	RMQ(int n_){
		n = 1;
		while(n < n_) n *= 2;
		REP(i, n*2-1){
			dat.push_back(INF);
		}
	}

	void update(int x, int i){
		i += n-1;
		dat[i] = x;
		while(i > 0){
			i = (i-1)/2;
			dat[i] = min(dat[i*2+1], dat[i*2+2]);
		}
	}

	int query(int a, int b, int l, int r, int x){
		if(a <= l && r <= b) return dat[x];
		if(r <= a || b <= l) return INF;

		int res = INF;
		res = min(query(a, b, l, (l+r)/2, x*2+1), query(a, b, (l+r)/2, r, x*2+2));
		return res;
	}
	int query(int a, int b){
		return query(a, b, 0, n, 0);
	}
};

const ll mod = 1e9+7;
ll frac[214514] = {};

ll pow(ll a, ll b, ll m){
	if(b == 0) return 1;
	if(b == 1) return a%m;
	ll res = 1;
	if(b % 2) res = (res*a)%m;
	ll calc = pow(a, b/2, m);
	res = (res*calc)%m;
	res = (res*calc)%m;
	return res;
}

ll comb(ll a, ll b){
	ll res = frac[a]*pow(frac[b], mod-2, mod)%mod;
	res = (res*pow(frac[a-b], mod-2, mod))%mod;
	return res;
}

int main(void){
	ll H, W, A, B;
	cin >> H >> W >> A >> B; H--; W--;
	frac[0] = 1;
	FOR(i, 1, 214514){
		frac[i] = (frac[i-1] * i)%mod;
	}
	if(A == 0 || B == 0){
		cout << comb(H+W, H) << endl;
		return 0;
	}
	ll res = 0;
	for(int w = B;w <= W;w++){
		res = (res + comb(w+H-A, w)*comb(A+W-w-1, A-1))%mod;
	}
	cout << res << endl;
	return 0;
}


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

using namespace std;

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

#define lp(i, a, b) for (int i=a; i<(b); i++)
#define lpd(i,a,b) for (int i = (b)-1; i >= a; i--)
	
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

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

vector<ll> uf;

int find_set(int v){
    if (uf[v] < 0) return v;
    return uf[v] = find_set(uf[v]);
}

ll size(int v){
    v = find_set(v);
    return -uf[v];
}

void union_sets(int a, int b){
    a = find_set(a);
    b = find_set(b);
    if (a == b) return;
    if (size(a) < size(b))
        swap(a, b);
    uf[a] += uf[b];
    uf[b] = a;
}

int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin>>n>>m;
    uf.assign(n+1, -1);
    vector<pi> v(m);
    vector<ll> ans;

    int a, b;
    lp(i, 0, m){
        cin>>a>>b;
        v[i] = mp(a, b);
    }

    ll res = (ll)n*(n-1)/2;
    lpd(i, 0, m){
        ans.pb(res);
        int x = find_set(v[i].first);
        int y = find_set(v[i].second);
        if (x != y){
            res -= (size(x)*size(y));
            union_sets(x, y);
        }
    }

    lpd(i, 0, m) cout<<ans[i]<<endl;

    return 0;
}

Measured memory_bytes: 3828
n = gets.chomp.to_i
as =  gets.chomp.split.map(&:to_i)

as_length = as.length

array = (1..n).to_a.sort

if array == as
  p 0
  exit
end

counter = 0
(0..n).each do |i|
  finder = as.find_index(i+1)
  if i == 0 && finder == nil
    p -1
    exit
  elsif finder == nil
    p as_length - counter
    exit
  elsif finder + 1 == as_length
    p as_length - counter
    exit
  elsif 
    as = as[finder..-1]
    counter = counter + 1
  end
end



Measured memory_bytes: 23600
#include <iostream>
#include <fstream>
#include <sstream>

#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <deque>
#include <string>

#include <algorithm>
#include <numeric>

#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>

#define pb push_back
#define pbk pop_back
#define mp make_pair
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define foreach(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
#define len(a) ((int) (a).size())

#ifdef CUTEBMAING
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

using namespace std;

typedef long long int64;
typedef long double ld;
typedef unsigned long long lint;

const int inf = (1 << 30) - 1;
const int64 linf = (1ll << 62) - 1;

int main() {
#ifdef XCODE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, m, k; cin >> n >> m >> k;
    vector<int> a(k);
    for (int i = 0; i < k; i++) {
    	cin >> a[i];
    }
    vector<pair<int, int>> order;
    for (int i = 0; i < n; i++) {
    	if (i % 2) {
    		for (int j = 0; j < m; j++) {
    			order.pb(mp(i, j));
    		}
    	} else {
    		for (int j = m - 1; j >= 0; j--) {
    			order.pb(mp(i, j));
    		}
    	}
    }
    vector<vector<int>> result(n, vector<int>(m));
    int it = 0;
    for (int i = 0; i < k; i++) {
    	for (int j = 0; j < a[i]; j++) {
    		result[order[it].fs][order[it].sc] = i + 1;
    		it++;
    	}
    }
    for (int i = 0; i < n; i++) {
    	for (int j = 0; j < m; j++) {
    		printf("%d%c", result[i][j], " \n"[j + 1 == m]);
    	}
    }
    return 0;
}


Measured memory_bytes: 512
y500 = int(input())
y100 = int(input())
y50 = int(input())
cost = int(input())

answer = 0

for i in range (y500 + 1) :
  for p in range (y100 + 1) :
    for q in range (y50 + 1) :
      if i * 500 + p * 100 + q * 50 == cost :
        answer += 1

print(answer)

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

int main() {
  ll A, B, V, W;
  double T;
  cin >> A >> V >> B >> W >> T;


  double dis = abs(B - A);
  double s = V - W;
  if (s < 0) {
    cout << "NO" << endl;
  } else {
    double tn = dis / s;
    if (tn <= T) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}


Measured memory_bytes: 3724
n = int(input())
s = ""
for i in range(n):
    i += 1
    if i % 3 == 0:
        s += " " + str(i)
    else:
        x = i
        while True:
            if x % 10 == 3:
                s += " " + str(i)
                break
            elif x == 0:
                break
            x //= 10
print(s)


Measured memory_bytes: 5628
#![allow(unused_imports)]

use proconio::{input, fastout};
use proconio::marker::*;

#[fastout]
fn main() {
    input! {
    n: usize,
    m: usize,
    mut ab: [(Usize1, Usize1); m]
    }
    let mut g = vec![vec![false; n]; n];
    for i in 0..m {
        let (a, b) = ab[i];
        g[a][b] = true;
        g[b][a] = true;
    }
    let mut ans = 0;
    for i in 0..m {
        let (a, b) = ab[i];
        g[a][b] = false;
        g[b][a] = false;
        if !is_connect(&g, n) {
            ans += 1;
        }
        g[a][b] = true;
        g[b][a] = true;
    }
    println!("{}", ans);
}

fn is_connect(g: & Vec<Vec<bool>>, n: usize) -> bool {
    let mut q = vec![0];
    let mut is_visited = vec![false; n];
    while let Some(v) = q.pop() {
        is_visited[v] = true;
        for i in 0..n {
            if g[v][i] && !is_visited[i] {
                q.push(i);
            }
        }
    }
    is_visited.iter().all(|&x| x)
}



Measured memory_bytes: 2196
#include <algorithm>
#include <stdio.h>
#include <deque>
#include <set>
using namespace std;
using ll = long long;
ll next(ll x) {
	ll i = 1;
	for (; i <= x; i *= 2);
	return i;
}
int main() {
	ll n, t, p;
	scanf("%lld", &n);
	multiset<ll> a;
	for (int i = 0; i < n; i++)
		scanf("%lld", &t), a.emplace(t);

	ll c = 0;
	while (!a.empty()) {
		t = *(--a.end());
		p = next(t) - t;
		a.erase(--a.end());

		auto lower = a.lower_bound(p);
		if (lower != a.end() && *lower == p)
			c++, a.erase(lower);
	}
	printf("%lld\n", c);
}

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

int main() { 
  int N;
  string S;
  cin >> N >> S;
  
  int count =0;
  
  for(int i=0; i<N-2; i++){
    if(S[i]=='A' && S[i+1]=='B' && S[i+2]=='C') count ++;
  }
  
  cout << count << endl;
    
  
}

Measured memory_bytes: 3560
#include<bits/stdc++.h>
#define ll long long int
#define pi pair<int,int>
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define dbg(x) cout<<#x<<" "<<x<<endl
#define clr(x,v) memset(x, v, sizeof(x))
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;

void cp()
{
    ll n;
    cin >> n;
    vector<int> fac;
    if(n % 2 == 0)
    {
        int cnt = 0;
        while(n % 2 == 0)
            cnt++, n = n / 2;
        fac.pb(cnt);
    }
    for(ll i = 3; i <= sqrt(n); i += 2)
    {
        if(n % i == 0)
        {
            int cnt = 0;
            while(n % i == 0)
                cnt++, n /= i;
            fac.pb(cnt);
        }
    }
    if(n > 1)
        fac.pb(1);
    ll ans = 0;
    for(int x: fac)
    {
    	int res = (-1 + (int)sqrt(1+8*x))/2;
    	ans += res;
    }
    cout << ans;
}

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

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


int main(){
    /*A
    ll a,b;
    cin>>a>>b;
    if(a%2!=b%2)
    {
        cout<<"IMPOSSIBLE"<<endl;
    }
    else
    {
        cout<<(a+b)/2<<endl;
    }
    */
    
    int n;
    cin>>n;
    vector <int> a(n);
    int count = 0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]!=i+1)
        {
            count++;
        }
    }

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

Measured memory_bytes: 3624
#include "bits/stdc++.h"
#include <string>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define int ll
//#define slld(t) scanf("%lld",&t)
//#define sd(t) scanf("%d",&t)
//#define pd(t) printf("%d\n",t)
//#define plld(t) printf("%lld\n",t)
#define vec vector < int >
#define vecp vector < pair < int , int > >
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define debug(x)   cerr<<#x<<": "<<x<<endl
#define pii pair < int , int >
#define pll pair < ll , ll > 
#define debug(x)   cerr<<#x<<": "<<x<<endl
// #define mod  1000000007
#define N 100005
// #define MAX 200005
#define mod1 1000000007
#define mod2 998244353
#define bitcnt __builtin_popcount
#define PI acos(-1)
//#define endl "\n"
// const int inf = (int)1e18 + 10;
const int mod = 1e9 + 7;

const int inf = 1e15;
const int MAX = 1000000;

int a[100005] , b[100005];
vec v[100005];
bool cmp(pii a , pii b){
  if(a.ff == b.ff)
    return (a.ss < b.ss);
  else return(a.ff > b.ff);
}
signed main(){
  fast;

 int n , m , maxa = 0;
   cin >> n >> m;
   for(int i = 0 ; i < n ; i++){
      cin >> a[i] >> b[i] ;
      v[a[i]].pb(b[i]);
      maxa = max(a[i] , maxa);

   }

    int prevj = 0 , ans = 0;
    priority_queue < int > pq;
   for(int i = 1 ; i <=m ; i++){
       for(int j = prevj + 1 ; j<= i ; j++){
            for(int k = 0 ; k < v[j].size() ; k++ )
                pq.push(v[j][k]);
       }
       if(pq.size() > 0){
       ans += pq.top();
       // cout << pq.top() << endl;
       pq.pop();
     }
     prevj = i;
   }

   cout << ans << endl;








// 4  1
// 3  2
// 3  1
// 2  1
// 1  2









}


Measured memory_bytes: 7296
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define valmax 100000
#define wmax 10000000000
ll bigpow(ll base,ll pw,ll M)
{
	if(pw==0) return 1;
	if(pw%2==0)
	{
		ll ret=bigpow(base,pw/2,M);
		return ((ret%M)*(ret%M))%M;
	}
	else return ((base%M)*(bigpow(base,pw-1,M)))%M;

}
ll maxi(ll a,ll b)
{
    if(a>b)return a;
    return b;
}
ll mini(ll a,ll b)
{
    if(a<b)return a;
    return b;
}
ll abse(ll a)
{
    if(a<0)return -a;
    return a;
}
ll ceili(ll a,ll div)
{
    return (a+div-1)/div;
}
int val[101];
int w[101];
ll ks[valmax+1][101]={0};
int N,W;
ll dp(int sval,int i)
{
    if(sval<=0)return 0;
    if(i==N) return wmax;
    if(ks[sval][i])return ks[sval][i];
    return ks[sval][i]=mini(dp(sval,i+1),w[i]+dp(sval-val[i],i+1));
}
int main(void)
{
    io;
    int T=1,cas=0;
    //cin>>T;
    while(T--)
    {
        cin>>N>>W;
        int i;
        ll j;
        for(i=0;i<N;i++)
        {
            cin>>w[i]>>val[i];
        }
        for(i=valmax;i>=0;i--)
        {
            j=dp(i,0);
            if(j<=W)
            {
               break;
            }
        }
        cout<<i;
    }
}



Measured memory_bytes: 79232
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[200005], b[200005], vis[200005], pos = 1, p;
ll k;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	while (!vis[pos])
	{
		b[p] = pos;
		vis[pos]++;
		pos = a[pos];
		p++;
	}
	for (int i = 0; i < p; i++)
	{
		if (b[i] == pos)
		{
			if (k > i)
			{
				cout << b[(int)((k - i) % (ll)(p - i)) + i] << endl;
			}
			else
			{
				cout << b[k] << endl;
			}
			return 0;
		}
	}
}


Measured memory_bytes: 5964
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int , int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<(int)(n);i++)
#define rep2(i,n) for(int i=2;i<(int)(n);i++)
#define repk(i,k,n) for(int i=k;i<(int)(n);i++)
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
//const int64 INF = 1LL << 58;
#define dame { puts("-1"); return 0;}
#define YES { cout << "YES" << endl; return 0; }
#define NO { cout << "NO" << endl; return 0; }
#define Yes { cout << "Yes" << endl; return 0; }
#define No { cout << "No" << endl; return 0; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

vl a={1,2,3,4,5,6,7,8,9};


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

    int x,y;
    cin >> x >> y;
    cout << x + y/2 << endl;

    return 0;
}
    

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i,a,n) for(ll i =(a);i<(n);++i)
#define urep(i,a,n) for(ll i = (a);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define INF 1e18
const int mod = 1e9 + 7;
typedef long long ll;
using namespace std;
ll dx[4] = { 1,-1,0,0 };
ll dy[4] = { 0,0,1,-1 };
ll N,M,X,Y,A,B,C,Q;
string S,T;
ll ans;
ll x[1000000];
ll y[1000000];
ll a[1000000];
ll b[1000000];
ll c[1000000];
ll gcd(ll a, ll b) {
	if (b == 0)return a;
	else return gcd(b, a%b);
}
int main() {
	cin>>N>>S;
	ll left=0;
	ll right=0;
	ll leftadd=0;
	ll rightadd=0;
	urep(i,N-1,0){
		if(S[i]=='('){
			right++;
			left--;
			if(left<0){
				rightadd++;
				left=0;
			}
		}else{
			left++;
		}
	}
	right=0;
	left=0;
	rep(i,0,N){
		if(S[i]=='('){
			right++;
		}else{
			left++;
			right--;
			if(right<0){
				leftadd++;
				right=0;
			}
		}
	}
	rep(i,0,leftadd){
		cout<<"(";
	}
	cout<<S;
	rep(i,0,rightadd){
		cout<<")";
	}
	cout<<endl;
	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>


typedef struct {
    char name[11];
    int time;
} Queue;


int main()
{
    int n, q;
    scanf("%d%d", &n, &q);

    int size = 2 * n; 
    Queue Queue[size];
    int pre = 0, rear = 0, cnt = 0; 
    for( rear = 0; rear < n; ++rear )
        scanf("%s %d", Queue[rear].name, &Queue[rear].time);


    
    while( pre != rear ){
        if( Queue[pre].time <= q ){ 
            cnt += Queue[pre].time;
            printf("%s %d\n", Queue[pre].name, cnt);
        } else {
            cnt += q;
            Queue[pre].time -= q;
            Queue[rear] = Queue[pre]; 
            rear = (rear + 1) % size;
        }
        
        pre = (pre + 1) % size;
    }


    return 0;
}


Measured memory_bytes: 3296
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll<<60;

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 (a > b) { a = b; return 1; } return 0; }

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

ll A, B;

int main()
{
  std::cin >> A >> B;

  std::cout << std::max({A+B, A-B, A*B}) << std::endl;

  return 0;
}

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

using namespace std;

int main() {
  
  string S;
  
  cin >> S;
  
  if(S=="ABC") {
    
    cout << "ARC" << endl;
    
  } else {
    
    cout << "ABC" << endl;
    
  }
  
  return 0;
  
}

Measured memory_bytes: 3588
import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		ArrayList<Integer>[] peo=new ArrayList[n];
		for(int i=0; i<n; i++){
			peo[i]=new ArrayList<>();
			int a=sc.nextInt();
			for(int j=0; j<a; j++){
				int x=sc.nextInt()-1;
				int y=sc.nextInt();
				peo[i].add(x);
				peo[i].add(y);
			}
		}
		int ans=0;
		for(int i=1; i<(1<<n); i++){
			int c=i;
			int index=0;
			int count=0;
			Boolean[] px=new Boolean[n];
			while(index!=n){
				if((c & 1)==1){
					if(px[index]!=null && !px[index]){
						count=0;
						break;
					}
					px[index]=true;
					count++;
					for(int j=0; j<peo[index].size(); j+=2){
						Boolean kp=false;
						if(peo[index].get(j+1)==1){
							kp=true;
						}else{
							kp=false;
						}
						if(px[peo[index].get(j)]!=null && px[peo[index].get(j)]!=kp){
							count=0;
							break;
						}
						px[peo[index].get(j)]=kp;
					}
					if(count==0){
						break;
					}
				}else{
					if(px[index]!=null && px[index]){
						count=0;
						break;
					}
					px[index]=false;
				}
				index++;
				c>>=1;
			}
			ans=Math.max(ans,count);
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 26684
s=input()
ans=set()
def two(k):
	 qq="0"*(9-len(bin(k)[2:]))+bin(k)[2:]
	 return qq

for i in range(1,2**9):
	p=""
	for j in range(9):
		if two(i)[j]=="1":
			p+="AKIHABARA"[j]
		if "KI" in p and "H" in p and "B" in p and "R" in p:
			ans.add(p)
if s in ans:
	print("YES")
else:
	print("NO")

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

#define int long long
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rep1(i,n) for(int i=1;i<=(int)(n);++i)
#define rep11(i,n) for(int i=1;i<(int)(n);++i)
#define repo(i,o,n) for(int i=o;i<(int)(n);++i)
#define repm(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

string s,t;

signed main() {
    cin >> s >> t;
    cout << t << s << endl;
}



Measured memory_bytes: 256
/*..........AAKASH GOYAL............*/
/*............DAIICT................*/

#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>
using namespace std;

#define    int                      long long int

#define    f(i,j,n)                 for(int i = j; i <= n; i++)
#define    r(i,n,j)                 for(int i = n; i >= j; i--)
#define    mod                      1000000007
#define    speed                    ios_base::sync_with_stdio(false);
#define    upp                      cin.tie(NULL);
#define    pb                       push_back
#define    mp                       make_pair
#define    T                        int tt; cin >> tt; while(tt--)
#define    ff                       first
#define    ss                       second
#define    all(container)           container.begin() , container.end()
#define    sz(container)            (int)container.size()
#define    pii                      pair <int , int>
#define    ppl                      pair <int , pii>               
#define    sp(x)                    setprecision(x)


int32_t main()
{
    speed upp
    int n,k;
    cin>>n>>k;
    
    long double y,b[n+1],x,mm=0.00,sum=0.00;
    f(i,1,n)
    {
        cin>>x;
        y=((long double)(x+1))/(long double)(2.00 );
        b[i]=y;
    }
    f(i,1,k)
        sum+=b[i];
    mm=sum;
    f(i,k+1,n)
    {
        sum=sum-b[i-k] + b[i];
        if(sum>mm)
            mm=sum;
    }
    cout<<fixed<<sp(10)<<mm;
}

Measured memory_bytes: 3456
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
s = [0]*(n+1)
mod = 10**9+7
aa = set(a)

ste = [0] *(n+1)
ste[0] = 1
if 1 not in aa:
    ste[1] = 1
else:
    ste[1] = 0
for i in range(2,n+1):
    if i in aa:
        continue
    ste[i] = (ste[i-1]+ste[i-2])%mod

print(ste[n])


Measured memory_bytes: 13960
import sys
input = sys.stdin.readline

N = int(input())
train = [[int(_) for _ in input().split()] for _ in range(N-1)]

for i in range(N-1):
    ans = 0
    for j in range(i, N-1):
        if ans <= train[j][1]:
            ans = train[j][1]
        elif ans%train[j][2] != 0:
            ans += train[j][2] - (ans%train[j][2])
        
        ans += train[j][0]
    print(ans)

print(0)

Measured memory_bytes: 3188
#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,abm,fma,popcnt,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 186, L = 16;

int n, m;
string grid[N];
int pref[N][N];
unsigned char dp[N][N][N][L];
const unsigned char NFILL = 255, BAD = 252;

void init() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + (grid[i-1][j-1] == '#');
    }
  }
  memset(dp, NFILL, sizeof dp);
}

inline bool mono(int b, int t, int l, int r) {
  int area = pref[t+1][r+1] - pref[b][r+1] - pref[t+1][l] + pref[b][l];
  return area == 0 || area == (r-l+1)*(t-b+1);
}

int solve(int b, int t, int l, int k) {
  if (l >= BAD) return -2;
  if (l < 0) return -2;
  if (l >= m) return m-1;
  if (dp[b][t][l][k] != NFILL) return dp[b][t][l][k] >= BAD ? -1 : dp[b][t][l][k];
  if (k == 0) {
    int lb = l, rb = m-1, ans = -1;
    while (lb <= rb) {
      int mid = (lb+rb)/2;
      if (mono(b, t, l, mid)) {
        ans = mid;
        lb = mid+1;
      } else rb = mid-1;
    }
    dp[b][t][l][k] = (ans < 0 ? BAD : ans);
    return ans;
  }
  int ans = solve(b, t, solve(b, t, l, k-1)+1, k-1);
  if (b < t) {
    int lb = b, rb = t-1, bsplit = t-1;
    while (lb <= rb) {
      int mid = (lb+rb)/2;
      //ans = max(ans, min(solve(b, mid, l, k-1), solve(mid+1, t, l, k-1)));
      if (solve(b, mid, l, k-1) >= solve(mid+1, t, l, k-1)) {
        bsplit = mid;
        lb = mid+1;
      } else rb = mid-1;
    }
    for (int i = max(b, bsplit-2); i <= min(t-1, bsplit+2); i++) {
      ans = max(ans, min(solve(b, i, l, k-1), solve(i+1, t, l, k-1)));
    }
  }
  //cerr << b << " " << t << " " << l << " " << k << " -> " << ans << nl;
  dp[b][t][l][k] = (ans < 0 ? BAD : ans);
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  init();
  for (int k = 0; k < L; k++) {
    if (solve(0, n-1, 0, k) >= m-1) {
      cout << k << nl;
      return 0;
    }
  }
	cout << L << nl;
  return 0;
}



Measured memory_bytes: 100992
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <list>
#include <string>
using namespace std;

int ans = 0;

void dfs(int node, vector<vector<int> > adj, vector<int> visited){
	visited[node] = 1;
	for (int i = 0; i < visited.size(); i++){
		if (adj[node][i] && !visited[i]){
			dfs(i, adj, visited);
		}
	}
	int vsc = 0;
	for (int i = 0; i < visited.size(); i++)
		vsc += visited[i];
	if (vsc == visited.size())
		ans++;
}

int main()
{
	int i = 0, j = 0, p = 0, q = 0;
	ifstream fin("input.txt");
	int n, m, a, b;
	cin >> n >> m;
	vector<vector<int> > adj(n, vector<int>(n));
	vector<int>visited(n);
	for (i = 0; i < m; i++){
		cin >> a >> b;
		a--;
		b--;
		adj[a][b] = 1;
		adj[b][a] = 1;
	}
	dfs(0, adj, visited);
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
def nCr(n,r):
    com = 1
    for i in range (1,r+1):
        com =com*(n+1-i)//i
    return com
N,K = map(int,input().split())
mod = (10**9)+7
for i in range(1,K+1):
    com1 = nCr(N - K + 1, i)
    com2 = nCr(K-1,i-1)
    print((com1*com2)%mod)


Measured memory_bytes: 3316
#include<bits/stdc++.h>
using namespace std;
int check(int n){
	for(int i=1;i<=9;i++){
		for(int j=1;j<=9;j++){
			if(i*j==n)return 1;
		}
	}
	return 0;
}
int main(){
	int n;
	cin>>n;
	if(check(n))cout<<"Yes";
	else cout<<"No";
	return 0;
}

Measured memory_bytes: 3596
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include<random>
#include<math.h>
#include<limits.h>
#include<iterator>
#define rep(i,a) for(int i=0;i<a;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i>=0;i--)
#define ll long long
#define vl vector<ll> 
#define vvl vector<vector<ll> > 
#define vb vector<bool> 
#define vvb vector<vector<bool> > 
#define INF 1123456789012345
using namespace std;

int main(){
	ll maxi = -INF;
	ll mini = INF;
	ll n; cin >> n;
	rep(i, n){
		ll temp; cin >> temp;
		if (temp > maxi) maxi = temp;
		if (temp < mini) mini = temp;
	}cout << (maxi - mini) << endl;
	return 0;
}

Measured memory_bytes: 256
//File Name: code_festival_2017_qualb_b.cpp
  //Created Time: 2017年10月10日 星期二 16时19分02秒
                                   
#include <bits/stdc++.h>
using namespace std;
map<int,int> a,b;

bool solve(){
	for(auto cur:b){
		int x(cur.first),y(cur.second);
		if(a.find(x) == a.end() || a[x] < y) 
			return false;
	}
	return true;
}
int main(){
	int n,m;
	scanf("%d",&n);
	for(int i(0),u;i<n;++i){
		scanf("%d",&u);
		++a[u];
	}
	scanf("%d",&m);
	for(int i(0),u;i<m;++i){
		scanf("%d",&u);
		++b[u];
	}
	if(solve()) puts("YES");
	else puts("NO");
	return 0;
}


Measured memory_bytes: 18944
// Java implementation of Kosaraju's algorithm to print all SCCs
import java.io.*;
import java.util.*;
import java.util.LinkedList;

// This class represents a directed graph using adjacency list
// representation
public class Main
{ // Driver method
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt(),k=in.nextInt();
        int arr[]=new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=in.nextInt();
        }
        if(k>=n){
            System.out.println(1);
        }else{
            int ans=1;
            int rem=n-k;
            int x=(int)Math.ceil((n-k)/((k-1)*1.0));
            System.out.println(x+ans);
        }
    }
}

Measured memory_bytes: 50536
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  int dp[n][3];
  int a,b,c;
  for(int i=0;i<n;i++)
  {
    cin>>a>>b>>c;
    if(i==0)
    {
      dp[0][0]=a;
      dp[0][1]=b;
      dp[0][2]=c;
    }
    else
    {
      dp[i][0] = a + max(dp[i-1][1],dp[i-1][2]);
      dp[i][1] = b + max(dp[i-1][0],dp[i-1][2]);
      dp[i][2] = c + max(dp[i-1][0],dp[i-1][1]);
    }
  }
  int ans = max({dp[n-1][0],dp[n-1][1],dp[n-1][2]});
  cout<<ans;
}


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

int main() {
  int N,A;
  int ans = 100000000000;
  int Even = 0;
  cin >> N ;
  for (int i = 0;i < N ; i++){
    cin >> A;
  while(true){
    if (A == 0){
      break;
    }
    else{
      
     if (A % 2 == 0){
      Even++;
      A /= 2;
     }
     else {
       if (ans >= Even){
         ans = Even ;  
          Even = 0;
         break;
       }    
       else {
          Even = 0;
         break;
       }
     }
      
    }
   }
    
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1005;
const int M = 7005;
int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> vx, vy;
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
bool ma[M][M], vis[M][M];
int get_x(int x) {
    return lower_bound(vx.begin(), vx.end(), x) - vx.begin();
}
int get_y(int x) {
    return lower_bound(vy.begin(), vy.end(), x) - vy.begin();
}
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    vx.push_back(-1.1e9), vx.push_back(0);
    vy.push_back(-1.1e9), vy.push_back(0);
    for(int i = 0; i < n; i++) {
        scanf("%d%d%d", &a[i], &b[i], &c[i]);
        vx.push_back(a[i]), vx.push_back(b[i]);
        vy.push_back(c[i]);
    }
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d", &d[i], &e[i], &f[i]);
        vy.push_back(e[i]), vy.push_back(f[i]);
        vx.push_back(d[i]);
    }
    sort(vx.begin(), vx.end()), vx.erase(unique(vx.begin(), vx.end()), vx.end());
    sort(vy.begin(), vy.end()), vy.erase(unique(vy.begin(), vy.end()), vy.end());
    for(int i = 0; i < n; i++) {
        a[i] = get_x(a[i]), b[i] = get_x(b[i]), c[i] = get_y(c[i]);
        for(int j = a[i] * 2 - 1; j < 2 * b[i]; j++) {
            ma[j][2 * c[i] - 1] = true;
        }
    }
    for(int i = 0; i < m; i++) {
        d[i] = get_x(d[i]), e[i] = get_y(e[i]), f[i] = get_y(f[i]);
        for(int j = 2 * e[i] - 1; j < 2 * f[i]; j++) {
            ma[2 * d[i] - 1][j] = true;
        }
    }
    int sx = get_x(0) * 2 - 1, sy = get_y(0) * 2 - 1;
    bool flag = false;
    ll ans = 0;
    queue<pair<int, int>> qu;
    qu.push({sx, sy}), vis[sx][sy] = true;
    while(!qu.empty()) {
        int ux = qu.front().first, uy = qu.front().second;
        qu.pop();
        if(ux == M - 1 || uy == M - 1 || ux == 0 || uy == 0) {
            flag = true;
            break;
        }
        if(ux % 2 == 0 && uy % 2 == 0) {
            int x = ux / 2, y = uy / 2;
            ans += 1LL * (vx[x + 1] - vx[x]) * (vy[y + 1] - vy[y]);
        }
        for(int i = 0 ; i < 4; i++) {
            int xx = ux + dx[i], yy = uy + dy[i];
            if(xx >= M || xx < 0 || yy >= M || yy < 0 || vis[xx][yy] || ma[xx][yy]) continue;
            qu.push({xx, yy}), vis[xx][yy] = true;
        }
    }
    if(flag) printf("INF\n");
    else printf("%lld\n", ans);
    return 0;
}

Measured memory_bytes: 89036
from collections import defaultdict
from operator import add, mul
import sys
input=sys.stdin.readline


class Reroot:
    def __init__(self, N, M):
        self.G = defaultdict(list)
        self.parents = [-1]*(N+1)
        self.dp = defaultdict(lambda: defaultdict(int))
        self.ans = [0]*(N+1)
        self.merge = lambda x,y:mul(x, y)
        self.add_root = lambda x:x#+1
        self.e = 1
        self.M = M

    def add_edge(self, s, t):
        self.G[s].append(t)

    def build(self):
        self.dfs1(1)
        self.dfs2(1)
        return self.ans

    def dfs1(self, s):
        l = []
        q = [1]
        while q:
            s = q.pop()
            l.append(s)
            for t in self.G[s]:
                if t == self.parents[s]:
                    continue
                self.parents[t] = s
                q.append(t)

        for s in l[::-1]:
            v = self.e
            for t in self.G[s]:
                if t == self.parents[s]:
                    continue
                v = self.merge(v, self.dp[s][t]+1)
            self.dp[self.parents[s]][s] = self.add_root(v)

    def dfs2(self, s):
        q = [(s, self.e)]
        while q:
            s, dp_p = q.pop()
            par = self.parents[s]
            for t in self.G[s]:
                if t == par:
                    self.dp[s][t] = dp_p

            size = len(self.G[s])
            ac_l = [self.e] * (size+1)
            ac_r = [self.e] * (size+1)
            for i, t in enumerate(self.G[s]):
                ac_l[i+1] = self.merge(ac_l[i], self.dp[s][t]+1)%self.M
            for i, t in enumerate(self.G[s][::-1]):
                ac_r[size-i-1] = self.merge(ac_r[size-i], self.dp[s][t]+1)%self.M
            self.ans[s] = self.add_root(ac_l[size])
            for i, t in enumerate(self.G[s]):
                if t == par:
                    continue
                q.append((t, self.add_root(self.merge(ac_l[i], ac_r[i+1])%self.M)))


def solve():
    N, M = map(int, input().split())
    tree = Reroot(N, M)
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
        tree.add_edge(b, a)
    ans = tree.build()
    for i in range(1, N+1):
        print(ans[i])


if __name__ == "__main__":
    solve()


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

#define rep(l, r) for (int i = (l); i < (r); i++)

typedef long long ll;
 
using namespace std;

void solve() {
    int n;
    cin >> n;

    int ans;
    string is;
    rep(n, 1000) {
        is = to_string(i);
        if (is[0] == is[1] && is[1] == is[2]) {
            ans = i;
            break;
        }
    }

    cout << ans << endl;
}

int main() {
    solve();

    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct P{
  int x,c;
  P(int x,int c):x(x),c(c){}
};
int n;
vector<P> s[155];
int dp[155][11][77];
int solve(int y,int i,int m){
  if(y >= n) return 0;
  if(m && y+1 >= n) return 0;
  if(dp[y][i][m] >= 0) return dp[y][i][m];

  int ret = 1 << 30;
  for(int j = 0; j < (int)s[y+1].size(); j++){
    ret = min(ret,solve(y+1,j,m) + ((y)?(s[y][i].c + s[y+1][j].c) * abs(s[y][i].x - s[y+1][j].x):0));
  }
  if(m){
    for(int j = 0; j < (int)s[y+2].size(); j++){
      ret = min(ret,solve(y+2,j,m-1) + ((y)?(s[y][i].c + s[y+2][j].c) * abs(s[y][i].x - s[y+2][j].x):0));
    }
  }
  return dp[y][i][m] = ret;
}
int main(void){
  while(1){
    int m;
    scanf("%d%d",&n,&m); if(!n) break;
    memset(dp,-1,sizeof(dp));
    for(int i = 1; i <= n; i++){
      s[i].clear();
      int k;
      scanf("%d",&k);
      for(int j = 0; j < k; j++){
	int x,d;
	scanf("%d%d",&x,&d);
	s[i].push_back(P(x,d));
      }
    }
    s[0].push_back(P(0,0));
    /*
    for(int i = 0; i < (int)s[0].size(); i++){
      ret = min(ret,solve(0,i,m));
    }
    */
    printf("%d\n",solve(0,0,m));
    //break;
  }
}

Measured memory_bytes: 1588
#include <iostream>
#include <cstdlib>

using namespace std;

int N;
const int MAX_N = 100000;
int A[MAX_N];

void solve() {
	int result = 1;
	int isIncreased = 0;

	for (int i = 1; i < N - 1; i++) {
		if (isIncreased == 0) {
			if (A[i] > A[i - 1]) {
				isIncreased = 1;
			}
			else if (A[i] < A[i - 1]) {
				isIncreased = 2;
			}
		}
			if (isIncreased == 1 && A[i + 1] < A[i]) {
				result++;
				i++;
				isIncreased = 0;
			}
			if (isIncreased == 2 && A[i + 1] > A[i]) {
				result++;
				i++;
				isIncreased = 0;
			}
		}

	cout << result << endl;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	solve();
    return 0;
}

Measured memory_bytes: 640
graph=[[False for _ in range(8)] for _ in range(8)]
visited=[False for _ in range(8)]

def dfs(v,N,visited):
    all_visited=True
    for i in range(N):
        if(visited[i]==False):
            all_visited=False

    if(all_visited):
        return 1

    ret=0

    for i in range(N):
        if(graph[v][i] == False):
            continue
        if(visited[i]):
            continue
        visited[i]=True
        ret = ret + dfs(i,N,visited)
        visited[i]=False
    return ret


def main():
    N,M=map(int,input().split())

    for _ in range(M):
        A,B=map(int,input().split())
        graph[A-1][B-1]=True
        graph[B -1][A-1]=True

    
    visited[0]= True
    print(dfs(0,N,visited))

if __name__ == "__main__":
    main()



Measured memory_bytes: 3064
a,b,c,k = map(int,input().split())
sum1 = 0
while(k>0):
    if(k>=a and a!=0):
        k = k-a
        sum1+=a
        a=0
    elif(k<a):
        a = a - k
        sum1 += k
        k = k-k
    
    elif(k>=b and b!=0):
        k = k-b
        b=0
    elif(k<b):
        b = b - k
        k = k-k
    
    elif(k>=c and c!=0):
        k = k-c
        sum1 = sum1 - c
        c=0
    elif(k<c):
        c  = c-k
        sum1 = sum1 - k
        k = k-k

print(sum1)

Measured memory_bytes: 9152
#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("O3")

#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstring>
#include <numeric>
#include <set>
#include <queue>
#include <deque>
#include <array>
#include <string>
#include <cmath>
#include <random>
#include <ctime>
#include <map>
#include <functional>
#include <bitset>
#include <complex>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back

using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using ld = long double;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector<ld> p(n);
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	vector<ld> dp(n + 1, 0);
	dp[0] = 1;
	for (int i = 0; i < n; i++) {
		vector<ld> dp_new(n + 1, 0);
		for (int j = 0; j <= i + 1; j++) {
			dp_new[j] = (j > 0 ? p[i] * dp[j - 1] : 0) + (1 - p[i]) * dp[j];
		}
		dp = dp_new;
	}
	ld ans = 0;
	for (int i = (n + 1) / 2; i <= n; i++) {
		ans += dp[i];
	}
	cout.precision(10);
	cout << fixed << ans;
}


Measured memory_bytes: 384
/*
corner case (aaza, 26), (aaaa, 10000000)
*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

string s;
int k;

int main(){
  cin >> s >> k;
  int n = s.size();
  rep(i, n){
    if(s.at(i) == 'a') continue;
    int c = s.at(i) - 'a';
    if(k >= (26 - c)){
      k -= (26 - c);
      s.at(i) = 'a';
    }
  }
  k %= 26;
  s.at(n - 1) += k;
  cout << s << endl;
  return 0;
}

Measured memory_bytes: 512