akhauriyash/Code-Regression huggingface dataset:
import math
a, b, x = map(int, input().split())
def suku(a, b, x):
    return math.atan((a*b*b)/(2*x)) * (180/math.pi)
def ooi(a, b, x):
    return math.atan((2*(a*a*b - x))/(a*a*a)) * (180/math.pi)

if a*a*b >= 2*x:
    print(suku(a, b, x))
else:
    print(ooi(a, b, x))


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


char chess(int h, int w) {
    if((h+w)%2==0) {
        return '#';
    }
    return '.';
}

int main(void)
{
    int H, W;
    while(true) {
        cin >> H >> W;
        if (H == 0 && W == 0) {
            break;
        }
        for(int i = 0; i < H; i++) {
            for(int j = 0; j < W; j++) {
                cout << chess(i,j);
            }
            cout << endl;
        }
        cout << endl;
    }
}



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

int main()
{
	unsigned long long n;
	cin >> n;

	unsigned long long sum = 0;
	for(unsigned long long i=1;i<sqrt(n);i++){
		if(n%i==0){
			unsigned long long m = n / i - 1;
			if(i < m)
				sum += m;
		}
	}
	cout << sum << endl;
	return 0;
}

Measured memory_bytes: 384
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()

cnt = 0
# a <= b <= c となる3数を探す
for i in range(N - 1):
    for j in range(i + 1, N):
        T = bisect.bisect_left(L, L[i] + L[j])  # c < a + bを満たすcを探している
        cnt += T - j - 1

print(cnt)

Measured memory_bytes: 46300
#include<iostream>
using namespace std;
int main(){
  double xa,ya,xb,yb,xc,yc,xd,yd;
  while(cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd){
    if((yb-ya)*(yd-yc)==-1*(xb-xa)*(xd-xc)) cout << "YES" << endl;
    else cout << "NO" << endl;
  }
  return 0;
}

Measured memory_bytes: 1216
//templates
#include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <iomanip>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define ALL(x) (x).begin(),(x).end()
#define sort_key(struct_name,key_name) [](const struct_name &x, const struct_name &y){return x.key_name < y.key_name;}
#define pb(x) push_back(x)
typedef long long ll;
using namespace std;

//main
int main(){
  int N; cin >> N;
  vector<int> H(N);
  rep(i,N) cin >> H[i];
  int ans = 1;
  for(int i=1; i<N; i++){
    if(H[i] >= *max_element(H.begin(),H.begin()+i)) ans += 1;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>

typedef long long ll;

using namespace std;

class UnionFind {
public:
    vector <ll> par; // 各元の親を表す配列
    vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)

    // Constructor
    UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);  // resize だとなぜか初期化されなかった
        for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
    }

    // Member Function
    // Find
    ll root(ll x) { // 根の検索
        while (par[x] != x) {
            x = par[x] = par[par[x]]; // x の親の親を x の親とする
        }
        return x;
    }

    // Union(Unite, Merge)
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        // merge technique（データ構造をマージするテク．小を大にくっつける）
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) { // 連結判定
        return root(x) == root(y);
    }

    ll size(ll x) { // 素集合のサイズ
        return siz[root(x)];
    }
};

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

    UnionFind uf(N);

    for (int i = 0; i < M; ++i) {
        int A, B;
        cin >> A >> B;

        uf.merge(A - 1, B - 1);
    }

    int ans = -1;
    for (int i = 0; i < N; ++i) {
        if (ans < uf.size(i)) {
            ans = uf.size(i);
        }
    }

    cout << ans << endl;


    return 0;
}

Measured memory_bytes: 6420
def main():
    li = [int(i) for i in input()]
    t = li[1:]
    op = ["+", "+", "+"]
    t_sum = sum(li)
    if t_sum == 7:
        print("{}+{}+{}+{}=7".format(li[0], li[1], li[2], li[3]))
    else:
        for i in range(3):
            if t_sum - 2*t[i] == 7:
                op[i] = "-"
                print("{}{}{}{}{}{}{}=7".format(li[0], op[0], li[1], op[1], li[2], op[2], li[3]))
                return
            elif t_sum - 2*t[i] > 7:
                for j in range(1, 3):
                    if t_sum - (t[i] + t[i-j])*2 == 7:
                        op[i] = "-"
                        op[i-j] = "-"
                        print("{}{}{}{}{}{}{}=7".format(li[0], op[0], li[1], op[1], li[2], op[2], li[3]))
                        return

main()



Measured memory_bytes: 3064
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<complex>
#include<string>
#include<cstring>

using namespace std;
#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
#define INF 100000000
#define all(x) x.begin(),x.end()
typedef pair<int,int> P;
typedef pair<int,P> PP;
int now;
int owari;
int nowb;
string s;
vector<string> array;
vector<int> bo;
vector<int> num;
int flag[100000];//0???????????? 1?¶???? 2?????????
int atai[100000];
int n;
int main()
{
	while(1)
	{
		rep(i,100000)
		{
			flag[i]=0;
			atai[i]=0;
			now=0;
			nowb=0;
			owari=0;
		}
		num.clear();
		bo.clear();
		array.clear();
		now=0;
		cin>>n;
		rep(i,n)
		{
			cin>>s;
			array.push_back(s);
		}
		if(n==0)break;
		if(n==1)
		{
			cout<<array[0]<<endl;
			continue;
		}
		rep(i,n)
		{
			bo.push_back(array[i].size()-1);
			if('0'<=array[i][bo[i]]&&array[i][bo[i]]<='9')
			{
				num.push_back((int)(array[i][bo[i]]-'0'));
			}
			else if(array[i][bo[i]]=='+')
			{
				num.push_back(-1);
			}
			else if(array[i][bo[i]]=='*')
			{
				num.push_back(-2);
			}
		}
		now=0;
		while(1)
		{
			if(now==n)
			{
				break;
			}
			if(num[now]==-1)
			{
				flag[bo[now]]=-1;
			}
			else if(num[now]==-2)
			{
				flag[bo[now]]=-2;
				atai[bo[now]+1]=1;
			}
			else
			{
				if(flag[bo[now]-1]==-1)
				{
					atai[bo[now]]+=num[now];
				}
				else if(flag[bo[now]-1]==-2)
				{
					atai[bo[now]]*=num[now];
				}
				else cout<<"error"<<endl;
				if(now==n-1||(now!=n-1&&bo[now]>bo[now+1]))
				{
					if(now==n-1)
					{
						owari=1;
					}
					else
					{
						owari=bo[now+1];
					}
					nowb=bo[now];
					while(1)
					{
						if(nowb==owari)
						{
							break;
						}
						if(flag[nowb-2]==-1)
						{
							atai[nowb-1]+=atai[nowb];
						}
						else if(flag[nowb-2]==-2)
						{
							atai[nowb-1]*=atai[nowb];
						}
						else cout<<"error1"<<endl;
						atai[nowb]=0;
						flag[nowb]=0;
						nowb--;
						if(nowb==owari)
						{
							break;
						}
					}
				}
				
			}
			now++;
		}
		cout<<atai[1]<<endl;
	}
	return 0;
}

