akhauriyash/Code-Regression huggingface dataset:
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill(0).map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill(0).map(x=>"+r+")";return eval(r);}

console.log(main());
function main(){
  var r = o.a();
  var g = o.a();
  var b = o.a();
  var n = o.a();
  var ans = 0;
  for(var i = 0; i <= n/r; i++){
    for(var j = 0; j <= n/g; j++){
      var t = n-i*r-j*g;
      if(t<0)break;
      if(t/b===(t/b|0))ans++;
    }
  }
  return ans;
}

Measured memory_bytes: 21064
n,m = map(int,input().split())
h = list(map(int,input().split()))
p = [0]*n
for i in range(m):
    a,b = map(int,input().split())
    if h[a-1] >= h[b-1]:
        p[b-1] = -1
    if h[a-1] <= h[b-1]:
        p[a-1] = -1
print(p.count(0))


Measured memory_bytes: 20032
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int n;
int res;
vector<int>v;
int lst=-1;
int match[210];
int me,mo;
int mx;
bool f[10000010];
vector<int>ok[210];
int vis[210];
int prm[1000010];
int sz;
int l;
bool bp(int x,int p)
{
	vis[x]=p;
	for(int i=0;i<ok[x].size();i++)
	{
		int y=ok[x][i];
		if(match[y]==-1 || (vis[match[y]]!=p && bp(match[y],p)))
		{
			match[y]=x;
			match[x]=y;
			return true;
		}
	}
	return false;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(x!=lst+1)
		{
			if(lst+1)v.push_back(lst+1);
			v.push_back(x);
		}
		lst=x;
		mx=lst+1;
	}
	v.push_back(lst+1);
	f[1]=true;
	for(int i=2;i<=mx;i++)
	{
		if(!f[i])prm[++sz]=i;
		for(int j=1;j<=sz;j++)
		{
			if(prm[j]*i>mx)break;
			f[prm[j]*i]=true;
			if(i%prm[j]==0)break;
		}
	}
	l=v.size();
	for(int i=0;i<l;i++)
	{
		match[i]=-1;
		if(v[i]&1)mo++;else me++;
		for(int j=i+1;j<l;j++)
		{
			if(v[j]-v[i]!=2 && !f[v[j]-v[i]])
			{
				ok[i].push_back(j);
				ok[j].push_back(i);
			}
		}
	}
	for(int i=0;i<l;i++)
	{
		if(match[i]>=0)continue;
		memset(vis,-1,sizeof(vis));
		if(bp(i,i))res++;
	}
	printf("%d",res+((me-res)/2+(mo-res)/2)*2+((me-res)%2)*3);
	return 0;
} 

Measured memory_bytes: 14080
/* while(!cin.eof()) */
 
#include<bits/stdc++.h>
using namespace std; 
#define int long long 
#define mod (int)(1e9+7)
#define MAXI (int)(1e17+100)
#define N 1000005



// Driver code to test above functions
int32_t main()
{
    
 
    
     
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, k, s;
    cin >> n >> k >> s;
    
    if(s==1000000000)
    {
        for(int i=0;i<k;i++)
            cout << s << " ";
        for(int i=k;i<n;i++)
            cout << 1 << " ";
    }
    else
    {
        for(int i=0;i<k;i++)
            cout << s << " ";
        for(int i=k;i<n;i++)
            cout << s+1 << " ";
    }

    return 0;
    
} 

Measured memory_bytes: 1280
import java.util.Scanner;
 
public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long N = scanner.nextLong();
    long A = scanner.nextLong();
    long B = scanner.nextLong();
    long answer = A * (N / (A+B));
    answer += Math.min(N % (A+B),A);//小さいほうの値を返す
    /*
    <上と同じ>
    if(N % (A+B) > A){
    	answer = answer + N % (A+B);
    }else{
    	answer = answer + A;
    }
    */
    System.out.println(answer);
  }
}

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

#define int long long

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

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

	bool b[n];
	memset(b, 0, sizeof(b));

	for (int i = 0; i < k; ++i) {
		int d;
		cin >> d;

		for (int j = 0; j < d; ++j) {
			int a;
			cin >> a;
			b[a - 1] = 1;
		}
	}

	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		if (!b[i]) ++cnt;
	}
	cout << cnt;
}


Measured memory_bytes: 3604
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

// 公舎の入居者数
func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	var all = [4][3][]int{}
	for i, x := range all {
		for j := range x {
			all[i][j] = make([]int, 10)
		}
	}
	for i := 0; i < n; i++ {
		sc.Scan()
		ary := strings.Split(sc.Text(), " ")
		b, _ := strconv.Atoi(ary[0])
		f, _ := strconv.Atoi(ary[1])
		r, _ := strconv.Atoi(ary[2])
		v, _ := strconv.Atoi(ary[3])
		all[b-1][f-1][r-1] += v
	}
	s := ""
	for i, x := range all {
		for j, y := range x {
			for k, z := range y {
				if k == 0 {
					s += fmt.Sprintf(" %v ", z)
				} else if k == 9 {
					s += fmt.Sprintf("%v", z)
				} else {
					s += fmt.Sprintf("%v ", z)
				}
			}
			if i == 3 && j == 2 {
			} else {
				s += "\n"
			}
		}
		if i < 3 {
			s += "####################\n"
		}
	}
	fmt.Println(s)
}



