akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<math.h>
#include<map>
#include<iomanip>
#include<functional>

using namespace std;
using  ull = unsigned long long int;

#define rep(i,n,m) for(long long int i=n;i<(long long int)(m);i++)



int main() {

	long long int n;
	cin >> n;
	vector<long long int>num(n);
	vector<long long int>b;

	vector<long long int>zero;
	vector<long long int>one;


	rep(i, 0, n) {
		cin >> num[i];

		if (n < 100) {
			b.push_back(num[i]);
			reverse(b.begin(), b.end());
		}

		if (i % 2 == 0)zero.push_back(num[i]);
		else one.push_back(num[i]);
		
	}


	if (n < 100) {
		rep(i, 0, n) {
			cout << b[i] << " ";
		}
		cout << endl;
		return 0;
	}



	if (n % 2 == 0) {

		reverse(one.begin(), one.end());


		rep(i, 0, one.size())cout << one[i] << " ";
		rep(i, 0, zero.size())cout << zero[i] << " ";
		cout << endl;
	}
	else {
		reverse(zero.begin(), zero.end());

		rep(i, 0, zero.size())cout << zero[i] << " ";
		rep(i, 0, one.size())cout << one[i] << " ";
		
		cout << endl;


	}


	return 0;
}

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


int main()
{
    int N ;
    string S;
    string A ;
    cin >> N;
    cin >> S;

    int count = 0;

    for (int n = 0; n < N; n++)
    {
        if(S[n] == 'A'){
            if(S[n+1] == 'B'){
                if(S[n+2] == 'C'){
                    count += 1;
                }
            }

        }
    }
    cout << count << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin >> a >> b >> c;
    if(a==max(a,max(b,c)))cout << a*10+b+c << endl;
    else if(b==max(a,max(b,c)))cout << b*10+a+c << endl;
    else if(c==max(a,max(b,c)))cout << c*10+a+b << endl;
}


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

using namespace std;

int main()
{
    int cnt = 0, answer = 0;
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++)
    {
        if (s.at(i) == 'A' || s.at(i) == 'T' || s.at(i) == 'G' || s.at(i) == 'C')
        {
            cnt++;
        }
        else
        {
            cnt = 0;
        }
        answer = max(answer, cnt);
    }
    cout << answer << endl;
    return 0;
}

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

#define for0(i, n) for (ll i = 0; i < (ll)(n); ++i) // 0 based indexing
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (ll i = (ll)(l); i <= (ll)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (ll i = (ll)(n); i >= 1; --i) // reverse 1 based

#define ff first
#define ss second
#define pb push_back

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n;
  cin >> n;
  ll x =0;
  vector <ll > num(n);
  for0(i,n){
    cin >> num[i];
    if (num[i] != i+1) x++;
  }
  if(x ==2 || x ==0) cout << "YES" << endl;
  else cout << "NO" << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i,m,n) for(lli i = m;i < n;i++)
#define rrep(i,m,n) for(lli i=m-1;i>=n;i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(),N.end()),N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(),n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K,L,N,S) vector<L> K(N,S)
#define DV(K,L,N,M,S) vector<vector<L>> K(N,vector<L>(M,S))
#define TV(K,L,N,M,R,S) vector<vector<vector<L>>> K(N,vector<vector<L>>(M,vector<L>(R,S)))
#define pint pair<lli,lli>
#define paf(L,R) pair<L,R>
#define mod 1000000007
#define MAX 5100000
#define ALL(a)  a.begin(),a.end()
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
vector<lli> divisor(lli N){
  vector<lli>res;
  for(lli i=1;i*i<=N;i++){
    if(N%i==0){
      res.push_back(i);
      if(i!=N/i)res.push_back(N/i);
    }
  }
  Sort(res);
  return res;
}
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli A,B,C,D,E,F,N,M,K,L,R,X,Y,Z,H,W,sum=0,num=0,flag=0;string S,T;
  cin >> N;
  M=sqrt(N);
  rep(i,2,M+1){
    A=N;
    while(A>=i){
      if(A%i==0)A/=i;
      else A-=(A/i)*i;
    }
    if(A==1)sum++;
  }
  auto P=divisor(N-1);
  for(auto v:P)if(v>M)sum++;
  Out(sum+1);
}


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

struct UnionFind {
    vector<ll> par; // par[i] is the parent node of node i.
    vector<ll> siz; // size of the group

    UnionFind(ll N) : par(N) { // Initialize to have all nodes to be roots of themselves
        for (ll i = 0; i < N; i++) par[i] = i;
        siz.assign(N, 1LL);
    }

