akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
using namespace __gnu_pbds;
using namespace std;
 
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
typedef long long ll;
typedef long double ld;
typedef pair<long long ,long long> ii; 

ll m = 1e9+7;
 
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll h,w;
	cin >> h >> w;
	vector <vector<ll>> grid(h,vector<ll>(w,0));
	bitset<12801> dp[h][w];
	for (int z=0;z<h;z++){
		for (int x=0;x<w;x++){
			cin >> grid[z][x];
		}
	}
	for (int z=0;z<h;z++){
		for (int x=0;x<w;x++){
			ll temp; cin >> temp;
			grid[z][x] -= temp;
			grid[z][x] = abs(grid[z][x]);
		}
	}
	dp[0][0][grid[0][0]+6400] = 1;
	dp[0][0][-grid[0][0]+6400] = 1;
	for (int z=0;z<h;z++){
		for (int x=0;x<w;x++){
			if (z+1 < h){
				dp[z+1][x] |= dp[z][x] << grid[z+1][x];
				dp[z+1][x] |= dp[z][x] >> grid[z+1][x];
			}
			if (x+1 < w){
				dp[z][x+1] |= dp[z][x] << grid[z][x+1];
				dp[z][x+1] |= dp[z][x] >> grid[z][x+1];
			}
		}
	}
	ll ans = LLONG_MAX;
	for (ll z=-6400;z<=6400;z++){
		if (dp[h-1][w-1][z+6400]){
			ans = min(abs(z),ans);
		} 
	} 
	cout << ans << endl;
}


Measured memory_bytes: 10368
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
constexpr int kMod = 1000000007;
typedef long long LL;

std::map<char, int> countup(std::string S) {
  std::map<char, int> cnt;
  for (char c : S) {
    ++cnt[c];
  }
  return cnt;
}

int main() {
  int N; std::cin >> N;
  std::string S; std::cin >> S;
  std::map<char, int> ret = countup(S);
  for (int i = 1; i < N; ++i) {
    std::cin >> S;
    std::map<char, int> tmp = countup(S);
    for (auto p : ret) {
      char ch = p.first;
      int cnt = p.second;
      if (tmp.find(ch) != tmp.end()) {
        ret[ch] = std::min(ret[ch], tmp[ch]);
      } else {
        ret[ch] = 0;
      }
    }
  }

  for (auto p : ret) {
    for (int i = 0; i < p.second; ++i) std::cout << p.first;
  }
  std::cout << std::endl;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long A = sc.nextLong();
    long B = sc.nextLong();

    // B is n bit.
    long b = B;
    int n = 0;
    while (b/2 > 0) {
      b /= 2;
      n++;
    }

    long pow = 1;
    long ans = 0;
    int i = 0;
    while (i <= n) {
      long num1 = num1(B, pow) - num1(A-1, pow);
      if (num1 % 2 == 1) {
        ans += pow;
      }

      pow *= 2;
      i++;
    }
    
    System.out.println(ans);
  }

  private static long num1(long a, long pow) {
    long x = (a+1)/pow;
    long num1 = x/2 * pow;
    if (x%2 == 1) {
      num1 += (a+1)%pow;
    }
    return num1;
  }

}

Measured memory_bytes: 23380
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
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;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int n;
    cin >> n;
    vector<tuple<int, int, char, ll, string>> a(n);
    int v, w;
    char x;
    ll y;
    string z;
    REP(i, n) {
        cin >> v >> w >> x >> y >> z;
        a[i] = make_tuple(v, w, x, y, z);
    }

    SORT(a);
    for (auto p : a) {
        tie(v, w, x, y, z) = p;
        cout << v << " " << w << " " << x << " " << y << " " << z << endl;
    }
}



Measured memory_bytes: 11136
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll ; 
typedef long double ld ;
typedef pair<ll , ll> pll ;

const ll N = 2e5 + 30 , Mod = 1e9 + 7 ;
const ll SQ = 330 ;

int main () {
    ios::sync_with_stdio(0), cin.tie(0) ;
    ll n ;
    cin >> n ;
    ll m = n ;
    m /= 27 ;
    m *= 25;
    //cout << m << endl;
    for (ll i = max(1ll , m - 500) ; i < m + 500 ; i ++) if (i * 27 / 25 ==   n) return cout << i , 0;
    cout << ":(";
    
    return (0) ;
}

Measured memory_bytes: 256
#include <stdio.h>
int main()
{
    int n,l,h,p[101],c[1000] = {0},ma[1000] = {0},ans = 0;
    scanf("%d%d%d",&n,&l,&h);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&p[i]);
    }
    for(int i = 1; i < h + 1; i++)
    {
        for(int j = 0; j < n; j++)
        {
            c[(p[j] * i) % l]++;
        }
        for(int j = 0; j < l; j++)
        {
            if(ma[j] < c[j])ma[j] = c[j];
        }
        if(i != 1)
        {
            for(int j = 0; j < n; j++)
            {
                c[(p[j] * i) % l]--;
            }
        }
    }
    for(int i = 0; i < l; i++)
    {
        ans += ma[i];
    }
    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 1032
#include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;

#define fi first
#define se second
#define pb push_back
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define H_MAX 50
#define W_MAX 50

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

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

int main() {

	string s;
	cin >> s;
	int n = s.length();
	int count1=0, count2=0;
	rep(i,2) {
		if(i==0) {
			rep(j,n) {
				if(j % 2 == 0 && s[j] == '0') 
					count1++;
				if(j % 2 == 1 && s[j] == '1')
					count1++;
			}
			continue;
		}
		rep(j,n) {
			if(j % 2 == 0 && s[j] == '1')
				count2++;
			if(j % 2 == 1 && s[j] == '0')
				count2++;
		}
	}
	cout << min(count1, count2) << endl;

  return 0;
}
 


