akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <string>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define jai_shree_ram  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i, a, b, d) for (int i = a; i <= b; i += d)
#define brep(i, a, b, d) for (int i = a; i >= b; i -= d)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define endl '\n'
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
////////////////////////////////

int n, m;
vector <int> g[MAX];
int cnt_in[MAX];
vector <int> root;
int fans = 0, vis[MAX], lx[MAX];


int dfs(int r){
    vis[r] = 1;
    if(!g[r].size()) return 0;

    for(auto y: g[r]){
        if(!vis[y]) lx[y] = dfs(y);
        lx[r] = max(lx[r], lx[y] + 1);
    }
    fans = max(fans, lx[r]);
    return lx[r];
}

int32_t main(){
    jai_shree_ram
    cin >> n >> m;
    rep(i,1,m,1){
        int x, y;
        cin >> x >> y;
        g[x].pb(y);
        cnt_in[y]++;
    }
    rep(i,1,n,1) if(!cnt_in[i]) root.pb(i);
    for(auto r: root){
        int rr = dfs(r);
    }
    cout << fans << endl;
    return 0;
}

Measured memory_bytes: 12800
def main():
    n, m = map(int, input().split())
    x, y = min(n, m), max(n, m)
    if x == 1 and y == 1:
        ans = 1
    elif x == 1:
        ans = y - 2
    else:
        ans = (x - 2) * (y - 2)
    print(ans)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>

#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }

template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
    for (auto& e:t) fill_v(e, v);
}

typedef pair<int, int> P;

class BABC {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        string s;
        cin >> s;
        int sum = 0;
        vector<int> num(s.size());

        char prev = s[s.size() - 1];
        REV(i, s.size() - 2, 0) {
            char c = s[i];
            if (c == 'B') {
                if (prev == 'C') {
                    sum++;
                }
                if (prev == 'A') {
                    sum = 0;
                }
                if (prev == 'B') {
                    sum = 0;
                }
            }
            if (c == 'C') {
                if (prev == 'C') {
                    sum = 0;
                }
            }
            if (c == 'A') {
                if (prev == 'C') {
                    sum = 0;
                }
            }

            prev = c;
            num[i] = sum;
        }

        int res = 0;
        REP(i, s.size()) {
            if (s[i] == 'A') {
                res += num[i];
            }
        }
        cout << res << endl;

    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    BABC solver;
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 2196
#include<cstdio>
int n,x;
int main()
{
	scanf(" %d",&n);
	while(n--)
	{
		scanf(" %d",&x);
		if(x&1)continue;
		if(x%3&&x%5)
		{
			printf("DENIED\n");return 0;
		}
	}
	printf("APPROVED\n");
	return 0;
}

Measured memory_bytes: 128
using System;

public class hello
{
    public static void Main()
    {
        while (true)
        {
            var t = int.Parse(Console.ReadLine().Trim());
            if (t == 0) break;
            var n = int.Parse(Console.ReadLine().Trim());
            var studytotal = 0;
            for (int i = 0; i < n; i++)
            {
                string[] line = Console.ReadLine().Trim().Split(' ');
                studytotal += int.Parse(line[1]) - int.Parse(line[0]);
            }
            if (studytotal >= t) Console.WriteLine("OK");
            else Console.WriteLine(t-studytotal);
        }
    }
}

Measured memory_bytes: 9264
#include <iostream>
typedef long long ll;
using namespace std;

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

ll lcm(ll a, ll b){
    ll g = gcd(a, b);
    return (a / g) * b;
}

int main(void){
    int N;  cin >> N;
    ll T, ans = 1LL;
    for(int i=0; i<N; i++){
        cin >> T;
        ans = lcm(ans, T);
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

#define MAX 100005
#define NIL -1

int p[MAX],lc[MAX],rs[MAX],d[MAX];
int Depth(int n)
{
  int d=0;
  while(p[n]!=NIL)
  {
    n=p[n];
    d+=1;
  }
  return d;
}
void printChildren(int n)
{
  int child = lc[n];
  while(child!=NIL)
  {
    printf("%d",child);
    child=rs[child];
  }
}
int main()
{
  int i,j,id,num,nc,child,leftc,r;
  scanf("%d",&num);
  for(i=0;i<num;i++)
  {
    p[i]=lc[i]=rs[i]=NIL;
  }
  for(i=0;i<num;i++)
  {
    scanf("%d%d",&id,&nc);

    for(j=0;j<nc;j++)
    {
      scanf("%d",&child);
      if(j!=0)
      {
        rs[leftc]=child;
      }
      else
      {
        lc[id]=child;
      }
      leftc=child;
      p[child]=id;
    }
  }
  for(i=0;i<num;i++)
  {
    d[i]=Depth(i);
  }
  for(i=0;i<num;i++)
  {
    j=0;
    printf("node %d: parent = %d, depth = %d, ",i,p[i],d[i]);
    if(p[i]==NIL)printf("root, ");
    else if(lc[i]==NIL)printf("leaf, ");
    else printf("internal node, ");
    printf("[");
    child=lc[i];
    while(child!=NIL)
    {
      if(j!=0)printf(", ");
      printf("%d",child);
      child=rs[child];
      j++;
    }
    printf("]\n");
  }
  return 0;
}


Measured memory_bytes: 3280
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <math.h>

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)

int h;
int w;
char **data;

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

void dfs(int y, int x, char f) {
	int i, j;
	data[y][x] = '.';
	for (i=0; i<4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (0<=nx && nx<w && 0<=ny && ny<h && data[ny][nx] == f)
			dfs(ny, nx, f);
	}
}

int main()
{
	int i, j;
	int res;
	
	while (1) {
		res = 0;
		scanf("%d%d ", &h, &w);
		if (!h && !w) break;
		data = (char **)calloc(h, sizeof(char *));
		for (i=0; i<h; i++)
			data[i] = (char *)calloc(w, sizeof(char));

		for (i=0; i<h; i++)
			for (j=0; j<w; j++)
				scanf("%c ", &(data[i][j]));

		FOR(i,0,h) FOR (j,0,w) {
			if (data[i][j] != '.') {
				dfs(i, j, data[i][j]);
				res++;
			}			
		}	

		printf("%d\n", res);
		
		for (i=0; i<h; i++)
			free(data[i]);
		free(data);
	}

	return 0;
}

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

long long gcd(long long aaa,long long bbb) {
    if(bbb==0) {
        return aaa;
    }
    return gcd(bbb,aaa%bbb);
}

int main (){ 
    
    string s,ans;
    cin >> s;
    for(int i = 0; i < s.size(); i++) {
        if(s[i] == 'B') {
            ans = ans.substr(0,ans.size()-1);
        }else{
            ans += s[i];
        }
    }
    cout << ans << endl;
    return 0;
}


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

using namespace std;

int main(int argc, const char *argv[]) {
  int a, b, c, k;
  cin >> a >> b >> c >> k;

  int max_n, rest;
  if (a >= b && a >= c) {
    max_n = a;
    rest = b + c;
  } else if (b >= a && b >= c) {
    max_n = b;
    rest = a + c;
  } else {
    max_n = c;
    rest = a + b;
  }

  for (int i = 0; i < k; ++i) {
    max_n *= 2;
  }

  cout << max_n + rest << '\n';
  return 0;
}

Measured memory_bytes: 256
def main():
    H, W = map(int, input().split())
    a = [[0] * W]
    for _ in range(H):
        ai = list(map(int, input().split()))
        ai.insert(0, 0)
        a.append(ai)
    N = 0
    yx = []
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if a[i][j] % 2 == 0:
                continue
            if W >= j + 1:
                a[i][j] -= 1
                a[i][j + 1] += 1
                N += 1
                yx.append([i, j, i, j + 1])
            elif H >= i + 1:
                a[i][j] -= 1
                a[i + 1][j] += 1
                N += 1
                yx.append([i, j, i + 1, j])
    print(N)
    for row in yx:
        print(' '.join(list(map(str, row))))


main()


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

using namespace std;

const int maxn=405;

vector < long long > l;
long long dp[maxn][maxn];
long long pref[maxn];


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	int x;
	for(int i=0; i<n; i++){
		cin >> x;
		l.push_back(x);
		pref[i+1]=pref[i]+x;
	}
	long long mini;
	for(int i=0; i<n; i++){
		for(int j=0; j<n-i; j++){
			if(i==0){
				dp[j][j+i]=0;
			}
			else{
				mini=1e18;
				for(int k=j; k<j+i; k++){
					mini=min(mini, dp[j][k]+dp[k+1][j+i]);
				}
				dp[j][j+i]=mini+pref[j+i+1]-pref[j];
			}
		}
	}
	cout << dp[0][n-1] << '\n';
	return 0;
}


Measured memory_bytes: 1536
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
#define ll long long
#define pb push_back

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

