akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner


object Main {

  case class RR(representative: Long) extends AnyVal {
    def +(that: RR): RR = RR((this.representative + that.representative) % RR.mod)

    def -(that: RR): RR = RR((this.representative - that.representative + RR.mod) % RR.mod)

    def *(that: RR): RR = RR((this.representative * that.representative) % RR.mod)

    def unary_- : RR = RR((-this.representative + RR.mod) % RR.mod)
  }


  object RR {
    val mod: Long = (1e9 + 7).toLong

    val _0: RR = RR(0)
    val _1: RR = RR(1)
  }


  def read() = {
    val sc = new Scanner(System.in)
    val n, m = sc.nextInt()
    val a = IndexedSeq.fill(m)(sc.nextInt())

    (n, m, a)
  }

  def solve(n: Int, m: Int, a: IndexedSeq[Int]): Long = {
    val canUse = Array.fill(n + 1)(true)
    for (ai <- a) {
      canUse(ai) = false
    }

    val dp = Array.fill(n + 1)(RR(0))
    dp(0) = RR(1)
    dp(1) = if (canUse(1)) RR(1) else RR(0)
    for (i <- 2 to n) {
      if(canUse(i)){
        dp(i) = dp(i-2) + dp(i-1)
      }
    }
    dp(n).representative
  }

  def main(args: Array[String]): Unit = {
    val (n, m, a) = read()
    println(solve(n, m, a))
  }
}

Measured memory_bytes: 49668
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
//typedef __int64 LL;
typedef vector<int> ivec;
typedef vector<string> svec;

const int MOD=1000000007;

int d[110];
set<int> nuee;
int main(){
	int n,k;
	cin>>n>>k;
	
	for(int i=0;i<k;i++)
	{
		int d;cin>>d;
		for(int j=0;j<d;j++)
		{
			int x;
			cin>>x;
			nuee.insert(x);
		}
	}
	cout<<n-nuee.size()<<"\n";
	return 0;
}

Measured memory_bytes: 3644
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		ProblemE solver = new ProblemE();
		solver.solve(1, in, out);
		out.close();
	}

	static class ProblemE {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			char[] s = ("0" + in.next()).toCharArray();
			int n = s.length - 1;

			List<Integer>[] adj = new List[n];
			for (int i = 0; i < n; i++) {
				adj[i] = new ArrayList<>();
			}
			addEdge(adj, 0, 1);
			int root = 1;
			for (int i = 2; i < n; i++) {
				addEdge(adj, root, i);
				if (s[i] == '1') {
					root = i;
				}
			}

			int[] sz = new int[n];
			dfs(adj, sz, root, -1);
			boolean[] got = new boolean[n + 1];
			for (int i = 0; i < n; i++) {
				if (i != root) {
					got[sz[i]] = true;
					got[n - sz[i]] = true;
				}
			}
			for (int i = 0; i <= n; i++) {
				if (got[i] != (s[i] == '1')) {
					out.println(-1);
					return;
				}
			}
			for (int i = 0; i < n; i++) {
				for (int j : adj[i]) {
					if (i < j) {
						out.println((i + 1) + " " + (j + 1));
					}
				}
			}
		}

		private void dfs(List<Integer>[] adj, int[] sz, int root, int p) {
			sz[root] = 1;
			for (int v : adj[root]) {
				if (v == p) {
					continue;
				}
				dfs(adj, sz, v, root);
				sz[root] += sz[v];
			}
		}

		private void addEdge(List<Integer>[] adj, int a, int b) {
			adj[a].add(b);
			adj[b].add(a);
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

	}
}



Measured memory_bytes: 72316
{-# LANGUAGE OverloadedStrings #-}

import           Control.Monad (unless)
import qualified Data.Text     as T
import qualified Data.Text.IO  as T

main :: IO ()
main = do
    t <- T.getLine
    unless (t == "END OF INPUT") $ do
        (putStrLn . concat . map show . map T.length . T.splitOn " ") t
        main

Measured memory_bytes: 4096
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

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

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

    ll N, K;
    ll cnt = 0;
    cin >> N >> K;

    ll P1 = N/K;

    if(K & 1) {
        cnt += P1 * P1 * P1;
    }else{
        ll P2 = ((N + K/2) / K);
        cnt += P1 * P1 * P1;
        cnt += P2 * P2 * P2;
    }

    cout << cnt << endl;

}



Measured memory_bytes: 256
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
//#include<assert.h>
#include<algorithm> 
//#include<iostream>
#include<bitset> 
using namespace std;

int n;
#define maxn 2011
bitset<2000011> f[2]; int cur=0; 

int a[maxn];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	
	f[0][0]=1;
	for (int i=1;i<=n;i++)
	{
		cur^=1;
		f[cur]=f[cur^1]<<(a[i])|f[cur^1];
	}
	int tot=0;
	for (int i=1;i<=n;i++) tot+=a[i];
	int id=0;
	for (int j=tot/2;j>=0;j--) if (f[cur][j]) {id=j; break;}
	printf("%d\n",tot-id);
	return 0;
}

Measured memory_bytes: 1408
#include<stdio.h>