Measured memory_bytes: 512
#include <iostream>
using namespace std;
void ABC100(void);
void ABC101(void);
void ABC102(void);
void ABC103(void);
void ABC104(void);
void ABC105(void);
void ABC106(void);
void ABC107(void);
void ABC108(void);
void ABC109(void);

int main(void){
    ABC104();
}

void ABC104(void){
    int r;
    cin>>r;
    if(r<1200)cout<<"ABC"<<endl;
    else if(r<2800)cout<<"ARC"<<endl;
    else cout<<"AGC"<<endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<algorithm>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp = boost::multiprecision;
using namespace std;
int main()
{

	int n;
	cin >> n;
	for (int i = 0; i < 9; i++)
	{
		for (int w = 0; w < 9; w++)
		{
			if ((i + 1) * (w + 1) == n)
			{
				cout << "Yes" << endl;
				return 0;
			}
		}
	}
	cout << "No" << endl;

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,ans,pre,mod=1e9+7,x[N];
int q_pow(int x,int y){
	int res=x,ans=1;
	for(;y;y>>=1,res=1ll*res*res%mod)if(y&1)ans=1ll*ans*res%mod;
	return ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&x[i]);
	for(int i=1;i<n;i++)pre=(pre+q_pow(i,mod-2))%mod,ans+=1ll*(x[i+1]-x[i])*pre%mod,ans%=mod;
	for(int i=1;i<n;i++)ans=1ll*ans*i%mod;
	printf("%d",ans);
}

Measured memory_bytes: 640
#include<stdio.h>

int main(){
    int n;
    scanf("%d",&n);
    int a[n];
    a[0] = a[1] = 1;
    for(int i=2 ; i<=n ; i++)
        a[i] = a[i-1] + a[i-2];
    printf("%d\n",a[n]);
    return 0;
}



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

using namespace std;
using ll = long long;
auto const INF = static_cast<ll>(1e9);
void a(){
    int a,b;
    int c[3] = {1,2,3};
    cin >> a >> b;
    for(int i:c){
        if(i==a||i==b)continue;
        cout <<i << endl;
        return;
    }
}

void b(){
    int n;
    string s,t;
    cin >> n;
    cin >> s >> t;
    for(int i=0;i<n;i++){
        cout << s[i] << t[i];
    }
}

int gcd(int a,int b){
    return a==0? a:gcd(b,a%b);
}

void c(){
    cout <<gcd(3,6);
}

void d(){
    int n;
    cin >> n;
    auto a = vector<int>(n,0);
    for(int i=0;i<n;i++){
        cin >>a[i];
        a[i]--;
    }
    int next = 0;
    int ans = 0;
    for(int i=0;i<n;i++){
        if(a[i]==next){
            next++;
        }else{
            ans++;
        }
    }
    
  cout << (next==0?-1:ans) << endl;
}
int main()
{
    d();
    return 0;
}


Measured memory_bytes: 1024
#define _GLIBCXX_DEBUG //エラーを出すためのおまじない
#include <bits/stdc++.h>
//#include <cstdlib>  // abs() for integer
#include <cmath>    // abs() for float, and fabs()
#include <math.h>
using namespace std;
#define coYES cout << "YES" << endl
#define coNO cout << "NO" << endl
#define coYes cout << "Yes" << endl
#define coNo cout << "No" << endl
#define coyes cout << "yes" << endl
#define cono cout << "no" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int64_t n, k;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << endl;
  }
  else {
    n %= k;
    cout << min(n, abs(n-k)) << endl;
  }
}


Measured memory_bytes: 3596
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int N=100000;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u];i;i=sq[i].nxt)
#define fir first
#define sec second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
int n;
char s[100100];

int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	int cnt=0,ans=0;
	rep(i,1,n)
	{
		if (s[i]=='p')
		{
			if (cnt>0) cnt--;else {cnt++;ans--;}
		}
		else if (s[i]=='g')
		{
			if (cnt>0) {cnt--;ans++;}else cnt++;
		}
	}
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
bool minmin(int x, int y, int z) {
  if(x + y > z){
    return true;
  }
  else {
    return false;
  }
}
int main () {
  int N;
  cin >> N;
  vector<int> S(N);
  for(int i = 0; i < N; i++) {
    cin >> S.at(i);
  }
  sort(S.begin(), S.end());
  int count = 0;
  for(int i = 0; i < N-2; i++) {
    for(int j = i+1; j < N-1; j++) {
      for(int k = j+1; k < N; k++) {
        if(minmin(S.at(i), S.at(j), S.at(k))) {
          if(!(S.at(i) == S.at(j) || S.at(j) == S.at(k))) {
            if(!(S.at(i) == S.at(k))) {
          count++;
        }
      }
        }
      }
    }
  }
  cout << count << endl;
}

Measured memory_bytes: 3628
// Author: HarshKumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 505, M = 1e9 + 7;
ll d[N][N], f[N][N];
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    ll n, m, c = 0, ans = 0;
    cin >> n >> m;
    vector<string> s(n);
    for (auto &e : s)
        cin >> e;
    for (ll i = 0; i < n; i++)
        for (ll j = 0; j < m; j++)
            if (s[i][j] == '.')
            {
                f[i][j] = c;
                if (i && s[i - 1][j] == '.')
                    d[c][f[i - 1][j]] = d[f[i - 1][j]][c] = 1;
                if (j && s[i][j - 1] == '.')
                    d[c][f[i][j - 1]] = d[f[i][j - 1]][c] = 1;
                c++;
            }
    for (ll i = 0; i < c; i++)
        for (ll j = 0; j < c; j++)
            if (i != j && !d[i][j])
                d[i][j] = M;
    for (ll k = 0; k < c; k++)
        for (ll i = 0; i < c; i++)
            for (ll j = 0; j < c; j++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    for (ll i = 0; i < c; i++)
        for (ll j = 0; j < c; j++)
            if (d[i][j] < M)
                ans = max(ans, d[i][j]);
    cout << ans;
    return 0;
}

