akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<ll>>;
struct edge{ll to ; ll cost ;} ;
using graph =vector<vector<edge>> ;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second 
#define set20 cout<<fixed<<setprecision(20) ;
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
double pi=acos(-1) ;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
 

 int main(){
 ll m,k ; cin>>m>>k ;
 if(m==0){
   if(k==0) cout<<0<<" "<<0<<endl ;
   else cout<< -1<<endl ;
 }
 else if(m==1){
   if(k==0) cout<<0<<" "<<0<<" "<<1<<" "<<1<<endl ;
   else cout<<-1<<endl ;
 }
 else{
   if(k>pow(2,m)-1) cout<<-1<<endl ;
   else{
    ll d= pow(2,m)-1 ;
    rep(i,d+1){
      if(i<k) cout<< i<<" " ;
      if(i>=k&&i!=d) cout <<i+1<<" " ;
      if(i==d) cout<<k<<" " ;
    }
    rep3(i,d,0){
     if(i>k) cout<<i<<" " ;
     if(i<=k&&i!=0) cout<< i-1<<" " ;
     if(i==0) cout<<k<<endl ; 
    }
   }
 }
   return 0 ;
 }



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

vector<int>to[200005];//辺を入力するための配列を準備
vector<int>ans;//答の配列を準備
void dfs(int v,int p=-1){//注目しているのはv、親なしは-1、デフォルト引数
  for(int u:to[v]){//
    if (u==p)continue;//逆走させない枝刈り
    ans[u]+=ans[v];//累積和
    dfs(u,v);//再帰、uが始点、vが終点
  }
}
int main(){
  int n,q;
  cin>>n>>q;  
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    --a;--b;//ノードは1始まりなので0始まりに
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);//resizeしないとエラー
  rep(i,q){
    int p,x;
    cin>>p>>x;
    --p;//ノードは1始まりなので0始まりに
    ans[p]+=x;//累積和
  }
  dfs(0);//ゼロからスタート、親なのでp=-1（デフォルト引数）
  rep(i,n){
    cout<<ans[i]<<endl;
  }
  return 0;
}

Measured memory_bytes: 23424
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,m;
bool ok[35][35][4];
int d[35][35][4];
int main(){
	while(1){
		memset(ok,0,sizeof(ok));
		cin >> n >> m; if(n==0) return 0;
		for(int i=1;i<=m;i++){
			ok[1][i][0] = 0;
			ok[n][i][2] = 0;
		}
		ok[1][1][0] = ok[n][m][2] = 1;
		
		for(int i=1;i<=2*n-1;i++){
			if(i%2 == 1){
				ok[i/2+1][1][3] = 0;
				for(int j=1;j<m;j++){
					int x; cin >> x;
					ok[i/2+1][j][1] = ok[i/2+1][j+1][3] = 1-x;
				}
				ok[i/2+1][m][1] = 0;
			}
			else{
				for(int j=1;j<=m;j++){
					int x; cin >> x;
					ok[i/2][j][2] = ok[i/2+1][j][0] = 1-x;
				}
			}
		}
		int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				for(int k=0;k<4;k++){
					if(1<=i+dx[k]&&i+dx[k]<=n&&1<=j+dy[k]&&j+dy[k]<=m&&ok[i][j][k]);
					else{
						d[i][j][k] = -1; continue;
					}//cout << i << " " << j  << " " << k << endl;
					queue<P>que; bool used[35][35]={}; int dist[35][35];
					//(i,j)-(i+dx[k],j+dy[k]) is forbid
					for(int i=0;i<35;i++) for(int j=0;j<35;j++) dist[i][j] = INF;
					que.push(mp(i,j)); dist[i][j] = 0;
					while(!que.empty()){
						P p = que.front(); que.pop();
						if(used[p.fi][p.sc]) continue;
						used[p.fi][p.sc] = 1;
						for(int q=0;q<4;q++){
							if(1<=p.fi+dx[q]&&p.fi+dx[q]<=n&&1<=p.sc+dy[q]&&p.sc+dy[q]<=m&&ok[p.fi][p.sc][q]){
							    //cout << p.fi+dx[q] << " " << p.sc+dy[q] << "g " << k << endl;
								if(p == mp(i,j) && q == k) continue;
								if(p == mp(i+dx[k],j+dy[k]) && (q^2) == k) continue;
								if(used[p.fi+dx[q]][p.sc+dy[q]]) continue;
								dist[p.fi+dx[q]][p.sc+dy[q]] = min(dist[p.fi+dx[q]][p.sc+dy[q]],dist[p.fi][p.sc]+1);
								que.push(mp(p.fi+dx[q],p.sc+dy[q]));
								//cout << p.fi+dx[q] << " " << p.sc+dy[q] << endl;
							}
						}
					}
					d[i][j][k] = (dist[n][m]>1e8?-1:dist[n][m]);
				}
			}
		}
		int lb = 0,ub = 2000;
		while(ub-lb > 1){
			int mid = (lb+ub)/2;
			int D[35][35];
			for(int i=0;i<35;i++) for(int j=0;j<35;j++) D[i][j] = INF;
			D[1][1] = 0;
			queue<P>que; bool used[35][35]={};
			que.push(mp(1,1));
			while(!que.empty()){
				P p = que.front(); que.pop();
				if(used[p.fi][p.sc]) continue;
				used[p.fi][p.sc] = 1;
				for(int k=0;k<4;k++){
					if(d[p.fi][p.sc][k] == -1) continue;
					if(D[p.fi][p.sc]+d[p.fi][p.sc][k] > mid) continue;
					int X = p.fi+dx[k],Y = p.sc+dy[k];
					if(used[X][Y]) continue;
					D[X][Y] = min(D[X][Y],D[p.fi][p.sc]+1);
					que.push(mp(X,Y));
				}
			}
			if(D[n][m] <= mid) ub = mid;
			else lb = mid;
		}
		cout << (ub>1900?-1:ub) << endl;
	}
}