    ll root(ll x) { // gain the root of a node recursively
        if (par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    void unite(ll x, ll y) { // unite x and y
        x = root(x);
        y = root(y);
        if (x == y) return; // roots of x and y are same => they are in the smae group
        if (siz[x] < siz[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (siz[x] == siz[y]) siz[x] ++;
        }
    }

    bool same(ll x, ll y) {
        return root(x) == root(y);
    }
};

int main() {
    ll N, M, K;
    cin >> N >> M >> K;
    UnionFind uf(N);
    vector<vector<ll>> dame(N, vector<ll> ());
    for (ll i = 0; i < M; i++) {
        ll a, b; cin >> a >> b; a--; b--;
        dame[a].push_back(b);
        dame[b].push_back(a);
        uf.unite(a, b);
    }
    for (ll i = 0; i < K; i++) {
        ll c, d; cin >> c >> d; c--; d--;
        if (uf.same(c, d)) {
            dame[c].push_back(d);
            dame[d].push_back(c);
        }
    }

    vector<ll> root_num(N, 0);
    for (ll i = 0; i < N; i++) {
        root_num[uf.root(i)] ++;
    }

    for (ll i = 0; i < N; i++) {
        ll ans = 0;
        ans += root_num[uf.root(i)];
        ans --;
        ans -= dame[i].size();
        cout << ans << " ";
    }
    cout << endl;
}

Measured memory_bytes: 12032
/**
                 MD.Faysal Hasan
                Comilla Univesity               **/

#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<string>
#include <stdlib.h>
using namespace std;

#define         rep(i,n)       for ( i=0 ; i<n ; i++ )
#define         rep1(i,n)      for ( i=1 ; i<=n ; i++ )
#define         sajo(str)      sort(str.begin(),str.end())
#define         rev(str)       reverse(str.begin(),str.end())
#define         usajo(str)     sort(str.rbegin(),str.rend())
#define         pi             2*acos(0.0)
#define         nel            printf("\n")
#define         longmx         92233720368547758
#define         gcd(a,b)       __gcd(a,b)
#define         lcm(a,b)       (a/gcd(a,b))*b
#define         yes            cout<<"YES"<<"\n"
#define         no             cout<<"NO"<<"\n"
#define         vcl            vector<ll>
#define         pb             push_back
#define         prl            pair< ll , ll >
#define         mkp(x,y)       make_pair(x,y)
#define         ff             first
#define         sc             second
#define         spc            ' '
#define         nn             '\n'
#define         Faysal         ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef         long long int  lli;
typedef         long long      ll;

int main(){Faysal

    ll ans,sum,tot,cnt,mx,mn,idx,chk,rem,ret,tmp,mid,lft,rgt,pos;

    ll tc,n,m,a,b,c,d,g,h,l,r,x,y,i,j,k;

    cin>>k>>n;
    vcl v(n);
    for(auto &it:v)cin>>it;
    cnt=(v[n-1]-v[0]);
    mx=0;
    for(i=1;i<n;i++)
    {
        mx=max(mx,(v[i]-v[i-1]));
    }
    cout<<min(cnt,k-mx)<<endl;
    return 0;
}


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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

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

    int H, W;
    cin >> H >> W;

    char a[110][110];
    bool check[2][110] = {{}};
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> a[i][j];
            if(a[i][j] == '#') {
                check[0][i] = 1;
                check[1][j] = 1;
            }
        }
    }

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

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

using namespace std;

int main(){
    int x,n;cin>>x>>n;
    bool visited[150];
    memset(visited,false,sizeof(visited));
    int u;
    for(int i=0;i<n;i++){
        cin>>u;
        visited[u]=true;
    }
    int smaller=x,larger=x;
    while(true){
        //cout <<smaller<<","<<larger<<":"<<visited[smaller]<<","<<visited[larger]<<endl;
        if(!visited[smaller]){
            cout <<smaller<<endl;
            return 0;
        }
        if(!visited[larger]){
            cout <<larger<<endl;
            return 0;
        }
        smaller--;
        larger++;
    }
}


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

int main()
{
    int A,B;    
    cin >> A >> B;
    int ans = 0;
     if(A >= B){
        ans += A;
        A--;
     }else{
        ans += B;
        B--;
     }
    if(A >= B){
        ans += A;
        A--;
     }else{
        ans += B;
        B--;
     }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
#include<math.h>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<set>
#include<vector>
using namespace std;

#define INF 1<<21
#define MOD 1000000007

int c[51][200];

int main(){
    int n;
    string ans="";
    int tmp;
    string s;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s;
        for(int j=0;j<s.length();j++){
            c[i][s[j]]++;
        }
    }
    for(char k='a';k<='z';k++){
        tmp=51;
        for(int i=0;i<n;i++){
            tmp=min(c[i][k],tmp);
        }
        for(int i=0;i<tmp;i++){
            ans+=k;
        }
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

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

using namespace std;

int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
//    ifstream cin("A.in");
    int r, c;
    cin >> r >> c;
    vector<vector<bool> > x(r);
    queue<pair<int, int> > indices;
    queue<int> dist;
    FOR (i, r){
        x[i].resize(c);
        FOR (j, c){
            char a;
            cin >> a;
            if (a == '#'){
                x[i][j] = true;
                indices.push({i, j});
                dist.push(0);
            }
            else
                x[i][j] = false;
        }
    }
    int ans = 0;
    while (!indices.empty()){
        ans = max(ans, dist.front());
        int i = indices.front().first, j = indices.front().second;
        int ii[] = {i, i, max(i - 1, 0), min(i + 1, r - 1)}, jj[] = {max(j - 1, 0), min(j + 1, c - 1), j, j};
        FOR (k, 4)
            if (!x[ii[k]][jj[k]]){
                x[ii[k]][jj[k]] = true;
                indices.push({ii[k], jj[k]});
                dist.push(dist.front() + 1);
            }
        indices.pop();
        dist.pop();
    }
    cout << ans << endl;
}


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

void Main()
{
    string s;
    int k;
    cin >> k >> s;
    if(s.size() > k){
        s = s.substr(0, k);
        s += "...";
    }
    cout << s << endl;
}

int main(int argc, char **argv)
{
    Main();
    return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<n;i++)
#define cY cout << "Yes" << endl;
#define cN cout << "No" << endl;

int main() {
  int h,a;
  cin >> h >> a;
  int cnt = 0;
  while(h>0){
    h -= a;
    cnt++;
  }
  cout << cnt << endl;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args){
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int N = sc.nextInt();
        
        int result = 0;
        int cal = 0;
        
        for(int i = 0; i <= A; i++){
            for(int j = 0; j <= B; j++){
                for(int k = 0; k <= C; k++){
                    cal = (500 * i) + (100 * j) + (50 * k);
                    if(cal == N){
                        result++;
                    }
                    cal = 0;
                }
            }
        }
        System.out.println(result);
        
    }
}


Measured memory_bytes: 27088
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int64_t X;
    cin >> X;
    int64_t money=100;
    int year=0;
    for(int i;money<X;i++){
        money=money+money/100;
        year++;
    }
    cout << year << endl;
}

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

int main(){

    int n;
    cin >> n;

    vector<tuple<string,int,int>> restaurant(n);
    for(int i=0; i<n; i++){
        cin >> get<0>(restaurant[i]) >> get<1>(restaurant[i]);
        get<1>(restaurant[i])*=-1;
        get<2>(restaurant[i])=i+1;
    }

    sort(restaurant.begin(),restaurant.end());

    for(int i=0; i<n; i++){
        cout << get<2>(restaurant[i]) << endl;
    }
}