Measured memory_bytes: 1920
#include<bits/stdc++.h>
using namespace std;
#define int long long 
int p[100010];
vector<int> G[100010];
int wpath[100010];
int gw[100010];
int dfs_n[100010];
int n_dfs[100010];
int par[100010]; 

struct Lazseg{
  static const int MAX=1<<18;
  int n;
  int dat[2*MAX-1],laz[2*MAX-1];
  Lazseg(){}
  Lazseg(int n_){
    init(n_);
  }
  void init(int n_){
    n=1;
    while(n<n_)n*=2;
    for(int i=0;i<2*n-1;i++)
      dat[i]=laz[i]=0;
  }
  inline void eval(int len,int k){
    if(k*2-1<n*2-1){
      laz[k*2+1]+=laz[k];
      laz[k*2+2]+=laz[k];
    }
    dat[k]+=laz[k]*len;
    laz[k]=0;
  }
  int update(int a,int b,int x,int k,int l,int r){
      eval(r-l,k);
      if(r<=a||b<=l) return dat[k]+laz[k]*(r-l);
      if(a<=l&&r<=b) return dat[k]+(laz[k]+=x)*(r-l);
      eval(r-l,k);
      return dat[k]=update(a,b,x,k*2+1,l,(l+r)/2)+update(a,b,x,k*2+2,(l+r)/2,r);
     
  }
  int query(int a,int b,int k,int l,int r){
    eval(r-l,k);
    if(r<=a||b<=l) return 0;
    if(a<=l&&r<=b) return dat[k];
    int vl=query(a,b,k*2+1,l,(l+r)/2);
    int vr=query(a,b,k*2+2,(l+r)/2,r);
    return vl+vr;
  }
};


int rec_w(int k){
  int sum=1;
  int maw=0;
  int man;
  for(int i=0;i<G[k].size();i++){
    int w=rec_w(G[k][i]);
    if(maw<w)maw=w,man=G[k][i];
    sum+=w;
  }
  if(sum==1)return sum;
  wpath[man]=1;
  gw[k]=man;
  return sum;
}

int used[100010];
void nab(int k){
  if(used[k])return;
  used[k]=1;
  static int s=0;
  n_dfs[k]=s;
  dfs_n[s++]=k;
  if(wpath[k]){
    par[n_dfs[k]]=par[n_dfs[p[k]]];
  }
  nab(gw[k]);
  for(int i=0;i<G[k].size();i++){
    if(G[k][i]==gw[k])continue;
    nab(G[k][i]);
  }
}
Lazseg ls(100010);
signed main(){
  p[0]=-1;
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    par[i]=i;
    int k;
    cin>>k;
    for(int j=0;j<k;j++){
      int c;
      cin>>c;
      G[i].push_back(c);
      p[c]=i;
    }
  }
  rec_w(0);
  nab(0);
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int a;
    cin>>a;
    if(a){
      int u;
      cin>>u;
      u=n_dfs[u];
      int sum=0;
      while(u>0){
	sum+=ls.query(par[u]-1,u,0,0,ls.n);
	u=n_dfs[p[dfs_n[par[u]]]];
      }
      cout<<sum<<endl;
    }else{
      int v,w;
      cin>>v>>w;
      v=n_dfs[v];
      while(v>0){
	ls.update(par[v]-1,v,w,0,0,ls.n);
	v=n_dfs[p[dfs_n[par[v]]]];
      }
    }
  }
  return 0;
}



Measured memory_bytes: 24380
#include<stdio.h>
#include<math.h>
int main(){
	int n,a,f;
	double ax,ay,ar,bx,by,br;
	double rp,rm,atob;
	scanf("%d",&n);
	for(a=0;a<n;a++){
		scanf("%lf %lf %lf",&ax,&ay,&ar);
		scanf("%lf %lf %lf",&bx,&by,&br);
		atob=sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
		rp=ar+br;
		if(ar>br){rm=ar-br;}
		else{rm=br-ar;}
		if(atob>rp){f=0;}
		else if(atob<rm){
			if(ar>br){f=2;}
			else{f=-2;}
		}
		else{f=1;}
		printf("%d\n",f);
	}
	return 0;
}

Measured memory_bytes: 0
from collections import defaultdict as dd
N, u, v = map(int, input().split())
es = dd(dict)
for _ in range(N-1):
    a, b = map(int, input().split())
    es[a-1][b-1] = es[b-1][a-1] = 1

d_ao = [None] * N
q = set([v-1])
dist = 0
dist_taka_ao = None
while q:
    nq = set()
    for node in q:
        d_ao[node] = dist
        if node == v-1:
            dist_taka_ao = dist
        for child in es[node].keys():
            if d_ao[child] is None:
                nq.add(child)
    dist += 1
    q = nq

d_taka = [None] * N
q = set([u-1])
dist = 0
ans = 0
while q:
    nq = set()
    for node in q:
        if d_ao[node] <= dist:
            continue
        d_taka[node] = dist
        ans = ans if ans > d_ao[node] else d_ao[node]
        for child in es[node].keys():
            if d_taka[child] is None:
                nq.add(child)
    dist += 1
    q = nq

