akhauriyash/Code-Regression huggingface dataset:
using System;

class Program
{

    static int N, M, Q;
    static int[,] P;
    static int[] A;

    static int dfs(int i) {
        int score = 0;
        
        if(i==N+1) {
            for(int q=0; q<Q; ++q) {
                int a = P[q,0],
                    b = P[q,1],
                    c = P[q,2],
                    d = P[q,3];
                   
                if(A[b]-A[a]==c) score+=d;
            }
            return score;
        }

        for(int x=A[i-1]; x<=M; ++x) {
            A[i]=x;
            score = Math.Max(score, dfs(i+1));
        }
        return score;
    }
    
    static void Main()
    {
        string[] buf;
        
        buf = Console.ReadLine().Split(new char[]{' '});
        N = int.Parse(buf[0]);
        M = int.Parse(buf[1]);
        Q = int.Parse(buf[2]);

        P = new int[Q,4];
        for(int i=0; i<Q; ++i) {
            buf = Console.ReadLine().Split(new char[]{' '});
            for(int j=0; j<4; ++j) P[i,j] = int.Parse(buf[j]);
        }

        A = new int[N+1];
        A[0] = 1;
        Console.WriteLine(dfs(1));
        
    }
}


Measured memory_bytes: 17924
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

vector<int> carrot(10010,0);
int n,k,t,u,v,l,d;


int main(){
	cin>>n>>k>>t>>u>>v>>l;
	for(int i=0; i<n; i++){
		cin>>d;
		carrot.at(d)+=1;
	}
	int boost_l=0;
	int normal_l=0;
	int haveNum=0;
	bool flag=false;
	int end_l=0;
	for(int i=0; i<l; i++){
		if(i==end_l)flag=false;
		if(carrot.at(i)>0){
			if(!flag){
				flag=true;
				end_l=i+v*t;
				//cout<<"d:"<<d<<endl;
			}
			else{
				if(haveNum<k)haveNum++;
				else {
					end_l=i+v*t;
				}
			}
		}
		else{
			if(!flag&&haveNum>0){
				haveNum--;
				flag=true;
				end_l=i+v*t;
			}
		}
		if(flag){
			//cout<<"boost:"<<i<<endl;
			boost_l++;
		}
		else normal_l++;
	}
	printf("%.9lf\n",(double)boost_l/v+(double)normal_l/u);
	//cout<<"bl:"<<boost_l<<" nl:"<<normal_l<<endl;
	//cout<<"bt:"<<(double)boost_l/v<<" nt:"<<(double)normal_l/u<<endl;
}

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

using namespace std;
using ull = unsigned long long;
using ll = long long;
using lli = long long int;
using ld = long double;
using pa = pair<int,int>;
using ppa = pair<pa,int>;
#define SORT(v, n) sort(v, v+n);
#define ALL(v) v.begin(),v.end()
#define VSORT(v) sort(ALL(v))
#define GRESORT(v) sort(ALL(v),greater<ll>())
#define REVERSE(v) reverse(ALL(v))
#define overlap(v) v.erase(unique(ALL(v)),v.end())
#define debug(x) cout << #x << ": " << x << endl
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i = (b-1); i >= a; i--)
#define rrep(i,n) RFOR(i,0,n)
#define INF INT_MAX
#define SUP INT_MIN
#define mins(a,b) a = min(a,b)
#define maxs(a,b) a = max(a,b)

//4??????
int dy[]={0, 1, 0, -1};
int dx[]={1, 0, -1, 0};

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

const ll MOD = 1000000007;

ll pow_mod(ll a,ll b,ll mod){
    ll tmp = 1;
    a %= mod;
    if(b > 0){
        tmp = pow_mod(a, b/2, mod);
        if(b % 2) tmp = (((tmp*tmp)%mod)*a)%mod;
        else tmp = (tmp*tmp)%mod;
    }
    return tmp;
}


void solve() {
    ll a,b,c,d,p; cin >> a >> b >> c >> d >> p;
    ll sum1 = a * p, sum2 = 0;
    if(c > p) sum2 = b;
    else sum2 = b + (p-c) * d;
    cout << min(sum1,sum2) << endl;
}

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

    solve();

    return 0;
}

Measured memory_bytes: 3220
/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
					*/

#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>

#pragma GCC optimize("Ofast")
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define rep2(i, n) for(int i = 2; i < (n); ++i)
#define repr(i, n) for(int i = n; i >= 0; --i)
#define reprm(i, n) for(int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printin(a) printf(a ? "possible\n" : "inposible\n")
#define printdb(a) printf("%.50lf\n", a)//少数出力
#define printdbd(a) printf("%.16lf\n", a)//少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str())//string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2)//等差数列の和、初項,末項,項数
#define pb push_back
#define priq priority_queue
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg)/360.0)*2.0*PI)
#define rad_to_deg(rad) (((rad)/2.0/PI)*360.0)
#define Please return
#define AC 0

