akhauriyash/Code-Regression huggingface dataset:
#!/usr/bin/env python3

import heapq

def main():
    n, m = map(int, input().split())
    ab = []
    for i in range(n):
        a, b = map(int, input().split())
        ab.append((a, b))
    ab.sort(key=lambda x: x[0])
    doable = []
    next = 0
    res = 0
    for dt in range(m + 1):
        while next < n:
            if ab[next][0] <= dt:
                a, b = ab[next]
                heapq.heappush(doable, (-b, a))
                next += 1
            else:
                break
        if doable:
            mb, a = heapq.heappop(doable)
            b = -mb
            res += b
    print(res)

if __name__ == "__main__":
    main()


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

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

const ll mod =  1000000007;  //出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60;   // INF = 1152921504606846976

int main()
{
    int k,a,b; cin >> k >> a >> b;
    bool ok = false;
    for(int i = a; i <= b; i++){
        if(i % k == 0) ok = true;
    }
    if(ok)cout << "OK";
    else cout << "NG";
}

Measured memory_bytes: 3680
#include<bits/stdc++.h>
using namespace std;
long long n,m,ans;
set<long long>st;
int main()
{
  cin>>n;
  m=n-1;
  for(long long i=2;i*i<=n;i++)
  {
	if(n%i!=0)continue;
	long long qwq=n;
	while(qwq%i==0)qwq/=i;
	qwq%=i;
	if(qwq==1)
    	st.insert(i);
  }
	for(long long i=2;i*i<=m;i++)
		if(m%i==0)
			st.insert(i),st.insert(m/i);
	st.insert(n);
	if(m!=1)st.insert(m);
	cout<<st.size()<<"\n";
}


Measured memory_bytes: 256
N = int(input())

SS = []
for i in range(N):
  s = list(input())
  s.sort()
  SS.append(s)
  
SS.sort()

ans = 0
prev = None
cnt = 0
for i in range(N):
  s = SS[i]
  if s == prev:
    if not cnt:
      cnt = 2
    else:
      cnt += 1
  else:
    ans += cnt * (cnt - 1) // 2
    cnt = 0
  prev = s
  
ans += cnt * (cnt - 1) // 2
print(ans)
  


Measured memory_bytes: 24652
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset> 
#include<unordered_map> 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
// RAJJI KI MEHER 
#define fio ios_base::sync_with_stdio(false)
//#define mod 1000000007
#define mod1 mod
#define mod2 100000009
#define li long long int
#define ll int
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%lld",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 100000000000000005
#define MINN -10000000000000000
#define INTMAX 1000001000
#define pii pair<li,li>
#define pdd pair<double,double>
#define pic pair<int,char>
//#define N 5005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20
using namespace std;

ddouble intersection(pii p1,pii p2){
   if(p1.fi==p2.fi) return((ddouble)1000000000000000000);
   return((ddouble)(p2.sec-p1.sec)/(ddouble)(p1.fi-p2.fi));}

int main(){
li n,i,j,size,C,ans=100000000000000000;
readl(n);readl(C);li h[n+1];
for(i=1;i<=n;i++){readl(h[i]);}
li dp[n+1];dp[0]=0;dp[1]=0;
pii dpi;
vector<pii>lines;vector<double>points;
for(i=1;i<=n;i++){
if(i==1){dp[1]=0;}
else{j=0;
if(points.size()) j=lower_bound(points.begin(),points.end(),h[i])-points.begin();
dp[i]=lines[j].fi*h[i]+lines[j].sec+h[i]*h[i]+C;
}

dpi={-2*h[i],dp[i]+h[i]*h[i]};
size=lines.size();
if(size==0){lines.pb(dpi);}
else if(size==1){lines.pb(dpi);points.pb(intersection(lines[size-1],dpi));}
else{
while(size>=2 && points[size-2]>=intersection(lines[size-2],dpi)){
size--;
points.pop_back();
lines.pop_back();
}
points.pb(intersection(lines[size-1],dpi));
lines.pb(dpi);
}

}
cout<<dp[n];

}

Measured memory_bytes: 13076
#include <stdio.h>
#include <stdlib.h> 
int main(void){
    
    
    char S[200001] = {};
    char T[200001] = {};
    scanf("%s %s", S, T);
    int a = 0;
    
    for (long i = 0; i < 200001; i++){
        if(S[i] != T[i]){
            a++;
        }
    }
    printf("%d", a);
    return 0;
}


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

using namespace std;

const int N = 305;
const int inf = (int) 1e9;

typedef pair <int, int> ii;

struct open {
  int u, reload, fuel;
  open(int u, int reload, int fuel) : u(u), reload(reload), fuel(fuel) {}
};

struct cmp {
  bool operator()(const open &a, const open &b) {
    if (a.reload != b.reload) return a.reload < b.reload;
    if (a.fuel != b.fuel) return a.fuel > b.fuel;
    return 0;
  }
};

int n, m, l;
vector <ii> adj[N];
pair <int, int> dist[N][N];

void go(int u) {
  dist[u][u].first = 0;
  dist[u][u].second = l;
  multiset <open, cmp> pq;
  pq.insert(open(u, 0, l));
  while (!pq.empty()) {
    auto here = *pq.begin(); pq.erase(pq.begin());
    if (here.reload > dist[u][here.u].first ||
        (here.reload == dist[u][here.u].first && here.fuel < dist[u][here.u].second)) continue;
    for (auto v : adj[here.u]) {
      if (v.second > l) continue;
      int nreload = here.reload + (here.fuel < v.second);
      int nfuel = (here.fuel < v.second ? l - v.second : here.fuel - v.second);
      if (nreload < dist[u][v.first].first ||
          (nreload == dist[u][v.first].first && nfuel > dist[u][v.first].second)) {
        dist[u][v.first].first = nreload;
        dist[u][v.first].second = nfuel;
        auto add = open(v.first, nreload, nfuel);
        pq.insert(add);
      }
    }
  }
}