#if dist_taka_ao % 2 == 1:
#    ans += 1
print(ans-1)

Measured memory_bytes: 54980
using System;
using System.Linq;
using System.Collections.Generic;

static class NagataKanaIsAGenius {
    public static void Main() {
        int n=int.Parse(Console.ReadLine());
        String s=Console.ReadLine();
        String t=Console.ReadLine();
        int x=0;
        for(int i=1;i<=n;i++){
            bool ng=false;
            for(int j=0;j<i;j++){
                if(s[n-i+j]!=t[j])ng=true;
            }
            if(!ng)x=i;
        }
        Console.WriteLine(2*n-x);
    }
}


Measured memory_bytes: 13140
#include <iostream>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pl;
typedef vector <ll> vl;
typedef set <ll> sl;
typedef vector <pair <ll, ll>> vll;
typedef map <string, int> msi;


int main() {
	ios::sync_with_stdio(false);

	int a, b, c;
	cin >> a >> b >> c;
	if ((10 * b + c) % 4 == 0)
		cout << "YES";
	else
		cout << "NO";


	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include<string>
#include <cstdio>
#include<utility>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<bitset>
#include<queue>
#include<set>
#include <limits>
#include <numeric>
#include <type_traits>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
typedef pair<double,double>P_D;
#define PI 3.1415926535897932384626433832795
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define reprev(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define ALL(a)  (a).begin(),(a).end()
#define MAX 100100
#define MAX_N 200100
#define SENTINEL 2000000000
#define MAX_B 1001001
#define NIL -1
#define MOD 1000000007
#define INF 1 << 29
#define INFTY 1000000000000000000LL
int main(){
    int n,m;cin>>n>>m;
    vector<P>p(m);
    rep(i,m){
        int a,b;cin>>a>>b;
        a--;b-=2;//橋の番号として記録する
        p[i]=make_pair(b,a);
    }
    sort(ALL(p));
    int ans=0,end=-SENTINEL;
    rep(i,m){
        if(end<p[i].second){
            ans++;
            end=p[i].first;
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 1024
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define PB push_back
#define MP make_pair
#define all(x) x.begin(),x.end()

#define MOD 1000000007

int N;
vector<int> edge[100000];
vector<int> child[100000];
void dfs(int now, int par){
	for(int i=0; i<edge[now].size(); i++){
		if(edge[now][i] == par) continue;
		child[now].PB(edge[now][i]);
		dfs(edge[now][i], now);
	}
}

LL dp[100000][2];
LL solve(int now, int last){
	if(child[now].size() == 0){
		if(last == 0) return 2LL;
		else return 1LL;
	}
	if(dp[now][last] != -1) return dp[now][last];
	LL ans = 0;
	if(last == 0){ //親が白、自分を黒に
		LL tmp = 1;
		for(int i=0; i<child[now].size(); i++){
			tmp *= solve(child[now][i], 1);
			tmp %= MOD;
		}
		ans += tmp;
	}
	LL tmp = 1;
	for(int i=0; i<child[now].size(); i++){
		tmp *= solve(child[now][i], 0);
		tmp %= MOD;
	}
	ans += tmp;
	return dp[now][last] = ans%MOD;
}

int main(){
	cin >> N;
	for(int i=0; i<N-1; i++){
		int x,y;
		cin >> x >> y;
		edge[x-1].PB(y-1);
		edge[y-1].PB(x-1);
	}
	dfs(0,-1);

	for(int i=0; i<N; i++){
		dp[i][0] = -1;
		dp[i][1] = -1;
	}
	cout << solve(0, 0) << endl;
	
	return 0;
}



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

res=n
for i in range(n+1):
    count=0
    j=n-i
    while i>0:
        count+=i%6
        i//=6
    while j>0:
        count+=j%9
        j//=9
    if res>count:
        res=count

print(int(res))

Measured memory_bytes: 3060
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<sstream>
#include<algorithm>
#include<stack>
#include<bitset>
#include<map>
#include<set>
#define rep(i,n) for(int i=0;(i)<(n);i++)
#define rep1(i,n) for(int i=1;(i)<=(n);i++)
#define mst(a,b) memset(a,b,sizeof(a))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define dbg(a) cout<<"* "<< #a <<" : "<<a<<endl
#define fr first
#define se second
#define ls x<<1
#define rs x<<1|1
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define lowbit(x) x&(-x)
#define ac cout<<ans<<endl
//#define DEBUG 0
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-8;
const ull hashp=131;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const int maxn=1e5+100;
ll sum[5][maxn];
map<char,int>mp;
ll get(ll n){
	return n*(n-1)*(n-2)/6;
}
ll cnt(ll n){
	return n*(n-1)/2;
}
char s[maxn];
void solve(){
	int n;scd(n);
	scanf("%s",s+1);
	ll ans=get(n);
	mp['R']=1;mp['G']=2;mp['B']=3;
	rep1(i,n){
		rep1(j,3)	
			sum[j][i]=sum[j][i-1];
		sum[mp[s[i]]][i]++;
	}
	rep1(i,3){
		ans-=get(sum[i][n]);
		ans-=cnt(sum[i][n])*(n-sum[i][n]);
	}
	rep1(j,n)rep1(i,j-1)
		if(s[i]!=s[j])
			if(j*2-i<=n&&s[j*2-i]!=s[i]&&s[j]!=s[j*2-i])
				ans--;ac;
}
int main(){
    //IOS;
    //freopen("fenwick.in","r",stdin);
    //freopen("fenwick.out","w",stdout);
    solve();
    
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;;                                             ;;
	;;         ;;            ;;;;;;;;;       ;;    ;;
	;;        ;; ;;        ;;                ;;    ;;
	;;       ;;   ;;       ;;                ;;    ;;
	;;      ;;;;;;;;;      ;;                ;;    ;;
	;;     ;;       ;;     ;;                      ;;
    ;;    ;;         ;;      ;;;;;;;;;       ;;    ;;
    ;;                                             ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    return 0;
}

Measured memory_bytes: 3344
#include<bits/stdc++.h>
using namespace std;
int i,j,n,a[100],b[100],c,d,h,w,k,l;
char s[100][100];
int main(){
  for(cin>>h>>w;i<h;i++)for(j=0;j<w;j++)cin>>s[i][j];
  for(i=0;i<h;i++){
    for(c=0,j=0;j<w;j++){
      if(s[i][j]=='.')c++;
    }
    if(c==w){
      for(k=i;k<h;k++){
        for(l=0;l<w;l++){
          s[k][l]=s[k+1][l];
        }
      }
      i--;
      h--;
    }
  }

  for(i=0;i<w;i++){
    for(c=0,j=0;j<h;j++){
      if(s[j][i]=='.')c++;
    }
    if(c==h){
      for(k=i;k<w;k++){
        for(l=0;l<h;l++){
          s[l][k]=s[l][k+1];
        }
      }
      i--;
      w--;
    }
  }

  for(i=0;i<h;i++){
    for(j=0;j<w;j++)cout<<s[i][j];
    cout<<endl;
  }

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
// 型エイリアス
using pii = pair<int, int>;
using tisb = tuple<int, string, bool>;
using vi = vector<int>;
using vii = vector<vi>;

int gcd(int x, int y){
  int r;
  if(x<y) swap(x, y);
  while(y>0){
    r = x%y;
    x = y;
    y = r;
  }

  return x;
}

int main(){
  int x, y; cin >> x >> y;
  int ans = gcd(x, y);

  cout  << ans << endl;
}


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

template<class T> struct LowestCommonAncestor {
    using Graph = vector<vector<pair<int, T>>>;
    const T t_inf = numeric_limits<T>::max();
    pair<T, int> id = make_pair(t_inf, INT_MAX);
    int n;
    vector<pair<T, int>> data;
    vector<int> idx;
    
    LowestCommonAncestor(Graph G, int s = 0) : idx(G.size(), -1), n(1) {
        vector<pair<T, int>> p;
        EulerTour(G, s, s, 0, p);
        int sz = p.size();
        for (int i = sz-1; i >= 0; i--) idx[p[i].second] = i;
        while (n < sz) { n *= 2; }
        data.resize(2*n-1, id);
        for (int i = 0; i < sz; i++) data[i+n-1] = p[i];
        for (int i = n-2; i >= 0; i--) data[i] = min(data[i*2+1], data[i*2+2]);
    }
    void EulerTour(const Graph &G, int now, int par, T dep, vector<pair<T, int>> &path) {
        path.emplace_back(dep, now);
        for (auto nxt : G[now]) if (nxt.first != par) {
            EulerTour(G, nxt.first, now, dep+nxt.second, path);
            path.emplace_back(dep, now);
        }
    }
    
    pair<T, int> query (int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return id;
        if (a <= l && r <= b) return data[k];
        pair<T, int> vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
        pair<T, int> vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
    
    int ancestor (int a, int b) {
        int l = min(idx[a], idx[b]), r = max(idx[a], idx[b]);
        return query(l, r + 1).second;
    }
    
    T distance(int a, int b) {
        int l = min(idx[a], idx[b]), r = max(idx[a], idx[b]);
        T an = query(l, r + 1).first;
        T d1 = data[idx[a]+n-1].first, d2 = data[idx[b]+n-1].first;
        return (d1 + d2 - an*2);
    }
};

int main() {
    int n, q;
    cin >> n;
    vector<vector<pair<int, int>>> G(n);
    for (int i = 0; i < n; i++) {
        int k;
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            int c;
            scanf("%d", &c);
            G[i].emplace_back(c, 1);
        }
    }
    
    LowestCommonAncestor<int> lca(G, 0);
    
    cin >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        int anc = lca.ancestor(u, v);
        printf("%d\n", anc);
    }
    return 0;
}


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

using namespace std;
using ll = int64_t;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using mii = map<int, int>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using tpi = tuple<int, int, int>;

using vqi = vector<queue<int>>;
using vpi = vector<pii>;
using bs = bitset<60>;
using vbi = vector<bs>;

ll gcd(ll a, ll b) {
    if (a < b) {
        swap(a, b);
    }

    /* ユークリッドの互除法 */
    ll r = a % b;
    while (r != 0) {
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

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

constexpr int64_t __pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
    if (p == 0) {
        return 1;
    }
    if (p % 2 == 0) {
        int64_t t = __pow_mod(n, p / 2, mod);
        return (t * t) % mod;
    }
    return (n * (__pow_mod(n, p - 1, mod) % mod)) % mod;
}

constexpr int64_t pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
    int64_t res = __pow_mod(n, p, mod);
    if (res == 0) {
        res = mod;
    }
    return res;
}

void solve(long long N, long long K) {
    double ans = log(N) / log(K);
    cout << max(1, static_cast<int>(ceil(ans))) << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
    long long N;
    std::cin >> N;
    long long K;
    std::cin >> K;
    solve(N, K);
    return 0;
}


Measured memory_bytes: 512
#include <sstream>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<ctime>
#include<cmath>
#include<map>
#include<unordered_map>
#include<stack>
#include<random>
#include<queue>
#include<list>
#include<math.h>
#include<bitset>
#include<fstream>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld,ld>
#define ii pair<int,int>
#define si set<int>
#define vii vector<pair<int,int> >
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int> >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mul(x,y) ((ll)(x)*(y))%mod
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
#include<fstream>
const ll INF=9223372036854775807;
const ll mod=1000000007;
int  MOD(int  a, int  b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
ll  max3(ll  a,ll  b, ll  c)
{
    return max(c,max(a,b));
}
ll min3(ll  a,ll  b,ll  c)
{
    return min(a,min(b,c));
}
ll  power(ll  x,ll  y,ll mod1)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%mod1;
        y = y>>1;
        x = (x*x)%mod1;
    }
    return res;
}
ll logg(ll  a)
{
    ll  x=0;
    while(a>1)
    {
        x++;
        a/=2;
    }
    return x;
}
ll  gcd(ll  a,ll   b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
ll abso(ll x)
{
    if(x<0)
    {
        return -x;
    }
    return x;
}
ll ceiling (ll  a,ll  b)
{
    if(a%b==0)
    {
        return a/b;
    }
    else
    {
        return a/b+1;
    }
}
ll modinv(ll x,ll p)
{
    return power(x,p-2,p);
}
int main()
{
  fastio;
    string str;
    cin>>str;
    int n=str.length();
    vector<vll> dp(n+1);
    for(int i=0;i<n+1;i++)
    {
        dp[i].resize(13,0);
    }
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        if(str[i-1]=='?')
        {
            for(int j=0;j<13;j++)
            {
                for(int k=0;k<10;k++)
                {
                    dp[i][(j*10+k)%13]=(dp[i][(j*10+k)%13]+dp[i-1][j])%mod;
                }
            }
        }
        else
        {
            for(int j=0;j<13;j++)
            {
                dp[i][(j*10+(str[i-1]-'0'))%13]=(dp[i][(j*10+(str[i-1]-'0'))%13]+dp[i-1][j])%mod;
            }
        }
    }
    cout<<dp[n][5];
}


Measured memory_bytes: 13648
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define IOS ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define pf push_front
#define PI acos(-1)
#define EPS 1e-5
#define sz(v) ((int)(v).size())
#define all(v) v.begin(),v.end()
#define forn(i, n) for(ll i=0;i<(ll)n;i++)
#define fi first
#define se second
 

int main() {
	int n;
	cin >> n;
	vector<pii> c(n);
	forn(i, n) {
		int x, y; 
		cin >> x >> y;
		c[i] = {x, y};
	}
	double dist = 0;
	double cnt = 0;
	sort(all(c));
	do {
		for(int i = 0; i+1 < n; i++) {
			dist += sqrt((c[i].fi-c[i+1].fi)*(c[i].fi-c[i+1].fi)+(c[i].se-c[i+1].se)* (c[i].se-c[i+1].se));
		}
		cnt++;
	} while(next_permutation(all(c)));
	double res = dist / cnt;
	cout << fixed << setprecision(10) << res << endl;
	return 0;
}



Measured memory_bytes: 3792
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define SPC << " " <<
#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;

int main() {
	//freopen("test.in","r",stdin);
	int n,d;
	scanf("%d %d",&n,&d);
	int st=0;
	REP (i,n) {
		int x,y;
		scanf("%d %d",&x,&y);
		if ((int64)x*x+(int64)y*y<=(int64)d*d) st++;
	}
	printf("%d\n",st);
	return 0;
}


Measured memory_bytes: 3856
#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <string.h>
#include <numeric>
using namespace std;

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) rep(i,(n)+1)
#define repst(i,s,n) reps(i,s,(n)+1)
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(i,v) for(auto &(i):(v))
#define eachr(i,v) for(auto &(i)=(v).rbegin();(i)!=(v).rend();(i)++)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (((i)==(n)-1)?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln<<flush
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ')<<flush;}}
#define zero(a) memset(a,0,sizeof(a))
#define unq(a) sort(all(a)),a.erase(unique(all(a)),a.end())

//typedef complex<double> P;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

ll mop(ll a,ll b,ll m=mod) {ll r=1;a%=m;for(;b;b>>=1){if(b&1)r=r*a%m;a=a*a%m;}return r;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
bool ool(int x,int y,int h,int w) {return((x<0)||(h<=x)||(y<0)||(w<=y));}
bool deq(double a,double b) {return abs(a-b)<eps;}

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;



string s;
int n;

signed main() {
	cin >> s;
	n = s.size();
	rep(i,n-1) if (s[i]==s[i+1]) return cout << i+1 << ' ' << i+2 << endl, 0;
	rep(i,n-2) if (s[i]==s[i+1] || s[i+1]==s[i+2] || s[i+2]==s[i])
		return cout << i+1 << ' ' << i+3 << endl, 0;
	cout << "-1 -1" << endl;
}

Measured memory_bytes: 512
#!/usr/bin/env python
# coding: utf-8

# In[16]:


S = input()
T = input()


# In[20]:


mydict = {}
for i in range(len(S)):
    if S[i] in mydict:
        if T[i] != mydict[S[i]]:
            ans = "No"
            break
    else:
        mydict[S[i]] = T[i]
else:
    a = list(mydict.values())
    b = list(set(a))
    if len(b) != len(a):
        ans = "No"
    else:
        ans = "Yes"
print(ans)


# In[ ]:






Measured memory_bytes: 9356
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        string s = Console.ReadLine();
        int cnt = 0;
        char b = '\0';
       
        for (int i = 0; i < s.Length; i++)
        {
            if(b != s[i])
            {
                cnt++;
                b = s[i];
            }
        }

        Console.WriteLine(cnt);
    }
}


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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }


const string YES = "Yes";
const string NO = "No";

void func(std::string S){

    sort(S.begin(),S.end());
    if(S=="abc")cout<<YES<<endl;
    else cout<<NO<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    std::string S;
    std::cin >> S;
    func(S);
    return 0;
}


Measured memory_bytes: 256
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>
#include <iomanip>

using namespace std;

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
    int n, k;
    cin >> n >> k;
    
    if(k == 1)
        cout << 0 << endl;
    else
        cout << n-k << endl;

    return 0;
}