Measured memory_bytes: 3124
h,w,n=map(int,input().split())
from itertools import product as pr
from collections import Counter as co
ans=[(h-2)*(w-2)]+[0]*9
io=[tuple(map(int,input().split())) for i in range(n)]
for k,v in co(co([(a-x)*w+b-y for x,y in pr(range(3),repeat=2) for a,b in io if h-1>a-x>0<b-y<w-1]).values()).items():
    ans[k]=v
    ans[0]-=v
print(*ans,sep='\n')

Measured memory_bytes: 119736
import math


def Manhattan(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i])
    return D


def Euclidean(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D += (x[i] - y[i]) ** 2
    return math.sqrt(D)


def Tri(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i]) ** 3
    return math.pow(D, 1.0 / 3.0)


def Chebyshev(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D = max(D, abs(x[i] - y[i]))
    return D


_ = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

print(Manhattan(x, y))
print(Euclidean(x, y))
print(Tri(x, y))
print(Chebyshev(x, y))



Measured memory_bytes: 5712
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;


ll calc(ll x){
    return x * (x - 1) / 2;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll n, x, d;
    cin >> n >> x >> d;
    if(x == 0 && d == 0) return cout << 1, 0;
    if(d == 0) return cout << n + 1, 0;
    // if(x == 0) return cout << calc(n), 0;
    if(d < 0){
        d = -d;
        x = -x;
    }
    map<int, vector<pair<ll, ll>> > mp;
    for(int i = 0; i <= n; i++){
        ll l = x * i + (calc(i)) * d;
        ll r = x * i + (calc(n) - calc(n - i)) * d;
        mp[((l % d) + d) % d].push_back({l, r});
    }
    ll ans = 0;
    for(auto &i: mp){
        sort(i.second.begin(), i.second.end());
        ll l, r;
        bool found = 0;
        for(auto &j: i.second){
            if(!found){
                found = 1;
                l = j.first;
                r = j.second;
            }
            else{
                if(j.first <= r) r = max(r, j.second);
                else{
                    ans += (r - l) / d + 1;
                    l = j.first;
                    r = j.second;
                }
            }
        }
        ans += (r - l) / d + 1;
    }
    cout << ans;

    return 0;
}


Measured memory_bytes: 25460
#include <iostream>
#include <algorithm>
#include <vector>

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

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

std::vector<std::vector<int>> to;

mod_int dfs(int i)
{
    mod_int ret = 1;
    for (int j = 0; j < to[i].size(); ++j)
    {
        ret *= dfs(to[i][j]);
    }
    return ret + 1;
}

int main()
{
    int n;
    std::cin >> n;
    std::vector<std::pair<int, int>> robot;
    robot.reserve(n);
    for (int i = 0; i < n; ++i)
    {
        std::pair<int, int> p;
        std::cin >> p.first >> p.second;
        robot.push_back(p);
    }

    std::sort(robot.begin(), robot.end());

    std::vector<std::pair<int, int>> xi;
    to.resize(n);
    for (int i = n - 1; i >= 0; --i)
    {
        while (!xi.empty() && xi.back().first < robot[i].first + robot[i].second)
        {
            to[i].push_back(xi.back().second);
            xi.pop_back();
        }
        std::pair<int, int> p(robot[i].first, i);
        xi.push_back(p);
    }

    mod_int ans = 1;
    for (int i = 0; i < xi.size(); ++i)
    {
        ans *= dfs(xi[i].second);
    }

    std::cout << ans.x << std::endl;

    return 0;
}

Measured memory_bytes: 22144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


    class Program
    {
        static void Main(string[] args)
        {
            // 一行に多重配列の引数設定
            string str = Console.ReadLine();
            string[] strArr = new string[2];
            strArr = str.Split(' ');

            //　1番目の多重配列生成
            int num1 = int.Parse(strArr[0]);
            int num2 = int.Parse(strArr[1]);
            int[,] arr1 = new int[num1,num2];

            //１番目の配列入力
            for(int i = 0; i < num1; i++)
            {
                string str2 = Console.ReadLine();
                string[] str2Arr = new string[num2];
                str2Arr = str2.Split(' ');

                for(int j = 0; j < num2; j++)
                {
                    arr1[i, j] = int.Parse(str2Arr[j]);
                }
            }

            //２番目の配列生成
            int[] arr2 = new int[num2];

            // 2番目の配列入力
            for(int i = 0; i < num2; i++)
            {
                string str3 = Console.ReadLine();
                arr2[i] = int.Parse(str3);
            }
            
            //３番目の配列生成
            int[] arr3 = new int[num1];
            
            for(int i = 0; i < num1; i++)
            {
                for(int j = 0; j < num2; j++)
                {
                    arr3[i] += arr1[i,j] * arr2[j];
                }
            }

            // 答え出力
            for(int i =0; i < num1; i++)
            {
                Console.WriteLine(arr3[i]);

            }

        }   // end main
    }   // end class




Measured memory_bytes: 17952
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <array>
#include <bitset> 
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> P;


void solve(std::string a, std::string b, std::string c){

}

bool tb[3][3][2000];
int main(){
    std::string a[3];
	size_t ret=0;
	for(int i=0;i<3;i++){
		std::cin >> a[i];
		ret+=a[i].size();
	}
	vector<size_t> ps[3][3];
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			if(i==j)continue;
			for(int k=0;k<a[i].size();k++){
				bool ok=true;
				for(int u=0;u+k<a[i].size()&&u<a[j].size();u++){
					if(a[i][u+k]!=a[j][u]&&a[i][u+k]!='?'&&a[j][u]!='?'){
						ok=false;
						break;
					}
				}
				if(ok){
					ps[i][j].push_back(k);
					tb[i][j][k]=true;
				}
			}
		}
	}
	int od[]={0,1,2};
	do{
		int i=od[0];
		int j=od[1];
		int k=od[2];
		if(ps[j][k].size()){
			ret=min(ret,a[i].size()+max(ps[j][k][0]+a[k].size(),a[j].size()));
		}
		for(int p:ps[i][j]){
			for(int q:ps[j][k]){
				if(p+q>=a[i].size()||tb[i][k][p+q]){
					ret=min(ret,max({a[i].size(),p+a[j].size(),p+q+a[k].size()}));
					break;
				}
			}
		}
		for(int p:ps[i][j]){
			for(int q:ps[i][k]){
				if(p+a[j].size()<=q){
					ret=min(ret,max(a[i].size(),q+a[k].size()));
				}
			}
		}
	}while(next_permutation(od,od+3));
	cout << ret << endl;
    return 0;
}


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