Measured memory_bytes: 1132
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#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 equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const ll INF = 1e18;
const long double EPS = 1e-10;

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<vector<int>> ans(h, vector<int>(w));
  int c = 1, res = 0;
  rep(i, h) {
    rep(j, w) {
      ans[i][j] = c;
      res++;
      if (a[c - 1] == res) {
        c++;
        res = 0;
      }
    }
  }

  rep(i, h) {
    if (i & 1) {
      repr(j, w - 1) {
        cout << ans[i][j];
        if (j) cout << " ";
      }
    }
    else {
      rep(j, w) {
        if (j) cout << " ";
        cout << ans[i][j];
      }
    }
    cout << endl;
  }


  
  return 0;
}

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

using namespace std;

const int P=1000000007;
const int N=1000005;

int f[N],tag[N];
int n;

inline void update(int &x,int y){(x+=y)%=P;}

void dp()
{
	f[1]=1;
	for (int i=1,cur=0;i<=n+1;++i)
	{
		update(cur,tag[i]),update(f[i],cur);
		if (i>n) continue;
		if (i==n) update(f[n+1],1ll*f[n]*n%P);
		else
		{
			update(f[i+1],f[i]);//put an one in this blank
			/*put an number x(x>1) in this place and put x ones in the following blanks*/
			if (i+2<=n) update(tag[i+3],f[i]),update(tag[n+1],P-f[i]),update(f[n+1],1ll*(i+1)*f[i]%P);
			else update(f[n+1],1ll*f[i]*(n-1)%P);
			/*put an number x(x>1) in this place and put infinity non-ones in the following blanks*/
			update(f[n+1],1ll*f[i]*(n-1)%P*(n-1)%P);
		}
	}
}

int main()
{
	//freopen("neutral.in","r",stdin),freopen("neutral.out","w",stdout);
	scanf("%d",&n),dp(),printf("%d\n",f[n+1]);
	//fclose(stdin),fclose(stdout);
	return 0;
}

Measured memory_bytes: 8064
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    k = int(input())
    keta = 1
    mod = 7%k
    if k % 7 == 0:
        max_k = 9*k//7
    else:
        max_k = 9*k
    while keta < max_k:
        if mod == 0:
            print(keta)
            break
        mod = (mod*10 + 7)%k
        keta += 1
    else:
        print(-1)


if __name__=='__main__':
    main()

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

int n, a[100000+1], num = 0, c = 0, ans = 0;

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

	sort(a, a+n);

	for (int i = 0; i < n+1; i++) {
		if (num != a[i]) {
			//cout << "num:" << num << " c:" << c << endl;
			if (c > num) {
				ans += c - num;
			}
			else if (c < num) {
				ans += c;
			}
			c = 0;
			num = a[i];
		}
		c++;
	}

	cout << ans << endl;
}

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

int main(){

  long long ans = 0;
  int N, M;

  // typedef pair<long, long> p;
  // vector<long long> A(10010);
  // vector<long long> B(10010);
  // vector< pair<int , int> > pair;
  // pair<int , int> pair;
  // vector< p > pair(10010);

  vector<pair<long, long> > pair;

  cin >> N >> M;
  long long A, B;
  for(int i = 0 ; i < N ; i++){
    cin >> A >> B;
    pair.push_back(make_pair(A, B));
  }

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

  for(int i = 0 ; i < N ; i++){
    // cout << pair[i].first << "\t" << pair[i].second << "\t" << pair[i].first * pair[i].second  <<endl;
    ans += pair[i].first * pair[i].second;
    M -= pair[i].second;
    if(M <= 0){
      // cout << pair[i].first * M << endl;;
      ans += pair[i].first * M;
      break;
    }
  }


  cout << ans << endl;

}

Measured memory_bytes: 2420
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    string s, t;
    cin >> s >> t;
    int ans = 0;
    for (int i = 0; i < s.size(); ++i)
        ans += s[i]!=t[i];
    cout << ans << endl;
}

Measured memory_bytes: 3784
n=int(input())
s=[None]*n
minv=100
for i in range(n):
  s[i]=int(input())
  if minv>s[i]:
    if s[i]%10!=0:
      minv=s[i]
if minv==100:
  print(0)
elif sum(s)%10==0:
  print(sum(s)-minv)
else:
  print(sum(s))



Measured memory_bytes: 3060
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			String line = scanner.next();
			if (line.equals("0"))
				break;
			Pattern pattern = Pattern.compile("^r+$|^g+$|^b+$");
			Deque<String> deque = new ArrayDeque<String>();
			Set<String> set = new HashSet<String>();
			deque.offer(line);
			boolean flag = false;
			int step = 0;
			loop: while (!deque.isEmpty()) {
				int size = deque.size();
				for (int i = 0; i < size; i++) {
					String tmp = deque.pop();
					if (pattern.matcher(tmp).find()) {
						flag = true;
						break loop;
					}
					if (!set.contains(tmp)) {
						char[] chs = tmp.toCharArray();
						for (int j = 0; j < chs.length - 1; j++) {
							char l = chs[j];
							char r = chs[j + 1];
							if (l != r) {
								char[] tmpchs = Arrays.copyOf(chs, chs.length);
								if (l != 'r' && r != 'r')
									tmpchs[j] = tmpchs[j + 1] = 'r';
								else if (l != 'g' && r != 'g')
									tmpchs[j] = tmpchs[j + 1] = 'g';
								else {
									tmpchs[j] = tmpchs[j + 1] = 'b';
								}
								deque.offer(String.valueOf(tmpchs));
							}
						}
					}
					set.add(tmp);
				}
				step++;
			}
			System.out.println(flag ? step : "NA");
		}
	}
}

