akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n)for(int i=0;i<(int)(n);i++)
 
int main() {

	int n, a;
	int i;
	int cnt = 0, min = 100000000;
	cin >> n;

	for (i = 0; i < n; i++)
	{
		cin >> a;
		while (a % 2 == 0)
		{
			a /= 2;
			cnt++;
		}
		if (min > cnt)
		{
			min = cnt;
		}
		cnt = 0;
	}
	cout << min << endl;

}

Measured memory_bytes: 3620
/** 
If comrades that you trust gather around you, hope can take physical form and become visible. That’s what I believe.
*/
#include <bits/stdc++.h>
using namespace std;
/**
(▀̿Ĺ̯▀̿ ̿) (ง ͠° ͟ل͜ ͡°)ง
*/
const int N = 2e5 + 7;
int f[N];
const int MOD = 1e9 + 7;
int add(int a, int b) {
    a += b;
    if (a < 0) a += MOD;
    return a % MOD;
}
int mul(int a, int b) {
    return (long long) a * b % MOD;
}
int bin_pow(int a, int b) {
    a %= MOD;
    int ret = 1;
    while(b) {
        if (b & 1) ret = mul(ret, a);
        a = mul(a, a);
        b >>= 1;
    }
    return ret;
}
int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    int tt = clock();
    int n, k; cin >> n >> k;
    f[0] = 1;
    f[1] = 1;
    auto inv2 = bin_pow(2, MOD - 2);
    for (int i = 2; i <= n + 1; ++i) f[i] = mul(f[i-1], i);
    int ans = 0;
    for (int i = k; i <= n + 1; ++i) {
        auto ini = mul(mul(i - 1, i), inv2);
        auto last = add(mul(n + 1 - i, i), mul(mul(i - 1, i), inv2));
        // cerr << ini << ' ' << last << "\n";
        ans = add(ans, add(last, -ini));
        ans = add(ans, 1);
        // cerr << i << ' ' << ans << "\n";
    }
    cout << ans << '\n';


    cerr << "TIME = " << (double) 1.0 * (clock() - tt) / CLOCKS_PER_SEC << " seconds" << endl;
	return 0;
}


Measured memory_bytes: 4548
using System;

public class ITP1_9_C{
    public static void Main(){
        var n = int.Parse(Console.ReadLine());
        
        var t_score = 0;
        var h_score = 0;
        for (var i = 0; i < n; i++)
        {
            var cards = Console.ReadLine().Split(' ');
            var t_card = cards[0];
            var h_card = cards[1];
            
            var result = String.Compare(t_card, h_card);
            if (result < 0)
            {
                h_score += 3;
            }
            else if (result == 0)
            {
                t_score++;
                h_score++;
            }
            else
            {
                t_score += 3;
            }
        }
        
        Console.WriteLine($"{t_score} {h_score}");
    }
}


Measured memory_bytes: 18792
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size() 
#define all(v) v.begin(), v.end()
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)


/****************************** CODE IS HERE ***********************************/
int n, m, k;
vector <vector<char>> A;
vector <vector<int>> ans;

int cnt_black(int r, int c) {
    int res = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (A[i][j] == '#' and !(r >> i & 1) and !(c >> j & 1)) {
                res++;
            }
        }
    }
    return (res == k);
}



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

    cin >> n >> m >> k;
    A.assign(n, vector<char> (m));
    ans.assign(1 << (n), vector<int> (1<<(m), 0));
    
    for (int i = 0; i < n; ++i)  {
        for (int j = 0; j < m; ++j) cin >> A[i][j];
    }
    for (int i = 0; i < sz(ans); ++i) {
        for (int j = 0; j < sz(ans[0]); ++j) {
            ans[i][j] = cnt_black(i, j);
        }
    }

    
    int res = 0;
    for (int i = 0; i < sz(ans); ++i) {
        for (int j = 0; j < sz(ans[0]); ++j) {
            res += ans[i][j];
        }
    }

    cout << res;
    return 0;
}


Measured memory_bytes: 3628
#include <iostream>
#include <string>
#include <cmath>
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long int ll;


int main(){
	  int N,M,k=0,MOD=1000000007;
		cin>>N>>M;
    int a[M],ans[N];
    for(int i=0;i<M;i++){
      cin>>a[i];
    }
    for(int i=0;i<N;i++){
			if(a[k]!=i+1){
				if(i==0) ans[i]=1;
				else if(i==1) ans[i]=ans[i-1]+1;
				else ans[i]=(ans[i-1]+ans[i-2])%MOD;
			}

			else {
				ans[i]=0;
				k++;
			}
    }

	  cout<<ans[N-1]<<endl;
}


Measured memory_bytes: 1024
import networkx as nx

N, M = map(int, input().split())
S = [input() for _ in range(N)]
G = nx.MultiDiGraph()
cnt = 0
for s in S:
    cnt += s.count('o')
G.add_node('s', demand=-cnt)
G.add_node('t', demand=cnt)
for i in range(N):
    for j in range(M):
        if S[i][j] == '#':
            continue
        if S[i][j] == 'o':
            G.add_edge('s', M * i + j, weight=0, capacity=1)
        if j < M - 1 and S[i][j+1] != '#':
            G.add_edge(M * i + j, M * i + j + 1, weight=-1, capacity=10000)
        if i < N - 1 and S[i+1][j] != '#':
            G.add_edge(M * i + j, M * (i + 1) + j, weight=-1, capacity=10000)
        G.add_edge(M * i + j, 't', weight=0, capacity=1)