int main() {
  cin >> n >> m >> l;
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    adj[a].push_back({b, x});
    adj[b].push_back({a, x});
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dist[i][j] = make_pair(inf, inf);
    }
  }
  for (int i = 1; i <= n; i++) {
    go(i);
  }
  int q;
  cin >> q;
  vector <int> ans;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    ans.push_back(dist[a][b].first);
  }
  for (int a : ans) {
    if (a == inf) {
      cout << "-1\n";
    } else
    cout << a << '\n';
  }
  return 0;
}


Measured memory_bytes: 2824
#include <stdio.h>

struct Node{
  int p;
  int l;
  int r;
};

int n;
struct Node T[10001];


void preParse(int u){
  if(u==-1)return;
  printf(" %d",u);
  preParse(T[u].l);
  preParse(T[u].r);
}

void inParse(int u){
  if(u==-1)return;
  inParse(T[u].l);
  printf(" %d", u);
  inParse(T[u].r);
}

void postParse(int u){
  if(u==-1)return;
  postParse(T[u].l);
  postParse(T[u].r);
  printf(" %d",u);
}

int main(){
  int i, v, l, r, root;
  scanf("%d", &n);
  for(i=0; i<n; i++){
    T[i].p=-1;
  }
  for(i=0; i<n; i++){
    scanf("%d %d %d",&v, &l, &r);
    T[v].l=l;
    T[v].r=r;
    if(l!=-1)T[l].p=v;
    if(r!=-1)T[r].p=v;
  }
  for(i=0; i<n; i++){
    if(T[i].p==-1)root=i;
  }
  printf("Preorder\n");
  preParse(root);
  printf("\n");
  printf("Inorder\n");
  inParse(root);
  printf("\n");
  printf("Postorder\n");
  postParse(root);
  printf("\n");
  
  return 0;
}
    

Measured memory_bytes: 620
import math
n = int(input())
x = []
y = []
for _ in range(n):
    xi, yi = map(int, input().split())
    x.append(xi)
    y.append(yi)
    
total = 0
for i in range(n):
    for j in range(i):
        total += math.sqrt((x[j] - x[i])**2 + (y[j] - y[i])**2)
print(total/(n/2))

Measured memory_bytes: 3060
/*
    Author - FireDrake99
*/
#include<bits/stdc++.h>
using namespace std;

//------------Constants-----------------
#define ll long long int
//------------Variables-----------------
int a,b,c;
//------------Functions-----------------

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//------------Input---------------------
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin >> a >> b >> c;
//------------Work----------------------
    if ((a<=c && c<=b) || (b<=c && c <=a)) cout << "Yes"; else cout << "No";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pii pair<int, int>

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    map<int, int> a, b;
    int n, m;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a[x]++;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        int x; 
        cin >> x;
        b[x]++;
    }
    bool ans = true;
    for(auto &el : b) {
        if(el.se > a[el.fi]) {
            ans = false;
        }
    }
    if(ans) {
        cout << "YES\n";
    } else {
        cout << "NO\n"; 
    }
}

Measured memory_bytes: 25216
#include <bits/stdc++.h>
using namespace std;
int main(){
	int x;
	cin >> x;
	string s;
	cin >> s;
	if(s.size() <= x) cout << s << endl;
	else{
		for(int i = 0; i < x; i++){
			cout << s[i];
		}
		cout << "...";
	}
	return 0;
}

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

using namespace std;

using ll = int64_t;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int IINF = 2147483647;
constexpr ll LINF = 9223372036854775807LL;

//===




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

	Vi d;
	for (int i = 1; i * i <= M; ++i) {
		if (M % i == 0) {
			d.push_back(i);
			if (M / i != i) {
				d.push_back(M / i);
			}
		}
	}

	int ans = 0;
	for (auto& di : d) {
		if (N <= M / di) {
			ans = max(ans, di);
		}
	}

	cout << ans << endl;
}

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

int main() {
  
  int men = 700;
  
  string s;
  cin >> s;
  int count = 0;
  
  for ( int i =0; i < 3; i++) {
    if ( s[i] == 'o' ) count++;
  }
  
  cout << men + 100 * count << endl;
}
  

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
using namespace std;
 
struct Card{
    char pic;
    int  num;
};
 
bool compare_as_ints(Card i, Card j)        
{
    return (i.num<j.num);
}
 
int partition(Card A[], int p, int r)
{
    Card x = A[r];
    int i = p-1;
 
    for(int j=p; j<=r-1; j++){
        if(A[j].num<=x.num){
            i++;
            swap(A[i], A[j]);
        }
    }
 
    swap(A[i+1], A[r]);
 
    return i+1;
}
 
void quickSort(Card A[], int p, int r)
{
    if(p<r){
        int q = partition(A, p, r);
        quickSort(A, p, q-1);
        quickSort(A, q+1, r);
    }
}
 
int main()
{
    int n;
    cin >> n;
 
    Card A[100000], B[100000];
    for(int i=0; i<n; i++){
        cin >> A[i].pic >> A[i].num;
        B[i] = A[i];
    }
 
    quickSort(A, 0, n-1);
    stable_sort(B, B+n, compare_as_ints);   
 
    bool flg = true;
    for(int i=0; i<n; i++){
        if(!(A[i].pic==B[i].pic && A[i].num==B[i].num)){
            flg = false;
            break;
        }
    }
    if(flg){
        cout << "Stable" << endl;
    }
    else{
        cout << "Not stable" << endl;
    }
 
    for(int i=0; i<n; i++){
        cout << A[i].pic << ' ' << A[i].num << endl;
    }
 
    return 0;
}

Measured memory_bytes: 4716
#include<stdio.h>

