akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <bitset>
#include <cctype>
using namespace std;

string ABCD;
string ans;

void bitSearch(){
    
    for(int i = 0; i < (1 << 3); i++){
        
        bitset<3> bit(i);
        
        int sum = ABCD[0] - '0';
        ans = ABCD[0];
        
        for(int j = 0; j < 3; j++){
            if( bit.test(j) ){
                sum += ABCD[j+1] - '0';
                ans += "+";
                ans += ABCD[j+1];
            }
            else{
                sum -= ABCD[j+1] - '0';
                ans += "-";
                ans += ABCD[j+1];
            }
        }
        
        if(sum == 7){
            cout << ans << "=7" << endl;
            return;
        }
        
    }
    return;
    
}



int main(void){
    
    cin >> ABCD;
    
    bitSearch();
    
    return 0;
    
}


Measured memory_bytes: 3092
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;


signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  int a[3];
  cin>>a[0]>>a[1]>>a[2];
  sort(a,a+3);
  if(a[0]==a[1] && a[0]!=a[2]) cout<<"Yes"<<endl;
  else if(a[2]==a[1] && a[0]!=a[2])cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  

  return 0;
}


Measured memory_bytes: 256
// #pragma GCC target("avx2")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-10;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3121000;
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
long long Comb(int a,int b){
 	if(a<b||b<0)return 0;
 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
}
void init_C(int n){
	fact[0]=finv[0]=inv[1]=1;
	for(int i=2;i<n;i++){
		inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
	}
	for(int i=1;i<n;i++){
		fact[i]=fact[i-1]*i%mod;
		finv[i]=finv[i-1]*inv[i]%mod;
	}
}
long long pw(long long a,long long b){
	if(a<0LL)return 0;
	if(b<0LL)return 0;
	long long ret=1;
	while(b){
		if(b%2)ret=ret*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return ret;
}
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
double ABS(double a){return max(a,-a);}
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
// ここから編集しろ
int rev[3100][3100];
int dp[3100][3100];
char s[3100];
char t[3100];
char ans[3100];
int main(){
	scanf("%s%s",s,t);
	int N=strlen(s);
	int M=strlen(t);
	for(int i=0;i<3100;i++)for(int j=0;j<3100;j++)
		dp[i][j]=-mod;
	dp[0][0]=0;
	for(int i=0;i<=N;i++){
		for(int j=0;j<=M;j++){
			if(i+1<=N){
				if(dp[i+1][j]<dp[i][j]){
					dp[i+1][j]=dp[i][j];
					rev[i+1][j]=0;
				}
			}
			if(j+1<=M){
				if(dp[i][j+1]<dp[i][j]){
					dp[i][j+1]=dp[i][j];
					rev[i][j+1]=1;
				}
			}
			if(i+1<=N&&j+1<=M&&s[i]==t[j]){
				if(dp[i+1][j+1]<dp[i][j]+1){
					dp[i+1][j+1]=dp[i][j]+1;
					rev[i+1][j+1]=2;
				}
			}
		}
	}
	int ns=N;
	int nt=M;
	int at=dp[N][M]-1;
	while(ns||nt){
		if(rev[ns][nt]==0)ns--;
		else if(rev[ns][nt]==1)nt--;
		else{
			ns--;nt--;
			ans[at]=s[ns];
			at--;
		}
	}
	printf("%s\n",ans);
}

Measured memory_bytes: 75904
#include <iostream>
using namespace std;
int main(){
    int N;
    cin>>N;
    int T[N];
    for(int i=1;i<=N;i++){
        cin>>T[i];
    }
    int M;
    cin>>M;
    int p[M];
    int P[M];
    for(int i=1;i<=M;i++){
        cin>>p[i];
        cin>>P[i];
    }
    int k=0;
    for(int i=1;i<=M;i++){
       int a=0;
   
        k=T[p[i]];
        T[p[i]]=P[i];
        for(int i=1;i<=N;i++){
            a+=T[i];
        }
        cout<<a<<endl;
        T[p[i]]=k;
    }
    return 0;
    
}


Measured memory_bytes: 256
N,M=map(int,input().split())
def kaijo(a):
    start=1
    for i in range(1,a+1):
        start*=i
    return start
def comb(a,b):
    if a==0 or a==1:
        ans=0
    elif a==2:
        ans=1
    else:
        ans=kaijo(a)/(kaijo(b)*kaijo(a-b))
        #returnは一回で
    return ans
print(int(comb(M,2)+comb(N,2)))


Measured memory_bytes: 3060
let () =
  let rec check num a b =
    if b = 0 then false else if a * b = num then true 
    else if a = 1 then check num 9 (b-1)
    else check num (a-1) b in
  Scanf.scanf "%d\n" @@ fun n ->
  Printf.printf "%s\n" (if (check n 9 9) then "Yes" else "No")


Measured memory_bytes: 3720
#include <bits/stdc++.h>
using namespace std;
int main()
{

    int t,n,p,i;
    char a;
    cin>>a;
    if(a=='T') cout<<"A"<<endl;
    if(a=='A') cout<<"T"<<endl;
    if(a=='C') cout<<"G"<<endl;
    if(a=='G') cout<<"C"<<endl;

}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
long long r[105][100005];
const int MOD = 1000000007;
int main()
{
    int b, k;
    cin >> b >> k;
    int n[b];
    for(int i = 0; i < b; i++)
    {
    	cin >> n[i];
	}
	r[0][0] = 1;
	for(int i = 0; i <= n[0]; i++)
	{
		r[0][i] = 1;
	}
	for(int i = 0; i < b; i++)
	{
		r[i][0] = 1;
	}
    for(int i = 1; i < b; i++)
    {
    	for(int j = 1; j <= k; j++)
    	{
    		r[i][j] = r[i][j-1] + r[i-1][j];
    		if(j-n[i] > 0)
    		{
    		    r[i][j] -= r[i-1][j-n[i]-1];
    		}
    		r[i][j] %= MOD;
    		r[i][j] += MOD;
    		r[i][j] %= MOD;
    		
		}
	}
	cout << r[b-1][k] << endl;
}



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

#define maxn 200005
#define INF 0x3f3f3f3f
#define MINF 0x7fffffffffffffff
#define mod 1000000007
typedef long long ll;
using namespace std;

int a[maxn], f[maxn] = { 0 };

void slove()
{
	int n;
	cin >> n;
	for (int i = 0; i < n - 1; i++) cin >> a[i], f[a[i]]++;
	for (int i = 1; i <= n; i++) cout << f[i] << '\n';
}

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

	return 0;
}