int main(){
	int n,v[60],c[60],ans=0;
	scanf("%d",&n);
	int i;
	for(i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(i=0;i<n;i++){
		scanf("%d",&c[i]);
		if(v[i]>c[i])
			ans+=v[i]-c[i];
	}
	printf("%d",ans);
	return 0;
	
}

		

Measured memory_bytes: 128
#include <stdio.h>
int main(){
    int a,b,c,m;
    scanf("%d%d%d",&a,&b,&c);
    int d=c%b;
    int i=2;
    while(m<=10000){
        m=a*i;
        i++;
        if(m%b==d){
            printf("YES\n");
            return 0;}
    }
    printf("NO\n");
    return 0;
}

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

using namespace std;

int main() {
    string s;
    cin >> s;
    int n = s.length();
    int ans = 1<<30;;
    for(char c='a';c<='z';++c) {
        int prev = -1, tmpans = 0;
        for(int i=0;i<n;++i) {
            if(s[i] == c) {
                tmpans = max(tmpans, i-1-prev);
                prev = i;
            }
        }
        tmpans = max(tmpans, n-1-prev);
        ans = min(ans, tmpans);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3512
// https://ynucpc.github.io/blog/2018/04/15/cp_vscode/
// http://www.atmarkit.co.jp/ait/articles/1509/08/news019.html

#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);i++)
#define rep3(i,n,m) for(ll i=(n);i<(ll)(m);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << (x) << endl
// #define max(a, b) ((a) > (b) ? (a) : (b))
// #define min(a, b) ((a) < (b) ? (a) : (b)) 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
int _stoi(string s, int tmp=10){
	istringstream stm(s);
	int res;
	stm >> res;
	return res;
}
double _stod(string s, int tmp=10){
	istringstream stm(s);
	double res;
	stm >> res;
	return res;
}

template<typename T>string to_string(const T& n){
	std::ostringstream stm;
	stm << n;
	return stm.str();
}


// 降順ソート: sort(A, A+N, greater<>());
// min: *min_element(A, A+N)
// argmin: min_element(A, A+N)-A

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


ll m[10] = {0,2,5,5,4,5,6,3,7,6};
ll usable[10] = {};
V<ll> v;
ll aaaaa(ll a){
	//show(a);
	if (a==0)return 0;
	else if(a<0)return -1ll<<60;
	else{
		ll res = -1ll<<60;
		for(auto vv : v){
			ll r = aaaaa(a-vv);
			if (r < 0){continue;}
			ll re;
			if(r!=0){
				re = _stoi(to_string(usable[vv]) + to_string(r));
				//show(re);
			}else{
				re = usable[vv];
			}
			//show(re);
			chmax(res, re);
		}
		return res;
	}
}
ll uchu(string &a, string &b){
	if (a.size() > b.size()){
		return 1;
	}else if(a.size() < b.size()){
		return -1;
	}else{
		rep(i, a.size()){
			if (a[i]>b[i])return 1;
			if (a[i]<b[i])return -1;
		}
		return 0;
	}
}
int main(){	
	// ll A;
    // cin >> A;
	// cout << "hello" << endl;
	// cout << stoi("14") << endl;
	// cout << max({1,3}) << endl;
	ll N, M;
	cin >> N >> M;
	ll A[M];
	
	rep(i, M){
		cin >> A[i];
		chmax(usable[m[A[i]]], A[i]);
	}
	ll mi;
	rep(i, 10)if(usable[i]!=0){
		mi = i;
		break;
	}
	ll keta = N/mi;
	ll mo = N%mi;
	
	if (mo==0){
		rep(i, keta)cout << usable[mi];
		cout << endl;
		return 0;
	}

	rep(i,10)if(usable[i]!=0&&i!=mi)v.push_back(i);
	
	//show(v);



	string ans = "1";
	rep(ooooo, 10){
		keta--;
		if(keta<0)break;
		mo+=mi;
		ll aa = aaaaa(mo);
		//show(aa);
		if (aa<0)continue;
		string saa = to_string(aa);
		bool f = true;
		string an = "";
		rep(i, saa.size()){
			if (!f || saa[i]-'0'>usable[mi])an += saa[i];
			else {
				rep(i, keta)an += to_string(usable[mi]);
				an += saa[i];
				f = false;
			}
		}
		if (f)rep(i, keta)an += to_string(usable[mi]);
		if (uchu(an, ans)==1){ans = an;}
		

	}
	cout << ans << endl;


}








Measured memory_bytes: 256
#include <iostream>

using namespace std;

int gcd(int x, int y) {
	if (x < y) {
		int tmp = x;
		x = y;
		y = tmp;
	}

	while (y > 0){
		int r = x % y;
		x = y;
		y = r;
	}
	return x;
}

int main() {
	int x, y;
	cin >> x >> y;
	cout << gcd(x, y) << endl;
}




Measured memory_bytes: 3096
#include <bits/stdc++.h>
#define cinf(n,x) for(int i=0;i<(n);i++) cin >> x[i];
typedef long long int ll;
using namespace std;
const long long INF = 1LL<<60;

int main(){
	int n; cin >>n;
	vector<int> x(n),a(n); cinf(n,x);
	for(int i=0;i<n;i++){
		a[i]=x[i];
	}
	sort(a.begin(),a.end());
	int Xn=a[n/2-1];
	int Xn1=a[n/2];
	for(int i=0;i<n;i++){
		if(x[i]<=Xn){
			cout << Xn1 << endl;
		}
		else cout << Xn << endl;
	}
}


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

#define max 1000
 
int main(){
  int len;
  char str[max];
  
  scanf("%d", &len);
  scanf("%s", str);
  
  if(len % 2 == 1){
    printf("No\n");
    return 0;
  }else{
    int i;
    int n = len/2;
    int count = 0;
    
    for(i = 0; i < n; i++){
      if(str[i] == str[i+n]){
        count++;
      }
    }
    if(count == n){
      printf("Yes\n");
      return 0;
    }else{
      printf("No\n");
    }
  }
}

Measured memory_bytes: 1632
//#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm;
inline char getcha() {
	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	char C = getcha();
	int A = C - '0';
	while ((C = getcha()) >= '0') A = A * 10 + C - '0';
	return A;
}

const int x = (1 << 30) - 1;

int P[100001];
int D[100001];
ll Find(int A) {
	if (P[A] < 0) return A;

	ll k = Find(P[A]);
	int d = k >> 30;
	int p = k & x;
	D[A] += d;
	P[A] = p;

	return ((ll)(unsigned)D[A] << 30) + p;
}

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


	int N = getint();
	int M = getint();
	rep1(i, N) P[i] = -1;

	int OK = 1;
	rep(i, M) {
		int L = getint();
		int R = getint();
		int d = getint();

		ll k = Find(L);
		int dl = k >> 30;
		int l = k & x;
		k = Find(R);
		int dr = k >> 30;
		int r = k & x;

		if (l != r) {
			if (P[l] <= P[r]) {
				P[l] += P[r];
				P[r] = l;
				D[r] = dl - dr + d;
			}
			else {
				P[r] += P[l];
				P[l] = r;
				D[l] = dr - dl - d;
			}
		}
		else {
			if (dl + d != dr) {
				OK = 0;
				break;
			}
		}
	}
	if (OK) printf("Yes");
	else printf("No");

	Would you please return 0;
}

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

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n)
    {
        int x;
        cin >> x;
        if (x % 2 == 0)
            a[i] = 0;
        else
            a[i] = 1;
    }
    int sum = 0;
    rep(i, n) sum += a[i];
    if (sum % 2 == 0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}

Measured memory_bytes: 3792
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<iterator>
#include<complex>
#include<stack>
#include "stdio.h"
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)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const long long mod_const = 1e9+7;
using MIte = multimap<long, long, greater<long> >::iterator;

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

	// long long?
	int a, b, c;
	cin >> a >> b >> c;

	int	S = 0;
	if (a > b && a > c) {
		S = b * c / 2;
	}
	else if (b > a && b > c) {
		S = a * c / 2;
	}
	else if (c > a && c > b) {
		S = a * b / 2;
	}

	cout << S << endl;

	return 0;

}

