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

#define ld long double
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define nl cout << endl
#define pii pair <ll, ll>
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323846264
#define y1 abacaba
#define fn "abc"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Need_For_Speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define __lcm(a, b) (a * b) / __gcd(a, b)

using namespace std;

const long long MXN = 1e5 + 1;
const long long MOD = 1e9 + 7;

int a[22][22];

int dp[(1 << 21) + 28];

int main () {

    Need_For_Speed
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }
    dp[0] = 1;
    for (int mask = 0; mask < (1 << n); mask++) {
        int x = __builtin_popcount(mask);
        for (int j = 1; j <= n; j++) {
            if (a[x + 1][j] && !((mask >> (j - 1)) & 1)) {
                (dp[mask | (1 << (j - 1))] += dp[mask]) %= MOD;

            }
        }
    }
    cout << dp[(1 << n) - 1];
    return 0;
}



Measured memory_bytes: 8448
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
#include<iomanip>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;


/*

*/

ll a,b,q;
vector<ll> s,t,x;

int main(void){
	cin>>a>>b>>q;
	rep(i,a){
		ll n;
		cin>>n;
		s.push_back(n);
	}
	rep(i,b){
		ll n;
		cin>>n;
		t.push_back(n);
	}
	rep(i,q){
		ll n;
		cin>>n;
		x.push_back(n);
	}
	//
	// cout<<"---"<<endl;
	//
	rep(i,q){
		ll y[2],z[2],ans=1e18;
		auto itrs = lower_bound(s.begin(),s.end(),x[i]);
		auto itrt = lower_bound(t.begin(),t.end(),x[i]);
		if(itrs==s.end()){
			itrs--;
			y[0]=*itrs;
			y[1]=1e15;
		}else if(itrs==s.begin()){
			y[0]=-1e15;
			y[1]=*itrs;
		}else{
			y[1]=*itrs;
			itrs--;
			y[0]=*itrs;
		}
		if(itrt==t.end()){
			itrt--;
			z[0]=*itrt;
			z[1]=1e15;
		}else if(itrt==t.begin()){
			z[0]=-1e15;
			z[1]=*itrt;
		}else{
			z[1]=*itrt;
			itrt--;
			z[0]=*itrt;
		}
		ans=min({
			abs(x[i]-y[0])+abs(y[0]-z[0]),
			abs(x[i]-y[0])+abs(y[0]-z[1]),
			abs(x[i]-y[1])+abs(y[1]-z[0]),
			abs(x[i]-y[1])+abs(y[1]-z[1]),
			abs(x[i]-z[0])+abs(z[0]-y[0]),
			abs(x[i]-z[0])+abs(z[0]-y[1]),
			abs(x[i]-z[1])+abs(z[1]-y[0]),
			abs(x[i]-z[1])+abs(z[1]-y[1])
		});
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 5108
#include<stdio.h>

int main(void){ 
 long long int n,i,j,ans;
 scanf("%lld",&n);
 long long int min=n-1;


 //掛け算の表の計算
 for(i=1;i*i<=n;i++){
  
    if(n%i==0){
      j=n/i;
      ans=i+j-2;
      if(min>ans)min=ans;
    }
 }
if(min!=0){
 printf("%lld",min);
  }


  return 0;
}

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

void dfs(vector<vector<int> > &G, vector<int> &ans, vector<int> &d, int par, int node){
    ans[node] = par;
    for(int i = 0; i < G[node].size(); i++) d[G[node][i]]--;
    for(int i = 0; i < G[node].size(); i++) if(d[G[node][i]] == 0) dfs(G, ans, d, node, G[node][i]);
    for(int i = 0; i < G[node].size(); i++) d[G[node][i]]++;
}

signed main(){

    int n, m; cin >> n >> m;
    vector<vector<int> > G(n);
    vector<int> d(n);
    for(int i = 0; i < n + m - 1; i++){
        int a, b; cin >> a >> b; a--; b--;
        d[b]++;
        G[a].push_back(b);
    }

    vector<int> ans(n, 0);
    for(int i = 0; i < n; i++){
        if(d[i] == 0){
            dfs(G, ans, d, -1, i);
            break;
        }
    }
    
    for(int i = 0; i < n; i++) cout << ans[i] + 1 << endl;
    return 0;
}

Measured memory_bytes: 14080
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String args[]) throws Exception{
        BufferedReader stdR = new BufferedReader(new InputStreamReader(System.in));
        String[] s = stdR.readLine().split(" ");
        int N = Integer.parseInt(s[0]);
        int num = (int)(Math.pow(2, N) - 1);
        int[] A = {Integer.parseInt(s[1]),Integer.parseInt(s[2]), Integer.parseInt(s[3])};
        int[] l = new int[N];
        for(int i = 0 ; i < N ; i++) {
            l[i] = Integer.parseInt(stdR.readLine());
        }
        Arrays.sort(l);
        Arrays.sort(A);
        int min = Integer.MAX_VALUE;
        for(int i = 1; i <= num ; i++) {
            for(int j = 1 ; j <= num ; j++) {
                for(int k = 1 ; k <= num ; k++) {
                    if((i & j) == 0 && (j & k) == 0 && (k & i) == 0) {
                        int turn = (Integer.bitCount(i) + Integer.bitCount(j) + Integer.bitCount(k) - 3) * 10;
                        turn += Math.abs(A[0] - returnnum(addzero(Integer.toBinaryString(i), N), l)) + Math.abs(A[1] - returnnum(addzero(Integer.toBinaryString(j), N), l)) + Math.abs(A[2] - returnnum(addzero(Integer.toBinaryString(k), N), l));
                        min = Math.min(min, turn);
                    }
                }
            }
        }
        System.out.println(min);
    }
    
    public static String addzero(String bin, int num) {
        for(int j = bin.length() ; j < num ; j++) {
            bin = "0" + bin;
        }
        return bin;
    }
    
    public static int returnnum(String str, int[] l) {
        int num = 0;
        for(int i = l.length - 1 ; i >= 0; i--) {
            if(str.charAt(i) == '1')num += l[l.length - i - 1];
        }
        return num;
    }
}