print(-nx.min_cost_flow_cost(G))

Measured memory_bytes: 63492
<?php

$rectangle = 0;
$rhombus = 0;
while (($line = trim(fgets(STDIN))) !== '') {
    sscanf($line, '%d,%d,%d', $a, $b, $c);
    //2辺が等しいので菱型
    if ($a === $b) {
        $rhombus++;
    //三平方の定理が成立するので長方形
    } elseif (pow($a, 2) + pow($b, 2) === pow($c, 2)) {
        $rectangle++;
    }
}

printf("%d\n%d\n", $rectangle, $rhombus);

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

long long n, k, q;
vector<long long> a;

long long solve();

int main() {
  cin >> n >> k >> q;
  a.resize(n);
  for(int i = 0; i < n; ++i) cin >> a[i];
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long ans = (long long)1e16;
  for(int i = 0; i < n; ++i) {
    long long minnum = a[i];
    vector<long long> now;
    for(int id = 0; id < n; ++id) {
      priority_queue<long long, vector<long long>,
                     greater<long long>>
          pq;
      while(id < n && a[id] >= minnum) pq.push(a[id++]);
      while(pq.size() >= k) {
        now.push_back(pq.top());
        pq.pop();
      }
    }
    if(now.size() < q) continue;
    sort(now.begin(), now.end());
    ans = min(ans, now[q - 1] - minnum);
  }
  return ans;
}


Measured memory_bytes: 384
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

#define rep(n) for(int i=0;i<n;i++)
#define repp(j, n) for(int j=0;j<n;j++)
#define reppp(i, m, n) for(int i=m;i<=n;i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define MOD 1000000007
#define MAX 1000000001
#define INF 1410065408
#define EPS 1e-9
#define DEBUG 0
#define ll long long
#define Pll pair<ll, ll>
#define Pii pair<int, int>
 
using namespace std;
struct edge{int from, to; ll cost;};

signed main(){
    ll n, m;
    scanf("%lld %lld", &n, &m);
    if(abs(n-m) > 1){
        printf("%d", 0);
        return 0;
    }
    if(n > m) swap(n, m);
    ll factn = 1, factm;
    reppp(i, 2, n){
        factn *= i;
        factn %= MOD;
    }
    factm = factn;
    reppp(i, n+1, m){
        factm *= i;
        factm %= MOD;
    }
    if(n == m){
        factn *= 2; 
        factn %= MOD;
    }
    printf("%lld", (factn * factm) % MOD);
    return 0;

}

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

using namespace std;

using int64 = long long;

int main()
{
  int N;
  scanf("%d", &N);
  vector< pair< int, int > > D(N);
  for(int i = 0; i < N; i++) {
    scanf("%d %d", &D[i].first, &D[i].second);
  }
  sort(D.begin(), D.end());

  int ret = 0;
  int64 A = 0;
  priority_queue< pair< int, int >, vector< pair< int, int > >, greater< pair< int, int > > > que;
  for(auto &p : D) {
    que.emplace(p.second, p.first);
    A += p.first;
    while(A > 1LL * que.top().first * que.size()) {
      A -= que.top().second;
      que.pop();
    }
    ret = max(ret, (int) que.size());
  }
  
  cout << ret << endl;
}


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

#define int long long
#define REP(i,n) for(int i=0;i<n;i++)
int n,a[100002],d[100002],sum=0,dsum=0;

signed main(){
    cin>>n;
    int N=n*(n+1)/2;
    REP(i,n)cin>>a[i],sum+=a[i];
    if(sum%N){
        cout<<"NO"<<endl;
        return 0;
    }
    REP(i,n)d[i]=a[(i+1)%n]-a[i],dsum+=d[i];
    if(dsum){
        cout<<"NO"<<endl;
        return 0;
    }
    int t=sum/N;
    REP(i,n)d[i]-=t;
    REP(i,n){
        if(d[i]>0||d[i]%n){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}


Measured memory_bytes: 1792
n, k = gets.chomp.split(" ").map(&:to_i)
p = gets.chomp.split(" ").map(&:to_i)

sum = 0
(0..k-1).each do |i|
  sum += p[i]
end
max = sum
if (n>k)
  (0..n-k-1).each do |i|
    sum = sum - p[i] + p[i+k]
    max = sum if max < sum
  end
end
max = (max+k)/2.0
puts max

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

using namespace std;


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

  vector<int> H(N);
  for(int i = 0; i < N; i++){
    cin >> H[i];
  }

  int maxH = H[0];
  int cnt = 1;
  for(int i = 1; i < N; i++){
    if(H[i] >= maxH){
      cnt++;
      maxH = H[i];
    }
  }

  cout << cnt << endl;

  return 0;
}


Measured memory_bytes: 256
s=[]
p=[]
a=i=0
for c in input():
 if"\\"==c:s+=[i]
 elif"/"==c and s:
  j=s.pop()
  t=i-j
  a+=t
  while p and p[-1][0]>j:
   t+=p[-1][1]
   p.pop()
  p+=[(j,t)]
 i+=1
print(a)
if p:print(len(p),*list(zip(*p))[1])
else:print(0)


Measured memory_bytes: 6500
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <limits>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>

using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define MAX_N (100010)

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

	map<int, vector<int>> sushi;
	for (int i = 0; i < N; i++) {
		int t, d;
		scanf("%d%d", &t, &d);
		sushi[t].push_back(d);
	}

	vector<int> top, other;
	for (auto iter = sushi.begin(); iter != sushi.end(); iter++) {
		vector<int> elements = iter->second;
		sort(elements.begin(), elements.end(), greater<int>());
		top.push_back(elements[0]);
		for (int i = 1; i < (int)elements.size(); i++)
			other.push_back(elements[i]);
	}
	sort(top.begin(), top.end(), greater<int>());
	sort(other.begin(), other.end(), greater<int>());

	ll res = 0;
	for (int k = 1; k <= K; k++) {
		if (k > (int)top.size() || K - k > (int)other.size())
			continue;

		ll local = 0;
		for (int i = 0; i < k; i++)
			local += (ll)top[i];
		for (int i = k; i < K; i++)
			local += (ll)other[i - k];
		local += (ll)k * (ll)k;

		res = max(res, local);
	}

	printf("%lld\n", res);

	return 0;
}