Measured memory_bytes: 384
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int f[105][10];
ll p[105][11];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n)rep(j,10) cin >> f[i][j];
    rep(i,n)rep(j,11) cin >> p[i][j];
    ll ans = -1e18;
    for(int S = 1; S < (1<<10); ++S){
        ll score = 0;
        rep(i,n){
            int cnt = 0;
            rep(j,10){
                if((S>>j & 1) && (f[i][j])) ++cnt;
            }
            score += p[i][cnt];
        }
        chmax(ans, score);
    }
    cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
n = int(input())
s = [input() for i in range(n)]
cnt = 0
for e in s:
  if e=="A":
    cnt += 1
  else:
    cnt -= 1
  if cnt<0:
    print("NO")
    exit()
if cnt==0:
  print("YES")
else:
  print("NO")


Measured memory_bytes: 5604
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MOD 1000000007
#define MOD2 998244353
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<=b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
#define trace(x) cerr<<#x<<": "<<x<<'\n';
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
#define T int
#define ordered_set tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
 
ll mexp(ll a, ll b, ll m){
    ll ans=1;
    a%=m;
    while(b){
        if(b&1) ans=ans*a%m;
        b>>=1;
        a=a*a%m;
    }
    return ans;
}
 
int main(){
    IOS
    int n,d;
    cin>>n>>d;
    d = 2*d+1;
    int x;
    if(n%d==0){
    	x=0;
    }
    else{
    	x=1;
    }
    cout<<(n/d + x);
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <cinttypes>
#include <string.h>
#include <bits/stdc++.h>

#define countof(a) (sizeof(a)/sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int> >
#define pi pair<int,int>
#define fi first
#define se second

#define FOR(var, to)          for (register s64 var = 0; var < (s64)to; var++)
#define FROMTO(var, from, to) for (register s64 var = from; var <= (s64)to; var++)

#define INIT(var) FOR(i,countof(var)) var[i] = 0
#define INPUT(var) FOR(i,countof(var)) var[i] = ri()

#define SORT(v) qsort(v,countof(v),sizeof(*v),int_less)
#define SORTT(v) qsort(v,countof(v),sizeof(*v),int_greater)

#define MOD 1000000007
#define INF (1 << 30)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

using namespace std;

static inline int ri() {
  int a;
  scanf("%d", &a);
  return a;
}

static inline s64 rs64() {
  s64 a;
  scanf("%" SCNd64, &a);
  return a;
}

static inline void wi(int a) {
  printf("%d", a);
}

static inline void wu64(u64 a) {
  printf("%" PRIu64, a);
}

static inline void ws64(s64 a) {
  printf("%" PRId64, a);
}

int int_less(const void *a, const void *b) {
  return (*((const int*)a) - *((const int*)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int*)b) - *((const int*)a));
}

/* originally written by SugarDragon5 */
struct UnionFind{
    vi data;
    UnionFind(int size):data(size,-1){}
    bool unite(int x,int y){
        x=root(x);y=root(y);
        if(x!=y){
            if(data[y]<data[x])swap(x,y);
            data[x]+=data[y];data[y]=x;
        }
        return x!=y;
    }
    bool find(int x,int y){
        return root(x)==root(y);
    }
    int root(int x){
        return data[x]<0?x:data[x]=root(data[x]);
    }
    int size(int x){
        return -data[root(x)];
    }
};

int main() {
    int n = ri();
    int t[n];
    int x[n];
    int y[n];
    FOR(i,n) t[i] = ri(), x[i] = ri(), y[i] = ri();
    
    bool res = true;
    int xx = 0;
    int yy = 0;
    FOR(i,n) {
        int tdis = i?t[i]-t[i-1]:t[i];
        int dis = abs(xx-x[i])+abs(yy-y[i]);
        if (dis > tdis) res = false;
        if ((tdis-dis)%2) res = false;
        
        xx = x[i];
        yy = y[i];
    }
    cout << (res?"Yes":"No") << endl;
    return 0;
}


Measured memory_bytes: 1408
#include <iostream>
using namespace std;
int main(){
	while(1){
	int m,n; cin >> n >> m ;
	if(n==0 && m==0) break;
	bool* a=new bool[n];
	for(int j=0;j<n;j++) a[j]=true;
	int i=0; int count=0 ; int out=0;
	while(1){
		if(a[i]==true && ++count%m==0) {a[i]=false;out++;};
		if(out==n-1) break;
		if(++i==n) i=0;
	};
	for(int j=0;j<n;j++)
		if(a[j]==true) cout << j+1 << '\n' ;
	delete[] a;
	};
}

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

using namespace std;

const int N = 1e5 + 2;

int n, vis[N], bad[N], d[N], mx;
vector <int> g[N];
vector < pair <int, int> > e;
string s;

int dfs1(int v, int pr, vector < pair <int, int> > &e){
      int val = 0;
      vis[v] = 1;
      for(int to : g[v]){
            if(to == pr)
                  continue;
            int now = dfs1(to, v, e);
            if(now){
                  val += now;
                  e.push_back({v, to});
            }
      }
      if(s[v] == 'W')
            val ++;
      return val;
}