int main() {
  int N, M, X, Y;
  cin >> N >> M >> X >> Y;

  vector<int> x(N), y(M);
  rep(i, N) cin >> x[i];
  rep(i, M) cin >> y[i];

  x.push_back(X);
  y.push_back(Y);

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  if (x[N] < y[0])
    puts("No War");
  else
    puts("War");
}

Measured memory_bytes: 256
N = int(input())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))

ans = 0
pre = -10
for i in range(1, N + 1):
    ans += B[A[i]]
    if i >= 2 and pre + 1 == A[i]:
        ans += C[pre]
    pre = A[i]
print(ans)


Measured memory_bytes: 38256
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include<math.h>
#include<iomanip>
#include<functional>
using namespace std;

#define ll long long
using vt = ll; //ここで数値の型を変えられる
#define rep(i, n) for (vt i = 0; i < (vt)(n); i++)
#define reps(i, s, n) for(vt i = (vt) (s); i < (vt) (n); i++) 
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
const ll  MOD = 1000000007;

int main() {
	ll s;
	cin >> s;
	set<ll> a;
	a.insert(s);
	ll x = s;
	ll len_bfr = a.size();
	ll len_afr = 0;
	ll ans = 1;
	while (true) {
		if (x % 2 == 1) x = 3 * x + 1;
		else x /= 2;
		a.insert(x);
		ans++;
		len_afr = a.size();
		if (len_afr == len_bfr) break;
		else len_bfr = len_afr;
	}
	cout << ans;
}

Measured memory_bytes: 256
from typing import List


def answer(x1: int, y1: int, x2: int, y2: int) -> List[int]:
    x = x2 - x1
    y = y2 - y1

    x3 = x2 - y
    y3 = y2 + x
    x4 = x1 - y
    y4 = y1 + x

    return [x3, y3, x4, y4]


def main():
    x1, y1, x2, y2 = map(int, input().split())
    x3, y3, x4, y4 = answer(x1, y1, x2, y2)
    print(x3, y3, x4, y4)


if __name__ == '__main__':
    main()


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

