akhauriyash/Code-Regression huggingface dataset:
#include<cstdio>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define M 256
using namespace std;

int main(){
  int n,l[M],x,y,z;
  while(scanf("%d",&n),n){
    rep(i,n)scanf("%d",&l[i]);
    
    double m = 1e9;
    rep(s,16)rep(a,16)rep(c,16){
      int u[M] = {0};
      
      int r = s;
      rep(i,n){
	r = (a*r + c)%M;
	u[(r + l[i])%M]++;
      }
      
      double tmp = 0;
      rep(i,M)if(u[i]){
	tmp -= u[i]*log((double)u[i]/n);
	if(m + 1e-9 < tmp)break;
      }
      if(m > tmp + 1e-9){ m = tmp; x = s; y = a; z = c; }
    }
    printf("%d %d %d\n",x,y,z);
  }
}

Measured memory_bytes: 1068
#include<bits/stdc++.h>
#define N 100005
#define LL long long
#define mod 1000000007
#define pr pair<int,int>
#define mp(x,y) make_pair(x,y)
using namespace std;

LL read(){
	char c=getchar();
	LL t=0,f=1;
	while(c>'9'||c<'0') f=(c=='-')?-1:1,c=getchar();
	while(c>='0'&&c<='9') t=t*10+c-'0',c=getchar();
	return t*f;
}

int n,Q;
LL a[N],dt[N],t[N],now,tp;

int find(LL v,int lim){
	int l=0,r=lim,mid,ret=0;
	while(l<=r) (a[mid=(l+r)>>1]<=v)?(ret=mid,l=mid+1):(r=mid-1);
	return ret;
}

int main(){
	n=read(),Q=read(),a[++tp]=n;
	for(int i=1;i<=Q;i++){
		LL x=read();
		while(x<=a[tp]) tp--;
		a[++tp]=x; 
	}
	t[tp]=1;
	for(int i=tp;i;i--){
		LL res=a[i],po=find(res,i-1);
		for(;po;po=find(res,po-1)) t[po]+=(res/a[po])*t[i],res%=a[po];
		dt[1]+=t[i],dt[res+1]-=t[i];
	}
	for(int i=1;i<=n;i++) printf("%lld\n",(now+=dt[i]));
}


Measured memory_bytes: 4096
# -*- coding: utf-8 -*-

from collections import deque

X,Y,A,B,C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

P = sorted(P, reverse=True)[:X]
Q = sorted(Q, reverse=True)[:Y]
R = sorted(P+Q+R, reverse=True)[:X+Y]

print(sum(R))

Measured memory_bytes: 23616
#include <stdio.h>
#include <stdlib.h>

#define N	100
#define A	10000000

char composite[A + 1];

void init() {
	int a, b;

	for (a = 2; a <= A / a; a++) {
		if (composite[a])
			continue;
		for (b = a * a; b <= A; b += a)
			composite[b] = 1;
	}
}

int *ov[1 + N], oo[1 + N];

void link(int u, int v) {
	int o = oo[u]++;

	if (o >= 2 && (o & o - 1) == 0)
		ov[u] = (int *) realloc(ov[u], o * 2 * sizeof *ov[u]);
	ov[u][o] = v;
}

int vv[1 + N], uu[1 + N], dd[1 + N];

int bfs(int n) {
	static int qu[1 + N];
	int u, head, cnt;

	for (u = 0; u <= n; u++)
		dd[u] = n;
	head = cnt = 0;
	for (u = 1; u <= n; u++)
		if (!vv[u])
			dd[u] = 0, qu[head + cnt++] = u;
	while (cnt) {
		int d, o;

		u = qu[cnt--, head++], d = dd[u] + 1;
		for (o = 0; o < oo[u]; o++) {
			int v = ov[u][o], w = uu[v];

			if (dd[w] == n) {
				dd[w] = d;
				if (w == 0)
					return 1;
				qu[head + cnt++] = w;
			}
		}
	}
	return 0;
}

int dfs(int u, int n) {
	int d, o;

	if (u == 0)
		return 1;
	d = dd[u] + 1;
	for (o = 0; o < oo[u]; o++) {
		int v = ov[u][o], w = uu[v];

		if (dd[w] == d && dfs(w, n)) {
			vv[u] = v, uu[v] = u;
			return 1;
		}
	}
	dd[u] = n;
	return 0;
}

int hopcroft_karp(int n) {
	int cnt = 0;

	while (bfs(n)) {
		int u;

		for (u = 1; u <= n; u++)
			if (!vv[u] && dfs(u, n))
				cnt++;
	}
	return cnt;
}

int main() {
	static int xx[N], xx0[N], xx1[N];
	int n, n0, n1, m, i, u, v;

	init();
	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &xx[i]);
	n0 = n1 = 0;
	for (i = 0; i < n; i++) {
		if (i == 0 || xx[i - 1] + 1 != xx[i]) {
			if (xx[i] % 2 == 0)
				xx0[n0++] = xx[i];
			else
				xx1[n1++] = xx[i];
		}
		if (i + 1 == n || xx[i] + 1 != xx[i + 1]) {
			if ((xx[i] + 1) % 2 == 0)
				xx0[n0++] = xx[i] + 1;
			else
				xx1[n1++] = xx[i] + 1;
		}
	}
	for (u = 1; u <= n0; u++)
		ov[u] = (int *) malloc(2 * sizeof *ov[u]);
	for (u = 0; u < n0; u++)
		for (v = 0; v < n1; v++) {
			int x = abs(xx0[u] - xx1[v]);

			if (x > 2 && !composite[x])
				link(1 + u, 1 + v);
		}
	m = hopcroft_karp(n0);
	printf("%d\n", m + (n0 + n1 - m * 2) + (n0 - m) % 2);
	return 0;
}


Measured memory_bytes: 11516
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(){

	double r;
	cin >> r;

	cout << fixed << setprecision(6) << M_PI*r*r << " " << 2*M_PI*r << endl;

	return 0;
}