Measured memory_bytes: 256
a = []
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
h, w = map(int, input().split())
ans = [["#"] * w for _ in range(h)]

for _ in range(h):
    a.append(input())

for k in range(h):
    for j in range(w):
        if a[k][j] == ".":
            count = 0
            for i in range(8):
                nx = j + dx[i]
                ny = k + dy[i]

                if nx < 0 or ny < 0 or nx >= w or ny >= h:
                    continue
                if a[ny][nx] == "#":
                    count += 1
            ans[k][j] = count

for s in ans:
    print(*s, sep="")

Measured memory_bytes: 9292
#include <stdio.h>
typedef long long ll;
#define rep(i,n) for(ll i=0; i<(ll)(n); i++)
#define max(l,r) ((l)>(r)?l:r)

ll dp[3030][3030][5] = {0};
int mp[3030][3030] = {0};
int main(){
    int R, C, K;
    scanf("%d%d%d", &R, &C, &K);
    rep(i, K) {
        int x, y, v;
        scanf("%d%d%d", &x, &y, &v);
        mp[x][y] = v;
    }

    rep(i, R) {
        rep(j, C) {
            rep(k, 4) {
                if(mp[i+1][j+1] > 0) {
                    if(k > 0) dp[i+1][j+1][k] = max(dp[i+1][j+1][k], dp[i+1][j][k-1] + mp[i+1][j+1]);
                    if(i > 0) dp[i+1][j+1][1] = max(dp[i+1][j+1][1], dp[i][j+1][k] + mp[i+1][j+1]);
                }
                dp[i+1][j+1][k] = max(dp[i+1][j+1][k], dp[i+1][j][k]);
                if(i > 0) dp[i+1][j+1][0] = max(dp[i+1][j+1][0], dp[i][j+1][k]);
                
            }
        }
    }

    ll ans = 0;
    rep(i, 4) ans = max(ans, dp[R][C][i]);
    printf("%lld", ans);
}