	std::cout << std::fixed;
    std::cout << std::setprecision(16);

	int A,B,H,M;

	cin>>A>>B>>H>>M;

	double h_ang = 0.5*(H*60 + M);
	double m_ang = 6*M;

	double angle = abs(h_ang - m_ang);

	angle = min(360-angle,angle);

	//cout<<angle<<endl;

	double rad = M_PI/180;
	rad = angle * rad;

	//cout<<rad<<endl;

	//cout<<cos(rad)<<endl;

	double cos_val = cos(rad);

	double res = A*A + B*B - 2*A*B*cos_val;

	res = sqrt(res);

	cout<<res<<endl;

	return 0;
}

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

typedef long long ll;
typedef pair<int, int> P;

int MOD=1e9+7;
ll INF=1e18;

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

int main(){
    int n, cnt=0; cin>>n;
    for(int i=0; i<n; i++){
        int a; cin>>a;
        if(a%2==1) cnt++;
    }
    cout << (cnt%2==0?"YES":"NO") << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include <fstream>
#include <time.h>
#include <iterator>


#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 REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<endl;
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MOD = (ll)1000000000 + 7;
const ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int>P;

int N, M;
int k[11], s[11][11];
int p[11];
signed main() {
	cin >> N >> M;
	REP(i, M) {
		cin >> k[i];
		REP(j, k[i]) {
			cin >> s[i][j];
			s[i][j]--;
		}
	}
	REP(i, M) {
		cin >> p[i];
	}
	int ans = 0;
	for (int bit = 0; bit < (1 << N); ++bit)
	{
		bool ok = true;
		REP(i, M) {
			int cnt = 0;
			REP(j, k[i]) {
				if (bit&(1 << s[i][j]))cnt++;
			}
			if (cnt % 2 != p[i]) {
				ok = false;
			}
		}
		if (ok)ans++;
	}
	print(ans);
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <queue>
#include <stack>
#define rep(i, s, g) for (i = s; i <= g; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1e9 + 7;

struct factorial
{
    vector<ll> fac, finv, inv;

    factorial(int n)
    {
        int i;

        rep(i, 0, 1)
        {
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }

        rep(i, 2, n)
        {
            fac.push_back(fac[i - 1] * i % MOD);
            inv.push_back(MOD - inv[MOD % i] * (MOD / i) % MOD);
            finv.push_back(finv[i - 1] * inv[i] % MOD);
        }
    }

    ll comb(int n, int k)
    {
        if (n < k)
        {
            return 0;
        }

        if (n < 0 || k < 0)
        {
            return 0;
        }

        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
};

int main(void)
{
    int N, K;
    ll a[100010];
    int i, j;
    ll maxs = 0;
    ll mins = 0;

    cin >> N >> K;

    factorial f(N);

    rep(i, 0, N - 1)
    {
        cin >> a[i];
    }

    sort(a, a + N);

    rep(i, 0, N - 1)
    {
        maxs += (a[i] * f.comb(i, K - 1)) % MOD;
        maxs -= (a[i] * f.comb(N - i - 1, K - 1)) % MOD;
        maxs %= MOD;
    }

    cout << maxs % MOD << endl;
}

Measured memory_bytes: 3680
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 999999999;
const double pi = acos(-1);

#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define mod(i) (i%(ll)(1e9 + 7))

ull dp[55][55][3000] = {};

int main(){
	int n,a,x[55] = {};
    ull ans = 0;
    
	cin >> n >> a;
	rep(i,0,n)cin >> x[i];

    dp[0][0][0] = 1;
    
	rep(i,0,n){
	    rep(j,0,n){
	        rep(k,0,2800){
	            dp[i+1][j+1][k+x[i]] += dp[i][j][k];
                dp[i+1][j][k] += dp[i][j][k];
	        }
        }
    }

    rep(i,1,n+1){
        ans += dp[n][i][a*i];
    }

    cout << ans << endl;
    
	return 0;
}

Measured memory_bytes: 66688
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))


def main():
    n, m = map(int, input().split())
    edges = [[] for _ in range(n)]
    for _ in range(m):
        s, t = map(int, input().split())
        edges[s-1].append(t-1)
    
    ans = n + 1
    for i in range(n - 1):
        # 頂点 i からの辺を消す
        dp = [0 for _ in range(n)]

        for j in range(n - 2, -1, -1):
            totale, maxe = 0, 0
            for e in edges[j]:
                totale += dp[e]
                maxe = max(maxe, dp[e])
        
            if i == j and len(edges[j]) > 1:
                dp[j] = (totale - maxe) / (len(edges[j]) - 1) + 1
            else:
                dp[j] = totale / len(edges[j]) + 1
        
        ans = min(ans, dp[0])
    
    print("%.10f" % ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 53980
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7,ohara=1e6+10;
const ll LINF=1e18;
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl
#define doublecout(a) cout<<fixed<<setprecision(15)<<a<<endl;
#define Cerr(x) cerr<<(x)<<endl
#define fi first
#define se second
#define P pair<ll,ll> 
#define m_p make_pair
#define V vector<ll> 

ll n,cnt,ans,a[ohara],b[ohara],c[ohara],d,tmp,tmpp,m,h,w,x,y,sum,pos,k;
ld doua;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"),s;
bool fl;
struct edge{int to,cost;};
priority_queue<ll,vector<ll>,greater<ll>> que;
P p[ohara];

//-------------------------↓↓↓↓↓↓------------------------

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

    cin>>n>>m;
    rep(i,n)cin>>a[i];
    rep(i,m)cin>>b[i]>>c[i];
    rep(i,n)que.push(a[i]);
    rep(i,m){
        p[i]=m_p(c[i],b[i]);
    }
    sort(p,p+m);
    reverse(p,p+m);
    rep(i,m){
        rep(j,p[i].se){
            if(que.top()>=p[i].fi)break;
            que.push(p[i].fi);
            que.pop();
        }
    }
    while(!que.empty()){
        ans+=que.top();
        que.pop();
    }
    Cout(ans);
    return 0;
}


Measured memory_bytes: 10996
#include <stdio.h>
#define MAX_N 100000
 
int A[MAX_N];
 
void swap(int *x, int *y) {
    int tmp = *x;
    *x = *y;
    *y = tmp;
}
 
int partition(int p, int r) {
    int x = A[r];
    int i = p-1;
 
    int j;
    for (j = p; j < r; j++) {
        if (A[j] <= x) {
            i++;
            swap(&A[i], &A[j]);
        }
    }
    swap(&A[i+1], &A[r]);
 
    return i+1;
}
 
int main(void) {
    int i, n;
    scanf("%d", &n);
 
    for (i = 0; i < n; i++) {
        scanf("%d", &A[i]);
    }
 
    int q = partition(0, n-1);
    for (i = 0; i < n; i++) {
        if (i) printf(" ");
        if (i == q) printf("[");
        printf("%d", A[i]);
        if (i == q) printf("]");
    }
    putchar('\n');
 
    return 0;
}


Measured memory_bytes: 2132
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int>(1000,10000)(rng)

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

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

string to_upper(string a)
{
    for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
    return a;
}
 
string to_lower(string a)
{
    for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
    return a;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    string s;
    int n=0;
    cin>>s;
    for (auto c : s)
        n+=(c-'0');
    if (n%9)
        cout<<"No";
    else
        cout<<"Yes";
    return 0;
}

Measured memory_bytes: 3712
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iterator>
#include <algorithm>
#include <map>
#include <string.h>
#include <vector>
#include <cmath>
#include <deque>
#include <iomanip>
#include <regex>

using namespace std;
#define range(a) a.begin(), a.end()
#define rep(i, c) for(int i = 0; i < (int)c; i++)
typedef long long ll;

template <typename T> ostream& operator<<(ostream& out, vector<T> &v){
  for (int i = 0; i < (int)v.size(); i++) {
    out<<v[i]<<' ';
  }
  return out;
}

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

  string a, b; cin >> a >> b;
  int ans = 0;
  if(a == b){
    cout << "0" << endl;
    return 0;
  }else{
    int len = a.length();
    for(int i = 0; i < len; i++){
      if(a[i] != b[i]){
        ans++;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 3800
#include <iostream>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <limits>
#include <numeric>
#include <algorithm>
#include <functional>

using namespace std;

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

	long long N;
	cin >> N;

	cout << N * (N - 1) / 2;

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
using namespace std;

#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 400000;







int main() {

	ll p2[MAX];
	ll p3[MAX];

	p2[0] = 1;
	p3[0] = 1;
	for (int i = 1; i < MAX; i++) {
		p2[i] = p2[i - 1] * 2 % MOD;
		p3[i] = p3[i - 1] * 3 % MOD;
	}


	string s;
	cin >> s;

	int N = s.size();

	ll ans = 0;

	int one = 0;

	for (int i = 0; i < N; i++) {

		if (s[i] == '1') { 
			ans = (ans +p2[one] * p3[N-1 - i]%MOD)%MOD;
			one++;
		}

	}

	ans = (ans + p2[one]) % MOD;


	cout << ans;
	

	system("PAUSE");

}

Measured memory_bytes: 6912
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<iomanip>
#include<set>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define brep(index,num) for(int index=num-1;index>=0;index--)
#define brep1(index,num) for(int index=num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
int main(){
	string S;
	scan(S);
	int N=S.size();
	int ans=0;
	rep(i,N/2){
		if(S[i]!=S[N-1-i]) ans++;
	}
	prin(ans);
	return 0;
}


Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = 998244353;

int  N, M;
long long dat[2000009];

long long quickpow(long long b, long long e)
{
	return e ? ((e & 1 ? b : 1)*quickpow((b*b) % MOD, e >> 1)) % MOD : 1;
}

long long combi(int n, int m)
{
	long long retval = dat[n];
	retval = retval * quickpow(dat[n - m], MOD - 2);
	retval = retval % MOD;
	retval = retval * quickpow(dat[m], MOD - 2);
	retval = retval % MOD;
	return retval;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N >> M;
	dat[0] = 1;
	for (int i = 1; i < 2000009; i++) {
		dat[i] = dat[i - 1] * i;
		dat[i] = dat[i] % MOD;
	}
	int lb = min(N, M);
	long long ans = 0, tmp;
	for (int i = 0; i <= lb; i++) {
		if (3 * M - i >= 0 && (3 * M - i) % 2 == 0) {
			tmp = combi((3 * M - i) / 2 + N - 1, N - 1);
			tmp = tmp * combi(N, i);
			tmp = tmp % MOD;
			ans += tmp;
			ans = ans % MOD;
		}
	}
	tmp = combi(M + N - 1, N - 1);
	tmp = tmp + MOD - combi(M + N - 2, N - 2);
	tmp = tmp * N;
	tmp = tmp % MOD;
	ans = ans + MOD - tmp;
	ans = ans % MOD;
	cout << ans << E;
	//system("pause");
	return 0;
}

Measured memory_bytes: 15872
#include <iostream>

int main()
{
  int a,b;
  
  std::cin >> a >> b;
  if(a<b)
    {
      std::cout<<"a < b"<<std::endl;
    }
  else if(a>b)
    {
      std::cout<<"a > b"<<std::endl;
    }
  else
    {
      std::cout<<"a == b"<<std::endl;
    }

  return 0;
}



Measured memory_bytes: 3084
#include <stdio.h>
#define N 2000001
int A[N], B[N], C[N];
int n;
main() {
  int i, j;
  
  scanf("%d", &n);
  for(i = 1; i < n + 1; i++) {
    scanf("%d", &A[i]);
  }
  
  for(i = 0; i < 10001; i++) {
    C[i] = 0;
  }
  
  for(j = 1; j < n + 1; j++) {
    C[A[j]]++;
  }
  
  for(i = 1; i < 10001; i++) {
    C[i] = C[i] + C[i - 1];
  }

  for(j = n; j >= 1; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
  
  for(i = 1; i < n + 1; i++) {
    if(i == n) {
      printf("%d\n", B[i]);
    }
    else {
      printf("%d ", B[i]);
    }
  }
  return 0;
}

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

int way[51] = {};

int main(int , char **)
{
	int N,M;
	cin >> N >> M;
	for(int i=0;i<M;i++){
		int a,b;
		cin >> a >> b;
		a--;b--;
		way[a]++;
		way[b]++;
	}

	for(int i=0;i<N;i++){
		printf("%d\n",way[i]);
	}

	return 0;
}

Measured memory_bytes: 256
Scanf.scanf "%d %d" (fun n x ->
    let m = Array.init n (fun _ -> Scanf.scanf " %d" (fun m -> m)) in
    Array.sort compare m;
    let s = Array.fold_left (+) 0 m in
    let r = x - s in
    Printf.printf "%d\n" @@ n + r / m.(0)
)

Measured memory_bytes: 384
import sys
from fractions import gcd

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

X, Y = inm()


def solve():
    if X % Y == 0:
        return -1
    return X * (Y + 1)


print(solve())


Measured memory_bytes: 10364
#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout<<((f)?"Yes":"No")<<endl
#define YORN(f) cout<<((f)?"YES":"NO")<<endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define println(x) cout << x << endl
using namespace std;
int main()
{
    int m;
    cin >> m;
    cout << 24 + 24 - m << endl;
    return 0;
}

Measured memory_bytes: 3588
#include<bits/stdc++.h>
using namespace std;
int main()
{
  long long int n,a,b,i,tem,v,sum=0;
  cin>>n>>a>>b;
  for(i=1;i<=n;i++)
  {
    if(i>1)tem=v;
    cin>>v;
    if(i>1)
    {
      if(abs(v-tem)*a>b)
      sum+=b;
    else sum+=abs(v-tem)*a;
    tem=v;
    }
  }
  cout<<sum<<endl;
  return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  set<int> to, from;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    if (a == 1)
      to.insert(b);
    else if (b == n)
      from.insert(a);
  }
  bool possible = false;
  for (int c : to) {
    if (from.count(c)) {
      possible = true;
      break;
    }
  }
  cout << (possible ? "" : "IM") << "POSSIBLE\n";

  return 0;
}


Measured memory_bytes: 9600
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int tap = 1;
    int atap = 0;
    while(tap<b){
      tap--;
      tap+=a;
      atap++;
    }
    System.out.println(atap);
  }
}


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

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

int Result_int;
long Result_lng;
int A,B,C;

int main() {
    int r_min,r_max;
    cin >> A >> B >> C;
    r_min = min(A,B);
    r_max = max(A,B);
    if (r_min > C || r_max < C) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
}





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

int main() {
  int H, W;
  cin>>H>>W;
  vector<string> grid(H);
  for(int i=0;i<H;i++){
    string line;
    cin>>line;
    grid[i]=line;
  }
  string white(W,'.');
  vector<int> row(H),column(W);

  for(int i=0;i<H;i++){
    if(grid[i]!=white){
      row[i]=1;
    }
  }

  for(int i=0;i<W;i++){
    for(int j=0;j<H;j++){
      if(grid[j][i]=='#'){
        column[i]=1;
      }
    }
  }

  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(row[i]==1 && column[j]==1){
        cout<<grid[i][j];
      }
    }
    if(row[i]==1){
      cout<<endl;
    }
  }
}