Measured memory_bytes: 3624
#include <iostream>
#include <algorithm>
#include <cmath> 
#include <string>
using namespace std;
using ll = long long;
int main(){
    ll N; cin>>N;

    int ans = 1e9;
    for(int i=1;i<=sqrt(N);++i){
        if(N % i == 0) ans = min(ans,(int)to_string(N/i).size()); 
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int l=200000,r=200000,a[200010],b[400010];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	b[200000]=a[1];
	for(int i=2;i<=n;i++){
		if((n-i+1)&1){
			b[--l]=a[i];
		}else{
			b[++r]=a[i];
		}
	}
	for(int i=l;i<=r;++i){
		cout<<b[i]<<' ';
	}
	return 0;
}

Measured memory_bytes: 3712
import scala.io.StdIn.readLine
 
object Main {
  def search(a:Array[Int],value:Int) = {
    def scan(l:Int,r:Int):Boolean = {
      val mid = (l+r)/2
      if(a(mid) == value) true
      else if(l>=r) false
      else if(a(mid)<value) scan(mid+1,r)
      else scan(l,mid-1)
    }
    scan(0,a.length-1)
  }
 
  def main(args:Array[String]) = {
    readLine
    val s = readLine.split(" ").map(_.toInt).toArray
    readLine
    val t = readLine.split(" ").map(_.toInt).toArray
    println(t.count(i=> search(s,i) ))
  }
}

Measured memory_bytes: 72108
#include <stdio.h>

int main(){
	int m,a,b,c,d;
	int i,j,k,l;
	int count=0;
	int a1,b1,c1,d1;
	int a2,b2,c2,d2;
	int tmp,sum;
	
	scanf("%d\n",&m);
	while(m!=0){
		scanf("%d %d %d %d\n",&a,&b,&c,&d);
		a1=a; b1=b; c1=c;d1=d;
		sum=0;
		while(a>0){
			a--;
			sum+=10;
		}
		while(b>0){
			b--;
			sum+=50;
		}
		while(c>0){
			c--;
			sum+=100;
		}
		while(d>0){
			d--;
			sum+=500;
		}
		//printf("%d\n",sum);
		sum=sum-m;
		//printf("%d\n",sum);
		while(sum>=500){
			d++;
			sum-=500;
			//printf("%d\n",sum);
		}
		while(sum>=100){
			c++;
			sum-=100;
			//printf("%d\n",sum);
		}
		while(sum>=50){
			b++;
			sum-=50;
		}
		while(sum>=10){
			a++;
			sum-=10;
		}
		if(a1-a>0){
			printf("10 %d\n",a1-a);
		}
		if(b1-b>0){
			printf("50 %d\n",b1-b);
		}
		if(c1-c>0){
			printf("100 %d\n",c1-c);
		}
		if(d1-d>0){
			printf("500 %d\n",d1-d);
		}
		scanf("%d\n",&m);
		if(m!=0){
			printf("\n");
		}
	}
	return 0;
}

Measured memory_bytes: 1040
from heapq import heappush, heappop, heapreplace

def main():
    INF = 10 ** 9 + 7

    N, K, *TD = map(int, open(0).read().split())

    E = [[] for _ in range(N)]
    for t, d in zip(*[iter(TD)] * 2):
        E[t - 1].append(d)

    for e in E:
        if e:
            e.sort(reverse=True)
        else:
            e.append(-INF)
    E.sort(reverse=True)

    cur = 0
    Q = []
    for i in range(K):
        cur += E[i][0]
        for j in range(1, len(E[i])):
            heappush(Q, -E[i][j])

    for i in range(K, N):
        for eij in E[i]:
            heappush(Q, -eij)

    res = cur + K * K
    for x in reversed(range(1, K)):
        v, w = E[x][0], -Q[0]
        if v < w:
            heapreplace(Q, -v)
            cur += w - v

        tmp = cur + x * x
        if res < tmp:
            res = tmp

    print(res)

if __name__ == '__main__':
    main()


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

using namespace std;
vector<vector<int> > g;
bool vis[1000000];
int cont=0;
void dfs(int u){
    vis[u]=true;
    cont++;
    for(int i=0;i<g[u].size();++i){
        if(vis[g[u][i]]==false)dfs(g[u][i]);
    }
}


int main(){
    int n,a,x,y,ans=0; cin>> n >> a;
    vector<pair<int,int> > p;
    for(int i=0;i<a;++i){
        cin>> x >> y;
        x--,y--;
        p.push_back({x,y});
    }
    for(int i=0;i<a;++i){
        memset(vis,false,sizeof vis);
        g.assign(n+5,vector<int> ());
        for(int j=0;j<a;++j){
            if(j!=i)
            {g[p[j].first].push_back(p[j].second);
            g[p[j].second].push_back(p[j].first);}
        }
        dfs(0);
        if(cont<n)ans++;
        cont=0;
    }
    cout<<ans<<endl;

    return 0;
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  int n;
  cin >> n;
  vector<int> dp(n + 1);
  dp[0] = 0;
  rep(i, 1, n + 1) {
    dp[i] = dp[i - 1] + 1;

    for (int j = 6; j <= i; j *= 6)
      chmin(dp[i], dp[i - j] + 1);
    for (int j = 9; j <= i; j *= 9)
      chmin(dp[i], dp[i - j] + 1);
  }

  cout << dp[n] << endl;
}


Measured memory_bytes: 3604
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		int N = 0;
		String S;
		int K = 0;

		Scanner sc = new Scanner(System.in);
		N = Integer.parseInt(sc.next());
		S = sc.next();
		K = Integer.parseInt(sc.next());
		char s = S.charAt(K-1);
		int n;
		for(n=0;n<N;n++){
			if((String.valueOf(s).equals(String.valueOf(S.charAt(n))))==false){
				S= S.replaceFirst(String.valueOf(S.charAt(n)),"*");
			}
		}
		System.out.println(S);
	}
}

Measured memory_bytes: 21844
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>
#include <deque>
#include <numeric>

using namespace std;

#define mod 1000000007