Measured memory_bytes: 11772
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  
  vector<int> V;
  vector<int> C;
  
  for(int i=0; i<N; i++)
  {
    int n;
    cin >> n;
    V.push_back(n);
  }
  
  for(int i=0; i<N; i++)
  {
    int n;
    cin >> n;
    C.push_back(n);
  }
  
  int high = 0;
  int low = 0;
  
  for(int i=0; i<N; i++)
  {
    int mouke = V[i] - C[i];
    if(mouke>0)
    {
      high += mouke;
    }else{
      low += mouke;
    }
  }
  
  cout << high << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int,int>
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;

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


int main() {
    fast_io
    int n, x;
    cin >> n;
    ll ans = 0;
    rep(i,n) {
        rep(j,n) {
            x = gcd(i+1, j+1);
            rep(k,n) {
                ans += gcd(x, k+1);
            }
        }
    }
    cout << ans << endl;
    return 0;
}


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

int main () {
  int N;
  cin >> N;
  vector <int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  int a_score = 0, b_score = 0; 
  for (int i = N - 1; i >= 0; i-=2) {
    a_score += a[i];
    b_score += a[i - 1];
  }
  
  cout << a_score - b_score << endl;
  
  return 0;
}
    
  

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=200+5;
const int inf=99999999;
int dist[11][11]={},d[11][11];
int main()
{
    int h,w;
    while(scanf("%d%d",&h,&w)==2){
        for(int i=0;i<10;i++)
        for(int j=0;j<10;j++){
            scanf("%d",&d[i][j]);
        }
        for(int k=0;k<=9;k++)
            for(int i=0;i<=9;i++)
            for(int j=0;j<=9;j++)
            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
        int ans=0;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                int x;
                scanf("%d",&x);
                if(x==-1) continue;
                ans+=d[x][1];
            }
        printf("%d\n",ans);
    }
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int main(void)
{

    int N;

    cin >> N;

	int yaku_8 = 0;	

	for(int i=1; i<=N; i++){
		int x = 0;
		for (int j=1; j<=i; j++){
			if(i%2 != 0 && i%j ==0) x++;
		}
		if (x == 8) yaku_8++;
	}
	
	printf("%d",yaku_8);	
	
	return 0;
}



Measured memory_bytes: 256
#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
	int n, m;
	cin >> n >> m;
	int cnt = 0;
	int a[100];
	int suma = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		suma += a[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] * 4 * m >= suma) cnt++;
	}
	if (cnt >= m)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

Measured memory_bytes: 256
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

h, w = MAP()
n = INT()
a = LIST()
ans = [[0 for _ in range(w)] for _ in range(h)]
index = 0
cnt = 0
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            if cnt < a[index]:
                ans[i][j] = index + 1
                cnt += 1
            else:
                index += 1
                cnt = 0
                if cnt < a[index]:
                    ans[i][j] = index + 1
                    cnt += 1
    else:
        for j in reversed(range(w)):
            if cnt < a[index]:
                ans[i][j] = index + 1
                cnt += 1
            else:
                index += 1
                cnt = 0
                if cnt < a[index]:
                    ans[i][j] = index + 1
                    cnt += 1
for i in range(h):
    print(' '.join(map(str, ans[i])))



Measured memory_bytes: 5588
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

void solve(long long N, long long K, std::vector<long long> A){
    mint ans =1;
    if(K==N){
        for(int i=0;i<N;i++){
            ans *= mint(A[i]);
        }
        cout << ans << endl;
        return;
    }
    //0以下しかない時小さい順に選ぶ
    int notPlus =0;
    for(int i=0;i<N;i++){
        if(A[i] <=0){
            notPlus++;
        }
    }
    if(N== notPlus && K%2==1){
        sort(A.rbegin(),A.rend());
        for(int i=0;i<K;i++){
            ans *=mint(A[i]);
        }
        cout << ans << endl;
        return;
    }
    sort(A.begin(),A.end());
    int lindex=0; int rindex =N-1;
    ll minusNum =0;
    for(int i=0;i<K;i++){
        if(abs(A[lindex]) >= abs(A[rindex])){
            ans *= mint(A[lindex]);
            if(A[lindex]<0){
                minusNum++;
            }
            lindex++;
        }else{
            ans *= mint(A[rindex]);
            rindex--;
        }
    }
    //結果がマイナスになった時,rindexを減らすか、lindexを減らす。
    // cerr <<"minus=" <<minusNum <<"lindex="<<lindex <<"rindex=" << rindex<< endl;
    if(minusNum%2==1){
        // cerr <<"minus" << endl;
        if(rindex<N-1 &&( A[lindex] * A[lindex-1]>A[rindex]*A[rindex+1])){
            ans /= mint(A[rindex+1]);
            ans*= mint(A[lindex]);
        }else{
            ans/=mint(A[minusNum-1]);
            ans*=mint(A[rindex]);
        }
       
    }
    cout << ans << endl;


}