int main(void)
{
    long n, min=1000001, max=-1000001, sum=0, i, j;
    scanf("%d", &n);
    for (i =0; i < n; i++) {
        scanf("%ld", &j);
        if (min > j) min=j;
        if (max < j) max=j;
        sum+=j;
    }
    printf("%ld %ld %ld\n", min, max, sum);
}



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

int main() {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    int dx = x2 - x1;
    int dy = y2 - y1;

    cout << x2 - dy << " " << y2 + dx << " "
         << x1 - dy << " " << y1 + dx << endl;
}

Measured memory_bytes: 256
using System;

namespace atcoderA
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Split(' ');
            var a = input[0];
            var b = input[1];
            if (a == "H")
            {
                Console.WriteLine(b == "H" ? "H" : "D");
            }
            else
            {
                Console.WriteLine(b == "H" ? "D" : "H");
            }
        }
    }
}

Measured memory_bytes: 11092
while True:
    n = int(input())
    if n == 0:
        break
    m = int(input())
    a = [[10000 for i in range(n)] for j in range(n)]

    for i in range(m):
        a_i, b_i, d_i = map(int, input().split(","))
        a[a_i][b_i] = d_i//100 - 1
        a[b_i][a_i] = d_i//100 - 1

    w = 0
    v = set()
    v.add(0)

    while len(v) < n:
        min_w   = 10000
        min_idx = 0
        i = 0
        for node in v:
            if min_w > min(a[node]):
                min_w = min(a[node])
                i = node
                min_idx = a[i].index(min_w)
        if min_idx in v:
            a[i][min_idx] = 10000
            continue
        else:
            w += min_w
            v.add(min_idx)

    print(w)



Measured memory_bytes: 5684
//THIS IS THE AATMANIRBHAR SOLUTION I FOUND..!!!!!

#include<bits/stdc++.h>
using namespace std ;
// #define int double
#define int long long
#define pb push_back
#define mp make_pair
#define F first 
#define S second 
#define P push
#define vvi vector<vector<int>>
#define vi vector<int>
#define vf vector<float>
#define vc vector<char>
#define vs vector<string>
#define pii vector<pair<int, int>> 
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define s(v) sort(all(v))
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(j,a,b) for(int j=a-1;j>=b;j--) 
#define binod ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;


void solve()
{
  string s;
  cin>>s;
  int n=sz(s);
  vi a(n);
  rep(i,0,n)
  a[i]=s[i]-'0';
  int sum=0;
  rep(i,0,n)
  {
    sum+=a[i];
  }
  if(sum%9==0)
  cout<<"Yes";
  else cout<<"No";
}

signed main(){
    binod;
    int t1=1;  
    // cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){ 
        solve();
        cout<<endl;
    }    
}
                                                                            
                                                                            //Don't judge me or anyone
                                                                            //this is life, 
                                                                            //not a reality show
                                                                            


Measured memory_bytes: 5088
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;

ll gcd(ll a, ll 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;
}

ll mulMod(ll a, ll b)
{
    return (((a % MOD) * (b % MOD)) % MOD);
}

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

void solve(long long A, long long B, long long C)
{
    if (A <= C && C <= B)
    {
        cout << YES << endl;
    }
    else
    {
        cout << NO << endl;
    }
}

int main()
{
    long long A;
    scanf("%lld", &A);
    long long B;
    scanf("%lld", &B);
    long long C;
    scanf("%lld", &C);
    solve(A, B, C);
    return 0;
}


Measured memory_bytes: 256
n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
p=0
for i in range(n):
	if b[i]>=a[i]:
		p+=a[i]
		x=b[i]-a[i]
		if x<a[i+1]:
			p+=x
			a[i+1]-=x
		else:
			p+=a[i+1]
			a[i+1]=0
	else:
		p+=b[i]
print(p)

Measured memory_bytes: 19116
#include<bits/stdc++.h>
#define pb push_back
#define fori(j,n) for(int i=j ; i<n ; i++)
#define forj(i,n) for(int j=i ; j<n ; j++)
#define fork(i,n) for(int k=i ; k<n ; k++)
#define ppb pop_back()
#define umap unordered_map
#define uset unordered_set
#define lli long long int

using namespace std;

void solve()
{
	int n;cin>>n;
	if(n>=30) cout<<"Yes";
	else cout<<"No";
	
}

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

	// int t;cin>>t;
	// while(t--)
	// {
		solve();
		// cout<<endl;
	// }


	return 0;

}

Measured memory_bytes: 3644
#include <iostream>
#include <set>
#include <string>

using namespace std;

long long int MOD = 1000000007;

int H, W, K;

set<string> S, newS;

long long int dp[105][12];

int main(void) {

	cin >> H >> W >> K;

	
	newS.insert("#");
	for (int i = 1; i <= W - 1; i++) {
		S = newS; newS.clear();
		for (auto it = S.begin(); it != S.end(); it++) {
			string str = *it;
			int m = str.size();
			if (str[m - 1] == '1') { newS.insert(str + "0"); }
			else { newS.insert(str + "0"); newS.insert(str + "1"); }
		}
	}

	S.clear();
	for (auto it = newS.begin(); it != newS.end(); it++) {
		S.insert((*it) + "#");
	}

	/*for (auto it = S.begin(); it != S.end(); it++) {
		cout << *it << endl;
	}*/

	//=========
	dp[0][1] = 1;
	//===========

	for (int i = 0; i <= H-1; i++) {
		for (int k = 1; k <= W; k++) {
			for (auto it = S.begin(); it != S.end(); it++) {
				string str = *it;
				if (str[k - 1] == '1') { dp[i + 1][k - 1] += dp[i][k]; dp[i + 1][k - 1] %= MOD; }
				else if (str[k] == '1') { dp[i + 1][k + 1] += dp[i][k]; dp[i + 1][k + 1] %= MOD; }
				else { dp[i + 1][k] += dp[i][k]; dp[i + 1][k] %= MOD; }
			}
		}
	}


	cout << dp[H][K] << endl;

	//system("pause");
	return 0;
}

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