void dfs(int v, int pr = 0){
      int mx1 = 0, mx2 = 0;
      for(int to : g[v]){
            if(to == pr)
                  continue;
            dfs(to, v);
            if(d[to] > mx1){
                  mx2 = mx1;
                  mx1 = d[to];
            } else if(d[to] > mx2)
                  mx2 = d[to];
      }
      mx = max(mx, mx1 + mx2 + bad[v]);
      d[v] = mx1 + bad[v];
}

int solve(int v){
      int ret = (int)e.size() * 2;
      for(int i = 1; i <= n; i ++){
            bad[i] = ((s[i] == 'W') ^ (g[i].size() & 1));
            if(vis[i])
                  ret += bad[i];
      }
      dfs(v);
      ret -= 2 * mx;
      return ret;
}

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

      for(int i = 1; i < n; i ++){
            int u, v;
            scanf("%d %d", &u, &v);
            g[u].push_back(v);
            g[v].push_back(u);
      }

      cin >> s;
      s = ' ' + s;

      if(count(s.begin(), s.end(), 'W') == 0){
            cout << 0 << endl;
            return 0;
      }

      if(count(s.begin(), s.end(), 'W') == 1){
            cout << 1 << endl;
            return 0;
      }

      for(int i = 1; i <= n; i ++){
            if(s[i] == 'W'){
                  dfs1(i, 0, e);
                  for(int i = 1; i <= n; i ++)
                        g[i].clear();
                  for(int i = 0; i < e.size(); i ++){
                        int u = e[i].first, v = e[i].second;
                        g[u].push_back(v);
                        g[v].push_back(u);
                  }
                  cout << solve(i) << endl;
                  return 0;
            }
      }
}


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

const int N = 8 + 7;
int arr[N], perm[N];
int n;

int ans() {
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  iota(perm, perm + n, 1);
  int cur = 1;
  do {
    bool flag = true;
    for (int i = 0; i < n; ++i) {
      flag &= arr[i] == perm[i];
    }
    if (flag) {
      return cur;
    }
    cur++;
  } while (next_permutation(perm, perm + n));
  return -1;
}

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

  cin >> n;
  int a = ans();
  int b = ans();
  cout << abs(a - b) << "\n";

  return 0;
}



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

// @@ !! LIM()

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

  ll N; cin >> N;
  vector A(N, 0LL);
  ll s = 0;
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
    s ^= A.at(i);
  }
  for (ll i = 0; i < N; i++) {
    if (i > 0) cout << " ";
    cout << (s ^ A.at(i));
  }
  cout << endl;

  return 0;
}



Measured memory_bytes: 4760
A1,A2,A3=map(int,input().split())

if abs(A1-A2)==abs(A2-A3):
    print(abs(A1-A2)+abs(A2-A3)+abs(A3-A1)-abs(A1-A2))
elif abs(A2-A3)==abs(A3-A1):
    print(abs(A1-A2)+abs(A2-A3)+abs(A3-A1)-abs(A2-A3))
elif abs(A3-A1)==abs(A1-A2):
    print(abs(A1-A2)+abs(A2-A3)+abs(A3-A1)-abs(A3-A1))
else :
    print(abs(A1-A2)+abs(A2-A3)+abs(A3-A1)-max(abs(A1-A2),abs(A2-A3),abs(A3-A1)))

Measured memory_bytes: 3064
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
  long long int N,K,t,s;
  scanf("%lld %lld",&N,&K);
  t = N%K;
  s = K-t;
  
  if(t>s)
  {
    printf("%lld\n",s);
    exit(0);
  }
  else
  {
    printf("%lld\n",t);
    exit(0);
  }
  return 0;
}
  

Measured memory_bytes: 1684
A = int(input())
B = int(input())
C = int(input())
X = int(input())
count = 0
for i in range(A+1):
    for j in range(B+1):
        for k in range(C+1):
            if 500*i+100*j+50*k == X:
                count += 1
print(count)

Measured memory_bytes: 3060
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <set>

#define ll long long
#define pll pair<ll, ll>

#define reps(i, f, n) for (int i = (f); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

const int INF = 1111111111;
const double EPS = 1e-9;

using namespace std;

void init()
{
}

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

	int mod = n % 1000;
	if(mod==0)
	{
		cout << 0 << endl;
		return;
	}
	cout << 1000 - mod << endl;
}

int main()
{
	init();
	solve();
}


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

#define rep1(i,n) for (decay_t <decltype (n)> i = 1, i ## __len = (n); i <= i ## __len; ++ i)

struct UnionFind
{
  using node_t = size_t;
  vector <node_t> parent;
  explicit UnionFind (node_t n) : parent (n) { for (node_t i = 0; i < n; ++ i) parent [i] = i; }
  auto root (node_t x) noexcept -> node_t { return parent [x] == x ? x : parent [x] = root (parent [x]); }
  auto merge (node_t x, node_t y) noexcept
  {
    x = root (x);
    y = root (y);
    return x != y ? static_cast <void> (parent [y] = x), true : false;
  }
};

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

  size_t n, m;
  cin >> n >> m;

  vector <int> p (n + 1);
  rep1 (i, n) cin >> p [i];

  UnionFind uni (n + 1);
  rep1 (j, m)
  {
    size_t x, y;
    cin >> x >> y;
    uni.merge (x, y);
  }

  auto ans = 0;
  rep1 (i, n)
  {
    if (uni.root (i) == uni.root (p [i]))
    {
      ++ ans;
    }
  }
  cout << ans << endl;
}



Measured memory_bytes: 1408
#include<stdio.h>

int Sum(int i,int j,int k,char a[5])
{
    int x=a[0];
    if(i)
        x+=a[1];
    else
        x-=a[1];
    if(j)
        x+=a[2];
    else
        x-=a[2];
    if(k)
        x+=a[3];
    else
        x-=a[3];
    if(x==7)
        return 1;
    else
        return 0;
}