Measured memory_bytes: 256
public class Main {
	public static void main(String args[]) {

		java.util.Scanner sc = new java.util.Scanner(System.in);
		long N = sc.nextLong();
		String s = sc.next();

		String ss = s.substring(0, (int)N/2);

		if (s.equals(ss + ss)) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}

}

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

using namespace std;

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

	int ans = 0;
	if (K <= A) {
		ans = K;
	} else if (K <= A + B) {
		ans = A;
	} else {
		ans = A - (K - (A + B));
	}

	cout << ans << endl;

	return 0;
}


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

#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif

signed main(){

  int n; cin >> n;

  int ans[501][501];
  for(int i = 0; i < n; i++){
    for(int j = i+1; j < n; j++){
      int bit = 0;
      while((i >> bit & 1) == (j >> bit & 1)){
        bit++;
      }
      ans[i][j] = bit;
    }
  }

  for(int i = 0; i < n; i++){
    for(int j = i+1; j < n; j++){
      cout << ans[i][j] + 1 << " ";
    }
    cout << "\n";
  }

  return 0;
}


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

int main() {
  int n, step = 1;
  cin >> n;
  vector<int> mochi(n);
  rep(i, n)
    cin >> mochi.at(i);
  
  for(int i = 1; i < n; i++){
    for(int j = 0; j < i; j++){
      if(mochi.at(i) > mochi.at(j)){
        int tmp;
        tmp = mochi.at(i);
        mochi.at(i) = mochi.at(j);
        mochi.at(j) = tmp;
      }
    }
  }
  
  int base;
  base = mochi.at(0);
  rep(i, n){
    if(base > mochi.at(i)){
      step++;
      base = mochi.at(i);
    }
  }
  
  cout << step << endl;

}