using namespace std;

#define int long long
#define fi first
#define se second
#define inf 1e9
const int mod=(int)1e9+7;


void test_case(){
	int n;
	cin>>n;
	vector<vector<pair<int,int> > > arr;
	for(int i=0;i<n;i++){
		int sz;
		cin>>sz;
		vector<pair<int,int> > t;
		for(int j=0;j<sz;j++){
			int x,y;
			cin>>x>>y;
			--x;
			t.push_back({x,y});
		}
		arr.push_back(t);
	}
	int ans=0;
	for(int mask=0;mask<(1<<n);mask++){
		bool cont=0;
		int count=0;
		for(int i=0;i<n;i++){
			if((mask&(1<<i))){
				count++;
				int sz=arr[i].size();
				for(int j=0;j<sz;j++){
					bool com=(mask&(1<<arr[i][j].fi));
					if(com!=arr[i][j].se)cont=1;
				}
			}	
		}
		if(!cont){
			ans=max(ans,count);
		}
	}
	cout<<ans;
}

signed main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t=1;
	//cin>>t;
	while(t--)test_case();
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<tuple>
#include<ctime>
#include<functional>
#include<cmath>
#include<cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> p; 
#define rep(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))

int main(){
    string s;
    cin>>s;
    rep(i,s.size()){
        if(i==3) cout<<8;
        else cout<<s[i];
    }
    
}

Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <string.h>
int main()
{
    std::string S;
    std::cin >> S;
    int ans = 0;
    int temp = 0;
    for (int i = 0; i < S.size(); ++i)
    {
        if (S[i] == 'A')
        {
            ++temp;
        }
        else if(S[i] == 'C')
        {
            ++temp;
        }
        else if(S[i] == 'G')
        {
            ++temp;
        }
        else if(S[i] == 'T')
        {
            ++temp;
        }
        else
        {
            temp = 0;
        }
        ans = std::max(ans, temp);
    }
    std::cout << ans << std::endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int amex = sc.nextInt();
        int sum = 0;
        int num =0;
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < N; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);

        for(int i = 0; i < N; i++){
            amex -= list.get(i);
            if (amex >= 0){
                num++;
            }else {
                break;
            }
        }
        if (amex > 0){
            num--;
        }
        System.out.println(num);

    }
}

Measured memory_bytes: 23380
#include<bits/stdc++.h>
using namespace std;
typedef pair<long long,int>P;
const int maxn=2e5+5;
long long n,x[maxn],s[maxn],m;
signed main(){
    priority_queue<P>q;
    scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld%lld",&x[i],&s[i]);
        s[i]+=s[i-1];q.push(P(s[i]-x[i],i));}
    for(int i=1,flag;i<=n;i++){
        flag=1;while(flag){
            P p=q.top();
            if(p.second<i)q.pop();
            else{m=max(m,p.first-s[i-1]+x[i]);flag=0;}
        }
    }
    printf("%lld\n",m); return 0;
}

Measured memory_bytes: 8560
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(x) (x).begin(),(x).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 (b<a) { a=b; return 1; } return 0; }

typedef long long ll;

using namespace std;
vector<vector<int>>dp(1001,vector<int>(4096,1e9));

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n,m;
  cin>>n>>m;
  dp[0][0]=0;
  rep(i,m)
  {
    int a,b;
    cin>>a>>b;
    int num=0;
    rep(j,b)
    {
      int t;cin>>t;
      num+=1<<(t-1);
    }
  //  cout<<"num"<<num<<endl;
    rep(j,1<<n)
    {
      dp[i+1][j]=dp[i][j];
    }
    rep(j,1<<n)
    {
      chmin(dp[i+1][j|num],dp[i][j]+a);
      //cout<<dp[i+1][j|num]<<endl;
    }
  }
  int res=dp[m][(1<<n)-1];
  if(res==1e9)cout<<-1<<endl;
  else cout<<res<<endl;

  return 0;

}


Measured memory_bytes: 16384
d, n = list(map(int, input().split()))
cnt = 0
i = 1

while True:
  if(d == 0 and i % 100 != 0):
    cnt += 1    
  if(d != 0 and i % (100 ** d) == 0 and i % (100 ** (d+1)) != 0):
    cnt += 1
  if(cnt == n):
    print(i)
    exit()
  i += 1

Measured memory_bytes: 3060
######ANSDEFINE##########
#unnecessary			#
#########################

A,B,C,K = map(int,input().split())

if K <= A:
  print(K*1)
  
else:
  if A + B >= K:
    print(1 * A)
    
  else :
    S = K - (A + B)
    print(A - S)
    
   

Measured memory_bytes: 9164
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
#define _GLIBCXX_DEBUG
#define ll long long
#define rep(i,n) for (ll i = 0; i < n; i++)
#define vl vector<ll>
#define vpl vector<pair<ll,ll>>
#define vvl vector<vector<ll>> //a(縦行,vector<ll>(横列));
#include<algorithm>//__gcd(a,b)
#include <iostream>
#include <vector>
#include <cstdlib>
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const long long INF = 1LL << 60;

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

    if(n!=0){
    vl a(n);rep(i,n)cin>>a[i];
    ll ans=x+1;
ll dis=0;
    sort(a.begin(),a.end());
    ll j=0;
 


rep(i,a[n]+200){
   
    if(abs(x-i)<ans&&i!=a[j]){ans=abs(x-i);dis=i;}
    if(i==a[j])j++;

}

cout<<dis<<endl;}

if(n==0)cout<<x<<endl;

}


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