int main(){
  int cost;
  int count = 0;
  int a[4];
  int value[] = {
    10, 50, 100, 500
  };
  while(cin >> cost && cost != 0){
    if(count++) cout << endl;
    cin >> a[0] >> a[1] >> a[2] >> a[3];
    int money = 0;
    for(int i = 0; i < 4; i++){
      money += a[i] * value[i];
    }
    int charge = money - cost;
    int r[4];
    r[3] = charge / 500;
    r[2] = charge % 500 / 100;
    r[1] = charge % 100 / 50;
    r[0] = charge % 50 / 10;
    for(int i = 0; i < 4; i++){
      a[i] = max(a[i] - r[i], 0);
    }
    for(int i = 0; i < 4; i++){
      if(a[i]) cout << value[i] << " " << a[i] << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 868
#include<stdio.h>
int LS(int X[], int n, int key){
  int i=0;
  while(i<n){
    if(X[i]==key)return 1;
    i++;
  }
  return 0;
}
int main(){
  int i, n, q, key, ans=0;
  int Y[10001];

  scanf("%d", &n);
  for(i=0 ; i<n ; i++){
    scanf("%d", &Y[i]);
  }
  scanf("%d", &q);
  for(i=0 ; i<q ; i++){
    scanf("%d", &key);
    if(LS(Y, n, key)==1)ans++;
  }
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 2144
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
double PI = 3.1415926535897932;
long mod = 1000000007;

int main() {
  int n;
  cin >> n;
  int num[10] = {0}, cnt = 0;
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if(a < 400) num[0]++;
    else if(a < 800) num[1]++;
    else if(a < 1200) num[2]++;
    else if(a < 1600) num[3]++;
    else if(a < 2000) num[4]++;
    else if(a < 2400) num[5]++;
    else if(a < 2800) num[6]++;
    else if(a < 3200) num[7]++;
    else cnt++;
  }
  
  int x = 0;
  for(int i = 0; i < 8; i++)
    if(num[i] != 0) x++;

  if(cnt == 0)
    cout << x << " " << x << endl;
  else {
    if(x == 0)
      cout << 1 << " " << cnt << endl;
    else
      cout << x << " " << x+cnt << endl;
  }
}


Measured memory_bytes: 3596
#include <stdio.h>

//5桁だけ
char isKaibun(int n){
    if(n/10000 == n%10 &&
       (n/1000)%10 == (n%100)/10) return 1;
    else return 0;
}
int main(){
    int A, B;
    scanf("%d %d",&A,&B);
    int count = 0;
    for(int n = A; n <=B; n++){
        if(isKaibun(n)) count++;
    }
    printf("%d\n",count);
}


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

int main(){
    int N;
    cin >> N;
    int A[N], x[N][15], y[N][15];
    for(int i = 0; i < N; i++){
        cin >> A[i];
        for(int j = 0; j < A[i]; j++){
            cin >> x[i][j] >> y[i][j];
            x[i][j]--;
        }
    }
    int ans = 0;
    for(int bit = 0; bit < (1<<N); bit++){
        bool sw = true;
        for(int i = 0; i < N; i++){
            if(bit & (1<<i)){
                for(int j = 0; j < A[i]; j++){
                    if((bool)(bit & (1<<x[i][j])) == !y[i][j]){
                        sw = false;
                        break;
                    }
                }
            }
            if(!sw) break;
        }
        if(sw) ans = max(ans, __builtin_popcount(bit));
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
    }
    auto ok = [&](int from, int v) {
        bool check = true;
        for (int j = from; j >= 0; j--) {
            check &= (vec[j] <= v);
        }
        return check;
    };
    int res = 1;
    for (int i = 1; i < n; i++) {
      res += ok(i - 1, vec[i]);
    }
    cout << res << '\n';
    return 0;
}


Measured memory_bytes: 256
N, M = map(int, input().split())
AB = [[int(i)-1 for i in input().split()] for _ in range(M)]

class UnionFind() :
    def __init__(self, N) :
        self.parent = [-1] * N
        self.size = [1] * N
        self.rest = N * (N - 1) // 2
        
    def root(self, x) :
        while self.parent[x] >= 0:
            x = self.parent[x]
        return x

    def unite(self, x, y) :
        rx = self.root(x)
        ry = self.root(y)

        if rx != ry :
            self.rest -= self.size[rx] * self.size[ry]
            if self.parent[rx] > self.parent[ry] :
                self.parent[rx] = ry
                self.size[ry] += self.size[rx]
            else :
                if self.parent[rx] == self.parent[ry] :
                    self.parent[rx] -= 1
                self.parent[ry] = rx
                self.size[rx] += self.size[ry]
                
ret = [0] * M
UF = UnionFind(N)
for i in range(M-1, -1, -1) :
    a, b = AB[i] 
    ret[i] = UF.rest
    UF.unite(a, b)
for i in range(M) :
    print(ret[i])

Measured memory_bytes: 75352
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


typedef long long LL;
const int SIZE = 1000001;
const int ESIZE = 101;
LL L;
bool isPrime[SIZE];
LL P[SIZE];
int PC;
int E[ESIZE]; 
int EC;
int res;

void makePrimes() {
    PC = 0;
    memset( P, false, sizeof(P) );
    for ( int i = 3; i < SIZE; i += 2 ) {
        isPrime[i] = true; 
    }
    P[PC++] = 2;
    for ( int i = 3; i < SIZE; i += 2 ) {
        if ( ! isPrime[i] ) continue;
        P[PC++] = i;
        for ( int j = i + i; j < SIZE; j += i ) {
            isPrime[j] = false;
        }
    }
}

void init() {
    memset( E, 0, sizeof(E) );
}

void calcExp( LL L ) {
    EC = 0;
    int i = 0;
    for ( ; i < PC && L > 1; ++ i ) {
        LL p = P[i];
        if ( L % p == 0 ) {
            while ( L > 1 && L % p == 0 ) {
                E[EC] ++;
                L /= p;
            }
            EC ++;
        }
    }
    if ( i >= PC ) {
        E[EC++] = 1;
    }
}

void dfs( int k, int r ) {
    if ( k >= EC ) {
        res += r;
        return;
    }
    for ( int i = 0; i < E[k]; ++ i ) dfs( k+1, r );
    dfs( k+1, r*(E[k]+1) );
}

int solve() {
    calcExp(L);
    res = 0;
    dfs( 0, 1 );
    return (res+1)/2;
}

int main() {
    makePrimes();
    while ( cin >> L && L ) {
        init();
        cout << solve() << endl;
    }
    return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define pb push_back
#define sz(x) (int)(x).size()
#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)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//グリッド：（典型）dp,dfs,bfs,最短経路,その他
int main(){
    ll n;cin>>n;
    set<string> s;
    rep(i,n){
        string ss;cin>>ss;
        s.insert(ss);
    }
    cout<<s.size()<<endl;
    return 0;
}

Measured memory_bytes: 19256
from sys import stdin

N = int(stdin.readline().rstrip())
ws = [int(x) for x in stdin.readline().rstrip().split()]

tmp1 = 0
tmp2 = 0
minv = 1000000000

for i in range(1,N):
    tmp1 = sum(ws[:i])
    tmp2 = sum(ws[i:])
    minv = min(minv, abs(tmp1-tmp2))

print(minv)

Measured memory_bytes: 3064
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define int ll
#define double long double
#define nn '\n'
#define YES {cout<<"YES"<<'\n';}
#define NO {cout<<"NO"<<'\n';}
#define Yes {cout<<"Yes"<<'\n';}
#define No {cout<<"No"<<'\n';}
#define yes {cout<<"yes"<<'\n';}
#define no {cout<<"no"<<'\n';}
void YN(bool flg){cout<<(flg?"YES":"NO")<<'\n';}
void Yn(bool flg){cout<<(flg?"Yes":"No")<<'\n';}
void yn(bool flg){cout<<(flg?"yes":"no")<<'\n';}
#define SORT(a) sort(a.begin(),a.end())
#define REVERSE(a) reverse(a.begin(),a.end())
#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 out(n) {cout << n << '\n';}
typedef vector<int> VI;
typedef vector<VI> VVI;
const int MOD = 1000000007;
const long long INF = 10e10;
const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T>auto MAX(const T& a) { return *max_element(a.begin(),a.end()); }
template<class T>auto MIN(const T& a) { return *min_element(a.begin(),a.end()); }
template<class T, class U>U SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); }
template<class T, class U>int LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); }
template<class T, class U>int UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); }
int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }
int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; }
int POW(int a, int n) { int r = 1; while (n > 0) { if (n & 1)r *= a; a *= a; n /= 2; }return r; }
int isPrime(int n){if(n<2){return 0;}for(int i=2; i*i<=n; i++){if(n%i==0){return 0;}}return 1;}
/*素因数分解*/map< int, int >prime_factor(int n){map< int, int > ret;for(int i = 2; i * i <= n; i++) {while(n % i == 0) {ret[i]++;n /= i;}}if(n != 1) ret[n] = 1;return ret;}
/*約数列挙*/vector<int> divisor(int n){vector<int> v; for(int i=1; i*i<=n; i++){if(n%i==0){v.push_back(i);if(i!=n/i){v.push_back(n/i);}}}sort(v.begin(),v.end()); return v;}
//----------------ライブラリとか----------------

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(15);

    int n; cin >> n;
    int ans = 0 , tmp = 0;
    REP(i,n){
        int a; cin >> a;
        if(a>=tmp) ans++;
        chmax(tmp,a);
    }
    out(ans);

    return 0;
}

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