Measured memory_bytes: 94532
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int INF=1e9+7;

int main(){
  int n;
  cin>>n;
  
  vector<int> A(n);
  rep(i,n) cin>>A[i];
  
  vector<int> L(n+1);
  rep(i,n+1) L[i]=INF;
  
  rep(i,n) *lower_bound(ALL(L),A[i])=A[i];
  
  cout<<lower_bound(ALL(L),INF)-L.begin()<<endl;

  return 0;
}


Measured memory_bytes: 3624
#include <stdio.h>
#define MAX 44

int F[MAX];

int fibonacci(int n){

	int i;

    F[0] = 1;
    F[1] = 1;
    for(i = 2; i < n+2  ; i++) F[i] = F[i-2] + F[i-1];

    return F[n];
}

int main(void){

	int n;

	scanf("%d",&n);
	printf("%d\n",	fibonacci(n));

	return 0;
}

Measured memory_bytes: 596
#include<cstdio>
#include <vector>
#include <map>
#include <cstring>
#include<algorithm>
using namespace std;
long long qw[100000],dp[100000],a,b;
long long solve(long long  k){
    if(dp[k]>0)return dp[k];
    if((k==0&&qw[k]>qw[k+1])||(k==a-1&&qw[k]>qw[k-1])||(qw[k]>qw[k-1]&&qw[k]>qw[k+1])){
        return dp[k]=b-qw[k];
    }
    if(k==0)return dp[k]=solve(1)+b-qw[k];
    if(k==a-1)return dp[k]=solve(a-2)+b-qw[k];
    if(qw[k]<qw[k-1])dp[k]=max(dp[k],solve(k-1)+b-qw[k]);
    if(qw[k]<qw[k+1])dp[k]=max(dp[k],solve(k+1)+b-qw[k]);
}
int main(){
    scanf("%lld %lld",&a,&b);
    for(int i=0;i<a;i++){
        scanf("%lld",&qw[i]);
    }
    for(int i=0;i<a;i++){ int ru=solve(i);}
    printf("%d\n",*max_element(dp,dp+a));
    return 0;
}

Measured memory_bytes: 2584
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<queue>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vc = vector<char>;
using vvi = vector<vector<int> >;
using vvc = vector<vector<char> >;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; //↑→↓←
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w){
  return (y >= 0 && y < h && x >= 0 && x < w);
}

#define INF 1e9
int w, h;
vvc cliff;

void solve(){
  cliff = vvc(h, vc(w));
  vector<pii> starts;
  vector<pii> goals;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cin >> cliff[i][j];
      if(cliff[i][j] == 'S') starts.push_back({i, j});
      if(cliff[i][j] == 'T') goals.push_back({i, j});
    }
  }
  vvvi dp(h, vvi(w, vi(2, INF)));
  for(int i = 0; i < starts.size(); i++){
    dp[starts[i].first][starts[i].second][0] = 0;
    dp[starts[i].first][starts[i].second][1] = 0;
  }
  queue<pair<int, pii>> q;
  vector<pii> r_foot;
  vector<pii> l_foot;
  for(int i = -2; i <= 2; i++){
    for(int j = 1; j <= 3; j++){
      if(i <= (3-j) && i >= (-3+j)) r_foot.push_back({i, j}), l_foot.push_back({i, -j});
    }
  }
  for(pii i: starts) q.push({0, i}), q.push({1, i});
  while(!q.empty()){
    pair<int, pii> now = q.front(); q.pop();
    int lr = now.first, y = now.second.first, x = now.second.second;
    for(int i = 0; i < r_foot.size(); i++){
      int nx_y, nx_x;
      if(lr == 0) nx_y = y + r_foot[i].first, nx_x = x + r_foot[i].second;
      else nx_y = y + l_foot[i].first, nx_x = x + l_foot[i].second;
      if(!is_in_field(nx_y, nx_x, h, w) || cliff[nx_y][nx_x] == 'X') continue;
      int t = (cliff[nx_y][nx_x] == 'T' || cliff[nx_y][nx_x] == 'S') ? 0 : cliff[nx_y][nx_x]-'0';
      if(dp[nx_y][nx_x][(lr+1)%2] > dp[y][x][lr] + t){
        dp[nx_y][nx_x][(lr+1)%2] = dp[y][x][lr] + t;
        q.push({(lr+1)%2, {nx_y, nx_x}});
      }
    }
  }
  int ans = INF;
  for(int i = 0; i < goals.size(); i++){
    ans = min({ans, dp[goals[i].first][goals[i].second][0], dp[goals[i].first][goals[i].second][1]});
  }
  if(ans == INF) cout << -1 << endl;
  else cout << ans << endl;
}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  while(cin >> w >> h && w){
    solve();
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}



Measured memory_bytes: 3224
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int          input;
    vector<int>  array;
    for (int i=0; i<5; ++i) {
        cin >> input;
        array.push_back(input);
    }
    sort(array.begin(), array.end(), greater<int>());
    for (unsigned int i=0; i<5; ++i) {
        cout << array[i];
        cout << (i==4 ? '\n' : ' ');
    }
}
    