#define dlog(str) cout << "====" << str << endl;
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for(int i = 0, i##_l = (n); i < i##_l; i++)
#define REP2(i, s, e) for(int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
    int K, T;
    cin >> K >> T;
    vector<int> v(T);
    REP(i, T) {
        cin >> v[i];
    }
    while(1 < v.size()) {
        RSORT(v);
        v[0] -= v[1];
        v.erase(v.begin() + 1);
    }
    cout << max(v[0] - 1,0);
    return 0;
}

Measured memory_bytes: 256
def II():return int(input())
def MI():return map(int,input().split())
def LI():return list(MI())
def ok():print("Yes")
def ng():print("No");exit()

import numpy
k,n=MI()
a=LI()
a+=(a[0]+k),
a=numpy.diff(a)
print(k-max(a))

Measured memory_bytes: 34156
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    ll N, A, B;
    cin >> N >> A >> B;
//    if(A > B) swap(A, B);
    ll s = A * (N - 1) + B;
    ll t = A + B * (N - 1);
    output(max(0LL, t - s + 1));
    
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
string N; int K, dp[105][2][105];
int main() {
  cin >> N >> K;
  int lng = N.size(); dp[0][0][0] = 1;
  for(int i = 1; i <= lng; ++i) {
    int n = N.at(i-1)-'0';
    for(int j = 0; j < 2; ++j) {
      for(int k = 0; k <= i; ++k) {
        for(int d = 0; d <= (j?9:n); ++d) {
          dp[i][j||d<n][k+(d>0)] += dp[i-1][j][k];
        }
      }
    }
  }
  cout << dp[lng][0][K]+dp[lng][1][K] << endl;
}

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

#define INF 0x3f3f3f3f
#define int long long
#define pii pair<int, int>

const int N = 100005;

int dp[105][N];
pii items[105];

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    memset(dp, 0, sizeof(dp));
    int n, w, i, j, w_, v_, ans;
    cin >> n >> w;
    memset(dp, 0, sizeof(dp));
    for(i = 1 ; i <= n ; i++) {
        cin >> w_ >> v_;
        items[i] = make_pair(w_, v_);
    }
    for(j = 1 ; j <= 100000 ; j++)
        dp[0][j] = INF;
    for(i = 1 ; i <= n ; i++) {
        for(j = 1 ; j <= 100000 ; j++) {
            dp[i][j] = dp[i-1][j];
            if(j >= items[i].second)
                dp[i][j] = min(dp[i][j], dp[i-1][j-items[i].second] + items[i].first);
        }
    }
    ans = 0;
    for(j = 0 ; j <= 100000 ; j++) {
        if(dp[n][j] <= w)
            ans = max(ans, j);
    }
    cout << ans << endl;
    return 0;
}


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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

ll solve(const string S, const int K) {
  const int N = S.size();

  ll D = 0, M = 0, C = 0;
  ll ans = 0;

  for (int i = 0; i < N; i++) {
    if (i - K >= 0) {
      if (S[i-K] == 'D') {
        D--;
        C -= M;
      } else if (S[i-K] == 'M') {
        M--;
      }
    }

    if (S[i] == 'D') {
      D++;
    } else if (S[i] == 'M') {
      M++;
      C += D;
    } else if (S[i] == 'C') {
      ans += C;
    }
  }
  return ans;
}

int main2() {
  int N = nextLong();
  string S; cin >> S;
  int Q = nextLong();
  while (Q--) {
    int K = nextLong();
    ll ans = solve(S, K);
    cout << ans << endl;
  }
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

Measured memory_bytes: 5420
//D Teleporter
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++) //これでiやjを定義しなくていいらしい
using namespace std;
using ll = long long;

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

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

  vector<int> s; //町の移動経路を保存
  vector<int> ord(n+1,-1); //訪れたことがあるかどうかを-1 or notで判別できる
  int c, l=0;

  int v=1;
  while(ord[v] == -1){//ループに入っていないうちは
    ord[v] = s.size();//==whileのループ回数==移動回数
    s.push_back(v);//通った町を順に記録
    v = a[v-1];//移動をvで管理
  }
  c = s.size() - ord[v];//周期。s.size()がループを発見した点で、ord[v]がループ手前までの移動数だから。
  l = ord[v];//これも記録

  if(k<l)
    cout << s[k] << endl;
  else{
    k = (k-l)%c;//ループに入れ、周期で割った余りを算出
    cout << s[l+k] << endl;//開始点からk回移動
  }
  return 0;
}


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

using namespace std;
typedef long long ll;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;


int main() {
  string S, T; cin >> S >> T;
  int sl = S.length(), tl = T.length();
  int sn[sl+1]={}, tn[tl+1]={};
  rep(i,sl) sn[i+1] = sn[i]+(S[i]=='A' ? 1 : 2);
  rep(i,tl) tn[i+1] = tn[i]+(T[i]=='A' ? 1 : 2);

  int q; cin >> q;
  rep(i,q) {
    int a, b, c, d; cin >> a >> b >> c >> d;
    int sums = sn[b] - sn[a-1];
    int sumt = tn[d] - tn[c-1];
    string result = ((sums-sumt)%3==0) ? "YES" : "NO";
    // cout << sums << " " << sumt << endl;
    cout << result << endl;
  }
  return 0;
}

Measured memory_bytes: 1664
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define io ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define endl '\n'
#define mod 1000000007
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
#define pii pair<ll, pair<int, char>>
ll n;
int r, g, b;
ll solve() {
	string s;
	cin>>s;
	int ans = 0;
	r = s[0] == 'R' ? 1 : 0;
	for(int i = 1; i < 3; i++) {
		if(s[i] == 'R' && s[i-1] == 'R') r++;
		else if(s[i] == 'R' && s[i-1] != 'R') {
			ans = max(ans, r);
			r = 1;
		}
	}
	return max(ans, r);
}
int main() {
	io
	ll t = 1;
	//cin>>t;
	while(t--) {
		cout<<solve()<<endl;
		//solve();
	}
}



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