int main()
{
    char a[5];
    int ok;
    while(scanf("%s",a)!=EOF)
    {
        ok=0;
        for(int i=0; i<4; i++)
            a[i]-=48;
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<2; j++)
            {
                for(int k=0; k<2; k++)
                {
                    if(Sum(i,j,k,a))
                    {
                        printf("%d",a[0]);
                        if(i)
                            printf("+");
                        else
                            printf("-");
                        printf("%d",a[1]);
                        if(j)
                            printf("+");
                        else
                            printf("-");
                        printf("%d",a[2]);
                        if(k)
                            printf("+");
                        else
                            printf("-");
                        printf("%d=7\n",a[3]);
                        ok=1;
                        break;
                    }
                }
                if(ok)
                    break;
            }
            if(ok)
                break;
        }
    }
    return 0;
}

Measured memory_bytes: 128
#include<iostream>
using namespace std;
int main() {
		int N, A, B;
		cin >> N >> A >> B;
		if (A > B || (N == 1 && A != B))
			cout << 0 << endl;
		else if (A == B)
		{
			cout << 1 << endl;
		}
		else
		{
			//int min = A + B + (N - 2)*A;
			//int max = A + B + (N - 2)*B;
			//long long num = max - min + 1;
			cout << (long long)(B - A)*(N - 2) + 1 << endl;
		}
		system("pause");
		return 0;
}

Measured memory_bytes: 504
// https://atcoder.jp/contests/abc094/tasks/arc095_a

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n)   for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++)
#define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\n' : ' ')
#define DUMP2D(a, n, m) REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\n' : ' '); puts("")

const int N_MAX = 200000;
const int X_I_MAX = 1e9;

int N;
int X[N_MAX];
int Y[N_MAX];

void solve() {
  copy(X, X + N, Y);
  sort(Y, Y + N);
  int m1 = Y[N / 2 - 1];
  int m2 = Y[N / 2];
  REP(i, N) {
    int ans = X[i] <= m1 ? m2 : m1;
    printf("%d\n", ans);
  }
}

void input() {
  scanf("%d", &N);
  REP(i, N) scanf("%d", X + i);
}

int main() {
  input();
  solve();
  return 0;
}


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

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

    vector<int> X;
    int l = 1000;
    int r = 0;
    for (int i=0;i<N;i++) {
        int x;
        cin >> x;
        X.push_back(x);
        if (x < l) {
            l = x;
        }
        if (r < x){
            r = x;
        }
    }

    int sum = 0;
    int min = INT_MAX;
    for (int p = l; p < r; p++) {
        for (auto x : X) {
            sum += pow(p - x, 2);
        }
        if (sum < min) {
            min = sum;
        }
        sum = 0;
    }
    if (min == INT_MAX) {
        cout << 0 << endl;
        return 0;
    }
    cout << min << endl;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<cstdio>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=200;