int main()
{
	int a[3];
	int evennum = 0;
	for(int i = 0; i < 3; i++){
		cin >> a[i];
		if(a[i] % 2 == 0) evennum++;
	}
	int ans = 0;
	if(evennum == 1){
		ans = 1;
		for(int i = 0; i < 3; i++){
			if(a[i] % 2 == 1) a[i]++;
		}
	} else if(evennum == 2){
		ans = 1;
		for(int i = 0; i < 3; i++){
			if(a[i] % 2 == 0) a[i]++;
		}
	}
	sort(a, a + 3);
	ans += (a[2] - a[0]) / 2 + (a[2] - a[1]) / 2;
	cout << ans << endl;
}


Measured memory_bytes: 256
n, m = map(int, input().split())

from collections import defaultdict
d = defaultdict(list)
ab = [tuple(input().split()) for i in range(m)]

nAC = 0
nWA = 0


for a,b in ab:
    a = int(a)


    if len(d[a]) == 0:
        flag = 0
        num = 0
        d[a].append((flag, num))
    else:
        flag, num = d[a]


    if flag == 1:
        continue

    if flag == 0 and b == 'AC':
        flag = 1

    if flag == 0 and b == 'WA':
        num += 1

    d[a] = (flag, num)


for flag, num in d.values():
    if flag == 1:
        nAC += 1
        nWA += num

print(nAC, nWA)


Measured memory_bytes: 39240
#include <bits/stdc++.h>
 
using namespace std;
typedef long long int lli;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef pair <lli,lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
 
 
 