// Generated by 1.1.7.1 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;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, K, std::move(A));
    return 0;
}


Measured memory_bytes: 4840
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>

using namespace std;
using vec = vector<int>;
using mat = vector<vec>;
using lst = list<int>;

vec parent;
vec color;

int find(int i)
{
    if(i==parent[i])return i;
    return parent[i]=find(parent[i]);
}

void unite(int i,int j)
{
    i=find(i);
    j=find(j);
    parent[i]=j;
    color[j]=color[i]|color[j];
}

bool solve()
{
    int w,h;
    cin >> w >> h;
    if(w==0)return false;

    parent=vec(w*h);
    color=vec(w*h);

    for(int i=0;i<w*h;++i)
    {
        parent[i]=i;
        color[i]=0;
    }

    mat Tochi(h,vec(w));
    for(int i=0;i<h;++i)
    {
        string s;
        cin >> s;
        for(int j=0;j<w;++j)
        {
            char c = s[j];
            if(c=='.')Tochi[i][j]=0;
            else if(c=='B')Tochi[i][j]=1;
            else if(c=='W')Tochi[i][j]=2;

            if(i!=0)
            {
                if(Tochi[i][j]==0&&Tochi[i-1][j]!=0)
                {
                    color[i*w+j]=color[i*w+j] | Tochi[i-1][j];
                }
                else if(Tochi[i][j]!=0&&Tochi[i-1][j]==0)
                {
                    color[(i-1)*w+j]=color[(i-1)*w+j] | Tochi[i][j];
                }
            }
            if(j!=0)
            {
                if(Tochi[i][j]==0&&Tochi[i][j-1]!=0)
                {
                    color[i*w+j]=color[i*w+j] | Tochi[i][j-1];
                }
                else if(Tochi[i][j]!=0&&Tochi[i][j-1]==0)
                {
                    color[i*w+j-1]=color[i*w+j-1] | Tochi[i][j];
                }
            }
        }
    }

    for(int i=0;i<h;++i)
    {
        for(int j=0;j<w;++j)
        {
            if(i!=0&&Tochi[i][j]==0&&Tochi[i-1][j]==0)
            {
                unite(i*w+j,(i-1)*w+j);
            }
            if(j!=0&&Tochi[i][j]==0&&Tochi[i][j-1]==0)
            {
                unite(i*w+j,i*w+j-1);
            }
        }
    }

    int ansB=0,ansW=0;
    for(int i=0;i<w*h;++i)
    {
        ansB += color[find(i)]==1;
        ansW += color[find(i)]==2;
    }

    cout << ansB << " " << ansW << endl;

    return true;
}

int main()
{
    while(solve());
    return 0;
}



Measured memory_bytes: 3212
/*
知らなかったところ

A,A+1,...,B  について○○を計算した合計を求めよ, という問題では
0,1,...,B について○○を計算した合計」から
「0,1,...,A−1 のうち○○を計算した合計」を引いたものを求めると考えるとよい

XORの場合は、引き算としてはXORをすればよい

左から4つ組で区切っていくと
その4つの中では必ず全てのビットについて1の個数が偶数個になっている
つまり4つの数のXORがちょうど0になる

B  を含む4つ組の最初の数字を求めて
この値から B までのXORを取ればよい

A−1  と B の両方に行い、その結果のXORを取る

//https://betrue12.hateblo.jp/entry/2019/03/09/224330

*/

#include <iostream>

using namespace std;

long long calc(long long x){
    
    long long result = 0;
    
    long long start = (x / 4) * 4;
    
    for (long long i = start; i <= x; i++) result ^= i;
    
    return result;
}

