akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define trav(a, x) for (auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
	cin.sync_with_stdio(false);
	cin.exceptions(cin.failbit);
	int H, W, K;
	cin >> H >> W >> K;
	vector<string> board(H);
	rep(i,0,H) cin >> board[i];
	int si = -1, sj = -1;
	rep(i,0,H) rep(j,0,W) if (board[i][j] == 'S') si = i, sj = j;
	queue<pii> q;
	q.push(pii(si, sj));
	vector<vi> dists(H, vi(W, -1));
	dists[si][sj] = 0;

	vector<pii> onestep;

	while (!q.empty()) {
		pii pa = q.front();
		q.pop();
		int di = dists[pa.first][pa.second];
		if (di <= K) onestep.push_back(pa);
		rep(d,0,4) {
			pii pa2 = pa;
			if (d == 0) pa2.first++;
			if (d == 1) pa2.second++;
			if (d == 2) pa2.first--;
			if (d == 3) pa2.second--;
			if (pa2.first < 0 || pa2.second < 0 || pa2.first >= H || pa2.second >= W) continue;
			if (board[pa2.first][pa2.second] == '#') continue;
			if (dists[pa2.first][pa2.second] != -1) continue;
			dists[pa2.first][pa2.second] = di+1;
			q.push(pa2);
		}
	}

	dists.assign(H, vi(W, -1));
	trav(pa, onestep) {
		q.push(pa);
		dists[pa.first][pa.second] = 0;
	}

	while (!q.empty()) {
		pii pa = q.front();
		q.pop();
		int di = dists[pa.first][pa.second];
		if (di <= K) onestep.push_back(pa);
		rep(d,0,4) {
			pii pa2 = pa;
			if (d == 0) pa2.first++;
			if (d == 1) pa2.second++;
			if (d == 2) pa2.first--;
			if (d == 3) pa2.second--;
			if (pa2.first < 0 || pa2.second < 0 || pa2.first >= H || pa2.second >= W) continue;
			// if (board[pa2.first][pa2.second] == '#') continue;
			if (dists[pa2.first][pa2.second] != -1) continue;
			dists[pa2.first][pa2.second] = di+1;
			q.push(pa2);
		}
	}

	int res = INT_MAX;
	rep(i,0,H) rep(j,0,W) {
		if (i == 0 || j == 0 || i == H-1 || j == W-1) {
			res = min(res, dists[i][j]);
		}
	}

	cout << (res + K-1) / K + 1 << endl;

	exit(0);
}


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

typedef long long lli;
#define MX 100005
set<lli>S;
lli A[MX], B[MX];

int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        cin>>A[i];
        B[A[i]] = i;
    }

    S.insert(0);
    S.insert(n + 1);
    S.insert(B[n]);

    lli ans = 0;
    for(int i = n-1; i >= 1; i--)
    {
        S.insert(B[i]);

        //int a, b, c, d;
        //a = b = c = d = 0;
        auto it = S.lower_bound(B[i]);
        it--;
        int a = *it;

        auto it2 = S.lower_bound(a);
        it2--;
        int b = *it2;

        int c = (a == 0)? 0 : b;

        auto it3 = S.upper_bound(B[i]);
		int d = *it3;

		auto it4 = S.upper_bound(d);
		int f = *it4;

		int e = (d == n+1)? n+1 : f;
		ans += (B[i] - a) * (e - d) * i;
		ans += (d - B[i]) * (a - c) * i;
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 6528
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9+7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(int)(n); (i)++)
int dx[4]={ 1,0,-1,0 };
int dy[4]={ 0,1,0,-1 };

int N;
int ans = 0;
void calc(string S, int k){
    if(S.size() == N){
        cout << S << endl;
        return;
    }
    rep(i, k) calc(S+(char)('a'+i), k);
    calc(S+(char)('a'+k), k+1);
}

signed main(){
    cin >> N;
    calc("", 0);
}

Measured memory_bytes: 1536
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

int main(void){
	ll n,m;
	cin>>n>>m;
	ll a;
	vector<ll> A;
	rep(i,n){
		cin>>a;
		A.push_back(a);
	}

	vector<pair<ll,ll>> kukan;//pair<ll,ll> 左端, 右端
	rep(i,n-1){
		if(A[i] < A[i+1]){
			kukan.push_back(pair<ll,ll>{A[i]+m,A[i+1]+m});
			kukan.push_back(pair<ll,ll>{A[i],A[i+1]});
		}else{
			kukan.push_back(pair<ll,ll>{A[i],A[i+1]+m});
			kukan.push_back(pair<ll,ll>{A[i]+m,A[i+1]+m*2});
		}
	}
	sort(all(kukan));

	//pair<ll,ll> : 右端,左端
	priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> que;
	
	ll val = 0;//現在の明るさtを挟む区間の個数
	int ki = 0;
	ll toku = 0;//お気に入りによって得した量
	ll max_toku = 0;
	rep(t,2*m+3){
		toku += val;
		chmax(max_toku, toku);//明るさtをお気に入りにした時に得した量(手数圧縮量) = toku になってる.
		while(que.size() > 0 && que.top().first == t){
			pair<ll,ll> top = que.top();
			val--;
			toku -= top.first - top.second - 1;//お気に入りがその区間から外れることで, 今まで得とカウントしてた量は無効
			que.pop();
		}
		while(ki < kukan.size() && kukan[ki].first == t){
			que.push(pair<ll,ll>{kukan[ki].second, kukan[ki].first});
			val++;
			ki++;
			toku -= 1;//t+1をお気に入りにしても, まだ得はしないので.
		}
		
		//cout<<toku<<" ";
		//cout<<que.size()<<" ";
	}
	//cout<<endl;
	ll ans = 0;
	rep(i,A.size()-1){//手数圧縮しなかった場合にかかる手数
		if(A[i] < A[i+1]) ans += A[i+1] - A[i];
		else ans += A[i+1] - A[i] + m;
	}
	ans -= max_toku;
	cout<<ans<<endl;
	//cout<<toku<<endl;
	//cout<<max_toku<<endl;
	return 0;
}


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