Measured memory_bytes: 3272
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define ROF(i,b,a) for (int i=(b);i>(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,n-1,-1)

typedef int i32;
typedef long long int i64;
typedef unsigned long long int ui64;
typedef float f32;
typedef double f64;
typedef long double f128;
typedef pair<int,int> Pi;

constexpr int INF = 1e9 + 7;
constexpr int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};

struct UnionFind{
    vector<int> data;
    UnionFind(int size): data(size,-1){}
    bool union_set(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_set(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; cin>>N;
    int M; cin>>M;
    int a[M],b[M]; REP(i,M) cin>>a[i]>>b[i];
    int ans=0;
    REP(i,M) {
        UnionFind u=UnionFind(N);
        REP(j,M) if (j!=i) u.union_set(a[j]-1,b[j]-1);
        if (u.size(1)!=N) ans++;
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, Y;
  cin >> N >> Y;
  int x=-1,y=-1,z=-1;

  for(int i=N;i>=0;i--){
    if(10000*i+1000*(N-i)<=Y&&Y<=10000*i+5000*(N-i)){
      for(int j=N-i;j>=0;j--){
        if(10000*i+5000*j+1000*(N-i-j)==Y){
          x = i;
          y = j;
          z = N - i - j;
        }
      }
    }
  }

  cout << x << " " << y << " " << z << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>				//Written by ThiDaiLoc
using namespace std;				//Team Three Wolves
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> trp;
typedef vector<ll> vi;
typedef vector<pll> vp;

#define fu(i,a,b)   for(ll i=a;i<=b;i++)
#define f1(i,n)     for(ll i=1;i<=n;i++)
#define fs(i,s)     for(ll i=0;i+1<=s.length();i++)
#define fd(i,b,a)   for(ll i=b;i>=a;i--)
#define fuv(i,a)    for(ll i=0;i<a.size();i++)
#define fdv(i,a)    for(ll i=(ll)a.size()-1;i>=0;i--)
#define ms(a,x)     memset(a, x, sizeof a)
#define prec(n)     fixed<<setprecision(n)
#define uni(a)      (a).erase(unique(all(a)), (a).end())
#define pb(i)       push_back(i)
#define pob         pop_back()
#define sc(a)       cin>>a
#define sc2(a,b)    cin>>a>>b
#define pr(a)       cout<<a<<endl
#define pr2(a,b)    cout<<a<<" "<<b<<endl
#define rpr(a)      return cout<<a<<endl,0
#define prY         cout<<"YES"<<endl
#define prN         cout<<"NO"<<endl
#define bit(n,i)    (((n)>>(i))&1)
#define lowb(a,n,x) lower_bound(a,a+n,x) -a
#define lowb2(a,x)  lower_bound(all(a),x) -a.begin()
#define all(x)      (x).begin(), (x).end()
#define sz(a)       (ll)a.size()
#define le(s)       (ll)s.length()		
#define re          return 	
#define	mp(a,b)     make_pair(a,b)
#define	mp3(a,b,c)  make_pair(a,make_pair(b,c))
#define se          second
#define fi          first 
#define sse         second.second
#define sfi         second.first
#define soA         sort(A+1,A+1+n)
#define so(A,n)     sort(A+1,A+1+n)
#define sov(v)      sort(all(v))
#define sovr(v)     sort(all(v),greater<ll>())
#define debug(x)    cerr << #x << " = " << x << endl
#define INPUT       freopen("locin.txt", "r", stdin)
#define OUTPUT      freopen("locout.txt", "w", stdout)
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline ll mnz(ll& a,ll b){return a=(a>b?b:a);}
inline ll mxz(ll& a,ll b){return a=(a<b?b:a);}
inline string toString(ll n) {stringstream ss; ss << n;return ss.str();}
double const eps = 1e-6;
ll const Base=1e9+7,oo=1e17,MAXN=1e6;


ll A[MAXN+5];
map<pll,ll> mymap;
ll fac[MAXN+5],revfac[MAXN+5];
ll inline mu(ll a,ll x){
	if(!x)re 1;
	if(x&1)re a*mu(a,x-1)%Base;
	else{
		ll t=mu(a,x>>1);
		re t*t%Base;
	}
}
ll inline nCk(ll n,ll k){
	re fac[n]*(mu(fac[k],Base-2)*mu(fac[n-k],Base-2)%Base)%Base;
}

ll Solves(){
	
	ll n,m,k,cnt=0,ans=0,x,y,q,c,sum=0,v,t;
	// PROCESS IN HERE
	fac[0]=revfac[0]=1;
	fu(i,1,MAXN)fac[i]=(fac[i-1]*i)%Base;
	//fu(i,1,MAXN)revfac[i]=mu(fac[i],Base-2);	debug("a");
	ll X,Y;
	sc2(X,Y);
	if((X+Y)%3==0){
		ll j=(X+Y)/3;
		if(X>=j and X<=j*2){
			rpr(nCk(j,X-j));
		}
	}
	pr(0);
	
	
	
	
	
	
	re 0;
	// Hack it if you can :)
}

int main(){
	//INPUT;
	if(fopen("locin.txt", "r"))INPUT;
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll test=1;
//	sc(test);
	fu(T,1,test){
//		cout<<"Case #"<<T<<": ";
		Solves();
	} 		
}

Measured memory_bytes: 10624
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String input = null;
		try {
			input = br.readLine();
		} catch (IOException e) {
		}
		int n = Integer.parseInt(input);

		for(int i = 1; i <= n; i++){
			if( i % 3 == 0 || String.valueOf(i).indexOf("3") >= 0){
				System.out.print(" " + i);
			}
		}
		System.out.println("");
	}
}



Measured memory_bytes: 27540
#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); }

const int MAXN=100;

int n;
char s[MAXN+1];

bool solve() {
	int nred=0,nblue=0; REP(i,n) if(s[i]=='R') ++nred; else if(s[i]=='B') ++nblue;
	return nred>nblue;
}

void run() {
	scanf("%d%s",&n,s);
	printf("%s\n",solve()?"Yes":"No");
}

int main() {
	run();
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
    int A, B;
    cin >> A >> B;
    int ans = -1;
    for (int i = 1; i <= 10000; ++i)
    {
        int x = floor(i * 0.080);
        int y = floor(i * 0.10);
        if (x == A && y == B)
        {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include <assert.h>
using namespace std;
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define repn(a,b,c) for(int a=b;a>=c;a--)
#define ll long long 
#define sum(a,b) a=a+b
#define mem(a, b) memset(a, (b), sizeof(a))
#define ctz(x) __builtin_ctz(x)
#define clz(x) __builtin_clz(x)
#define popcount(x) __builtin_popcount(x)
#define parity(x) __builtin_parity(x)


int main(){


int n;
cin>>n;


if(n<1200){
	cout<<"ABC"<<endl;
}else if(n<2800){
	cout<<"ARC"<<endl;
}else{
	cout<<"AGC"<<endl;
}

return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#define MAX 100
#define INF 1000000
#define NIL -1

int a[MAX][MAX];
int n;
int d[MAX];
int pi[MAX];

void R(int, int);
void ISS(int);
void D(int);

main(){

  int i, j ,u ,k,v,c;
  scanf("%d",&n);

  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
      a[i][j]=-1;
    }
  }

  for(i=0; i<n; i++){
    scanf("%d %d",&u,&k);
    for(j=0; j<k; j++){
      scanf("%d %d",&v,&c);
      a[i][v]=c;
    }
  }

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

  return 0;
}

void R(int u, int v){
  if(d[v]> d[u]+a[u][v]){
    d[v]=d[u]+a[u][v];
    pi[v]=u;
  }
}

void ISS(int s){
  int v;
  for(v=0; v<n; v++){
    d[v]=INF;
    pi[v]=NIL;
  }
  d[s]=0;
}

void D(int s){
  int i, v1, v, minv, u;
  int flag[MAX];
  ISS(s);
 
  for(i=0; i<n; i++) flag[i]=0;

  for(v1=0; v1<n; v1++){
   
    minv = INF;
    for ( i = 0; i < n; i++ ){
      if(flag[i]==1) continue;
      if(d[i]<=minv){
    u=i;
    minv=d[i];
      }
    }
   
    for(v=0; v<n; v++){
      if(a[u][v]==-1) continue;
      if(flag[v]==1) continue;

      R(u, v);
    }
    flag[u]=1;
  }
}

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



int main() {
  int N; cin >> N;
  string s[N];  for(int i=0;i<N;i++){cin >> s[i];}
  int M; cin >> M;
  string t[M]; for(int i=0;i<M;i++){cin >> t[i];}
  int r = 0;
  for(int num=0;num<N;num++){
    string say = s[num];
    int count = 0;
    for(int i=0;i<N;i++){
      count += (say==s[i]);
    }
    for(int i=0;i<M;i++){
      count -= (say==t[i]);
    }
    if(count>r){
      r = count;
    }
  }

  cout << r << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
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; }
typedef long long ll;
using namespace std;
const int MAX_N = 1e+5;
int par[MAX_N];//親
int pank[MAX_N];//木の深さ

//n要素で初期化
void init(int n){
    for(int i=0;i<n;i++){
        par[i]=i;
        pank[i]=0;
    }
}

//木の根を求める
int find(int x){
    if(par[x]==x){
        return x;
    }else{
        return par[x]=find(par[x]);
    }
}

//xとyの属する集合を併合
void unite(int x,int y){
    x = find(x);
    y = find(y);
    if(x==y)return;
    if(pank[x]<pank[y]){
        par[x]=y;
    }else{
        par[y]=x;
        if(pank[x]==pank[y])pank[x]++;
    }
}

//xとyが同じ集合に属するか否か
bool same(int x,int y){
    return find(x)==find(y);
}
int main(void){
    int n,m,k;cin>>n>>m>>k;
    init(n);
    map<int,int> mp,f;
    rep(i,m){
        int a,b;cin>>a>>b;a--;b--;
        unite(a,b);
        f[a]++;
        f[b]++;
    }
    vector<vector<int>> b(n);
    rep(i,k){
        int c,d;cin>>c>>d;c--;d--;
        b[c].push_back(d);
        b[d].push_back(c);
    }
    rep(i,n){
        int x=find(i);
        mp[x]++;
    }
    rep(i,n){
        int x=find(i);
        int res=mp[x]-f[i]-1;
        rep(j,b[i].size()){
            if(x==find(b[i][j]))res--;
        }
        cout<<res<<" ";
    }
    cout<<endl;
}

Measured memory_bytes: 14208
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,cnt=0,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n-2;i++)
    {
        if((a[i]<a[i+1]&&a[i+1]<a[i+2])||(a[i]>a[i+1]&&a[i+1]>a[i+2]))
           {
               cnt++;
           }
    }
    cout<<cnt<<endl;
    return 0;
}


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

using ll = long long;

int main() {
    int N;
    cin >> N;
    string s;
    map<string, int> m{};
    rep(i, N) {
        cin >> s;
        sort(s.begin(), s.end());
        //cout << s << endl;
        m[s]++;
    }
    ll ans = 0;
    for (auto itr = m.begin(); itr != m.end(); ++itr) {
        //cout << itr->first << " " << itr->second << endl;
        ll a = itr->second;
        ans += a * (a - 1) / 2;
    }
    cout << ans << endl;
}

Measured memory_bytes: 11136
use std::cmp::max;
use std::collections::HashSet;

fn read<T: std::str::FromStr>(
    stdin_lock: &mut std::io::StdinLock,
    buf: &mut Vec<u8>,
    delimiter: u8,
) -> T {
    buf.clear();
    let l = std::io::BufRead::read_until(stdin_lock, delimiter, buf).unwrap();
    buf.truncate(l - 1); // remove delimiter
    let s = unsafe { std::str::from_utf8_unchecked(&buf) };
    s.parse().unwrap_or_else(|_| panic!("read"))
}

fn main() {
    let stdin = std::io::stdin();
    let mut stdin_lock = stdin.lock();
    let mut buf: Vec<u8> = Vec::new();
    let n: usize = read(&mut stdin_lock, &mut buf, b'\n');
    let s: String = read(&mut stdin_lock, &mut buf, b'\n');
    let cs: Vec<char> = s.chars().collect();
    let mut ans = 0;
    for i in 1..n {
        let mut set1 = HashSet::new();
        let mut set2 = HashSet::new();
        for j in 0..i {
            set1.insert(cs[j]);
        }
        for j in i..n {
            set2.insert(cs[j]);
        }
        ans = max(ans, set1.intersection(&set2).count());
    }
    println!("{}", ans);
}


Measured memory_bytes: 4352
N = int(input())
v = list(map(int, input().split()))

import heapq

heapq.heapify(v)

for i in range(N-1):
  a = heapq.heappop(v)
  b = heapq.heappop(v)
  heapq.heappush(v, (a+b)/2)
print(v[0])

Measured memory_bytes: 3064
// scanf 等でエラーを出さないように追加
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <algorithm>
#include <numeric>
#include <set>
#include <functional>
#include <utility>


using namespace std;


typedef long long ll;


int a, b, c;
int ink[101];

int main() {

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

	ink[a] = 1;
	ink[b] = 1;
	ink[c] = 1;

	int answer = 0;

	for (int i = 0; i < 101; i++) {
		if (ink[i] > 0){
			answer++;
		}
	}


	cout << answer << endl;

	return 0;
}

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

char t[20][20];

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

int countPuyo(int x, int y){
  int res = 1;
  char tmp = t[y][x];
  t[y][x] = '.';

  for(int i = 0; i < 4; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];

    if(nx < 0 || 6 <= nx || ny < 0 || 12 <= ny) continue;
    if(t[ny][nx] != tmp) continue;

    res += countPuyo(nx, ny);
  }
  t[y][x] = tmp;

  return res;
}

void deletePuyo(int x, int y){
  char tmp = t[y][x];
  t[y][x] = '.';

  for(int i = 0; i < 4; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];

    if(nx < 0 || 6 <= nx || ny < 0 || 12 <= ny) continue;
    if(t[ny][nx] == 'O'){
      t[ny][nx] = '.';
    }
    if(t[ny][nx] != tmp) continue;

    deletePuyo(nx, ny);
  }
}