Measured memory_bytes: 2012
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define foreach(i,n) for(auto &&i:n)
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort(all(x))
#define GSORT(x) sort(all(x), greater<int>())
#define REV(x) reverse(all(x))
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define LOWitr(x, n) lower_bound(all(x), n)
#define UPRitr(x, n) upper_bound(all(x), n)
#define cii(x) int x;cin >> x
#define cill(x) LL x;cin >> x
#define cis(x) string x;cin >> x
#define co(x) cout << x << endl
#define dump(x)  cout << #x << " = " << (x) << endl
#define truecheck assert
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef long long int64;
template<class T,class U>bool chmax(T &a, const U &b) { if (a<b) { a=b; return true; } return false; }
template<class T,class U>bool chmin(T &a, const U &b) { if (b<a) { a=b; return true; } return false; }
const int INF = 1e9, MOD = 1e9 + 7;
const LL LLINF = 1e16;

int main() {
    fastcin();
    cout << fixed << setprecision(10);
    cii(r);
    int ans = 3 * pow(r, 2);
    co(ans);
    return 0;
}

Measured memory_bytes: 256
import re
import copy

N = int(input())
list = list(input().split())
pattern = '[DHSC]'

def bubble_sort(N, cards):
  flag = 1
  while flag == 1:
    flag = 0
    for i in range(N - 1, 0, -1):
      if int(re.sub(pattern, '', cards[i])) < int(re.sub(pattern, '', cards[ i - 1])):
        cards[i], cards[i - 1] = cards[i - 1], cards[i]
        flag = 1
  print(' '.join(cards))
  print('Stable')
  return cards

def selection_sort(N, cards):
  for i in range(N):
    minj = i
    for j in range(i, N):
      # print(j)
      if int(re.sub(pattern, '', cards[j])) < int(re.sub(pattern, '', cards[minj])):
        minj = j

    cards[i], cards[minj] = cards[minj], cards[i]

  print(' '.join(cards))
  return cards

bubblelist = bubble_sort(N, copy.deepcopy(list))
selectionlist = selection_sort(N, copy.deepcopy(list))

print('Not stable') if bubblelist != selectionlist else print('Stable')


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

int main()
{
    int k;
    int a, b;

    cin >> k >> a >> b;

    for(int i=a; i<=b; i++){
        if(i%k == 0) {cout << "OK" << endl; return 0;}
    }

    cout << "NG" << endl;

    return 0;
}

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

using namespace std;

using ll = long long;