int main() {
    int n,m,x;
    int can = false;
    cin >> n >> m >> x;
    int A[n][m+1];
    for (int i=0;i<n;i++){
        for (int j=0;j<m+1;j++){
            cin >> A[i][j];
        }
    }
    int ans=1e9;
    //2^n通りをbit全探索
    for(int bit = 0;bit<(1<<n);++bit){
        int sum=0;
        vector<bool> which(n);
        vector<int> us(m);
        for(int i=0;i<n;++i){
            if(bit & (1<<i)){
                which.at(i) = true;
            }
            // which.at(i)がtrueなら買う
            if(which.at(i)==1){
                for(int j=0;j<m;j++){
                    us.at(j)+=A[i][j+1];
                }
            }
        }
        // for (int i=0;i<m;i++){
        //     cout << us.at(i);
        // }
        // cout << endl;
        int cnt=0;
        for(int i=0;i<m;i++){
            if(us.at(i)>=x) cnt++;
        }
        //全部超えてたら
        if(cnt==m){
            can =true;
            for(int i=0;i<n;i++){
                if(which.at(i)==1){
                    sum+=A[i][0];
                }
            }
            ans = min(ans,sum);
        }

    }
    if(can){
        cout << ans << endl;
    }
    else cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 3632
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
typedef long long ll;
const int MAX_N=100001;
vector<pair<int, int> > edge;
ll N,M;
int par[MAX_N];  //親
ll sizes[MAX_N]; //木の深さ
void init(int n)
{
    for (int i = 0; i < n; i++)
    {
        par[i] = i;
        sizes[i] = 1;
    }
}
//木の根を求める
int find(int x)
{
    if (par[x] == x)
    {
        return x;
    }
    else
    {
        return par[x] = find(par[x]);
    }
}
//併合
void unite(int x, int y)
{
    x = find(x);
    y = find(y);
    if(x==y) return;
    if(sizes[x] > sizes[y]) swap(x, y); // merge technique
    par[y] = x;
    sizes[x] += sizes[y];
    }

bool same(int x,int y){
    return find(x)==find(y);
}

void solve(){
    init(N);
    ll ans[MAX_N];
    ans[M-1]=(ll)(N*(N-1)/2);
    for(int i=M-1;i>0;i--){
        int a=edge[i].first;
        int b=edge[i].second;
        if(!same(a,b)){
        ans[i-1]=ans[i]-(ll)(sizes[find(a)]*sizes[find(b)]);
        unite(a,b);
        }
        else ans[i-1]=ans[i];
    }
    for(int i=0;i<M;i++) cout<<ans[i]<<'\n';
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for(int i=0;i<M;i++) {
        int a,b;
        cin>>a>>b;
        a=a-1;
        b=b-1;
        edge.push_back(make_pair(a,b));
    }
    solve();
    return 0;
}


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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);  
	bool p[100005];
	fill(p, p + 100005, true);
	p[0] = p[1] = false;
	for(int i = 2; i < 100005; i++) {
		if(p[i]) {
			for(int j = i * 2; j < 100005; j += i) {
				p[j] = false;
			}
		}
	}
	vector<int> a(100005);
	for(int i = 0; i < 100005; i++) {
		if(p[i] && p[(i + 1) / 2]) {
			a[i] = a[i - 1] + 1;
		}
		else{
			a[i] = a[i - 1];
		}
	}
	int q;
	cin >> q;
	for(int i = 0; i < q; i++) {
		int l, r;
		cin >> l >> r;
		cout << a[r] - a[l - 1] << endl;
	}
	return 0;	
}


Measured memory_bytes: 1152
#include <iostream>
using namespace std;
int main() {
	int x, y, z;
	cin >> x >> y >> z;
	
	if ((x >= y) && (y >= z)) {
		cout << z << " " << y << " " << x << endl;
	} else if ((y >= x) && (x >= z)) {
		cout << z << " " << x << " " << y << endl;
	} else if ((x >= z) && (z >= y)) {
		cout << y << " " << z << " " << x << endl;
	} else if ((y >= z) && (z >= x)) {
		cout << x << " " << z << " " << y << endl;
	} else if ((z >= y) && (y >= x)) {
		cout << x << " " << y << " " << z << endl;
	} else if ((z >= x) && (x >= y)) {
		cout << y << " " << x << " " << z << endl;
	} else if ((x = y) && (y = z)) {
		cout << x << " " << y << " " << z << endl;
	}
	return 0;
}