void fallPuyo(){
  for(int i = 11; i > 0; i--){
    for(int j = 0; j < 6; j++){
      if(t[i][j] == '.'){
	int k;

	for(k = i; k > 0; k--){
	  if(t[k][j] != '.'){
	    break;
	  }
	}

	t[i][j] = t[k][j];
	t[k][j] = '.';
      }
    }
  }
}

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

  while(T--){
    for(int i = 0; i < 12; i++){
      cin >> t[i];
    }

    int rensa = 0;

    while(true){
      bool flg = false;

      for(int i = 0; i < 12; i++){
	for(int j = 0; j < 6; j++){
	  if(t[i][j] != '.' && t[i][j] != 'O' &&
	     countPuyo(j, i) >= 4){
	    deletePuyo(j, i);
	    flg = true;
	  }
	}
      }

      if(!flg) break;
      fallPuyo();
      rensa++;
    }

    cout << rensa << endl;
  }
}

Measured memory_bytes: 1164
import java.util.Scanner;

public class Main {

	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		long h = sc.nextInt();
		long w = sc.nextInt();
		
		if(h%3==0||w%3==0)
		{
			System.out.println(0);
			return;
		}
		
		long an = 999999999999999l;
		an = Math.min(h, w);
		
		//vertical first
		for(int x = 1; x < w; x++)
		{ //x: width of first piece
			long firstpiece = x*h;
			long secpiece = (w-x)*(h/2);
			long thirpiece = (w-x)*(h/2 + (h%2));
			long max = Math.max(firstpiece, secpiece);
			max = Math.max(max, thirpiece);
			long min = Math.min(firstpiece, secpiece);
			min = Math.min(min, thirpiece);
			an = Math.min(an, max-min);
			
//			System.out.println("x: " + x + ", " + max + ", " + min);
		}
		
