akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
int x,sx,sy,g[3][3],dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
struct node{
	int v[3][3],x,y,step;
};
map<string,int> ht;
int judge(node a){
	string s="";
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			x=a.v[i][j];
			s+=x+'0';
		}
	}
	if(!ht[s]){
		ht[s]=1;
		return 1;
	}
	else return 0;
}
int check(node a){
	int cnt=1,f=1;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			if(a.v[i][j]==cnt){
				cnt++;
				if(cnt==9) cnt=0;
			}
			else f=0;
		}
	}
	return f;
}
void bfs(){
	queue<node> q;
	node tmp,tt;
	tmp.x=sx,tmp.y=sy,tmp.step=0;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			tmp.v[i][j]=g[i][j];
		}
	}
	judge(tmp);
	q.push(tmp);
	while(!q.empty()){
		tt=q.front();
		q.pop();
		if(check(tt)) {
			printf("%d\n",tt.step);
			break;
		}
		for(int i=0;i<4;i++){
			int tx=tt.x+dir[i][0];
			int ty=tt.y+dir[i][1];
			if(tx>=0&&tx<3&&ty>=0&&ty<3){
				tmp.x=tx,tmp.y=ty,tmp.step=tt.step+1;
				for(int i=0;i<3;i++){
					for(int j=0;j<3;j++){
						tmp.v[i][j]=tt.v[i][j];
					}
				}
				tmp.v[tt.x][tt.y]=tt.v[tx][ty];
				tmp.v[tx][ty]=0;
				if(judge(tmp)){
					q.push(tmp);
				}
			}
		}
	} 
}
int main()
{
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			scanf("%d",&x);
			if(x==0) {
				sx=i,sy=j;
			}
			g[i][j]=x;
		}
	}
	bfs();
 } 


Measured memory_bytes: 26908
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include <cmath>
#include <climits>
#include <queue>

using namespace std;

int main() {
    int n; cin >> n;
    int ans = 0;
    for(int i = 0; i < n; ++i){
        int tmp; cin >> tmp;
        ans += tmp;
    }
    ans -= n;
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define scanVec(vec , n) for(int i = 0; i < n ; i++){ cin>>vec[i];}
#define printVec(vec , n) for(int i = 0; i < n ; i++){ cout<<vec[i]<<" ";}
#define S second
#define F first
const int  MOD = 1e9 + 7;
int cnt[4];
double dp[301][301][301];

//  a d f j k  l  ;
int main(){
    ios::sync_with_stdio(0);  cin.tie(0);
    int n , tmp;
    cin>>n;
    for(int i = 0 ; i < n ; i++){
        cin>>tmp;
        cnt[tmp]++;
    }
 
    memset(dp, 0, sizeof(dp));
 
    for (int k = 0; k <= n; k++){
        for (int j = 0; j <= n; j++){
            for (int i = 0; i <= n; i++){
                if ((i + j + k) && i + j + k <= n){
                    if (i) dp[i][j][k] += dp[i - 1][j][k] * i / n;
                    if (j) dp[i][j][k] += dp[i + 1][j - 1][k] * j / n;
                    if (k) dp[i][j][k] += dp[i][j + 1][k - 1] * k / n;
                    dp[i][j][k] += 1;
                    dp[i][j][k] /= 1.0 * (i + j + k) / n;
                }
            }
        }
    }
 
    cout << setprecision(20) << dp[cnt[1]][cnt[2]][cnt[3]] << endl;


    return 0;
}

Measured memory_bytes: 213376
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define REP(i, a, b) for(int i = a; i <= b; i++)
#define PER(i, a, b) for(int i = a; i >= b; i--)
#define LL long long
#define lowbit(x) x &(-x)

inline int read() {
    int x = 0, flag = 1;char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') flag = - 1;
        ch = getchar();
    }
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x * flag;
}
string s[100], c;
int n, a[100];
int main() {   
    n = read();
    REP(i, 1, n) {
        cin >> s[i]; a[i] = read() + a[i - 1];
    }
    cin >> c;
    REP(i, 1, n) {
        if(s[i] == c) {
            cout << (a[n] - a[i]) << endl;
            break;
        }
    }
	return 0;
}


Measured memory_bytes: 764
N=int(input())
L=list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      if L[k] < (L[i] + L[j]):
        if L[i] == L[j] or L[j] == L[k]:
          continue
        ans += 1
print(ans)


Measured memory_bytes: 9184
#include <stdio.h>

int main(void){

  int a,b;

  scanf("%d %d",&a,&b);

  if(a < b){
    printf("a < b\n");
  }else if(a > b){
    printf("a > b\n");
  }else{
    printf("a == b\n");
  }
  return 0;
}



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

vector<bool> primes_create(int n){
	vector<bool> p(n+1, true);
	p[0] = p[1] = false;
	for(int i=2; i*i<=n; ++i)
		if(p[i]) for(int j=i+i; j<=n; j+=i) p[j] = false;
	return p;
}

int main(){
	vector<bool> primes = primes_create(100000+1);

	vector<int> C(primes.size());//Cumulative sum
	for(int n=3; n<primes.size(); ++n)
		C[n] += C[n-1] + (n%2 && primes[n] && primes[(n+1)/2]);

	int Q; cin>>Q;
	while(Q--){
		int l,r; cin>>l>>r;
		cout<< C[r] - C[l] + (C[l]!=C[l-1]) <<endl;
	}
}

Measured memory_bytes: 1024
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
xt = 0
BC = A
for i in range(1, N):
	xt += x
	A = [A[-1]] + A[0:-1] 
	BC = [v if v < BC[i] else BC[i] for (i, v) in enumerate(A)]
	tempcost = xt + sum(BC)
	if(ans > tempcost):
		ans = tempcost