Measured memory_bytes: 1216
/*   Abhijith  */
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#define lld long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b)	for(lld i=a;i<=b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define	endl '\n'
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
vector<lld> v(100001,0);
lld n,d;
vector<vector<vector<lld> > > dp(10001,vector<vector<lld> >(100,vector<lld>(2,-1)));
lld cou(lld sz,lld sum,lld edge)
{
    if(dp[sz][sum][edge]!=-1)
       return dp[sz][sum][edge];
    if(sz==0&&sum==0)
    {
        //cout<<sz<<" "<<sum<<" "<<edge<<" "<<"1"<<endl;
        return dp[sz][sum][edge]=1;
    }
    if(sz==0)
    {
        //cout<<sz<<" "<<sum<<" "<<edge<<" "<<"0"<<endl;
        return dp[sz][sum][edge]=0;
    }
    lld lim=9;
    if(edge==1)
        lim = v[n-sz];
    lld ans=0;
    for(lld i=0;i<=lim;i++)
    {
        if(edge==1&&i==lim)
        {
            ans=(ans+cou(sz-1,((sum+i)%d+d)%d,1LL))%mod;
            continue;
        }
        ans=(ans+cou(sz-1,((sum+i)%d+d)%d,0LL))%mod;
    }
   // cout<<sz<<" "<<sum<<" "<<edge<<" "<<ans<<endl;
    return dp[sz][sum][edge]=ans;
}
int main()
{
      ios;
      string k;
      cin>>k;
      cin>>d;
      n=k.size();
      rep(i,0,n-1)
      {
          v[i]=(k[i]-'0');
      }
      lld ans=0;
      rep(i,0,v[0]-1)
         ans=(ans+cou(n-1,i%d,0))%mod;
      ans=(ans+cou(n-1,v[0]%d,1))%mod;
      cout<<(ans-1+mod)%mod<<endl;
    return 0;
}


Measured memory_bytes: 57088
kazu, ashi = map(int, input().split())

kame, tsuru, hantei = 0, 0, 0

for i in range(kazu+1):
    kame = i
    tsuru = kazu - i
    if kame * 4 + tsuru * 2 == ashi:
        hantei = 1
        break

if hantei:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 9028
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>     //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>        //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int mod=1000000007;

vector<int> i2g;          // i2g[i] := アイテム i の所属するグループの番号(find)
vector<vector<int>> g2i;  // g2i[g] := グループ g に所属するアイテムたち

void union_init(int n) {
  i2g.resize(n);
  g2i.resize(n);
  for (int i = 0; i < n; ++i) {
    // 最初はアイテム i はグループ i に所属
    i2g[i] = i;
    g2i[i].assign(1, i);
  }
}

int find(int i){
  return i2g[i];
}

// アイテム ia の所属するグループとアイテム ib の所属するグループを 1 つにする
void merge(int ia, int ib) {
  if(i2g[ia]==i2g[ib])return;
  // ia の所属するグループが ib の所属するグループより小さくならないようにする
  if (g2i[i2g[ia]].size() < g2i[i2g[ib]].size()) {
    swap(ia, ib);
  }
  int ga = i2g[ia], gb = i2g[ib];
  // グループ gb に所属する全てのアイテムをグループ ga に移す
  for (int j : g2i[gb]) i2g[j] = ga;
  g2i[ga].insert(g2i[ga].end(), g2i[gb].begin(), g2i[gb].end());
  g2i[gb].clear();
}

// アイテム ia とアイテム ib は同じグループに所属しているか？
bool is_same_group(int ia, int ib) {
  return i2g[ia] == i2g[ib];
}


int main(){
	int n,m,p[101010]={};
	int x[101001]={},y[101010]={};
	cin>>n>>m;
	union_init(n);

	FOR(i,0,n){cin>>p[i];p[i]--;}
	FOR(i,0,m){
		cin>>x[i]>>y[i];
		merge(x[i]-1,y[i]-1);
	}
	int g[101010]={};
	int c=0;
	FOR(i,0,n){
		if(is_same_group(i,p[i])){
			c++;
			//cerr<<i<<"&"<<p[i];
		}

	}
	cout<<c<<endl;

    return 0;
}


Measured memory_bytes: 11032
// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

int n,ans[1<<15];

void f(int cur,int n,int pre){
	if(n >= 1<<15)return;
	ans[n]++;
	if(cur==4)return;
	rep2(i,pre,182){
		f(cur+1,n+i*i,i);
	}
}

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

	f(0,0,1);
	while(cin>>n && n){
		cout<<ans[n]<<endl;
	}
}