		//horizontal first
		for(int y = 1; y < h; y++)
		{ //y: height of first piece
			long firstpiece = y*w;
			long secpiece = (h-y)*(w/2);
			long thirpiece = (h-y)*(w/2 + (w%2));
			long max = Math.max(firstpiece, secpiece);
			max = Math.max(max, thirpiece);
			long min = Math.min(firstpiece, secpiece);
			min = Math.min(min, thirpiece);
			an = Math.min(an, max-min);
		}
		
		System.out.println(an);
	}

}


Measured memory_bytes: 23892
use v5.18;
use warnings;
use Math::BigInt;
use List::Util 'reduce';

my ($n, $e, $f);
{
    my $in = <STDIN>;
    ($n, $e, $f) = split(" ", $in);
}
my $MOD = 10 ** 9 + 7;

sub combination
{
    my ($n, $k) = @_;
    my $c = reduce {$a * $b % $MOD} (($n-$k+1)..$n);
    my $d = reduce {$a * $b % $MOD} (1..$k);

    $c * Math::BigInt->new($d)->bmodinv($MOD) % $MOD;
}
my $ans = Math::BigInt->new(2)->bmodpow($n, $MOD) - 1;
$ans += $MOD - combination($n, $e);
$ans += $MOD - combination($n, $f);
say $ans % $MOD;

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

const int MAXN = 105;
int N;
int a[MAXN];

int main() {
	// freopen("./input.txt", "r", stdin);
    cin >> N;
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        sum += a[i];
    }
    double avg = 1.0 * sum / N;
    double mind = INT_MAX;
    int ans;
    for (int i = 0; i < N; i++) {
        double diff = abs((double)a[i] - avg);
        if (diff < mind) {
            mind = diff;
            ans = i;
        }
    }
    cout << ans << endl;

	return 0;
}


Measured memory_bytes: 764
#include<algorithm> // sort,reverse,min,lower_bound
#include<cmath>
#include<iostream>
using namespace std;
#define df 0