print(ans)


Measured memory_bytes: 3316
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int n = sc.nextInt();
		int[] values = new int[w];
		for(int i = 0; i < values.length; i++)
			values[i] = i + 1;

		for(int i = 0; i < n; i++) {
			String str = sc.next();
			String[] ab = str.split(",");
			int a = Integer.parseInt(ab[0]) - 1;
			int b = Integer.parseInt(ab[1]) - 1;

			int temp;
			temp = values[a];
			values[a] = values[b];
			values[b] = temp;
		}

		for(int i = 0; i < values.length; i++)
			System.out.println(values[i]);
	}
}

Measured memory_bytes: 26300
#include<iostream>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<math.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef long double ld;
const long double pi = 3.14159265358979323846264338327950288;
vector<int>e[100001];
int dp[100001][11];
int v[100000], d[100000], c[100000];
struct R {
	int pos, cost;//頂点、距離
};
int main() {
	int n, m, q;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> v[i] >> d[i] >> c[i];
	}
	for (int i = q - 1; i >= 0; i--) {
		queue<R>q;
		q.push(R{ v[i], d[i] });
		while (!q.empty()) {
			R t = q.front(); q.pop();
			if (dp[t.pos][t.cost] == 0) {
				for (int j = t.cost; j >= 0; j--) {
					if (dp[t.pos][j] == 0)
						dp[t.pos][j] = c[i];
				}
				if (t.cost > 0) {
					for (int j = 0; j < e[t.pos].size(); j++) {
						if (dp[e[t.pos][j]][t.cost - 1] == 0)
							q.push(R{ e[t.pos][j],t.cost - 1 });
					}
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << dp[i][0] << endl;
	}
	cin >> n;
}

Measured memory_bytes: 12424
#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <complex>
#include <valarray>
#include <fstream>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <random>

#define _overload(a, b, c, d, ...) d
#define _rep1(X, A, Y) for (int (X) = (A);(X) <= (Y);++(X))
#define _rep2(X, Y) for (int (X) = 0;(X) < (Y);++(X))
#define rep(...) _overload(__VA_ARGS__, _rep1, _rep2)(__VA_ARGS__)
#define rrep(X,Y) for (int (X) = Y-1;(X) >= 0;--(X))
#define all(X) (X).begin(),(X).end()
#define len(X) ((int)(X).size())
#define mod(n, m) (((n)%(m)+(m))%m)
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
const int INFINT = 1 << 30;                          // 1.07x10^ 9
const ll INFLL = 1LL << 60;                          // 1.15x10^18
const double EPS = 1e-10;
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MAX_N = 10, MAX_D = 10;
int N, D;
int X[MAX_N][MAX_D];


int dist(int y, int z) {
  int res = 0;
  for (int i = 0; i < D; ++i) {
    res += (X[y][i]-X[z][i])*(X[y][i]-X[z][i]);
  }
  return res;
}

bool isSq(int num) {
  for (int i = 1; i*i <= num; ++i) {
    if (i*i == num) {
      return true;
    }
  }
  return false;
}

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

  cin >> N >> D;
  rep(i, N) {
    rep(j, D) {
      cin >> X[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i+1; j < N; ++j) {
      int d = dist(i, j);
      if (isSq(d)) {
        ++ans;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


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

#define rep1(i, a, n) for (int i = a; i < n; i++)
#define rep2(i, a, n) for (int i = n; i >= a; i--)
#define asc(v) sort(v.begin(), v.end())
#define desc(v) sort(v.begin(), v.end(), greater<int>())

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  if ((N - 1) % (K - 1) == 0) {
    cout << (N - 1) / (K - 1) << endl;
  } else {
    cout << ((N - 1) / (K - 1)) + 1 << endl;
  }
  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int[] a=new int[5];
		for(int i=0;i<5;i++){
			a[i]=sc.nextInt();
		}
		for(int i=0;i<4;i++){
			for(int j=4;j>i;j--){
				if(a[j]<a[j-1]){
					int box=a[j];
					a[j]=a[j-1];
					a[j-1]=box;
				}
			}
		}
		for(int i=4;i>=0;i--){
			if(i==4){
				System.out.print(a[i]);
			}else{
				System.out.print(" "+a[i]);	
			}
		}
		System.out.println();
	}
}

Measured memory_bytes: 17232
#include <bits/stdc++.h>
using namespace std;
 
//krishrawat
 
 
//----------------------------MACROS-------------------------------------------------
#define pb insert//                                                                 |
#define eb emplace_back//                                                           |
#define ff first//                                                                  |
#define ss second//                                                                 |
typedef long long int ll;//                                                         |
typedef long double ld;//                                                           |
#define all(a) a.begin(), a.end()//                                                 |
#define show(x) cerr << #x << " is " << x << "\n"//                                 |
#define show2(x, y) cerr << #x << " " << #y << " " << x << " " << y << "\n"//       |
typedef vector<ll> vl;//                                                            |
typedef vector<vl> vvl;//                                                           |
typedef pair<ll, ll> pp;//                                                          |
typedef vector<pp> vp;//                                                            |
typedef map<pp, ll> ipi;//                                                          |
typedef map<pp, char> ipc;//                                                        |
typedef map<ll, ll> ii;//                                                           |
typedef set<ll> sl;//                                                               |
typedef multiset<ll> msl;//                                                         |
typedef map<char, ll> ci;//                                                         |
typedef set<pair<ll, ll>> sp;//                                                     |
const ll mod = 1e9 + 7;//                                                           |
const ll N = 1e5 + 1;//                                                             |
//----------------------------------------------------------------------------------


void solve()
{   
    ll n,m;cin>>n>>m;
    string s;cin>>s;
    ll f=0,cnt=0;
    ll ans=0;
    for(int i=1;i<n;i++){
        if((s[i]==s[0] && f) || s[i]==s[i-1]){
            cnt++;
        }
        else{
            f=0;
            if(m){
                cnt++;
                m--;
                f=1;
            }
            
        }
        
    }
    cout<<cnt<<"\n";
}
 
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    
    ll t; t=1;
    //cin>>t;
    while(t--) solve(); return 0;
}

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

void swap(char *i, char *j)
{
	*i += *j;
	*j = *i - *j;
	*i -= *j;
}

int main(void)
{
	char str[9];
	char tmp[9];
	int max, min;
	int n;
	int i, j;
	
	scanf("%d", &n);
	while (n--){
		scanf("%s", str);
		strcpy(tmp, str);
		
		for (i = 0; i < 7; i++){
			for (j = 0; j < 7 - i; j++){
				if (str[j] < str[j + 1]){
					swap(&str[j], &str[j + 1]);
				}
				
				if (tmp[j] > tmp[j + 1]){
					swap(&tmp[j], &tmp[j + 1]);
				}
			}
		}
		
		max = min = 0;
		for (i = 0; i < 8; i++){
			max *= 10;
			max += (str[i] - '0');
			min *= 10;
			min += (tmp[i] - '0');
		}
		
		printf("%d\n", max - min);
	}
	
	return (0);
}

Measured memory_bytes: 608
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define square(x) pow(x ,2.0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v) sort(all(v), [](int a, int b) { return a > b; })//大きい順にソート
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
#define co(x) cout << x << endl;
#define cins(n,v) rep(i,n){cin >> v.at(i);}
#define couts(v) rep(i,v.size()){cout << v.at(i) << endl;}
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
/*
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
*/

int main() {
  long double a,b,h,m,angle;
  cin >> a >> b >> h >> m;
  angle = abs(m/60.0 - (h*60.0+m)/(12.0*60.0));
  angle = min(angle,1-angle);
  angle *= 2.0*M_PI;
  angle = pow(square(a)+square(b)-2.0*a*b*cos(angle),0.5);
  std::cout << std::setprecision(10) << angle << endl;

  return 0;
}


Measured memory_bytes: 4152
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
const int N=1e5+5;
int n;

inline int nxi(){
	int x=0;
	char c;
	while((c=getchar())>'9'||c<'0');
	while(x=x*10-48+c,(c=getchar())>='0'&&c<='9');
	return x;
}

namespace G{
	int cnt,fir[N];
	bool linked[N];
	struct edge{
		int to,nx;
	}eg[N<<1];

	inline void add(const int a,const int b){
		eg[++cnt]=(edge){b,fir[a]};
		fir[a]=cnt;
	}

	void dfs(const int x,const int fa){
		for(int i=fir[x]; i; i=eg[i].nx){
			const int y=eg[i].to;
			if(y==fa) continue;
			dfs(y,x);
			if(!linked[y]){
				if(linked[x]) return;
				linked[x]=linked[y]=1;
			}
		}
	}

	bool link(){
		for(int i=1; i<=n; ++i){
			if(!linked[i]) return 0;
		}
		return 1;
	}
}

int main(){
	n=nxi();
	for(int i=1; i<n; ++i){
		const int a=nxi(),b=nxi();
		G::add(a,b);
		G::add(b,a);
	}
	G::dfs(1,1);
	puts(G::link()?"Second":"First");
	return 0;
}

Measured memory_bytes: 2304
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <map>
#include <numeric>
#include <random>
#include <queue>
#include <deque>
#include <tuple>
#include <iomanip>
#include <iterator>
#include <functional>

using namespace std;
typedef long long ll;

const int INF = (1 << 30) - 1;
const ll INFLL= (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N,C;
  cin>>N>>C;
  vector<tuple<int,int,int>> stc;
  REP(i,N){
    int s,t,c;
    cin>>s>>t>>c;
    stc.emplace_back(s,t,c);
  }
  sort(ALL(stc));
  vector<pair<int,int>> recorders(C,{0,-1});
  REP(i,N){
    int s=get<0>(stc[i]);
    int t=get<1>(stc[i]);
    int c=get<2>(stc[i]);
    bool ren=false;
    REP(j,C){
      if(c==recorders[j].first && s==recorders[j].second){
        ren=true;
        recorders[j]=make_pair(c,t);
        break;
      }
    }
    if(ren)continue;
    REP(j,C){
      if(recorders[j].second<s){
        recorders[j]=make_pair(c,t);
        break;
      }
    }
  }
  int ans=0;
  REP(i,C)ans+=(recorders[i].second!=-1);
  cout<<ans<<endl;
}


Measured memory_bytes: 1912
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
# define mod 1000000007
# define INF (1 << 29)
/* ちゃんと考えて実装して問題なくACできた時嬉しいよ！！ */

int main(void){
    int n,m;
    cin >> n >> m;
    map<int, vector<int> > job;
    for(int i=0; i<n; i++){
        int a,b; cin >> a >> b;
        job[a].push_back(b);
    }

    int ans = 0;
    priority_queue<int> pq;
    for(int i=1; i<=m; i++){
        if(job[i].size() >= 1){
            for(int j=0; j<job[i].size(); j++){
                pq.push(job[i][j]);
            }
        }
        // pqがからでないならあああああ
        if(!pq.empty()){
            ans += pq.top();
            pq.pop();
        }
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 10880
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
 
#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl
 
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
typedef deque<bool> db;
template<class T> using vv=vector<vector< T > >;

vvi g;
int cnt;
int max_distance;

void dfs(int prev, int current, int distance) {
  cnt += 1;
  if (distance == max_distance) return;
  for (int u : g[current]) {
    if (u == prev) continue;
    dfs(current, u, distance + 1);
  }
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  g.resize(n);
  vvi edges(n-1, vi(2));
  rep (i, n-1) {
    int a, b;
    scanf("%d%d", &a, &b);
    a -= 1;
    b -= 1;
    g[a].push_back(b);
    g[b].push_back(a);
    edges[i][0] = a;
    edges[i][1] = b;
  }

  max_distance = k / 2;
  int max_nodes = 0;
  if ((k&1) == 0) {
    rep (i, n) {
      cnt = 0;
      dfs(-1, i, 0);
      max_nodes = max(max_nodes, cnt);
    }
  } else {
    rep (i, n-1) {
      cnt = 0;
      dfs(edges[i][0], edges[i][1], 0);
      dfs(edges[i][1], edges[i][0], 0);
      max_nodes = max(max_nodes, cnt);
    }
  }
  printf("%d\n", n - max_nodes);

  return 0;
}


Measured memory_bytes: 512
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <queue>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

vector<int> G[100000];
bool used[100000];
int fc, sc;

void bfs(queue<int> &q, bool f_turn) {
    vector<int> vi;
    while (!q.empty()) {
        int v = q.front(); q.pop();
        for (int i : G[v]) if (!used[i]) {
            if (f_turn) fc++;
            else sc++;
            vi.push_back(i);
            used[i] = true;
        }
    }
    for (int i : vi) q.push(i);
}

int main() {
    int N, a, b;
    cin >> N;
    REP(i, N - 1) {
        cin >> a >> b;
        G[a - 1].push_back(b - 1);
        G[b - 1].push_back(a - 1);
    }

    used[0] = used[N - 1] = true;
    queue<int> fq, sq; fq.push(0); sq.push(N - 1);
    bool f_turn = true;
    while (!fq.empty() || !sq.empty()) {
        if (f_turn) bfs(fq, true);
        else        bfs(sq, false);
        f_turn = !f_turn;
    }

    if (fc > sc) cout << "Fennec" << endl;
    else cout << "Snuke" << endl;
    return 0;
}

Measured memory_bytes: 6196
#include<iostream>

int w, h;
int map[16][15], memo[16][15];

int rec(int x, int y){
	if(memo[y][x] >= 0){
		return memo[y][x];
	}

	if(map[y][x] == 1){
		return 0;
	}

	if(y >= h-1){
		return 1;
	}

	int res = 0;
	if(map[y][x] == 0){
		for(int i=-1;i<2;i++){
			if(x+i >= 0 && x+i < w && !(map[y+1][x+i] == 2 && i != 0)){
				res += rec(x+i, y+1);
			}
		}
		return memo[y][x] = res;
	}else if(map[y][x] == 2){
		return memo[y][x] = rec(x, y+2);
	}
}

int main(){
	while(std::cin >> w >> h, w){

		for(int i=0;i<16;i++){
			for(int j=0;j<15;j++){
				memo[i][j] = -1;
				map[i][j] = 0;
			}
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				std::cin >> map[i][j];
			}
		}

		int res = 0;
		for(int i=0;i<w;i++){
			res += rec(i, 0);
		}

		std::cout << res << std::endl;
	}
}

Measured memory_bytes: 1160
#include<iostream>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;

#define rep0(n) for(int i=0;i<n;i++)
#define rep(n) for(int i=1;i<=n;i++)

double const pi=3.1415926535;

int printint(int n){
	cout<<n<<endl;
	return 0;
}

int prints(string s){
	cout<<s<<endl;
	return 0;
}

int main(){
	int ans=0;
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	rep0(101){
		if(a<=i&&i<b&&c<=i&&i<d)ans++;
	}
	printint(ans);
	return 0;
}

Measured memory_bytes: 256
n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
cnt = 0

n.times do |i|
  if a[i]+a[i+1] >= b[i]
    cnt += b[i]
    if a[i] < b[i]
      a[i+1] -= b[i] - a[i]
    end
      
  else
    cnt += a[i]+a[i+1]
    a[i+1] = 0
  end
end

puts cnt


Measured memory_bytes: 15492
#include <iostream>
using namespace std;
bool has3(int x) {
    while(x) {
        if(x % 10 == 3) return true;
        x /= 10;
    }
    return false;
}
int main() {
    int n;
    cin >> n;
    for(int x = 1; x <= n; ++x) {
        if(x % 3 == 0 || has3(x)) {
            cout << " " << x;
        }
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 3092
#include <bits/stdc++.h>
using i64 = int_fast64_t;
#define repeat(i, a, b) for(int i = (a); (i) < (b); ++(i))
#define rep(i, n) repeat(i, 0, n)

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    rep(i, n) std::cin >> a[i];
    std::vector<int> mp(4);
    rep(i, n) mp[a[i] % 4]++;
    mp[1] += mp[3];
    mp.erase(mp.end() - 1);
    // std::cout << mp[0] << " " << mp[1] << " " << mp[2] << std::endl;
    if(mp[2] > 0) {
        if(mp[0] >= mp[1]) {
            std::cout << "Yes" << std::endl;
            return;
        }
    } else {
        if(mp[0] + 1 >= mp[1]) {
            std::cout << "Yes" << std::endl;
            return;
        }
    }
    std::cout << "No" << std::endl;
    return;
}

int main() {
    // std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    solve();
    return 0;
}

Measured memory_bytes: 640
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100000+20;
int n,a[N],sz[N];
int mabs(int x)
{
    if(x<0) return x*-1;
    return x;
}
int main()
{
    //freopen("out.txt","w",stdout);
    while(~scanf("%d",&n))
    {
        sz[0]=0;
        a[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sz[i]=sz[i-1]+mabs(a[i]-a[i-1]);
        }
        printf("%d\n",sz[n]-sz[2]+mabs(a[n])+mabs(a[2]));
        for(int i=2;i<n;i++)
            printf("%d\n",sz[n]-sz[i+1]+mabs(a[i+1]-a[i-1])+sz[i-1]+mabs(a[n]));
        printf("%d\n",sz[n-1]+mabs(a[n-1]));

    }


    return 0;
}


Measured memory_bytes: 2048
#!/usr/bin/env python3

def main():
    n = int(input())
    n_prev = [[2 for j in range(n)] for i in range(n)]
    post_matches = [[[] for j in range(n)] for i in range(n)]
    q = []
    for i in range(n):
        ai = list(map(int, input().split()))
        ai = [x - 1 for x in ai]
        for j in range(n - 1):
            x = min(i, ai[j])
            y = max(i, ai[j])
            if j == 0:
                n_prev[x][y] -= 1
                if n_prev[x][y] == 0:
                    q.append((x, y))
            else:
                xp = min(i, ai[j - 1])
                yp = max(i, ai[j - 1])
                post_matches[xp][yp].append((x, y))

    time = 0
    while q:
        time += 1
        qn = []
        while q:
            i, j = q.pop()
            for i2, j2 in post_matches[i][j]:
                n_prev[i2][j2] -= 1
                if n_prev[i2][j2] == 0:
                    qn.append((i2, j2))
        q = qn

    for i in range(n):
        for j in range(i + 1, n):
            if n_prev[i][j] > 0:
                print(-1)
                return
    print(time)

if __name__ == "__main__":
    main()


Measured memory_bytes: 129544
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <map>
#include <string>

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

int main(){
    map<string,int> mp;

    int cnt;
    cin >> cnt;
    string str,str2;
    cin >> str;
    mp[str]++;
    rep(i,cnt-1){
        cin >> str2;
        //cout << str[str.length()-1] << " " << str2[0] << endl; 
        if(str[str.length()-1] == str2[0]){
            if(mp[str2]==0){
                mp[str2]++;
                str = str2;
            }else{
                str = str2;
                cout << "No" << endl;
                return 0;
            }
        }else{
            cout << "No" << endl;
            return 0;
        }
    }

    for(auto &e : mp){
        //cout << e.first << " " << e.second << endl;
    }

    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
        #define REP(i, n) for(int i = 0;i < n;i++)
        #define REPR(i, n) for(int i = n;i >= 0;i--)
        #define FOR(i, m, n) for(int i = m;i < n;i++)
        #define FORR(i, m, n) for(int i = m;i >= n;i--)
        #define SORT(v, n) sort(v, v+n);
        #define VSORT(v) sort(v.begin(), v.end());
        #define llong long long
        #define pb(a) push_back(a)
        #define INF 1000000000000000000LL
        #define MOD 1000000007
        using namespace std;
        typedef pair<int, int> P;
        typedef pair<llong, llong> LP;
        typedef pair<int, P> PP;
        typedef pair<llong, LP> LPP;
        //vector<tuple<long long, long long, long long>>vec;
    
        template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
        template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
    
        int dy[]={0, 0, 1, -1, 0};
        int dx[]={1, -1, 0, 0, 0};
    
    
        int main()
        {
            int N,M;
            cin >> N >> M;
            int a[N],b[N],c[M],d[M];
            int i,j;
            for(i=0;i<N;i++){
                cin >> a[i] >> b[i];
            }
            for(i=0;i<M;i++){
                cin >> c[i] >> d[i];
            }
            long long ans,ansx,ansy,distance,tempdistance,tempx,tempy;
            for(i=0;i<N;i++){
                ansx= labs(a[i]-c[0]);
                ansy= labs(b[i]-d[0]);
                distance = ansx + ansy;
                ans = 1;
                for(j=0;j<M;j++){
                    tempx = labs(a[i]-c[j]);
                    tempy = labs(b[i]-d[j]);
                    tempdistance = tempx + tempy;
                    if(distance>tempdistance){
                        distance = tempdistance;
                        ans = j+1;
                    }
                }
                cout << ans << endl;
            }
            return 0;
        }
  

Measured memory_bytes: 256
/*"Success isn't permanent, failure isn't fatal,
                                      it's the courage to continue that counts"*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll long long 
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld",n)
#define pri(n) printf("%d",n)
#define pln() printf("\n")
#define priln(n) printf("%d\n",n)
#define lpriln(n) printf("%lld\n",n)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define pb push_back     
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod=1e9+7;         
const int N=2e5+4;

int main()
{
	ll n,k;

	cin>>n>>k;

	ll rem=n%k; 

	lpri(min(rem,k-rem));
	
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
long long get()
{
	char c=getchar();
	long long x=0LL;
	while(c<'0'||c>'9')
	c=getchar();
	while(c>='0'&&c<='9')
	{
		x*=10LL;
		x+=(c-'0');
		c=getchar();
	}
	return x;
}
long long n;
string s;
int main()
{
	ios::sync_with_stdio(0);
	cin>>n;
	while(n)
	{
		
		int x=n%26;
		n/=26;
		if(x==0)
		{
			x=25;
			n--;
		}
		else x--;
		s+=x+'a';
	}
	reverse(s.begin(),s.end());
	cout<<s;
	return 0;
}

Measured memory_bytes: 3608
L, *A = map(int, open(0))

D = [(2, 1), (1, 0), (0, 1)]

s0 = s1 = s2 = s3 = s4 = 0

for a in A:
    e, o = D[(a - 1) % 2 + 1 if a else 0]

    s0 += a
    s1 = min(s0, s1 + e)
    s2 = min(s1, s2 + o)
    s3 = min(s2, s3 + e)
    s4 = min(s3, s4 + a)

print(min(s0, s1, s2, s3, s4))


Measured memory_bytes: 11036
n, k = map(int, input().split())
A = {}
for _ in range(n):
    a, b = map(int, input().split())
    A[a] = A.get(a, 0) + b

ans = 0
for a in sorted(A):
    ans += A[a]
    if ans >= k:
        print(a)
        break

Measured memory_bytes: 10588
n=int(input())
a=list(map(int, input().split()))

l=[0]*9
for rate in a:
    if rate<=399:
        l[0]=1
    elif rate<=799:
        l[1]=1
    elif rate<=1199:
        l[2]=1
    elif rate<=1599:
        l[3]=1
    elif rate<=1999:
        l[4]=1
    elif rate<=2399:
        l[5]=1
    elif rate<=2799:
        l[6]=1
    elif rate<=3199:
        l[7]=1
    else:
        l[8]+=1

large=sum(l)

if sum(l)-l[8]==0:#rate>=3200のみの時
    small=1
else:
    small=sum(l)-l[8]

print(small, large)


Measured memory_bytes: 3064
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

string solve(){
    int N,A,B; cin >> N >> A >> B;
    string res;
    if ((B - A) % 2 == 0)  res = "Alice";
    else res = "Borys";
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define repeat(i,a,b) for(int i=(a),i##__end=(b);i<i##__end;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,i##__end=(a);i>=i##__end;i--)
#define mst(a,x) memset((a),(x),sizeof(a))
#define pii pair<int,int>
#define N 100010
#define mod 998244353
#define int ll
int f[N];
ll qpow(ll a,ll b/*,ll mod*/){
	ll ans=1;
	do{
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
	}while(b>>=1);
	return ans;
}
signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n,x;
	cin>>n;
	cin>>x;
	if(x!=0){cout<<0<<endl; return 0;}
	f[0]=1;
	repeat(i,1,n){
		cin>>x;
		if(x<=0){cout<<0<<endl; return 0;}
		f[x]++;
	}
	int m=n,ans=1;
	while(f[m]==0)m--;
	repeat_back(i,1,m+1){
		ans=(ans*qpow(f[i-1],f[i]))%mod;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 1024
#include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
using namespace std;
int main(void)
{
	char o[51],e[51];
	int i,len1,len2;
	scanf("%s %s",o,e);
	len1=strlen(o);
	len2=strlen(e);
	for(i=0;i<len2;i++)	printf("%c%c",o[i],e[i]);
	if(len1!=len2)	printf("%c",o[len1-1]);
	printf("\n");
	return 0;
}

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

int d[110];

int main() {
    int n;

    cin >> n;

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

    set<int> set;

    for (int i = 0; i < n; i++) {
        set.insert(d[i]);
    }

    cout << set.size() << endl;

    return 0;
}


Measured memory_bytes: 256
import sys
from io import StringIO
import unittest
import fractions
import math
from functools import reduce

def lcm_base(a, b):
    return (a * b) // fractions.gcd(a, b)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def resolve():
    n = int(input())
    t = [int(input()) for _ in range(n)]

    print(lcm_list(t))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2
2
3"""
        output = """6"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """5
2
5
10
1000000000000000000
1000000000000000000"""
        output = """1000000000000000000"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()


Measured memory_bytes: 77932
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
L = 1
R = n

for i in range(m):
  a,b = map(int,input().split())
  L = max(L,a)
  R = min(R,b)

if L > R:
  print('0')
else:
  print(R-L+1)

Measured memory_bytes: 3060
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import static java.util.Comparator.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			execute(scanner);
		}
	}

	private static void execute(Scanner scanner) {
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		if (n == 0 && m == 0)
			System.exit(0);
		String line = scanner.nextLine();
		List<Integer> priceList = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).sorted()
				.collect(Collectors.toList());

		int max = -1;
		for (int i = priceList.size() - 1; i >= 0; i--) {
			int first = priceList.get(i);
			if (first > m) {
				continue;
			}
			for (int j = priceList.size() - 1; j >= 0; j--) {
				if (i == j) {
					continue;
				}
				int second = priceList.get(j);
				if (first + second > m) {
					continue;
				}
				if (max < first + second) {
					max = first + second;
					break;
				}
			}
		}
		if (max == -1) {
			System.out.println("NONE");
		} else {
			System.out.println(max);
		}
	}
}

Measured memory_bytes: 49100
package main

import (
	"fmt"
)

func main() {
	var s, res string
	fmt.Scan(&s)

	for i:=0; i<len(s); i++ {
		if (i+1)%2 == 1 {
			res += string(s[i])
		}
	}

	fmt.Println(res)
}

Measured memory_bytes: 7808
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);++i)
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for(;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for(;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
#define fi first
#define se second
const int N=200002,M=1e9+7;
int n,A,B,f[N],g[N],fa[N],b[N],cnt,x,y,ans,num[N];
vector<pair<int,int> >v[N];
vector<int>vec[N];
bool cmp(int x,int y){return v[x][0]<v[y][0];}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void uni(int x,int y){
	int rx=find(x),ry=find(y);
	if (rx!=ry) fa[rx]=ry;
}
int main(){
	n=rd(),A=rd(),B=rd();
	rep(i,1,n) x=rd(),y=rd(),v[x].push_back(make_pair(y,i)),fa[i]=i;
	f[0]=g[0]=f[1]=g[1]=1;
	rep(i,2,n) f[i]=1ll*f[i-1]*i%M,g[i]=M-1ll*(M/i)*g[M%i]%M;
	rep(i,2,n) g[i]=1ll*g[i]*g[i-1]%M;
	rep(i,1,n)
		if (v[i].size()) b[++cnt]=i;
	rep(i,1,cnt) sort(v[b[i]].begin(),v[b[i]].end());
	sort(b+1,b+cnt+1,cmp);
	x=v[b[1]][0].fi,y=v[b[1]][0].se;
	rep(i,2,cnt)
		rep(j,0,v[b[i]].size()-1)
			if (x+v[b[i]][j].fi<=B) uni(y,v[b[i]][j].se);
	if (cnt>1){
		rep(j,1,v[b[1]].size()-1)
			if (v[b[2]][0].fi+v[b[1]][j].fi<=B) uni(v[b[2]][0].se,v[b[1]][j].se);
	}
	rep(i,1,cnt)
		rep(j,1,v[b[i]].size()-1)
			if (v[b[i]][0].fi+v[b[i]][j].fi<=A) uni(v[b[i]][0].se,v[b[i]][j].se);
	rep(i,1,n) fa[i]=find(fa[i]);
	rep(i,1,cnt)
		rep(j,0,v[b[i]].size()-1) vec[fa[v[b[i]][j].se]].push_back(i);
	ans=1;
	rep(i,1,n)
		if (vec[i].size()){
			ans=1ll*ans*f[vec[i].size()]%M;
			rep(j,0,vec[i].size()-1) num[vec[i][j]]++;
			rep(j,0,vec[i].size()-1) ans=1ll*ans*g[num[vec[i][j]]]%M,num[vec[i][j]]=0;
		}
	printf("%d",ans);
}

Measured memory_bytes: 23936
fun main(args: Array<String>) {
    val S = readLine()!!
    val K = readLine()!!.toLong()
    var index = 0

    for (c in S) {
        if(c != '1') {
            break
        } else {
            index += 1
        }
    }

    // println("index: ${index.toLong()}")
    // println("index: ${index.toLong()>=(K-1)}")
    if(index.toLong() >=K) {0} else {index}.let{ S[it]}.run(::println)
    
}

Measured memory_bytes: 33728
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i,n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793
using namespace std;
typedef long long ll;
template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}
int gcd(int a, int b){//ユークリッドの互除法
  if (a < b) gcd(b,a); //aの方がbよりでかいのが前提
  if (b == 0) return a; //aをbで割り切れたらreturn
  else gcd(b, a % b);
}

const int MOD = 1000000007;

class UnionFind{
public:
  //親の番号を格納する変数。数値がマイナスの場合は親であり、その絶対値のサイズのグループを持っている
  vector<int> Parent;
  
  //コンストラクタ。-1で初期化する。
  UnionFind(int N){
    Parent = vector<int>(N,-1);
  }
  
  //Aが何処のグループに属しているか返す
  int root(int A){
    if(Parent[A] < 0) return A; //0以下の時は自分が親。自分のグループなので自分を返す
    return Parent[A] = root(Parent[A]); //根でなかった場合は再帰的に関数を呼ぶ。ついでにParent[A]を更新して次の時高速化する。
  }
  
  //自分の親の頂点数を返す。マイナスでグループのサイズを持っているので反転して返す。
  int size(int A){
    return -Parent[root(A)];
  }
  
  //AとBを連結する関数
  bool connect(int A, int B){
    A = root(A); //Aの親を持ってくる
    B = root(B); //Bの親を持ってくる
    if (A==B){ //親が同じだったらくっつけない
      return false;
    }
    
    //Aが必ず大きい数にする
    if (size(A) < size(B)) swap(A,B);
    
    //AにBのグループをくっつける。
    Parent[A] += Parent[B];
    //Bの親をAにする。
    Parent[B] = A;
    
    return true;
  }
};
int main(){
  int n,m; cin >> n >> m;
  vector<int> p(n);
  rep(i,n){
    int tmp;
    cin >> tmp;
    tmp--;
    p[i] = tmp;
  }
  UnionFind uf(n);
  
  rep(i,m){
    int x,y; cin >> x >> y;
    x--,y--;
    uf.connect(p[x],p[y]);
  }
  
  int ans = 0;
  rep(i,n){
    if(uf.root(p[i]) == uf.root(i)) ans++;
  }
  
  cout << ans << endl;
  
  
  return 0;
}

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

int main() {
  string S;
  char a,b;
  cin >> S;
  a=S.at(5);
  b=S.at(6);
  if(a=='0'){
    if(b=='1'||b=='2'||b=='3'||b=='4'){
      cout << "Heisei" << endl;
    }else{
      cout << "TBD" << endl;
    }
  }else{
    cout << "TBD" << endl;
  }
}

Measured memory_bytes: 256
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
#include<set>
#include<iomanip>
#include<vector>
#define pi acos(-1)
using namespace std;
typedef unsigned long long int LL;
const int inf=1e9+10;
const int N=1e5+10;
int main()
{
    int x[6];
    for(int i=1;i<=5;i++)
    {
        scanf("%d",&x[i]);
    }
    for(int i=1;i<=5;i++)
    {
        if(x[i]==0)
        {
            printf("%d\n",i);
            return 0;
        }
    }
    return 0;
}


Measured memory_bytes: 3828
// C++ program to print all prime factors 
#include <bits/stdc++.h> 
using namespace std;
int main () {
  long long int h,n,sum=0;cin>>h>>n;
  long long int a[n];
  for (long long int i=0;i<n;i++) {cin>>a[i];sum+=a[i];}
  if (sum>=h) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;









}


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

int main(){
  long long int h;
  long long int count=0;
  cin >> h;
  while(h!=0){
    h/=2;
    count++;
  }
  printf("%lld\n",(long long int)pow(2,count)-1);
  return 0;
}


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

int main() {
  ll x, y, z;
  cin >> x;
  
  y = x/500;
  z = (x % 500) / 5;
  
  cout << y * 1000 + z * 5 << endl;
}



Measured memory_bytes: 3640
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
char s[maxn];
int t,len,l,r;
int main()
{
scanf("%s",s+1);
len=strlen(s+1);
t=100005;
   for(int i=1;i<len;++i)
   if(s[i]!=s[i+1]) t=min(t,max(len-i,i));
   if(t==100005)
   cout<<len<<endl;
   else 
   cout<<t<<endl; 
 return 0;
 }

Measured memory_bytes: 384
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

bool A[110][110];
int flag[110],in[110],out[110];
int now=1,n;

void dfs(int u){
	flag[u]=1;
	in[u]=now++;
	for(int i=1;i<=n;i++){
		if(flag[i]==0 && A[u][i]==true){
			dfs(i);
		}
	}
	out[u]=now++;
	return ;
}

int main(){
	int a,b,c;
	cin>>n;
	for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				A[i][j]=false;
		}
	}
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		for(int j=1;j<=b;j++){
			cin>>c;
			A[a][c]=true;
		}
	}
	for(int i=1;i<=n;i++){
		if(flag[i]==0) dfs(i);
	}
	for(int i=1;i<=n;i++){
		cout<<i<<" "<<in[i]<<" "<<out[i]<<endl;
	}
	return 0;
}


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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63)

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 	1,000,000 -> Loop
// 	100,000	-> vector(int:400KB, long:800KB) 