Measured memory_bytes: 44220
import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		while(true){
			int n = sc.nextInt();
			if(n == 0) break;

			int[] count = new int[26]; //eAt@xbgÌdÝðßé
			boolean[] flg = new boolean[26]; //eAt@xbgªPêÌæªÉ é©ÌtO
			boolean[] ap = new boolean[26]; //eAt@xbgªPêÉ»ê½©Ç¤©ÌtO
			int m = 0; //üÍÉo»µ½At@xbgÌíÞ

			for(int i=0;i<n;i++){
				char[] s = sc.next().toCharArray();
				int len = s.length;

				if(len != 1) flg[s[0]-'A'] = true; //1¶ÅÈ¢Àè,0ÉµÄÍ¢¯È¢At@xbg
				for(int j=0;j<len;j++){
					if(!ap[s[j]-'A']){
						m++;
						ap[s[j]-'A'] = true;
					}

					if(i == n-1)
						count[s[j]-'A'] -= Math.pow(10,len-j-1);
					else
						count[s[j]-'A'] += Math.pow(10,len-j-1);
				}
			}

			w = new int[m]; //countzñÌKvÈvf¾¯æèoµ½zñ
			initFlg = new boolean[m]; //flgzñÌKvÈvf¾¯æèoµ½zñ
			int j = 0;

			for(int i=0;j<m;i++){
				if(ap[i]){
					w[j] = count[i];
					initFlg[j++] = flg[i];
				}
			}

			//uv->gpµ½(rbgÇ)->o»ñvÌ}bv
			map = new HashMap<Integer,HashMap<Integer,Integer>>();

			//wzñÌæªm/2ÂªÌSTõðs¢,mapÉñ
			topDown(m/2,0,0,0);
			//wzñÌãë¼ªðSTõµ,topDownÆÌdÈèªÈ¢©T·
			System.out.println(bottomUp(m%2==0?m/2:m/2+1,m-1,0,0));
		}
	}

	private static int[] w;
	private static boolean[] initFlg;
	private static HashMap<Integer,HashMap<Integer,Integer>> map;

	private static void topDown(int rem,int idx,int sum,int used){
		if(rem == 0){
			if(map.containsKey(sum)){
				if(map.get(sum).containsKey(used)){
					//·ÅÉo½±ÆÌ ép^[ÈçÎ,JEgÉvXP
					map.get(sum).put(used,map.get(sum).get(used)+1);
				}
				else{
					//Ü¾o½±ÆÌÈ¢p^[ÈçÎ,JEgÍP
					map.get(sum).put(used,1);
				}
			}
			else{
				map.put(sum,new HashMap<Integer,Integer>());
				map.get(sum).put(used,1);
			}

			return;
		}

		for(int i=initFlg[idx]?1:0;i<10;i++){
			if((used&(1<<i)) == 0){
				topDown(rem-1,idx+1,sum+w[idx]*i,(used|(1<<i)));
			}
		}
	}

	private static int bottomUp(int rem,int idx,int sum,int used){
		int res = 0;

		if(rem == 0){
			//L[ÆµÄ(-v)ªÈ¯êÎJEg=0
			if(!map.containsKey(-sum)) return 0;
			for(int tmp : map.get(-sum).keySet()){
				//topDownÅgpµ½Æ©ÔÁÄÈ¯êÎJEg·é
				if((tmp & used) == 0){
					res += map.get(-sum).get(tmp);
				}
			}
			return res;
		}

		for(int i=initFlg[idx]?1:0;i<10;i++){
			if((used&(1<<i)) == 0){
				res += bottomUp(rem-1,idx-1,sum+w[idx]*i,(used|(1<<i)));
			}
		}
		return res;
	}
}

Measured memory_bytes: 22000
#include <stdio.h>

int main(void) {
    int N;
    scanf("%d", &N);
    char S[100];
    for (int i = 0;i < N;i++) {
        scanf("%c", &S[i]);
    }

    for (int i = 0;i < N;i++) {
        if (S[i] == 'Y') {
            printf("Four\n");
            return 0;
        }
    }

    printf("Three\n");

    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map> 
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 375;
int mapp[N][N];
int vis[N][N];
long long n,sum;
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
			cin >> mapp[i][j];
	}
    for(int i = 1; i <= n; i++)
	{
        for(int j = 1; j <= n; j++)
		{
            for(int k = 1; k<= n; k++)
			{
				if(i==j) continue;
                if(i==k) continue;
                if(j==k) continue;
                if(mapp[i][j]>mapp[i][k]+mapp[k][j])
                {
                    cout << "-1" << endl;
                    return 0;
                }
                if(mapp[i][j]==mapp[i][k]+mapp[k][j]) 
					vis[i][j] = vis[j][i] = 1;
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
	    for(int j = 1; j <= n; j++)
        {
		    if(vis[i][j]==0&&i!=j)
				sum += mapp[i][j];
		}
	}
    cout << sum/2 << endl;
    return 0;
}

Measured memory_bytes: 1152
'use strict'

const main = (input) => {
    const N = input.split('\n')[0]
    const fx = N.split('').reduce((p, c) => {
        c = parseInt(c)
         return p + c
    }, 0)
    
    if(parseInt(N) % fx === 0){
        console.log('Yes')
    }else{
        console.log('No')
    }
}

main(require('fs').readFileSync('/dev/stdin', 'utf-8'))

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

void input(vector<int> a){

}

void output(vector<int> x){
    int y = x.size();
    for(int i=0;i<y-1;i++){
        cout << x[i] << " ";
    }
    cout << x[y-1] << endl;;
}

void selectionsort(vector<int> x){
    int y = x.size(),t;
    int cnt=0 ,minj = 0;
    for(int i=0;i<y-1;i++){
        minj = i;
        for(int j=i;j<y;j++){
            if(x[j] < x[minj]){
                minj = j;
            }
        }   
        t = x[i];x[i] = x[minj];x[minj] = t;
        if(i != minj)cnt++;        
    }
    output(x);
    cout << cnt << endl;
}

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    selectionsort(a);
}