Measured memory_bytes: 392236
import java.util.*;

public class Main {
    private static int[] roots;
    
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        UnionFind uf = new UnionFind(n);
        
        while (m-- > 0) {
            int x = sc.nextInt() - 1;
            int y = sc.nextInt() - 1;
            sc.nextInt();
            uf.unite(x, y);
        }
        
        System.out.println(uf.connectedComponent);
    }
    
    public static class UnionFind {
        int[] par;
        int connectedComponent;
    
        UnionFind(int n) {
            par = new int[n];
            connectedComponent = n;
            for (int i = 0; i < n; i++) {
                par[i] = i;
            }
        }
    
        int root(int x) {
            if (par[x] == x) return x;
            return par[x] = root(par[x]);
        }
    
        void unite(int x, int y) {
            int rx = root(x);
            int ry = root(y);
            if (rx != ry) {
                par[ry] = rx;
                connectedComponent--;
            }
        }
    
        boolean same(int x, int y) {
            int rx = root(x);
            int ry = root(y);
            return rx == ry;
        }
        
        void printPar() {
            System.out.println(Arrays.toString(par));
        }
    }
}

Measured memory_bytes: 92084
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
  int n;
  int tmp;
  long int a[100001]={0};
  long int sum[100001]={0};
  int q;
  int b,c;

  scanf("%d",&n);
  for(int i=0;i<n;i++)
  {
    scanf("%d",&tmp);
    a[tmp]++;
    sum[0]+=tmp;
  }
  scanf("%d",&q);
  for(int i=0;i<q;i++)
  {
    scanf("%d %d",&b,&c);
    sum[i+1] = sum[i] + (c-b)*a[b];
    a[c]+=a[b];
    a[b]=0;
  }

  for(int i=0;i<q;i++)
  {
    printf("%ld\n",sum[i+1]);
  }

  return 0;
}