int main()
{
	ll a,b;
	cin>>a>>b;
	if((a+b)%2==0)
		cout<<(a+b)/2;
	else cout<<"IMPOSSIBLE"<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
//#########################################
signed main(){
	int n,d;cin >> n >> d;
	int ret = 0;
	rep(i,n){
		int a,b;cin >> a >> b;
		ret += a*a + b*b <= d*d;
	}
	cout << ret << endl;
	return 0;
}

Measured memory_bytes: 3632
program bingo
  implicit none
  integer :: a(3,3), n, b, i, j, k
  logical :: x
  read(*,*) a
  read(*,*) n
  do i = 1, n
    read(*,*) b
    do j = 1, 3
      do k = 1, 3
        if (a(j,k) == b) a(j,k) = -1
      end do
    end do
  end do
  x = a(1,1) < 0 .and. a(2,2) < 0 .and. a(3,3) < 0
  x = x .or. (a(3,1) < 0 .and. a(2,2) < 0 .and. a(1,3) < 0)
  do i = 1, 3
    x = x .or. (a(i,1) < 0 .and. a(i,2) < 0 .and. a(i,3) < 0)
    x = x .or. (a(1,i) < 0 .and. a(2,i) < 0 .and. a(3,i) < 0)
  end do
  write(*,'(a)') trim(merge("Yes","No ",x))
end program bingo

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

// ????????????????????£?¨?
int kruskal(priority_queue<pair<int, pair<int, int> > > edge, int v);

// ?????????
int main(void){
  while(true){
    // ???????????°??¨???????????°???????????????
    int n, m;
    cin >> n;
    if(n == 0){ break; }
    cin >> m;
    
    // ???????????±??????????????????????????????????????\????????????
    priority_queue<pair<int, pair<int, int> > > edge;
    for(int i = 0; i < m; i++){
      int a, b, d;
      scanf("%d,%d,%d", &a, &b, &d);
      edge.push(make_pair(-d, make_pair(a, b)));
    }
  
    // ????°???¨?????¨????????????????±??????????????????????
    int length = kruskal(edge, n);
    cout << length / 100 - n + 1 << endl;
  }

  return 0;
}

// UnionFind??¨
class UnionFindTree{
private:
  vector<int> parent;
  vector<int> rank;
  int find(int a);
public:
  void init(int size);
  void unite(int a, int b);
  bool isSameTree(int a, int b);
};

// UF??¨????????????
void UnionFindTree::init(int size){
  parent.clear();
  rank.clear();
  for(int i = 0; i < size; i++){
    parent.push_back(i);
    rank.push_back(0);
  }
  return; 
}

// UF??¨?????????????????????
int UnionFindTree::find(int a){
  if(parent[a] != a){ parent[a] = find(parent[a]); }
  return parent[a];
}

// UF??¨??§???????????£?????????
void UnionFindTree::unite(int a, int b){
  int x = find(a);
  int y = find(b);
  // ?????§???????????£???????????????????????????
  if(x == y){ return; }

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

// UF??¨??§???????????????????????????????????????
bool UnionFindTree::isSameTree(int a, int b){
  return find(a) == find(b);
}

// ??????????????????
// ?????°: ??????????????????????????????????????????????????\???(first: -?????????, second: to-front), ????????°
// ?????????: ??¨????????????
int kruskal(priority_queue<pair<int, pair<int, int> > > edge, int v){
  int length = 0; // ??¨????????????

  // UF??¨?????£?¨???¨?????????
  UnionFindTree uf;
  uf.init(v);

  // ?????????????????????
  while(!edge.empty()){
    // ????£????????????????????°?????????????????????????
    int cost = -edge.top().first;
    int a = edge.top().second.first;
    int b = edge.top().second.second;
    edge.pop();
    // ????????????????????????????????£????????????????£???°???
    if(uf.isSameTree(a, b)){ continue; }

    // UF??¨?????£??????????????????????????´??°
    length += cost;
    uf.unite(a, b);
  }
  
  return length;
}

Measured memory_bytes: 1396
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100005;
int a[N];
int main (){
	int n,m;scanf ("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf ("%d",&a[i]),a[i]/=2;
	ll lcm=1;
	for (int i=1;i<=n;i++){
		lcm=lcm/__gcd(lcm,(ll)a[i])*a[i];
		if (lcm>m){puts("0");return 0;}
	}
	for (int i=1;i<=n;i++)
		if (lcm%(a[i]*2)==0){puts("0");return 0;}
	printf ("%d",m/lcm-m/(2ll*lcm));
	return 0;
}

Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,l,n) for(int i=l; i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;

int main() {
  int n; cin>>n;
  int x[n];
  REP(i,n) cin>>x[i];
  int reg=100*100*n;
  FOR(i,1,101){
    int sum=0;
    REP(j,n) sum+=(x[j]-i)*(x[j]-i);
    reg=min(reg,sum);
  }
  cout<<reg<<endl;
}

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

// 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)

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

#define	MOD 1000000007

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

ll	gcd(ll A, ll B) {
	while (0!=B) {
		A %=B;
		swap(A, B);
	}
	return A;
}

ll	lcm(ll A, ll B) {
	ll G=gcd(A, B);
	return A/G*B;
}

ll	myPow(ll x, ll n) {
	ll r=1;
	while(n>0)
		if (0==n%2) {
			x *=x;
			x %=MOD;
			n /=2;
		} else {
			r *=x;
			r %=MOD;
			n--;
		}
	return r;
}

int	main() {
	ll N;
	cin >>N;
	map<string, ll> PP;
	map<string, ll> NN;
	ll Z=0;		// Case 0==A && 0==B
	ll ZA=0;	// Case 0==A && 0!=B
	ll ZB=0;	// Case 0!=A && 0==B
	vector<string> Key;
	Rep0(i, N) {
		ll A, B;
		cin >>A >>B;
		if ((0==A)&&(0==B)) {
			Z++;
			continue;
		}
		if (0==A) {
			ZA++;
			continue;
		}
		if (0==B) {
			ZB++;
			continue;
		}
		ll AA, BB;
		AA=abs(A);
		BB=abs(B);
		ll G=gcd(AA, BB);
		AA /=G;
		BB /=G;
		string S=to_string(AA)+'.'+to_string(BB);
		string SS=to_string(BB)+'.'+to_string(AA);
		if ((A>0)&&(B>0)) {
			PP[S]++;
			Key.push_back(S);
		} else if ((A<0)&&(B<0)) {
			PP[S]++;
			Key.push_back(S);
		} else {
			NN[SS]++;
			Key.push_back(SS);
		}
	}
	Sort(Key);
	ll S=myPow(2, ZA)+myPow(2, ZB)-1;// ZA combi. + ZA combi. + none 
	S +=MOD;
	S %=MOD;
	//Rep0(i, Key.size()) {
	ll NNNN=Key.size();
	for (ll i=0; i<NNNN; i++) {
		if (i!=0)
			if (Key.at(i)==Key.at(i-1))
				continue;
		string KK=Key.at(i);
		ll SS=0;
		ll PPP=PP[KK];
		ll NNN=NN[KK];
		SS +=myPow(2, PPP);
		SS +=myPow(2, NNN);
		SS %=MOD;
		SS--;
		SS +=MOD;
		SS %=MOD;
		S *=SS;
		S %=MOD;
	}
	// Z
	S +=Z;
	S %=MOD;
	// Remove the case non selected
	S--;
	S +=MOD;
	S %=MOD;
	cout <<S <<endl;
	return 0;
}



Measured memory_bytes: 68836
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int H,W;
	cin>>H>>W;
	int C[2];
	int D[2];
	cin>>C[1]>>C[0]>>D[1]>>D[0];
	C[0]=C[0]-1;
	C[1]=C[1]-1;
	D[0]=D[0]-1;
	D[1]=D[1]-1;
	vector<vector<int> >S(W,vector<int>(H));
	for(int y=0;y<H;y++)
	{
		for(int x=0;x<W;x++)
		{
			char c;
			cin>>c;
			if(c== '.')
				S[x][y]=1;
			else 
				S[x][y]=0;
		}
	}
	deque<pair<int,int> >stack;
	deque<pair<int,int> >stack2;
	pair<int,int> a(C[0],C[1]);
	S[a.first][a.second]=4;
	int result=0;
	while(true)
	{
		while(true)
		{
			stack2.push_back(a);
			if (a.second+1<H&&S[a.first][a.second+1]==1)
			{
				stack.push_back(a);
				S[a.first][a.second+1]=4;
				a.second++;
				continue;
			}
			if(a.second-1>=0&&S[a.first][a.second-1]==1)
			{
				stack.push_back(a);
				S[a.first][a.second-1]=4;
				a.second--;
				continue;
			}
			if(a.first+1<W&&S[a.first+1][a.second]==1)
			{
				stack.push_back(a);
				S[a.first+1][a.second] = 4;
				a.first++;
				continue;
			}
			if(a.first-1>=0&&S[a.first-1][a.second]==1)
			{
				stack.push_back(a);
				S[a.first-1][a.second] = 4;
				a.first--;
				continue;
			}
			if(stack.size()==0)
				break;
			a=stack.back();
			stack.pop_back();
		}
		if(S[D[0]][D[1]]==2||S[D[0]][D[1]]==4)
		{
			cout<<result<<endl;
			return 0;
		}
		bool frag=false;
		for(int i=0;i<stack2.size();i++)
		{
			int x=stack2[i].first,y=stack2[i].second;
			for(int xx=-2;xx<3;xx++)
			{
				for(int yy=-2;yy<3;yy++)
				{
					if(xx+x>=0&&xx+x<W&&yy+y>=0&&yy+y<H&&S[x+xx][y+yy]==1)
					{
						S[x+xx][y+yy]=4;
						stack.push_back({x+xx,y+yy});
						frag=true;
					}
				}
			}
		}
		stack2.clear();
		if(!frag)
		{
			cout<<-1<<endl;
			return 0;
		}
		a=stack[stack.size() - 1];
		stack.pop_back();
		result++;
	}
}

Measured memory_bytes: 7528
# なぜか 4WA
# ってか最初のテストケースでREなるのなんなん

# Dは不変なのでaiからai+1には必ずたどり着け、
# 最短でai+1 - aiぶん一気にすすむ1手、
# 最長で1ずつすすむ ai+1 - ai 手
# それぞれの差をとって1~最大公約数の約数を試していき、
# スタート地点から最も近い場所に到達できればその値ですべての都市を回れる
# （行き過ぎたら 次の値を試す）


# という方針でやったら2WAだったのだがどこが間違ってるのかわからん

# 諦めて解説をみたら全部の位置とのgcdを取るという感じで納得感が薄い

# difのとこがおかしかった（n=2の場合）のを直してみたバージョン
# ↑やっただけでTLEになったのでマジで鬱になった ふつうに解答通り終わらせるためだけの自戒バージョン

n, pos = gets.chomp.split.map(&:to_i)
@cities = gets.chomp.split.map(&:to_i).sort
# 最初にn==1の場合だけやっとく 1ずつしか進めない
if n==1
  puts (@cities[0]-pos).abs
  exit
end
<<~EOS
dif = []  # 各都市間の差
(n-1).times do |i|
  dif << @cities[i+1] - @cities[i]
end
EOS

def gcd(a, b)
  return a if b.zero?
  return gcd(b, a) if a<b
  return gcd(b, a%b)
end

gcdn = gcd((pos - @cities[0]).abs, (pos - @cities[1]).abs)
(n-2).times do |i|
  gcdn = gcd(gcdn, (pos-@cities[i+2]).abs)
end
puts gcdn
<<~EOS
# 各都市間の差の最大公約数
gcdn = 1
if dif.size == 1
  # これを忘れていたっぽい
  gcdn = gcd(dif[0], pos)
end
if dif.size >= 2
  gcdn = gcd(dif[0], dif[1])
end
2.upto(n-2) do |i|
  gcdn = gcd(dif[i], gcdn)  # これが間違ってたっぽい
end
# 約数をとる ダルいのでgcdn/2までループすればいいんじゃないかな
yakusu = []
1.upto(gcdn/2) do |i|
  if (gcdn % i).zero?
    yakusu << i
  end
end
yakusu << gcdn
yakusu.reverse!
# これのでかいやつから試していって到達できればおｋ
# 最も速く到達できるやつをにぶたんで求める（どうせ到達できるので「posを越える最小の値」を求める）
# そもそもposが既に配列にある場合もあるか
if @cities[-1] < pos
  goal = @cities[-1]
  # 実装するの忘れてた 1WA
  yakusu.each do |y|
    if (pos-goal)%y == 0
      puts y
      exit
    end
  end
elsif @cities[-1] == pos
  # 2番目に大きいやつに向けて下っていく
  goal = @cities[-2]
  # 実装するの忘れてた 1WA
  yakusu.each do |y|
    if (pos-goal)%y == 0
      puts y
      exit
    end
  end
else
  # にぶたんで求める
  l, r = 0, n
  while r-l>1 do
    mid = (r+l)/2
    if @cities[mid] < pos
      l = mid + 1
    else
      r = mid
    end
  end
  # lが求めるやつね
  goal = @cities[l]
  if goal == pos
    goal = @cities[l+1]
  end
  yakusu.each do |y| 
    if (goal-pos)%y == 0
      puts y
      exit
    end
  end
end
EOS

Measured memory_bytes: 11008
#include<cstdio>
using namespace std;
int n,a[10],tot,cnt;
int main()
{
  scanf("%d",&n);
  while(n)
  {
  	if(n&1)  a[++tot]=(1<<cnt);
  	cnt++,n>>=1;
  }
  printf("%d\n",a[tot]);
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#include <numeric>
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;

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

    int N; cin >> N;
    int leftB = 0, centralAB = 0, rightA = 0, leftB_rightA = 0;
    rep(i, 0, N) {
        string s;
        cin >> s;
        if (s[0] == 'B' and s[s.size()-1] == 'A') ++leftB_rightA;
        else if (s[0] == 'B') ++leftB;
        else if (s[s.size()-1] == 'A') ++rightA;

        int pos = s.find("AB");
        while(pos != string::npos) {
            ++centralAB;
            pos = s.find("AB", pos+2);
        }
    }

    //printf("%d %d %d %d\n", leftB, centralAB, rightA, leftB_rightA);

    int res = 0;
    if (leftB_rightA >= 2) {
        res += leftB_rightA - 1;
        if (leftB > 0 and rightA > 0) {
            res += 2;
            res += min(leftB, rightA) - 1;
        }
        else if (leftB > 0 or rightA > 0) {
            ++res;
        }
    }
    else if (leftB_rightA == 1) {
        if (leftB > 0 and rightA > 0) {
            res += 2;
            res += min(leftB, rightA) - 1;
        }
        else if (leftB > 0 or rightA > 0) {
            ++res;
        }
    }
    else if (leftB_rightA == 0) {
        res += min(leftB, rightA);
    }
    res += centralAB;

    cout << res << "\n";
}

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

using namespace std;

typedef long long ll;
#define INF 1000000007

int main(){
    int n; cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    ll ans = 0;
    int right = 0;
    ll tmp = 0;
    ll sum = 0, xorSum = 0;
    for(int left = 0; left < n; left++)
    {
        while(right < n && (xorSum ^ a[right]) == (sum + a[right])){
            sum += a[right];
            xorSum ^= a[right];

            right++;
            }
        ans += right - left;
        if(left == right) right++;
        else
        {
            sum -= a[left];
            xorSum ^= a[left];
        }
    }
    
     cout << ans << "\n";
     return 0;
}


Measured memory_bytes: 1792
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using static MyIO;


public class D
{
	public static void Main()
	{
		int A = GetInt();
		int B = GetInt();

		char[,] ans = new char[100, 100];
		for(int i = 0; i < 50; i++)
			for(int j = 0; j < 100; j++)
				ans[i, j] = '#';

		for(int i = 50; i < 100; i++)
			for(int j = 0; j < 100; j++)
				ans[i, j] = '.';

		A--;
		for(int i = 0; i < 50; i += 2)
		{
			for(int j = 0; j < 100; j += 2)
			{
				if(A == 0)
					break;
				
				ans[i, j] = '.';
				A--;
			}
			if(A == 0)
				break;
		}

		B--;
		for(int i = 99; i > 50; i -= 2)
		{
			for(int j = 99; j >= 0; j -= 2)
			{
				if(B == 0)
					break;
				
				ans[i, j] = '#';
				B--;
			}
			if(B == 0)
				break;
		}

		Console.WriteLine("100 100");
		for(int i = 0; i < 100; i++)
		{
			for(int j = 0; j < 100; j++)
				Console.Write(ans[i, j]);
			Console.WriteLine();
		}

	}
}


public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt(){ return int.Parse(getArg()); }
	public static long GetLong(){ return long.Parse(getArg()); }
	public static double GetDouble(){ return double.Parse(getArg()); }
	public static string GetString(){ return getArg(); }
}





Measured memory_bytes: 13240
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
  
int main(void){
    string s;
    cin >> s;
    
    //cout << s[s.size()-1] << endl;

    if(s[s.size()-1] == '3') {
        cout << "bon" << endl;
    } else if(s[s.size()-1] == '1' || s[s.size()-1] == '6' || s[s.size()-1] == '8' || s[s.size()-1] == '0') {
        cout << "pon" << endl;
    } else {
        cout << "hon" << endl;
    }
    
    return 0;
}

Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
int main(){
  int a,b,c,d;
  cin >> a >> b >> c >> d;

  if(a+b>c+d) cout << "Left" << endl;
  else if(a+b<c+d) cout << "Right" << endl;
  else cout << "Balanced" << endl;
}

Measured memory_bytes: 384
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  int n; cin>>n;
  while(n --> 0){
    int a[3]; cin>>a[0]>>a[1]>>a[2];
    sort(a, a+3);
    cout << (a[0]*a[0]+a[1]*a[1] == a[2]*a[2] ? "YES" : "NO") << endl;
  }
  return 0;
}