Measured memory_bytes: 3144
N, M = map(int, input().split())
Hs = list(map(int, input().split()))

lis = [[-1] for _ in range(N)]

for _ in range(M):
    A, B = map(int, input().split())
    lis[A-1].append(B-1)
    lis[B-1].append(A-1)

ans = 0
for i in range(N):
    L = lis[i]
    H = Hs[i]
    flg = True
    for j in L:
        if j==-1:
            continue
        if H<=Hs[j]:
            flg = False
            break
    if flg:
        ans += 1
print(ans)
    

Measured memory_bytes: 95748
# frozen_string_literal: true

w, h, n = gets.split(' ').map!(&:to_i)

arr = []
n.times do
  arr << gets.split(' ').map!(&:to_i)
end

# 座標を配列で表現
ans_x = [0, w]
ans_y = [0, h]

arr.each do |x, y, a|
  case a
  when 1
    ans_x[0] = x if ans_x[0] < x
  when 2
    ans_x[1] = x if ans_x[1] > x
  when 3
    ans_y[0] = y if ans_y[0] < y
  when 4
    ans_y[1] = y if ans_y[1] > y
  end
end

x = ans_x[1] - ans_x[0]
y = ans_y[1] - ans_y[0]

x = 0 if x.negative?
y = 0 if y.negative?

puts x * y


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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, c[3];
double P[301][301][301];
double E[301][301][301];

int main() {
	cin >> n;
	inc(i, n) {
		int a;
		cin >> a;
		inc(j, a) { c[j]++; }
	}
	
	P[c[0]][c[1]][c[2]] = 1;
	E[c[0]][c[1]][c[2]] = 0;
	decII(i, 0, c[0]) {
	decII(j, 0, c[1]) {
	decII(k, 0, c[2]) {
		if(! (i >= j && j >= k)) { continue; }
		
		double x = i - j;
		double y = j - k;
		double z = k - 0;
		double p = P[i][j][k];
		double e = E[i][j][k];
		double qx = x / (x + y + z);
		double qy = y / (x + y + z);
		double qz = z / (x + y + z);
		double f  = n / (x + y + z);
		if(x > 0) { P[i - 1][j][k] += p * qx; E[i - 1][j][k] += (e + p * f) * qx; }
		if(y > 0) { P[i][j - 1][k] += p * qy; E[i][j - 1][k] += (e + p * f) * qy; }
		if(z > 0) { P[i][j][k - 1] += p * qz; E[i][j][k - 1] += (e + p * f) * qz; }
	}
	}
	}
	
	printf("%.12f\n", E[0][0][0]);
	
	return 0;
}


Measured memory_bytes: 424320
#include<bits/stdc++.h>
using namespace std;
#define     siii(a,b,c)       scanf("%d%d%d",&a,&b,&c)
#define     si(a)             scanf("%d",&a)
#define     sii(a,b)          scanf("%d%d",&a,&b)
#define     sl(a)             scanf("%lld",&a);
#define     sll(a,b)          scanf("%lld%lld",&a,&b);
#define     slll(a,b,c)       scanf("%lld%lld%lld",&a,&b,&c);
#define     sd(a)             scanf("%lf",&a);
#define     sdd(a,b)          scanf("%lf%lf",&a,&b);
#define     pb(a)             push_back(a)
#define     deb(a)            printf("### %d ###\n",a)
#define     mp                make_pair
#define     pf                printf
#define     fl(a, b, c)       for(int a= b ;a<c;a++)
#define     mx                100000 //10^5
#define     fread             freopen("input.txt","r",stdin)
#define     CLR(a,b)          memset(a,b,sizeof(a))
#define     ll                long long
#define     D(x)              cout<<#x" = "<<x<<endl;
#define     sz                10000
#define     bl                printf("\n")
#define     spc               printf(" ")


int main()
{
    int n, k;
    cin>>n>>k;
    string s;
    cin>>s;
    s[k-1] = tolower(s[k-1]);
    cout<<s<<endl;




    return 0;
}


Measured memory_bytes: 256
N = int(input())
csf = [list(map(int, input().split())) for _ in range(N-1)]
for i in range(N):
    t = 0
    for j in range(i, N-1):
        C, S, F = csf[j]
        if t < S:
            t = S
        elif t % F:
            t = t + F - t%F
        t += C
    print(t)


Measured memory_bytes: 3188
import heapq

N, M = map(int, input().split())
AB = [[0] * 2] * N
for i in range(N):
	AB[i] = list(map(int, input().split()))
AB.sort()

x = []
ans, j = 0, 0
for i in range(1, M + 1):
	while j != N and AB[j][0] <= i:
		heapq.heappush(x, -AB[j][1])
		j += 1
	if len(x) != 0:
		ans -= heapq.heappop(x)

print(ans)

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

#define pout(n)  printf ("%d\n", n)

#define rep(i,a,n) for (int i = a;i < n;i++)
#define per(i,n,a) for (int i = n-1;i >= a;i--)

const int d4x[4] = {1, 0, -1, 0};
const int d4y[4] = {0, 1, 0, -1};
const int d8x[8] = { 1,1,0,-1,-1,-1,0,1 };
const int d8y[8] = { 0,1,1,1,0,-1,-1,-1 };
 