Measured memory_bytes: 3296
n, m = [int(i) for i in input().split() ]
cost = list([])
for i in range(n):
    cost.append([int(i) for i in input().split()])

cost.sort(key=lambda x:x[0])
count = 0
price = 0
for c in cost:
    if count >= m:
        break
    if count + c[1] <= m:
       price += c[0] * c[1]
       count += c[1]
    else:
       price += c[0] * (m-count)
       count = m
print(price)

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

int main() {
    int n,k; cin >> n >> k;
    string s; cin >> s;
    for(int i = 0; i < n; i ++) {
        if(i==k-1) {
            if(s[i]=='A') s[i] = 'a';
            if(s[i]=='B') s[i] = 'b';
            if(s[i]=='C') s[i] = 'c';
        }
    }
    cout << s << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <fstream>
#include <cmath>
#include <random>
#include <complex>
#include <functional>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template<class T> inline void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}
template<class T> inline void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}

vector<ll> s;

void dfs(ll a) {
    if (a > 1e12) return;
    ll b = a % 10;
    if (b == 0) {
        s.push_back(a*10);
        dfs(a*10);
        s.push_back(a*10 + 1);
        dfs(a*10 + 1);
    } else if (b < 9) {
        s.push_back(a*10 + b - 1);
        s.push_back(a*10 + b);
        s.push_back(a*10 + b + 1);
        dfs(a*10 + b - 1);
        dfs(a*10 + b);
        dfs(a*10 + b + 1);
    } else {
        s.push_back(a*10 + 8);
        s.push_back(a*10 + 9);
        dfs(a*10 + 8);
        dfs(a*10 + 9);
    }
}

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

    ll K;
    cin >> K;

    for (ll i = 1; i <= 9; i++) {
        s.push_back(i);
        dfs(i);
    }

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

    Rep (i, 100) {
        // cout << s[i] << " \n"[i==99];
    }

    cout << s[K-1] << endl;

}