Measured memory_bytes: 3084
r, g, b  = map(int, input().split())
k = int(input())

flag=0
for i in range(k):
    if g<=r:
        g=g*2
    elif b<=g:
        b=b*2
    else:
        break
    
if b>g and g>r:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 61644
#include <bits/stdc++.h>
#define int long long 
using namespace std;
int32_t main(){
    int a,b;cin>>a>>b;
    int tmp=0;
    for(int i=0;i<b;i++){
        tmp*=10;
        tmp+=a;
    }
    stringstream ss;
    ss<<tmp;
    string str;
    ss>>str;
    tmp=0;
    for(int i=0;i<a;i++){
        tmp*=10;
        tmp+=b;
    }
    ss.str("");
    ss.clear();
    string str2;
    ss<<tmp;
    ss>>str2;
    if(str<str2)cout<<str<<endl;
    else cout<<str2<<endl;
	return 0;
}

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

signed main() {
  cin.tie(0); ios::sync_with_stdio(false);
  int n, k; cin >> n >> k;
  vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
  sort(a.begin(), a.end());
  vector< vector<bool> > dp1(n + 1, vector<bool>(k, false));
  vector< vector<bool> > dp2(n + 1, vector<bool>(k, false)); 
  dp1[0][0] = dp2[n][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      dp1[i + 1][j] = dp1[i][j] or j - a[i] >= 0 and dp1[i][j - a[i]];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < k; j++) {
      dp2[i][j] = dp2[i + 1][j] or j - a[i] >= 0 and dp2[i + 1][j - a[i]];
    }
  }
  auto needed = [&](int i) {
    if (a[i] >= k) return true;
    int x = 0, y = k - 1;
    while (true) {
      while (x < k and not dp1[i][x]) x++;
      if (x == k) break;
      while (x + y >= k - a[i]) {
        if (y >= 0 and dp2[i + 1][y]) return true;
        y--;
      }
      x++;
    }
    return false;
  };
  int l = -1, r = n;
  while (true) {
    int m = (l + r) / 2;
    if (needed(m)) r = m;
    else l = m;
    if (r - l == 1) break;
  }
  cout << r << '\n';
  return 0;
}

Measured memory_bytes: 6912
# include <iostream>
# include <string>
# include <cstring>
using namespace std;
int main ()
{
	string a, b;
	int tot = 0;
	cin >> a >> b;
	int len = a.size();
	for (int i = 0; i < len; i ++)
	{
		if (a[i] != b[i])
		{
			tot ++;
		}
	}
	cout << tot;
	return 0;
}

Measured memory_bytes: 3820
#include<bits/stdc++.h>
using namespace std;
int a[55];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	double d=1.0*(a[0]+a[1])/2;
	for(int i=2;i<n;i++)
	{
		d=1.0*(d+a[i])/2;
	}
	cout<<d;
	return 0;
}


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

int main() {
  ll n,h;
  cin >> n >> h;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i,n){
    cin >> a[i] >> b[i];
  }
  sort(b.begin(),b.end(),greater<ll>());
  sort(a.begin(),a.end(),greater<ll>());
  ll s;
  rep(i,n){
    if(a[0]>b[i]){
      s = i;
      break;
    }
  }
  int cnt=0;
  int i=0;
  while(h>0){
    if(s>i&&i<n){
      h-=b[i];
      i++;
    }
    else
      h-=a[0];
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Measured memory_bytes: 4832
use proconio::input;

fn main() {
    input! {
        x: usize,
        y: usize,
    };
    let mut sum = 0;
    sum += match x {
        1 => 300_000,
        2 => 200_000,
        3 => 100_000,
        _ => 0,
    };
    sum += match y {
        1 => 300_000,
        2 => 200_000,
        3 => 100_000,
        _ => 0,
    };
    sum += if x == 1 && y == 1 { 400_000 } else { 0 };
    let ans = sum;
    println!("{}", ans);
}


Measured memory_bytes: 2164
a, b, c = map(int, input().split())

desc = sorted([a,b,c], reverse=True)
d1, m1 = divmod(desc[0] - desc[1], 2)
d2, m2 = divmod(desc[0] - desc[2], 2)

ans = d1 + d2
if m1 + m2 == 2:
  ans += 1
elif m1 + m2 == 1:
  ans += 2

print(ans)

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

int N;
vector<vector<bool>> if_road;
vector<bool> if_came;

void move(int now){
    if_came[now] = true;
    for(int i = 0; i < N; i++){
        if(if_road[now][i] && !if_came[i]){
            move(i);
        }
    }
}

int main(){
    int M;
    cin >> N >> M;
    if_road.resize(N);
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if_road[i].push_back(false);
        }
    }
    int roads[M][2];
    for(int i = 0; i < M; i++){
        cin >> roads[i][0] >> roads[i][1];
        if_road[roads[i][0] - 1][roads[i][1] - 1] = true;
        if_road[roads[i][1] - 1][roads[i][0] - 1] = true;
    }
    if_came.resize(N);
    int ans = 0;
    for(int i = 0; i < M; i++){
        if_road[roads[i][0] - 1][roads[i][1] - 1] = false;
        if_road[roads[i][1] - 1][roads[i][0] - 1] = false;
        fill(if_came.begin(), if_came.end(), false);
        move(0);
        bool if_ok = true;
        for(int j = 0; j < N; j++){
            if(!if_came[j]){
                if_ok = false;
                break;
            }
        }
        ans += !if_ok;
        if_road[roads[i][0] - 1][roads[i][1] - 1] = true;
        if_road[roads[i][1] - 1][roads[i][0] - 1] = true;
    }
    cout << ans << endl;
}


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