typedef long long ll;
using namespace std;
 
int main() {
  int n;
  cin >> n;
  int a[n],b[n],c[n];
  
  rep(i,0,n){
    cin >> a[i] >> b[i] >> c[i];
  }

  rep(i,0,n){
    if(a[i] < b[i]){
      swap(a[i],b[i]);
    }
    if(a[i] < c[i]){
      swap(a[i],c[i]);
    }
    if(pow(a[i],2) == pow(b[i],2)+pow(c[i],2)){
      puts("YES");
    }
    else{
      puts("NO");
    }
  }
  return 0;
}


Measured memory_bytes: 3036
#include <iostream>
 
using namespace std;
 
int main(){
  int n;
  cin >> n;
  bool flag = false;
  for (int i = 0; i < 26; i++){
    if (4 * i > n)
      break;
    if ((n - 4 * i) % 7 == 0){
      flag = true;
      cout << "Yes" << endl;
      break;
    }
  }
  if (flag == false)
  	cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 256
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Data.List
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Monad.ST

solve n vs = n - (getUsable $ map feat $ divide vs)

divide [] = [[], []]
divide [e] = [[e], []]
divide (e:o:xs) = [e:es, o:os]
  where [es, os] = divide xs

feat xs = runST $ do
  v <- VUM.replicate (10^5 + 1) 0
  (k, n, _, n') <- foldM (f v) (0, 0, 0, 0) xs
  return (k, n, n')
  where
    f v (k, n, k', n') x = do
      VUM.modify v (+ 1) x
      retVal <$> VUM.read v x
        where retVal w | w <= n'   = (k, n, k', n')
                       | w <= n    = (k, n, x , w )
                       | x == k    = (x, w, k', n')
                       | otherwise = (x, w, k , n )

getUsable [(kA, fA, sA), (kB, fB, sB)]
  | kA /= kB  = fA + fB
  | otherwise = max (fA + sB) (fB + sA)

bsReadInts = map (fst . fromJust . B.readInt) . B.words

main = do
  n:vs <- bsReadInts <$> B.getContents
  putStrLn $ show $ solve n vs


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

#define AndIamMr_Awesome ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);

using namespace std;

typedef long long int lli;


int main()
{
	AndIamMr_Awesome;
	lli i=1,j,t,d,n,k,x=9999999999,a,b,c,y=0,z,co,sum=0,p=0;
	cin>>n;
	x=n%1000;
	if(x){
		cout<<1000-x;
	}
	else{
		cout<<"0";
	}
	
	return 0;
}

Measured memory_bytes: 3632
#include<iostream>
#include<vector>
#include<cstring>

const int MAX_DIGIT = 50; // 2進数表記の最大桁数は50桁までと制限する(50桁に満たない場合は0埋め)
long long dp[MAX_DIGIT][2];

int main(){
    int N;
    long long K;
    std::cin >> N >> K;
    std::vector<long long> A(N);
    for(int i = 0; i < N; i++) std::cin >> A[i];

    //初期化
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for(int d = 0; d < MAX_DIGIT; d++){
        // 上からd桁目のみに1があるlong longの値
        long long mask = 1LL << (MAX_DIGIT - d - 1);

        // Aで上からd桁目のbitが立っている個数
        int num = 0;
        for(int i = 0; i < N; i++){
            if(A[i] & mask) num++;
        }

        // Xのd桁目を0, 1にしたときのコスト増加
        long long cost0 = mask * num;
        long long cost1 = mask * (N - num);

        // smaller -> smaller
        if(dp[d][1] != -1){
            dp[d + 1][1] = std::max(dp[d + 1][1], dp[d][1] + std::max(cost0, cost1));
        }

        // exact -> smaller
        if(dp[d][0] != -1){
            if(K & mask){
                // Kのd桁目が1だったらXのd桁目は0にする(exact -> smallerなので)
                dp[d + 1][1] = std::max(dp[d + 1][1], dp[d][0] + cost0);
            }
            // Kのd桁目が0だったらsmallerにしようがないので変化なし
        }

        // exact -> exact
        if(dp[d][0] != -1){
            if(K & mask) dp[d + 1][0] = std::max(dp[d + 1][0], dp[d][0] + cost1);
            else dp[d + 1][0] = std::max(dp[d + 1][0], dp[d][0] + cost0);
        }
    }

    std::cout << std::max(dp[MAX_DIGIT][0], dp[MAX_DIGIT][1]) << std::endl;

    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n;
  string s,t,r;
  cin >> n >> s >> t >> r;
  ll ans=0;
  rep(i,n) {
    set<char> se;
    se.in(s[i]);
    se.in(t[i]);
    se.in(r[i]);
    ans+=se.size()-1;
  }
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


Measured memory_bytes: 256
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;


bool solve(){
	int n;
	cin>> n;
	if(!n) return false;
	int ans = 1000000;
	for(int k=0;k*k*k<=n;k++){
		for(int j=0;j*j+k*k*k<=n;j++){
			int i = n - j*j - k*k*k;
			if(0 <= i && i <= n){
				int m = i+j+k;
				ans = min(ans, m);
			}
		}
	}
	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}

 

Measured memory_bytes: 1160
/*
Original problem: 
Author: sammochen
*/

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"

using namespace std;

#define REP(x,l,u) for(ll x = l;x<u;x++)
#define RREP(x,l,u) for(ll x = l;x>=u;x--)

#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef vector<ll> VLL;
typedef vector<int> VI;

namespace SOLVE {
    void main() {
        int n, k;
        cin >> n >> k;
        VI A(n);
        REP(i, 0, n) {
            cin >> A[i];
        }
        VI win(k + 1);
        win[0] = 0; // 0 cannot make a move: loss
        REP(i, 0, k + 1) {
            // if win[i] is a losing pos, win[i + A[j]] is a winning position
            if (win[i] == 0) {
                REP(j, 0, n) {
                    if (i + A[j] <= k) {
                        win[i + A[j]] = 1;
                    }
                }
            }
        }
        string ans = (win[k] == 1) ? "First" : "Second";
        cout << ans << endl;
    }
}


signed main() {
	int t;
	t = 1;
	// cin >> t;
	while (t--) {
		SOLVE::main();
	}

	return 0;
}

Measured memory_bytes: 640
#include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;
vector<long long int> divisor(long long int n) {
	vector<long long int> res;
	for (long long int i = 1; i * i <= n; ++i) {
		if (n % i == 0) {
			res.push_back(i);
			if (i * i != n) res.push_back(n / i);
		}
	}
	sort(begin(res), end(res));
	return res;
}
int X, Y, n;
vector<vector<int>>getans(const vector<vector<int>>&field, const int now, const vector<int>&nums,const vector<pair<int,int>>&ps) {
	if (now == ps.size())return field;
	vector<long long int>divs(divisor(nums[now]));
	vector<vector<int>>ans;
	const int fromy = ps[now].first;
	const int fromx = ps[now].second;
	for (const  int  yrange : divs) {
		const int xrange = nums[now] / yrange;
		int left = max(0, fromx - xrange + 1);
		int right = min(X-xrange, fromx);
		int up = max(0, fromy - yrange + 1);
		int down = min(Y- yrange, fromy);
		for (int leftside = left; leftside <= right; ++leftside) {
			for (int upside = up; upside <= down; ++upside) {
				bool ok = true;
				for (int x = leftside; x < leftside+xrange; ++x) {
					for (int y = upside; y < upside+yrange; ++y) {
						if (field[y][x] && field[y][x] != now) {
							ok = false;
							break;
						}
					}
				}
				if (ok) {
					vector<vector<int>>nfield(field);
					for (int x = leftside; x < leftside + xrange; ++x) {
						for (int y = upside; y < upside + yrange; ++y) {
							nfield[y][x] = now;
						}
					}
					vector<vector<int>>nans(getans(nfield, now + 1, nums, ps));
					if (!ans.empty()) {
						if (!nans.empty())return vector<vector<int>>();
					}
					else {
						ans = nans;
					}
				}
			}
		}
		
	}
	return ans;
}
int main() {
	while (1) { cin >> X >> Y >> n;
	if (!X)break;
		vector<int>nums(n+1);
		for (int i = 0; i < n; ++i) {
			int x, y; cin >> x >> y;
			nums[x] = y;
		}
		vector<vector<int>>field(Y, vector<int>(X));
		vector<pair<int, int>>froms(n + 1);
		for (int y = 0; y < Y; ++y) {
			for (int x = 0; x < X; ++x) {
				int a; cin >> a;
				field[y][x] = a;
				if (a) {
					froms[a] = make_pair(y, x);
				}
			}
		}
		auto v=getans(field, 1, nums, froms);
		if (v.empty()) {
			cout << "NA" << endl;
		}
		else {
			for (int i = 0; i < Y; ++i) {
				for (int j = 0; j < X; ++j) {
					cout << v[i][j];
					if (j != X - 1)cout << " ";
				}
				cout << endl;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 3032
#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for(int (i)=0; (i)<(n); (i)++)
#define REP2(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1,) (__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "{"; for(size_t i=0; i<v.size(); i++) os << v[i] << (i+1==v.size() ? "" : ", "); os << "}"; return os; }
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }

int main(){
    string S;
    cin >> S;
    int Q;
    cin >> Q;

    bool parity = false;

    string bef, aft;
    REP(q, Q){
        int t;
        cin >> t;
        if(t == 1){
            parity = !parity;
        }else{
            int s;
            string c;
            cin >> s >> c;
            bool qarity = (s == 2);
            if(!(parity ^ qarity)){
                bef += c;
                //S = c + S;
            }else{
                aft += c;
                //S = S + c;
            }
        }
    }
    reverse(bef.begin(), bef.end());
    S = bef + S + aft;
    if(parity){
        reverse(S.begin(), S.end());
    }
    cout << S << endl;
    return 0;
}

Measured memory_bytes: 1348
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define int ll
#define double long double
#define YES {cout<<"YES"<<'\n';}
#define NO {cout<<"NO"<<'\n';}
#define Yes {cout<<"Yes"<<'\n';}
#define No {cout<<"No"<<'\n';}
#define yes {cout<<"yes"<<'\n';}
#define no {cout<<"no"<<'\n';}
void YN(bool flg){cout<<(flg?"YES":"NO")<<'\n';}
void Yn(bool flg){cout<<(flg?"Yes":"No")<<'\n';}
void yn(bool flg){cout<<(flg?"yes":"no")<<'\n';}
#define SORT(a) sort(a.begin(),a.end())
#define REVERSE(a) reverse(a.begin(),a.end())
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define out(n) {cout << n << '\n';}
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<VI> VVI;
const int MOD = 1000000007;
const long long INF = 10e10;
const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T>auto MAX(const T& a) { return *max_element(a.begin(),a.end()); }
template<class T>auto MIN(const T& a) { return *min_element(a.begin(),a.end()); }
int ABS(int a) {if(a<0) a = -a; return a;}
template<class T, class U>U SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); }
template<class T, class U>U COUNT(const T& a, const U& v) { return count(a.begin(),a.end(), v); }
template<class T, class U>int LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); }
template<class T, class U>int UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); }
int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }
int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; }
int POW(int a, int n) { int r = 1; while (n > 0) { if (n & 1)r *= a; a *= a; n /= 2; }return r; }
int isPrime(int n){if(n<2){return 0;}for(int i=2; i*i<=n; i++){if(n%i==0){return 0;}}return 1;}
/*素因数分解*/map< int, int >prime_factor(int n){map< int, int > ret;for(int i = 2; i * i <= n; i++) {while(n % i == 0) {ret[i]++;n /= i;}}if(n != 1) ret[n] = 1;return ret;}
/*約数の個数*/int divisor_number(int n){auto p = prime_factor(n); int res = 1; for(auto x:p)res*=(x.second+1); return res;}
/*約数列挙*/vector<int> divisor(int n){vector<int> v; for(int i=1; i*i<=n; i++){if(n%i==0){v.push_back(i);if(i!=n/i){v.push_back(n/i);}}}sort(v.begin(),v.end()); return v;}
//----------------ライブラリとか----------------

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

    string s; cin >> s;
    bool ok = 1;
    if(s[0]!='A') ok = 0;
    int c = 0;
    FOR(i,1,s.size()){
        if(isupper(s[i])){
            if(i==1||i==s.size()-1||s[i]!='C'){ok = 0;}
            c++;
        }
    }
    if(c!=1) ok = 0;
    if(ok){out("AC");}
    else out("WA");

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
class Edge{
public:
    int from,to,value;
    Edge(LL a,LL b,LL c){
        from = a;
        to = b;
        value = c;
    }
    Edge(LL a,LL b){
        from = a;
        to = b;
    }
};