int main() {
	
	int k,n;
	cin>>n>>k;
	int na[k];
	
	int n1=n;
	
	for(int i=0;i<k;i++){
	    cin>>na[i];
	}
	
	
	
	while(1){
	    int check[10]={0,0,0,0,0,0,0,0,0,0};
	    int n2 = n1;
	    int flag = 1;
	    while(n2){
	        int face=n2%10;
	        check[face]++;
	        n2=n2/10;
	        }    
	    
	    for(int a=0;a<k;a++){
	        if(check[(na[a])]!=0){flag = 0;break;}
	    }
	    
	    if(flag){cout<<n1;break;}
	    n1++;
	}
	
	return 0;
}


Measured memory_bytes: 256
fn read_stdin() -> String {
    use std::io::Read;
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    input
}

fn scan<'a, I: Iterator<Item = &'a str>, T: std::str::FromStr>(tokens: &mut I) -> T {
    tokens.next().unwrap().parse().ok().unwrap()
}

fn main() {
    let input = read_stdin();
    let tokens = &mut input.split_whitespace();
    let s: String = scan(tokens);
    let mut iter = s.chars();
    loop {
        if let Some(c) = iter.next() {
            if c != 'h' {
                println!("No");
                return;
            }
            if let Some('i') = iter.next() {
                //
            } else {
                println!("No");
                return;
            }
        } else {
            println!("Yes");
            return;
        }
    }
}


Measured memory_bytes: 4352
use proconio::input;

fn main() {
    input! {
        n: usize,
        k: i64,
        p: [usize; n],
        c: [i64; n]
    }
    println!(
        "{}",
        p.iter()
            .map(|pi| {
                let (mut curr, mut costs) = (pi, vec![0]);
                while pi != &p[curr - 1] {
                    costs.push(costs.last().unwrap() + c[curr - 1]);
                    curr = &p[curr - 1];
                }
                costs.push(costs.last().unwrap() + c[curr - 1]);
                let l = costs.len() - 1;
                if costs[l] > 0 && k > l as i64 {
                    costs[l] * (k / l as i64 - 1)
                        + costs
                            .iter()
                            .chain(
                                // I don't know anything about rust
                                costs[1..(k as usize) % l + 1]
                                    .iter()
                                    .map(|x| x + costs.last().unwrap())
                                    .collect::<Vec<_>>()
                                    .iter(),
                            )
                            .max()
                            .unwrap()
                } else {
                    0 + costs[1..std::cmp::min(k as usize, l) + 1]
                        .iter()
                        .max()
                        .unwrap()
                }
            })
            .max()
            .unwrap()
    );
}


Measured memory_bytes: 2400
import java.util.Scanner;

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

        if(a<=b){
            if(b<=c){
                System.out.printf("%d %d %d\n",a,b,c);
            }else if(a<=c){
                System.out.printf("%d %d %d\n",a,c,b);
            }else{
                System.out.printf("%d %d %d\n",c,a,b);
            }
        }else{
            if(a<=c){
                System.out.printf("%d %d %d\n",b,a,c);
            }else if(b<=c){
                System.out.printf("%d %d %d\n",b,c,a);
            }else{
                System.out.printf("%d %d %d\n",c,b,a);
            }
        }
        sc.close();
        }
}


Measured memory_bytes: 26312
#include<bits/stdc++.h>
#define endl  "\n"
#define ll long long
#define PI acos(-1.0)
#define test cout<<"\n****\n"
#define precise fixed(cout);cout<<setprecision(12)
#define fast  ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using  namespace  std;

int main(){


    ll num;
    cin>>num;

    ll count = 0;
    for(ll i=1;i<=num;i++){
        for(int j=1;j<=num;j++){

            if(i*j<num){

                count++;
            } else{
                break;
            }
        }
    }

    cout<<count;


    return 0;
}