Measured memory_bytes: 1320
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> swi(n);
	rep(i, m) {
		int ki;
		cin >> ki;
		rep(j, ki) {
			int si;
			cin >> si;
			swi[si - 1].push_back(i);
		}
	}

	vector<int> p(m);
	rep(i, m) cin >> p[i];
	int ans = 0;
	for (int bit = 0; bit < (1 << n); bit++) {
		vector<int> cnt(m);
		rep(i, n) {
			if (bit & (1 << i)) {
				rep(j, swi[i].size()) {
					cnt[swi[i][j]]++;
				}
			}
		}
		bool flag = true;
		rep(i, m) {
			if (cnt[i] % 2 != p[i]) flag = false;
		}
		if (flag) ans++;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in()
{
    int x;
    scanf("%d", &x);
    return x;
}
template <typename T>
inline istream &operator>>(istream &i, v(T) & v)
{
    rep(j, sz(v)) i >> v[j];
    return i;
}
template <typename T>
string join(const v(T) & v)
{
    stringstream s;
    rep(i, sz(v)) s << ' ' << v[i];
    return s.str().substr(1);
}
template <typename T>
inline ostream &operator<<(ostream &o, const v(T) & v)
{
    if (sz(v))
        o << join(v);
    return o;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v)
{
    return i >> v.fi >> v.se;
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &o, const pair<T1, T2> &v)
{
    return o << v.fi << "," << v.se;
}
template <typename T>
inline ll suma(const v(T) & a)
{
    ll res(0);
    for (auto &&x : a)
        res += x;
    return res;
}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame        \
    {               \
        puts("-1"); \
        return 0;   \
    }
#define yn           \
    {                \
        puts("YES"); \
    }                \
    else { puts("NO"); }
const int MX = 20005;
typedef pair<int, P> Q;
typedef vector<Q> vq;

ll dp[MX];

int main()
{
    int n;
    scanf("%d", &n);
    vq a(n);
    cin >> a;
    rep(i, n)
    {
        swap(a[i].fi, a[i].se.fi);
        a[i].fi += a[i].se.fi;
    }
    sort(rng(a));
    rep(i, n)
    {
        int s = a[i].fi, w = a[i].se.fi, x = a[i].se.se;
        drep(j, s + 1)
        {
            if (j >= w)
                maxs(dp[j], dp[j - w] + x);
        }
        rep(j, MX - 1) maxs(dp[j + 1], dp[j]);
    }
    ll ans = dp[MX - 1];
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 384
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;


void main() {
    long N;
    scan(N);
    auto ans = N^^3;
    writeln(ans);
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}


Measured memory_bytes: 256
from collections import deque

n=int(input())
l=[[] for _ in range(n)]
cost=[[] for _ in range(n)]
ans=[-1 for _ in range(n)]
ans[0]=0
q=deque([])
q.append(0)
for iii in range(n-1):
  a,b,w=[int(_) for _ in input().split()]
  a-=1
  b-=1
  l[a].append(b)
  cost[a].append(w)
  l[b].append(a)
  cost[b].append(w)
while not q==deque([]):
  v=q.popleft()
  for jjj in range(len(l[v])):
    u = l[v][jjj]
    w = cost[v][jjj]
    if not ans[u]==-1:
      continue
    ans[u]=(ans[v]+w)%2
    q.append(u)
for a in ans:
  print(a)

Measured memory_bytes: 38284
#include<bits/stdc++.h>
#define pi acos(-1)
#define ll long long
#define pii pair<ll,ll>
#define debug(a) cout<<a<<'\n'
#define maxn 100009 /// Still trying to be normal
#define MOD 1000000007
#define F first
#define S second
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define per(i, b, a) for(ll i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
using namespace std;
const ll INF = 1e9 + 15;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    int cont[n+2];
    memset(cont,0,sizeof cont);
    rep(i,0,k){
        int d;
        cin>>d;
        rep(j,0,d){
            int a;
            cin>>a;
            cont[a]++;
        }
    }
    ll ans=0;
    rep(i,1,n+1){
        if(cont[i]==0)ans++;
    }
    cout<<ans<<"\n";
    return 0;   
}

Measured memory_bytes: 3652
#include<bits/stdc++.h>
using namespace std;
int n,k,S[70004],E[70004],a,mo=1e9+7;
long long AN[102][70004];
int main() {
	scanf("%d %d",&n,&k);
	int s=1,e,fo;
	while(s<=n) {
		fo=n/s,e=n/fo;
		S[++a]=s,E[a]=e;
		s=e+1;
	}
	for(int i=1; i<=a; i++) AN[1][i]=E[i];
	for(int i=2; i<=k; i++) {
		for(int j=1; j<=a; j++)AN[i][j]=AN[i-1][a-j+1]*(E[j]-S[j]+1)%mo;
		for(int j=1; j<=a; j++) (AN[i][j]+=AN[i][j-1])%=mo;
	}
	printf("%lld",AN[k][a]);
	return 0;
}

Measured memory_bytes: 55424
while True:
    n, m = map(int,input().split())
    if n==0 and m==0:
        break
    a = [int(input()) for i in range(n)]
    b = [int(input()) for i in range(m)]
    a.sort()
    b.sort()
    c=0
    d=0
    for i in range(len(a)):
        if d==1:
            break
        for j in range(len(b)):
            if sum(a)-a[i]+b[j]==sum(b)+a[i]-b[j]:
                print(a[i],b[j])
                d=1
                break
            else:
                c+=1
    if c==len(a)*len(b):
        print('-1')


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

int main(){
    int n,m;
    cin>>n>>m;
    vector<int>ans(n,-1),s(m),c(m);
    rep(i,m){
        cin>>s[i]>>c[i];
        s[i]--;
    }
    rep(i,m){
        if(ans[s[i]]==c[i]||ans[s[i]]==-1)ans[s[i]]=c[i];
        else{cout<<-1<<endl; return 0;}  
    }
    if(n==1){
        if(ans[0]!=-1)cout<<ans[0]<<endl;
        else cout<<0<<endl;
    }
    if(n==2){
        if(ans[0]==0)cout<<-1<<endl;
        else{
            if(ans[0]==-1)ans[0]=1;
            if(ans[1]==-1)ans[1]=0;
            cout<<ans[0]<<ans[1]<<endl;
        }
    }
    if(n==3){
        if(ans[0]==0)cout<<-1<<endl;
        else{
            if(ans[0]==-1)ans[0]=1;
            if(ans[1]==-1)ans[1]=0;
            if(ans[2]==-1)ans[2]=0;
            cout<<ans[0]<<ans[1]<<ans[2]<<endl;
        }
    }
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
	int s,k,a,b,ans=0;
	cin>>k>>s;
	for(a=0;a<=k;a++)
	for(b=0;b<=k;b++)
	if(s-a-b>=0&&s-a-b<=k)
	ans++;
	cout<<ans<<endl;
}

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

#define rep(i,k,n) for(int i=(k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\n' 

#define input() (*istream_iterator<int>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\n' 

int main(){
    ios::sync_with_stdio(false);
    int a, b, c, k;
    cin >> a >> b >> c >> k;
    if(k > a+b) cout << a - (k-(a+b));
    else if(k == a+b) cout << a;
    else cout << k;
}

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

using lint = long long;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector<int> monster(n+1), heroes(n);
	for (int i = 0; i <= n; ++i) cin >> monster[i];
	for (int i = 0; i < n; ++i) cin >> heroes[i];
	lint total = 0;
    for (int i = 0; i < n; ++i) {
		if (monster[i] - heroes[i] <= 0) {
			total += (lint)monster[i];
			heroes[i] -= monster[i];
			if (heroes[i]) {
				if (monster[i+1] > heroes[i]) {
					total += (lint)heroes[i];
					monster[i+1] -= heroes[i];
					heroes[i] = 0;
				}
				else {
					total += (lint) monster[i+1];
					heroes[i] -= monster[i+1];
					monster[i+1] = 0;
				}
			}
		}
		else {
			total += (lint) heroes[i];
			heroes[i] -= monster[i];
		}
	}
	cout << total << '\n';
}



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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

#define DEBUG(x) cout<<#x<<": "<<(x)<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n'
#define ALL(a) (a).begin(), (a).end()

template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;}
template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;}