ll solve(ll a, ll b) {
    ll res = 2 * (a - 1) + (a + 1 < b);
    ll ok = a, ng = b - 1;
    while (ng - ok > 1) {
        ll mid = (ok + ng) / 2;
        ll hoge = (a * b + mid - 1) / mid - 1;
        if (mid == hoge) {
            res--;
        }
        if (mid > hoge) ng = mid;
        else ok = mid;
    }
    res += (ok - a) * 2;
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        ll a, b;
        cin >> a >> b;
        if (a > b) swap(a, b);
        cout << solve(a, b) << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef long long int lli;
const lli mod = 1e9+7;
using namespace std;

int main()
{
	fast;
lli n,k;cin>>n>>k;
if(k>n)
{
	cout<<min(n,abs(k-n))<<"\n";
}
else
{
	lli x=n%k;
	//cout<<x<<" "<<k;
	cout<<min(x,abs(k-x) )<<"\n";
}
	

}


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

#define INF INT_MAX

using namespace std;

int main(void){
	int x,a,b;
	cin >> x >> a >> b;
	if(a >= b){
		cout << "delicious" << endl;
	}else if(x + a >= b){
		cout << "safe" << endl;		
	}else{
		cout << "dangerous" << endl;
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
	int N, M, K; cin >> N >> M >> K; 
	bool possible = false; 
	for(int i = 0; i <= N; i++){
		for(int j = 0; j <= M; j++){
			if(i * j + (N - i) * (M - j) == K){
				possible = true;
			}
        }
	}
	cout << (possible ? "Yes" : "No") << endl; 
}

Measured memory_bytes: 256
using System;
using System.Linq;

namespace B___Two_Colors_Card_Game
{
    class Program
    {
        static void Main (string[] args)
        {
            var n = int.Parse (Console.ReadLine ());
            var s = new string[n];
            for (var i = 0; i < s.Length; i++)
            {
                s[i] = Console.ReadLine ();
            }
            var m = int.Parse (Console.ReadLine ());
            var t = new string[m];
            for (var i = 0; i < t.Length; i++)
            {
                t[i] = Console.ReadLine ();
            }
            var ans = 0;
            for (var i = 0; i < s.Length; i++) //s
            {
                var a = s.Where (x => x == s[i]).Count ();
                var b = t.Where (x => x == s[i]).Count ();
                ans = Math.Max ((a - b), ans);
            }
            Console.WriteLine (ans);
        }
    }
}

Measured memory_bytes: 11348
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <functional>
#include <utility>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;
const int MOD = 1000000007;

vector<int> v[2020];
int N,K;
int searched[2020];
int ans = 1001001001;
int a[2020],b[2020];
int cnt=1;

void bfs(int start){
  queue<int> q;
  q.push(start);
  searched[start] = 1;
  for(int i=0;i<K/2;i++){
    int l=q.size();
    for(int j=0;j<l;j++){
      int tmp = q.front();
      q.pop();
      int t=v[tmp].size();
      for(int k=0;k<t;k++){
        if(searched[v[tmp][k]] == 0){
          q.push(v[tmp][k]);
          searched[v[tmp][k]] = 1;
          cnt++;
        }
      }
    }
  }
  while(!q.empty()) q.pop();
  if(K%2 == 0 && N-cnt < ans) ans = N-cnt;

}

int main(){
  cin >> N >> K;
  for(int i=0;i<N-1;i++){
    cin >> a[i] >> b[i];
    v[a[i]].push_back(b[i]);
    v[b[i]].push_back(a[i]);
  }
  if(K == 1){
    cout << N-2 << endl;
    return 0;
  }
  if(K%2 == 0){
    for(int i=1;i<=N;i++){
      for(int j=0;j<=N;j++){
        searched[j] = 0;
      }
      cnt = 1;
      bfs(i);
    }
  }
  else{
    for(int i=0;i<N-1;i++){
      for(int j=0;j<=N;j++){
        searched[j] = 0;
      }
      searched[a[i]] = 1;
      searched[b[i]] = 1;
      cnt = 2;
      bfs(a[i]);
      bfs(b[i]);
      if(N-cnt < ans) ans = N-cnt;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
#define REP(i,n) for(int i = 0;i < (n);i++)
#define P pair<int,int>
#define pb push_back
#define mk make_pair
using namespace std;
//const int INF = 1 << 20;
const int INF = 1e9;
typedef long long ll;




int main(){
  ll n,x;
  cin >> n >> x;
  vector <ll> a;
  REP(i,n){
    ll b;
    cin >> b;
    a.pb(b);
  }
  int b[n][n];
  ll res = 1e13;
  REP(i,n){
    b[0][i] = a[i];
  }
  for(int i = 1;i < n;i++){
    REP(j,n){
      int t;
      if(j >= i)
	t = a[j-i];
      else
	t = a[n-(i-j)];
      b[i][j] = min(b[i-1][j],t);
    }
  }
  REP(i,n){
    REP(j,n){
      //cout << b[i][j] << "  " ;
    }
    //cout << endl;
  }
  REP(i,n){
    ll num = 0;

    REP(j,n){
      num += b[i][j];
    }
    
    //cout << num << endl;
    if(res > num+x*i)
      res = num+x*i;
    //res = min(res,num+x*i);
  }
  cout <<res << endl;
 
  return 0;
}



Measured memory_bytes: 15872
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;


int main(){
  ll n; cin >> n; vl high(n);
  rep(i,n){ ll x; cin >> x; high[i] = x; }
  bool flagout = false; bool oute = false;
  
  for(ll i=0; i < n-1; i++){
    if(oute){
      if(high[i]-high[i+1] == 1){
        flagout = true;
        break;
      }
      else if(high[i]==high[i+1]) oute = true;
      else oute = false;
    }
    
    else{
      if(high[i]-high[i+1] > 1){
        flagout = true;
        break;
      }
      else if(high[i]-high[i+1] == 1) oute = true;
    }
  }
  
  if(flagout) cout << "No" << endl;
  else cout << "Yes" << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define REP(i,n) for(auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,n) for(ll i=s, i##_len=(ll)(n); i<i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

int main() {
#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(50);

	ll n, t; cin >> n >> t;
	vl a(n); REP(i, n) cin >> a[i];

	vl ma(n); ma[n - 1] = a[n - 1];
	for (ll i = n - 2; i >= 0; i--) ma[i] = max(a[i], ma[i + 1]);
	vl mi(n); mi[0] = a[0];
	FOR(i, 1, n) mi[i] = min(a[i], mi[i - 1]);
	
	ll ans;
	{
		ll mma = 0, cnt = 0;
		REP(i, n - 1) {
			if (mma <= ma[i + 1] - a[i]) {
				if (mma < ma[i + 1] - a[i]) {
					cnt = 0;
					mma = ma[i + 1] - a[i];
				}
				cnt++;
			}
		}
		ans = cnt;
	}
	{
		ll mma = 0, cnt = 0;
		FOR(i, 1, n) {
			if (mma <= a[i] - mi[i - 1]) {
				if (mma < a[i] - mi[i - 1]) {
					mma = a[i] - mi[i - 1];
					cnt = 0;
				}
				cnt++;
			}
		}
		ans = min<ll>(ans, cnt);
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 2560
function Main(input){
    const arr = input.split('\n');
    const NK = arr[0].split(' ').map(Number);
    const pn = arr[1].split(' ').map(Number);
    let answer = 0;
    pn.sort((a,b)=> a - b);
    for(let i=0; i<NK[1]; i++){
        answer += pn[i];
    }
    console.log(answer);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 29756
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	ll n;
	cin >> n;
	ll i = 1;
	while(1){
		if (i*(i+1)/2 >= n){
			break;
		}
		i++;
	}
	cout << i << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>

int main(void){
    std::string S, ans="AC";
    bool c_flg=false;
    std::cin >> S;

    if (S.at(0) != 'A'){
        ans = "WA";
    } else {
        for (int i=1; i<S.size(); ++i){
            if (std::isupper(S.at(i))){
                if (1 < i && i < S.size() - 1){
                    if (S.at(i) == 'C' && not(c_flg)){
                        c_flg = true;
                    } else {
                        ans = "WA";
                    }
                } else {
                    ans = "WA";
                    break;
                }
            }
        }
    }
    if (not(c_flg)) ans = "WA";

    std::cout << ans << std::endl;

    return 0;
}


Measured memory_bytes: 3596
#include <iostream>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e9;
const ll MOD = 1000000007;
typedef pair<ll,ll> P;
const ll MAX = 20000;
constexpr ll nx[4] = {1,0,-1,0};
constexpr ll ny[4] = {0,1,0,-1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,z,w;
    cin >> n >> z >> w;
    vector<int> a(n);
    cins(all(a));
    if (n == 1) PRINT(abs(a[0]-w));
    else PRINT(max(abs(a[n-1]-w),abs(a[n-1]-a[n-2])));
}


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

#define rep(X,N) for(ll X = 0; X < (N); X++)
#define PI (acos(-1.0))
#define pback push_back
#define mpair make_pair
#define MODN 1000000007
#define ALL(V) (V).begin(),(V).end()
#define INT_MAX_HALF (INT_MAX / 2)
#define EPS (1e-10)

using namespace std;
typedef long long ll;

int main(){

    int n, a;
    cin >> n >> a;

    vector<int> card(n);

    rep(i, n){
        cin >> card.at(i);
    }

    //何枚まで見たか、何枚使ったか、合計はいくつかを表したindexをつかってdp
    vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(n + 1, vector<ll>(n * 50 + 1)));

    dp.at(0).at(0).at(0) = 1;

    rep(i, n){
        rep(j, n){
            rep(k, i * 50 + 1){

                //使わないか使うか

                dp.at(i + 1).at(j).at(k) += dp.at(i).at(j).at(k);
                dp.at(i + 1).at(j + 1).at(k + card.at(i)) += dp.at(i).at(j).at(k);
            }
        }
    }

    ll ans = 0;

    rep(i, n){
        ans += dp[n][i + 1][(i + 1) * a];
    }

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


Measured memory_bytes: 52224
n = int(input())
a = []
b = []
for i in range(n):
    tempa,tempb = map(int,input().split())
    a.append(tempa)
    b.append(tempb)

ans = 0
for i in range(n):
    a[-1-i] += ans
    if a[-1-i]%b[-1-i] != 0:
        ans += b[-1-i] - a[-1-i]%b[-1-i]
print(ans)

Measured memory_bytes: 15016
h, w, k = map(int, input().split())
cs = []

for i in range(h):
    c = list(input())
    cs.append(c)

ans = 0
for i in range(2**h):
    for j in range(2**w):
        cnt = 0
        for ii in range(h):
            for jj in range(w):
                if ((i >> ii) & 1) and ((j >> jj) & 1) and cs[ii][jj] == "#":
                    cnt += 1
        if cnt == k:
            ans += 1
print(ans)


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

ll f(ll d) {
    if (d % 4 == 0) return d;
    if (d % 4 == 1) return 1;
    if (d % 4 == 2) return d + 1;
    return 0;
}

int main() {
    ll a, b;
    cin >> a >> b;
    cout << (f(b) ^ f(a - 1)) << endl;
    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <queue>
#define MOD (1000000007l)
#define rep(i, n) for(long i = 0; i < (n); i++)

using namespace std;

void solve() {
  long N, M;
  cin >> N >> M;
  if (N == 1 and M == 1) cout << 1 << endl;
  else if (N == 1) cout << M - 2 << endl;
  else if (M == 1) cout << N - 2 << endl;
  else cout << (N-2) * (M-2) << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  solve();
  return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <iterator>
#define loop(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define debug(x)  cout << #x << " = " << (x) << endl;

using namespace std;
using LL = long long;
using ULL = unsigned long long;

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

    LL ans = 0;
    rep(i, n) {
        loop(j, i + 1, n){
            ans += d[i] * d[j];
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define INF 2000000000
typedef long long int ll;
typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define DBG(a) cout<<#a<<" ->->->-> "<<a<<"\n"
#define EN cout<<"\n"
#define fi first
#define se second
#define PI (acos(-1))
#define fastread ios::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 200005
template<class T> ostream& operator<<(ostream &s,pair<T,ll> a)
{
    s<<a.fi<<' '<<a.se;
    return s;
}
ll dp[2005][2005];
ll solve(ll ind,ll l,vii &v)
{
    if(ind==v.size())return 0;
    if(dp[ind][l]!=-1)return dp[ind][l];
    return dp[ind][l]=max(solve(ind+1,l+1,v)+abs(v[ind].se-l)*v[ind].fi,solve(ind+1,l,v)+abs(v[ind].se-(ll)v.size()+1+ind-l)*v[ind].fi);
}
int main()
{
    fastread;
    ll T=1;
//    cin>>T;
    for(ll qq=1;qq<=T;qq++)
    {
        ll n;
        memset(dp,-1,sizeof dp);
        cin>>n;
        vii v(n);
        for(int i=0;i<n;i++)
        {
            cin>>v[i].fi;
            v[i].se=i;
        }
        sort(v.begin(),v.end(),greater<ii>());
        cout<<solve(0,0,v)<<endl;
    }
    return 0;
}

Measured memory_bytes: 35248
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;

int main()
{
  int n;
  string command, word;
  map<string, bool> dictionary;

  cin >> n;
  for (int i = 0; i < n; i++)
  {
    cin >> command >> word;
    if (command == "insert")
    {
      dictionary[word] = true;
    }
    else
    {
      if (dictionary[word])
      {
        cout << "yes" << endl;
      }
      else
      {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}


Measured memory_bytes: 52576
//
//  main.cpp
//  atcoder001
//
//  Created by 芦原智隆 on 2018/02/01.
//  Copyright © 2018年 芦原智隆. All rights reserved.
//

#include <iostream>
#include <math.h>
using namespace std;
// クラス宣言部
class myclass{
    
public:
    int input_check(int a, int b, int c ,int d);
    int calc_street(int a, int b, int c ,int d);
};

/*入力チェック*/
int myclass::input_check(int a, int b, int c, int d){
    if (a + b + c < 1){
        return (1);
    }
    if ((a > 50) || (b > 50) || (c > 50)){
        return (2);
    }
    if (d < 50 || d > 20000){
        return (3);
    }
    if (d % 50 != 0 ){
        return (4);
    }
    return (0);
}

/*何通りあるのか計算する*/
int myclass::calc_street(int a, int b, int c, int d){
    int street = 0;
    for(int coin500 = 0; coin500 <= a; coin500++){
        for(int coin100 = 0; coin100 <= b; coin100++){
            for(int coin50 = 0; coin50 <= c; coin50++){
                if((coin500 * 500) + (coin100 * 100) + (coin50 * 50) == d){
                    street = street + 1;
                }
            }
        }
    }
    return street;
}




int main (){
    
    //500円の枚数
    int coin_500;
    cin >>  coin_500;
    
    //100円の枚数
    int coin_100;
    cin >> coin_100;
    
    // 50円の枚数
    int coin_50;
    cin >> coin_50;
    
    // 合計金額
    int total_price;
    cin >> total_price;
    
    myclass x;
    //入力チェックの結果の確認
    int return_i_check = x.input_check(coin_500,coin_100,coin_50,total_price);
    if(return_i_check == 1){
        cout << "コインの合計枚数が１より小さいです" << "\n";
        return 0;
    } else if (return_i_check == 2){
                cout << "コインのいずれかが0〜50の間ではありません" << "\n";
                return 0;
    } else if (return_i_check == 3){
                cout << "合計金額が50〜20000の間ではありません" << "\n";
                return 0;
    } else if (return_i_check == 4){
                cout << "合計金額が５０で割り切れる数字ではありません" << "\n";
                return 0;
    }
    
    //計算結果を求める
    cout <<  x.calc_street(coin_500,coin_100,coin_50,total_price) << "\n";

    return 0;
    
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

int main() {
	int N,H,W;
	scanf("%d %d %d",&N,&H,&W);
	int A[N],B[N];
	for (int i = 0; i < N; i++) {
		scanf("%d %d",&A[i],&B[i]);
	}
	int count=0;
	for (int i = 0; i < N; i++) {
		if (A[i] >= H && B[i] >= W) {
			count++;
		}
	}
	printf("%d\n",count);
	return 0;
}

Measured memory_bytes: 128
import sys

mod = 10**9 + 7

def solve():
    n, m = map(int, input().split())
    x = [int(i) for i in input().split()]
    y = [int(i) for i in input().split()]

    yoko = 0

    for i in range(n - 1):
        yoko += (i + 1) * (n - 1 - i) * (x[i + 1] - x[i])
        yoko %= mod

    tate = 0

    for i in range(m - 1):
        tate += (i + 1) * (m - 1 - i) * (y[i + 1] - y[i])
        tate %= mod

    ans = (tate * yoko) % mod

    print(ans)

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()

Measured memory_bytes: 19648
#line 1 "graph/template.cpp"
#include <vector>
#include <algorithm>
#include <functional>
#include <limits>
#include <queue>
#include <utility>
#include <iostream>

using namespace std;

template< typename T >
struct Edge {
  int from, to;
  T cost;
  Edge() {}
  Edge(int f, int t) : from(f), to(t), cost(1) {}
  Edge(int f, int t, T c) : from(f), to(t), cost(c) {}
  friend bool operator < (const Edge& lhs, const Edge& rhs) { return lhs.cost < rhs.cost; };
  friend bool operator > (const Edge& lhs, const Edge& rhs) { return rhs < lhs; };
  friend bool operator <= (const Edge& lhs, const Edge& rhs) { return !(lhs > rhs); };
  friend bool operator >= (const Edge& lhs, const Edge& rhs) { return !(lhs < rhs); };
};

template< typename T >
using Edges = std::vector< Edge< T > >;
template< typename T >
using Graph = std::vector< Edges< T > >;

template < typename T >
struct Dijkstra {
  using P = std::pair<T, int>; // <始点からの距離, 点のid>
  const T INF; // 十分に大きいがoverflowしない値として、型Tの最大値の半分を使う
  const int V; // 頂点数
  Graph<T> g;
  std::vector<T> dist; // 始点からの距離
  std::vector<bool> visit; // すでに探索済みの点か
  Dijkstra(Graph<T> &g): INF(std::numeric_limits<T>::max()/2), V(g.size()), g(g), dist(V), visit(V) {}
  Dijkstra(Graph<T> &g, int V): INF(std::numeric_limits<T>::max()/2), V(V), g(g), dist(V), visit(V) {}
  void init(int s) {
    std::fill(dist.begin(), dist.end(), INF);
    std::fill(visit.begin(), visit.end(), false);
    dist[s] = T(); // 始点の距離を0で初期化
    std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
    pq.push({dist[s], s});
    while (!pq.empty()) {
      P node = pq.top(); pq.pop();
      int from = node.second;
      if (visit[from]) continue;
      visit[from] = true;
      for (auto &e : g[from]) {
        if (visit[e.to]) continue;
        T d = node.first + e.cost;
        if (d >= dist[e.to]) continue;
        dist[e.to] = d;
        pq.push({d, e.to});
      }
    }
  }
};


int main() {
  int n, m;
  cin >> n >> m;
  Graph<int> g(n*3);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v; --u; --v;
    g[u*3].emplace_back(u*3, v*3+1);
    g[u*3+1].emplace_back(u*3+1, v*3+2);
    g[u*3+2].emplace_back(u*3+2, v*3);
  }
  Dijkstra<int> di(g);
  int s, t;
  cin >> s >> t;
  --s; --t;
  di.init(s*3);
  int ans = di.dist[t*3];
  if (ans == di.INF) {
    cout << -1 << endl;
  }
  else {
    cout << ans / 3 << endl;
  }
}


Measured memory_bytes: 34304
n = int(input())
d = {'M':0, 'A':0, 'R':0, 'C':0, 'H':0}
va = []
ans = 0
for i in range(n):
    s = input()
    if s[0] in d:
        d[s[0]] += 1
for i in d:
    if d[i] > 0:
        va.append(d[i])
v = len(va)

if v < 3:
    print(0)
else:
    for i in range(v-2):
        for j in range(i+1,v-1):
            for k in range(j+1,v):
                ans += va[i]*va[j]*va[k]
    print(ans)



Measured memory_bytes: 3064
import numpy as np

def resolve():
    A, B, M = map(int, input().split())
    al = list(map(int, input().split()))
    bl = list(map(int, input().split()))
    xyc = [list(map(int, input().split())) for _ in range(M)]
    #print(xyc)

    # 割引なしの状態での最小の組み合わせ
    i_no = np.argmin(al)
    j_no = np.argmin(bl)
    v_no= al[i_no] + bl[j_no]

    #print(i_no, j_no, v_no)

    v_min = v_no
    for c in xyc:
        v = al[c[0]-1] + bl[c[1]-1] - c[2]
        if v < v_min:
            v_min = v

    print(v_min)

if __name__ == "__main__":
    resolve()

Measured memory_bytes: 48252
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 20:57:54 2020

@author: masat
"""

a, b = map(int, input().split())

answer = []
if a <= b:
    answer = [str(a)]*b
else:
    answer = [str(b)]*a

print("".join(answer))



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

int main (){
    string a, b, c;
    cin >> a >> b >> c;
    if(b[0]==a[(int)(a.length())-1] && b[(int)(b.length())-1]==c[0]){
        cout << "YES" << endl;
    }else {
        cout << "NO" << endl;
    }
}

Measured memory_bytes: 256
$input = <>;chomp $input;

$ans = 1;
$max_count = 0;
for my $num (1..$input) {
    my $n = $num;
    my $count = 0;
    while ($n % 2 == 0) {
        $count++;
        $n /= 2;
    }
    if ($count > $max_count) {
        $max_count = $count;
        $ans = $num;
    }
}

print $ans;


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

int s,y,m;

int main(){
    cin>>s;
    y=s/100,m=s%100;
    if(1<=m && m<=12){
        if(1<=y && y<=12) cout<<"AMBIGUOUS";
         else cout<<"YYMM";
    } else{
        if(1<=y && y<=12) cout<<"MMYY";
        else cout<<"NA";
    }
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 210

struct Info{
	double x,y,speed;
};

struct Data{
	Data(double arg_time,int arg_index){
		time = arg_time;
		index = arg_index;
	}
	bool operator<(const struct Data &arg) const{
		return time < arg.time;
	}
	double time;
	int index;
};

int N,M;
int V; //頂点数
vector<int> G[NUM]; //グラフの隣接リスト表現
int match[NUM]; //マッチングのペア
bool used[NUM]; //DFSですでに調べたかのフラグ
Info troops[100],bases[100];
vector<Data> DIST[100];


//fromとtoを結ぶ辺をグラフに追加する
void add_edge(int from,int to){
	G[from].push_back(to);
	G[to].push_back(from);
}

//増加パスをDFSで探す(node_idのペアを探す)
int dfs(int node_id){
	used[node_id] = true;

	for(int i = 0; i < G[node_id].size(); i++){
		int adj_node_id = G[node_id][i],pair_id = match[adj_node_id];
		if((pair_id < 0)|| //隣接するノードに、まだペアが存在しない
				(used[pair_id] == false && dfs(pair_id) == true)){

			match[node_id] = adj_node_id;
			match[adj_node_id] = node_id;
			return true;
		}
	}
	return false;
}


int bipartie_matching(){
	int ret = 0;
	for(int i = 0; i < V; i++)match[i] = -1; //ペアなしにしておく
	for(int node_id = 0; node_id < V; node_id++){
		if(match[node_id] < 0){ //node_idにペアがいない場合
			for(int i = 0; i < V; i++)used[i] = false;
			if(dfs(node_id)){ //増加パスが存在する場合
				ret++;
			}
		}
	}
	return ret;
}

bool is_OK(double time){
	for(int i = 0; i < NUM; i++)G[i].clear();

	int left,right,m,max_index;


	//troopから、time内に到達できる基地に辺を張る
	for(int i = 0; i < N; i++){
		left = 0,right = M-1,m = (left+right)/2;
		max_index = -1;

		while(left <= right){
			if(DIST[i][m].time <= time){
				max_index = m;
				left = m+1; //より右へ
			}else{
				right = m-1; //より左へ
			}
			m = (left+right)/2;
		}

		if(max_index == -1)continue; //到達できる基地がない

		for(int k = 0; k <= max_index; k++){
			add_edge(i,N+DIST[i][k].index);
		}
	}

	return  bipartie_matching() == M;
}

double calc_time(Info a,Info b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))/a.speed;
}


void func(){

	for(int i = 0; i < 100; i++)DIST[i].clear();

	V = N+M;

	for(int i = 0; i < N; i++){
		scanf("%lf %lf %lf",&troops[i].x,&troops[i].y,&troops[i].speed);
	}

	for(int i = 0; i < M; i++){
		scanf("%lf %lf",&bases[i].x,&bases[i].y);
	}

	double tmp_time;

	//troopとbaseの時間距離を求める
	for(int i = 0; i < N; i++){
		for(int k = 0; k < M; k++){
			tmp_time = calc_time(troops[i],bases[k]);
			DIST[i].push_back(Data(tmp_time,k));
		}
		sort(DIST[i].begin(),DIST[i].end());
	}

	double ans = 99999999999.0;
	double left = 0.0,right = ans,m = (left+right)/2;

	while(right-left > EPS){
		if(is_OK(m)){
			ans = m;
			right = m-EPS;
		}else{
			left = m+EPS;
		}
		m = (left+right)/2;
	}

	printf("%.20lf\n",ans);
}

int main(){

	while(true){
		scanf("%d %d",&N,&M);
		if(N == 0 && M == 0)break;

		func();
	}

	return 0;
}



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

const int INF = 1e+9 * 2;
double eps = 1e-10;

string days[] = {"Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"};
int main() {
	int m, d;
	while(cin >> m >> d && (m || d)){
		int cnt = 0;
		for(int i = 1; i <= 12; ++i){
			for(int j = 1; j <= 31; ++j){
				if(m == i && j == d){
					cout << days[cnt % 7] << endl;
					goto end;
				}
				cnt++; 
				if(i == 2 && j == 29)
					break;
				else if((i == 4 || i == 6 || i == 9 || i == 11) && j == 30)
					break;
			}
		}
		end:;
	}
	return 0;
}

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

int n,a,minNum,maxNum;
long sum=0;

int main(){
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>a;
        if(i == 0){
            minNum=a;
            maxNum=a;
        }
        else{
            if(a < minNum)minNum = a;
            if(a > maxNum)maxNum = a;
        }
        sum+=a;
    }
    cout<<minNum<<" "<<maxNum<<" "<<sum<<endl;
    return 0;
}


Measured memory_bytes: 3104
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

int a, b, c;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> a >> b >> c;
	if (a + b + c >= 22) cout << "bust";
	else cout << "win";
}

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> P(N + 1, 0);
  for (int i = 0; i < N - 1; i++) {
    P[i + 2] = P[i + 1];
    if (S[i] == 'A' && S[i + 1] == 'C') P[i + 2]++;
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << P[r] - P[l] << '\n';
  }
  return 0;
}


Measured memory_bytes: 1488
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main(){
    int n;
    cin >> n;
    int max_num = 1;
    int max_cnt = 0;
    for ( int i = 1; i <= n; ++i ) {
        int tmp_num = i;
        int tmp_cnt = 0;
        while(1) {
            if ( tmp_num % 2 == 1 ) {
                break;
            }
            tmp_num /= 2;
            ++tmp_cnt;
        }
        if ( max_cnt < tmp_cnt ) {
            max_num = i;
            max_cnt = tmp_cnt;
        }
    }
    cout << max_num << endl;
    return 0;
}


Measured memory_bytes: 256
from collections import deque
from copy import deepcopy


def main():
    h, w = map(int, input().split())
    grid = [list(input()) for _ in range(h)]
    ans = 0
    d = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    for i in range(h):
        for j in range(w):
            if grid[i][j] == "#":
                continue
            now_grid = deepcopy(grid)
            answer = [[float("inf") for _ in range(w)] for _ in range(h)]
            next = deque([[i, j]])
            answer[i][j] = 0
            while next:
                y, x = next.popleft()
                if now_grid[y][x] == ".":
                    now_grid[y][x] = "#"
                    for dx, dy in d:
                        if 0 <= x + dx < w and 0 <= y + dy < h and now_grid[y + dy][x + dx] == ".":
                            answer[y + dy][x + dx] = min(answer[y][x] + 1, answer[y + dy][x + dx])
                            next.append([y + dy, x + dx])
            for k in range(h):
                for l in range(w):
                    if grid[k][l] == "#":
                        continue
                    ans = max(ans, answer[k][l])
    print(ans)


if __name__ == '__main__':
    main()



Measured memory_bytes: 3436
x1, x2, x3, x4, x5 = map(int, input().split())
if (x1 == 0):
    ans = 1
if (x2 == 0):
    ans = 2
if (x3 == 0):
    ans = 3
if (x4 == 0):
    ans = 4
if (x5 == 0):
    ans = 5
print(int(ans))

Measured memory_bytes: 9116
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++ i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; -- i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++ i)
#define fore(e, u, v) for (int p = e(u), v = e[p].y; p; v = e[p = e[p].nxt].y)
#define ri rd<int>
using namespace std;
const int maxN = 1e5 + 7;

template<class T> inline T rd() {
	bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
	T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x;
}

int n, m;
int fa[maxN];
int ans = 0;
int depth[maxN];

struct vec {
	static const int maxE = maxN;
	int g[maxN], te;
	struct edge {int y, nxt;} e[maxE];
	inline void push(int x, int y) {e[++te] = (edge){y, g[x]}; g[x] = te;}
	inline void link(int x, int y) {push(x, y), push(y, x);}
	inline int& operator () (int x) {return g[x];}
	inline edge& operator [] (int x) {return e[x];}
}e;

void dfs(int x) {
	depth[x] = 1;
	fore (e, x, y) {
		dfs(y);
		depth[x] = max(depth[x], depth[y] + 1);
	}
	if (x != 1 && fa[x] != 1 && depth[x] == m - 1) {
		ans ++;
		depth[x] = 0;
	}
}

int main() {

	n = ri(), m = ri() + 1;
	rep (i, 1, n) fa[i] = ri();

	ans = (fa[1] != 1); fa[1] = 1;
	rep (i, 2, n) e.push(fa[i], i);
	dfs(1);
	printf("%d\n", ans);

	return 0;
}


Measured memory_bytes: 6784
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define FOR(i, o, n) for (auto i = (o); (i) < (n); ++(i))
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (auto i = (n)-1; (i) >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define fi first
#define se second
#define ll long long
#define V2(name, h, w) vector<vector<int> > name((h), vector<int>(w));
int main(){
	int A, B; cin >> A >> B;
	if(A == 1) A = 14;
	if(B == 1) B = 14;
	if(A == B) cout <<  "Draw" << endl; 
	else cout << (A > B ? "Alice" : "Bob") << endl;
}


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

int main()
{
    int x,a,b;
    cin>>x>>a>>b;
    int k=-a+b;
    if(k<=0)cout<<"delicious"<<endl;
    else if(k<=x)cout<<"safe"<<endl;
    else cout<<"dangerous"<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
  while(true){
	int n;
	cin>>n;
	if(n==0)
		break;

	int t[1000000];
        int c[1000000];
        
      
	for(int i=0;i<n;i++){
		int c1,m1,s1,m2,s2,m3,s3,m4,s4;
		cin>>c1>>m1>>s1>>m2>>s2>>m3>>s3>>m4>>s4;
	       t[i]=(m1+m2+m3+m4)*60+(s1+s2+s3+s4);
		c[i]=c1;
	}

       
        
	for(int j=0;j<n;j++){
	for(int i=0;i<n-1;i++){
		if(t[i]>t[i+1]){
			swap(t[i],t[i+1]);
			swap(c[i],c[i+1]);
		}
	}
	}

        int p1=c[0];
	 int p2=c[1];
	 int p3=c[n-2];

	cout<<p1<<endl;
	cout<<p2<<endl;
	cout<<p3<<endl;
}
       
        return 0;
}
		
		
	

Measured memory_bytes: 3232
//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
ll arr[15][15];
ll n,m,x; 
vector<ll> v;
ll solve(int idx, vector<ll> v)
{
    if(idx==n+1){
        // for(int i=1;i<=m;i++) cout<<v[i]<<" ";
        // cout<<endl;
        for(int i=1;i<=m;i++){
            if(v[i]<x) return INT_MAX; 
        }
        return 0;
    }
    ll x=solve(idx+1,v);
    for(int j=2;j<=m+1;j++) v[j-1]+=arr[idx][j];
    // cout<<"COST"<<arr[idx][1]<<endl;
    ll y=arr[idx][1]+solve(idx+1,v);
    // cout<<idx<<" GG "<<x<<" "<<y<<endl;
    return min(x,y);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>x;
    for(int i=1;i<=n;i++){
        ll a; cin>>a;
        arr[i][1]=a;
        for(int j=1;j<=m;j++) cin>>arr[i][j+1];
    }
    for(int i=0;i<=m;i++) v.push_back(0);
    ll ans=solve(1,v);
    if(ans>=INT_MAX) ans=-1;
    cout<<ans<<endl;
}

Measured memory_bytes: 3664
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author hamaron
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <climits>

using namespace std;

class BFrog2 {
public:
    void solve(std::istream& in, std::ostream& out) {
        int N, K;
        in >> N >> K;

        int h[N+1];
        for (int i = 1; i < N+1; i++) {
            in >> h[i];
        }

        int dp[N+1];
        for (int j = 1; j < N+1; j++) {
            if (j == 1) {
                dp[j] = 0;
                continue;
            }

            if (j == 2) {
                dp[j] = abs(h[1] - h[2]);
                continue;
            }

            int temp = INT_MAX;
            for (int k = 1; k < K+1; k++) {
                if (j - k >= 1) {
                    temp = min(temp, dp[j-k] + abs(h[j-k] - h[j]));
                }
            }

            dp[j] = temp;
        }

        out << dp[N];
    }
};


int main() {
	BFrog2 solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 1024
import sys
#input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()


def II(): return int(input())


def MI(): return map(int,input().split())


def MF(): return map(float,input().split())


def LI(): return list(map(int,input().split()))


def LF(): return list(map(float,input().split()))


def TI(): return tuple(map(int,input().split()))


# rstrip().decode('utf-8')
from math import *


def main():
	a,b,c,d=MI()
	c-=a
	d-=b
	
	s="URDLLURDRDLU"
	li=[d,c,d,c,1,d+1,c+1,1,1,d+1,c+1,1]
	
	t=""
	for i in range(len(s)):
		t+=s[i]*li[i]
	print(t)
	




	

if __name__=="__main__":
	main()


Measured memory_bytes: 9112
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int dep[200005];
void dfs(int u, int p = 0){
    dep[u] = dep[p] + 1;
    for(int v : g[u]){
        if(v == p) continue;
        dfs(v, u);
    }
}
int bckt[2];
int col[200005];
int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i = 1; i < n; i++){
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1);
    for(int i = 1; i <= n; i++){
        bckt[dep[i] % 2]++;
    }
    int t1 = n / 3 + (n % 3 > 0 ? 1 : 0);
    int t2 = n / 3 + (n % 3 == 2 ? 1 : 0);
    if((bckt[0] < bckt[1] && bckt[0] >= t1) || bckt[1] < t1){
        for(int i = 1; i <= n && t1 > 0; i++){
            if(dep[i] % 2 == 0){
                col[i] = 1;
                t1--;
            }
        }
    }else{
        for(int i = 1; i <= n && t1 > 0; i++){
            if(dep[i] % 2 == 1){
                col[i] = 1;
                t1--;
            }
        }
    }
    bckt[0] = bckt[1] = 0;
    for(int i = 1; i <= n; i++){
        if(col[i] == 0) bckt[dep[i] % 2]++;
    }
    if(bckt[0] >= t2){
        for(int i = 1; i <= n && t2 > 0; i++){
            if(col[i] == 0 && dep[i] % 2 == 0){
                col[i] = 2;
                t2--;
            }
        }
    }else{
        for(int i = 1; i <= n && t2 > 0; i++){
            if(col[i] == 0 && dep[i] % 2 == 1){
                col[i] = 2;
                t2--;
            }
        }
    }
    int p0 = 3;
    int p1 = 1;
    int p2 = 2;
    for(int i = 1; i <= n; i++){
        switch(col[i]){
            case 0:
                cout << p0 << " ";
                p0 += 3;
                break;
            case 1:
                cout << p1 << " ";
                p1 += 3;
                break;
            case 2:
                cout << p2 << " ";
                p2 += 3;
                break;
        }
    }
    cout << endl;
    return 0;
}

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

int h,w;
vector < vector <int> >c;

int dy[8]={-1,-1,-1,0,0,1,1,1};
int dx[8]={-1,0,1,-1,1,-1,0,1};

void dfs(int i, int j){
    c[i][j]=0;
    rep(dir,8){
        int vy=i+dy[dir];
        int vx=j+dx[dir];
        if(vy>=0&&vy<h&&vx>=0&&vx<w&&c[vy][vx]==1){
            dfs(vy,vx);
        }
    }
    return;
}

int main(){
    while(1){
        cin >> w >> h;
        if(h==0&&w==0)break;
        c = vector <vector <int> >(h, vector <int> (w) );
        rep(i,h)rep(j,w)cin >> c[i][j];
        int cnt=0;
        rep(i,h)rep(j,w){
            if(c[i][j]==1){
                dfs(i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


Measured memory_bytes: 3156