Measured memory_bytes: 67936
#include <iostream>
#include <sstream>
#include <stack>
#include <iomanip>
#include <string>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <queue>
#include <cmath>

#define BEGIN(x) x.begin()
#define END(x) x.end()
#define ALL(x) BEGIN(x), END(x)
#define PAIR make_pair
#define VEC(type) vector<type >

#define FOR(i, A, B) for(auto i = (A);i != (B); i++)
#define FORD(i, A, B) for(auto i = (A);i != (B); i--)

#define READRANGE(begin, end) FOR(it, begin, end) cin >> *it
#define READVEC(V) READRANGE(BEGIN(V), END(V))

using namespace std;

typedef long long lint;
typedef pair<int, int> Pii;
typedef pair<int, lint> Pil;
typedef pair<lint, lint> Pll;
typedef pair<lint, int> Pli;

// ---- BEGIN LIBRARY CODE ----
// ---- END LIBRARY CODE ----
void io_init()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
};

int main(int argc, char **argv)
{
    io_init();

    // Actual program code starts here.
    int n;
    string s;
    int k;
    cin >> n >> s >> k;
    FOR(i, 0, n)
        if (s[k - 1] != s[i])
            s[i] = '*';
    cout << s << endl;
    return 0;
};


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#define PI 3.141592653589793
using namespace std;
int main(){
	int n;
	string str;
	cin>>n;
	for(int i=0;i<n;i++){
		int cnt=0,num=0;
		int rui[5]={0};
		while(cin>>str){
			if(str=="HIT"){
				rui[4]=rui[3];
				rui[3]=rui[2];
				rui[2]=rui[1];
				rui[1]=1;
				if(rui[4]==1){
					rui[4]=0;
					cnt++;
				}
			}else if(str=="HOMERUN"){
				for(int j=1;j<=3;j++){
					if(rui[j]==1){
						rui[j]=0;
						cnt++;
					}
				}
				cnt++;
			}else{
				num++;
				if(num==3)break;
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}

Measured memory_bytes: 3112
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1<<30;
ll fact(ll num){
  if(num==1) return 1;
  else return ll(num*(fact(num-1)%ll(1e9+7)))%ll(1e9+7);
}
int main(){
  ll n, m;cin>>n>>m;
  if(n>m)swap(n, m);
  if(m-n>1)cout<<0<<endl;
  else if(n==m)cout<<(((fact(n)*fact(n))%ll(1e9+7))*2)%ll(1e9+7)<<endl;
  else cout<<(((fact(n)*fact(n))%ll(1e9+7))*m)%ll(1e9+7)<<endl;
}

Measured memory_bytes: 1792
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		while(scan.hasNext()){
			int a = scan.nextInt();
			List<Integer> l = new ArrayList<Integer>();
			
			for(int i = 0; i < 10; i++){
				
				if((a & 1 << i) != 0)l.add(1 << i);
			}
			boolean h = true;
			for(int i:l){
				if(!h)System.out.print(" ");
				h = false;
				System.out.print(i);
			}
			System.out.println();
		}
	}
}

Measured memory_bytes: 25948
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
using namespace std;
int main(){
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n;
  long long a;
  cin>>n;
  if(n%2!=0){
    a=n*2;
  }
  else{
    a=n;
  }
  cout<<a<<"\n";
  return 0;
}

Measured memory_bytes: 3656
#include<stdio.h>
#include<string.h>
int main()
{
    char s[30];
    int a[30],i;
    for(i=0;i<30;i++)
    {
        a[i]=0;
    }
    gets(s);
    int len=strlen(s);
    int loop,flag=0;
    for(i=0;i<len;i++)
    {
       a[s[i]-97]++;
    }
    for(i=0;i<len;i++)
    {
        if(a[i]>=2)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        printf("yes\n");
    }
    else
    {
        printf("no\n");
    }
    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a=0,b,c,d;
  string s;
  cin >>a>>b>>c;
  c-=a;
  if(c<=b&&c>=0) cout <<"YES\n";
  else cout <<"NO\n";
  return (0);
}


Measured memory_bytes: 892
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();

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

        List<Long> fac = new ArrayList<>();
        for(int i=1 ; i *i <= sum ; i++){
            if(sum %i ==0){
                fac.add((long)i);
                fac.add(sum/i);
            }
        }

        long ans =1;
        for (int i = 0; i < fac.size(); i++) {
            long x = fac.get(i);
            int need = 0;
            long[] mods = new long[nums.length];
            long diffSum = 0;
            for (int j = 0; j < nums.length; j++) {
                mods[j]= nums[j]%x;
                diffSum+=mods[j];
            }
            Arrays.sort(mods);
            long idx = diffSum/x;
            for (int j = 0; j < mods.length-idx ; j++) {
                need += mods[j];
            }
            if(need <=K){
                ans = Math.max(ans,x);
            }
        }

        System.out.println(ans);
    }
}


Measured memory_bytes: 34508