akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds;   
#define ll long long
#define ull unsigned long long
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> 
ll mod=1e9+7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a,ll b){if(a>b){return a;}return b;}
ll min(ll a,ll b){if(a<b){return a;}return b;}
#define MAXN 200005
// Comment this out for interactice problem
// #define endl '\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
 
// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output

int main() 
{ 
    fastio;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);

    ll n;
    cin>>n;
    vector<ii> v;
    ll a[n];
    for (int i = 0; i < n; ++i)
    {
        cin>>a[i];
        if(i==0){
            v.push_back({a[i], 0});
        }
        else{
            if(a[i]!=v.back().F){
                v.push_back({a[i], i});
            }
        }
    }

    ll ans=1000;
    ll i = 0; 
    while (i < n-1) { 
        while ((i < n-1) && (a[i + 1]<=a[i])) {
            i++; 
        }
  
        if (i==(n - 1)) {
            break; 
        }
  
        ll buy = i++;
        while ((i < n) && (a[i] >= a[i - 1])) {
            i++; 
        }
        ll sell = i-1; 

        ll bought_stocks = ans/a[buy];
        ll rem = ans%a[buy];
        ll sold_money = bought_stocks*a[sell];
        ans=sold_money+rem;
    }
    cout<<ans;

    return 0;
}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
typedef std::pair<int, int> ipair;
#define int long long
#define pb push_back
#define ff first
#define INF INT_MAX
#define ss second
#define rep(i, a) for (int i = 0; i < a; i++)
#define fr(i, j, a) for (int i = j; i < a; i++)
#define cnt_ones(x) __builtin_popcount(x)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define mod 1000000007
#define IOS                                                                    \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
void init() {
  int n,K;
  cin >> n>>K;
  vector<int> h(n), dp(n,INT_MAX);
  fr(i, 0, n) cin >> h[i];
  dp[0] = 0;
  /*fr(i, 0, n) {
      fr(j,i,i+k+1){
          if(j<n){
              dp[j]=min(dp[j],dp[i]+abs(h[i]-h[j]));
          }
      }
  }
  }*/
  for(int i=0;i<n;i++){
    for(int k=K;k>=0;k--){
      if(i-k>=0) dp[i]=min(dp[i],dp[i-k]+abs(h[i]-h[i-k]));
    }
  }
  cout<<dp[n-1]<<endl;
}
int32_t main() {
  IOS;
  init();
  return 0;
}


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

typedef long long ll;
typedef long double ld;

#define pb push_back
#define sort(s) sort(s.begin(),s.end())
#define reverse(s) reverse(s.begin(),s.end())
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)

const ll mod = 1e9+7;

//最大公約数
ll gcd(ll a, ll b) {
    return b ? gcd(b, a%b) : a;
}

//最小公倍数
ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

//素数判定
bool isPrime(ll x){
    ll i;
    if(x < 2)return 0;
    else if(x == 2) return 1;
    if(x%2 == 0) return 0;
    for(i = 3; i*i <= x; i += 2) if(x%i == 0) return 0;
    return 1;
}