int main(){
  if(df) printf("*debug mode*\n");
  int dict[]={1,8,27,32,125,128,216,243,343,512,729,1000};
  int a; cin >> a;
  auto p=upper_bound(dict,dict+12,a)-1;
  int s=sqrt(a);if(df)printf("%d\n",s);
  s*=s;
  if(s<*p)s=*p;
  cout << s;
}

/// confirm df==0 ///


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
 
#define int long long
#define pb emplace_back
#define mp make_pair
#define f(i,a,n) for(int i=a ; i<n ; i++)
#define F first
#define S second
#define fast ios:: sync_with_stdio(false),cin.tie(0);
 
int32_t main(){
	fast;
    int a,b;
    cin >> a >> b;
    cout << max({a+b , a-b , a*b});
}
 
 

Measured memory_bytes: 256
def dfs(v):
    global t, d, f, visited
    d[v] = t
    visited[v] = True
    t += 1
    for u in AL[v]:
        if not visited[u]:
            dfs(u)
    f[v] = t
    t += 1

# 入力
n = int(input())
AL = [[] for _ in range(n + 1)]
for v in range(1, n + 1):
    AL[v] = [int(x) for x in input().split()][2:]

# 計算
d = [0] * (n + 1)
f = [0] * (n + 1)
t = 1
visited = [False] * (n + 1)
for v in range(1, n + 1):
    if not visited[v]:
        dfs(v)

# 出力
for v in range(1, n + 1):
    print(v, d[v], f[v])



Measured memory_bytes: 5656
#include<cstdio>
#include<string>
#include<vector>
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<cwctype>
#include<algorithm>
#include<math.h>

using namespace std;

int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(n==0 && m==0)break;
		int map[1100]={0};
		int dice[1001]={0};
		for(int i=1;i<=n;i++){
			cin>>map[i];
		}
		for(int i=0;i<m;i++){
			cin>>dice[i];
		}
		int count=1;
		for(int i=0;i<m;i++){
			count +=dice[i];
			count+=map[count];
			if(count>=n){

				cout<<i+1<<endl;
				break;
			}
		}

	}

	return 0;
}

Measured memory_bytes: 1168
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args) {
#if DEBUG
        MyDebugger.Test(process, "../TextFile1.txt");
        //MyDebugger.Test(process, "../TextFile2.txt");
        //MyDebugger.MakeTestCase("../TextFile2.txt");
#else
        object ret = process(new StreamReader(Console.OpenStandardInput()));
        Console.WriteLine(ret);
#endif
    }

    static object process(TextReader input) {
        var n = long.Parse(input.ReadLine());

        var ary = new long[5];
        ary[0] = long.Parse(input.ReadLine());
        ary[1] = long.Parse(input.ReadLine());
        ary[2] = long.Parse(input.ReadLine());
        ary[3] = long.Parse(input.ReadLine());
        ary[4] = long.Parse(input.ReadLine());

        var min = long.MaxValue;
        var minp = 0;

        for(int i=0; i<ary.Length; i++) {
            if(ary[i] < min) {
                min = ary[i];
                minp = i;
            }
        }

        if(n % min == 0) {
            return 4 + n / min;
        } else {
            return 4 + n / min + 1;
        }
    }
}

Measured memory_bytes: 13140
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); 
		while(sc.hasNext()){
			int m = sc.nextInt();
			int n = sc.nextInt();
			if(m + n == 0)break;
			boolean[] member = new boolean [n+1];
			for(int i=1;i<=n;i++) {
				member[i] = false;
			}
			
			for(int i=1;i<=n;i++) {
				String str = sc.next();
				if(i%3==0 && i%5==0 && str.equals("FizzBuzz")) 
					member[i] = true;
				else if(i%3==0 && str.equals("Fizz"))
					member[i] = true;
				else if(i%5==0 && str.equals("Buzz"))
					member[i] = true;
				else if(i%3 != 0 && i%5!=0 && str.equals(String.valueOf(i)))
					member[i] = true;
				else member[i] = false;
			}
			print(member,m,n);
		}
	}
	
	private static void print(boolean[] a,int m,int n) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for(int i=1;i<=m;i++) {
			list.add(Integer.valueOf(i));
		}
		
		for(int i=1;i<=n;i++) {
			if(a[i] == true){
				int o = list.get(0);
				list.remove(0);
				list.add(o);
			}
			else {
				list.remove(0);
				m--;
			}
			if(m==1)break;
		}
		
		
		Collections.sort(list);
		
		for(int i=0;i<m;i++) {
			int o = list.get(0);
			list.remove(0);
			if(i==0)System.out.print(o);
			else System.out.print(" "+o);
		}
		System.out.println();
		list.clear();
	}
}



Measured memory_bytes: 74428
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigDecimal X = in.nextBigDecimal();
        BigDecimal yen = new BigDecimal("100");
        BigDecimal rate = new BigDecimal("1.01");
        int count = 0;
        while (yen.compareTo(X)<0){
            yen = new BigDecimal(yen.multiply(rate).toString().split("\\.")[0]);
            count++;
        }
        System.out.print(count);
    }
}

Measured memory_bytes: 41136
X, N= [int(x) for x in input().split()]
P = [int(x) for x in input().split()]

P_sub_abs = [abs(i - X) for i in P]
if(P_sub_abs.count(0) == 0):
    print(X)
    exit()
for i in range(1, 100):
    if(P_sub_abs.count(i) == 1):
        list = [X - i, X + i]
        list.remove(P[P_sub_abs.index(i)])
        print(list[0])
        exit()
    elif(P_sub_abs.count(i) == 0):
        print(X - i)
        exit()


Measured memory_bytes: 9204
import sys
# from collections import defaultdict, deque
import math
# import copy
# from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 1000000007

def solve():
    a, b, n = getList()
    if n >= b-1:
        x = b - 1
    else:
        x = n
    print(math.floor((a * x) / b) - a * math.floor(x/b))
    return
def main():
    n = getN()
    for _ in range(n):
        solve()
if __name__ == "__main__":
    # main()
    solve()


Measured memory_bytes: 62360
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
#define MAX 200000
#define INFTY 9000000000