while len(v) > 1:
    v_tmp = (v[0] + v[1]) / 2
    v.append(v_tmp)
    v.sort()
    v.remove(v[0])
    v.remove(v[1])

print(*v)

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

#define rep(i, n) for (int i = 0; i < (int) (n); i++)
#define Rrep(i,n) for(int i=((int)n-1); i>=0; i--)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define INF 2e15
#define MOD 10e9+7

typedef vector<int> vint;
typedef long long ll;



int main()
{
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(15);

        ll n;
        cin>>n;
        string c;
        cin>>c;

        ll cnt_r=0;
        ll cnt_w=0;

        for(ll i=0;i<n;i++){
                if(c[i]=='W'){
                        cnt_w++;
                }
        }
        for(ll i=n-cnt_w;i<n;i++){
                if(c[i]=='R'){
                        cnt_r++;
                }
        }
        cout<<cnt_r<<endl;

        return 0;
}


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

int main(){
    int n;
    long long zmi=2e9,zma=-2e9,wmi=2e9,wma=-2e9;
    cin>>n;
    while(n--){
	    long long x,y;
	    cin>>x>>y;
	    zmi=min(zmi,x+y);
	    zma=max(zma,x+y);
	    wmi=min(wmi,x-y);
	    wma=max(wma,x-y);
    }
    cout<<max(zma-zmi,wma-wmi)<<"\n";
}




Measured memory_bytes: 3620
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100001;
int n, val[maxn];
bool used[maxn];
long long ans;
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for(int i = 0;i < n;++i)cin >> val[i];
	--n;
	for(int c = 1;c < n;++c){
		long long sum = 0;
		for(int k = 1;(k + 1) * c < n and !used[c * k] and !used[n - c * k] and (((c * k) << 1) != n);++k){
			ans = max(sum += val[c * k] + val[n - c * k], ans);
			used[c * k] = used[n - c * k] = true;
		}
		// for(int k = 1;k * c < n and (used[c * k] || used[n - c * k]);++k){
		// 	used[c * k] = used[n - c * k] = false;
		// }
		memset(used, 0, sizeof(used));
	}
	cout << ans << '\n';
}

Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define REP2(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(v) (v).begin(),(v).end()
#define INF 2e9
typedef long long ll;

int main()
{
    int n,m;
    cin>>n>>m;
    vector<int> k(m),p(m);
    vector<vector<int> > s(m,vector<int>(n));
    REP(i,m){
        cin>>k[i];
        REP(j,k[i]) cin>>s[i][j];
    }
    REP(i,m) cin>>p[i];

    int v=0;
    REP(i,pow(2,n)){
        bool f=1;
        REP(j,m){
            int on=0;
            REP(l,k[j]){
                if(i/int(pow(2,s[j][l]-1))%2) on++;
            }
            if(on%2!=p[j]) f=0;
        }
        if(f==1) v++;
    }
    cout<<v<<endl;

	return 0; 
}

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

int main() {
    
  int N,K;
  cin >>N;
  cin >>K;
  int sum=1;
  for(int i=0;i<N;i++){
    int a=sum;
    int b=sum;
    a*=2;
    b+=K;
    if(a>b){
      sum=b;
    }
    else{
      sum=a;
    }
  }
  cout<<sum<<endl;

}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<cfloat>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(int i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<=n;i++)
#define RIP(i,a,b) for(int i=a;i<b;i++)
#define all(V) V.begin(),V.end()
#define sec  setprecision;
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops");
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
using namespace std;
using ll = long long;
using ld = long double;
using Pa = pair<ll,ll>;
ll mod = 1000000007;
ll MOD = 998244353;
ll gcd(ll a, ll b)
{
	if (a % b == 0)	return(b);
	else return(gcd(b, a % b));
}
ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
ll pow(ll x, ll n) {
	x %= mod;
	if (n == 0)
		return 1;
	if (n % 2 == 0)
		return (pow((x * x) % mod, n / 2) % mod);
	else
		return (x * pow(x, n - 1)) % mod;
}
bool prime(int n) {
	int i = 2;
	if (n == 2)return true;
	else if(n <= 1)return false;
	if (n % i == 0)return false;
	i = 3;
	while (i <= sqrt(n)) {
		if (n % i == 0)return false;
		i += 2;
	}
	return true;
}
ll nextprime(ll sosuu, vector<bool>a) {
	sosuu++;
	while (a[sosuu] == false)sosuu++;
	return sosuu;
}
vector<bool> primes(ll n) {
	vector<bool>a(n, true);
	ll sosuu = 2;
	ll j = 2;
	while (sosuu * j < n)a[sosuu * j] = false, j++;
	while (sosuu < sqrt(n)+3) {
		j = 2;
		sosuu = nextprime(sosuu, a);
		while (sosuu * j < n)a[sosuu * j] = false, j++;
	}
	return a;
}
const int MAX = 510000;
ll 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;
	}
}
ll 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;
}
vector<ll>primess(ll n) {
	vector<ll>a;
	while (n % 2 == 0)n /= 2, a.push_back(2);
	ll sosuu = 3;
	while (n != 1) {
		if (n % sosuu == 0)n / sosuu, a.push_back(sosuu);
		else sosuu += 2;
	}
	return a;
}
signed main() {
	ll n, d; cin >> n >> d;
	vector<Pa>x(n);
	rip(i, n)cin >> x[i].first >> x[i].second;
	int ans = 0;
	rip(i, n) {
		ll dis = x[i].first * x[i].first + x[i].second * x[i].second;
		if (dis <= d * d)ans ++ ;
	}
	cout << ans << endl;
}


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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
 public:
  int l;
  Solver(LL l) : l(l){};

  void solve() {
    vector<vector<int>> ans;
    int d = 0;
    int l2 = l;
    while (l2 > 0) {
      d++;
      l2 /= 2;
    }
    DBG(d)
    int n = d;
    cout << n << ' ';
    for (int i = 0; i < n - 1; ++i) {
      ans.push_back({i, i + 1, 0});
      ans.push_back({i, i + 1, (1 << i)});
    }
    int sum = (1 << (n - 1));
    DBG(ans)
    for (int i = 0; i < n - 1; ++i) {
      if ((l >> i) & 1) {
        ans.push_back({i, n - 1, sum});
        sum += (1 << i);
      }
    }
    cout << ans.size() << endl;
    for (auto &&v : ans) {
      cout << v[0] + 1 << ' ' << v[1] + 1 << ' ' << v[2] << endl;
    }
  }
};
}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


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