using ll = long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1000000007;
const double PI = acos(-1);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);//最初の改行対策
	while (c != '\n' && c != -1) {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

/*-----------------------------------------ここからコード-----------------------------------------*/


int main() {
	
	int n, k;
	scanf("%d%d", &n, &k);
	printyn(n & 1 && k & 1);
	
	Please AC;
}


Measured memory_bytes: 888
#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;

int main()
{
  int r; scanf("%d", &r);
  double pi = acos(-1);
  printf("%.15lf\n", 2*pi*r);
  return 0;
}

Measured memory_bytes: 3816
function myin(){return require("fs").readFileSync("/dev/stdin", "utf8").trim();}
function myout(t){console.log(t);}//standard output
function myerr(t){console.error(t);}//standard error
//[no]param
//0:何もしない  1:数値へ変換  2:半角SPで分割  3:改行で分割  4:半角SPで分割し、数値配列へ
//5:改行で分割し、数値配列へ  6:1文字に分割  7:1文字に分割し、数値配列へ
function myconv(i,no){switch(no){case 0:return i;case 1:return parseInt(i);case 2:return i.split(" ");case 3:return i.split("\n");case 4:return i.split(" ").map((a)=>Number(a));case 5:return i.split("\n").map((a)=>Number(a));case 6:return i.split("");case 7:return i.split("").map((a)=>Number(a));}}

function Main(input) {
  input = myconv(input,4).sort(function(a,b){return a-b;});
  //3つとも偶奇が一致しているか確認
  //一致しないときは必ず2つは偶奇が一致する。この二つを+1する
  //「最大から足りない分/2回」足せば数値も一致する
  var A = input[0];
  var B = input[1];
  var C = input[2];
  var output = 0;
  if(!(A % 2 == B % 2 && A % 2 == C % 2 && C % 2 == B % 2)){
     if(A % 2 == B % 2){
        A++;
       B++;
     }else if(A % 2 == C % 2){
       A++;
       C++;
     }else if(C % 2 == B % 2){
       B++;
       C++;
     }
    output++;
  }
  output += (Math.max.apply(null,[A,B,C]) - A)/2;
  output += (Math.max.apply(null,[A,B,C]) - B)/2;
  output += (Math.max.apply(null,[A,B,C]) - C)/2;
  myout(output);
}

Main(myin());


Measured memory_bytes: 9420
<?php
$n=trim(fgets(STDIN));
$a=explode(" ",trim(fgets(STDIN)));
$i=1;
$s=$a[0];
while($i<$n){
  $s=gmp_xor($s,$a[$i]);
  ++$i;
}
$i=0;
while($i<$n){
  if($i>0){
    echo " ";
  }
  echo gmp_xor($s,$a[$i]);
  ++$i;
}

Measured memory_bytes: 31824
/*
AuThOr Gwj
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr<<A.FIR<<" "<<A.SEC<<endl;
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
int val[505][505];
int N;
int dp[505][505];
vector<mp> v;
int res=0;
int walk[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool is[505][505];
int calc(int x,int y){
	if(x==1||x==N||y==1||y==N) return 0;
	int res=INF;
	rep(i,4)
	{
		 res=min(res,dp[x+walk[i][0]][y+walk[i][1]]+is[x+walk[i][0]][y+walk[i][1]]);
	}
	return res;
}
int main(){
	fastio;
	cin>>N;
	rb(i,1,N*N){
		int x,y,p;
		cin>>p;
		x=(p+N-1)/N;
		y=p-N*(x-1);
		v.PB(II(x,y));
	}
	rb(i,1,N)
		rb(j,1,N)
			dp[i][j]=min(min(i-1,j-1),min(N-i,N-j)),is[i][j]=1; 
	rb(i,0,N*N-1){
		int x,y;
		x=v[i].FIR;
		y=v[i].SEC;
		res+=dp[x][y];
		is[v[i].FIR][v[i].SEC]=0;	
		queue<mp> q;
		rep(j,4){
			int x_,y_;
			x_=v[i].FIR+walk[j][0];
			y_=v[i].SEC+walk[j][1];
			if(x_&&y_&&x_<=N&&y_<=N&&calc(x_,y_)!=dp[x_][y_]){
				
			dp[x_][y_]=calc(x_,y_);
			q.push(II(x_,y_));
			}
			
		}
		while(!q.empty()){
			int X,Y;
			X=q.front().FIR;
			Y=q.front().SEC;
			q.pop();
			rep(j,4){
			int nx,ny;
			nx=walk[j][0]+X;
			ny=walk[j][1]+Y;		
			if(nx<=N&&nx&&ny<=N&&ny)
			if(calc(nx,ny)!=dp[nx][ny]){
				dp[nx][ny]=calc(nx,ny);
				q.push(II(nx,ny));
			}
			}
		}
//		cout<<res<<endl;
	}
	cout<<res<<endl;
	return 0;
}


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

int main() {
	int N, X, Y;
	int dis;
	cin >> N >> X >> Y;
	vector<int> a(N + 5, 0);
	for (int i = 1; i < N; i++) {
		for (int j = i + 1; j <= N; j++) {
			dis = min(abs(i - X) + abs(j - Y) + 1, abs(i - j));
			a[dis]++;
		}
	}
	for (int i = 1; i < N; i++) {
		cout << a[i] << endl;
	}
	return 0;
}

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

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

int f(int n) {
  if (n % 2 == 0) {
    return n / 2;
  } else {
    return 3 * n + 1;
  }
}

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

  int s;
  cin >> s;

  int a_n, a_n_p;
  a_n_p = s;
  vector<bool> count(1000001, false);
  count.at(s) = true;

  REPI(i, 2, 1000001) {
    a_n = f(a_n_p);
    if (count.at(a_n)) {
      cout << i << endl;
      break;
    }

    count.at(a_n) = true;
    a_n_p = a_n;
  }

  return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <cmath>
#include <iomanip>
#include <functional>
#include <random>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;

ll BASE_NUM = 1000000007;


int main()
{
    // 整数の入力
    ll N;
    map<pair<ll,ll>,ll> counts;
    cin >> N;
    for(int i = 1;i<=N;i++){
        string i_s = to_string(i);
        ll first = i_s.front() - '0';
        ll last = i_s.back() - '0';
        counts[{first,last}]++;
    }

    ll ans = 0;

    for(auto p:counts){
        ans+= p.second * counts[{p.first.second,p.first.first}];
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
import copy
N=int(input())
a_list=list(map(int,input().split()))
a=set(a_list)
a=list(a)
if 0 in a:
        if len(a) ==2:
            if a_list.count(0)==len(a_list)//3 or a_list.count(0)==len(a_list)//2:
                    print("Yes")
            else:
                    print("No")
        elif len(a)==1:
            print("Yes")
        else:
                print("No")
else:
        if len(a)==3:
            a1=a[0]
            a2=a[1]
            a3=a[2]
            if a1 ^ a2 ==a3 and a1^a3==a2 and a2^a3==a1:
                if a_list.count(a1) == a_list.count(a2) == a_list.count(a3):
                        print("Yes")
                else:
                        print("No")
            else:
                print("No")
        else:
            print("No")


Measured memory_bytes: 14612
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvv(vector<vector<T>>& v) {for (auto u : v) printv(u);}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    ll n, k;
    cin >> n >> k;
    ll r = n % k;
    cout << min(r, k - r) << endl;
}


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

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

namespace pb_ds {   
    namespace io {
        const int MaxBuff = 1 << 15;
        const int Output = 1 << 23;
        char B[MaxBuff], *S = B, *T = B;
        #define getc() ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T) ? 0 : *S++)
        char Out[Output], *iter = Out;
        inline void flush() {
            fwrite(Out, 1, iter - Out, stdout);
            iter = Out;
        }
    }
 
    template<class Type = int> inline Type read() {
        using namespace io;
        register char ch; register Type ans = 0; register bool neg = 0;
        while(ch = getc(), (ch < '0' || ch > '9') && ch != '-')     ;
        ch == '-' ? neg = 1 : ans = ch - '0';
        while(ch = getc(), '0' <= ch && ch <= '9') ans = ans * 10 + ch - '0';
        return neg ? -ans : ans;
    }
}
 
using namespace pb_ds;

void File() {
#ifdef zjp_shadow
	freopen ("D.in", "r", stdin);
	freopen ("D.out", "w", stdout);
#endif
}

typedef long long ll;

const int N = 1e5 + 1e3;

int n, h[N], A, B;

inline bool check(ll times) {
	ll cur = 0;
	For (i, 1, n) {
		ll tmp = h[i] - B * times;
		if (tmp > 0) cur += tmp / (A - B) + (tmp % (A - B) ? 1 : 0);
	}
	return cur <= times;
}

int main () {

	File();

	n = read(), A = read(), B = read();
	
	ll l = 0, r = 0, res = 0;
	For (i, 1, n) 
		r += (h[i] = read()) / A + 1;

	while (l <= r) {
		ll mid = (l + r) >> 1;
		if (check(mid)) res = mid, r = mid - 1; else l = mid + 1;
	}
	printf ("%lld\n", res);

	return 0;

}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

int P[100001];
int K[100001];

int Find(int A) {
	if (P[A] < 0) return A;
	return P[A] = Find(P[A]);
}

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}

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


	int N = getint();

	vector<int> XY[100001];
	rep(i, N) {
		int x = getint(), y = getint();
		XY[x].pb(y);
	}

	rep1(i, 100000) P[i] = -1;

	rep1(i, 100000) {
		if (XY[i].size()) {
			int a = Find(XY[i][0]);
			for (int j = 1; j < (int)XY[i].size(); j++) {
				int b = Find(XY[i][j]);

				if (a == b) continue;
				P[a] += P[b];
				P[b] = a;
				K[a] += K[b];
			}
			K[a]++;
		}
	}

	ll kotae = 0;
	rep1(i, 100000) {
		if (K[i] && P[i] < 0) {
			kotae -= 1ll * K[i] * P[i];
		}
	}
	co(kotae - N);

	Would you please return 0;
}

Measured memory_bytes: 5120
//g++ -Wshadow -Wall -o A.cpp a -g -fsanitize=address -fsanitize=undefined 
#include <bits/stdc++.h>

#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define TC int tc; cin >> tc; while(tc--)

using namespace std;

const int INF = 1e9 + 7;
const int MxN = 3010;

int n;
ld dp[MxN], a[MxN];

int main(void)
{
	FAST
	cin >> n;
	memset(dp, 0.0, sizeof dp);

	dp[0] = 1;
	for(int coin = 0; coin < n; coin++){
		ld p_head;
		cin >> p_head;
		for(int heads = coin + 1; heads >= 0; heads--){
			dp[heads] = (heads == 0 ? 0 : dp[heads-1] * p_head) + dp[heads]*(1.0 - p_head);
		}
	}

	ld ans = 0.0;
	for(int heads = 0; heads <= n; heads++){
		int tails = n - heads;
		if(heads > tails) ans += dp[heads];
	}

	printf("%.10Lf\n", ans);
	
	return 0;
}


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

#define ll long long
#define ull unsigned long long
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

//sort(arr, arr+n, greater<int>())

//for(int i = 0; i < size; i++)
//    cout << a[i] << " ";

int main()
{
    fast_io;

    int n;
    cin >> n;

    vector <int> v;

    while(n--)
    {
        int temp;
        cin >> temp;

        v.emplace_back(temp);
    }

    int res = 1;

    for(int i = 0; i < v.size() - 1; i++)
    {
        if(v[i] > v[i + 1]) res++;
        else v[i + 1] = v[i];
    }

    cout << res << '\n';
    return 0;
}


Measured memory_bytes: 4288
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct mod_fac{
    private:
        vector<ll> f,i_f;
        int n;ll MOD;
    public:
        void init(int n,ll m){
            MOD=m;
            f.resize(n+1,0);
            i_f.resize(n+1,0);
            f[0]=1;
            rep(i,n){
                f[i+1]=f[i]*(i+1)%MOD;
            }
            i_f[n]=power(f[n],MOD-2);
            per(i,n){
                i_f[i]=(i_f[i+1]*(i+1))%MOD;
            }
        }
        ll power(ll a ,ll x){
            if (x==0) return 1;
            if (x==1) return a%MOD;
            if (x==2) return (a*a)%MOD;
            if (x%2==1) return (a*power(power(a,(x-1)/2),2))%MOD;
            return (power(power(a,x/2),2))%MOD;
        }
        ll fac(int x){
            return f[x];
        }
        ll invfac(int x){
            return i_f[x];
        }
        ll comb(int m,int k){
            if (m<0 or k<0) return 0;
            if (m<k) return 0;
            return (((f[m]*i_f[k])%MOD)*i_f[m-k])%MOD;
        }
};

ll p;
vector<ll> f,a;

void solve(){
    cin >> p;
    f.resize(p,0);
    a.resize(p,0);
    mod_fac m;m.init(p-1,p);
    rep(i,p){
        cin >> a[i];
        if (a[i]==1) {
            f[0]+=1;
            ll po=1;
            rep(k,p){
                f[p-1-k]+=-m.comb(p-1,k)*po%p+p;
                f[p-1-k]%=p;
                po*=p-i;po%=p;
            }

            //per(i,p) cout << f[i] << " ";
        }
    }

    rep(i,p) cout << f[i] << " ";
}

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

Measured memory_bytes: 384
/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;

#define DEBUG
#ifndef DEBUG
#define endl "\n"
#define log(args...)
#else
#define log(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); logf(_it, args); }
void logf(istream_iterator<string> it) {
}
template<typename T, typename ... Args>
void logf(istream_iterator<string> it, T a, Args ... args) {
	cout << *it << " = " << a << endl;
	logf(++it, args...);
}
#endif

#define forn(i,k,n) for(ll i=(k); i<(n); i++)

const double PI = acos(-1);
typedef long long ll;
#define fori(n) for(ll i=0; i<(n); i++)

#define cins(s) string s; cin>>s;
#define cini(i) int i; cin>>i;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cinai(a, n) vi a(n); fori(n) { cin>>a[i]; }
#define cinall(a, n) vll a(n); fori(n) { cin>>a[i]; }
#define cinas(s, n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a, n) vd a(n); fori(n) { cin>>a[i]; }

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;
typedef vector<ll> vll;

const int INF = 1e9;

/*
 * Every color change from white to black has cost 1.
 * Find cheapest path.
 */
void solve() {
	cini(H);
	cini(W);
	vvi s(H, vi(W));
	for (int i = 0; i < H; i++) {
		cins(str);
		for (int j = 0; j < W; j++)
			s[i][j] = (str[j] == '.');
	}

	vvi dp(H, vi(W, INF)); 	// dp[i][j]=cost if starting at i,j

	if (s[0][0]) {
		dp[0][0] = 0;
	} else {
		dp[0][0] = 1;
	}

	queue<pii> q;
	q.push( { 0, 0 });

	while (q.size()) {
		pii p = q.front();
		q.pop();
		pii dow = { p.first + 1, p.second };
		pii rig = { p.first, p.second + 1 };

		for (pii p1 : { dow, rig }) {
			if (p1.first < H && p1.second < W) {
				int inc = 0;
				if (s[p.first][p.second] && !s[p1.first][p1.second])
					inc++;

				if (dp[p.first][p.second] + inc < dp[p1.first][p1.second]) {
					dp[p1.first][p1.second] = dp[p.first][p.second] + inc;
					q.push(p1);
				}
			}
		}
	}

	cout << dp[H - 1][W - 1] << endl;
}

int main() {
	cin.tie(nullptr);
	std::ios::sync_with_stdio(false);
	solve();
}



Measured memory_bytes: 384
#!/usr/bin/env python3
s = input()
stock = [m for i in range(26) if (m := chr(ord("a") + i)) not in s]
if stock:
    print(s + stock[0])
else:
    for i in range(1, 26):
        if s[-i] > s[-1 - i]:
            print(s[:-1 - i] + min(j for j in s[-i:] if j > s[-1 - i]))
            break
        if i == 25:
            print(-1)


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

int main() {
  vector<int> a = {1, 3, 5, 7, 8, 10, 12};
  vector<int> b = {4, 6, 9, 11};
  vector<int> c = {2};

  int x, y;

  cin >> x >> y;

  int times = 0;

  for(int i=0;i<a.size();i++) {
    if(a[i] == x || a[i] == y) {
      times++;
    }
  }

  if(times == 1) {
    cout << "No" << endl;
    return 0;
  }

  times = 0;

  for(int i=0;i<a.size();i++) {
    if(b[i] == x || b[i] == y) {
      times++;
    }
  }

  if(times == 1) {
    cout << "No" << endl;
    return 0;
  }

  if(x == 2 || y == 2) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;

  return 0;
}

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


int main() {
    ll N, A, B;
    cin >> N >> A >> B;

    B = B - A;
    A = 0;

    ll ans = 0;
    if (B < 0) ans = 0;
    else if (B == 0) ans = 1;
    else {
        if (N==1) ans = 0;
        else if (N==2) ans = 1;
        else ans = B * (N - 2) + 1;
    }

    cout << ans << endl;

    return 0;
}

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

int main()
{
    uint64_t H, W;
    cin >> H >> W;

    uint64_t movable_place_count;

    if (H == 1 || W == 1)
    {
        movable_place_count = 1;
    }
    else
    {
        movable_place_count = (W / 2) * H;

        if ((W % 2) != 0)
        {
            movable_place_count += H / 2;
            if ((H % 2) != 0)
            {
                movable_place_count++;
            }
        }
    }

    cout << movable_place_count << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

int main() {
  int h1, h2, k1, k2, a, b, c, d, hsum = 0, ksum = 0;
  cin >> h1 >> h2 >> k1 >> k2 >> a >> b >> c >> d;
  hsum = ((h1 / 10) * c) + (h1 * a) + ((h2 / 20) * d) + (h2 * b);
  ksum = ((k1 / 10) * c) + (k1 * a) + ((k2 / 20) * d) + (k2 * b);
  if(hsum > ksum) cout << "hiroshi" << endl;
  else if(hsum < ksum) cout << "kenjiro" << endl;
  else cout << "even" << endl;
}




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

ll n, w;
ll dp[101][100005];
pair<ll, ll> wtv[101];

ll solve(ll val, ll at = 0) {
    if(at == n) {
        return val == 0 ? 0 : INF;
    }
    
    if(dp[at][val] != 0) return dp[at][val];

    ll ans = solve(val, at + 1);
    if(wtv[at].second <= val)
        ans = min(ans, solve(val - wtv[at].second, at + 1) + wtv[at].first);
    return dp[at][val] = ans;
}

int main()
{
    cin >> n >> w;
    for(ll i = 0; i < n; i++) cin >> wtv[i].first >> wtv[i].second;
    for(ll i = 100004; i >= 0; --i) {  
        if(solve(i) <= w) {
            cout << i << '\n';
            return 0;
        }
    }
    cout << 0 << '\n';
    return 0;
}

Measured memory_bytes: 81372
#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
char s[5010],t[5010];
int n,A[5010],B[5010],a,b,SA[5010],SB[5010];
il int sA(int l,int r,int k=0){
	if(l>r)return 0;
	if(k)return SA[r]-SA[l-1];
	else return (r-l+1)*n-SA[r]+SA[l-1];
}
il int sB(int l,int r,int k=0){
	if(l>r)return 0;
	if(k)return SB[r]-SB[l-1];
	else return (r-l+1)*n-SB[r]+SB[l-1];
}
int main(){
#ifdef XZZSB
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	n=gi();scanf("%s",s+1);scanf("%s",t+1);
	int ans=INT_MAX;
	A[++a]=0,B[++b]=0;
	if(s[1]=='1')A[++a]=0;if(t[1]=='1')B[++b]=0;
	for(int i=1;i<n;++i)if(s[i]!=s[i+1])A[++a]=i;
	for(int i=1;i<n;++i)if(t[i]!=t[i+1])B[++b]=i;
	A[++a]=n,B[++b]=n;
	if(s[n]=='1')A[++a]=n;if(t[n]=='1')B[++b]=n;
	for(int i=1;i<=a;++i)SA[i]=SA[i-1]+A[i];
	for(int i=1;i<=b;++i)SB[i]=SB[i-1]+B[i];
	for(int i=1;i<=b;i+=2){
		int c=std::min(b,a+i-1),res=sB(1,i-1,1)+sB(i+a,b)+sA(c-i+2,a);
		for(int j=i;j<=c;++j)res+=abs(A[j-i+1]-B[j]);
		ans=std::min(res,ans);
	}
	for(int i=1;i<=a;i+=2){
		int c=std::min(a,b+i-1),res=sA(1,i-1,1)+sA(i+b,a)+sB(c-i+2,b);
		for(int j=i;j<=c;++j)res+=abs(B[j-i+1]-A[j]);
		ans=std::min(res,ans);
	}
	return printf("%d\n",ans),0;
}


Measured memory_bytes: 384
k = gets.chomp.to_i
n = 1
suuretu = 7
if k.even?  or k % 5 == 0 then
    puts "-1"
else
for i in 1..k do
    break if suuretu % k == 0 
    suuretu = (suuretu * 10 + 7) % k
end
puts i
end

Measured memory_bytes: 14388
#include <bits/stdc++.h>
#define ll long long

const int MXN = 1e6 + 7;

using namespace std;

ll n, a[MXN], cnt, ans, m;

int main() {
  cin >>n >>m;
  n %= 500;
  if(n > m) cout <<"No";
  else cout <<"Yes";
}


Measured memory_bytes: 256
import sys
read = sys.stdin.readline
import time
import math
import itertools as it
def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------

N = inp()
A = inpl()
stk = 0
yen = 1000
for i in range(N):
    if i == 0:
        if A[i] < A[i+1]:
            stk += yen // A[i]
            yen %= A[i]
    elif i == N-1:
        yen += stk * A[i]
    else:
        if A[i-1] <= A[i] and A[i] > A[i+1]:
            yen += A[i]*stk 
            stk = 0
        if A[i-1] >= A[i] and A[i] < A[i+1]:
            stk += yen // A[i]
            yen %= A[i]
print(yen)

# -----------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)


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

int calcula(int n){
	if(n < 10)
		return n;
	else
		return n%10 + calcula(n/10); 
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    int menor = 100001;
    for (int i = 1; i < n; ++i){
    	int resto = n - i;
    	menor = min(menor,(calcula(i)+calcula(resto)));
    }
    cout << menor << endl;
    return 0;
}

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

using namespace std;


int main()
{
	int n;
	cin>>n;
	int x[n];
	for(int i=0;i<n;i++)
	{
		cin>>x[i];
	}
	sort(x,x+n);
	//for(int i=0;i<n;i++)
	//cout<<x[i];
	//cout<<endl;
	int min=0;
	for(int i=x[0];i<=x[n-1];i++)  //p点 
	{
		int sum=0;
		for(int j=0;j<n;j++)
		{
			
			int a=fabs(x[j]-i);
			sum+=a*a;
		}
		if(i==x[0])
			min=sum;
		else if(sum<min)
			min=sum;
	}
	cout<<min;
	return 0;
}

Measured memory_bytes: 256
n = gets.to_i
a = gets.chomp.split("")

opt = 0

k = 0
inum = a.count(?I)
a.each do |c|
    if c == ?O
        k += inum
    elsif c == ?I
        inum -= 1
    end
end
opt = k if k > opt

k = 0
jnum = a.count(?J)
a.reverse.each do |c|
    if c == ?O
        k += jnum
    elsif c == ?J
        jnum -= 1
    end
end
opt = k if k > opt

inum = a.count(?I)
jnum = 0
a.each do |c|
    if c == ?J
        jnum += 1
    elsif c == ?I
        inum -= 1
    end
    k = jnum * inum
    opt = k if k > opt
end

h = {:_ => 1, :J => 0, :O => 0, :I => 0}
a.each do |c|
    new_h = h.dup
    case c
    when ?J then new_h[:J] += h[:_]
    when ?O then new_h[:O] += h[:J]
    when ?I then new_h[:I] += h[:O]
    end
    h = new_h
end
p opt + h[:I]



Measured memory_bytes: 16996
#include<stdio.h>
int Tetris[7][4][2] = {
  {{0, 0}, {0, 1}, {1, 0}, {1, 1}},
  {{0, 0}, {1, 0}, {2, 0}, {3, 0}},
  {{0, 0}, {0, 1}, {0, 2}, {0, 3}},
  {{0, 1}, {1, 0}, {1, 1}, {2, 0}},
  {{0, 0}, {0, 1}, {1, 1}, {1, 2}},
  {{0, 0}, {1, 0}, {1, 1}, {2, 1}},
  {{0, 1}, {0, 2}, {1, 0}, {1, 1}}
};
char solve(int map[8][8]) {
  int x, y;
  int i, j, s, n;
  for (i = 0; i < 8; i ++) {
    for (j = 0; j < 8; j ++) {
      for (s = 0; s < 7; s ++) {
	for (n = 0; n < 5; n ++) {
	  if (n == 4) return s + 'A';
	  x = i + Tetris[s][n][0]; y = j + Tetris[s][n][1];
	  if (x > 7 || y > 7) break;
	  if (!map[x][y]) break;
	}
      }
    }
  }
}
int main() {
  char line[9];
  int map[8][8];
  int i, j;
  while (scanf("%s", line) != EOF) {
    for (j = 0; j < 8; j ++) map[0][j] = line[j] - '0';
    for (i = 1; i < 8; i ++) {
      scanf("%s", line);
      for (j = 0; j < 8; j ++) map[i][j] = line[j] - '0';
    }
    printf("%c\n", solve(map));
  }
  return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int>a(n);
	for (int i = 0; i < n; i++)cin >> a.at(i);
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (i % 2 == 0) {
			if (a.at(i) % 2 == 1)ans++;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3628
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <bits/stdc++.h>
    
    
using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,m,n) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cout(n) cout<<fixed<<setprecision(n)
#define dout(n) cout<<# n<<" = "<<n<<endl;
    
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    vector<ll> v(2e5+1);
    vector<ll> t(n);
    FOR(i,0,n){
        cin>>t[i];
        v[t[i]]++;
    }
    ll ans=0;
    FOR(i,0,2e+5+1){
        ans += v[i]*(v[i]-1);
    }

    FOR(i,0,n){
        ll p = t[i];
        cout<<(ans - v[p]*(v[p]-1) + (v[p]-1)*(v[p]-2))/2<<endl;;
    }

}

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

int main(){
	int N, A, B;
	cin >> N >> A >> B;

	int num = 0;
	for(int i = 1; i < N + 1; i++){
		int sum = 0;
		int tmp = i;
		while(1){
			sum += tmp % 10;
			tmp = tmp / 10;
			if(tmp == 0)
				break;
		}
		if(sum >= A && sum <= B)
			num += i;
	}
	cout << num << endl;
}


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

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

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;


//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}


int main(){
  ll n;
  cin >> n;
  VLL A(n);
  VLL B(n);
  vector<pair<ll,int> > a(n);
  vector<pair<ll,int> > b(n);
  VLL a_memo(n);
  ll sumA = 0;
  ll sumB = 0;
  rep(i,n) {
    cin >> A[i];
    sumA  += A[i];
  }
  rep(i,n) {
    cin >> B[i];
    sumB += B[i];
  }

  if(sumA - sumB < 0){
    cout << -1 << endl;
    return 0;
  }
  ll ans = 0;
  rep(i,n){

    if(A[i] < B[i]) ans++;

  }

  VLL s;
  rep(i,n){
    if(A[i] - B[i] >= 0){
      s.push_back(A[i] - B[i]);
    }
  }

  ans = n;
  sort(all(s));
  ll add = sumA - sumB;
  rep(i,s.size()){
    if(add >= s[i]){
      ans --;
      add -= s[i];
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 6520
//今回実装バグになった所(2箇所)->12行目,45行目

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <set>
#define INF 1000000000
using namespace std;
#define int unsigned long long // 手前に動かした
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define LOOP(i,N) for(int i=0;i<N;i++)
#define LOOP1(i,N) for(int i=1;i<=N;i++)
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
 
#define MAN 200000
 
int pow(int n,int k){
  int r=1;
  LOOP(i,k)r*=n;
  return r;
}
 
int sqrt(int n){
  LOOP(i,n){
    if(n<i*i*i)return i-1;
  }
  return -1;
}
 
signed main(){
  int N,P;
  cin >> N >> P;
  int res=1;
  if(N==1){
    cout << P << endl;
    return 0;
  }if(N>41 || N>=P){
    cout << 1 << endl;
    return 0;
  }
  for(int i=2,c=P;i*i<=c;i++){ // 条件式変えた、初期値は2
    // cerr << i << ":" << c;
    if(c%i){
      // cerr << "; :(" << endl;
      continue;
    }
    int ipn = pow(i,N);
    if(ipn>1000000000000LL)break;
    // cerr << "; " << ipn << endl;
    while(c%ipn==0){ // 割れる間割り続ける
      c/=ipn;
      res*=i;
    }
  }
  cout << res << endl;
 
}

Measured memory_bytes: 256
/*input
31
0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}

//}}}
const ll maxn=300005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const double PI=acos(-1);
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		
		b>>=1;
	}
	return res;
}

const ll half=(1+MOD)/2;
vector<int> edge[maxn];

struct state{
	ll dp[3];
	state(ll a,ll b,ll c){
		dp[0]=a;
		dp[1]=b;
		dp[2]=c;
	}
};
deque<state> ans[maxn];

int CNT=0;
void mrg(state &A,state B){
	CNT++;
	A.dp[1]=(A.dp[1]*B.dp[0]+A.dp[0]*B.dp[1])%MOD;
	A.dp[0]=(A.dp[0]*B.dp[0])%MOD;
	A.dp[2]=(1-A.dp[1]-A.dp[0]+MOD+MOD)%MOD;
}
void dfs(int u){
	int nxt=-1; 
	for(int v:edge[u]){
		dfs(v);
		if(nxt==-1||SZ(ans[nxt])<SZ(ans[v])) nxt=v;
	}
	if(nxt==-1){
		ans[u].pb(state(half,half,0));
		return;
	}
	int mx=0;
	for(int v:edge[u]) if(v!=nxt){
		mx=max(mx,SZ(ans[v]));
		REP(i,SZ(ans[v])) mrg(ans[nxt][i],ans[v][i]);
	}
	ans[nxt].swap(ans[u]);
	REP(i,mx) ans[u][i].dp[0]=(ans[u][i].dp[0]+ans[u][i].dp[2])%MOD,ans[u][i].dp[2]=0;
	ans[u].push_front(state(half,half,0));
	// cout<<u<<'\n';
	// for(auto i:ans[u]) cout<<i.dp[0]<<' '<<i.dp[1]<<"     ";
	// cout<<'\n';
}

int main(){
	IOS;
	int n;
	cin>>n;
	n++;
	for(int i=1;i<n;i++){
		int p;
		cin>>p;
		edge[p].pb(i);
	}
	dfs(0);
	ll sum=0;
	for(auto i:ans[0]) sum+=i.dp[1];
	sum%=MOD;
	cout<<sum*mypow(2,n)%MOD<<'\n';
	assert(CNT<=n);
	return 0;
}


Measured memory_bytes: 249600
class Handle753:
    def __init__(self, N):
        self.N = N
        self.ans = 0


    def judge753(self, x):
        counter = [False for _ in range(10)]
        while x > 0:
            a = x % 10
            counter[a] = True
            x //= 10

        for j, c in enumerate(counter):
            if j in (3, 5, 7):
                if not c:
                    return False
            else:
                if c:
                    return False
        
        return True


    def dfs(self, x=0):
        if x > self.N:
            return
        if self.judge753(x):
            self.ans += 1
        self.dfs(10 * x + 3)
        self.dfs(10 * x + 5)
        self.dfs(10 * x + 7)


    def show_answer(self):
        print(self.ans)


def main():
    N = int(input())
    hoge = Handle753(N)
    hoge.dfs()
    hoge.show_answer()


if __name__ == "__main__":
    main()


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

#if DEBUG
template <typename T>
void debug(const T& value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

using namespace std;

struct Trie {
  array<int, 26> value = {};
  array<Trie*, 26> children = {};
};

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

  rd(int, n);

  V<string> s(n);
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
  }

  Trie trie;
  int nonempty = 0;
  function<bitset<26>(Trie&, const string&, int)> add_trie =
      [&](Trie& t, const string& si, int i) {
        bitset<26> have;
        if (i < si.size()) {
          if (t.children[si[i] - 'a'] == NULL) {
            t.children[si[i] - 'a'] = new Trie;
          }
          Trie& c = *t.children[si[i] - 'a'];
          have |= add_trie(c, si, i + 1);
          have.set((si[i] - 'a'));
        }
        if (si.size() - i >= 2) {
          rep(i, 26) if (have.test(i))++ t.value[i];
        }
        return have;
      };
  for (const auto& si : s) {
    dbg(si.substr(0, 10), nonempty);
    add_trie(trie, si, 0);
  }
  ll ans = 0;
  for (const auto& si : s) {
    const Trie* t = &trie;
    rep(i, si.size() - 1) { t = t->children[si[i] - 'a']; }
    ans += t->value[si.back() - 'a'];
  }
  wt(ans);
}


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

int main(){
  int K;
  cin >> K;
  if(K%2==0){
    cout << (K/2)*(K/2) << endl;
    return 0;
  }
  cout << ((K+1)/2)*(K/2) << endl;
  return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
int N, M, count;
char a[8][9];
int main()
{
	scanf("%d%d", &N, &M);
	for (int i = 0; i < N; i++)
	{
		scanf("%s", a[i]);
		count += std::count(a[i], a[i] + M, '#');
	}
	if (count != N + M - 1 || a[0][0] != '#')
	{
		puts("Impossible");
		return 0;
	}
	for (int x = 0, y = 0; x != N - 1 || y != M - 1; )
		if (x < N - 1 && a[x + 1][y] == '#')
			x++;
		else if (y < M - 1 && a[x][y + 1] == '#')
			y++;
		else
		{
			puts("Impossible");
			return 0;
		}
	puts("Possible");
	return 0;
}


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

int main(){
  ll N, M, i, a;
  cin >> N >> M;
  ll ans = 0;
  priority_queue<double> A;
  rep(i, N){
    cin >> a;
    A.push(a);
  }
  
  double c;
  rep(i, M){
    c = A.top()/2;
    A.pop();
    A.push(c);
  }
  rep(i, N){
    ans += (ll)A.top();
    A.pop();
  }
  
  printf("%lld\n", ans);
  
  return 0;
}

Measured memory_bytes: 1400
#include <bits/stdc++.h>
#define rep(i,n) REP(i,0,n)
#define REP(i, k, n) for (int(i) = k; (i) <(int)(n); (i)++)
#define vi vector<int>
#define pii pair<int,int>

using ll = long long;

using namespace std;

int main(){
    string s,t;
    cin >> s >> t;

    cout << t + s << endl;
}

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

int main() {
  #define int long long
  int n, x;
  cin >> n >> x;
  int sum = 0;
  int l;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    cin >> l;
    sum += l;
    if (sum > x) {
      cout << i + 1 << endl;
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << n + 1 << endl;
  }
}




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


// type definitions
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vi > vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef complex<int> ci;
typedef complex<double> cd;
typedef complex<long double> cld;

// macros
#define rep(i, a, b) for(int i=a; i<b; i++)
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define all(c) c.begin(), c.end()
#define tr(c, it) for(auto it=(c).begin(); it!=c.end(); it++)
#define pb push_back
#define mp make_pair
#define bitcount __builtin_popcount
#define ispresent(c, x) ((c).find(x) != (c).end())
#define watch(x) cout<< (#x) << " is " << (x) <<"\n";
#define sz(x) ((int)((x).size()))
#define UNIQUE(c) (c).resize(unique(all(c)) - (c).begin())
#define pii2ll(p) ((ll)(p).first<<32 | (p).second)

// template functions
template <typename T>
inline void printV(vector<T>& a) { rep(i, 0, sz(a)) { cout<<a[i]<<" "; } cout<<"\n"; }
template <typename T>
inline void printV(vector<T>& a, int n) { rep(i, 0, n) { cout<<a[i]<<" "; } cout<<"\n"; }
template <typename T>
inline void printVV(vector<vector<T>>& a)
{
	rep(i, 0, sz(a)) {
		printV(a[i]);
	}
	cout<<"\n";
}

template <typename T>
inline void clearVV(vector<vector<T>>& a)
{
	for(auto& x:a) {
		x.clear();
	}
	a.clear();
}
template <typename T>
inline void assignVV(vector<vector<T>> &a, T val)
{
	for(int i=0; i<sz(a); i++) {
		for(int j=0; j<sz(a[i]); j++) {
			a[i][j]=val;
		}
	}
}


// constants
const long double eps = 1e-9;
const int inf = 5e8;
const int modn = 1e9+7;
const int MAX = 1e5;

inline int add(int a, int b)
{
	return ((ll)a+b+modn)%modn;
}

inline int mul(int a, int b)
{
	return ((ll)a*b)%modn;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Your code here
    int n;
	cin>>n;
	vector<vector<int>> like(n, vector<int>(n));
	for(int i=0; i<n; i++) {
		for(int j=0; j<n; j++) {
			cin>>like[i][j];
		}
	}	
	vector<vector<int>> dp(n, vector<int>(1<<n));
	assignVV(dp, -1);
	function<int(int , ll)> solve = [&](int i, ll mask)
	{
		if(i>=n) return 1;
		if(dp[i][mask]!=-1) return dp[i][mask];
		int &memo=dp[i][mask];
		memo=0;
		for(int j=0; j<n; j++) {
			if(!(mask&(1<<j)) and like[i][j]) {
				memo=add(memo, solve(i+1, mask|(1<<j)));
			}

		}
		return memo;
	};
	cout<<solve(0, 0)<<"\n";
    return 0;
}



Measured memory_bytes: 180608
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef map<char,int> mci;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pair<int,int>> vii;
typedef vector<char> vc;
typedef vector<vc> vcc;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define in insert
#define sz(v) v.size()
#define mina(a,b) (a)=min((a),(b));
#define maxa(a,b) (a)=max((a),(b));
const int INF = 1e9 + 5;
void solve(){
    int h,w;
    cin>>h>>w;
    vcc v(h+2,vc(w+2,'#'));
    vvi nearU(h+2,vi(w+2)),nearD(h+2,vi(w+2)),nearR(h+2,vi(w+2)),nearL(h+2,vi(w+2));
    int cur;
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            cin>>v[i][j];
        }
    }
    for(int i = 1; i <= w; i++){
        for(int j = 0; j <= h; j++){
            if(v[j][i]=='#') cur=j;
            nearU[j][i]=cur;
        }
    }
    for(int i = 1; i <= w; i++){
        for(int j = h+1; j >= 0; j--){
            if(v[j][i]=='#') cur=j;
            nearD[j][i]=cur;
        }
    }
    for(int i = 1; i <= h; i++){
        for(int j = 0; j <= w; j++){
            if(v[i][j]=='#') cur=j;
            nearL[i][j]=cur;
        }
    }
    for(int i = 1; i <= h; i++){
        for(int j = w+1; j >= 0; j--){
            if(v[i][j]=='#') cur=j;
            nearR[i][j]=cur;
        }
    }
    /*for(int i = 0; i <= h+1; i++){
        for(int j = 0; j <= w+1; j++){
            cout<<nearD[i][j];
        }
        cout<<endl;
    }*/
    int ans=-1;
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            if(i==3 && j==6){
                //cout<<i-nearU[i][j]<<" "<<j-nearL[i][j]<<" "<<nearR[i][j]-j<<" "<<nearD[i][j]-i;
            }
            maxa(ans,i-nearU[i][j]+j-nearL[i][j]+nearR[i][j]-j+nearD[i][j]-i-3);
        }
    }
    cout<<ans;
}
int main(){
    int T=1;
    //cin>>T;
    for(int i = 0; i < T; i++) solve(); 
}

Measured memory_bytes: 67456