int main(){
  int N,maxL=0,sum=0,data;
  cin>>N;
  for(int i=0;i<N;i++){
    cin>>data;
    sum+=data;
    maxL=max(maxL,data);
  }
  sum-=maxL;
  if(maxL<sum) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;int main(){int N;cin >> N;map<pair<int, int>, int> mp;for (int i = 0, y, x, w; i < N; i++) cin >> y >> x >> w, mp[{y, x}] += w, mp[{y - 1, x}] += w, mp[{y, x - 1}] += w, mp[{y - 1, x - 1}] += w;int ans = 0;for (const auto& p : mp) ans = max(ans, p.second);cout << ans << " / " << 1 << endl;return 0;}


Measured memory_bytes: 28108
N = int(input())
A = [int(input()) for _ in range(N)]

maximum = max(A)
maximum_index = A.index(maximum)
second = max(A[:maximum_index] + A[maximum_index+1:])

for i in range(N):
    if i == maximum_index:
        print(second)
    else:
        print(maximum)

Measured memory_bytes: 14124
S = int(input())
MOD = 10 ** 9 + 7
ans = 0
N = S

fact = [1] * (N + 1)
rfact = [1] * (N + 1)
r = 1

for i in range(1, N + 1):
    fact[i] = r = r * i % MOD

rfact[N] = r = pow(fact[N], MOD - 2, MOD)
for i in range(N, 0, -1):
    rfact[i - 1] = r = r * i % MOD


# nCk (mod MOD) を求める
def comb0(n, k):
    return fact[n] * rfact[k] * rfact[n - k] % MOD


for i in range(S // 3):
    r = S - (i + 1) * 3
    ans = (ans + comb0(i + r, r)) % MOD

print(ans)

Measured memory_bytes: 64652
#include<cstdio>
#include<algorithm>
using namespace std;
char p[120];
int D[120];
int main(){
    int i, j, n, res = 1e9;
    scanf("%s",p);
    for(i=0;p[i];i++);
    n = i;
    for(i='a';i<='z';i++){
        for(j=0;p[j];j++){
            D[j] = 1e9;
            if(p[j]==i)D[j]=0;
        }
        D[n] = 1e9;
        int M = 0;
        for(j=n-1;j>=0;j--){
            D[j] = min(D[j], D[j+1] + 1);
        }
        for(j=0;j<n;j++){
            M = max(M,D[j]);
            res = min(res, max(n-j-1,M));
        }
    }
    printf("%d\n",res);
}

Measured memory_bytes: 128
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    #n=int(readline())
    ss=readline().rstrip()
    n=0
    for s in ss:
        n+=int(s)
    #yn(1)
    if n%9 == 0:
        print("Yes")
    else:
        print("No")

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

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

int main() {
	int N;
	cin >> N;  
	string S;
	cin >> S;
	int ans = 1;
	for (int n = 1;n < S.size();++n) {
		if (S[n-1] != S[n]) {
			++ans;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3708
#include<stdio.h>

int main(){

int n;
int x,s=0;

while(1){
scanf("%d",&n);
if(n==0)break;
for(int i=0;i<n/4;i++)
{scanf("%d",&x);
s+=x;}

printf("%d\n",s);
s=0;
}
return 0;
}

Measured memory_bytes: 1028
import itertools
n, m = map(int, input().split())
e = set([tuple(sorted(map(int, input().split()))) for x in range(m)])
cnt = 0
for i in itertools.permutations(range(2, n+1), n-1):
    l = [1]+list(i)
    if sum(1 for x in range(n-1) if tuple(sorted(l[x: x+2])) in e) == n-1:
        cnt += 1
print(cnt)

Measured memory_bytes: 3064
#include<stdio.h>
int main(void)
{
	int n,c;
	while(1){
	scanf("%d",&n);
	if(n==0) break;
	c=0;
	n=1000-n;
	while(n>=500){
		n=n-500;
		c++;
	}
	while(n>=100){
		n=n-100;
		c++;
	}
	while(n>=50){
		n=n-50;
		c++;
	}	
	while(n>=10){
		n=n-10;
		c++;
	}
	while(n>=5){
		n=n-5;
		c++;
	}	
	while(n>=1){
		n=n-1;
		c++;
	}
	printf("%d\n",c);
	}
	return 0;
}

Measured memory_bytes: 2716
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
 
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gp_hash_table<int, int> mp;*/
using ll = long long;
using ld = long double;
const ll MOD = 998244353;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const double eps = -1e6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
ttgl a[mxN];
ttgl b[mxN];
int back[mxN];
int n;
int main()
{   
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>n;
    owo(i, 0, n) {
        cin>>a[i].first;
        a[i].second= i;
    }
    owo(i, 0, n ){
        cin>>b[i].first;
        b[i].second = i;
    }
    sort(a, a+n);
    sort(b, b+n);
    owo(i, 0, n) {
        back[a[i].second] = i;
        //cout<<a[i].second<<" "<<a[i].first<<"\n";
    }
    bool ok = false;
    owo(i, 0, n) {
        if(a[i].first>b[i].first) {
            cout<<"No\n";
            return 0;
        }
        if(i==n-1)break;
        if(i>0&&a[i].first<=b[i-1].first){
          ok = true;
        }
        if(a[i].second==b[i].second) {
          ok = true;
        }else {
          //cout<<a[i].second<<" "<<a[back[b[i].second]].second<<"\n";
          //cout<<i<<" "<<back[b[i].second]<<"\n";
          swap(a[i].second, a[back[b[i].second]].second);
          swap(back[a[i].second], back[a[back[b[i].second]].second]);
        }
    }
    if(ok) {
        cout<<"Yes\n";
    }else {
        cout<<"No\n";
    }
    return 0;
}


Measured memory_bytes: 5516
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
        MyScanner in = new MyScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, MyScanner in, PrintWriter out) {
            int N = in.nextInt();
            for (int i = N; i <= 999; i++) {
                if (i / 100 == (i / 10) % 10 && (i / 10) % 10 == i % 10) {
                    out.println(i);
                    return;
                }
            }
        }

    }

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

        public MyScanner(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();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}



Measured memory_bytes: 21588
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

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


Measured memory_bytes: 256
#include<iostream>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int n, x[1000], ans[1000];
vector<vector<int>> e;

void rec(int c, int p){
	vector<int> dp(5010, 1e9);
	dp[0] = 0;
	for(int i : e[c]) if(i != p){
		rec(i, c);
		vector<int> next(5010, 1e9);
		for(int j = 0; j < 5010; j++){
			if(j + x[i] < 5010) next[j + x[i]] = min(next[j + x[i]], dp[j] + ans[i]);
			if(j + ans[i] < 5010) next[j + ans[i]] = min(next[j + ans[i]], dp[j] + x[i]);
		}
		dp.swap(next);
	}
	ans[c] = 1e9;
	rep(i, x[c]+1) ans[c] = min(ans[c], dp[i]);
}

int main(){
	cin >> n; e.resize(n);
	for(int i = 1; i < n; i++){
		int p; cin >> p; p--;
		e[i].push_back(p);
		e[p].push_back(i);
	}
	rep(i, n) cin >> x[i];
	rec(0, 0);
	cout << (ans[0] < 1e9 ? "POSSIBLE" : "IMPOSSIBLE") << endl;
	//rep(i, n) cerr<<"i:"<<i<<" ans:"<<ans[i]<<endl;
	return 0;
}


Measured memory_bytes: 8748
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define sz(x) int(x.size())
using ll = long long;
const ll LLINF = numeric_limits<long long>::max();
const int mod = 1000000007;
const int INF = 1001001001;
 
 
 
int main()
{
    int N;
    string S;
    cin >> N >> S;
    ll ans = 0;
    
    vector<int> r;
    vector<int> g;
    vector<int> b;
 
    for(int i=0;i<N;i++){
        if(S[i]=='R'){
            r.push_back(i+1);
        }
        else if(S[i]=='G'){
            g.push_back(i+1);
        }
        else if(S[i]=='B'){
            b.push_back(i+1);
        }
    }
 
    ans = r.size() * g.size() * b.size();
 
    for (int j=0;j<N;j++){
        for(int l=1;l<=N/2;l++){
            int i=j-l;
            int k=j+l;
 
            if(k>=N || i<0){
                continue;
            }
            if(S[i]!=S[j] && S[i]!=S[k] && S[j]!=S[k]){
                // cout << i << " " << j << " " << k << endl;
                ans--;
            }
        }
    }
    
    cout << ans<<endl;
    return 0;
}

Measured memory_bytes: 3632
import re

s = input()
ans = 0
s = s.replace("BC", "D")
L = re.split('[BC]', s)
for i in L:
  k = len(i)
  temp = 0
  for j in range(k):
    if i[k-1-j] == "D":
      temp += 1
    else:
      ans += temp
print(ans)

Measured memory_bytes: 5668
#include <stdio.h>
#include <math.h>

int main(){
    unsigned long long int n, sum=0;

    scanf("%llu", &n);

    for(unsigned long long int a=1; a<n; ++a){
        if(n%a != 0){
            sum += n/a;
        }else{
            sum += (n/a - 1);
        }
        // printf("%llu %llu\n", a, n/a);
    }

    printf("%llu", sum);
}

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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  int n , l ;; 
  cin >> n >> l  ;
  vector<string> v(n) ;
  
  rep(i,n)
    cin >>v.at(i);
  
  sort(all(v));
  
  rep(i,n)
    cout << v.at(i);


}


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

#define REP(i, stop) FOR(i, 0, stop)
#define FOR(i, start, stop) for (int i = start, i##_len = stop; i < i##_len; ++i)

struct InitCpp { InitCpp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } initCpp;

template<typename G> class LCA { private: int n; int l; std::vector<std::vector<int>> P; std::vector<int> D; void dfs(const G &g, int v, int p, int d) { P[0][v] = p; D[v] = d; for (auto &to : g[v]) { if (to != p) dfs(g, to, v, d + 1); } } public: LCA(const G &g): n(g.size()), l(log2(n) + 1), P(l, std::vector<int>(n)), D(n) { dfs(g, 0, -1, 0); for (int k = 0; k + 1 < l; ++k) { for (int v = 0; v < g.size(); ++v) { if (P[k][v] < 0) P[k + 1][v] = -1; else P[k + 1][v] = P[k][P[k][v]]; } } } int query(int u, int v) { if (D[u] > D[v]) std::swap(u, v); for (int k = 0; k < l; ++k) { if ((D[v] - D[u]) >> k & 1) { v = P[k][v]; } } if (u == v) return u; for (int k = l - 1; k >= 0; --k) { if (P[k][u] != P[k][v]) { u = P[k][u]; v = P[k][v]; } } return P[0][u]; } };

using Graph = vector<vector<int>>;
signed main() {
  int n;
  cin >> n;
  Graph G(n);
  REP(i, n) {
    int k;
    cin >> k;
    REP(_, k) {
      int c;
      cin >> c;
      G[i].push_back(c);
    }
  }
  LCA<Graph> lca(G);
  int q;
  cin >> q;
  REP(_, q) {
    int u, v;
    cin >> u >> v;
    cout << lca.query(u, v) << endl;
  }
}



Measured memory_bytes: 18444
import std.algorithm;
import std.bigint;
import std.concurrency;
import std.container;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;


auto solve(long N, long M, long[] A) {
    auto heap = make!(RedBlackTree!(long, "a > b", true))(A);
    
    foreach (_; 0 .. M) {
        auto f = heap.front;
        heap.removeFront;
        heap.insert(f / 2);
    }

    return heap[].sum;
}


void main() {
    auto input = stdin.byLine.map!split.joiner;

    long N;
    N = input.front.to!long;
    input.popFront;

    long M;
    M = input.front.to!long;
    input.popFront;

    long[] A = new long[](cast(size_t) (N));
    foreach (i; 0 .. cast(size_t) (N)) {
        A[i] = input.front.to!long;
        input.popFront;
    }

    static if (is(ReturnType!solve == void))
        solve(N, M, A);
    else
        solve(N, M, A).writeln;
}


Measured memory_bytes: 20904
N, M, X, Y = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
last_x = sorted(x)[-1]
farst_y = sorted(y)[0]
if (last_x < Y) and (X < farst_y) and (last_x < farst_y):
  print("No War")
else:
  print("War")

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
#define pb push_back
#define ios                           \
    std::ios::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define fill(x, v) memset(x, v, sizeof(x))
const ll mod = 1000000007;
const double pi = 3.14159265359; // PI
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
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...);
}
/*
std::cout << std::setprecision(9) << f << '\n';//3.141590000;
map<char,int>::reverse_iterator rit;
for(rit = mp.rbegin(); rit!=mp.rend(); rit++)
*/
int power(int x, unsigned int y, int p)
{
    int res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
// string parser
vector<int> parse(string s)
{
    stringstream ss(s);
    int x;
    vector<int> v;
    while (ss >> x)
    {
        v.pb(x);
    }
    return v;
}
//-----------------------------

int main()
{
    ios;
    int k, a, b;
    cin >> k >> a >> b;
    int d = a / k;
    d *= k;
    if (d == a || (d + k) <= b && (d + k) >= a)
    {
        cout << "OK" << endl;
    }
    else
        cout << "NG" << endl;
}


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

int dp[100010];

int main(){
    int n;
    cin>>n;
    dp[0]=0;
    for(int i=1; i<=n; ++i){
        dp[i]=1e9;
        int power=1,power2=1;
        while(power<=i){
            dp[i]=min(dp[i],dp[i-power]+1);
            power*=6;
        }
        while(power2<=i){
            dp[i]=min(dp[i],dp[i-power2]+1);
            power2*=9;
        }
    }
    cout<<dp[n]<<endl;
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); i++)
int main()
{
    int a, b, c, k;
    cin >> a >> b >> c >> k;

    int m[3] = {a, b , c};
    sort(m, m+3);

    int max_figure = m[2];
    rep(i, k){
        max_figure *= 2;
    }
    int ans = max_figure;
    ans += m[0] + m[1];

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
n, k = map(int, input().split())
d = []
a = []
sunuke = [int(i)+1 for i in range(n)]

for i in range(k):
  d.append(int(input()))
  for i in input().split():
    a.append(int(i))

ans = 0

for i in sunuke:
  if i not in a:
    ans += 1

print(ans)

Measured memory_bytes: 9236
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(),(x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define MAX (100000)
#define pii pair<int, int>

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

class XY{
public:
  ll x, y;
  XY() {x = y = 0;}
  XY(ll u, ll v) {x = u; y = v;}
};

template<typename T1, typename T2>
void chmin(T1 &a, T2 b) {if(a > b) a = b;}
template<typename T1, typename T2>
void chmax(T1 &a, T2 b) {if(a < b) a = b;}

template<typename T1, typename T2>
T1 mypow(T1 a, T2 n){
  if(n == 0) return 1;
  if(n == 1) return a;
  if(n % 2) return a * mypow(a, n - 1);
  T1 tmp = mypow(a, n / 2);
  return tmp * tmp;
}

template<typename T>
int BS(vector<T> V, int left, int right, T key){
  int mid = (left + right) / 2;
  if(V[mid] <= key) left = mid;
  else right = mid;
  if(right - mid == 1) return left;
  else return BS(V, left, right, key);
}

bool isprime(int N){
  if(N == 1) return false;
  int n = 2;
  while(n != N){
    if(N % n == 0) return false;
    n++;
  }
  return true;
}

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

void Main(){
  int H, W, D; cin >> H >> W >> D;

  map<int, XY> place;
  rep(i, 1, H + 1) rep(j, 1, W + 1) {
    XY tmp;
    tmp.x = i;
    tmp.y = j;
    int n; cin >> n;
    place.insert(make_pair(n, tmp));
  }

  map<int, int> ma;
  rep(i, 1, D + 1){
    int n = i;
    ma[n] = 0;
    n += D;
    while(n <= H * W){
      ma[n] = ma[n - D] + abs(place[n].x - place[n - D].x) + abs(place[n].y - place[n - D].y);
      n += D;
    }
  }

  int Q; cin >> Q;
  rep(q, 0, Q){
    int l, r; cin >> l >> r;
    cout << ma[r] - ma[l] << endl;
  }
}

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

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);

  Main();
}