Measured memory_bytes: 256
def inpl():
    return list(map(int, input().split()))


N, M = inpl()
AB = [inpl() for i in range(M)]

n1 = []
nN = []

for a, b in AB:
    if a == 1:
        n1.append(b)
    elif b == N:
        nN.append(a)

n1 = set(n1)
nN = set(nN)

print('POSSIBLE' if len(n1 & nN) > 0 else 'IMPOSSIBLE')

Measured memory_bytes: 60252
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int Query(int ans)
{
    char ret[20];
    printf("%d\n", ans);
	fflush(stdout);
    scanf("%s", ret);
    if(ret[0] == 'V') exit(0);
    return ret[0] == 'M' ? 1 : 0;
}
int main()
{
	scanf("%d",&n);
	int l=0,r=n-1,f=Query(0);
	while(l<=r)
	{
		int mid=(l+r)>>1;
		int p=Query(mid);
		if(p==f)
		{
			if(mid%2) r=mid-1;
			else l=mid+1;
		}
		else
		{
			if(mid%2) l=mid+1;
			else r=mid-1;
		}
	}
}

Measured memory_bytes: 760
#include<bits/stdc++.h>
#define up(l,r,i) for(int i=l;i<=r;i++)
#define dn(l,r,i) for(int i=l;i>=r;i--)
using namespace std;