int main(){
    string s;cin >> s;
    for(int i = 0;i < 3;i++){
        if(s.at(i) == s.at(i+1)){
            cout<<"Bad"<<endl;
            return 0;
        }
    }
    cout<<"Good"<<endl;
}


Measured memory_bytes: 256
import sys
input=sys.stdin.readline
n=int(input())
p_list=list(map(int,input().split()))
count=0
for i in range(n):
    if p_list[i]!=i+1:
        count+=1
if count==0 or count==2:
    print('YES')
else:
    print('NO')
        



Measured memory_bytes: 38384
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int INF = 100000000;
const int MAX_N = 10000;
struct edge{int to, dist, cost;};

int main() {
  // ifstream cin("../test.txt");
  while (true) {
    // 入力
    int N, M;
    cin >> N >> M;
    if (!(N | M)) break;
    vector<edge> roads[MAX_N];
    int u, v, t, c;
    while (M--) {
      cin >> u >> v >> t >> c;
      u--; v--;
      edge e1 = {v, t, c};
      edge e2 = {u, t, c};
      roads[u].push_back(e1);
      roads[v].push_back(e2);
    }

    int d[MAX_N];
    fill(d, d + N, INF);
    d[0] = 0;
    // P(最短距離、頂点)
    priority_queue<P, vector<P>, greater<P> > qu;
    qu.push(P(0,0));
    // ダイクストラ
    while (!qu.empty()) {
      P p = qu.top();
      qu.pop();
      int v = p.second;
      if (d[v] < p.first) continue;
      for (int i = 0; i < roads[v].size(); i++) {
        edge e = roads[v][i];
        if (d[e.to] > d[v] + e.dist) {
          d[e.to] = d[v] + e.dist;
          qu.push(P(d[e.to], e.to));
        }
      }
    }

    //距離表示
    // cout << "----------" << endl;
    // for (int i = 0; i < N; i++) {
    //   cout << i << " " << d[i] << endl;
    // }

    // コスト最小の経路を探して、コストの和の最小値を求める
    int min_total = 0;
    for (int v = 1 ; v < N; v++) {
      int min_cost = INF;
      for (int i = 0; i < roads[v].size(); i++) {
        edge e = roads[v][i];
        if (d[v] == d[e.to] + e.dist && e.cost < min_cost)
          min_cost = e.cost;
      }
      min_total += min_cost;
    }

    // 出力
    cout << min_total << endl;
  }
}