int main()
{
	int v[4][3][10] = {0};
	int n;
	int b, f, r, vin;
	
	cin >> n;
	
	for(int i=0; i<n; i++){
		cin >> b >> f >> r >>vin;
		v[b-1][f-1][r-1] += vin;
	}
	
	for(b=0; b<4; b++){
		for(f=0; f<3; f++){
			for(r=0; r<10; r++)
				cout << ' ' << v[b][f][r];
			cout <<endl;
		}
		if(b < 3)
			cout << "####################\n";
	}
	
	return 0;
}


Measured memory_bytes: 3080
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
int main(){
    int n;
    cin >> n;
    long long a[n],b[n];
    long long ans = 0;
    for(int i = 0;i<n;i++){
        cin >> a[i] >> b[i];
    }
    for(int i = n-1;i>=0;i--){
        if((a[i] + ans) % b[i] != 0){
            ans = ans + (b[i] - (a[i] + ans) % b[i]); 
        }
    }
    cout << ans << endl;
}

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

int main() {
  int a,b,c,x,y;
  //入力
  cin >> a >> b >> c >> x >> y;
 
  //処理
  int ans=INT_MAX;

  for(int i = 0;i<2*(x+y);i++){
    ans=min(ans,max(0,x-i/2)*a + max(0,y-i/2)*b+c*i);
  }

  //出力
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3604
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    for(int i=0;i<3;i++)
    {
        if(s[i]==s[i+1])
        {
            cout<<"Bad";
            return 0;
        }
    }
    cout<<"Good";
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define FORL(i,x) for(int i=head[x];i;i=nxt[i])
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define PB push_back
#define MP make_pair
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; }


int main() {
    int input;
    cin >> input;
    FOR(i,1,50000){
      double n = floor((double)i * 1.08);
      if(n==input) {
        cout << i << endl;
        return 0;
      }
    }
    cout << ":(" << endl;


}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

string s;
int k;
int dp[501][5][2];

int main(void){
	cin >> s;
	scanf("%d",&k);
	dp[0][0][0]=1;
	int n=s.size();
	for(int i=0;i<n;i++){
		for(int j=0;j<=k;j++){
			for(int l=0;l<2;l++){
				for(int a=0;a<=9;a++){
					if(l==1){
						dp[i+1][j+(a!=0?1:0)][l]+=dp[i][j][l];
					}else if((s[i]-'0')>a){
						dp[i+1][j+(a!=0?1:0)][1]+=dp[i][j][l];
					}else if((s[i]-'0')==a){
						dp[i+1][j+(a!=0?1:0)][0]+=dp[i][j][l];
					}
				}
			}
		}
	}
	printf("%d\n",dp[n][k][0]+dp[n][k][1]);
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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; }

typedef long long ll;
typedef pair<ll, ll> p;


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  double n;
  cin >> n;
  cout << ceil(n / 2.0) << endl;

}


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

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

  double a, b;
  cin >> a >> b;
  cout << ceil((a + b) / 2) << endl;
}


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

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9+7)
#define INF (1LL<<60)
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define rep1(i,n) for(ll i = 1; i <= (n); i++)

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