Measured memory_bytes: 5152
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#define ill long long
#define rep(i, a, b) for(ill i = (ill)(a); i < (ill)(b); i++)
#define fcout cout << fixed << setprecision(10)
using namespace std;

int main() {
  int n;
  cin >> n;
  cout << 800*n - (n/15)*200 << "\n";
}


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

int main(){
  int K;
  cin>>K;
  long long ans = 0;
  for (int i=1;i<=K;i++){
    for (int j=1;j<=K;j++){
      for (int k=1;k<=K;k++){
        ans+=gcd(gcd(i,j),k);
      }
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3608
n = int(input())
s = ''
for i in range(1, n+1):
	if i % 3 == 0: 
		s += ' {0}'.format(i)
		continue
	x = i
	while x > 0:
		if x % 10 == 3:
			s += ' {0}'.format(i)
			break
		x //= 10
print(s)


Measured memory_bytes: 7656
N=int(input())
A=list(map(int,input().split()))
flag=True
if N%2==0:
    if 0 in A or len(set(A))!=N//2:flag=False
else:
    if len([0 for a in A if a==0])!=1 or len(set(A))!=N//2+1:flag=False
if flag:print(2**(N//2)%(10**9+7))
else:print(0)

Measured memory_bytes: 14004
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

int D, G;
vector<int> p(11, 0);
vector<int> c(11, 0);
vector<int> dp(100000000, INT_MAX);

int main(void){
    
    scanf("%d %d", &D, &G);
    for(int i = 0; i < D; ++i) scanf("%d %d", &p[i], &c[i]);
    
    dp[0] = 0;
    for(int i = 0; i < D; ++i){
        for(int sum = G - 1; sum >= 0; --sum){
            if(dp[sum] == INT_MAX) continue;
            for(int n = 0; n <= p[i]; ++n){
                int point = (n == p[i]) ? 100 * (i + 1) * n + c[i] : 100 * (i + 1) * n;
                if(G <= sum + point) dp[G] = min(dp[G], dp[sum] + n);
                else dp[sum + point] = min(dp[sum + point], dp[sum] + n);
            }
        }
    }
    printf("%d\n", dp[G]);
    
    return 0;
}

Measured memory_bytes: 390912
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ll long long int
#define MO 1000000007

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

ll gcd3(ll a,ll b,ll c){
  return gcd2(a,gcd2(b,c));
}

int kumi(int i,int j,int k){
  if(i==j && j==k) return 1;
  if(i==j || j==k || k==i) return 3;
  return 6;
}

ll modpow(ll a,ll n,ll mod){
  ll res=1;
  while(n>0){
    if(n&1) res=res*a%mod;
    a=a*a%mod;
    n >>= 1;
  }
  return res;
}

int main(){
  ll n,k;
  scanf("%lld%lld",&n,&k);

  ll ans=0;

  //0->sosu, 1->not sosu
  /*
  int sosu[100001]={};
  for(int i=2;i<=k;i++){
    for(int j=2;j<=k/i;j++){
      sosu[i*j]=1;}}
  */

  ll kumi[100001];
  
  for(int i=k;i>=1;i--){
    ll sum = modpow(k/i,n,MO);
    for(int j=2;j<=k/i;j++){
      sum = (sum-kumi[i*j]+MO)%MO;}
    kumi[i] = sum;
  }

  for(int i=1;i<=k;i++){
    ans = (ans+kumi[i]*i)%MO;
  }

  printf("%lld\n",ans);
  
  return 0;
}


Measured memory_bytes: 2460
#include <bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long


int main()
{
    io
	int t=1;
//	cin >> t;
	while (t--)
	{
       int flag = 0;
       int th,ts,ah,as;
       cin >> th >> ts >> ah >> as;
       int cnt = 1;
       while(th > 0 && ah > 0)
       {
           if(cnt%2 != 0)
           {
               ah = ah-ts;
           }
           else{
               th = th - as;
           }
           cnt++;
       }
       if(ah <= 0)
            cout << "Yes\n";
        else cout << "No\n";
	}
	return 0;
}

Measured memory_bytes: 3652
#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(){
	ll n, k;
	cin >> n >> k;
	if(n%k == 0){
		cout << 0 << endl;
	}else{
		cout << 1 << endl;
	}
	return 0;
}

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

using namespace std;

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

	int mx = 0;
	long long ans = 0;
	while(n--) {
		int x;
		scanf("%d", &x);
		mx = max(mx, x);
		ans += mx - x;
	}
	printf("%lld\n", ans);
}


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

int main(){
  ll n;
  cin>>n;
  int sum(0);
  int tmp(0);
  for (int i(1);i<=n;i++){
    sum+=i;
    if (sum>=n){
      tmp=i;
      break;
    }
  }
  for (int i(1);i<=n;i++){
    if (i==sum-n) continue;  
    else if(tmp<i) break;
    else {
      cout << i << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 384
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public final class Main {

    public static void main(String[] args) {
        final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        String first = in.nextLine();
        first += first;
        final String second = in.nextLine();
        System.out.println(first.contains(second) ? "Yes" : "No");
    }
}


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

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

Measured memory_bytes: 3596
inp = []
i = 0
j = 0
k = 0
l = 0

while True:
    inp.append(list(map(int, input().split())))
    if inp[i][0] == 0 and inp[i][1] == 0:
        del inp[i]
        break
    i += 1

for j in range(len(inp)):
    for k in range(inp[j][0]):
        for l in range(inp[j][1]):
            print(end="#")
        print("")
    print("")
        




Measured memory_bytes: 6312
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

int main() {
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> arrive_t(n);
  rep(i, n) cin >> arrive_t[i];
  sort(all(arrive_t));
  vector<int> limit_t(n);
  rep(i, n) limit_t[i] = arrive_t[i] + k;
  
  int ans = 0, limit = limit_t[0], cnt = 0;
  for (int i = 0; i < n;) {
    while (arrive_t[i] <= limit && cnt < c) {
      cnt++;
      i++;
    }
    
    limit = limit_t[i];
    cnt = 0;
    ans++;
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<long int>a(n);
  int t=0;
  rep(i,n){
    cin >> a.at(i);
  }
  
  while(true){
    bool exist =false;
    rep(i,n){
      if(a.at(i)% 2 != 0){
        exist =true;
      }
    }
    if(exist == true) break;
    
    rep(i,n){
      a.at(i) /=2;
    }
    t++;
  }
  
  cout << t << endl;
}

    
    

Measured memory_bytes: 3600
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define INF 1000000

int dp[25][50005];
int c[25];

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

    for(int i=1; i<=m; i++)
        scanf("%d",&c[i]);

    for(int i=0; i<=20; i++)
        for(int j=0; j<=50000; j++)
            dp[i][j] = INF;

    dp[0][0] = 0;


    for(int i=1; i<=m; i++)
        for(int j=0; j<=50000; j++)
            if(j>=c[i])
                dp[i][j] = min( dp[i-1][j], dp[i][j-c[i]]+1 );
            else
                dp[i][j] = dp[i-1][j];

    printf("%d\n", dp[m][n]);

    return 0;
}

Measured memory_bytes: 5132
fun main(args: Array<String>){
    val (N, K) = readLine()!!.split(" ").map { it.toLong() }
    val ab = Array(N.toInt()){ readLine()!!.split(" ").map { it.toInt() } }
    ab.sortBy { it[0] }
    var x = K
    var i = 0
    while (x > 0){
        x -= ab[i][1]
        i++
    }
    println(ab[i - 1][0])
}

Measured memory_bytes: 83388
LARGE = 10 ** 9 + 7

def prepare(n, mod):
    f = 1
    for m in range(1, n + 1):
        f *= m
        f %= mod
    return pow(f, mod - 2, mod)

n, a, b = map(int, input().split())

Ca = prepare(a, LARGE)
for i in range(n - a + 1, n + 1):
    Ca *= i
    Ca %= LARGE

Cb = prepare(b, LARGE)
for i in range(n - b + 1, n + 1):
    Cb *= i
    Cb %= LARGE

print((pow(2, n, LARGE) - Ca - Cb - 1 + LARGE) % LARGE)


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
 
/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll mod = 1e9+7;

// void chmod(ll &M){
//     if(M >= mod) M %= mod;
//     else if(M < 0){
//         M += (abs(M)/mod + 1)*mod;
//         M %= mod;
//     }
// }

int n, m;
vector<int> lans, rans;
int ep,ep2; // End point

vector<int> adj[100005];
vector<bool> used(100005, false);

void rdfs(int now){
	bool flg = true;

	for(auto e:adj[now]) if(used[e] == false) flg = false;
	if(flg){
		return;
	}

	for(auto e:adj[now]) if(used[e] == false){
		used[e] = true;
		ep = e;
		rans.push_back(ep);
		rdfs(ep);
		return;
	}

}

void ldfs(int now){
	bool flg = true;

	for(auto e:adj[now]) if(used[e] == false) flg = false;
	if(flg){
		cout << rans.size()+lans.size() << endl;
		reverse(lans.begin(), lans.end());
		for(int i=0; i<lans.size(); ++i){
			if(i) cout << " ";
			cout << lans[i];
		}
		for(int i=0; i<rans.size(); ++i){
			cout << " " << rans[i];
		}
		cout << endl;
		return;
	}

	for(auto e:adj[now]) if(used[e] == false){
		used[e] = true;
		ep = e;
		lans.push_back(ep);
		ldfs(ep);
		return;
	}

}



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

	cin >> n >> m;

	for(int i=0; i<m; ++i){
		int a, b; cin >> a >> b;
		if(i == 0){
			used[a] = used[b] = true;
			rans.push_back(a);
			rans.push_back(b);
			ep = b;
			ep2 = a;
		}
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	rdfs(ep);
	ldfs(ep2);

	return 0;
}

Measured memory_bytes: 6904
N = int(input())


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


fcl = factorization(N)

result = 0
count = len(fcl)

for i in range(count):
    num = 1
    while fcl[i][1] >= num:
        fcl[i][1] = fcl[i][1] - num
        num += 1
        if fcl[i][0] > 1:
            result += 1
print(result)

Measured memory_bytes: 9508
import java.io.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String line = reader.readLine();
		String[] sarray = line.split(" ", -1);
		int n = Integer.parseInt(sarray[0]);
		int m = Integer.parseInt(sarray[1]);
		int matrix[][] = new int[n][m];
		for(int i = 0; i < n; i++){
			String str = reader.readLine();
			String[] mat = str.split(" ", -1);
			for(int j = 0; j < m; j++){
				matrix[i][j] = Integer.parseInt(mat[j]);
			}
		}
		int vector[] = new int[m];
		for(int k = 0; k < m; k++){
			String vec = reader.readLine();
			vector[k] = Integer.parseInt(vec); //j<m k<m
		}
		int result[] = new int[n];
		for(int p = 0; p < n; p++){
			for(int q = 0; q < m; q++){
				result[p] += matrix[p][q]*vector[q];
			}
			System.out.println(result[p]);
		}
	}
}

Measured memory_bytes: 27516
a,b,c = [int(i) for i in input().split()]
arr = set()
ret = "NO"
r = a%b
if r==0:
    if c==0:
        ret="YES"
    else:
        pass
elif r==1:
    ret = "YES"
else:
    rr = r
    while not(rr in arr):
        # print(arr,rr)
        if rr <= c and (c-rr)%r == 0:
            ret = "YES"
            break
        else:
            arr.add(rr)
            n = int((b-rr)/r)
            n += 1 if (b-rr)%r != 0 else 0
            rr = (n*r + rr)%b
            # print(n,rr)
print(ret)



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

using namespace std;

template <class T>inline T updmax(T& a, T b) {return a = max(a, b);}
template <class T>inline T updmin(T& a, T b) {return a = min(a, b);}

class Solution {
public:
	void solve() {
		int n, m;
		cin >> n >> m;
		vector<int> cnt(m, 0);
		for(int i=0; i < n; i++){
			int k;
			cin >> k;
			for(int j=0; j < k; j++){
				int a;
				cin >> a;
				cnt[a-1]++;
			}
		}
		int ret = 0;
		for(int i=0; i < m; i++)if(cnt[i] == n)ret++;
		cout << ret << "\n";
		return;
	};
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	Solution solution;
	solution.solve();
	return 0;
}

Measured memory_bytes: 256
import java.io.PrintWriter
import java.lang.StringBuilder

fun main() {
    PrintWriter(System.out).use { out ->
        solve(out)
        out.flush()
    }
}

fun solve(out: PrintWriter) {
    var x = readLong()
    val sb = StringBuilder()
    while (x > 0) {
        x -= 1
        sb.append('a' + (x % 26).toInt())
        x /= 26
    }
    println(sb.reverse())
}

fun readInteger() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntegerList() = readStringList().map(String::toInt)
fun readLongList() = readStringList().map(String::toLong)
fun readDoubleList() = readStringList().map(String::toDouble)


Measured memory_bytes: 34580
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define vv vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int> >
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define INF 100000000000
#define eps 1e-7
#define mod 1000000007
#define int ll
using namespace std;

int N,C;
int table[40][100000+10];

signed main(void) {
    cin>>N>>C;
    REP(i, 1, C) {
        REP(j, 0, 100000+10) {
            table[i][j]=0;
        }
    }
    rep(i, N) {
        int s,t,c; cin>>s>>t>>c;
        REP(j, s, t) {
            table[c][j]=1;
        }
    }

    int ans=0;
    REP(j, 0, 100000+10) {
        int colsum=0;
        REP(i, 1, C) {
            colsum+=table[i][j];
        }
        ans=max(colsum, ans);
    }
    cout << ans << endl;


    return 0;
}

Measured memory_bytes: 24064
import java.util.*
import java.lang.Math.*


val readString: ()->String = {readLine()!!}
val readInt: ()->Int = {readLine()!!.toInt()}
val readLong: ()->Long = {readLine()!!.toLong()}
val readIntArray: ()->IntArray = {readLine()!!.split(' ').map{it.toInt()}.toIntArray()}
val readLongArray: ()->LongArray = {readLine()!!.split(' ').map{it.toLong()}.toLongArray()}
val errPrintln: (String)->Unit = {msg -> System.err.println(msg)}
val MOD = 1_000_000_007L
val INF = Int.MAX_VALUE/2
val LINF = Long.MAX_VALUE/2
data class IntPair(val first: Int, val second: Int)
data class To(val idx: Int, val cost: Long)


fun solve(A: IntArray): Int {
    val N = A.size
    if (N == 1) return 1

    when (A.count{it == 1}) {
        in 2..N -> { return 0 }
        1 -> { return 1 }
        else -> {
            var B = A.sorted()
            if (B[0] == B.last()) return 0

            val AMAX = 1_000_000
            val hist = IntArray(AMAX+1){0}
            B.groupBy { it }.forEach{ hist[it.key] = it.value.size }

            val C = B.distinct()

            val fc = IntArray(AMAX+1){0}
            for (i in C.indices) {
                if (fc[C[i]] != 0) continue

                fc[C[i]] = C[i]

                for (j in C[i]*2 .. AMAX step C[i]) {
                    if (fc[j] == 0) fc[j] = C[i]
                }
            }

            return C.filter { fc[it]==it && hist[it] == 1 }.count()
        }
    }
}


fun main(args: Array<String>) {
    val N = readInt()
    val A = readIntArray()

    val ans = solve(A)

    println(ans)
}


Measured memory_bytes: 114548
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t[7368791];

int main(){
	int m;ll n;
	int cnt=0;
	while(cin>>m>>n){
		if(m==0)break;
		/*if(m==2&&n==500000){
			cout<<7368791<<endl;
			continue;
		}*/
		memset(t,0,sizeof(t));
		for(int j=0;j<n;j++){
			cnt=0;
			for(ll i=m+j;i<7368792;i+=(m+j)){
				cnt++;
				if(t[i]!=0&&cnt==1){
					n++;break;
				}
				t[i]=1;
			}
		}
//		cout<<"aa"<<endl;
		while(t[m]==1)m++;
		cout<<m<<endl;
		/*for(ll i=m;i<7368792;i++){
			//cout<<t[i]<<endl;
			if(t[i]==0){
				cout<<i<<endl;
				//return 0;
				break;
			}
		}*/
	}
}


Measured memory_bytes: 31880
#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> A(N);

  int SUM = 0;
  rep(i, N) {
    cin >> A[i];
    SUM += A[i];
  }

  int NUM = 0;
  rep(i, N) {
    if (A[i] * (4 * M) >= SUM) NUM++;
  }

  if (NUM >= M)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
from math import ceil
n = int(input())
data = [list(map(int,input().split())) for i in range(n-1)]
ret = 0

for cnt in range(n):
    ret = 0
    for i in data:
        if ret <= i[1]:
            ret = i[1]
        else:
            ret = ceil(ret/i[2]) * i[2]
        ret += i[0]
    print(ret)
    if cnt != n-1 : data.pop(0)


Measured memory_bytes: 3188
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<utility>
#include<vector>

#define rep(i, a, n)  for(int i=a; i<n; i++)
#define per(i, a, n)  for(int i=n-1; i>=a; i--)
#define fill0(n)  setfill('0') << right << setw(n)
#define all(x)  (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MAX 1000000000+7
using namespace std;
typedef long long ll;

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

int main(){
  ll n;  cin >> n;
  vector<ll> a(n);
  rep(i, 0, n){
    cin >> a[i];
  }
  ll ans = a[0];
  rep(i, 1, n){
    ans = Euclid(ans, a[i]);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 1024
#include <stdio.h>

int binarySearch(int *,int ,int );

int main(){

int i,n,q,A[100000],T[50000],sum=0,key;

scanf("%d",&n);
for(i=0;i<n;i++){
  scanf("%d",&A[i]);
  }

scanf("%d",&q);
for(i=0;i<q;i++){
  scanf("%d",&T[i]);
  key = T[i];
  if(A[binarySearch(A,n,key)] == T[i]) sum++;
  }
  printf("%d\n",sum);

return 0;
}

int binarySearch(int A[],int n,int key){
int left = 0, right = n,mid;

while(left < right) {
  mid = (left + right)/2;
  if (A[mid] == key) return mid;
  else if (key < A[mid]) right = mid;
  else left = mid + 1;
}
  return -1;
}

Measured memory_bytes: 1184
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define eb emplace_back
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
typedef vector<char> vc;
typedef vector<vector<char> > vvc;
typedef vector<double> vd;
typedef vector<vector<double> > vvd;
template<class T> using vv=vector<vector< T > >;
typedef deque<int> di;
typedef deque<deque<int> > ddi;

// cout vector
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
    int len = v.size();
    for (int i = 0; i < len; ++i) {
        s << v[i]; if (i < len - 1) s << "\t";
    }
    return s;
}

// cout 2-dimentional vector
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
    int len = vv.size();
    for (int i = 0; i < len; ++i) {
        s << vv[i] << endl;
    }
    return s;
}

int main() {
    int n;
    cin >> n;
    printf("%d\n", n*(n+1)/2);
    return 0;
}


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

int main(){
  int n; cin>>n;
  long long ans=0;
  vector<int > num(n);
  for(int i=0;i<n;i++){
    cin>>num[i];
    num[i]--;
    ans+=num[i];
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[100005], b[100005], c[100005];
ll dp[100005][5];
int main() {
	int n;
	cin >> n;

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

	dp[0][0] = a[0];
	dp[0][1] = b[0];
	dp[0][2] = c[0];

	for (int i = 1; i < n; i++) {
		dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
		dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
		dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
	}
	int max = -1e5;
	for (int i = 0; i < n; i++) {
		if (dp[n - 1][i] > max) {
			max = dp[n - 1][i];
		}
	}
	cout << max << endl;
	return 0;
}


Measured memory_bytes: 6528
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;


#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(),a.end()

typedef tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)

int main(void)
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, m;
    cin>>n>>m;
    for(int i = 0; i <= n; i++) {
    	ll l = i;
    	ll r = n - i;
    	if(l * 2 + r * 4 == m) {
    		cout<<"Yes\n";
    		return 0;
    	}
    }
    cout<<"No\n";
    return 0;
}

Measured memory_bytes: 3652
class UnionFind():
    def __init__(self, n):
        self.n=n
        self.parents=[-1]*n #各要素の親要素の番号を格納するリスト

    def find(self, x): #要素xが属するグループの根を返す
        if self.parents[x]<0:
            return x
        else:
            self.parents[x]=self.find(self.parents[x])
            return self.parents[x]

    def union(self,x,y): #要素xが属するグループと要素yが属するグループとを併合する
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        if self.parents[x]>self.parents[y]:
            x,y=y,x

        self.parents[x]+=self.parents[y]
        self.parents[y]=x

    def size(self,x): #要素xが属するグループのサイズ（要素数）を返す
        return -self.parents[self.find(x)]

    def same(self,x,y): #要素x, yが同じグループに属するかどうかを返す
        return self.find(x)==self.find(y)

    def members(self,x): #要素xが属するグループに属する要素をリストで返す
        root=self.find(x)
        return [i for i in range(self.n) if self.find(i)==root]

    def roots(self): #すべての根の要素をリストで返す
        return [i for i, x in enumerate(self.parents) if x<0]

    def group_count(self): #グループの数を返す
        return len(self.roots())

    def all_group_members(self): #{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

#print(list(uf.all_group_members().values())) #グループ一覧を表示

n,q=map(int,input().split())
uf=UnionFind(n)
for i in range(q):
    t,x,y=map(int,input().split())
    if t==0:
        uf.union(x,y)
    else:
        judge=uf.same(x,y)
        print(1 if judge else 0)



Measured memory_bytes: 5796
/*input
1 100
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int,int>
#define pb push_back
#define f first
#define s second
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

signed main() {
	IOS;
	int  k,x;
	cin>>k>>x;
	int mn = 1000000;
	for(int i = max(-mn , x-k+1);i<=x;i++){
		cout << i << " ";
	}
	for(int i=x+1;i<min(1000000LL,x+k);i ++){
		cout << i << " ";
	}
return 0;
}


Measured memory_bytes: 256
reet = [(1,399),(400,799),(800,1199),(1200,1599),
        (1600,1999),(2000,2399),(2400,2799),(2800,3199)]
n = int(input())
alist = list(map(int, input().split()))
 
dorei = []
osama = 0
for a in alist:
    osamaflag = True
    for i in range(len(reet)):
       	bottom, top = reet[i]
        if (bottom <= a <= top):
            dorei.append(i)
            osamaflag = False
            break
    if osamaflag:
        osama += 1

min_ = len(set(dorei))
if min_ == 0:
    min_ = 1
max_ = len(set(dorei)) + osama
print(min_, max_)

Measured memory_bytes: 3064
class F:
    def __init__(self,n,mod):
        self.mod=mod
        self.f=[1]
        for i in range(1,n+1):
            self.f.append(self.f[-1]*i%mod)
        self.i=[pow(self.f[-1],mod-2,mod)]
        for i in range(1,n+1)[::-1]:
            self.i.append(self.i[-1]*i%mod)
        self.i.reverse()
    def factorial(self,i):
        return self.f[i]
    def ifactorial(self,i):
        return self.i[i]
    def comb(self,n,k):
        return 0 if k>n else self.f[n]*self.i[n-k]*self.i[k]%self.mod
n,k=map(int,input().split())
M=10**9+7
c=F(n,M).comb
for i in range(k):
    print(c(n-k+1,i+1)*c(k-1,i)%M)

Measured memory_bytes: 42608
#include<bits/stdc++.h>
using namespace std;
char s[110000] = {0};
int main(){
	cin >> s;
	bool flag = 0;
	int len = strlen(s);
	for(int i = 0;i<len;i++){
		if(s[i]==s[i+1]){
			cout << i+1 << " " << i+2 << endl;
			flag = 1;
			break;
		}else if(s[i]==s[i+2]){
			cout << i+1 << " " << i+3 << endl;
			flag = 1;
			break;
		}
	}
	if(!flag)cout << -1 << " " << -1 << endl;
	return 0;
}

Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int q = sc.nextInt();
		StringBuilder sb = new StringBuilder();
		while (q-- > 0) {
			long a = sc.nextLong();
			long b = sc.nextLong();
			sb.append(solve(a, b));
			sb.append("\n");
		}
		System.out.print(sb.toString());
	}

	private static long solve(long a, long b) {
		if (a == b) return 2 * (a - 1);
		if (a > b) {
			long t = a;
			a = b;
			b = t;
		}
		long l = 0;
		long r = b - 1;

		while (r > l) {
			long m = (l + r + 1) / 2;
			long l2 = 0;
			long r2 = m - 1;
			while (r2 > l2) {
				long m2 = (l2 + r2) / 2;
				if ((m - m2) * (a + 1 + m2) <= (m - (m2 + 1)) * (a + 2 + m2)) {
					l2 = m2 + 1;
				} else {
					r2 = m2;
				}
			}

			if ((m - l2) * (a + 1 + l2) < a * b) {
				l = m;
			} else {
				r = m - 1;
			}
		}

		return (a - 1) + l;
	}
}


Measured memory_bytes: 23124
def main():
    N = int(input())
    A = list(map(int, input().split()))

    avg = sum(A) // N

    ans = 10 ** 10
    
    for i in [avg, avg+1]:
        tmp = 0
        for ai in A:
            x = (i - ai) ** 2
            tmp += x
        ans = min(ans, tmp)

    print(ans)



if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int N=405;
struct data{
	int a[4];
	data nxt(){
		return (data){a[1],a[2],a[3],a[0]};
	}
	bool operator == (const data &b) const{
		for(int i=0;i<4;i++){
 			if(a[i]!=b.a[i]){
				return false;
			}
		}
		return true;
	}
}a[N];
namespace std{
	template <>
	struct hash <data>{
		hash<ll> h;
		size_t operator () (data n) const{
			ll res=0;
			for(int i=0;i<4;i++){
				res=res*2333+n.a[i];
			}
			return res;
		}
	};
}
unordered_map<data,int> mp;
int n;
ll ans;
void insert(data x,int v){
	for(int i=0;i<4;i++,x=x.nxt()){
		mp[x]+=v;
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d%d",&a[i].a[0],&a[i].a[1],&a[i].a[2],&a[i].a[3]);
		insert(a[i],1);
	}
	for(int i=1;i<=n;i++){
		insert(a[i],-1);
		for(int j=i+1;j<=n;j++){
			insert(a[j],-1);
			for(int k=0;k<4;k++,a[j]=a[j].nxt()){
				data t1=(data){a[i].a[1],a[i].a[0],a[j].a[0],a[j].a[3]};
				data t2=(data){a[i].a[2],a[i].a[1],a[j].a[3],a[j].a[2]};
				data t3=(data){a[i].a[3],a[i].a[2],a[j].a[2],a[j].a[1]};
				data t4=(data){a[i].a[0],a[i].a[3],a[j].a[1],a[j].a[0]};
				if(!mp[t1]||!mp[t2]||!mp[t3]||!mp[t4]){
					continue;
				}
				ll res=1;
				res*=mp[t1];
				insert(t1,-1);
				res*=mp[t2];
				insert(t2,-1);
				res*=mp[t3];
				insert(t3,-1);
				res*=mp[t4];
				insert(t3,1);
				insert(t2,1);
				insert(t1,1);
				ans+=res;
			}
			insert(a[j],1);
		}
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 19316
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
if N%7 == 0:
	print("Yes")
	exit()
	
for i in range(-(-N//7)):
	if (N - 7*i)%4 == 0:
		print("Yes")
		break
else:
	print("No")

Measured memory_bytes: 5588
#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int INF = 1e9;
const int N = 15;

int G[N][N];
int n;
int val[1<<N];
int dp[1<<N][N];

bool isSet(int mask, int x) {
  return ((mask>>x)&1) == 1;
}

int g(int mask) {
  int &res = val[mask];
  if(res != -1) {
    return res;
  }
  res = 0;
  rep(i,0,n) if(isSet(mask, i)) {
    rep(j,i+1,n) if(isSet(mask, j)) {
      res += G[i][j];
    }
  }
  return res;
}

int f(int mask, int p) {
  int &res = dp[mask][p];
  if(res != -1) {
    return res;
  }
  if(p == n - 1) {
    return res = g(mask ^ ((1<<n) - 1));
  }
  res = -INF;
  int m = mask ^ ((1<<n) - 1);
  for(int i = m; i; i = (i - 1) & m) {
    rep(j,0,n) if(isSet(i, j) && G[p][j]) {
      res = max(res, G[p][j] + f(mask | i, j) + g(i));
    }
  }
  return res;
}

int main() {
  int m;
  S2(n,m);
  memset(val, -1, sizeof(val));
  int tot = 0;
  rep(i,0,m) {
    int u,v,c;
    scanf("%d%d%d",&u,&v,&c);
    u--; v--;
    G[u][v] = G[v][u] = c;
    tot += c;
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for(int i = 1; i < (1<<n); i += 2) {
    ans = max(ans, f(i, 0) + g(i));
  }
  P(tot - ans);
  return 0;
}


Measured memory_bytes: 2304
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
    ll n,k,a,b,x,ans;
    scanf("%lld %lld",&n,&k);
    x=n/k;
    ans=min(abs(n-(x-1)*k),abs(n-x*k));
    ans=min(ans,abs(n-(x+1)*k));
    printf("%lld",ans);
}


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

using namespace std;

int main(){
  string s,t,u;
  int a,b;
  cin>>s>>t;
  cin>>a>>b;
  cin>>u;
  if(u==t)
    b-=1;
  else
    a-=1;
  cout<<a<<" "<<b;
  
  return 0;
}

Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
namespace AtCoder
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }

        static readonly long Mod = 1000000000 + 7;

        static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            var NM = Sarray();
            var N = NM[0];
            var M = NM[1];
            var nums = new long[M];
            for (var i = 0; i < N; ++i)
            {
                var KA = Sarray();
                for (var j = 1; j < KA[0] + 1; ++j)
                    ++nums[KA[j] - 1];
            }
            var ans = 0;
            for (var i = 0; i < M; ++i)
                if (nums[i] == N)
                    ++ans;
            WriteLine(ans);
            //Out.Flush();
        }
    }
}

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

using P = pair<char, int>;

int root(int x, vector<int>& par, vector<int> const& cnt) {
    if(cnt[x] > 0) {
        return x;
    }
    return par[x] = root(par[x], par, cnt);
}

int main() {
    int N, Q;
    while(cin >> N >> Q, N) {
        vector<int> parent(N);
        for(int i=1; i<N; ++i) {
            int p;
            cin >> p;
            p--;
            parent[i] = p;
        }
        vector<P> query(Q);
        vector<int> m_cnt(N);
        m_cnt[0] = 1;
        for(int i=0; i<Q; ++i) {
            cin >> query[i].first >> query[i].second;
            query[i].second--;
            if(query[i].first == 'M') {
                m_cnt[query[i].second]++;
            }
        }
        reverse(query.begin(), query.end());
        long long res = 0;
        for(auto& q : query) {
            if(q.first == 'Q') {
                res += root(q.second, parent, m_cnt) + 1;
            } else {
                m_cnt[q.second]--;
            }
        }
        cout << res << endl;
    }
}


Measured memory_bytes: 4328
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

//
#define int long long
#define pb(x) push_back(x)
#define m0(x) memset((x), 0, sizeof(x))
#define mm(x) memset((x), -1, sizeof(x))

//container
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
#define PERM(c) \
  sort(ALL(c)); \
  for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))

// debug
#define GET_VAR_NAME(variable) #variable
#define test(x) cout << GET_VAR_NAME(x) << " = " << x << endl;

// bit_macro
#define bit(n) (1LL << (n))
#define bitset(a, b) (a) |= (1 << (b))
#define bitunset(a, b) (a) |= ~(1 << (b))
#define bitcheck(a, b) ((((a) >> (b)) & 1) == 1)
#define bitcount(a) __builtin_popcountll((a))

//typedef
typedef long long lint;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

//constant
const int INF = (int)1e18;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
const int dx[] = {-1, 0, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, 0, 1, -1, 1, -1};

//
template <typename T>
void chmax(T &a, T b) { a = max(a, b); }
template <typename T>
void chmin(T &a, T b) { a = min(a, b); }
//
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
struct Accelerate_Cin {
  Accelerate_Cin() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
};

signed main() {
  int n;
  cin >> n;

  int cont = 0;
  while (n > 0) {
    int tmp = n % 10;
    if (tmp == 2) cont++;
    n /= 10;
  }

  cout << cont << endl;
  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

int main() {
	int N, M, K;
	cin >> N >> M >> K;

	vector<long long> sumA(N);
	cin >> sumA[0];
	for(int i = 1; i < N; i++) {
		cin >> sumA[i];
		sumA[i] += sumA[i - 1];
	}

	vector<long long> sumB(M);
	cin >> sumB[0];
	for(int i = 1; i < M; i++) {
		cin >> sumB[i];
		sumB[i] += sumB[i - 1];
	}

	// Bの山のみの場合
	auto itr = upper_bound(sumB.begin(), sumB.end(), K);
	int result = distance(sumB.begin(), itr);

	for(int i = 0; i < N; i++) {
		if(sumA[i] > K) {
			break;
		}
		auto itr = upper_bound(sumB.begin(), sumB.end(), K - sumA[i]);
		const auto d = distance(sumB.begin(), itr);

		const auto numBook = (i + 1) + d;
		if(numBook > result) {
			result = numBook;
		}
	}

	cout << result << endl;

	return 0;
}


Measured memory_bytes: 6428
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <utility>

using namespace std;
typedef long long ll;

int main() {
    string s;
    cin >> s;
    if(s[0] == '9' || s[1] == '9') {
        cout << "Yes" << endl;
    } else
        cout << "No" << endl;

}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
  int k, tmp, a, b;
  cin >> k;
  cin >> a >> b;
  tmp = k;
  while(tmp <= b) {
    if (a <= tmp && tmp <= b) {
      cout << "OK" << endl;
      return 0;
    } else {
      tmp += k;
    }
  }
  cout << "NG" << endl;
  return 0;
}


Measured memory_bytes: 3624
n = int(input())

mx = int(n**(1/2))
if (mx+1)**2 == n:
    mx += 1
a = 0
for i in range(mx, 0, -1):
    if n%i==0:
        a = i
        break
#print(a, n//a)
print(len(str(n//a)))

Measured memory_bytes: 38512
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>

using namespace std;

typedef long long int ll;
typedef long int li;

int main(void){
    ll ans;
    ll s;
    ans =100000;
    cin >> s;
    
    while(s>110){
        ll x;
        x = s % 1000;
        s = s / 10;
        ans=min(ans,abs(x-753));
    }
    cout << ans << endl;
    return 0;
}


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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using P = pair<int, int>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 29;
const ll INF64 = 1ll << 45;
const int mod = 1000000007;
//const int mod = 998244353;

template <class T>inline bool chmin(T &a, const T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline bool chmax(T &a, const T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint
{
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a)
  {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a)
  {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a)
  {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const
  {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const
  {
    mint res(*this);
    return res -= a;
  }
  mint &operator++()
  {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--()
  {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const
  {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const
  {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const
  {
    return pow(mod - 2);
  }
  mint &operator/=(const mint a)
  {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const
  {
    mint res(*this);
    return res /= a;
  }
};

struct UnionFind
{
  vector<int> par;
  vector<int> siz;
  UnionFind(int N) : par(N), siz(N, 1) {for (int i = 0; i < N; i++) par[i] = i;}
  void init(int N) {
    siz.assign(N, 1);
    par.resize(N);
    for(int i = 0; i < N; ++i) par[i] = i;
  }
  int root(int x) {
    while(par[x] != x) {x = par[x] = par[par[x]];}
    return x;
  }
  bool unite(int x, int y)
  {
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  bool isSame(int x, int y) {return root(x) == root(y); }
  int size(int x) { return siz[root(x)]; }
};

int main()
{
  double a, b, x;
  cin >> a >> b >> x;
  if(x <= a*a*b/2) {
    double w = b;
    double h = (2*x)/(w*a);
    double tan = w/h;
    printf("%0.10f\n", atan(tan)*180/M_PI);
  }
  else {
    double h = a;
    double w = 2*(b*h*a-x)/(h*a);
    double tan = w/h;
    printf("%0.10f\n", atan(tan)*180/M_PI);
  }
}

Measured memory_bytes: 256