int main(){
    
    long long a, b;
    cin >> a >> b;
    
    long long ans = calc(b) ^ calc(a-1);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define INF 2147483647//int max
const int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int,int>;


int main() {
    int a,b,m;
    cin >> a >> b >> m;
    vector<int> a_p(a,0);
    rep(i,a)cin >> a_p[i];
    vector<int> b_p(b,0);
    rep(i,b)cin >> b_p[i];
    vector< vector<int>> sell(m,vector<int>(3,0));
    rep(i,m)cin >> sell[i][0] >> sell[i][1] >> sell[i][2];

    ll ans = *min_element(a_p.begin(),a_p.end()) + *min_element(a_p.begin(),a_p.end());
    rep(i,m){
        ll price = 0;
        price = a_p[sell[i][0]-1] + b_p[sell[i][1]-1] - sell[i][2];
        ans = min(ans,price);
    }
    cout << ans << endl;
    return 0;
}

/*
やっほい！
　　　　　　　やほほい！
　　　　+　　 　*
　　　 ∧∧　　. ∧∞∧　*
*　ヽ(=´ω｀)人(´ω｀*)ﾉ
　.～（ O x.） （ 　 O)～　+
。*　 　∪　　　　∪
*/

Measured memory_bytes: 6528
#include<iostream>
#include<string>
#include<cmath>
#include<math.h>
#include<array>
#include<algorithm>
//#include<queue>
//#include<deque>
#include<vector>
//#include<utility>
//#include<set>
//#include<map>
//#include<list>
#include<bitset>
#include<iomanip>

using namespace std;
using LLI = long long int;
LLI N;

//array<int, 8>dir_x = { 0,0,1,1,1,-1,-1,-1 };
//array<int, 8>dir_y = { 1,-1,1,0,-1,1,0,-1 };
//array<int, 4>dir_x2 = { 0,0,1,-1 };
//array<int, 4>dir_y2 = { 1,-1,0,0 };
//char alp[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };

//class UnionFind {
//public:
//	vector<int>data;
//	UnionFind(int size) :data(size, -1) {};
//	bool unionset(int x, int y) {
//		x = root(x), y = root(y);
//		if (x != y) {
//			if (data[y] < data[x]) {
//				swap(x, y);
//			}
//			data[x] += data[y];
//			data[y] = x;
//		}
//		return x != y;
//	}
//	bool findset(int x, int y) {
//		return root(x) == root(y);
//	}
//	int root(int x) {
//		return data[x] < 0 ? x : data[x] = root(data[x]);
//	}
//	int size(int x) {
//		return -data[root(x)];
//	}
//};

//LLI gcd(LLI x, LLI y) {
//	LLI  r;
//	if (x < y) {
//		swap(x, y);
//	}
//	r = x % y;
//	while (r != 0) {
//		x = y;
//		y = r;
//		r = x % y;
//	}
//	return y;
//}
//LLI lcm(LLI x, LLI y) {
//	return x / gcd(x, y) * y;
//}

//long nCr(int n, int r) {
//	long ans = 1;
//	for (int i = n; i > n - r; i--) {
//		ans = ans * i;
//	}
//	for (int i = 1; i < r + 1; i++) {
//		ans = ans / i;
//	}
//	return ans;
//}
//long nHr(int n, int r) {
//	n = n + r - 1;
//	return nCr(n, r);
//}

//const int n_max = 8;
//array<array<bool, n_max>, n_max>graph;
//int dfs(int v,int N,array<bool,n_max>visited) {
//	int ret = 0;
//	bool all_visited = true;
//	for (int i = 0; i < N;i++) {
//		if (visited[i] == false) {
//			all_visited = false;
//		}
//	}	
//	if (all_visited) {
//		return 1;
//	}
//	for (int i = 0; i < N;i++) {
//		if (graph[v][i] == false) {
//			continue;
//		}
//		if (visited[i] == true) {
//			continue;
//		}
//		visited[i] = true;
//		ret += dfs(i,N,visited);
//		visited[i] = false;
//	}
//	return ret;
//}

//vector<vector<int>>sub;
//int dfs(int id) {
//	if (sub[id].size() == 0) {
//		return 1;
//	}
//	int minP = (int)1e9;
//	int maxP = 0;
//	for (int i : sub[id]) {
//		int P = dfs(i);
//		minP = min(minP, P);
//		maxP = max(maxP, P);
//	}
//	return minP + maxP + 1;
//}

//#define debug(x) cerr << #x << " : " << x << endl
//#define debugv(x) cerr << #x << " : "; for(auto i:x) cerr << i << ", "; cerr<<endl

int main(void) {
	LLI N;
	cin >> N;
	LLI cnt = 0;
	for (int i = 1; i < N;i++) {
		cnt += i;
	}
	cout << cnt << endl;
}

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

ans = 0
ans_lis = []

for i in range(1, n+1):
    s1 = sum(w_lis[0:i])
    s2 = sum(w_lis[i:])
    ans = s1 - s2
    if ans < 0:
        ans = -ans
    ans_lis.append(ans)

print(min(ans_lis))

Measured memory_bytes: 3064
n = gets.to_i
an = gets.split.map(&:to_i)
arai = an.pop
sunuke = an.inject(:+)
ans = [(sunuke-arai).abs]
1.upto(n-2) do |i|
  tmp = an.pop
  ans << ((sunuke -= tmp) - (arai += tmp)).abs
end

puts ans.min

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

bool is_square(int n)
{
    for (int i = 0; i <= n; i++)
    {
        if (i * i == n)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    int n, d;
    cin >> n >> d;
    int ans = 0;
    vector<vector<int>> vp(n, vector<int>(d, 0));
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < d; j++)
        {
            cin >> vp[i][j];
            /* code */
        }
    }
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = i + 1; j < n; j++)
        {
            int sum = 0;
            for (size_t k = 0; k < d; k++)
            {
                sum += (vp[i][k] - vp[j][k]) * (vp[i][k] - vp[j][k]);
                /* code */
            }
            if (is_square(sum))
            {
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  string s,t;
  cin >> s >> t;
  
  int ans=0;
  if(s.at(0) == t.at(0)){
    ans++;
  }
  if(s.at(1) == t.at(1)){
    ans++;
  }
  if(s.at(2) == t.at(2)){
    ans++;
  }
  
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

template <class T>
struct segtree {
  int n;
  vector<T> data;

  segtree(const int s) {
    init(s);
  }

  segtree(const int s, const T u) {
    init(s);
    for (int i = 0; i < s; i++) {
      data[i+n-1] = u;
    }
    for (int i = n-2; i >= 0; i--) {
      data[i] = min(data[i*2+1], data[i*2+2]);
    }
  }

  segtree(const vector<T> &v) {
    int s = v.size();
    init(s);
    for (int i = 0; i < s; i++) {
      data[i+n-1] = v[i];
    }
    for (int i = n-2; i >= 0; i--) {
      data[i] = 0;
    }
  }

  void init(const int s) {
    n = 1;
    while (n < s) n <<= 1;
    data = vector<T>(2*n-1);
  }

  void update(int x, T v) {
    int p = x + n - 1;

    // update bottom
    data[p] = v;

    // update rest
    while (p > 0) {
      p = (p - 1) >> 1;
      data[p] = min(data[p*2+1], data[p*2+2]);
    }
  }

  T query(int l, int r, int p = 0, int a = 0, int b = -1) {
    if (b < 0) b = n; // init
    if (r <= a || b <= l) return 0x7fffffff; // out of range

    if (l <= a && b <= r) return data[p];// fully covered

    int vl = query(l, r, p*2+1, a, (a + b) / 2); // left
    int vr = query(l, r, p*2+2, (a + b) / 2, b); // right
    return min(vl, vr);
  }
};

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

  int n, q;
  cin >> n >> q;
  vector<vector<int> > query(q, vector<int>(4));
  for (int i = 0; i < q; i++) {
    cin >> query[i][0] >> query[i][1] >> query[i][2];
  }

  segtree<int> st(n, 0x7fffffff);
  vector<ll> ans;
  for (int i = 0; i < q; i++) {
    if (query[i][0] == 0) { // update
      st.update(query[i][1], query[i][2]);
    }
    else { // findMin
      ans.push_back(st.query(query[i][1], query[i][2]+1));
    }
  }

  for (auto &x: ans) cout << x << endl;

  return 0;
}



Measured memory_bytes: 9976
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5;
#define endl '\n'

int a[N],n,k;

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cout.setf(ios::fixed),cout.precision(3);
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n);
	int sum=0;
	for(int i=0;i<k;i++){
		sum+=a[i];
	}
	cout<<sum<<endl;
	return 0;
}