int main(){
	fast;
	int n, m, cnt=0;
	double tot=0;
	cin>>n>>m;
	vector<int> v(n);
	for(int i=0; i<n; i++){ cin>>v[i]; tot+=v[i]; }
	double val= tot/((double)(4*m));
	sort(v.begin(), v.end());
	for(int i= n-1; i>=0; i--){
	    if(v[i]>=val){ cnt++;}
	    if(cnt==m) break;
	}
	if(cnt==m) cout<<"Yes";
	else cout<<"No";
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <limits>
#include <map>
#define REP(i,n) for(long (i)=0;(i)<(n);(i)++)
#define REPI(i,a,b) for(long (i)=(a);(i)<(b);(i)++)
#define INF numeric_limits<double>::infinity()
constexpr long MOD = 1e9 + 7;
using namespace std;
using P = pair<long, long>;
using VI = vector<long>;

int main() {
    long n, a;
    VI v;
    cin >> n;
    REP(i,n) {
        cin >> a;
        v.push_back(a);
    }
    
    sort(v.begin(), v.end(), greater<long>());
    long ans = 0;
    REP(i,n) {
        if (i % 2 == 0) {
            ans += v[i];
        } else {
            ans -= v[i];
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
defmodule Main do
  def get_input do
    IO.read(:line) |> String.trim
  end

  def get_arr(0), do: []
  def get_arr(n), do: [get_input() | get_arr(n - 1)]

  def main do
    get_input()
    |> String.to_integer
    |> get_arr
    |> MapSet.new
    |> MapSet.size
    |> IO.inspect
  end
end


Measured memory_bytes: 152432
import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

         int N = sc.nextInt();
         int a[] = new int[N];
         for(int i=0;i<N;i++){
             a[i] = sc.nextInt();
         }



         int count = 0;
         for(int i=0;i<N;i=i+2){
            if(a[i]%2!=0){
                count++;
            }
         }
        System.out.println(count);
    }
}


Measured memory_bytes: 35896
#include <stdio.h>
int main(){
	int N,K,X,Y;
 	int pay=0;
	//N(高橋君の宿泊数),K(X円の上限),X(K泊までの料金),Y(K+1泊目以降の料金)
	scanf("%d",&N);
	scanf("%d",&K);
	scanf("%d",&X);
	scanf("%d",&Y);
  	if(N>K){
    	pay=K*X+(N-K)*Y;
    }else{
    	pay=N*X;
    }
	printf("%d",pay);
	return 0;
}

Measured memory_bytes: 128
object Main extends App {
	override def main(args: Array[String]) = {
		val sc = new java.util.Scanner(System.in)
		
		val pw = new java.io.PrintWriter(System.out);
		
		Iterator.continually(new Rectangle(sc.nextInt,sc.nextInt))
			.takeWhile(_.isContinue)
			.foreach(_.print(pw))

		pw.flush
		
	}
	
	class Rectangle(val H: Int, val W:Int){
		val isContinue = !(H == 0 && W == 0)
		
		def print(pw : java.io.PrintWriter):Unit = {
			for(i <- (1 to H)){
				for(j <- (1 to W)){
					(i,j) match{
						case (n,m) if (n + m) % 2 == 0 =>
							pw.print("#")
						case (_,_) => 
							pw.print(".")
					}
				}
				pw.println
			}
			pw.println
		}
		
	}
}

Measured memory_bytes: 48672
N, M = map(int, input().split())
H = list(map(int, input().split()))


A_list = []
B_list = []
Remove_list = []

for _ in range(M):
  A, B = map(int, input().split())
  A_list.append(A - 1)
  B_list.append(B - 1)

for A, B in zip(A_list, B_list):
  A_height = H[A]
  B_height = H[B]
  
  if A_height <= B_height:
    Remove_list.append(A)
  
  if B_height <= A_height: 
    Remove_list.append(B)
  
S = set(range(N))
S = S - set(Remove_list)

print(len(S)) 
 

Measured memory_bytes: 34168
import sys
input = sys.stdin.readline

def sol():
    N,M = list(map(int,input().split()))
    A = list(map(int,input().split()))
    tmp = sorted([list(map(int,input().split())) for _ in range(M)],key = lambda x:-x[1])
    cnt = 0
    t = []
    for a,b in tmp:
        t+=[b]*a
        cnt = cnt + a
        if cnt >=N:
            break

    A += t
    A.sort(reverse = 1)
    print(sum(A[:N]))

if __name__ == '__main__':
    sol()


Measured memory_bytes: 36508
#include<bits/stdc++.h>
#include <iomanip>
#include <iostream>
using namespace std;
using ll = long long;
ll INF = 9999999999999999;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll max(ll a, ll b){
    if(a > b) return a;
    else return b;
}
 
ll min(ll a, ll b){
    if(a < b) return a;
    else return b;
}

int main(void){
    int N; cin >> N;
    vector<string> s(N); for(int i = 0; i < N; i++) cin >> s[i];
    // vector<string> s_sort(N);
    map<string, int> mp;
    
    for(int i = 0; i < N; i++){
        sort(s[i].begin(), s[i].end());
        mp[s[i]]++;
    }
    
    ll ans = 0;
    for(auto itr : mp){
        if(itr.second > 1){
            if(itr.second % 2 == 0) ans += ((ll)(itr.second/2) * (itr.second-1));
            else ans += ( (ll)((itr.second-1)/2) * itr.second );
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 18176
#from collections import deque
#from heapq import heapify, heappop, heappush
#from bisect import insort
#from math import gcd
#mod = 10**9 + 7
#N = int(input())
#N, K = map(int, input().split())
#A = list(map(int, input().split()))
#flag = True
#for k in range(N):
#ans = 0
#print(ans)
#print('Yes')
#print('No')
S = input()
if S[-1] != 's':
	print(S + 's')
else:
	print(S+'es')


Measured memory_bytes: 61520
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int iinf = 1 << 28;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;


int main() {
    int n; string s; cin>>n>>s;
    int ans = 1;
    for (int i = 1; i < n; ++i) {
        if (s[i] != s[i-1]) ++ans;
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mk make_pair
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()

using namespace std;

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

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

	vector <int> p, q;

	for(int i = 0; i < n; i++){
		int a, b;
		cin >> a >> b;

		p.pb(a+b);
		q.pb(a-b);
	}

	sort(all(p));
	sort(all(q));

	int ans = 0;
	ans = max(ans, p[n-1] - p[0]);
	ans = max(ans, q[n-1] - q[0]);

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


Measured memory_bytes: 5068
import std.string;
import std.conv;
import std.array;
import std.stdio;
import std.algorithm;
import std.range;
import std.math;
import std.container;

struct Log {
	double length, beforeLength;
	long cut;
}

void main() {
	Log convert(long s) {
		Log log;
		log.length = log.beforeLength = cast(double)s;
		return log;
	}

	long n, k;
	readf("%s %s\n", &n, &k);

	auto lengthes = readln.chomp.split(' ').map!(to!long).array;
	auto sumLength = lengthes.sum;
	auto logs = lengthes.map!convert.array;

	auto rest = k;
	foreach (ref log;logs) {
		log.cut = cast(long)(log.beforeLength / sumLength * k);
		if (log.cut > rest) {
			log.cut = rest;
		}
		log.length = log.beforeLength / (log.cut + 1);

		rest -= log.cut;
		if (rest <= 0) break;
	}

	auto heap = heapify!"a.length < b.length"(logs);
	while (rest > 0) {
		auto log = heap.front;
		heap.removeFront;

		log.cut++;
		log.length = log.beforeLength / (log.cut + 1);

		heap.insert(log);
		--rest;
	}

	writeln(cast(long)ceil(heap.front.length));
}


Measured memory_bytes: 15192
import java.util.Random;
import java.util.Scanner;
 


public class Main {
	private static Scanner sc = new Scanner(System.in);
	//result = Math.pow(num1, num3)
	//StringBuffer str = new StringBuffer(hoge1);
	//String hoge2 = str.reverse().toString();
	//map.containsKey(A)

	//Map<String, Integer> map = new HashMap<String, Integer>(n);
	/*for ( キーのデータ型 key : マップの名前.keySet() ) {
		データのデータ型 data = マップの名前.get( key );
		
		// keyやdataを使った処理;
	}*/
	//int i = Integer.parseInt(s);
	//Queue<String> qq=new ArrayDeque<>(); //add,poll,peek
	//Deque<String> qq=new ArrayDeque<>();//push,pop,peek
	//ArrayList<String> cc = new ArrayList<>(n);
	//Collections.sort(list);
	//Array.sort(list);   cc.contains(tmp)
	//Arrays.asList(c).contains("a")
	//list.set(1,"walk");//1 1 2 3 5
	//static long mod =1000000007;
	//static 	ArrayList<Integer> cc = new ArrayList<>(100);
	
	
	
	public static void main(String[] args) {
		//int N=sc.nextInt();for(int i=0;i<N;i++) {}
		//int a[]=new int[N];
		
			
			
			int N=sc.nextInt();
			
			long a[]=new long[N];
			long b[]=new long[N];
			for(int i=0;i<N;i++) {
				a[i]=sc.nextInt();
				b[i]=sc.nextInt();
				a[i]=b[i]-a[i]%b[i];
			}
			
			
			long sum=0;
			
			for(int i=N-1;i>=0;i--) {
				a[i]=a[i]-sum%b[i];
				if(a[i]<0) {
					a[i]+=b[i];
					
				}
				a[i]%=b[i];
				//p(sum);
				sum+=a[i];
			}
			
		
			p(sum);
		}
		
		
	

	

	public static long gcd(long a, long b) {
        return b == 0 ? a: gcd(b, a % b);
	}
	public static long lcm(long n, long m) {
        return n / gcd(n, m) * m;
}
	
	

	


	
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p() {System.out.println();};
	static void p(long ans) {System.out.println(ans);};
	static void p(double ans) {System.out.println(ans);};
	
}
	


Measured memory_bytes: 83512
#include<iostream>
#include<algorithm>
#define Max 44
#define null (-1)
using namespace std;
int f[Max];
int fib(int a){
	if(a==0 || a==1)
	return f[a]=1;
	if(f[a]!=null)
	return f[a]; 
	return f[a]=fib(a-1)+fib(a-2);
	
}
main(){
	int n;
	cin>>n;
	for(int i=0;i<=n;i++)
	f[i]=null;
	cout<<fib(n)<<endl;
	return 0;
}


Measured memory_bytes: 3108
#include <iostream>
#include <iomanip>
#include <random>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <sstream>
#include <bitset>
#include <cassert>
#include <fstream>
#include <queue>

#define len(X) ((int)(X).size())

#ifdef __LOCAL
	#define DBG(X) cout << #X << "=" << (X) << '\n';
#else
	#define DBG(X)
#endif

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::pair;
using std::swap;
using std::sort;
using std::lower_bound;
using std::unique;

using int64 = long long int;
using pii   = pair<int, int>;
using pii64 = pair<int64, int64>;

const int NIL = -1;
const int INF = (int)2e9;
const int64 INF64 = (int64)2e18;

void init_IO() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
}

void solve() {
	int n, k;
	string str;
	cin >> n >> k >> str;

	vector<pii> data;

	for(int i = 0; i < n;) {
		int j = i;

		while(j + 1 < n && str[j + 1] == str[i]) {
			++j;
		}

		data.emplace_back((str[i] - '0') ^ 1, j - i + 1);
		i = j + 1;
	}

	int m = len(data);
	int r = 0, cnt = 0, sum = 0, answ = 0;

	for(int l = 0; l < m; ++l) {
		while(r < m && cnt + data[r].first <= k) {
			cnt += data[r].first;
			sum += data[r].second;

			++r;
		}

		answ = std::max(answ, sum);

		cnt -= data[l].first;
		sum -= data[l].second;
	}

	cout << answ << '\n';
}

int main() {
	init_IO();
	solve();

	return 0;
}


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

// 桁数、未満フラグ、mod D
ll dp[100001][2][101];
string K;
int D;

void addmod(ll& src, const ll& tgt, const ll& mod=1000000007) {
  int ans = ((src % mod) + (tgt % mod)) % mod;
  if (ans < 0) ans += mod;
  src = ans;
}

ll memo(int i, int smaller, int modD) {
  // 桁数を超えたら終了
  if (i >= K.size()) {
    return modD == 0;
  }

  // メモしてある値ならそれを返す
  if (dp[i][smaller][modD] != -1) {
    return dp[i][smaller][modD];
  }

  // 超えてるフラグ
  int current = K[i] - '0';
  int limit = smaller ? 9 : current;
  ll ret = 0;
  for (int d = 0; d <= limit; d++) {
    ll tmp = memo(i + 1, smaller ? smaller : d < limit, (modD + d) % D);
    addmod(ret, tmp);
  }

  return dp[i][smaller][modD] = ret;
}

int main() {
  rep(i, 100001) rep(j, 2) rep(k, 101) { dp[i][j][k] = -1; }

  cin >> K;
  cin >> D;

  ll cnt = memo(0, 0, 0);
  addmod(cnt, -1);
  cout << cnt << endl;
}

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

// @@ !! LIM(UnionFind)
// --> UnionFind
// ---- inserted library file UnionFind.cc

class UnionFind {
protected:
  int size;
  vector<int> _leader;
  vector<int> _rank;
  vector<int> _gsize;
  unordered_map<int, vector<int>> _groups;
  
public:
  UnionFind(int s);
  int leader(int i);
  int merge(int i, int j);
  int groupSize(int i);
  const unordered_map<int, vector<int>>& groups();
  const vector<int>& group(int i);
};

UnionFind::UnionFind(int s) {
  size = s;
  _leader = vector<int>(size);
  for (int i = 0; i < size; i++) { _leader.at(i) = i; }
  _rank = vector<int>(size, 1);
  _gsize = vector<int>(size, 1);
}

int UnionFind::leader(int i) {
  int cur = i;
  int nxt = _leader.at(cur);
  vector<int> seen;
  while (cur != nxt) {
    seen.push_back(cur);
    cur = nxt;
    nxt = _leader.at(cur);
  }
  for (auto j : seen) _leader.at(j) = cur;
  return cur;
}

int UnionFind::merge(int i0, int j0) {
  if (!_groups.empty()) {
    cerr << "merge() cannot be called after group() or groups()." << endl;
    exit(1);
  }
  int li = leader(i0);
  int lj = leader(j0);
  if (li == lj) return li;
  int ri = _rank.at(li);
  int rj = _rank.at(lj);
  int rep = li;
  int other = lj;
  if      (ri <  rj) swap(rep, other);
  else if (ri == rj) _rank.at(rep)++;
  _leader.at(other) = rep;
  _gsize.at(rep) += _gsize.at(other);
  return rep;
}

int UnionFind::groupSize(int i) {
  return _gsize.at(leader(i));
}

const unordered_map<int, vector<int>>& UnionFind::groups() {
  if (_groups.empty()) {
    for (int i = 0; i < size; i++) _groups[leader(i)].push_back(i);
  }
  return _groups;  
}

const vector<int>& UnionFind::group(int i) { return groups().at(leader(i)); }



// ---- end UnionFind.cc
// @@ !! LIM  -- end mark --

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

  ll N, M; cin >> N >> M;
  UnionFind uf(N);
  for (ll i = 0; i < M; i++) {
    ll a, b; cin >> a >> b; a--; b--;
    uf.merge(a, b);
  }
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    if (uf.leader(i) != i) continue;
    ans = max<ll>(ans, uf.groupSize(i));
  }
  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 5576
#include <iostream>
#include <string>
#include <map>
#include <vector>

using namespace std;

int main()
{
	int N;
	cin >> N;
	map<string, int> m;
	for (int i = 0; i < N; ++i) {
		string S;
		cin >> S;
		++m[S];
	}

	const vector<string> v = { "AC", "WA", "TLE", "RE" };
	for (auto& s : v) {
		cout << s << " x " << m[s] << endl;
	}
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <db, db> pdd;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;

#define FOR(k,a,b) for(int k=(a); k<=(b); ++k)
#define IFOR(k,a,b) for(int k=(a); k>=(b); --k)
#define REP(k,a) for(int k=0; k<(a);++k)
#define IREP(k,a) for(int k=a-1; k>=(0);--k)
#define SZ(a) int((a).size())
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define x first
#define y second
#define INF 1000000001
#define INFLONG 1000000000000000000
#define MOD 1000000007
#define MAX 100
#define ITERS 100
#define MAXM 200000
#define MAXN 1000000
#define PI 3.1415926535897932384626
#define ERR -987654321
#define MEM(a,b) memset(a,b,sizeof(a));
#define read1(a) scanf("%d",&a)
#define read2(a,b) scanf("%d%d",&a,&b)
#define read3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define readl1(a) scanf("%lld",&a)
#define readl2(a,b) scanf("%lld%lld",&a,&b)
#define readl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    int rn = uniform_int_distribution<int>(0, MAXN)(rng);

    int n,k;
    cin>>n>>k;

    pii a[k];
    ll f[2*n], h[2*n];
    bool g[2*n][2*n];
    ll dp[2*n][2*n], power[1001];

    MEM(g,false);
    
    REP(i,2*n) h[i] = 1, f[i] = -1;

    REP(i,k) {
        int p,q;
        cin>>p>>q;
        p--, q--;
        a[i] = MP(p,q);
        f[p] = q, f[q] = p;
        h[p] = h[q] = 0;
    }
    
    REP(i,2*n) if(i) h[i] += h[i-1];
    // REP(i,2*n) cout<<h[i]<<" "; cout<<endl;

    REP(i,2*n) {
        FOR(j,i+1,2*n-1) {
            bool flag = true;
            FOR(k,i,j) {
                if(f[k] < i && f[k] != -1) flag = false;
                if(f[k] > j && f[k] != -1) flag = false;
            }
            g[i][j] = flag;
        }
    }
    
    // REP(i,2*n){ REP(j,2*n) cout<<g[i][j]<<" "; cout<<endl;}

    power[1] = power[0] = 1ll;
    FOR(i,2,1000) power[i] = (power[i-1] * (2*i-1))%MOD;

    FOR(l,2,2*n) {
        FOR(i,0,2*n-l) {
            int j = i+l-1;
            int p = h[j] - ((i==0)? 0 : h[i-1]);
            if(p%2 == 1 || !g[i][j]) dp[i][j] = 0;
            else {
                dp[i][j] = power[p/2];
                FOR(k,i+1,j-1) {
                    int q = h[j] - h[k];
                    if(q%2 == 0) dp[i][j] = (dp[i][j] - (dp[i][k]*power[q/2])%MOD + MOD)%MOD;
                }
            }
            // cout<<i<<" "<<j<<" "<<p<<" "<<dp[i][j]<<endl;
        }
    }

    // cout<<"asdads"<<endl;
    
    ll res = 0;
    REP(i,2*n) {
        FOR(j,i+1,2*n-1) {
            int  p = h[2*n-1] - (h[j] - ((i==0)? 0 : h[i-1]));
            // cout<<i<<" "<<j<<" "<<p<<" "<<dp[i][j]<<endl;
            if(p%2 == 0) res = (res + (dp[i][j]*power[p/2])%MOD)%MOD;
        }
    }
    cout<<res;
    return 0;
}

Measured memory_bytes: 3456
// 2010/12/28 Tazoe

#include <iostream>
using namespace std;

void rotate(int pct[50][50], int m)
{
	int tmp[50][50];

	for(int y=0; y<m; y++)
		for(int x=0; x<m; x++)
			tmp[y][x] = pct[y][x];

	for(int y=0; y<m; y++)
		for(int x=0; x<m; x++)
			pct[x][m-1-y] = tmp[y][x];
}

bool match(int scn[100][100], int n, int pct[50][50], int m, int xs, int ys)
{
	for(int i=0; i<4; i++){
		bool flg = true;
		for(int y=0; y<m; y++){
			for(int x=0; x<m; x++){
				if(pct[y][x]==-1)
					continue;
				if(scn[ys+y][xs+x]!=pct[y][x]){
					flg = false;
					break;
				}
			}
			if(!flg)
				break;
		}

		if(flg)
			return true;

		if(i<3)
			rotate(pct, m);
	}

	return false;
}

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;
		if(n==0&&m==0)
			break;

		int scn[100][100];
		for(int y=0; y<n; y++)
			for(int x=0; x<n; x++)
				cin >> scn[y][x];

		int pct[50][50];
		for(int y=0; y<m; y++)
			for(int x=0; x<m; x++)
				cin >> pct[y][x];

		bool flg = false;
		for(int y=0; y<n-m+1; y++){
			for(int x=0; x<n-m+1; x++){
				if(match(scn, n, pct, m, x, y)){
					int dx, dy;
					bool flg2 = false;
					for(dy=0; dy<m; dy++){
						for(dx=0; dx<m; dx++){
							if(pct[dy][dx]!=-1){
								flg2 = true;
								break;
							}
						}
						if(flg2)
							break;
					}

					cout << x+1+dx << ' ' << y+1+dy << endl;
					flg = true;
					break;
				}
			}
			if(flg)
				break;
		}

		if(!flg)
			cout << "NA" << endl;
	}

	return 0;
}

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

const int M = 1000000007;

class unionfind {
    public:
    int n;
    vector<int> par;
    unionfind(int m) {
        n = m;
        par.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i;
    }
    int find(int x) { return par[x] == x ? x : (par[x] = find(par[x])); }
    int isunion(int x, int y) { return find(x) == find(y); }
    int tounion(int x, int y) {
        int p = find(x), q = find(y);
        if (p == q) return 0;
        par[q] = p;
        return 1;
    }
};

int main() {
    while (1) {
        int n, q;
        scanf("%d%d", &n, &q);
        if (!n) return 0;
        vector<int> par(n);
        for (int i = 1; i < n; ++i) {
            cin >> par[i];
            --par[i];
        }
        vector<pair<char, int>> inp(q);
        vector<int> cnt(n, 0);
        for (int i = q - 1; i >= 0; --i) {
            cin >> inp[i].first >> inp[i].second;
            --inp[i].second;
            if (inp[i].first == 'M') ++cnt[inp[i].second];
        }
        unionfind uf(n);
        for (int i = 1; i < n; ++i) if (!cnt[i]) uf.tounion(par[i], i);
        long long sum = 0;
        for (auto i : inp) {
            if (i.first == 'Q') sum += uf.find(i.second) + 1;
            else if (!--cnt[i.second]) uf.tounion(par[i.second], i.second);
        }
        printf("%lld\n", sum);
    }
}

Measured memory_bytes: 4840
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ls( s) (s&(-s))
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 2e5+5;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod =    2019;
int n;
ll ans=0;
multiset<ll> ms;
int main()
{  fast
cin>>n;

vector<ll> v(n);
for(auto&x:v)cin>>x;
for(auto x: v)ms.insert(x);
while(!ms.empty())
{
	ll val=*ms.rbegin();
	ms.erase(ms.find(val));ll y;
	for( y=1;y<=val;y*=2);
		auto it=ms.find(y-val);
		if(it!=ms.end())
		{
			ans++;
			ms.erase(it);

		}
}
cout<<ans;
}


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

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; each (i, v) os << i << ","; os << ")"; return os; }
template<typename T> istream& operator >> (istream& is, vector<T>& v) { each (i, v) is >> i; return is; }

template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }

constexpr int N = 1e5 + 5;

const int D = log2(N) + 1;
int parent[N][D];
int depth[N];

vector<pair<int, lli>> g[N];

void dfs(int v, int p, int d)
{
  parent[0][v] = p;
  depth[v] = d;
  each (u, g[v]) {
    if (u.first != p) dfs(u.first, v, d + 1);
  }
}
void init(int root, int V)
{
  dfs(root, -1, 0);
  for (int k = 0; k < D; ++k) {
    for (int v = 0; v < V; ++v) {
      if (parent[k][v] < 0) parent[k][v] = -1;
      else parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v)
{
  if (depth[u] > depth[v]) swap(u, v);
  for (int k = 0; k < D; ++k) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
    if (u == v) return u;
    for (int k = D - 1; 0 <= k; --k) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }
  }
  return parent[0][u];
}

lli dist[N];
void rec(int curr, int prev)
{
  each (e, g[curr]) {
    int next = e.first;
    if (next != prev) {
      dist[next] = dist[curr] + e.second;
      rec(next, curr);
    }
  }
  return ;
}

int main(int argc, char *argv[])
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  while (cin >> n) {
    fill(g, g + N, vector<pair<int, lli>>());
    fill(dist, dist + N, 0);
    for (int i = 0; i < n - 1; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      g[a].push_back(make_pair(b, c));
      g[b].push_back(make_pair(a, c));
    }

    int q, k;
    cin >> q >> k;
    --k;

    dist[k] = 0;
    rec(k, k);
    init(k, n);

    while (q--) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      // int z = query(x, y);
      int z = lca(x, y);

      // cout << x+1 << ' ' << y+1 << ' ' << z+1 << endl;
      // cout << dist[x] << ' ' <<  dist[y] << ' ' <<  dist[z] << endl;

      cout << dist[x] + dist[y] << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 15232
using System;

class AOJ{
  static void Main(string[] args)
  {
    int[] ans = new int[31];
    int w = int.Parse(Console.ReadLine());
    int n = int.Parse(Console.ReadLine());

    for (int i = 0; i < w; i++)
      ans[i] = i + 1;

    for (int i = 0; i < n; i++){
      string[] s = Console.ReadLine().Split(',');

      int a = int.Parse(s[0]);
      int b = int.Parse(s[1]);

      int tmp = ans[a - 1];
      ans[a - 1] = ans[b - 1];
      ans[b - 1] = tmp;
    }

    for (int i = 0; i < w; i++)
      Console.WriteLine(ans[i]);
  }
}

Measured memory_bytes: 9156
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <set>
#include <stack>
#include <cctype>
#include <cmath>
#include <bitset>

using namespace std;

/* typedef */
typedef long long ll;

/* constant */
const int INF = 1 << 30;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;

/* global variables */

/* function */
void findPredfix(string pattern,  vector<int>& prefArray);
void kmpSearch(string mainString, string pattern);

/* main */
int main(){
    string T, P;

    cin >> T >> P;

    kmpSearch(T, P);

}

void findPredfix(string pattern, vector<int>& prefArray) {
    prefArray[0] = 0;
    int length = 0;
    for (int i = 1; i < prefArray.size(); i++) {
        if (pattern[i] == pattern[length]) {
            length++;
            prefArray[i] = length;
        } else {
            if (length != 0) {
                length = prefArray[length - 1];
                i--;
            } else
                prefArray[i] = 0;
        }
    }
}

void kmpSearch(string mainString, string pattern) {
    // Knuth-Morris-Pratt Algorithm
    // The time complexity of KMP algorithm is O(n)
    int n, m, i, j;
    n = mainString.size();
    m = pattern.size();
    // =========================
    // First : Make prefix Array
    // ex) for pattern = "ABABCA"
    // A B A B C A
    //   A
    //     A B A
    //           A
    // 0 0 0 1 2 0
    vector<int> prefArray(m);
    findPredfix(pattern, prefArray);
    // =========================
    // Second : Search string using kmp algo
    i = 0, j = 0;
    while (i < n) {
        if (mainString[i] == pattern[j]) {
            i++; j++;
        }

        if (j == m) {
            cout << i - j << '\n';  // pattern matching at i-j pos
            j = prefArray[j-1];  // get the prefix lenght from array
        }
        else if (pattern[j] != mainString[i]) {
            if (j != 0)
                j = prefArray[j-1];  // get the prefix lenght from array
            else 
                i++;  // if pattern[0] != mainString[0] -> i++
        }
    }
}



Measured memory_bytes: 4912
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
    ll x,flag,i,t,ans=0,j,k,l,n;
    scanf("%lld %lld",&n,&k);
    ll N=n;
    while(n)
    {
        n/=k;
        ans++;
    }
    printf("%lld\n",ans);
}


Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<fstream>
#include<stdlib.h>
#include<set>
#include<climits>
#include<cmath>  
#include<memory.h> 
#include<sstream>
#include<time.h>
#include<iomanip>
using namespace std;
const unsigned long long BIGEST=1000000000000000000+1000000000000000000;
const long long BIGER=1000000000000000000;
const int BIG=1000000000;
const int MOD=1000000007;
const long long LMOD=2147483647;
//const long long LMOD=1000000007;
#define F first
#define S second
#define p2 pair
#define pii pair<int,int>
#define cb make_pair
#define ll long long
#define ull unsigned long long
int main()
{
	string st;
	cin>>st;
	int g=0;
	for(int i=0;i<st.size();i++)if(st[i]=='x')g++;
	if(g>7)puts("NO");
	else puts("YES");
	return 0;
}

Measured memory_bytes: 256