// 最大公約数
ll gcd(ll a, ll b)
{
  if(b == 0) return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b; swap(a, b);
    u -= t * v; swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n)
{
  ll s = sqrt(n);
  ll r = 0;
  for(ll i = 2; i <= s; i++) {
    if((n % i) == 0) {
      r = 0;
      do {
	r++;
	n = n / i;
      } while((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if(n > s) {
    prim.push_back({n, 1});
  }
}

void solve()
{
  ll N; cin >> N;
  // ll N, K; cin >> N >> K;
  // ll x, y, z; cin >> x >> y >> z;
  // ll a, b, c; cin >> a >> b >> c;
  // string s; cin >> s;
  // vector<ll> a(N); rep(i, N) cin >> a[i];
  
  // ll ans = 0;
  bool ok = N >=30;

  // cout << ans << endl;
  cout << (ok?"Yes":"No") << endl;
}


int main(void)
{
  // ll t; cin >> t; rep(i, t)
  solve();
}


Measured memory_bytes: 3624
import sys

sa = [ord(c)- ord("a") for c in input()]
sb = [ord(c)- ord("a") for c in input()]
sc = [ord(c)- ord("a") for c in input()]
card_lst = [sa, sb, sc]

player = 0

while True:
    cards = card_lst[player]
    if len(cards) == 0:
        print(chr(ord("A") + player))
        sys.exit()

    card_lst[player] = cards[1:]
    player = cards[0]

Measured memory_bytes: 3064
#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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b)for(int i=(a);i<=(b);i++)
#define repR(i,n) for(int i=n;i>=0;i--)
#define P pair<int, int>
#define sz(x) x.size()
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define VE vector<int>
#define COUT(x) cout<<(x)<<endl
#define MA map<int,int>
#define SE set<int>
#define PQ priority_queue
#define COUT(x) cout<<(x)<<endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl

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;
}

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;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
	if (a.second != b.second) {
		return a.second < b.second;
	}
	else {
		return a.first < b.first;
	}
}

const long long MOD = 1000000007;
const long long INF = 1LL << 50;

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



signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int x, y; cin >> x >> y;
	int a[7] = { 1,3,5,7,8,10,12 };
	int b[4] = { 4,6,9,11 };
	int c[1] = { 2 };
	bool ok = false;
	bool ok2 = false;
	rep(i, 7) {
		if (x == a[i])ok = true;
		if (y == a[i])ok2 = true;
	}
	if (ok&&ok2) {
		cout << "Yes" << endl; return 0;
	}
	ok = false; ok2 = false;
	rep(i, 4) {
		if (x == b[i])ok = true;
		if (y == b[i])ok2 = true;
	}
	if (ok&&ok2) {
		cout << "Yes" << endl; return 0;
	}
	ok = false; ok2 = false;
	rep(i, 1) {
		if (x == c[i])ok = true;
		if (y == c[i])ok2 = true;
	}
	if (ok&&ok2) {
		cout << "Yes" << endl; return 0;
	}
	cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 384
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

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 (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

struct UnionFind {
  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)];
  }
};

int n, q;
const int BLOCK = 255;
main(){
	ios::sync_with_stdio(false);
	while(cin >> n >> q, n){
		vector<vi> g(n);
		vi par(n);
		REP(i, n-1){
			int p; cin >> p; p--;
			g[p].push_back(i+1);
			par[i+1] = p;
		}
		ll sum = 0;
		vector<pii> query;
		vi marked(n);
		REP(i, q){
			char c;
			int v;
			cin >> c >> v; v--;
			if(c == 'Q'){
				query.emplace_back(1, v);
			}else{
				if(marked[v] == 0){
					marked[v] = i+1;
					query.emplace_back(0, v);
				}
			}
		}
		UnionFind uf(n);
		queue<int> que;
		que.push(0);
		while(!que.empty()){
			int v = que.front();que.pop();
			if(marked[v] == 0) uf.unionSet(par[v], v);
			FOR(u, g[v]) que.push(*u);
		}
		RREP(i, query.size()){
			int v = query[i].second;
			if(query[i].first) sum += 1 + uf.root(v);
			else uf.unionSet(par[v], v);
		}
		cout << sum << endl;
	}
	return 0;
}