int calc(int x, int k) {
  if (x <= 0) return 0;
  if (x <= k) return 1; 
  else        return (x - 2) / (k - 1) + 1;
}

int main(void) {
  int n, k, zohe, ans = 1145141919;
  cin >> n >> k;
  
  for (int i = 0; i < n; i++) {
    int in;
    cin >> in;
    if (in == 1)
      zohe = i;
  }
  
  for (int i = zohe; i <= zohe + k - 1; i++) {
    int tmp = calc(i - k + 1 <= 0 ? 0 : i - k + 2, k) + calc((n - i <= 1 ? 0 : n - i), k) + 1;
    ans = min(ans, tmp);
  }
  
  cout << ans << endl;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	static long mod = (long) Math.pow(10, 9) + 7;

	public static void main(String[] args) {

		long n = 0;
		long a = 0;
		long b = 0;
		long anser = 0;

		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		a = scan.nextLong();
		b = scan.nextLong();

		//
		long c = (long) (powB(2, n) - 1) % mod;

		long bunsiA = conbination(n, a);
		long bunboA = factorial(a);
		long bunsiB = conbination(n, b);
		long bunboB = factorial(b);

		//分母をmod-2乗する
		bunboA = powAlfa(bunboA);
		bunboB = powAlfa(bunboB);

		//かけ合わせる
		long nca = (bunsiA * bunboA) % mod;
		long ncb = (bunsiB * bunboB) % mod;

		anser = (c - nca - ncb) % mod;

		if (anser < 0) {
			anser += mod;
		}

		System.out.println(anser);

	}

	private static long factorial(long n) {
		long fact = 1;
		for (long i = 1; i <= n; i++) {
			fact = fact * i % mod;
		}
		return fact;
	}

	//conbination(leftNum,rightNum)の分子の計算
	private static long conbination(long leftNum, long rightNum) {
		long pai = 1;

		for (long i = leftNum; i > leftNum - rightNum; i--) {
			pai = pai * i % mod;
		}
		return pai;

	}

	//mod-2乗の計算
	private static long powAlfa(long number) {
		long anser = 1;
		long n = mod - 2;
		while (n > 0) {
			if ((n & 1) == 1) {
				anser = anser * number % mod;
			}
			number = number * number % mod;
			n >>= 1;
		}
		return anser;
	}

	private static long powB(long number, long sisuu) {
		long anser = 1;
		while (sisuu > 0) {
			if ((sisuu & 1) == 1) {
				anser = anser * number % mod;
			}
			number = number * number % mod;
			sisuu >>= 1;
		}
		return anser;
	}

}


Measured memory_bytes: 22868
//設定言語check
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <limits.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <queue>
using namespace std;

long long int power(long long int x, long long int y, long long int z){
  if(y == 1) return (z * x)%1000000007 ;
  else if((y % 2) == 0) return power((x * x)%1000000007, y / 2, z) ;
  else return power((x * x)%1000000007, y / 2, (z * x)%1000000007) ;
}

int main(){
  long long int N, ans = 0, sets = 10, sign = -1 ;
  cin >> N ;
  if(N < 2){
    cout << 0 << endl ;
    return 0 ;
  }
  ans = power(10,N,1) ;
  // for(int i=9;i>7;i--){
  //   ans += (sign * sets * power(i,N,1)) % 1000000007 ;
  //   if(ans < 0) ans += 1000000007 ;
  //   sign = -(sign) ;
  //   sets = (sets * i) % 1000000007 ;
  // }
  ans -= (power(9,N,1) * 2) % 1000000007 ;
  if(ans < 0) ans += 1000000007 ;
  ans += (power(8,N,1)) ;
  ans %= 1000000007 ;

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

Measured memory_bytes: 3632
import java.util.*;
public class Main {
	public static void main(String[] args) {
		int MAX = 1000000;
		boolean[] p;
		p = new boolean[MAX+1]; p[0] = p[1] = true;
		for(int i=2;i<=MAX;i++) if(!p[i])
			for(int j=i*2;j<=MAX;j+=i) p[j] = true;
		Scanner sc = new Scanner(System.in);
		for(;;) {
			int a = sc.nextInt(), d = sc.nextInt(), n = sc.nextInt();
			if( (a|d|n) == 0 ) break;
			for(;n!=0;a+=d) if(!p[a])n--;
			System.out.println(a-d);
		}
	}
}

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

int main() {
	int N, K, S;
	cin >> N >> K >> S;
	if (S < 1000000000) {
		for (int i = 0; i < K; i++) cout << S << " ";
		for (int i = K; i < N; i++) cout << S + 1 << " ";
	}
	else {
		for (int i = 0; i < K; i++) cout << S << " ";
		for (int i = K; i < N; i++) cout << S - 1 << " ";
	}
	cout << endl;
}


Measured memory_bytes: 1280
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int compare(const void *a,const void *b)
{
  return *(int*)b-*(int*)a;
} 


int main()
{
  int n,m;
  scanf("%d%d",&n,&m);
  
  int a[n];
  int i;
  double sum=0.0;
  int count=0;
  
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    sum+=a[i];
  }
  
  qsort(a,n,sizeof(int),compare);
  
  for(i=1;i<=m;i++)
  {
    if(sum<=a[i-1]*4*m)
    {
      count++;
    }
  }
 
  
  if(count==m)
  {
    printf("Yes\n");
  }
  else
  {
    printf("No\n");
  }
 
  return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>
#define REP(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
template<class T> inline void chmax(T &a,T b){if(a<b)a=b;}
template<class T> inline void chmin(T &a,T b){if(a>b)a=b;}

int main(){
    cin.tie(0); ios_base::sync_with_stdio(false);
    int x,a,b;cin >> x >> a >> b;
    x -= a;
    x %= b;
    cout << x << endl;
    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 repp(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
typedef long long lint;
const lint MOD =1000000007;
const lint MAX = 10000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
 
 
lint fac[MAX], finv[MAX], inv[MAX];
 
void COMinit() 
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (lint i = 2; i < MAX; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
long long COM(lint n, lint k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
lint primary(lint num)
{
    if (num < 2) return 0;
    else if (num == 2) return 1;
    else if (num % 2 == 0) return 0;
 
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            return 0;
        }
    }
 
    return 1;
}
   long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
    lint lcm(lint a,lint b){
        return a/__gcd(a,b)*b;
    }
     lint gcd(lint a,lint b){
        return __gcd(a,b);
    }    

  int main(){
      lint n,q,x,y,z;
      string s;
      cin>>x>>y>>z;

      cout<<z<<" "<<x<<" "<<y;
  }
     
  

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

using namespace std;

#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(int)(n);i>=0;--i)
#define RFOR(i, a, b) for(int i=(int)(a);i>=(int)(b);--i)
#define ALL(a) (a).begin(),(a).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;

const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
  if(a < b) {a = b; return true;}
  return false;
}

template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
  if(a > b) {a = b; return true;}
  return false;
}

template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if(i) os << " ";
    os << v[i];
  }
  return os;
}