Measured memory_bytes: 3624
#include <iostream>

using namespace std;

int main(){
    int a;
    int b;
    int c;
    int d;
    cin >> a >> b >> c >> d;
    if(abs(a - c) <= d || (abs(a - b) <= d && abs(b - c) <= d)){
        cout << "Yes" << endl; 
    }
    else{
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
#!/usr/bin/env python3
import sys, bisect
sys.setrecursionlimit(300000)


def solve(s: str, t: str):
    N = 26
    pos = [[] for _ in range(N)]
    for i, c in enumerate(s):
        pos[ord(c) - ord('a')].append(i)

    tmp = 0
    num = 0
    for C in t:
        c = ord(C) - ord('a')
        le = len(pos[c])
        if le < 1:
            print(-1)
            return
        idx = bisect.bisect_left(pos[c], tmp)
        #print(pos[c], C, idx)
        if idx < le:
            tmp = pos[c][idx] + 1
        else:
            idx = bisect.bisect_left(pos[c], 0)
            tmp = pos[c][idx] + 1
            num += 1
        #print(C, num, tmp)
    ret = num * len(s) + tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    t = next(tokens)  # type: str
    solve(s, t)

if __name__ == '__main__':
    main()


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



int main(){
    string s;
    cin >> s;
    long long  ans=0;
    int len = (int)(s.length());
    
    for(int bit=0; bit<(1 << len-1); bit++){
        string out;
        out += s[0];
        for(int i=0;i<len-1;i++){
            if(bit & (1<<i)){
                ans+=stoi(out);
                out="";
                //out+="+";
                out+=s[i+1];
            }
            else out+=s[i+1];
        }
        ans+=stoll(out);
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<iomanip>
#include<queue>
#include<stack>
#include<time.h>
//もう二度とつかわん//
//    ↓ ↓　↓　　 //
#define ll long long
//    ↑　↑　↑    //
//フェルマー方程式の恨み//
#define rep(i,n) for(int i=0;i<n;i++)
#define ggr getchar(); getchar();return 0;
using namespace std;
signed main() {
	ll a1, a2, a3, b1, b2, b3, c1, c2, c3;
	cin >> a1 >> a2 >> a3 >> b1 >> b2 >> b3 >> c1 >> c2 >> c3;
	if (a1 + b2 + c3 == a1 + b3 + c2 && a1 + b3 + c2 == a2 + b1 + c3 && a2 + b1 + c3 == a2 + b3 + c1 && a2 + b3 + c1 == a3 + b1 + c2 && a3 + b1 + c2 == a3 + b2 + c1)puts("Yes");
	else puts("No");
	ggr
}

Measured memory_bytes: 256
#include <iostream>
#include <sstream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <iomanip>
#include <string>
using namespace std;

//関数getinputdata宣言
vector<vector<string>> getinputdata();
vector<vector<int>> getinputdata2();
vector<vector<double>> getinputdata3();

void abc120_a(vector<vector<int>> v) {

    int a = v[0][0];
    int b = v[0][1];
    int c = v[0][2];

    if (a * c <= b) {
        cout << c;
    } else {

        cout << b/a;
    }

    cout << endl;


}

int main() {

    vector<vector < int>> vec_arr_result;
    //    vector<vector<int>> vec_arr_result;
    vec_arr_result = getinputdata2();

    abc120_a(vec_arr_result);

    return 0;
}

//関数getinputdata実装

vector<vector<double>> getinputdata3() {

    string str;
    string ret;
    stringstream ss;
    vector<string> v1;
    vector<vector<double>> vec_arr;

    //標準入力から入力がある間ループ処理でvector配列にデータ格納
    while (getline(cin, str)) {

        v1.push_back(str);

    }

    //stringstreamを利用してvector配列v1をスペースで分解処理
    for (string s : v1) {

        vector<double> array_data;
        ss << s;

        while (!ss.eof()) {

            ss >> ret;

            array_data.push_back(atof(ret.c_str()));
        }

        //vector配列に追加
        vec_arr.push_back(array_data);

        //バッファクリア
        ss.str("");
        //ストリームクリア
        ss.clear(stringstream::goodbit);

    }

    return vec_arr;
}

//関数getinputdata実装

vector<vector<int>> getinputdata2() {

    string str;
    string ret;
    stringstream ss;
    vector<string> v1;
    vector<vector<int>> vec_arr;

    //標準入力から入力がある間ループ処理でvector配列にデータ格納
    while (getline(cin, str)) {

        v1.push_back(str);

    }

    //stringstreamを利用してvector配列v1をスペースで分解処理
    for (string s : v1) {

        vector<int> array_data;
        ss << s;

        while (!ss.eof()) {

            ss >> ret;

            array_data.push_back(atoi(ret.c_str()));
        }

        //vector配列に追加
        vec_arr.push_back(array_data);

        //バッファクリア
        ss.str("");
        //ストリームクリア
        ss.clear(stringstream::goodbit);

    }

    return vec_arr;


}

//関数getinputdata実装

vector<vector < string >> getinputdata() {

    string str;
    string ret;
    stringstream ss;
    vector<string> v1;
    vector<vector < string>> vec_arr;

    //標準入力から入力がある間ループ処理でvector配列にデータ格納
    while (getline(cin, str)) {

        v1.push_back(str);

    }

    //stringstreamを利用してvector配列v1をスペースで分解処理
    for (string s : v1) {

        vector<string> array_data;
        ss << s;

        while (!ss.eof()) {

            ss >> ret;

            array_data.push_back(ret);
        }

        //vector配列に追加
        vec_arr.push_back(array_data);

        //バッファクリア
        ss.str("");
        //ストリームクリア
        ss.clear(stringstream::goodbit);

    }

    return vec_arr;

}


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

//using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
//typedef unsigned long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
typedef pair<int, int> P;
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
//const ll INF = LONG_LONG_MAX / 100;
//const ll INF = (1ll << 31) - 1;
const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
//typedef pair<int, int> P;
//typedef pair<double, double> P;

bool contain(set<ll> &s, ll a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
const int mod = 1000000007;
typedef priority_queue<long long, vector<long long>, greater<long long> > PQ_ASK;

int main() {
    vector<int> nums(3);
    rep(i, 3)cin >> nums[i];
    sort(nums.begin(), nums.end());
    if (nums[0] == nums[1] && nums[1] != nums[2]) {
        cout << "Yes" << endl;
    } else if (nums[0] != nums[1] && nums[1] == nums[2]) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }


}



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

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
const int inf = 1000000001;
const ll INF = 2e18 * 2;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };

int main() {
	ll x, y;
	cin >> x >> y;
	ll cnt = 0;
	while (x <= y) {
		cnt++;
		x *= 2;
	}
	cout << cnt << endl;
}

Measured memory_bytes: 256
N, M = map(int,input().split())

menu = [0 for i in range(M)]

for i in range(N):
    love = list(map(int,input().split()))
    for idx,t in enumerate(love):
        if idx == 0:
            continue
        menu[t-1] += 1

ans = 0
for i in menu:
    if i == N:
        ans += 1
print(ans)

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

using namespace std;
using ll = long long;

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

int N;
string S = "";
set<string> Slist;

void dfs(int pos) {
    if (pos == N) {
        Slist.insert(S);
        return;
    }

    char c = 'a';

    while (S.find(c) != string::npos) c++;
    c--;

    for (char i = 'a'; i <= c + 1; i++) {
        S.push_back(i);
        dfs(pos + 1);
        S.pop_back();
    }
}

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

    dfs(0);

    for (auto& x:Slist) {
        cout << x << endl;
    }

    return 0;
}


Measured memory_bytes: 14208
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <map>
#include <iomanip>
#include <limits.h>

using namespace std;    
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const bool DEBUG = true;

int main(){

    int N, K;
    cin >> N >> K;

    vector<ll> v(N);
    for(int i = 0; i < N; i++) cin >> v[i];

    sort( all(v) );

    if(K >= N) cout << 0 << endl;
    else cout << accumulate( v.begin(), v.begin() + (N - K), 0ll )  << endl;


}

Measured memory_bytes: 1792
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define pint pair<int, int>
#define mp make_pair

using namespace std;

typedef long long ll;






int N;
ll a[100005], b[100005];

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

	ll ans = 0;
	for (int i = 0; i < N; i++) {
		if (a[i] >= b[i]) {
			ans += b[i];
		}
		else {
			ans += a[i];
			b[i] -= a[i];
			if (a[i + 1] > b[i]) {
				ans += b[i];
				a[i + 1] -= b[i];
			}
			else {
				ans += a[i + 1];
				a[i + 1] = 0;
			}
		}
	}

	cout << ans << endl;


	return 0;
}


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



int main() {

  string S;
  cin >> S;

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

  string str[] = {"dream","dreamer","erase","eraser"};
  for(int i = 0;i < 4;i++) {
      reverse(str[i].begin(),str[i].end());
  }
  
  string ans = "YES";

  for(int i = 0;i < S.size();) {
      bool can = false;
      for(int j = 0;j < 4;j++) {
          int len = str[j].size();
          if(S.substr(i,len) == str[j]) {
              can = true;
              i += len;
              break;
          }
      }

      if(!can) {
          ans = "NO";
          break;
      }
  }

  cout << ans << endl;

}



Measured memory_bytes: 512
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
const int N = (int)(1e6) + 5;
pii s[N];
int v[N];
int L[N];
int R[N];
int t[N];
int dp[N];
int n;
const auto MIN = [&](int x,int y) {return min(x,y);};
const auto MAX = [&](int x,int y) {return max(x,y);};
const int mod = 1e9 + 7;
void init(int val)
{
    for (int i = 1;i <= n;++i)
        t[i] = val;
}
void U(int i,int val,auto comb)
{
    for (;i <= n;i += i&(-i))
        t[i] = comb(t[i],val);
}
int Q(int i,auto comb)
{
    int ans = t[i];
    for (i -= i&(-i);i;i -= i&(-i))
        ans = comb(ans,t[i]);
    return ans;
}
int main(void)
{
    cin>>n;
    for (int i = 1;i <= n;++i)
        cin>>s[i].se>>s[i].fi,L[i] = R[i] = i;
    sort(s + 1,s + 1 + n);
    vi ss;
    for (int i = 1;i <= n;++i)
    {
        v[i] = s[i].se;
        ss.pb(v[i]);
    }
    sort(all(ss));
    ss.resize(unique(all(ss)) - ss.begin());
    for (int i = 1;i <= n;++i)
        v[i] = lower_bound(all(ss),v[i]) - ss.begin() + 1;
    init(n);
    for (int i = 1;i <= n;++i)
    {
        U(n - v[i] + 1,i,MIN);
        L[i] = Q(n - v[i] + 1,MIN);
    }
    init(0);
    for (int i = n;i;--i)
    {
        U(v[i],i,MAX);
        R[i] = Q(v[i],MAX);
    }
    vector < pii > w;
    for (int i = 1;i <= n;++i)
        w.pb(mp(L[i],R[i]));
    sort(all(w));
    for (int i = 0;i < n;++i)
        L[i + 1] = w[i].fi,R[i + 1] = w[i].se;
    int ans = 0;
    for (int i = 1;i <= n;++i)
    {
        if (L[i] == 1)
            dp[i] = 1;
        int index = lower_bound(R + 1,R + 1 + n,L[i] - 1) - R;
        if (index <= i - 1)
            dp[i] = (1ll * dp[i] + dp[i - 1] + mod - dp[index - 1]) % mod;
        if (R[i] == n)
            (ans += dp[i]) %= mod;
        dp[i] = (dp[i] + dp[i - 1]) % mod;
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 23920
n,m = map(int,input().split())
ansl = [-1 for i in range(n)]
for _ in range(m):
    s,c = map(int,input().split())
    if s == 1 and c == 0 and n != 1:
        print(-1)
        exit()
    if ansl[s-1] == -1 or ansl[s-1] == c:
        ansl[s-1] = c
    else:
        print(-1)
        exit()
if len(ansl) == 1 and ansl[0] == -1:
    ansl[0] = 0
elif ansl[0] == -1:
    ansl[0] = 1
for idx, i in enumerate(ansl):
    if i == -1:
        ansl[idx] = 0

print("".join([str(i)for i in ansl]))


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

int main() {
  int N=0;
  cin >> N;
  //string syurui[200000]={};
  string temp="";
  //int flag=0;
  int kosuu=0;
  int i=0;
  int j=0;
  std::map<std::string, int> S;
  
  for (i = 0 /* 初期化 */ ; i < N /* 条件式 */ ; i++ /* 更新 */) {
	cin >> temp;
    /*for (j=0;j<kosuu;j++){
      if (temp == syurui[j]) {
        flag=1;
        break;
      }
    }
    if (flag == 0) {
      syurui[kosuu] = temp;
      kosuu++;
    }
    flag=0;*/
    S[temp]++;
  }
    for(auto itr = S.begin(); itr != S.end(); ++itr) {
      kosuu++;
    }
  cout << kosuu;
}

Measured memory_bytes: 19120
#include <bits/stdc++.h>
using namespace std;
const int N=4005,mod=998244353;
int n,k,C[N][N],pw2[N];
int c(int n,int m){
	if(m<0||n<0)return 0;
	return C[n][m];
}
int calc(int m,int n){return c(n+m-1,m-1);}
int Get(int v,int n,int k){
    int ans=0;
    int lim=max(0,v/2-max(0,v-1-k));
    for (int i=0;i<=lim;i++)
        ans=(1LL*pw2[i]*C[lim][i]%mod*c(n+k-2*lim-1,k-lim*2+i-1)+ans)%mod;
    return ans;
}
int main(){
    pw2[0]=1;
    for (int i=1;i<N;i++)
        pw2[i]=pw2[i-1]*2%mod;
    for (int i=0;i<N;i++)
        C[i][0]=C[i][i]=1;
    for (int i=1;i<N;i++)
        for (int j=1;j<i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
    scanf("%d%d",&k,&n);
    for (int i=2;i<=2*k;i++)
        if (i&1)
            printf("%d\n",Get(i,n,k));
        else
            printf("%d\n",(Get(i-1,n,k-1)+Get(i-1,n-1,k-1))%mod);
    return 0;
}


Measured memory_bytes: 61440
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())

#define MOD 1000000007

//逆元で二項係数を計算(MODは素数)　前処理O(n),本計算O(1)
#define MAXN 2000000
long long inv[MAXN + 1];//MODを法とする乗法の逆元
long long fact[MAXN + 1];//階乗
long long ifact[MAXN + 1];//階乗の逆元
void init() {
	inv[1] = 1;
	for (int i = 2; i <= MAXN; i++) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
	fact[0] = ifact[0] = 1;
	for (int i = 1; i <= MAXN; i++) {
		fact[i] = i * fact[i - 1] % MOD;
		ifact[i] = ifact[i - 1] * inv[i] % MOD;
	}
}
long long C(int n, int r) {
	if (n < 0 || r < 0 || r > n)return 0;
	if (r > n / 2)r = n - r;
	return fact[n] * ifact[n - r] % MOD * ifact[r] % MOD;
}

long long H(int n, int k) {
	if (n < 0 || k < 0)return 0;
	return fact[n + k - 1] * ifact[k] % MOD * ifact[n - 1] % MOD;
}

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

	int x, y;
	cin >> x >> y;

	if ((x + y) % 3 || max(x, y) > min(x, y) * 2) {
		cout << 0 << endl;
		return 0;
	}

	int n = (x + y) / 3;
	int a = x - n;
	int b = n - a;
	init();

	cout << H(a + 1, b) << endl;



	return 0;
}

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

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
    ll N, K;
    cin >> N >> K;
    ll a[N + 2], ruiseki[N + 2];
    a[0] = a[N + 1] = ruiseki[0] = ruiseki[N + 1] = 0ll;
    rep(i, 1, N + 1) {
        cin >> a[i];
        ruiseki[i] = max(0ll, a[i]);
    }

    rep(i, 1, N + 1) {
        a[i] += a[i - 1];
        ruiseki[i] += ruiseki[i - 1];
    }

    ll rsl = 0ll;
    for(int i = 0; i + K <= N; i++) {
        ll tmp = ruiseki[N] - ruiseki[i + K] + ruiseki[i];
        ll tmp2 = a[i + K] - a[i];
        tmp = max(tmp, tmp + tmp2);
        rsl = max(rsl, tmp);
    }
    cout << rsl << endl;




}


Measured memory_bytes: 1792