typedef long long LL;
const int INF =2147483647;
int qread(){
	int w=1,c,ret;
	while((c=getchar())> '9'||c< '0')
	w=(c=='-'?-1:1); ret=c-'0';
	while((c=getchar())>='0'&&c<='9')
	ret=ret*10+c-'0';
	return ret*w;
}
const int MAXN =1e5 +3;
int fa[MAXN],sz[MAXN],n,m;
int A[MAXN],B[MAXN];
LL ans[MAXN],cnt;
int getfa(int x){
	return fa[x]==x?x:fa[x]=getfa(fa[x]);
}
void qwrite(LL x){
	if(x>9) qwrite(x/10);
	putchar('0'+x%10);
}
int main(){
	n=qread(),m=qread();
	up(1,n,i) fa[i]=i,sz[i]=1;
	up(1,m,i) A[i]=qread(),B[i]=qread();
	ans[m]=(LL)n*(n-1)/2;
	dn(m-1,1,i){
		int u=A[i+1],v=B[i+1],fau=getfa(u),fav=getfa(v);
		if(fav!=fau){
			cnt+=(LL)sz[fau]*sz[fav];
			fa[fau]=fav,sz[fav]+=sz[fau];
		}
		ans[i]=(LL)n*(n-1)/2-cnt;
	}
	up(1,m,i) qwrite(ans[i]),putchar('\n');
	return 0;
}