vector<P> ord;

void dfs(int now, int prev, int nowd, const vvi &g) {
  ord[now] = {nowd, now};
  for(auto &nxt: g[now]) {
    if(nxt == prev) continue;
    dfs(nxt, now, nowd+1, g);
  }
}

void col(int now, int prev, const vvi &g, vector<bool> &sel) {
  if(sel[now]) return;
  sel[now] = true;
  for(auto &nxt: g[now]) {
    if(nxt == prev) continue;
    col(nxt, now, g, sel);
  }
}

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

  ll n, k; cin >> n >> k;

  vvi g(n);
  vi p(n);
  ll ans = 0;
  REP(i, n) {
    int a; cin >> a;
    a--;
    p[i] = a;
    if(i == 0) {
      ans += a != 0;
      p[i] = 0;
    } else {
      g[a].push_back(i);
    }
  }

  ord.resize(n);
  dfs(0, -1, 0, g);
  sort(ord.rbegin(), ord.rend());

  vector<bool> sel(n);
  sel[0] = true;
  REP(i, n-1) {
    if(ord[i].first <= k) break;
    int now = ord[i].second;
    if(sel[now]) continue;
    int par = now;
    bool ok = false;
    REP(j, k-1) {
      par = p[par];
      if(sel[par]) {
        ok = true;
        break;
      }
    }
    if(!ok && p[par] != 0) {
      ans++;
      col(par, p[par], g, sel);
    }
  }
  cout << ans << endl;


}
























Measured memory_bytes: 13184
function Main(input) {
	input = input.trim().split("\n").map(function(x) { return x.split(" ")});
	let C = 1e9 + 7;
	let C2 = 1e5;
	let line = input[0].map(e => parseInt(e, 10));
	let r = [line[0], line[2]];
	let c = [line[1], line[3]];

	v_nCr1 = nCr_amari(r[1] + 1 + c[0], c[0] + 1, c[1] - c[0], C, C2);
	v_nCr2 = nCr_amari(r[0] + c[0], c[0] + 1, c[1] - c[0], C, C2);
	if (v_nCr2 === 0) v_nCr2 = Array.from({length: v_nCr1.length}, () => 0);

	let ans = 0;
	for (let i = 0; i < v_nCr1.length; i++){
		ans += (v_nCr1[i] - v_nCr2[i])
		ans = mod(ans, C);
	}
	
	console.log(ans);

}

function mod(i, j) {
    return (i % j) < 0 ? (i % j) + 0 + (j < 0 ? -j : j) : (i % j + 0);
}

function kakeruR(a, b, C, C2){
    var tmpQ, tmpR, ans;
    tmpQ = (b / C2 | 0);
    tmpR = b - (tmpQ * C2);
    ans = (a * tmpQ % C) * C2 % C + (a * tmpR);
    ans %= C;
    return ans;
}

function nCr_amari(n, k, MAX, MOD, C2) { //return [nCk, n+1Ck+1, ..., n+maxCk+max]
    // 前処理
    var v_ans = [ ];
    var fac, finv, inv;
    fac = [1,1];
    finv = [1,1];
    inv = [0,1];
    for (var i = 2; i <= MAX + n; i++){
        fac.push(fac[i - 1] * i % MOD);
        inv.push(MOD - kakeruR(inv[MOD%i], (MOD / i | 0), MOD, C2) % MOD);
        finv.push(kakeruR(finv[i - 1],inv[i] ,MOD,C2));
    }
    //二項係数計算
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    for (var i = 0; i <= MAX; i++){
    v_ans.push(kakeruR(kakeruR(fac[n + i] , finv[k + i],MOD,C2), finv[n - k],MOD,C2));
    }
    return v_ans;
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));


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

using ll = long long;