Measured memory_bytes: 3672
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<limits.h>
#include<float.h>

#define NUM 100001

int gcd(int a, int b){
	if(a<b){
		int tmp = a;
		a = b;
		b = tmp;
	}
	if(b == 0) return a;
	int c = a%b;
	while(c){
		a = b;
		b = c;
		c = a%b;
	}
	return b;
}

int calcL(int i, int *a, int *l){
	if(i==0) return 0;
	if(l[i] != 0) return l[i];
	l[i] = gcd(calcL(i-1, a, l), a[i-1]);
	return l[i];
}

int calcR(int i, int *a, int *r, int n){
	if(i==n+1) return 0;
	if(r[i] != 0) return r[i];
	r[i] = gcd(calcR(i+1, a, r, n), a[i]);
	return r[i];
}

int calcM(int i, int *a, int *l, int *r, int n){
	return gcd(calcL(i, a, l), calcR(i+1, a, r, n));
}

int main(){
	int a[NUM], m, l[NUM], r[NUM];
	int n, max = 0;
	scanf("%d\n", &n);
	for(int i=0; i<n; i++){
		scanf("%d\n", &a[i]);
		l[i] = 0;
		r[i] = 0;
	}
	l[n]=0;
	r[n]=0;
	for(int i=0; i<n; i++){
		m = calcM(i, a, l, r, n);
		if(m > max) max = m;
	}
	printf("%d\n", max);
	return 0;
}




Measured memory_bytes: 4480
S = input()
gx, gy = map(int, input().split())


op_x = []
op_y = []
cnt_turn = 0
cnt_f = 0
sx,sy = 0, 0
for s in S:
    if s == "F":
        cnt_f += 1
    else:
        if cnt_turn  == 0:
            sx = cnt_f
            cnt_turn += 1
            cnt_f = 0
        elif cnt_turn % 2 == 0:
            op_x.append(cnt_f)
            cnt_turn += 1
            cnt_f = 0
        else:
            op_y.append(cnt_f)
            cnt_turn += 1
            cnt_f = 0

if cnt_turn  == 0:
    sx = cnt_f
    cnt_turn += 1
    cnt_f = 0
elif cnt_turn % 2 == 0:
    op_x.append(cnt_f)
    cnt_turn += 1
    cnt_f = 0
else:
    op_y.append(cnt_f)
    cnt_turn += 1
    cnt_f = 0

X = set()
X.add(sx)
for dx in op_x:
    nxt = set()
    for x in X:
        nxt.add(x + dx)
        nxt.add(x - dx)
    X = nxt

Y = set()
Y.add(sy)
for dy in op_y:
    nxt = set()
    for y in Y:
        nxt.add(y + dy)
        nxt.add(y - dy)
    Y = nxt

print("Yes" if gx in X and gy in Y else "No")


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

int main() {
    int t[2],b[2];
    cin >> t[0] >> t[1] >> b[0] >> b[1];

    sort(t,t+2);
    sort(b,b+2);

    cout << t[0]+b[0];
    return 0;
}

Measured memory_bytes: 256
#include <iostream>

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

// 2017-like-number.cpp --- 2017-like Number
// author: Seong Yong-ju <sei40kr@gmail.com>

bool fs[100001];
int imos[100001];

using namespace std;