Measured memory_bytes: 3712
#include <iostream>
using namespace std;
int main(){
  int x,y;
  while(true){
      cin  >> x >>y ;
      if(x == 0 && y == 0){
          break;
      }
 if(x>y){
     cout <<y << " " <<x<< endl;
 }
 else {
     cout << x << " "<<y<< endl;
 }
 }
 }



Measured memory_bytes: 2996
program main
  implicit none
  character(len=10) :: S
  integer :: N
  character(:),allocatable :: stmp
  integer :: out
  integer :: i, j, k
  logical :: OK
  read(*,*) S
  N = len_trim(S)
  out = 0
  do i = 1, N
     do j = i,N
        stmp = S(i:j)
        OK = .true.
        !write(*,*) stmp
        do k = 1,len(stmp)
           if ( stmp(k:k) =='A' .or. &
                stmp(k:k) =='C' .or. &
                stmp(k:k) =='G' .or. &
                stmp(k:k) =='T'                 ) then
              cycle
           else
              OK = .false.
           end if
        end do
        if ( OK ) then
           if ( out < len(stmp)) then
              out = len(stmp)
           end if
        end if
        deallocate(stmp)
     end do
  end do
  write(*,'(i0)') out
  stop
end program main


Measured memory_bytes: 256
import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container;
import std.range;
import core.stdc.stdlib;
import std.math;

void main() {
  int a, b;
  readf("%d %d\n", &a, &b);
  int nokori = a - 2*b;
  writeln(nokori<0 ? 0: nokori);
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <numeric>
#include <complex>
#define rep(i,n) for(int (i) = 0; (i) < (n); ++(i))
using namespace std;
typedef long long ll;

int main(){
    int n, r;
    cin >> n >> r;
    if(n >= 10) cout << r << endl;
    else cout << r + 100*(10-n) << endl;
    
    return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProblemA
{
    class Program
    {
        static void Main(string[] args)
        {
            var read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var A = read[0];
            var P = read[1];
            Console.WriteLine((3 * A + P) / 2);
        }
    }
}


Measured memory_bytes: 13268
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)


import heapq
BIGDI = 10**18
# N nodes  M arcs  go from S to T (node# starts from 1)
n,m = inm()
s = 1
dst = [{} for i in range(n+1)]
for i in range(m):
    u,v = inm()
    dst[u][v] = 1
    dst[v][u] = 1

cost = [BIGDI]*(n+1)
q = [(0,s)]
while len(q)>0:
    d,p = heapq.heappop(q)
    if cost[p]<=d:
        continue
    cost[p] = d
    for v in dst[p]:
        heapq.heappush(q,(d+dst[p][v], v))

# cost[t] is the answer
ans = [-1]*(n+1)
for i in range(1,n+1):
    for j in dst[i]:
        if cost[j] == cost[i]+1:
            ans[j] = i
print('Yes')
for i in range(2,n+1):
    print(ans[i])


Measured memory_bytes: 154216
import java.util.Scanner;
class Main{
	public static void main(String[] args) {
		Scanner kbd = new Scanner(System.in);
		int w = kbd.nextInt();
		int h = kbd.nextInt();
		int x = kbd.nextInt();
		int y = kbd.nextInt();
		int r = kbd.nextInt();
		if ((x-r)<0 || (y-r) <0 || (x+r) >w || (y+r) >h)
			System.out.println("No");
		else
			System.out.println("Yes");
	}

}

Measured memory_bytes: 26396
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<string.h>

const int MAXN = 1000000000;
const int MOD = 1000000007;

int power(long long m, int n) {
	long long rs = 1;
	while (n) {
		if (n & 1) {
			rs = ((rs % MOD) * m) % MOD;
		}
		m = ((m % MOD) * (m % MOD)) % MOD;
		n >>= 1;
	}
	
	return rs % MOD;
}

int main() {
	int m, n;
	scanf("%d%d", &m, &n);
	printf("%d\n", power(m, n));
	return 0;
}


Measured memory_bytes: 2752
x,y=map(int,input().split())
if y%2!=0:
  print('No')
  exit(0)

a=y//2

while a>=0:
  if x-a<0:
    a-=1
    continue
  if 2*a+4*(x-a)==y:
    print('Yes')
    exit(0)
  a-=1
print('No')


Measured memory_bytes: 61640
N,M = map(int, input().split())
drink_list = [list(map(int, input().split())) for _ in range(N)]

drink_list.sort(key=lambda x: x[0])
#print(drink_list)

_cnt=0
_sum=0
i=0
if N==1 or drink_list[i][1]>=M:
    _sum=drink_list[i][0]*M
else:
    while _cnt<=M:
        if _cnt+drink_list[i][1]>=M:
            break
        else:
            _sum+=drink_list[i][0]*drink_list[i][1]
            _cnt+=drink_list[i][1]
            i+=1
        #print(_sum,_cnt,i)
    _sum+=drink_list[i][0]*(M-_cnt)

print(_sum)

Measured memory_bytes: 64600
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

  string s;
  cin >> s;

  reverse(s.begin(), s.end());
  s += '0';

  ll keta = s.size();

  int dp[keta + 1][2];
  for (int i = 0; i < keta + 1; i++) {
    for (int j = 0; j < 2; j++) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;
  for (int i = 0; i < keta; i++) {
    for (int j = 0; j < 2; j++) {
      int x = s[i] - '0';

      x += j;

      for (int a = 0; a < 10; a++) {
        int ni = i + 1;
        int nj = 0;
        int b = a - x;
        if (b < 0) {
          nj = 1;
          b += 10;
        }
        dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
      }
    }
  }

  int ans = dp[keta][0];

  cout << ans << endl;

  return 0;
}

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

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  if (A >= K) {
    cout << K << endl;
  }
  else if (A < K && K <= A + B) {
    cout << A << endl;
  }
  else {
    cout << A - (K - (A + B)) << endl;
  }
}

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