Measured memory_bytes: 2536
#include <iostream>

using namespace std;

unsigned long long int hoge(int n) {
  static unsigned long long int data[10001] = {};
  if (data[n])
    return data[n];
  return data[n] = (n == 1) ? 2 : hoge(n-1) + n;
}

int main() {
  int n;
  while (cin >> n) {
    cout << hoge(n) << endl;
  }
}


Measured memory_bytes: 3192
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG_ARR(x, a, b) for(int i=a; i<=b; i++) cerr<<x[i]<<" "; cerr<<endl;
#define DEBUG_STR(x, a, b) for(int i=a; i<=b; i++) cerr<<x[i]; cerr<<endl;
#define TEST(x) int x; cin>>x; while(x--)
#define ITR(type, var) type::iterator var;
#define ALIAS(type, alias) using alias = type;
#define ALIAS_M(container, A, B, alias) using alias = container<A,B>;

using namespace std;
using ii = pair<int,int>;
using vii = vector<ii>;
using vi = vector<int>;
using vvi = vector<vi>;
using hmapii = unordered_map<int,int>;
using hmapsi = unordered_map<string, int>;
using hmapss = unordered_map<string, string>;
using ll = long long;
using llu = unsigned long long;

int main() {
	FIO;
	int n, m;
	cin>>n>>m;
	ll arr[m];
	for(int i=0; i<m; i++) cin>>arr[i];
	ll sum = 0;
	for(int i=0; i<m; i++) sum += arr[i];
	if(sum <= n){
		cout<<(n-sum);
	} else {
		cout<<-1;
	}
	return 0;
}


Measured memory_bytes: 3660
/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

void cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
int main(void){
    ll n;
    cin >> n;
    vector<ll> x(n), y(n);
    rep(i, n){
        cin >> x[i] >> y[i];
    }

    vector<pair<ll,ll>> c45;
    rep(i, n){
        c45.push_back({x[i] - y[i], x[i] + y[i]});
    }

    ll mxd = -INF;
    ll mixd = INF;
    ll myd = -INF;
    ll miyd = INF;
    rep(i, n){
        mxd = max(mxd, c45[i].first);
        mixd = min(mixd, c45[i].first);
        myd = max(myd, c45[i].second);
        miyd = min(miyd, c45[i].second);
    }

    cout << max(abs(mxd - mixd), abs(myd - miyd)) << endl;
}


Measured memory_bytes: 10588
N, A, B, C = map(int, input().split())
li = [int(input()) for i in range(N)]
INF = 10**9
def dfs(cur, a, b, c):
  if cur == N:
    if min(a, b, c) > 0:
      return abs(a-A) + abs(b-B) + abs(c-C) - 30
    else:
      return INF
  else:
    ret0 = dfs(cur+1, a, b, c) 
    ret1 = dfs(cur+1, a+li[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+li[cur], c) + 10
    ret3 = dfs(cur+1, a, b, c+li[cur]) + 10
    
    return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

Measured memory_bytes: 3064
//============================================================================
// Name        : twdht.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

string RangeProduct(int a,int b)
{
	int limit ;
	if(a < 0 && b > 0 )
		return "Zero";
	else if(a > 0)
		return "Positive";
	else{
		if ( b < 0 )
			limit = b;
		else
			limit = 0;
		if(((-1* (a-limit+1)) % 2) == 0)
			return "Positive";
		else
			return "Negative";
	}
}

int main() {
	int a , b;
	cin >> a >> b;
	cout << RangeProduct(a,b);
	return 0;
}


Measured memory_bytes: 256
package main
import "fmt"
import "math"
func main(){
    // Your code here!
    var n int
    fmt.Scan(&n)
    fmt.Println(rec(n))
    
}

func rec(v int) int{
    if v == 1{
        return 1
    }
    
    var j int
    j = int(math.Trunc(float64(v)/2.0))
    return 1 + 2 * rec(j)
}

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

int main() {
    int n;
    cin>>n;
    string s[n];
    for(int i=0; i<n; i++){
        cin>>s[i];
    }
    int m;
    cin>>m;
    string t[m];
    for(int i=0; i<m; i++){
        cin>>t[i];
    }
    
    int result[n];
    int result_max=0;
    
    for(int i=0; i<n; i++){
        result[i]=0;
        for(int j=0; j<n; j++){
            if(s[i].compare(s[j])==0){
                result[i]++;
            }
        }
        for(int k=0; k<m; k++){
            if(s[i].compare(t[k])==0){
                result[i]--;
            }
        }
    }
    
    for(int i=0; i<n; i++){
        if(result_max<result[i]){
            result_max=result[i];
        }
    }
    
    cout<<result_max<<endl;
    return 0;
}


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

inline vector<int> InitKMP(const string& p) {
    vector<int> f(p.size(), -1);

    for (int i = 1; i < p.size(); i++) {
        int j = f[i-1];
        while (j >= 0 && p[j+1] != p[i]) j = f[j];
        if (p[j+1] == p[i]) f[i] = j + 1;
        else f[i] = -1;
    }

    for (int i = 0; i < p.size(); i++) ++f[i];
    return f;
}

int n, ans;
char s[500055];
const int MOD(1e9+7);

int main()
{
    scanf("%s", s), n = strlen(s);

    // for (int i = 0; i < n; i++) printf("%c ", s[i]); puts("");

    auto&& fail = InitKMP(s);
    reverse(s, s + n);
    auto&& fail2 = InitKMP(s);
    reverse(fail2.begin(), fail2.end());

    // for (auto& i: fail) printf("%d ", i); puts("");
    // for (auto& i: fail2) printf("%d ", i); puts("");

    // The string is good 'as it is' :)
    if (n % (n - fail[n-1]) || fail[n-1] == 0) return puts("1\n1"), 0;
    // The string is super bad :'(
    if (fail[n-1] + 1 == n) return printf("%d\n1\n", n), 0;
    // The string is just.. not good :(
    puts("2");
    // Conjecture: dividing by two parts ALWAYS can eliminate
    // 'bad' property of the NON-UNIFORM string(I think proof is easy).
    for (int i = 1; i < n; i++) {
        // Left part is bad :(
        if (fail[i-1] && i % (i - fail[i-1]) == 0) continue;
        // Right part is bad :(
        if (fail2[i] && (n-i) % (n-i - fail2[i]) == 0) continue;
        ++ans;
    }

    printf("%d\n", ans);
    return 0;
}


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

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)

using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;

using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;

namespace output {
	void pr(int x) { cout << x; }
	void pr(long x) { cout << x; }
	void pr(ll x) { cout << x; }
	void pr(unsigned x) { cout << x; }
	void pr(unsigned long x) { cout << x; }
	void pr(unsigned long long x) { cout << x; }
	void pr(float x) { cout << x; }
	void pr(double x) { cout << x; }
	void pr(ld x) { cout << x; }
	void pr(char x) { cout << x; }
	void pr(const char* x) { cout << x; }
	void pr(const string& x) { cout << x; }
	void pr(bool x) { pr(x ? "true" : "false"); }
	template<class T> void pr(const complex<T>& x) { cout << x; }
	
	template<class T1, class T2> void pr(const pair<T1,T2>& x);
	template<class T> void pr(const T& x);
	
	template<class T, class... Ts> void pr(const T& t, const Ts&... ts) { 
		pr(t); pr(ts...); 
	}
	template<class T1, class T2> void pr(const pair<T1,T2>& x) { 
		pr("{",x.f,", ",x.s,"}"); 
	}
	template<class T> void pr(const T& x) { 
		pr("{"); // const iterator needed for vector<bool>
		bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; 
		pr("}");
	}
	