constexpr ll MOD=1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

const double EPS=1e-11;
#define EQ0(x) (abs((x))<EPS)
#define EQ(a, b) (abs((a)-(b))<EPS)

struct UnionFind{
	vi par;
	vi rank;

	UnionFind(int n=1){
		init(n);
	}

	// n要素で初期化
	void init(int n=1){
		par.resize(n);
		rank.resize(n);
		REP(i, n){
			par[i]=i;
			rank[i]=0;
		}
	}

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

	// xとyが属する集合を併合
	bool unite(int x, int y){
		x=find(x);
		y=find(y);
		if(x==y){
			return false;
		}
		if(rank[x]<rank[y]){
			par[x]=y;
		}
		else{
			par[y]=x;
			if(rank[x]==rank[y]){
				++rank[x];
			}
		}
		return true;
	}

	// xとyが同じ集合に属するか判定
	bool issame(int x, int y){
		return find(x)==find(y);
	}
};

bool used[114514];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	// cout<<setprecision(10)<<fixed;
	int n, m, x[114514], y[114514], z[114514];
	cin>>n>>m;
	UnionFind uf(n+1);
	REP(i, m){
		cin>>x[i]>>y[i]>>z[i];
		--x[i];
		--y[i];
		uf.unite(x[i], y[i]);
	}
	REP(i, n){
		used[uf.find(i)]=true;
	}
	int cnt=0;
	REP(i, n){
		if(used[i]){
			++cnt;
		}
	}
	cout<<cnt<<'\n';
	return 0;
}


Measured memory_bytes: 2304
#include<bits/stdc++.h>
#define int long long
#define INFINITE 1e18
using namespace std;

const int mod=1e9+7;

int f(int i,int k,int arr[],int l[],int r[])
{
	int x=((k*(k+1))/2)%mod;
	int y=((k*(k-1))/2)%mod;

	return ((x*r[i])%mod + (y*l[i])%mod)%mod;
}