Measured memory_bytes: 0
import java.util.*;

public class Main {
	static long P = (int)1e9 +7;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		long dp[] = new long[n+1];
		dp[0] = 1;
		char s1[] = new char[n+1];
		char s2[] = new char[n+1];
		
		sc.next().getChars(0, n, s1, 1);
		sc.next().getChars(0, n, s2, 1);
		
		int i;
		if(s1[1] == s2[1]){
			dp[1] = 3;
			i=2;
		}
		else{
			dp[2] = 6;
			i=3;
		}
		
		for(;i<=n;i++){
			if(s1[i] == s2[i]){
				if(s1[i-1] == s2[i-1])
					dp[i] = dp[i-1] * 2 % P;
				else
					dp[i] = dp[i-1];
			}
			else{
				i++;
				if(s1[i-2] == s2[i-2]){
					dp[i] = dp[i-2] * 2 % P;
				}
				else{
					dp[i] = dp[i-2] * 3 % P;
				}
			}
		}
		System.out.println(dp[n]);
	}

}


Measured memory_bytes: 23252
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int gcd(int a,int b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}
set<int> fact(int z)
{
	set<int> ret;
	for(int j=2;j*j<=z;j++)
	{
		while(z%j==0)
			z/=j,ret.insert(j);
	}
	if(z!=1)
		ret.insert(z);
	return ret;
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	bool pairwise=1;
	set<int> allfactors;
	int g=a[0];
	for(int p=0;p<n;p++)
	{
		cin>>a[p];
	}
	for(int p=0;p<n;p++)
	{
		if(p>0)
			g=__gcd(a[p],g);
	}
		
	for(int k=0;k<n;k++)
	{
		if(pairwise)
		{
			set<int> prime_factors=fact(a[k]);
			for(auto df:prime_factors)
			{
				if(allfactors.count(df)>0)
				{
					pairwise=0;
					break;
				}
				else
				{
					allfactors.insert(df);
				}
			}
		}
	}
	if(pairwise)
	{
		cout<<"pairwise coprime"<<endl;
	}
	else if(g==1)
	{
		cout<<"setwise coprime"<<endl;
	}
	else
	{
		cout<<"not coprime"<<endl;
	}
}