int main() {
  FOR(i, 2, 100001) {
    if (!fs[i]) {
      for (int j = i << 1; j < 100001; j += i) {
        fs[j] = true;
      }
    }
  }

  for (int i = 3; i < 100001; i += 2) {
    if (!fs[i] && !fs[(i + 2) >> 1]) {
      ++imos[i];
    }
  }

  FOR(i, 3, 100001) { imos[i] += imos[i - 1]; }

  int n;
  cin >> n;

  REP(i, n) {
    int l, r;
    cin >> l >> r;

    cout << (imos[r] - imos[l - 1]) << endl;
  }
}


Measured memory_bytes: 1152
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)
int main() {
    int k, s;
    cin >> k >> s;
    int ans = 0;
    rep(i, k+1) rep(j, k+1) if ( s - ( i + j ) <= k && s >= ( i + j ) ) {
        ++ans;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define gadd(x,y) x=add(x,y)
#define gmul(x,y) x=mul(x,y)
using namespace std;

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

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	int p,n;
	cin >> p >> n;
	vector<int> v(n);
	rep(i,0,n)cin >> v[i];
	int res = p - v[n-1] + v[0];
	rep(i,0,n-1){
		gmax(res, v[i+1] - v[i]);	
	}
	cout << p-res << '\n';
}


Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <stdio.h>
#include <tuple>
#include <bitset>
#include <cfloat>
#include <fstream>
#include <limits.h>
#include <list>
#include <math.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <numeric>
#include <utility>
#include <sstream>
// *テンプレート*
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define sz(x) x.size()
// aよりbの方が小さかったら、aの値をbの値に置き換える。(swapではない！)
template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
// aよりbの方が大きかったら、aの値をbの値に置き換える。
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;
//最大公約数
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (b == 0) return a;
	return gcd(b, a % b);
}
//最小公倍数
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}
// 素数かどうか判定
bool prime(int n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n%i == 0)return false;
	}
	return n != 1;
}


const long long MOD = 1000000007;
const long long INF = 1LL << 60;
// *テンプレートここまで*

// pair -> vector<pair<int,int>> 
//		   AandB.push_back(make_pair(a[i],b[i]))


int now[110][110];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int w, h, n; cin >> w >> h >> n; 
	rep(tmp, n) {
		int x, y, a; cin >> x >> y >> a;
		if (a == 1) {
			rep(i, x) {
				rep(j, h) {
					now[i][j] = 1;
				}
			}
		}
		else if (a == 2) {
			for (int i = x; i < w; i++) {
				rep(j, h) {
					now[i][j] = 1;
				}
			}
		}
		else if (a == 3) {
			rep(i, w) {
				rep(j, y) {
					now[i][j] = 1;
				}
			}
		}
		else {
			rep(i, w) {
				for (int j = y; j < h; j++) {
					now[i][j] = 1;
				}
			}
		}
	}
	int ans = 0;
	rep(i, w) {
		rep(j, h) {
			if (now[i][j] == 0)ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 384
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)

int main(){
    int n,a,b;
    cin>>n>>a>>b;
    
    int t = abs(a-b);
    if(t%2)cout<<"Borys"<<endl;
    else cout<<"Alice"<<endl;
}


Measured memory_bytes: 764
#include <iostream>
#include <iomanip>
#include <vector>
#include <valarray>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <bitset>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <complex>
#include <string>
#include <sstream>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cassert>

#include <unordered_set>
#include <unordered_map>
#include <random>
#include <thread>
#include <chrono>

using namespace std;
using ll = long long;

#define val const auto
#define var auto

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x) 
#endif

typedef complex<double> point;

template<typename T,std::size_t N>
struct _v_traits {using type = std::vector<typename _v_traits<T,N-1>::type>;};
template<typename T>
struct _v_traits<T,1> {using type = std::vector<T>;};
template<typename T,std::size_t N=1>
using vec = typename _v_traits<T,N>::type;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "{";
    for(auto it = v.begin(); it != v.end(); ++it){
        if(it != v.begin()){
            os << ",";
        }
        os << *it;
    }
    return os << "}";
}

template <typename T>
T read_next(){
    T ret; cin >> ret;
    return ret;
}


template<typename T>
void isort(std::vector<T>& v, std::function<bool(T,T)> comp=less<T>()){
    sort(v.begin(), v.end(), comp);
}
template<typename T>
std::vector<T> sort(std::vector<T> v, std::function<bool(T,T)> comp=less<T>()){
    isort(v);
    return v;
}
template<typename T1, typename T2>
std::vector<T2> rmap(const std::vector<T1>& v, std::function<T2(T1)> f){
    std::vector<T2> t; t.reserve(v.size());
    for(const auto& i: v) t.push_back(f(i));
    return t;
}
std::vector<std::string> split(std::string str, char delim){
    std::vector<std::string> res;
    size_t current = 0, found;
    while((found = str.find_first_of(delim, current)) != std::string::npos){
        res.push_back(std::string(str, current, found - current));
        current = found + 1;
    }
    res.push_back(std::string(str, current, str.size() - current));
    return res;
}
string join(const std::vector<string>& v, string delim){
    string ret = "";
    for(auto it = v.begin(); it != v.end(); ++it){
        if(it != v.begin()){
            ret += delim;
        }
        ret += *it;
    }
    return ret;
}


using i64 = int64_t;
using i32 = int32_t;

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

    ll a, b, c, x, y;
    cin >> a >> b >> c >> x >> y;

    ll ret = 100000000000000LL;
    for(int i=0; i <= 2*max(x,y); i+=2){
        ll d = i/2;
        ll cost = c*i + a*max(0LL, x-d) + b*max(0LL, y-d);
        ret = min(ret, cost);
    }

    cout << ret << endl;

    return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-8)