//桁和
int digsum(ll n) {
    int res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

//桁数
int dignum(ll n) {
    int res = 0;
    while(n > 0) {
        res++;
        n /= 10;
    }
    return res;
}

//文字列中の特定の文字カウント
ll stringcount(string s,char c){
    return count(s.cbegin(),s.cend(),c);
}



int main(){
    int n,d,x;
    cin >> n >> d >> x;
    int a[n];
    rep(i,n) cin >> a[i];
    int ans=x;
    for(int i=0;i<n;i++){
        for(int j=0;j<d;j++){
            if((a[i]*j+1)<=d){
                ans++;
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<vector>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define rep(i,a,b) for(int i=(a),i##_end_=(b);i<=i##_end_;i++)
#define dwn(i,a,b) for(int i=(a),i##_end_=(b);i>=i##_end_;i--)
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=310;
const int mod=1000000007;
int n,m,f[maxn][maxn];
vector<pii> G[maxn];
int check(int i,int j,int k) {
	int p=max(i,max(j,k));
	rep(z,0,G[p].size()-1) if((i>=G[p][z].xx)+(j>=G[p][z].xx)+(k>=G[p][z].xx)!=G[p][z].yy) return 0;
	return 1;
}
int main() {
	n=read();m=read();
	rep(i,1,m) {
		int l=read(),r=read(),v=read();
		G[r].pb(mp(l,v));
	}
	f[0][0]=1;
	int ans=0;
	rep(i,0,n) dwn(j,i,0) dwn(k,j,0) if(check(i,j,k)) {
		int res=f[j][k];
		if(i==n) (ans+=res)%=mod;
		(f[i][k]+=res)%=mod;
		(f[i][j]+=res)%=mod;
	}
	else f[j][k]=0;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 640
#include<algorithm>
#include<functional>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

typedef unsigned long long UInt64;
typedef long long Int64;

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

using namespace std;

template<class _RanIt,class _Diff,class _Tmp> inline void _insertion_sort (_RanIt _Array,_Diff _Count,_Tmp)
{
	_Tmp tmp;
	for(_Diff i = 1; i < _Count; i++)
	{
		tmp = *(_Array + i);
		if( *(_Array + i - 1) > tmp)
		{
			_Diff j = i;
			do
			{
				*(_Array + j) = *(_Array + j - 1);
				j--;
			}while(j > 0 && *(_Array + j - 1) > tmp);
			*(_Array + j) = tmp;
		}
	}

}

template<class _RanIt,class _Diff,class _Tmp,class _Pr> inline void _insertion_sort (_RanIt _Array,_Diff _Count,_Tmp,_Pr _Pred)
{
	_Tmp tmp;
	for(_Diff i = 1; i < _Count; i++)
	{
		tmp = *(_Array + i);
		if( _Pred(*(_Array + i - 1) , tmp))
		{
			_Diff j = i;
			do
			{
				*(_Array + j) = *(_Array + j - 1);
				j--;
			}while(j > 0 && _Pred(*(_Array + j - 1) , tmp));
			*(_Array + j) = tmp;
		}
	}

}
	
template<class _RanIt> inline void insertion_sort(_RanIt _First,_RanIt _Last)
{
	_insertion_sort(_First,_Last - _First,*(_First));

}

template<class _RanIt,class _Pr> inline void insertion_sort(_RanIt _First,_RanIt _Last,_Pr _Pred)
{
	_insertion_sort(_First,_Last - _First,*(_First),_Pred);

}


//bubble_sort
template<class T> inline void myswap(T& a,T& b)
{
	T tmp;
	tmp = a;
	a = b;
	b = tmp;
}


template<class _RanIt,class _Diff> inline	void _bubble_sort (_RanIt _Array,_Diff _Count)
{
	for(_Diff i = 0; i < _Count; i++)
	{
		for(_Diff j = _Count - 1; j > i; j--)
		{
			if(_Array[j] < _Array[j-1])
			{
				myswap(_Array[j],_Array[j-1]);
			}

		}
	}	
}

template<class _RanIt,class _Diff,class _Pr> inline void _bubble_sort (_RanIt _Array,_Diff _Count,_Pr Pred)
{
	for(_Diff i = 0; i < _Count; i++)
	{
		for(_Diff j = _Count - 1; j > i; j--)
		{
			if(Pred(_Array[j] , _Array[j-1]))
			{
				myswap(_Array[j],_Array[j-1]);
			}

		}
	}	

}

template<class _RanIt> inline	void bubble_sort(_RanIt _First,_RanIt _Last)
{
	_bubble_sort(_First,_Last - _First);
}

template<class _RanIt,class _Pr> inline	void bubble_sort (_RanIt _First,_RanIt _Last, _Pr _Pred)
{
	_bubble_sort(_First,_Last - _First,_Pred);
}
	
int main()
{
	
	int a[10];
	rep(i,10) cin>>a[i];

	insertion_sort(a,a+10);


	cout<<a[9]<<endl;
	cout<<a[8]<<endl;
	cout<<a[7]<<endl;

	return 0;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
#define rep(i,n) for (int i=0;i<(n);i++)
#define PI 3.14159265358979323846264338327950L
using namespace std;
using P = pair<int,int>;
using ll = long long;
using namespace std;
int main(void){
    // Your code here!
    int n, m;
    cin >> n >> m;
    int a[n];
    int ans=0;
    rep(i, n){cin >> a[i]; ans+= a[i];}
    sort(a, a+n, greater<int>());
    double ans2 = ans/4.0/m;
    rep(i, m){
        if(a[i] < ans2){cout << "No" << endl; return 0;}
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 3600
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int main(){
  string s;
  cin >> s;
  cout << s[0] << s[1] << s[2] << endl;
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, s, n) for (int i = (s); i < (n); ++i)
#define rrep(i, n, g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define len(x) (int)(x).size()
#define dup(x, y) (((x) + (y)-1) / (y))
#define pb push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int a, b;
  cin >> a >> b;
  if(a < b) cout << 0 << endl;
  else cout << 10 << endl;
}

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

#define p_ary(ary,a,b,i) do { cout << "["; for (int i = a;i <= b;++i) cout << ary[i] << (b == i ? "]\n" : ", "); } while(0)
#define p_map(map,it) do {cout << "{";for (auto it = map.begin();;++it) {if (it == map.end()) {cout << "}\n";break;}else cout << "" << it->first << "=>" << it->second << ", ";}}while(0)

typedef pair<int, int> P;

int main() {
    int h,w,n,x,y;
    vector<vector<int>> obs(200010);
    vector<P> xy;
    map<P,bool> is_obs;

    cin >> h >> w >> n;
    for (int i = 0;i < n;++i) {
        cin >> x >> y;
        xy.push_back(P(x,y));
    }
    sort(xy.begin(),xy.end());
    for (int i = 0;i < n;++i) {
        obs[xy[i].second].push_back(xy[i].first);
        is_obs[xy[i]] = true;
    }
    for (int i = 1;i <= h;++i) is_obs[P(i,w+1)] = true;
    for (int i = 1;i <= w;++i) {
        obs[i].push_back(h+1);
        is_obs[P(h+1,i)] = true;
    }

    int i = 0,ans = 1000000;
    x = 1;y = 1;
    while (1) {
        if (i%2) {
            int m = *lower_bound(obs[y].begin(),obs[y].end(),x);
            ans = min(ans,m-1);
            if (!is_obs[P(x,y+1)]) y++;
        } else {
            if (is_obs[P(x+1,y)]) {
                ans = min(ans,x);
                break;
            }
            x++;
        }
        i++;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 71024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200000 + 5;
int n, a[N];

ll absLL(ll x) {
	if(x < 0) return -x;
	return x;
}

ll calc(ll x) {
	ll ret = 0;
	for(int i=0; i<n; ++i)
		ret += absLL(a[i] - x);
	return ret;
}

int main() {
	scanf("%d", &n);
	for(int i=0; i<n; ++i) {
		scanf("%d", a+i);
		a[i] -= i+1;
	}

	ll l = -2e9, r = 2e9, m1, m2, r1, r2, res = 1e18;
	while(l <= r) {
		m1 = (l+l+r)/3;
		m2 = (l+r+r)/3;
		r1 = calc(m1);
		r2 = calc(m2);
		if(r1 <= r2) {
			r = m2-1;
			res = min(r1, res);
		} else {
			l = m1+1;
			res = min(r2, res);
		}
	}
	printf("%lld\n", res);
	return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
    int f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
    while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
    return f * x; 
}
int n, m, k;
int a[100005], b[100005];
void init(){
    n = read(), m = read(), k = read();
    for (int i = 1; i <= n; ++i)
        a[i] = read();
    for (int i = 1; i <= m; ++i)
        b[i] = read();
}
void solve(){
    int minia = INT_MAX, minib = INT_MAX;
    for (int i = 1; i <= n; ++i)
        minia = min(minia, a[i]);
    for (int i = 1; i <= m; ++i)
        minib = min(minib, b[i]);
    int ans = minia + minib;    
    for (int i = 1; i <= k; ++i){
        int x = read(), y = read(), c = read();
        ans = min(ans, a[x] + b[y] - c);
    }
    printf("%d\n", ans);
}
int main(){
    init();
    solve();
    return 0;
}

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

int main() {
 int a,b,c,d;
  cin >> a >> b >> c >> d;
  int leftWeight = a + b;
  int rightWeight = c + d;
  if( leftWeight > rightWeight) {
   cout << "Left" << endl; 
  } else if( leftWeight == rightWeight) {
   cout << "Balanced" << endl;
  } else {
   cout << "Right" << endl;
  }
  
}


Measured memory_bytes: 256
// abc135d: Digits Parade

// 多桁の数の剰余 a%b=r は、
// 上位桁(a_0) -> (a_0)%b=(r_0), 下位桁(a_1) -> ((r_0)*10+(a_1))%b=(r_1) ...
// で、全体の余りが求まる

// 解説より
// 漸化式
// dp[i][j] := 先頭からの i 文字で考えられる数のうち，%13 == j であるものの数
// i の昇順に dp テーブルを見ると，i−1 文字目までを 13 で割ったあまり (dp[i−1][j] の j) と
// s[i] としてあり得る数字を全て試すことで dp[i][0] ∼ dp[i][12] の値がわかります．

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;

#define MOD 1000000007
#define upto(i,s,e,d) for(int i=(s);i<(e);i+=(d))
#define in(x) cin>>(x)
#define oute(x) cout<<(x)<<endl

int main(){
  string s;in(s);
  int n = s.size();
  vector<vector<ull>> dp(n+1, vector<ull>(13));
  dp[0][0] = 1;
  upto(i,0,n,1){
    int digit = s[i]=='?'? -1: s[i]-'0';
    upto(low,0,10,1){
      // s[i] が数字のときは その数だけを処理
      // s[i] が ? のときは 0~9 を処理
      if(digit<0 || low==digit)
        upto(r,0,13,1) dp[i+1][(r*10 + low)%13] += dp[i][r];
    }
    upto(r,0,13,1) dp[i+1][r] %= MOD;
  }
  oute(dp[n][5]);
}

Measured memory_bytes: 13696
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

int N;
string ans;

int main(void) {

	cin >> N;

	int y = N % 10;
	if (y == 3) { ans = "bon"; }
	else if (y == 0 || y == 1 || y == 6 || y == 8) { ans = "pon"; }
	else { ans = "hon"; }
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rrep(i, n) for(ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
#define debug(V) rep(i, V.size()) cout << V[i] << " ";
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v;
string S, T;
vl A, B;
void input() {
    cin >> n;
    rep(i, n) {
        cin >> x;
        A.pb(x);
        a ^= x;
    }
}
void solve() {
    rep(i, n) { cout << (a ^ A[i]) << " "; }
}
int main() {
    // cout<<fixed<<setprecision(15);
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    solve();
}

Measured memory_bytes: 5316
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n-1);i>=0;i--)
#define FOR(i,n,m) for(int i=n;i<=(int)(m);i++)
#define RFOR(i,n,m) for(int i=(int)(n);i>=m;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
const double PI=3.14159265358979323846;
using namespace std;
vector<int> dy={1,0,-1,0};
vector<int> dx={0,1,0,-1};
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

int main(){
    int n,m;
    cin>>n>>m;
    cout<<(100*(n-m)+1900*m)*pow(2,m)<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  int n, t, a; cin >> n >> t >> a;
  t *= 1000, a *= 1000;
  V<> h(n); for (int i = 0; i < n; ++i) cin >> h[i];
  int res = -1, mn = 1e9;
  for (int i = 0; i < n; ++i) {
    if (mn > abs(t - 6 * h[i] - a)) {
      res = i;
      mn = abs(t - 6 * h[i] - a);
    }
  }
  cout << res + 1 << '\n';
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
// template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }


int main() {
    int n, m;
    cin >> n >> m;
    if (abs(n - m) >= 2) {
        cout << 0 << endl;
        return 0;
    }
    ll ans = 1;
    if (n > m) swap(n, m);
    rep3(i, 2, n + 1) (ans *= i) %= inf;
    rep(i, n - 1) (ans *= (m - i)) %= inf;
    // どちらにせよ
    (ans *= 2) %= inf;
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<class F>auto recursive(F f){return[f](auto...a){return f(f,a...);};}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int p;fixprec(int p):p(p){}}; ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.p);}
/*!* [modint] *!*/
/*+* 剰余環整数 *+*/
struct int_m;
int_m invmod(int_m);
int_m powmod(int_m, i64);
struct int_m {
    static i64 m;
    i64 v;
    int_m() {}
    int_m(i64 x) { v = (x % m + m) % m; }
    int_m operator+(const int_m &y) const { return v + y.v > m ? v + y.v - m : v + y.v; }
    int_m operator-(const int_m &y) const { return v - y.v < 0 ? v - y.v + m : v - y.v; }
    int_m operator*(const int_m &y) const { return (v * y.v) % m; }
    bool operator==(const int_m &y) const { return v == y.v; }
    bool operator!=(const int_m &y) const { return v != y.v; }
    void operator+=(const int_m &y) { *this = *this + y; }
    void operator-=(const int_m &y) { *this = *this - y; }
    void operator*=(const int_m &y) { *this = *this * y; }
    friend istream &operator>>(istream &ist, int_m &m) { ist >> m.v; return ist; }
    friend ostream &operator<<(ostream &ost, int_m m) { ost << m.v; return ost; }
    int_m operator^(const i64   &y) const {
        int_m v = 1; int p = y;
        for (int_m b = *this; p > 0; p >>= 1, b *= b) if (p & 1) v *= b;
        return v;
    }
    void operator^=(const i64   &y) { *this = *this ^ y; }
    // for prime mod
    int_m operator/(const int_m &y) const { return *this * (y ^ (m - 2)); }
    void operator/=(const int_m &y) { *this = *this / y; }
};
i64 int_m::m;

/*!* [modfact] *!*/
/*+* 剰余階乗
     requirement: modint *+*/
struct ModFact {
    static vector<int_m> val, inv;
    static void Init(int N) { // O(N)
        val = vector<int_m>(++N, 1), inv = vector<int_m>(N, 1);
        for(int i = 1; i < N; i++) val[i] = val[i - 1] * i;
        inv[N - 1] /= val[N - 1];
        for(int i = N - 1; i > 0; i--) inv[i - 1] = inv[i] * i;
    }
};
vector<int_m> ModFact::val, ModFact::inv;
int_m Permutation(int n, int m) {
    if(n < m || m < 0 || n < 0) return 0;
    return ModFact::val[n] * ModFact::inv[n - m];
}
int_m Combination(int n, int m) {
    if(n < m || m < 0 || n < 0) return 0;
    return ModFact::val[n] * ModFact::inv[m] * ModFact::inv[n - m];
}
int_m Combination(vector<int> &n) {
    int s = accumulate(n.begin(), n.end(), 0);
    int_m r = ModFact::val[s];
    for(auto x : n) r *= ModFact::inv[x];
    return r;
}
int_m Multichoose(int n, int m) { return Combination(n + m - 1, m); }

int main() {
    int_m::m = 1e9 + 7;
    ModFact::Init(1e6);
    int N, M;
    cin >> N >> M;
    int_m A = Permutation(M, N) ^ 2;
    int_m Q = 0;
    for(int k = 1; k <= N; k++) {
        Q += (int_m(-1)^(k + 1)) *
             Permutation(M, k) *
             Combination(N, k) *
             (Permutation(M - k, N - k) ^ 2);
    }
    A -= Q;
    cout << A << endl;
    return 0;
}


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

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

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

#define MAX 100005
#define INF 1001001001

int main(int, char**)
{
  int n, W;
  cin >> n >> W;

  vector<P> item;
  rep(i,n) {
    int v, w;
    cin >> v >> w;
    item.emplace_back(v,w);
  }
  sort(item.begin(), item.end(), [&](P a, P b)->bool{ return (ll)a.first*b.second > (ll)b.first*a.second; });
  double ans = 0;
  rep(i,n) {
    int v, w;
    tie(v,w) = item[i];
    if (W < w) {
      ans += (double)v*W/w;
      break;
    } else {
      ans += v;
      W -= w;
    }
  }
  cout << setprecision(20) << ans << endl;

  return 0;
}



Measured memory_bytes: 3996
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

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

typedef long long ll;

const int Inf = 1e9;
const double EPS = 1e-9;

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

int lcm(int a, int b) {
    return a * b / gcd(a, b);
}

int bitCount(long bits) {
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

int getHeight(int n) {
  int sum = 0;
  n--;
  while (n > 0) {
    sum += n;
    n--;
  }
  return sum;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a, b, diff, height, ans;
    cin >> a >> b;
    diff = b - a;
    height = getHeight(diff);
    ans = height - a;
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 256
d,g = map(int,input().split())
po = [0]
co = [0]
for i in range(d):
    p,c = map(int,input().split())
    po.append(p)
    co.append(c)

# f(g,i) :　残りG点で、i番目の問題を最大いくつ選べるか
# i番目の問題をn問解いて、
# その得点sがGより小さかったら、f(G-s,i-1)をnに加える。
# ここでiをデクリメントするのは、完全に解く問題より高い得点の問題を
# 中途半端に解くことが最適な問題の選び方ではないため。
# i == 0 の時は float('inf') を返す。（解けない場合）
def f(G,i):
    if i == 0:
        return float("inf")
    n = min(G//(i * 100),po[i])
    s = 100 * i * n 
    if n == po[i]:
        s += co[i]
    if s < G:
        n += f(G-s,i-1)
    return min(n,f(G,i-1))

print(f(g,d))

Measured memory_bytes: 3064
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <fstream>
#include <cmath>
#include <vector>

using namespace std;

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define dum(x) cout<<#x<<'='<<x<<endl
#define darr(arr,n) rep(i,(n-arr)){cout<<*(arr+i)<<' ';}cout<<endl;

int main() {
    int X;
    cin >> X;
    int ansarr[100];
    int N = 0;
    if (X < 4) {
        cout << 1 << endl;
        return 0;
    }
    for (int i = 2; i <= 34; i++) {
        if (X < i*i) {
            break;
        }
        int ans = 1;
        int cou = 0;
        int copyX = X;
        while (true) {
            copyX /= i;
            cou++;
            if (copyX < i) {
                for(int j=0;j<cou;j++){
                    ans *= i;
                }
                ansarr[N] = ans;
                N++;
                break;
            }
        }
    }
    sort(ansarr, ansarr + N);
    cout << ansarr[N - 1] << endl;
}

Measured memory_bytes: 256
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline()
    count = 0
    s1 = []
    for i, x in enumerate(S):
        if x == 'W':
            count += 1
        s1.append(count)
    count = 0
    s2 = []
    for i, x in enumerate(S[::-1]):
        if x == 'E':
            count += 1
        s2.append(count)
    s2.reverse()

    result = N
    for i in range(N):
        count = 0
        idx = i - 1
        if idx >= 0:
            count += s1[idx]
        idx = i + 1
        if idx < N:
            count += s2[idx]
        result = min(result, count)

    print(result)


if __name__ == '__main__':
    main()


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

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

const ll MOD = 1000000007;
const int MN = 100010;

int N;
int A[MN];
ll ret;
ll inv[MN];
ll ac[MN];

int main() {
	inv[1] = 1;
    for (int i = 2; i < MN; ++i) {
        inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    }

    for (int i = 1; i < MN; ++i) {
    	ac[i] = (ac[i-1] + inv[i]) % MOD;
    }

	cin >> N;
	rep(i, N) cin >> A[i];

	rep(i, N) {
		int l = i;
		int r = N - 1 - i;
		ll t = (ac[l+1] - 1 + ac[r+1]) * A[i] % MOD;
		ret = (ret + t) % MOD;
	}

	for (int i = 1; i <= N; ++i) {
		ret = ret * i % MOD;
	}

	cout << ret << endl;
	return 0;
}

Measured memory_bytes: 2176
import java.util.Arrays;
import java.util.Scanner;


/**
 * Created by Reopard on 2014/06/05.
 */
public class Main{
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args){
        int a, d, n;
        boolean[] prime = new boolean[1000000];
        Arrays.fill(prime, true);
        prime[0] = false;
        prime[1] = false;
        for(int i = 0; i < prime.length; i++)
            if(prime[i])
                for(int j = i + i; j < prime.length; j += i) prime[j] = false;
        while((a = sc.nextInt()) != 0 && (d = sc.nextInt()) != 0 && (n = sc.nextInt()) != 0){
            for(int i = a; i < prime.length; i += d){
                if(prime[i]) n--;
                if(n == 0){
                    System.out.println(i);
                    break;
                }
            }
        }
    }
}

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

const ll INF = 1001001001;

int main(){
  int n; cin>>n;
  int a=n/100;
  int b=(n%100)/10;
  int c=n%10;
  if(a==7 || b==7 || c==7){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;
  }
}


Measured memory_bytes: 3596
// Lec08-D Ring
#include<iostream>
#include<string>
int main(){
  std::string str1, str2;
  std::getline(std::cin, str1);
  std::getline(std::cin, str2);
  int str1_len=str1.length(), str2_len=str2.length(),tmp;
  bool jdg;
  for(int i=0; i<str1_len; i++){
    jdg = 0;
    for(int j=0; j<str2_len; j++){
      i+j<str1_len ? tmp=i+j : tmp=i+j-str1_len;
      if(str1[tmp] != str2[j]) break;
      if(str2_len == j + 1) jdg = 1;
    }
    if(jdg) break;
  }
  std::cout << (jdg ? "Yes" : "No") << std::endl;
  return 0;
}

Measured memory_bytes: 3196
import java.util.*;
public class Main {
	
	
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[0]-h[1]);
		for(int i =2; i < n; i++) {
			int one = dp[i-1] + Math.abs(h[i-1] - h[i]);
			int two = dp[i-2] + Math.abs(h[i-2] - h[i]);
			dp[i] = Math.min(one, two);
		}
		System.out.println(dp[n-1]);
		//System.out.println(Arrays.toString(dp));
	}
	
}


Measured memory_bytes: 50644
#include <iostream>
#include "bits/stdc++.h"
#include <vector>
using namespace std;
typedef long long ll;

int main() {
	ll x, y, a, b, c,mem,sum=0;
	cin >> x >> y >> a >> b >> c;
	vector<ll> p{0};
	vector<ll> q{0};
	vector<ll> r{0};
	vector<ll> memory;
	int tomeru = 1;
	for (int i =0 ; i < a; i++) {
		cin >> mem;
		p.push_back(mem);
	}
	for (int i = 0; i < b; i++) {
		cin >> mem;
		q.push_back(mem);
	}
	for (int i = 0; i < c; i++) {
		cin >> mem;
		r.push_back(mem);
	}
	sort(p.begin(), p.end(), std::greater<ll>());
	sort(q.begin(), q.end(), std::greater<ll>());
	
	for (ll i = 0; i < x; i++) {
		r.push_back(p[i]);
	}
	for (ll i = 0; i < y; i++) {
		r.push_back(q[i]);
	}
	sort(r.begin(), r.end(), std::greater<ll>());
	for (ll i = 0; i < x+y; i++) {
		sum = r[i] + sum;
	}
	cout << sum << endl;

}

Measured memory_bytes: 7536
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define SIZE 262144
class segtree
{
public:
	int seg[SIZE * 2];
	void update(int a, int b)
	{
		a += SIZE;
		seg[a] = b;
		for (;;)
		{
			a /= 2;
			if (a == 0)break;
			seg[a] = min(seg[a * 2], seg[a * 2 + 1]);
		}
	}
	int get(int beg, int end, int node, int lb, int ub)
	{
		if (ub < beg || end < lb)return 1000000000;
		if (beg <= lb&&ub <= end)return seg[node];
		return min(get(beg, end, node * 2, lb, (lb + ub) / 2), get(beg, end, node * 2 + 1, (lb + ub) / 2 + 1, ub));
	}
};
segtree tree1, tree2;
typedef pair<int, int>pii;
typedef pair<pii, pii>pi4;
int dat[SIZE];
int rdat[SIZE];
pii get(int lb, int ub)
{
	if (lb % 2 == 0)
	{
		int z = tree1.get(lb, ub, 1, 0, SIZE - 1);
		int y = tree2.get(rdat[z] + 1, ub, 1, 0, SIZE - 1);
		return make_pair(z, y);
	}
	else
	{
		int z = tree2.get(lb, ub, 1, 0, SIZE - 1);
		int y = tree1.get(rdat[z] + 1, ub, 1, 0, SIZE - 1);
		return make_pair(z, y);
	}
}
int main()
{
	int num;
	scanf("%d", &num);
	for (int i = 0; i < num; i++)scanf("%d", &dat[i]), dat[i]--, rdat[dat[i]] = i;
	for (int i = 0; i < num; i++)
	{
		if (i % 2 == 0)tree1.update(i, dat[i]), tree2.update(i, 1000000000);
		else tree2.update(i, dat[i]), tree1.update(i, 1000000000);
	}
	priority_queue<pi4>que;
	pii zz = get(0, num - 1);
	que.push(make_pair(make_pair(-zz.first, -zz.second), make_pair(0, num - 1)));
	for (;;)
	{
		if (que.empty())break;
		pi4 z = que.top();
		que.pop();
		int x = -z.first.first, y = -z.first.second;
		int lb = z.second.first, ub = z.second.second;
		printf("%d %d\n", x + 1, y + 1);
		x = rdat[x], y = rdat[y];
		if (lb < x)
		{
			pii t = get(lb, x - 1);
			que.push(make_pair(make_pair(-t.first, -t.second), make_pair(lb, x - 1)));
		}
		if (x + 1 < y)
		{
			pii t = get(x + 1, y - 1);
			que.push(make_pair(make_pair(-t.first, -t.second), make_pair(x + 1, y - 1)));
		}
		if (y < ub)
		{
			pii t = get(y + 1, ub);
			que.push(make_pair(make_pair(-t.first, -t.second), make_pair(y + 1, ub)));
		}
	}
}

Measured memory_bytes: 7924
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
#define ll long long

#define md 1000000007
#define mx 200005
int arr[mx];

int main(){
    int n,k;
    scanf("%d %d",&n,&k);

    for(int i=0;i<n;i++){
        scanf("%d",arr+i);
    }

    sort(arr,arr+n,[](int a, int b){return abs(a)>abs(b);});

    bool solved=false;

    int fks=1;
    for(int i=0;i<k;i++){
        if(arr[i]<0) fks*=-1;
        if(arr[i]==0) fks*=0;
    }

    if(fks==1){
        solved=true;
        ll prod=1;
        for(int i=0;i<k;i++) prod=(prod*arr[i])%md;
        printf("%lld\n", prod);
    }
    else if(fks==0){
        solved=true;
        printf("0\n");
    }
    else{
        int last_neg=1,last_pos=-1,fst_neg=1,fst_pos=-1;
        int lpidx,lnidx;

        for(int i=0;i<k;i++){
            if(arr[i]>0) {last_pos=arr[i]; lpidx=i;}
            if(arr[i]<0) {last_neg=arr[i]; lnidx=i;}
        }
        for(int i=n-1;i>=k;i--){
            if(arr[i]>0) fst_pos=arr[i];
            if(arr[i]<0) fst_neg=arr[i];
        }

        ll prod1=-1,prod2=-1;
        if(last_neg<0 && fst_pos>0){
            solved=true;
            prod1=fst_pos;
            for(int i=0;i<k;i++) if(i!=lnidx) prod1=(prod1*arr[i])%md;
            assert(prod1>=0);
        }
        if(fst_neg<0 && last_pos>0){
            solved=true;
            prod2=fst_neg;
            for(int i=0;i<k;i++) if(i!=lpidx) prod2=(prod2*arr[i])%md;
            assert(prod2>=0);
        }

        if(prod1>=0 and prod2>=0){
            if(fst_pos*(ll)last_pos > fst_neg*(ll)last_neg){
                printf("%lld\n",prod1);
            }
            else{
                printf("%lld\n",prod2);
            }
        }
        else if(solved){
            printf("%lld\n",max(prod1,prod2));
        }
    }

    if(!solved){
        if(arr[n-1]==0){
            solved=true;
            printf("0\n");
        }
        else{
            solved=true;
            ll prod=1;
            for(int i=0;i<k;i++) prod=(prod*arr[n-1-i])%md;
            if(prod<0) prod=prod+md;
            printf("%lld\n",prod);
        }
    }
}


Measured memory_bytes: 2408
#define _USE_MATH_DEFINES

#include <bits/stdc++.h>
using namespace std;
using i32 = int; using i64 = long long int; using f64 = double; using str = string;
template <typename T> using vec = vector<T>;
template <typename T> using heap = priority_queue<T, vec<T>, greater<T>>;
#define times(n, i) for (i32 i = 0; i < (n); ++i)
#define range(a, b, i) for (i32 i = (a); i < (b); ++i)
#define upto(a, b, i) for (i32 i = (a); i <= (b); ++i)
#define downto(a, b, i) for (i32 i = (a); i >= (b); --i)
#define all(xs) (xs).begin(), (xs).end()
#define sortall(xs) sort(all(xs))
#define reverseall(xs) reverse(all(xs))
#define uniqueall(xs) (xs).erase(unique(all(xs)), (xs).end())
#define even(x) (((x) & 1) == 0)
#define odd(x) (((x) & 1) == 1)
#define head(xs) *(xs).begin()
#define last(xs) *(xs).rbegin()
#define append emplace_back
#define findge lower_bound
#define findgt upper_bound
const i64 MOD = 1000000007;
const f64 EPS = 1e-10;

i64 n;

i32 main()
{
    cin >> n;
    vec<i64> a(n);
    times(n, i) {
        cin >> a[i];
    }
    i64 ans = 0;
    i64 acc = 0;
    vec<i64> b; b.insert(b.end(), all(a));
    times(n, i) {
        if (even(i)) {
            if (acc + b[i] <= 0) {
                i64 t = (1 - acc) - b[i];
                b[i] += t;
                ans += abs(t);
            }
        }
        else {
            if (acc + b[i] >= 0) {
                i64 t = (-1 - acc) - b[i];
                b[i] += t;
                ans += abs(t);
            }
        }
        acc += b[i];
    }
    i64 ans2 = 0;
    acc = 0;
    b.clear(); b.insert(b.end(), all(a));
    times(n, i) {
        if (odd(i)) {
            if (acc + b[i] <= 0) {
                i64 t = (1 - acc) - b[i];
                b[i] += t;
                ans2 += abs(t);
            }
        }
        else {
            if (acc + b[i] >= 0) {
                i64 t = (-1 - acc) - b[i];
                b[i] += t;
                ans2 += abs(t);
            }
        }
        acc += b[i];
    }
    cout << min(ans, ans2) << endl;
    return 0;
}

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

template <typename T>
void print_vec(const vector<T>& v, bool is_reverse=false, ll num=0){
  if(num == 0) num = (ll)v.size();
  cout << endl; cout << "i=  ";for(ll i=0; i<num; i++) cout << i << "   ";cout << endl;  
  cout << "    ";
  if(is_reverse) for(ll i=num-1; i>=0; i--){ cout<<v[i]; if(i!=0) cout<<"   ";}
  else for(ll i=0; i<num; i++){ cout<<v[i]; if(i!=num-1) cout<<"   ";}
  cout << endl;
}

// template <typename T>
// void print_pairvec(const vector<T> &v, ll num=0){
//   if(num == 0) num = (ll)v.size();
//   cout << endl; for(ll i=0; i<num; i++){ cout << v[i].first << " " << v[i].second << endl;}
// }
template <typename T>
void print_pairvec(const T &_pair, ll num=0){
  cout << endl; for(pair<ll, int> x: _pair){ cout << x.first << " " << x.second << endl;}
}

template <typename T>
void print_vec2(const vector<vector<T>>& v){
  cout << endl; cout << "       ";
  for(ll i=0; i<v[0].size(); i++) cout << i << "   "; 
  cout << endl;
	for(ll i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(ll j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

const int INF = 1e+5;
int op(int a, int b){return max(a, b);}
// int e(){return -INF;}
int e(){return 0;}
int main(){
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for(int i=0; i<n; i++) cin >> a[i];
  
  const int A_MAX = 3e5 + 5;
  segtree<int, op, e> seg(A_MAX+5);
  // dp[j]: 最後がjであるときのaの部分列の最大長
  for(int i=0; i<n; i++){
    int j = a[i];
    int l = max(0, j-k);
    int r = min(j+k+1, A_MAX);
    seg.set(j,  seg.prod(l, r)+1);
  }

  // int ans = seg.all_prod();
  int ans = seg.prod(0, A_MAX);
  cout << ans <<endl;
  return 0;
  // cout << fixed;//小数点を確実に10桁で表示するためのもの
  // cout << setprecision(10); 
}


Measured memory_bytes: 9640
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <iterator>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define MOD 1000000007
using namespace std;


int main(int argc, char* argv[])
{
    char s[105]={0};
    char t[105]={0};
    scanf("%s", s);
    scanf("%s", t);

    long lens=strlen(s);
    sort(s+0, s+lens);

    long lent=strlen(t);
    sort(t+0, t+lent, greater<char>());

    if(strcmp(s, t)<0) {
        printf("Yes\n");
    }
    else {
        printf("No\n");
    }

    return 0;
}


Measured memory_bytes: 128
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

inline int getint()
{
	static char c;
	while ((c = getchar()) < '0' || c > '9');

	int res = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		res = res * 10 + c - '0';
	return res;
}

template <class T>
inline void relax(T &a, const T &b)
{
	if (b > a)
		a = b;
}

const int MaxN = 100000;

int n, nK;
vector<int> adj[MaxN + 1];

int res = 0;

int dfs(const int &u)
{
	int depth = 0;
	for (int v : adj[u])
	{
		int d = dfs(v);
		if (d == nK)
			res += u != 1;
		else
			relax(depth, d);
	}
	return depth + 1;
}

int main()
{
	cin >> n >> nK;
	for (int u = 1; u <= n; ++u)
	{
		int v = getint();
		if (u != 1)
			adj[v].push_back(u);
		else
			res += v != 1;
	}

	dfs(1);

	cout << res << endl;

	return 0;
}

Measured memory_bytes: 11904
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    }
}


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

using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
#define DEBUG freopen("in.txt", "r", stdin);

struct fastio {
    fastio() {
        ios::sync_with_stdio(false);
        cout << setprecision(10) << fixed;
        cin.tie(0);
    }
};

fastio _fast_io;

const int N = 1e5 + 5;
int n, m, k;
ll ans;
string s;
//  M, A, R, C or H.
int a[5];
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> s;
        if (s[0] == 'M')
            ++a[0];
        else if (s[0] == 'A')
            ++a[1];
        else if (s[0] == 'R')
            ++a[2];
        else if (s[0] == 'C')
            ++a[3];
        else if (s[0] == 'H')
            ++a[4];
    }
    for (int i = 0; i < 5; ++i) {
        for (int j = i + 1; j < 5; ++j) {
            for (int l = j + 1; l < 5; ++l) {
                ans += 1LL * a[i] * a[j] * a[l];
            }
        }
    }
    cout << ans << endl;
    return 0;
}


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

using ll = long long;

int main() {
    // dp[i][j] y_i=jであるときの二乗和の最小値
    int N, M;
    cin >> N >> M;
    while (N != 0) {
        vector<int> C(M), x(N);
        rep(i, M) cin >> C[i];
        rep(i, N) cin >> x[i];
        vector<vector<int>> dp(N + 1, vector<int>(256, 1e9));
        dp[0][128] = 0;
        rep(i, N) {
            rep(j, 256) {
                rep(k, M) {
                    int y = j + C[k];
                    if (y < 0) y = 0;
                    if (y > 255) y = 255;
                    dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + (x[i] - y) * (x[i] - y));
                    /*
                    if (dp[i + 1][y] < 1e9) {
                        cout << "dp[" << i + 1 << "][" << y << "]=" << dp[i + 1][y] << "\n";
                    }
                    */
                }
            }
        }
        int ans = 1e9;
        rep(j, 256) {
            ans = min(ans, dp[N][j]);
        }
        cout << ans << "\n";
        cin >> N >> M;
    }
}


Measured memory_bytes: 23744
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;

int n;
string str[12];
vector<char> v;
map<char,int> m;
int num[15];
int z[2];
bool used[15];
int ppow10[10];
int flu[10][2];
bool cant[15];

void Pow10(){
	int ret = 1;
	rep(i,10){
		ppow10[i] = ret;
		ret *= 10;
	}
}

int func(int x){
	int ret = 0;
	if(x <= v.size()-1){
		rep(i,10){
			if(used[i]) continue;
			if(cant[x] && i == 0) continue;
			num[x] = i;
			used[i] = true;
			rep(j,2) z[j] += flu[x][j]*i;
			ret += func(x+1);
			rep(j,2) z[j] -= flu[x][j]*i;
			num[x] = -1;
			used[i] = false;
		}
	} else{
		if(z[0] == z[1]) return 1;
		else return 0;
	}
	return ret;
}

void solve(){
	v.clear();
	m.clear();
	rep(i,15) cant[i] = false;
	rep(i,2) z[i] = 0;
	rep(i,10) rep(j,2) flu[i][j] = 0;
	rep(i,15){ num[i] = -1; used[i] = false;}
	rep(i,12) str[i].clear();
	rep(i,n){
		cin >> str[i];
		rep(j,str[i].size()){
			bool ok = true;
			rep(k,v.size()) if(v[k] == str[i][j]){
				if(i != n-1) flu[m[v[k]]][0] += ppow10[str[i].size()-1-j];
				else flu[m[v[k]]][1] += ppow10[str[i].size()-1-j];
				ok = false;
			}
			if(ok) v.push_back(str[i][j]);
			if(ok) m[str[i][j]] = v.size()-1;
			if(ok){
				if(i != n-1) flu[m[str[i][j]]][0] += ppow10[str[i].size()-1-j];
				else flu[m[str[i][j]]][1] += ppow10[str[i].size()-1-j];
			}
			if(j == 0 && str[i].size() > 1) cant[m[str[i][j]]] = true;
		}
	}
	cout << func(0) << endl;
}

int main(){
	Pow10();
	while(true){
		cin >> n;
		if(n == 0) break;
		solve();
	}
}

Measured memory_bytes: 1228
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] in = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		
		int tmp = in[0] - 1;
		int s = in[1] - tmp;
		int e = in[1] + tmp;
		List<String> list = new ArrayList<>();
		IntStream.rangeClosed(s, e).forEach(c -> list.add(String.valueOf(c)));
		String coordinate = String.join(" ", list);
		
		System.out.println(coordinate);
	}
}


Measured memory_bytes: 28368
# 初期入力
import math
from math import ceil
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())

num_prob =1/N
ans =0
for i in range(1,N+1):
    
    if K <=i:
        ans =(ans + num_prob)
    else:
        x =ceil(math.log2(K/i))
        ans += num_prob *(0.5**x) #サイコロの確立と表が出続ける確率
print(ans)

Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <sstream>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <utility> //pair

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

typedef long long ll;

using namespace std;

int main() {
    
    int a,b,c;
    
    cin >> a >> b >> c;
    
    vector<int> ans(6);
    ans[0] = abs(b-a) + abs(c-b);
    ans[1] = abs(a-b) + abs(c-a);
    ans[2] = abs(c-a) + abs(b-c);
    ans[3] = abs(c-b) + abs(a-c);
    ans[4] = abs(b-c) + abs(a-b);
    ans[5] = abs(a-c) + abs(b-a);
    
    /*
    FOR(i, 0, 6){
        cout << ans[i] << endl;
    }
     */
    
    int min = *min_element(ans.begin(),ans.end());
    
    cout << min << endl;
    return 0;
}


Measured memory_bytes: 256
n = int(input())
b = input()
a = [int(i) for i in b.split()]

ans = 1
for i in range(n):
    if a[i] == 0 : 
        ans = 0
        break

if ans :
    for i in range(n):
        ans *= a[i];
        if ans > 1000000000000000000 :
            ans = -1
            break
        
print(ans)
    

Measured memory_bytes: 23608
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
using namespace std;
typedef long long ll;

int main(){
  int n, m;cin>>n>>m;
  int maxl=-1, minr=10000000;
  for(int i=0;i<m;i++){
  	int l, r;cin>>l>>r;
    maxl=max(maxl, l);minr=min(minr, r);
  }
  int ans;
  if(n<maxl || maxl>minr){
    ans=0;
  }else{
    ans=min(n, minr)-maxl+1;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256