Measured memory_bytes: 7520
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
const ll mo = 1000000007;
const ll INF = 1LL << 60; //MAX 9223372036854775807
ll ans = 0;

int main()
{
ll n,m;
cin >> n>>m;
vector<pair<ll,ll> > G[n+1];

rep(i, m)
{
    ll a,b,c;
    cin >> a>>b>>c;
    G[a].push_back(pair<ll,ll>(b, c));
}
ll cost[n+1];
rep(i,n+1){
    cost[i]=INF;
}

queue<ll> qq;
qq.push(1);
cost[1]=0;
ll cou=0,cou1=0;
while(!qq.empty()){
    ll p = qq.front();
    qq.pop();
    rep(i,G[p].size()){
        if(cost[G[p][i].first]==INF){
            cost[G[p][i].first]=G[p][i].second+cost[p];
            qq.push(G[p][i].first);
        }
        else{
            if(G[p][i].second+cost[p]>cost[G[p][i].first]){
            cost[G[p][i].first]=G[p][i].second+cost[p];
            if(cou1<1900000){qq.push(G[p][i].first);cou1++;}
            if(G[p][i].first==n)cou++;
            }
            if(n<cou){
                cout << "inf" << endl;
                return 0;
            }
        }
    }
}
cout << cost[n] << endl;
return 0;
}

Measured memory_bytes: 1408
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric;

void main(){
    auto abc=readln.split.map!(to!int).array;
    ((abc[0]<=abc[2]&&abc[2]<=abc[1])?"Yes":"No").writeln;
}

Measured memory_bytes: 256