#define MOD 1000000007
#define PI 3.141592653589793
#define RMAX 4294967295
#define equals(a,b)(fabs((a)-(b))<EPS)

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef complex<double> comp;
typedef vector<complex<double>> vcomp;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef string::const_iterator State;
class ParseError {};
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int N;
Graph G;
vi X;

P dfs(int pos, int par) {
	if (pos != 0 && G[pos].size() == 1) {
		return mp(0, X[pos]);
	}
	int M = G[pos].size() + (pos == 0 ? 1 : 0);
	vvb dp(M, vb(X[pos] + 1, false));
	dp[0][0] = true;
	ll sum = 0;
	int cnt = 0;
	rep(i, G[pos].size()) {
		Edge edge = G[pos][i];
		if (edge.to == par) {
			continue;
		}
		P ch = dfs(edge.to, pos);
		if (ch.second < 0)return mp(-1, -1);

		sum += ch.first + ch.second;
		rep(j, X[pos] + 1) {
			if (j - ch.first >= 0 && dp[cnt][j - ch.first]) {
				dp[cnt + 1][j] = true;
			}
		}
		rep(j, X[pos] + 1) {
			if (j - ch.second >= 0 && dp[cnt][j - ch.second]) {
				dp[cnt + 1][j] = true;
			}
		}
		cnt++;
	}

	reprev(i, X[pos] + 1) {
		if (dp[M - 1][i]) {
			return mp(min((ll)X[pos], sum - i), max((ll)X[pos], sum - i));
		}
	}
	return mp(-1, -1);
}

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

	cin >> N;
	X = vi(N);
	G = Graph(N);
	rep(i, N - 1) {
		int p;
		cin >> p;
		p--;
		G[i + 1].push_back(Edge{ i + 1,p,1 });
		G[p].push_back(Edge{ p,i + 1,1 });
	}
	rep(i, N) {
		cin >> X[i];
	}

	P ans = dfs(0, -1);

	if (ans.first >= 0 && ans.second >= 0) {
		cout << "POSSIBLE" << endl;
	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}

	return 0;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

int main() {
  i64 n;
  cin >> n;
  string s;
  cin >> s;
  char b = '$';
  i64 ans = 0;
  for(char c : s) {
    if(b != c) ans++;
    b = c;
  }
  cout << ans << endl;
}


Measured memory_bytes: 512
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        a:usize,
        b:usize,
        c:usize,
    }
    let mut array:Vec<usize> = Vec::new();
    array.push(a);
    array.push(b);
    array.push(c);
    array.sort();
    if array[0] == 5 && array[1] == 5 && array[2] == 7{
        println!("YES");
    }else{
        println!("NO");
    }

}


Measured memory_bytes: 4352
import bisect

s = list(input())
t = list(input())

if not set(t).issubset(set(s)):
    print(-1)
    exit()

indices = [[] for _ in range(26)]
idx = [0]*26

for i in range(len(s)):
    indices[ord(s[i]) - 97].append(i)

now = 0
rep = 1
for i in range(len(t)):
    x = ord(t[i]) - 97
    y = bisect.bisect_left(indices[x], now)
    if y == len(indices[x]):
        rep += 1
        now = indices[x][0] + 1
    else:
        now = indices[x][y] + 1

print(len(s)*(rep-1) + now)

Measured memory_bytes: 60400
#include<iostream>
using namespace std;
	long long a[200][200];
	long long b[200][200];
	long long c[200][200];
int main(){
	int n,m,l;
	cin>>n>>m>>l;
	int sum=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=l;j++){
			cin>>b[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=l;j++){
			long sum=0;
			for(int k=1;k<=m;k++){
				sum+=a[i][k]*b[k][j];
			}
			if(j>1)cout<<" ";
			cout<<sum;
			if(j==l)cout<<endl;
		}
	}
}

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

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}


int main() {
    int N,K;
    cin >> N >>K;
    COMinit();
    for(int i=1;i<=K;i++){
        ll ans = COM(N+1-K,i) * COM(K-1,i-1);
        cout << ans%MOD << endl;
    }
    return 0;
}

Measured memory_bytes: 12288
CONST   fi='';
        fo='';
VAR
    f:text;
    n:longint;
    check:boolean;
PROCEDURE input;
  begin
    Assign(f,fi);reset(f);
      readln(f,n);
    Close(f);
  end;

PROCEDURE main;
  var i:longint;
  begin
    check:=false;
    for i:=0 to 15 do
      if (n - i*7 >=0) and ((n - i*7) mod 4 =0 ) then check:=true;
  end;

PROCEDURE output;
  begin
    Assign(f,fo);rewrite(f);
       if check then writeln(f,'Yes') else writeln(f,'No');
    Close(f);
  end;

BEGIN
   input; main; output;
END.


Measured memory_bytes: 128
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
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; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;



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

	int N; cin >> N;
	VI A(100010);
	VI cnt(100010 + 1, 0);
	int sum = 0;
	REP(i, N) {
		cin >> A[i];
		cnt[A[i]]++;
		sum += A[i];
	}
	int Q; cin >> Q;
	REP(i, Q) {
		int b, c; cin >> b >> c;
		sum -= cnt[b] * b;
		sum += cnt[b] * c;
		cnt[c] += cnt[b];
		cnt[b] = 0;
		cout << sum << endl;
	}


	return 0;
}



Measured memory_bytes: 4784