	void print() { pr("\n"); } // print w/ spaces
	template<class T, class... Ts> void print(const T& t, const Ts&... ts) { 
		pr(t); if (sizeof...(ts)) pr(" "); print(ts...); 
	}
}

using namespace output;

string ans(ll N) {
	string res;
	if (N < 26) {
		res += (N + 'a');
		return res;
	} else {
		res += ans(N / 26 - 1);
		res += (N % 26) + 'a';
		return res;
	}
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll N; cin >> N; --N;
	print(ans(N));
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
 
#define TRACE(x) cerr << #x << " = " << x << endl
#define pb push_back
#define mp make_pair
#define fi first
#define se second
 
#define SZ(x) (int)(x).size()
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
 
template<class Num>
Num mabs(Num A){
  if(A < 0) return -A;
  return A;
}

const int N = 2005, mod = 1e9 + 7;

inline int add(int a, int b){
  int ret = a + b;
  if(ret >= mod) ret -= mod;
  return ret;
}

inline int sub(int a, int b){
  int ret = a - b;
  if(ret < 0) ret += mod;
  return ret;
}

int main(){
  int x; cin >> x;
  cout << 1 - x << endl;
  return 0;
}


Measured memory_bytes: 3548
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
using namespace std;
typedef long long int LL;
typedef pair<LL,LL> P;
typedef pair<LL,int> LP;
const LL INF=(LL)1<<50;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

vector<P> path[100005];
priority_queue<P,vector<P>,greater<P> > q1;
LL dis[100005];

int main(){
	LL n,m;
	int i,j,k;
	LL a,b,c;
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>a>>b>>c;
		a--,b--;
		path[a].push_back(make_pair(c,b));
	}
	for(i=0;i<n;i++){
		for(auto node:path[i]){
			node.first+=dis[i];
			q1.push(node);
		}
		while(!q1.empty() && q1.top().second<=i)q1.pop();
		if(!q1.empty())dis[i+1]=q1.top().first;
		else break;
	}
	if(i!=n-1)dis[n-1]=-1;
	cout<<dis[n-1]<<endl;
}

Measured memory_bytes: 7544
#include <iostream>

using namespace std;
using ll = long long;

const ll INF = 1LL << 50;

int a[2000];
int b[2000];
ll c[2000];

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

    for (int i = 0; i < M;i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
        c[i] = -c[i];
    }

    ll dist[1000];

    for (int i = 0; i < N;i++)
    {
        dist[i] = INF;
    }

    dist[0] = 0;

    for (int i = 0; i < N - 1;i++)
    {
        for (int j = 0; j < M;j++)
        {
            int u = a[j];
            int v = b[j];
            if (dist[u] == INF)
            {
                continue;
            }
            if(dist[v] > dist[u] + c[j])
            {
                dist[v] = dist[u] + c[j];
            }
        }
    }

    ll ans = dist[N - 1];

    bool negative[1000];

    for (int i = 0; i < N;i++)
    {
        negative[i] = false;
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            int u = a[j];
            int v = b[j];
            if (dist[u] == INF)
            {
                continue;
            }
            if (dist[v] > dist[u] + c[j])
            {
                dist[v] = dist[u] + c[j];
                negative[v] = true;
            }
            if(negative[u] == true)
            {
                negative[v] = true;
            }
        }
    }

    if(negative[N - 1] == true)
    {
        cout << "inf" << endl;
    }
    else
    {
        cout << -ans << endl;
    }

    return 0;
}

Measured memory_bytes: 256
def main():
    N, X = map(int, input().split())
    L = list(map(int, input().split()))
    for i in range(N+1):
        D = sum(L[:i])
        if D > X:
            print(i)
            return
    print(N+1)
    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 2940
def test(N, A):
  B = 0
  mx = A[0]
  if N != 1: 
    for i in range(1,N):
      if A[i] - mx < 0:
        B = B + mx - A[i]
      else:
        mx = A[i];
  return B

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

B = test(N, A)

print(B)

Measured memory_bytes: 32164
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (signed)(a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define MOD 1000000007
#define pb push_back
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
#define tp(a, b, c) make_tuple(a, b, c)
#define lb(a, b) lower_bound((a).begin(), (a).end(), (b))
#define ub(a, b) upper_bound((a).begin(), (a).end(), (b))
using namespace std;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
//------------------変数-----------------------//
std::vector<int> v;
//-------------------関数----------------------//
int main() {
  ll n;
  cin >> n;
  REP(i, n) {
    ll a;
    cin >> a;
    v.pb(a);
  }
  ll cnt = 0;
  while (1) {
    bool flag = 1;
    REP(i, v.size()) {
      if (v[i] % 2 == 0) {
        v[i] /= 2;
      } else {
        flag = 0;
        break;
      }
    }
    if (!flag) {
      break;
    }
    cnt++;
  }
  cout << cnt << endl;
}


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

const int N = 2e5 + 10;
char a[N];
int wf;
int main(){
    scanf(" %s",a);
    int len = strlen(a);
    long long ans = 0;
    for(int i=0;i<len;i++){
        if(a[i] == 'W'){
            ans += i - wf;
            wf++;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 384
from collections import deque

tx = [1, 0, -1, 0]
ty = [0, 1, 0, -1]

H, W = map(int,input().split())

MAP = []

for _ in range(H):
    MAP.append(list(input()))

d = deque()

for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            d.append([i, j, 0])

ans = 0

while len(d) != 0:
    x, y, cnt = d.popleft()
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx < H and 0 <= ny < W and MAP[nx][ny] == '.':
            MAP[nx][ny] = '#'
            d.append([nx, ny, cnt + 1])
            ans = max(ans, cnt + 1)

print(ans)

Measured memory_bytes: 205068