int32_t main()
{
	
	ios::sync_with_stdio(0);

	int n,k;

	cin>>n>>k;
	int arr[n];

	for(int i=0;i<n;i++) cin>>arr[i];

	int r[n]={0};
	int l[n]={0};

	int cnt=0;

	for(int i=0;i<n;i++)
	{
		int val=arr[i];

		for(int j=0;j<i;j++)
		{
			if(arr[j]<val) l[i]++;
		}

		for(int j=n-1;j>i;j--)
		{
			if(arr[j]<val) r[i]++;
		}
	}

	for(int i=0;i<n;i++)
	{
		cerr<<l[i]<<" ";
	}cerr<<endl;


	for(int i=0;i<n;i++)
	{
		cerr<<r[i]<<" ";
	}cerr<<endl;
	

	int ans=0;

	for(int i=0;i<n;i++)
	{
		ans+=f(i,k,arr,l,r)%mod;
		//cerr<<ans<<endl;
	}

	cout<<ans%mod<<endl;

	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#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 SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int main(){

    int n;
    string s;
    cin >> n >> s;

    int ans = 0;

    for(int i = 0;i < s.length();i++){


        vector<int> tmp1_alpha(26,0),tmp2_alpha(26,0);

        string tmp1 = s.substr(0,i+1);
        string tmp2 = s.substr(i+1,6-tmp1.length());
        
        REP(j,tmp1.length()){
            tmp1_alpha[tmp1[j]-'a']++;
        }
        REP(j,tmp2.length()){
            tmp2_alpha[tmp2[j]-'a']++;
        }

        int count = 0;
        REP(j,26){
            if(tmp1_alpha[j] > 0 && tmp2_alpha[j] > 0){
                count++;
            }
        }

        ans = max(ans,count);

    }

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

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

def main():
    N = int(input())
    X = [list(map(int,input().split())) for _ in range(N)]
    dp = [[0,0,0] for _ in range(N)]
    dp[0][0] = X[0][0]
    dp[0][1] = X[0][1]
    dp[0][2] = X[0][2]
    for i in range(N-1):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                else:
                    dp[i+1][k] = max(dp[i+1][k],dp[i][j]+X[i+1][k])

    print(max(dp[-1]))
    
if __name__ == "__main__":
    main()

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

//{ START
using namespace std;
#define int int64_t
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define reps(i, a, n) for (int i = (a); i > (n); --i)
#define arep(i, x) for (auto &&i : (x))
#define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define rirep(i, x) for (auto i = (x).rbegin(); i != (x).rend(); ++i)
//降順はgreater<T>()
#define all(x) (x).begin(), (x).end()
#define rv(s) reverse((s).begin(), (s).end())
// gcd lcmはそのままok
#define gcd(a, b) __gcd(a, b)
#define bits(n) (1LL << (n))
#define pcnt(x) __builtin_popcountll(x)
//配列内等要素削除
#define unique(x) (x).erase(unique((x).begin(), (x).end()), (x).end())
#define Fixed(n) fixed << setprecision(n)
//総和
#define sowa(n) ((n) * ((n) + 1)) / 2
#define pb(x, a) (x).push_back(a)
#define fb(x, a) (x).push_front(a)
#define eb(x, a) (x).emplace_back(a)
#define cauto const auto &
using P = pair<int, int>;
using Graph = vector<vector<P>>;
template <class T>  //昇順
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T>  //降順
using max_heap = priority_queue<T>;
template <class A, class B>
using umap = unordered_map<A, B>;
template <class A>
using uset = unordered_set<A>;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {  //多次元初期化
  std::fill((T *)array, (T *)(array + N), val);
}
template <class A, class B>
bool chmax(A &a, const B &b) {  //最大値更新 返り値はbool
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class A, class B>
bool chmin(A &a, const B &b) {  //最小値更新 返り値はbool
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;
//} END

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  string s;
  cin >> s;
  bool jud = false;
  rep(i, 0, 7) {
    if (s.substr(0, i) + s.substr(s.size() - 7 + i) == "keyence") jud = true;
  }
  cout << (jud ? "YES" : "NO") << '\n';
  return 0;
}

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

/* macro */
#define rep(i,a,b) for(int i=a;i<b;i++)
#define revrep(i,a,b) for(int i = a; i > b; i--)
#define int long long
#define exist(s,e) ((s).find(e)!=(s).end())
#define all(v) (v).begin(), (v).end()
#define each(s,itr) for(auto (itr) = s.begin(); (itr) != s.end(); (itr)++)
#define sum(v) accumulate(all(v), (0LL))
#define isin(a, b, c) (b <= a && a <= c)
#define println cout << "\n";
#define sz(v) (int)v.size()


/* alias */
template<class T> using vec = vector<T>;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef tuple<int, int, int> ti;
typedef map<int, int> mi;
typedef set<int> si;


/* constant */
const int inf = 1LL << 62;
const int mod = 1e9 + 7;
const int dx[8]={1,0,-1,0,-1,1,-1,1};
const int dy[8]={0,1,0,-1,-1,-1,1,1};
const string alphabet = "abcdefghijklmnopqrstuvwxyz";

/* io_method */
int input(){int tmp;cin >> tmp;return tmp;}
string raw_input(){string tmp;cin >> tmp;return tmp;}
template<class T> void printx(T n){cout << n;}
template<class T, class U> void printx(pair<T, U> p){cout << "(" << p.first << "," << p.second << ")";}
template<class T, class U, class V> void printx(tuple<T, U, V> t){cout << "{" << get<0>(t) << "," << get<1>(t) <<"," << get<2>(t) << "}" << endl;}
template<class T> void printx(vector<T> v){cout << "{";rep(i,0,v.size()){printx(v[i]);if(i != v.size()-1)printx(",");}cout << "}";}
template<class T> void print(T n){printx(n);cout << endl;}
template<class T> void print(set<T> s){cout << "{";each(s, e){if(e != s.begin()) printx(",");printx(*e);}cout << "}" << endl;}
template<class T, class U> void print(map<T, U> mp){cout << "{";each(mp, e){cout << "(" << e -> first << "," << e -> second << ")";}cout << "}" << endl;}
template<class T> void printans(vec<T> v){rep(i,0,sz(v)){cout << v[i] << (i == sz(v) - 1 ? "" : " ");}cout << endl;}


/* general_method */
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; }
template<class T>T cut(T &a, int l, int r){return T(a.begin()+l, a.begin()+r);}

/* main */

string s;
void in(){
    cin >> s;
}

void solve(){

    stack<int> stk;
    int ans = 0;
    rep(i,0,sz(s)){
        int t = s[i] - '0';
        if(stk.empty() or stk.top() == t){
            stk.push(t);
        }else{
            stk.pop();
            ans++;
        }
    }

    print(ans * 2);

}

signed main(){

    cin.tie(0);
    ios::sync_with_stdio(false);
    in();
    solve();
    
}

Measured memory_bytes: 1232
#include <iostream>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <string>
#include <functional>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <bitset>
#define pb push_back
#define mp make_pair
#define YES cout<<"YES"<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define No cout<<"No"<<endl
#define INF (1<<30)
#define LLINF (1<<60)
#define MOD 1000000007
#define rep(i, n) for(int i=0;i<n;i++)
using ll = long long;
using namespace std;

int main() {
    int h, w, a[505][505] = {};
    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
        }
    }
    int count = 0;
    vector<pair<int, int>> before, after;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w - 1; j++) {
            if (a[i][j] % 2 != 0) {
                a[i][j]--;
                a[i][j + 1]++;
                count++;
                before.pb(mp(i+1, j+1));
                after.pb(mp(i+1, j+2));
            }
        }
    }
    for (int i = 0; i < h - 1; i++) {
        if (a[i][w - 1] % 2 != 0) {
            a[i][w - 1]--;
            a[i + 1][w - 1]++;
            count++;
            before.pb(mp(i+1, w));
            after.pb(mp(i+2, w));
        }
    }
    cout << count << endl;
    for (int i = 0; i < count; i++) {
        cout << before[i].first << ' ' << before[i].second << ' ' << after[i].first << ' ' << after[i].second << endl;
    }
    return 0;
}