Measured memory_bytes: 9092
n,m=map(int,input().split())
s=input()
t=input()
lens=len(s)
lent=len(t)
from fractions import gcd
g=gcd(lens,lent)
for i in range(g):
    if s[i*(lens//g)]!=t[i*(lent//g)]:
        print(-1)
        break
else:
    print(lens*lent//g)

Measured memory_bytes: 5304
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define vl vector <long>
#define vll vector < ll >
#define pll pair < ll , ll >
#define pb push_back
#define mp make_pair
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MODCONST 1000000007
#define pinf 1e+18L+7
#define ninf 1e-18L+7

using namespace std;

void solve()
{
    typedef struct{
        string s;
        ll l;
    } object;
    string s,t;
    cin>>s>>t;
    ll lens=s.length(),lent=t.length();
    // cout<<lens<<lent<<endl;
    ll dp[lens+1][lent+1];
    // dp init
    // dp[0][0].s = "";
    rep(i,0,lens+1){
        dp[i][0] = 0;
    }
    rep(i,0,lent+1){
        dp[0][i] = 0;
    }
    // dp form
    rep(i,1,lens+1){
        rep(j,1,lent+1){
            if(s[i-1]==t[j-1]){
                dp[i][j] = dp[i-1][j-1]+1;
                // dp[i][j].s = dp[i-1][j-1].s+s[i-1];
            }else{
                if(dp[i-1][j]>=dp[i][j-1]){
                    dp[i][j] = dp[i-1][j];
                    // dp[i][j].s = dp[i-1][j].s;
                }else{
                    dp[i][j] = dp[i][j-1];
                    // dp[i][j].s = dp[i][j-1].s;
                }
            }
        }
    }
    string ans = "";
    ll i=lens,j=lent;
    while(dp[i][j]!=0){
        if(s[i-1]==t[j-1]){
            ans = s[i-1]+ans;
            i--;
            j--;
        }else{
            if(dp[i-1][j]>=dp[i][j-1]){
                i--;
            }else{
                j--;
            }
        }
    }
    cout<<ans<<endl;
}

int main()
{
    fastio ;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll tc=1;
    //cin>>tc; //for number of testcases
    //ll ts = tc;
    while(tc--){
        //cout<<"Case #"<<ts-tc<<": ";
        solve();
    }
    return 0;
}


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

int main(){
  long long Mod=1000000007;
  string s;
  cin >> s;
  int n=s.size();
  long long a[13];
  long long b[13];  
  a[0]=1;
  for(int j=1; j<13; j++) a[j]=0;
  for(int j=0; j<13; j++) b[j]=0;
  
  for(int i=0; i<n; i++){
    if(s.at(i) == '?'){
      //cout << "?";
      for(int p=0; p<13; p++){
        for(int q=0; q<10; q++){
          b[(10*p+q)%13] += a[p];
        }
      } 
    } else {
      int q=s.at(i)-48;
      for(int p=0; p<13; p++){
        b[(10*p+q)%13] = a[p];
      } 
    }
    for(int j=0; j<13; j++){
      a[j]=(b[j])%Mod;
      b[j]=0;
    }
  }
  cout << a[5];
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int n,e;
int color[10001];
int indegree[10001];
vector<int> ans;
vector<int> vec[10001];
void Topological(int s){
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        int u=que.front();que.pop();
        ans.push_back(u);
        color[u]=1;
        for(int i=0;i<vec[u].size();++i){
            indegree[vec[u][i]]--;
            if(indegree[vec[u][i]]==0&&color[vec[u][i]]==0){
                que.push(vec[u][i]);
            }
        }
    }
}
int main(){
    scanf("%d %d",&n,&e);
    for(int i=0;i<e;++i){
        int u,v;
        scanf("%d %d",&u,&v);
        vec[u].push_back(v);
        indegree[v]++;
    }
    for(int i=0;i<n;++i){
        if(indegree[i]==0&&color[i]==0)
        Topological(i);
    }
    for(int i=0;i<n;++i){
        printf("%d\n",ans[i]);
    }
    return 0;
}



Measured memory_bytes: 3688
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>

int GetGCD(int a, int b){
  int maxval = std::max(a, b);
  int minval = std::min(a, b);
  if (maxval%minval==0){
    return minval;
  } else {
    return GetGCD(minval, maxval%minval);
  }
}

int main(){

  int N;
  scanf("%d",&N);
  std::vector<int > A(N);
  for (int i=0; i<N; i++){
    scanf("%d",&A[i]);
  }
  int gcd = A[0];
  for (int i=0; i<N; i++){
    gcd = GetGCD(gcd, A[i]);
  }
  printf("%d\n",gcd);
  return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
#include<cassert>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
#define ceil(a, b) a / b + !!(a % b)

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<tuple<int, int, int> > v(n - 1);
    rep(i, n - 1) { //n - 1!!
        int c, s, f; cin >> c >> s >> f;
        get<0>(v[i]) = c;
        get<1>(v[i]) = s;
        get<2>(v[i]) = f;
    }
    rep(i, n - 1) {
        int t = get<1>(v[i]);
        rrep(j, i, n - 1) {
            int c = get<0>(v[j]);
            int s = get<1>(v[j]);
            int f = get<2>(v[j]);
            if (t < s) {
                t = s;
            } else if (t > s) { //ちょうど割り切れるように
                int tmp = f;
                while (tmp < t) {
                    tmp += f;
                }
                t = tmp;
            }
            t += c;
        }
        cout << t << endl;
    }
    cout << 0 << endl;
}


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

#define N 20

int n,A[N],q,m;
bool ans;
 
void dfs(int x,int s){
  if(x==n){
    if(s==m) ans=1;
    return;
  }
  dfs(x+1,s+A[x]);
  dfs(x+1,s);
}
 
int main(){
  cin>>n;
  for(int i=0;i<n;i++) cin>>A[i];
  cin>>q;
  while(q--){
    cin>>m;
    ans=0;
    dfs(0,0);
    if(ans)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

Measured memory_bytes: 3096
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
const int MOD=1e9+7;
typedef long long ll;
typedef pair<ll,ll> P;

const ll MAX_N=1e18;

ll N;

//ll memo[1000][1000];
map<P,ll> memo;

ll dfs(ll s,ll t){
	if (s==0 && t==0){
		return 1;
	}
	if (s<0 || t<0){
		return 0;
	}
	if (memo.find(P(s,t))!=memo.end()){
		return memo[P(s,t)];
	}
	ll res=dfs(s/2,t/2);
	if (s>=1 && t>=1){
		res+=dfs((s-1)/2,(t-1)/2);
		res%=MOD;
	}
	if (s>=2){
		res+=dfs((s-2)/2,t/2);
		res%=MOD;
	}
	return 	memo[P(s,t)]=res;
}

int main(){
//	memset(memo,-1,sizeof(memo));
	scanf("%lld",&N);
	printf("%lld\n",dfs(N,N));
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rrep(i, n) for(int i=n-1; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
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; }

int n;
ll m;
vector<ll> a;

int main() {
    cin >> n >> m;
    a.resize(n);
    rep(i, n) cin >> a[i];
    sort(ALL(a));

    vector<ll> v(n+1);
    v[0] = 0;
    rep(i, n) v[i+1] = v[i] + a[i];

    ll ok = 0, ng = 1e10;
    ll ans = 0;
    while (ng - ok > 1) {
        ans = 0;
        ll mid = (ok + ng) / 2;
        ll sum = 0;
        rep(i, n) {
            int d = lower_bound(ALL(a), mid - a[i]) - a.begin();
            ans += (v[n] - v[d] + a[i] * (n - d));
            sum += n - d;
        }
        if (sum < m) ng = mid;
        else ok = mid;

        ans -= ok * (sum - m);
    }
    cout << ans << endl;
}


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

using ll=long long;

int main() {
  string S; cin >>S;
  if (S.size()==3) {
    reverse(S.begin(),S.end());
  }
  cout <<S <<endl;
  return 0;
}


Measured memory_bytes: 3616
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n, m;
int a[64], b[64];
set<int> G[10];

bool check(const vector<int>& idx) {
    for (int i = 0; i < n-1; i++) {
        if (!G[idx[i]].count(idx[i+1]))
            return false;
    }
    return true;
}

int solve() {
    for (int i = 0; i < m; i++) {
        G[a[i]].insert(b[i]);
        G[b[i]].insert(a[i]);
    }

    vector<int> idx(n);
    for (int i = 0; i < n; i++) idx[i] = i;
    int res = 0;
    do {
        if (idx[0] == 0)
            res += check(idx);
    } while (next_permutation(ALL(idx)));
    
    return res;
}

void input() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
    }
}

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

    input();
    cout << solve() << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using ld = long double;
const ll ATCODER_AMARI = 1000000007; //10^9 + 7
const ll INF = 10000000000000;

//階乗関数　ATCODER_AMARIに注意
ll factorial(ll k){
  ll sum = 1;
  for(int i = 1 ; i < k + 1; i++) {
    sum *= i;
    //sum = sum % ATCODER_AMARI;
  }
  return sum;
}

int main() {
  ll n,k;
  cin >> n >> k;

  vector<ll> a(n);
  rep(i,n) {
    cin >> a.at(i);
  }

  for(ll i = k; i < n; i++) {
    if(a.at(i) > a.at(i - k)) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
}


Measured memory_bytes: 4840
#include<bits/stdc++.h>
using namespace std;
int a[102];
int main()
{
    int n,d,x;
    scanf("%d",&n);
    scanf("%d%d",&d,&x);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==1)
            ans+=d;
        else if(a[i]<d)
        {
            ans+=1;
            for(int j=a[i]+1;j<=d;j+=a[i])
                ans++;
        }
        else ans++;
    }
    printf("%d\n",ans+x);
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    ll n;
    cin>>n;
    vector<pair<ll,ll>> a(n);
    rep(i,n) cin>>a[i].second>>a[i].first;
    sort(all(a));
    ll sum=0;
    rep(i,n){
        sum+=a[i].second;
        if(sum>a[i].first){
            cout<<"No";
            return 0;
        }
    }
    cout<<"Yes";
}

Measured memory_bytes: 6424
#include <cstdio>
#include <algorithm>
#include <vector>

const int MN = 100005, MQ = 100005;

int N, M;
std::vector<int> G[MN];
int Q, qv[MQ], qd[MQ], qc[MQ];
int tag[MN], tmp[MN];

int main() {
	scanf("%d%d", &N, &M);
	for (int i = 1, x, y; i <= M; ++i) {
		scanf("%d%d", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	scanf("%d", &Q);
	for (int i = 1; i <= Q; ++i) scanf("%d%d%d", &qv[i], &qd[i], &qc[i]);
	for (int j = 10; j >= 0; --j) {
		for (int u = 1; u <= N; ++u) tmp[u] = 0;
		for (int u = 1; u <= N; ++u) if (tag[u])
			for (int v : G[u]) tmp[v] = std::max(tmp[v], tag[u]);
		for (int u = 1; u <= N; ++u) tag[u] = std::max(tag[u], tmp[u]);
		for (int i = 1; i <= Q; ++i) if (qd[i] == j) tag[qv[i]] = std::max(tag[qv[i]], i);
	}
	for (int u = 1; u <= N; ++u) printf("%d\n", qc[tag[u]]);
	return 0;
}

Measured memory_bytes: 10680
import numpy as np

N = int(input())
A = list(map(int, input().split()))

exist = np.zeros(sum(A)+1, dtype=bool)
exist[0] = True
for a in A:
    exist[a:] = exist[:-a] | exist[a:]

for i in range((sum(A)+1) // 2, sum(A) + 1):
    if exist[i]:
        print(i)
        exit()


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

int main(){
  vector<int64_t> v(3);
  for(int i=0;i<3;i++){
    cin >> v.at(i);
  }
  sort(v.begin(),v.end());
  int64_t x=v.back()/2;
  int64_t y=v.back()-x;
  cout << v.at(0)*v.at(1)*(y-x) << endl;
  return 0;
}


Measured memory_bytes: 256