#define	INF (1<<30)
//1,073,741,824
//= 536,870,912 *2

#define	MOD 1000000007

#define	Rep0(i, n)	for (int i=0; i<n; i++)
#define	Rep1(i, n)	for (int i=1; i<=n; i++)
#define Sort(P)	sort(P.begin(), P.end())
#define Rev(P)	reverse(P.begin(), P.end())

int	main() {
	string s;
	cin >>s;
	int N=s.size();
	vector<int> S(N+1, 0);
	for (int i=0; i<N; i++)
		if ('1'==s.at(i))
			S.at(i+1)++;
	if (0==S.at(1)) {
		cout <<-1 <<endl;
		return 0;
	}
	if (1==S.at(N)) {
		cout <<-1 <<endl;
		return 0;
	}
	for (int i=1; i<N; i++)
		if (S.at(i)!=S.at(N-i)){
			cout <<-1 <<endl;
			return 0;
		}
	int B=1;
	for (int i=1; i<N; i++) {
		if (1==S.at(i)) {
			cout <<B <<' ' <<i+1 <<endl;
			B=i+1;
		} else {
			cout <<B <<' ' <<i+1 <<endl;
		}
	}
	return 0;
}



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

class Program
{
    // 状態のリスト
    static Dictionary<string, State> states = new Dictionary<string, State>();
    // 辿っているQueue
    static Queue<State> queue = new Queue<State>();