Measured memory_bytes: 5228
/**********************************************************************
   File hoge.cpp
   Author  harunaya
   Date   2020-03-06(Fri)
   note
**********************************************************************/

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, p;
  cin >> a >> p;

  int ans = 0;
  ans = ( a * 3 + p ) / 2;
  cout << ans << endl;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    static int MOD = 1000000007;
    
    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();
        boolean[] ng = new boolean[n + 1];
        for (int i = 0; i < m; i++) {
            ng[sc.nextInt()] = true;
        }
        
        long[] dp = new long[n + 1];
        dp[0] = 1;
        if (!ng[1]) dp[1] = 1;
        
        for (int i = 2; i <= n; i++) {
            if (!ng[i-2]) dp[i] += dp[i-2];
            dp[i] %= MOD;
            if (!ng[i-1]) dp[i] += dp[i-1];
            dp[i] %= MOD;
        }
        
        System.out.println(dp[n]);
    }
}


Measured memory_bytes: 50980
# -*- coding: utf-8 -*-
# B

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

s = input()
t = input()

ans = 0
for i in range(len(s)):
    if s[i] == t[i]:
        pass
    else:
        ans += 1

print(ans)


Measured memory_bytes: 68260
import java.io.*;

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

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    try {
      int num;
      String line;
      line = reader.readLine();
      num = Integer.parseInt(line);
      System.out.println(num*num*num);
    } catch (NumberFormatException e) {
      System.out.println(e);
    } catch (IOException e) {
      System.out.println(e);
    }
  }
}


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

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t,n,i,flag;
    t = 1;
    while(t--) {
     flag = 0;
     cin >> n;
     int arr[n];
     for(i = 0; i < n; i++)
      cin >> arr[i];
     for(i = n - 1; i > 0; i--) {
      if(arr[i] > arr[i - 1])
       arr[i] = arr[i] - 1;
      else if(arr[i] < arr[i - 1])
       arr[i - 1] = arr[i - 1] - 1;
	  if(arr[i] < arr[i - 1]) {
	   flag = 1;
	   break;
	  }
	 }
     if(flag == 0) 
      cout << "Yes" << endl;
     else
      cout << "No" << endl;
    }
    return 0;
}



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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

int main() {
    ll N;
    ll sum = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        ll A;
        cin >> A;
        sum += A;
    }
    cout << sum - N * (N + 1) / 2 << endl;
    return (0);
}



Measured memory_bytes: 3120
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;

alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");

T lread(T = long)()
{
    return readln.chomp.to!T();
}

T[] aryread(T = long)()
{
    return readln.split.to!(T[])();
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}

void main()
{
    auto n = lread();
    auto v = aryread();
    v.sort!"a<b"();
    // v.writeln();
    double ans = v[0];
    foreach (e; v[1 .. $])
        ans = (ans + e) / 2;
    ans.writeln();
}

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

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

int main() {
  long long A,B,cnt;
  cin >> A >> B;
 
  long long n = gcd(A,B);
  
  cnt=1;
  
  for(long long i = 2; i*i <= n; i++){
    if(n%i == 0){
      cnt++;
      while(n%i==0)
        n /= i;
    }
  }
    
  if(n != 1)
    cnt++;
    
  cout << cnt << endl;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int[] ints = new int[n];
		for (int i=0; i<n; i++) {
			ints[i] = sc.nextInt();
		}
		
		int count = 0;
		while (true) {
			boolean flag = false;
			for (int i=0; i<n-1; i++) {
				if (ints[i] > ints[i+1]) {
					int temp = ints[i];
					ints[i] = ints[i+1];
					ints[i+1] = temp;
					flag = true;
					count++;
				}
			}
			if (flag==false) {break;}
		}
		
		for (int i=0; i<n; i++) {
			System.out.print(ints[i]);
			if (i!=n-1) {System.out.print(" ");}
		}
		System.out.println();
		System.out.println(count);
	}
}			


Measured memory_bytes: 26404
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

def main():
    #全体に同じものを足すときは累積和を考えたい
    N,Q = map(int,input().split())
    AB = [list(map(int,input().split())) for _ in range(N-1)]
    PX = [list(map(int,input().split())) for _ in range(Q)]

    graph = [[] for _ in range(N+1)]

    for a,b in AB:
        graph[a].append(b)
        graph[b].append(a)
    #どちらが親か分からないがとりあえずつながっている
    #どちらが親かは後で判断

    counter = [0 for _ in range(N+1)]
    #counter[i]:i番目の頂点のカウンター
    parent = [None for _ in range(N+1)]

    for p,x in PX:
        counter[p] += x

    dfs(graph,parent,counter,1)

    ans = " ".join(map(str,counter[1:]))
    print(ans)

if __name__ == "__main__":
    main()


Measured memory_bytes: 153328