int main(){
	int W, H, x, y, r;

	cin >> W >> H >> x >> y >> r;
	if (y - r >= 0) {
		if (y + r <= H){
			if (x - r >= 0){
				if (x + r <= W){
					cout << "Yes" << endl;
				}
				else{
					cout << "No" << endl;
				}
			}
			else{
				cout << "No" << endl;
			}
		}
		else{
			cout << "No" << endl;
		}
	}
	else{
		cout << "No" << endl;
	}

	return 0;
}



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

using namespace std;
typedef long long LL;

#ifdef BTK
#define DEBUG if(1)
#else
#define CIN_ONLY if(1)
struct cww {cww() {CIN_ONLY{ios::sync_with_stdio(false); cin.tie(0);}}
}star;
#define DEBUG if(0)
#endif

#define ALL(v) (v).begin(),(v).end()
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l, T r){bool a = l>r; if (a)l = r; return a;}
template <typename T>inline bool chmax(T &l, T r){bool a = l<r; if (a)l = r; return a;}
template <typename T>istream& operator>>(istream &is, vector<T> &v){for (auto &it : v)is >> it;return is;}

class range {private: struct I { int x; int operator*() { return x; }bool operator!=(I& lhs) { return x<lhs.x; }void operator++() { ++x; } }; I i, n;public:range(int n) :i({ 0 }), n({ n }) {}range(int i, int n) :i({ i }), n({ n }) {}I& begin() { return i; }I& end() { return n; }};

typedef vector<int> V;
typedef vector<V> VV;

constexpr LL INF = 1e18;
LL ret = INF;

int N;
int a[112345];
int b[112345];
int c[112345];
VV g;
int isAllBlack[112345];

VV tree;
vector<vector<LL>> depth;
LL E = 0;
int root=-1;
int deg[112345];
void dfs1(int v,int p=-1) {
	isAllBlack[v] = 1;
	deg[v] = (p != -1);
	for (int e : g[v]) {
		const int u = v ^ a[e] ^ b[e];
		if (u == p)continue;
		dfs1(u, v);
		isAllBlack[v] &= isAllBlack[u];
		isAllBlack[v] &= c[u];
	}
	for (int e : g[v]) {
		const int u = v ^ a[e] ^ b[e];
		if (u == p)continue;
		if (isAllBlack[u] && c[u])continue;
		E+=2;
		tree[v].push_back(u);
		//自分の親へ向かう辺も含む
		depth[v].push_back(*max_element(ALL(depth[u])) + 1);
		deg[v]++;
	}
	int btk = (deg[v] % 2 == c[v]) ? 1 : -1;
	for (auto &it : depth[v])it += btk;
	depth[v].push_back(1);
	if (isAllBlack[v] && c[v]);
	else E += (deg[v] % 2 == c[v]);
}
void dfs2(int v,int p = -1, LL pl = 0) {
	int b = (deg[v] % 2 == c[v]) ? 1 : -1;
	LL ans = pl;
	for (int u : tree[v]) {
		chmax(ans,*max_element(ALL(depth[u])));
	}
	ans = E - ans;
	ans -= b;
	chmin(ret, ans);
	auto d = depth[v];
	if(p!=-1)d.push_back(pl + b + 1);
	sort(ALL(d));
	for (int i:range(tree[v].size())) {
		const int u = tree[v][i];
		if (d.back() != depth[v][i]) {
			dfs2(u, v, d.back());
		}
		else {
			dfs2(u, v, d[d.size() - 2]);
		}
	}
}


int main() {
	cin >> N;
	g.resize(N);
	tree.resize(N);
	depth.resize(N);
	for (int i : range(N - 1)) {
		cin >> a[i] >> b[i]; a[i]--; b[i]--;
		g[a[i]].push_back(i);
		g[b[i]].push_back(i);
	}
	string s; cin >> s;
	for (int i : range(N)) {
		c[i] = (s[i] == 'B');
		if (c[i] == 0)root = i;
	}
	if (root == -1) {
		cout << 0 << endl;
		return 0;
	}
	dfs1(root);
	if (E == 1) {
		cout << 1 << endl;
		return 0;
	}
	dfs2(root);
	cout << ret << endl;
	return 0;
}