int main(){
  int H,A;
  cin >> H >> A;
  int ans=0;
  while(H>0){
    H -= A;
    ans++;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
// Problem : G - Longest Path
// Contest : AtCoder - Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_g
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<long,long> pl;
typedef pair<ll,ll> pll;
typedef vector<long> vl;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;

#define FOR(i,a,b) for(long long i=a;i<b;++i)
#define REV(i,a,b) for(long long i=a;i>=b;i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define tc ll tests;cin>>tests;while(tests--)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define coutv(v) for(auto it: (v))cout<<it<<" ";newl;
#define cout2d(v) for(auto it: (v)) {for(auto j:it) cout<<j<<" ";newl;}
#define cinv(v,n) vll (v)(n);FOR(i,0,(n)){cin>>v[i];}
#define cinvg(v,n) (v).resize(n);FOR(i,0,(n)){cin>>v[i];}
#define cin2d(v,n,m) vvll (v)(n,vll(m,0));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}}
#define cin2dg(v,n,m) (v).resize(n,vll(m));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}}
#define newl cout<<"\n"
#define mod 1000000007
#define INF 1e18

vvll g;
vb vis;
vll dp;
ll ans=0;

void dfs(ll s)
{
	vis[s]=true;
	for(auto v:g[s])
	{
		if(!vis[v])
		{
			dfs(v);
		}
		dp[s]=max(dp[s],1+dp[v]);
	}
	ans=max(ans,dp[s]);
}

int main()
{
	io;
	ll n,m;
	cin>>n>>m;
	g.resize(n);
	dp.resize(n);
	vis.resize(n);
	FOR(i,0,m)
	{
		ll u,v;
		cin>>u>>v;
		u--;v--;
		g[u].pb(v);
	}
	
	FOR(i,0,n)
	{
		if(!vis[i])
		{
			dfs(i);
		}
	}
	//coutv(dp)
	cout<<ans;
	
	return 0;
}


Measured memory_bytes: 8576
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
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; }
const int INF = 1<<30;

int main(){
	int x, y, z, K;
	cin >> x >> y >> z >> K;
	vector<ll> A(x), B(y), C(z);
	for (int i = 0; i < x; i++) cin >> A[i]; sort(A.rbegin(), A.rend());
	for (int i = 0; i < y; i++) cin >> B[i]; sort(B.rbegin(), B.rend());
	for (int i = 0; i < z; i++) cin >> C[i]; sort(C.rbegin(), C.rend());
	vector<ll> ANS;
	for (int i = 1; i <= x; i++){
		for (int j = 1; j <= y; j++){
			for (int k = 1; k <= z; k++) {
				if(i*j*k <= K) ANS.push_back(A[i-1] + B[j-1] + C[k-1]);
				else break;
			}
		}
	}
	sort(ANS.rbegin(), ANS.rend());
	for (int i = 0; i < K; i++) cout << ANS[i] << endl;
	return 0;
}


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

int main(){
  
  long long N, M;
  cin >> N >> M;

  if(N != 1 && M != 1){
    cout << (N-2)*(M-2) << endl;
  }else if(N == 1 && M == 1){
    cout << 1 << endl;
  }else{
    cout << max(N, M)-2 << endl;
  }
  return 0;
}

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

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

B = sorted(A)[::-1]

T=0
for i in range(N):
  T=T+B[i]
  
#print(B)
#print(B[M-1])
#print('total',T)
#print(B[M-1])

if B[M-1]>=T/4/M:
  print('Yes')
else:
  print('No')




Measured memory_bytes: 2940
N = int(input())
K = int(input())
S = str(N)
L = len(S)

dp = [[[1, 0], [0, 0], [0, 0], [0, 0]] for i in range(L)]
dp[0] = [[1, 0], [int(S[0])-1, 1], [0, 0], [0, 0]]

for l in range(1,L):
    s = int(S[l])
    if s == 0:
        dp[l][1][1] += dp[l-1][1][1]
        dp[l][2][1] += dp[l-1][2][1]
        dp[l][3][1] += dp[l-1][3][1]
    else:
        dp[l][1][1] += dp[l-1][0][1]
        dp[l][2][1] += dp[l-1][1][1]
        dp[l][3][1] += dp[l-1][2][1]
        dp[l][1][0] += dp[l-1][1][1]
        dp[l][2][0] += dp[l-1][2][1]
        dp[l][3][0] += dp[l-1][3][1]

    dp[l][1][0] += dp[l-1][0][0]*9 + dp[l-1][1][0]
    dp[l][2][0] += dp[l-1][1][0]*9 + dp[l-1][2][0]
    dp[l][3][0] += dp[l-1][2][0]*9 + dp[l-1][3][0]
    dp[l][1][0] += dp[l-1][0][1]*max(0,s-1)
    dp[l][2][0] += dp[l-1][1][1]*max(0,s-1)
    dp[l][3][0] += dp[l-1][2][1]*max(0,s-1)

print(sum(dp[L-1][K]))

Measured memory_bytes: 3444
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
t, a = inintm()
H = inintl()

diff = 10**6
ans = 0

for i in range(n):
    if abs(a-(t + -0.006*H[i])) < diff:
        diff = abs(a-(t + -0.006*H[i]))
        ans = i+1

print(ans)

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

int main(){
    int a, b, c, t, p;
    cin >> a >> b >> c;

    if (a > b){
        t = a;
        a = b;
        b = t;
    }

    if (b > c){
        p = b;
        b = c;
        c = p;
    }

    if (a > b){
        t = a;
        a = b;
        b = t;
    }

    cout << a << " " << b << " " << c << endl;

    return 0;
}


Measured memory_bytes: 3104
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD=998244353;

int main(void){
    int n;
    cin>>n;
    vector<int> A(n);
    rep(i,n) cin>>A[i];
    int first=0,second=-1;
    rep(i,n){
        if(first<A[i]){
            second=first;
            first=A[i];
        }
        else{
            if(second<A[i])
                second=A[i];
        }
    }
    rep(i,n){
        if(A[i]==first) cout<<second<<endl;
        else cout<<first<<endl;
    }
    return 0;
}

Measured memory_bytes: 2432
// AOJ_C++.cpp : アプリケーションのエントリ ポイントを定義します。
//
#include <iostream>
#include <math.h>

int main()
{
	int a;
	std::cin >> a;
	int h, m, s;
	h = a >= 3600 ? a / 3600 : 0;
	m = a >= 60 ? (a - h * 3600) / 60 : 0;
	s = a - h * 3600 - m * 60;
	
	std::cout << h << ":" << m << ":" << s << std::endl;
    return 0;
}




Measured memory_bytes: 3124