	public static int ToInt(char self)
    {
        return self - '0';
    }

    class State
    {
    	public int dist = -1;
    	public string str = "";
    	public List<string> adjacentStrs = new List<string>();
    	public State(string str)
    	{
    		this.str = str;
    		for (int i = 0; i < str.Length - 1; i++)
    		{
    			string adjacentStr = "";
    			int changed = (6 - ToInt(str[i]) - ToInt(str[i+1])) % 3;
    			for (int j = 0; j < str.Length; j++)
    			{
    				if (j == i || j == i + 1)
    				{
    					adjacentStr += changed.ToString();
    				}
    				else
    				{
						adjacentStr += str[j].ToString();
    				}
    			}
				this.adjacentStrs.Add(adjacentStr);
    		}
    	}
    }

    // メイン
    static void Main(string[] args)
    {
    	var input = "0";
    	while ((input = Console.ReadLine()) != "0")
    	{
    		states.Clear();
    		queue.Clear();
    		State start = new State(ConvertInput(input));
    		states.Add(start.str, start);
        	Bfs(start);
    	}
    }

    // 幅優先探索
    static void Bfs(State start)
    {
    	start.dist = 0;
    	queue.Enqueue(start);
    	while (queue.Count() > 0)
    	{
    		State state = queue.Dequeue();
    		if (CheckIdEnd(state.str))
    		{
    			System.Console.WriteLine(state.dist);
    			return;
    		}
    		foreach (var str in state.adjacentStrs)
    		{
    			if (!states.ContainsKey(str))
    			{
    				State adjacentState = new State(str);
    				adjacentState.dist = state.dist + 1;
    				states.Add(str, adjacentState);
    				queue.Enqueue(adjacentState);
    			}
    		}
    	}
    	System.Console.WriteLine("NA");
	}

	static string ConvertInput(string input)
	{
		string converted = "";
    	foreach (char s in input)
		{
			switch (s)
			{
				case 'r':
					converted += "0";
					break;
				case 'g':
					converted += "1";
					break;
				case 'b':
					converted += "2";
					break;
			}
		}
		return converted;
	}

	static bool CheckIdEnd(string str)
	{
		char prevNum = str[0];
		foreach (char num in str)
		{	
			if (prevNum != num)
			{
				return false;
			}
			prevNum = num;
			
		}
		return true;
	}
}


Measured memory_bytes: 26048
import re,sys
def S(): return sys.stdin.readline().rstrip()
S = S()
str = 'keyence'
pattern_keyence = []
for i in range(len(str)+1):
    pattern_keyence.append(str[:i]+'.*'+str[i:])
for x in pattern_keyence:
    if re.fullmatch(x,S):
        print('YES')
        break
else:
    print('NO')


Measured memory_bytes: 3188