Measured memory_bytes: 35920
package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 3
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	s := getNextString(scanner)
	s = fmt.Sprintf("0%s", s)
	n := len(s)
	ysp48 := make([][2]int, n+1)
	for i := 0; i < n; i++ {
		ysp48[i][0] = math.MaxInt32 >> 1
		ysp48[i][1] = math.MaxInt32 >> 1
	}
	ysp48[n][1] = math.MaxInt32 >> 1
	for i := n - 1; i >= 0; i-- {
		num := int(s[i] - '0')
		if ysp48[i][0] > num+ysp48[i+1][0] {
			ysp48[i][0] = num + ysp48[i+1][0]
		}
		if num > 0 && ysp48[i][1] > (10-num)+ysp48[i+1][0]+1 {
			ysp48[i][1] = (10 - num) + ysp48[i+1][0] + 1
		}
		if num < 9 && ysp48[i][0] > num+ysp48[i+1][1] {
			ysp48[i][0] = num + ysp48[i+1][1]
		}
		if ysp48[i][1] > (9-num)+ysp48[i+1][1] {
			ysp48[i][1] = (9 - num) + ysp48[i+1][1]
		}

	}
	ans := ysp48[0][0]

	fmt.Fprintln(writer, ans)
}


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

int main() {
    int K;
    cin >> K;
    int sum = 0;
    for (int a = 1; a <= K; a++) {
        for (int b = 1; b <= K; b++) {
            for (int c = 1; c <= K; c++) {
                sum += gcd(gcd(a, b), c);
            }
        }
    }
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 3624
N=int(input())
A,B=map(int,input().split())
P=[int(i) for i in input().split()]

x = len([0 for i in P if i<=A])
y = len([0 for i in P if i>=A+1 and i<=B])
z = len([0 for i in P if i>=B+1])

print(min(min(x,y),z))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RES(a, n) (a).resize(n)
ll n, q;
vl a, b, p, x;
vl cost;
vll G;
vector<bool> seen;
void dfs(ll v){
	seen[v] = true;
	for(auto nv : G[v]){
		if(seen[nv]) continue;
		cost[nv] += cost[v];
		dfs(nv);
	}
}
int main() {
	cin >> n >> q;
	RES(a, n-1);
	RES(seen, n);
	RES(G, n);
	RES(b, n-1);
	RES(cost, n);
	RES(p, q);
	RES(x, q);
	REP(i, n-1){
		cin >> a[i] >> b[i];
		G[a[i]-1].push_back(b[i]-1);
		G[b[i]-1].push_back(a[i]-1);
	}
	REP(i, q){
		cin >> p[i] >> x[i];
		cost[p[i]-1] += x[i];
	}
	dfs(0);
	REP(i, n){
		cout << cost[i] << " ";
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 31396
#include <iostream>
using namespace std;
int main(void){
    int n,a,w=0,ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a;
        if(a==i)w++;
        if(a!=i || i==n){
            ans+=(w+1)/2;
            w=0;
        }
    }
    cout<<ans<<endl;
}


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

// GNU C Compiler
//template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

// type
using ll = int_fast64_t;
using ull = uint_fast64_t;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
//using pair<T1, T2> = p12;

//container
#define PB emplace_back
#define MP make_pair 
#define SZ(a) int((a).size())
#define ALL(v) v.begin(),v.end()

//repetition
#define REP(n) for(int i=0; i<(n); i++)
#define REPj(n) for(int j=0; j<(n); j++)
#define REPinit(i,a,n) for(int i=(a); i<(n); i++)
#define REPinitj(j,a,n) for(int j=(a); j<(n); j++)

//algorithm
//#define SORT(c) sort((c).begin(),(c).end())

//constant

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//Optimizer
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/* 
	add utility Algorithm
*/

int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<bool> multipleTable(101, false);
    REPinit(i, 1, 10) {
        REPinit(j, 1, 10) {
            multipleTable[i * j] = true;
        }
    }

    int N;
    cin >> N;

    cout << ((multipleTable[N] ? "Yes" : "No")) << endl;

    return 0;
}


Measured memory_bytes: 3636
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    s = []
    ord = [-1 for i in range(n + 1)]
    rep = 1
    pos = 1
    while ord[pos] == -1:
        ord[pos] = len(s)
        s.append(pos)
        pos = A[pos - 1]
        rep += 1

    rep = len(s) - ord[pos]
    # l: ループが始まる点
    l = ord[pos]
    if k < l:
        print(s[k])
    else:
        # ループが始まるまでずらす
        k -= l
        k %= rep
        print(s[l + k])
resolve()

Measured memory_bytes: 32240
N, A, B = map(int, input().split())

def calsum(x):
    a = 0
    while x > 0:
        a += x % 10
        x = x // 10
    return a
ans = 0
for i in range(N+1):
    if A <= calsum(i) and calsum(i) <= B:
        ans += i
print(ans)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
#include<iomanip>
#include<numeric>

using namespace std;
using ll = long long;
constexpr int mo = 1e9+7;
constexpr int  mod = mo;
constexpr ll inf = 1ll<<62;
int main(){
    int n;
    cin>>n;
    vector<int> v(n);
    for(auto & a : v) cin>>a;
    ll ans = 0;
    for(auto a  :v){
        ans = ans ^ a;
    }
    if( ans == 0) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}



Measured memory_bytes: 640
x, y = map(int, input().split())

a = [1, 3, 5, 7, 8, 10, 12]
b = [4, 6, 9, 11]
c = [2]
if (x in a and y in a) or (x in b and y in b) or ((x in c and y in c)):
    print("Yes")
else:
    print("No")

Measured memory_bytes: 61784
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int main()
{
	scanf("%d",&n);
	int t,x,y;
	bool tmp=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&t,&x,&y);
		if(t<x+y||(t-x-y)%2!=0)
			{
				tmp=0; break;
			}
	}
	if(tmp) printf("Yes");
	else printf("No");
	return 0;
}

Measured memory_bytes: 128