Measured memory_bytes: 11008
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  ll n;
  R n;
  ll a[n];
  rep(i,n) R a[i];
  ll x=0,ans=0;
  rep(i,n) {
    ans+=max(0LL,a[i]-x);
    x=a[i];
  }
  pr(ans);
}

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


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll maxn=2e3+1;
const ll INF=1e16;
const ll MOD=1e9+7;
vector<ll> adj_list[maxn];
bool visited[maxn];
ll c[maxn];
ll N;
void bfs(ll s)
{
	ll dist[maxn]={0};
	queue<ll> q;
	q.push(s);
	visited[s]=true;
	dist[s]=0;
	while(!q.empty())
	{
		ll x=q.front();
		q.pop();
		for(ll i=0;i<adj_list[x].size();i++)
		{
			if(!visited[adj_list[x][i]])
			{
				dist[adj_list[x][i]]=dist[x]+1;
				visited[adj_list[x][i]]=true;
				q.push(adj_list[x][i]);
			}
		}
	}
	for(ll i=1;i<=N;i++)
	{
		c[dist[i]]++;
	}
}
void solve()
{
	ll X,Y;
	cin>>N>>X>>Y;
	memset(visited,false,sizeof(visited));
	memset(c,0,sizeof(c));
	for(ll i=1;i<N;i++)
	{
		adj_list[i].push_back(i+1);
		adj_list[i+1].push_back(i);
	}
	adj_list[X].push_back(Y);
	adj_list[Y].push_back(X);
	for(ll i=1;i<=N;i++)
	{
		memset(visited,false,sizeof(visited));
		bfs(i);
	}
	for(ll i=1;i<=N-1;i++)
	{
		cout<<c[i]/2<<"\n";
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
	return 0;
}

Measured memory_bytes: 384