akhauriyash/Code-Regression huggingface dataset:
void E(int d[6]){
    int temp;
    temp = d[0];
    d[0] = d[3];
    d[3] = d[5];
    d[5] = d[2];
    d[2] = temp;
}
void S(int d[6]){
    int temp;
    temp = d[0];
    d[0] = d[4];
    d[4] = d[5];
    d[5] = d[1];
    d[1] = temp;
}
void N(int d[6]){
    int temp;
    temp = d[0];
    d[0] = d[1];
    d[1] = d[5];
    d[5] = d[4];
    d[4] = temp;
}
void W(int d[6]){
    int temp;
    temp = d[0];
    d[0] = d[2];
    d[2] = d[5];
    d[5] = d[3];
    d[3] = temp;
}
 
#include <stdio.h>
 
int main(int argc, const char * argv[]) {
    int i, n, t, f, l, r, b, c, count=0;
    int t2, f2, l2, r2, b2, c2;
    int d[6], d2[6];
     
    for(i=0; i<6; i++) {
        scanf("%d",&d[i]);
    }
    b = d[4];
    t = d[0];
    r = d[2];
    l = d[3];
    c = d[5];
    f = d[1];
     
    for(i=0; i<6; i++) {
        scanf("%d", &d2[i]);
    }
    b2 = d2[4];
    t2 = d2[0];
    r2 = d2[2];
    l2 = d2[3];
    c2 = d2[5];
    f2 = d2[1];
     
    while (d2[1] != f) {
         
        if((d2[1] != b) &&
           (d2[1] != t) &&
           (d2[1] != r) &&
           (d2[1] != l) &&
           (d2[1] != c) &&
           (d2[1] != f) ){
            count++;
            break;
        }
         
        if (d2[2] == f || d2[3] == f) {
            E(d2);
        }
        N(d2);
    }
    while(d2[5] != c){
        if(0 < count) break;
        E(d2);
    }
    while (d2[0] != t) {
        if(0 < count) break;
        E(d2);
    }
     
    b2 = d2[4];
    t2 = d2[0];
    r2 = d2[2];
    l2 = d2[3];
    c2 = d2[5];
    f2 = d2[1];
     
    if(b == b2 &&
       t == t2 &&
       r == r2 &&
       l == l2 &&
       c == c2 &&
       f == f2){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;
}




Measured memory_bytes: 2140
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);++i)
#define rrep(i,n) for(ll i=(n)-1;i>=0;--i)
#define rep1(i,n) for(ll i=1;i<=(n);++i)
#define rrep1(i,n) for(ll i=(n);i>=1;--i)
#define fr first
#define sc second
#define pb push_back
#define INF 0xfffffff
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;
struct edge{ll to,cap,rev; }; // ????????????????????????

vector<edge> G[100];
vector<edge> rG[100];
ll d[100];
ll a[100];
ll cnt=0;
void add_edge(ll from,ll to,ll cap){
	G[from].pb((edge){to, cap, rG[to].size() });
	rG[to].pb((edge){from, 0, G[from].size()-1} );
}

ll max_flow(ll V,ll E,ll s,ll t){
	ll res=0;
	memset( d, 0, sizeof(d) );
	memset( a, 0, sizeof(a) );
	d[s] = V;
	a[s]=-1;
	rep(i,G[s].size()){
		edge &e = G[s][i];
		a[e.to] += e.cap;
		rG[e.to][e.rev].cap = e.cap;
		e.cap = 0;
	}

	for(;;){
		ll v = -1;  //1
		ll dmax=-1;
		rep(i,V){
			if( a[i]>0 && dmax<d[i] && i!=t ){
				dmax=d[i];
				v = i;
			}
		}
		if(v < 0) return a[t];

		ll id=-1,i=0;
		ll J=0; //2
		if(0){
			re1:
			J=0;
			i=id+1;
			id=-1;
		}
		for(;i<G[v].size();i++){
			if( G[v][i].cap>0 && d[v] - d[G[v][i].to] == 1 ){
				id = i;
				J=1;
				break;
			}
		}
		i=0;
		if(0){
			re2:
			J=0;
			i=id+1;
			id=-1;
		}
		if(!J) for(;i<rG[v].size();i++){
			if( rG[v][i].cap>0 && d[v] - d[rG[v][i].to] == 1 ){
				id = i;
				J=2;
				break;
			}
		}
	
		if(J){  //3
			edge &e = (J==1?G[v][id]:rG[v][id]);
			if(J==1){
				ll f = min(a[v], e.cap );
				a[v] -= f;
				e.cap -= f;
				rG[e.to][e.rev].cap += f;
				if(a[e.to]>=0) a[e.to] += f;
				goto re1;
			}
			else{
				edge &efor = G[e.to][e.rev];
				ll f = min(a[v], e.cap);
				a[v] -= f;
				e.cap -= f;
				efor.cap += f;
				if(a[e.to]>=0) a[e.to] += f;
				goto re2;
			}
		}

		else{  //4
			ll dmin=0xfffffff;
			rep(i,G[v].size()){
				if(G[v][i].cap>0) dmin = min(dmin,d[G[v][i].to]);
			}
			rep(i,rG[v].size()){
				if(rG[v][i].cap>0) dmin = min(dmin,d[rG[v][i].to]);
			}
			d[v] = dmin + 1;
		}
	}
}

ll V,E,A[1000],B[1000],C[1000];

void input(){
	scanf("%lld%lld",&V,&E);
	rep(i,E){
		scanf("%lld%lld%lld",&A[i],&B[i],&C[i]);
	}
}

int main()
{
	input();
	rep(i,V){
		G[i].clear(); rG[i].clear();
	}
	rep(i,E) add_edge(A[i], B[i], C[i]);
	cout<<max_flow(V,E,0,V-1)<<endl;
}

Measured memory_bytes: 3296
use proconio::input;

fn main() {
	input!(w: String);

	let mut v = vec![1; 26];
	for i in 0..w.len() {
		let c = w.chars().nth(i).unwrap();
		let a = c as usize - 'a' as usize;
		v[a] = 1 - v[a];
	}
	v.sort();
	if v[0] == 0 {
		println!("No");
	}
	else {
		println!("Yes");
	}
}


Measured memory_bytes: 2124
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(void)
{
    int n;
    char s;

    cin >> n;

    bool a = false;
    for (int i = 0; i < n; i++) {
        cin >> s;
        if (s == 'Y') {
            a = true;
            break;
        }
    }

    if (a) {
        cout << "Four" << endl;
    }
    else {
        cout << "Three" << endl;
    }
    return 0;
}

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

int main() {
  int n;
  char s[5];
  int a[5]={0};
  scanf("%d", &n);
  for(int i=0;i<n;i++){
    scanf("%s", s);
    if(strcmp(s, "AC")==0) a[0]++;
    else if(strcmp(s, "WA")==0) a[1]++;
    else if(strcmp(s, "TLE")==0) a[2]++;
    else a[3]++;
  }

  printf("AC x %d\n", a[0]);
  printf("WA x %d\n", a[1]);
  printf("TLE x %d\n", a[2]);
  printf("RE x %d\n", a[3]);

  return 0;
}


Measured memory_bytes: 2156
#include <bits/stdc++.h>
#define oo int(1e9)
#define all(s) s.begin(), s.end()
using namespace std;
typedef unsigned long long ll;
typedef double db;
const ll MOD = (ll)(1e9) + 7;
ll dp[10001][100][2];
int D;
int n;
string K; 
ll calc(int id,int sum_digit,int tight,const string& K){
    if (id == K.size()) return (sum_digit == 0);
    if (dp[id][sum_digit][tight] != -1) return dp[id][sum_digit][tight];
    int limit = (tight) ? (K[id] - '0'): 9;
    ll ans = 0;
    for (int dig = 0; dig <= limit; dig ++){
        int newtight = ((K[id] - '0') == dig) ? tight : 0;
        ans = (ans + calc(id + 1,(sum_digit + dig) % D,newtight,K) ) % MOD;
    }
    return dp[id][sum_digit][tight] = ans;
}
int main() {
	cin.tie(0); ios::sync_with_stdio(0); cout.tie(0);
    cin >> K;
    cin.ignore();
    cin >> D;
    memset(dp,-1,sizeof(dp));
    cout << (calc(0,0,1,K) - 1 + MOD) % MOD;
	return 0;
}

Measured memory_bytes: 16896
#include<bits/stdc++.h>
#define lli long long int 
using namespace std;
int main(){
    lli h,ans=0,k=1;
    cin >> h;
    while(h>1){
        h=h/2;
        ans=ans+k;
        k*=2;
    }
    ans=ans+k;
    cout << ans;
    return 0;
}

Measured memory_bytes: 256
{-# LANGUAGE TypeApplications, TupleSections #-}

import Control.Monad
import Data.List

main = do
    xs <- fmap (, 1) <$> ((readLn @ Int) >>= (flip replicateM) getLine)
    ys <- fmap (, -1) <$> ((readLn @ Int) >>= (flip replicateM) getLine)

    print $ (foldl max 0 . fmap (sum . fmap snd) . groupBy ((. fst) . (==) . fst) . sort) (xs ++ ys)


Measured memory_bytes: 4096
def sekinohi(m, d)
  d10, d1 = d.divmod(10)
  d1 >= 2 && d10 >= 2 && d1 * d10 == m
end

M, D = gets.split.map(&:to_i)
ans = 0
if D < 10
  puts ans
  exit
end
(1..M).each do |m|
  (10..D).each do |d|
    ans += 1 if sekinohi(m, d)
  end
end
puts ans

Measured memory_bytes: 14232
### subroutines

def transform(name)
  trans = {}
  len = name.length

  # 1. Delete
  for i in (0...len)
    v = name.clone
    v[i] = ''
    trans[v] = true
  end

  # 2. Insert
  for i in (0..len)
    for ch in ('a'..'z')
      v = name.clone
      v[i, 0] = ch
      trans[v] = true
    end
  end

  # 3. Replace
  for i in (0...len)
    for ch in ('a'..'z')
      v = name.clone
      v[i] = ch
      trans[v] = true
    end
  end

  # 4. Swap
  (0...len).each_cons(2) do |i, j|
    v = name.clone
    v[i] = name[j]
    v[j] = name[i]
    trans[v] = true
  end

  trans
end

### main

loop do
  n = gets.to_i
  break if n == 0

  d = gets.to_i

  names = []
  nmthash = {}
  nmts = {}

  n.times do
    name = gets.strip
    names << name
    trans = transform(name)
    nmthash[name] = trans
    nmts[name] = trans.keys
  end

  confs = []

  for i in (0...n)
    nmi = names[i]
    nmithash = nmthash[nmi]

    for j in ((i + 1)...n)
      nmj = names[j]

      if nmithash[nmj]
        confs << [nmi, nmj].sort
        next
      end
      next if d < 2

      nmts[nmj].each do |nmtj|
        if nmithash[nmtj]
          confs << [nmi, nmj].sort
          break
        end
      end
    end
  end

  confs.sort.each do |nm0, nm1|
    puts [nm0, nm1].join(',')
  end
  puts confs.length
end

Measured memory_bytes: 26040
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            int K;
            double S;
            string o = "no";

            for (int i = 0; i < 7; i++ )
            {
                S = Math.Pow(2,i);
                if (N >= S)
                {
                    o = S.ToString();
                }
            }
           
            Console.WriteLine(o);
        }
    }
}

Measured memory_bytes: 11220
N,K=map(int, raw_input().split())
S=raw_input()

c=S[0]
# T[0] := ("1" or "0", num)
T=[[c, 1]]
for s in S[1:]:
    if c==s:
        T[-1][1]+=1
    else:
        c=s
        T.append([c,1])

ans=-1
p=0
n=p
k=0
s=0
while n<len(T):
    if(T[n][0]=="0"):
        if k<K:
            k+=1
            s+=T[n][1]
        else:
            break
    else:
        s+=T[n][1]
    n+=1
ans=s
for pp in range(len(T)-1):
    p=pp+1
    if T[p][0]=="1":
        s-=T[p-1][1]
        if p+2*k-1<len(T):
            s+=T[p+2*k-1][1]
        if p+2*k<len(T):
            s+=T[p+2*k][1]
    else:
        s-=T[p-1][1]        
    ans=max(ans,s)  
print(ans)     



Measured memory_bytes: 16900
#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
#define ALL(x) (x).begin(), (x).end()

int N, i, j;
vector<P> task;
bool unable = false;

int main(){
    cin >> N;
    task.resize(N);
    for(int i = 0; i < N; i++) cin >> task[i].second >> task[i].first;

    sort(ALL(task)); //締め切り早い順にソート
    i = 0;
    int deadline;
    int day = 0;
    while(i < N){
        deadline = task[i].first;
        while(i < N && task[i].first == deadline) day += task[i++].second;
        if(day > deadline){ unable = true; break; }
    }

    if(unable) cout << "No" << endl;
    else cout << "Yes" << endl;

    return 0;
}
    


Measured memory_bytes: 1792
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<iomanip>
#include<queue>
#include<functional>
#include<iso646.h>
#include<bitset>

using namespace std;

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

const ll INF = 1e10;
//const ll MOD = 1e9 + 7;
const ll MOD = 998244353;

const ll MAX = 310000;


long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++){
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k){
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(){
	ll N, A, B, K;
	cin >> N >> A >> B >> K;
	ll i, j;
	ll ans = 0;
	COMinit();
	for (i = 0; i <= N; i++){
		if (i*A > K){
			break;
		}
		ll resi = K - i*A;
		if (resi%B == 0){
			j = resi / B;
			ll atmp = COM(N, i);
			ll btmp = COM(N, j);
			ans = (ans + 1LL * atmp * btmp % MOD) % MOD;
		}
	}
	cout << ans << endl;

	return 0;
}



Measured memory_bytes: 7552
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size();
#define all(x) (x).begin(), (x).end()
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define rep(i, n) for(int i =0 ; i < n; ++i)
#define Rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair < int, int >
typedef vector<int> vi;
typedef  long long ll;
const int inf = int(1e9);
const int mod =1e9+7;
const int N = (int)2e5 + 5;

void solve(){
	int a,b,c;
	cin>>a>>b>>c;
	swap(a,b);
	swap(a,c);
	cout<<a<<" "<<b<<" "<<c;


}
int main(){
	Red;
	int t=1;
	//cin>>t;
	while(t--)
	solve();
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
char S[51];

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

    int ans = 0;
    for(int i = 0; i < n - 2; i++) {
        if(S[i] == 'A' && S[i + 1] == 'B' && S[i + 2] == 'C') ans++;
    }

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

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9+100;
const ll INFL = 1e18+100;
const ll MOD = 1e9+7;
const bool debug = 0;
//-----------------------------------------------//

int main() {
	int D, N;
	cin >> D >> N;
	
	int ans = 1;
	REP(i, D) ans *= 100;
	if (N == 100) N++;
	ans *= N;
	
	cout << ans << endl;
	
	return 0;
}


Measured memory_bytes: 256
#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long LL;
LL h,i,j,k,l,m,n,p,t,v,w,x,y,a[200040];
string s[6];
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>h>>w>>p;
	for(;i<h;i++)cin>>s[i];
	for(i=0;i<1<<h;i++)
	{
		for(j=0;j<1<<w;j++)
		{
			for(k=m=0;k<h;k++)
			{
				if(i&1<<k)
				{
					for(l=0;l<w;l++)
					{
						if(j&1<<l&&s[k][l]=='#')m++;
					}
				}
			}
			if(m==p)t++;
		}
	}
	cout<<t;
}

Measured memory_bytes: 3640
def selection(n, data):
    __count = 0
    for i in range(n):
        min_i = i
        for j in range(i+1, n):
            if data[min_i][1] > data[j][1]:
                min_i = j
        tmp = data[i]
        data[i] = data[min_i]
        data[min_i] = tmp
        if min_i != i:
            __count += 1
    return data, __count

def bubble_sort(n, data):
    __count = 0
    for i in range(n-1):
        for j in range(n-i-1):
            if data[j][1] > data[j+1][1]:
                tmp = data[j+1]
                data[j+1] = data[j]
                data[j] = tmp
                __count += 1
    return data, __count

def show(data):
    for i in range(len(data)):
        if i != (len(data)-1):
            print(data[i], end = " ")
        else:
            print(data[i])

if "__main__" == __name__:
    n = int(input())
    data_b = input().split()
    data_s = data_b.copy()
    sort1, count = bubble_sort(n, data_b)
    show(sort1)
    print("Stable")
    sort2, count = selection(n, data_s)
    show(sort2)
    if sort1 == sort2:
        print("Stable")
    else:
        print("Not stable")



Measured memory_bytes: 5612
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd seocnd

using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    int a[n];
    fr(i, 0, n){
        cin >> a[i];
    }
    ll pre[n];
    ll suf[n];
    pre[0] = 0;
    fr(i, 0, n){
        if(i > 0) pre[i] = pre[i - 1];
        pre[i] += a[i];
    }
    suf[n - 1] = 0;
    for(int i = n - 1; i >= 0; i --){
        if(i + 1 < n) suf[i] = suf[i + 1];
        suf[i] += a[i];
    }
    int l = 0;
    int r = 0;
    ll ans = 1e11;
    fr(c, 1, n - 2){
        if(r <= c) r = c + 1;
        while(l + 1 < c && abs((pre[c] - pre[l]) - pre[l]) > abs((pre[c] - pre[l + 1]) - pre[l + 1])){
            l ++;
        }
        while(r + 2 < n && abs((pre[r] - pre[c]) - suf[r + 1]) > abs((pre[r + 1] - pre[c]) - suf[r + 2])){
            r ++;
        }
        ll MAX = max(max(pre[c] - pre[l], pre[l]), max(pre[r] - pre[c], suf[r + 1]));
        ll MIN = min(min(pre[c] - pre[l], pre[l]), min(pre[r] - pre[c], suf[r + 1]));
        

        ans = min(ans, MAX - MIN);

    }
    cout << ans << endl;



    return 0;
}


Measured memory_bytes: 4096
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

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

    int n, c;
    cin >> n >> c;

    //重さj「以下」で実現できる最大の価値
    vector<ll> r(c + 1, 0);

    for (int i = 0; i < n; i++) {
        int w, v;
        cin >> w >> v;

        for (int j = c; j >= w; j--) {
            r[j] = max(r[j], r[j - w] + v);
        }
    }

    cout << r[c] << endl;

    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h> 
 
using namespace std;
 
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

 
//typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;

//container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair 
#define SZ(a) int((a).size())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
//constant
//--------------------------------------------
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
 
const LL INF=LONG_MAX;
const double EPS=0.00001;
const long double PI = acos(-1.0);

const int MAX_N=1e5;


int main(){
    int N;
    cin>>N;
    vector<int>A(N);
    int cnt=0;
    REP(i,N){
        cin>>A[i];
        if(A[i]%2==0)cnt++;
    }
    LL res1=1,res2=1;
    REP(i,cnt)res1*=2LL;
    REP(i,N)res2*=3;
    cout<<res2-res1<<endl;
    return 0;
}


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

using namespace std;

typedef long long ll;
typedef vector<vector<char>> G;

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

long long bfs(int sx, int sy, vector<vector<bool>> &vis, G &g) {
  int h = g.size();
  int w = g[0].size();

  queue<pair<int, int>> que;
  que.push(make_pair(sx, sy));
  vis[sy][sx] = true;

  long long cnt_w = 0, cnt_b = 0;

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    int cur_x = cur.first;
    int cur_y = cur.second;

    if (g[cur_y][cur_x] == '#') {
      cnt_b++;
    } else {
      cnt_w++;
    }

    for (int i = 0; i < 4; i++) {
      int nx = cur_x + dx[i];
      int ny = cur_y + dy[i];

      if (nx < 0 || ny < 0 || nx >= w || ny >= h) {
        continue;
      }
      if (vis[ny][nx]) {
        continue;
      }
      if (g[cur_y][cur_x] == g[ny][nx]) {
        continue;
      }
      vis[ny][nx] = true;
      que.push(make_pair(nx, ny));
    }
  }
  return cnt_b * cnt_w;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> grid(h, vector<char>(w));

  for (int y = 0; y < h; y++) {
    string tmp_s;
    cin >> tmp_s;
    for (int x = 0; x < w; x++) {
      grid[y][x] = tmp_s[x];
    }
  }

  long long ans = 0;
  vector<vector<bool>> is_visited(h, vector<bool>(w, false));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (!is_visited[y][x]) {
        ans += bfs(x, y, is_visited, grid);
      }
    }
  }
  cout << ans << "\n";

  return 0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct info {
	int size, Max, Min;
} dp[MAXN];
int ans, now;
char s[MAXN];
vector <int> a[MAXN];
info operator + (info a, int one) {
	a.Max += a.size;
	a.Min += a.size;
	return a;
}
info operator + (info a, info b) {
	info ans;
	ans.size = a.size + b.size;
	ans.Max = a.Max + b.Max;
	if (a.Min > b.Min) swap(a, b);
	if (a.Max > b.Min) ans.Min = ans.Max & 1;
	else ans.Min = b.Min - a.Max;
	return ans;
}
void work(int pos, int fa, int depth) {
	dp[pos] = (info) {0, 0, 0};
	for (auto x : a[pos])
		if (x != fa) {
			work(x, pos, depth + 1);
			dp[pos] = dp[pos] + (dp[x] + 1);
		}
	if (s[pos] == '1') {
		dp[pos].size++;
		now += depth;
	}
}
int main() {
	int n; read(n), ans = 1e9;
	scanf("%s", s + 1);
	for (int i = 1; i <= n - 1; i++) {
		int x, y; read(x), read(y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	for (int i = 1; i <= n; i++) {
		now = 0, work(i, 0, 0);
		if (dp[i].Min == 0) chkmin(ans, now / 2);
	}
	if (ans == 1e9) puts("-1");
	else writeln(ans);
	return 0;
}

Measured memory_bytes: 512
const teststr = 
"""
46 4 40 5
"""

function lines()
  Channel{String}(32) do ch
    ll = readlines()
    lines = isempty(ll) ? split(teststr, "\n") : ll
    for s in lines
      put!(ch, s)
    end
  end
end

quit() = exit(0)
in_1(ch) = parse(Int, take!(ch))
in_a(ch) = parse.(Int, split(take!(ch)))

function main()
  ch = lines()
  A,B,C,D = in_a(ch)
  while true
    C -= B
    if C <= 0
      println("Yes")
      break
    end
    A -= D
    if A <= 0
      println("No")
      break
    end
  end
end

main()


Measured memory_bytes: 187132
//AtCoder ABC050 C - Lining Up
#include <bits/stdc++.h>
using namespace std;
using lli=int64_t;
constexpr lli MOD=1e9+7;

int main() {
    int N;
    cin>>N;
    vector<int> atbl(N);
    for(auto&& a:atbl){
        cin>>a;
    }
    sort(atbl.begin(),atbl.end());
    int st=(N%2==0?1:0);
    int cnt=(N%2==0?0:1);
    bool dis=false;
    for(const auto& a: atbl){
        if(a==st){
            ++cnt;
            if(cnt%2==0) st+=2;
        }else{
            dis=true;//0
            break;
        }
    }
    lli ans=dis?0:1;
	for(int i = 1; i <= N / 2; ++i) {
		ans = ans * 2 % MOD;
	}
    cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 640
#include<iostream>

int main(){
  
  int first,end,width;
  std::cin >> first >> end >> width;

  if((first+width-1)>=(end-width+1)){
    int i=0;
    while(true){
      std::cout << (first+i) << std::endl;
      if((first+i)== end){
	return 0;
      }
      i++;
    }
  }

  for(int i=0;i<width;i++){
    std::cout << first+i << std::endl;
  }

  for(int i=width-1;i>=0;i--){
    std::cout << end-i << std::endl;
  }
  return 0;
}
      
  


Measured memory_bytes: 256
#include <iostream>
using namespace std;
#include <stdio.h>
int main()
{
	char str[1201];
	int count[26] = { 0 }, i = 0, k;
	for (i = 0;;i++){
		str[i] = getchar();
		if (str[i] == '\n') break;
		if ('a' <= str[i] && str[i] <= 'z'){
			k = str[i] - 'a';
			str[i] = 'A' + k;
		}
		else if ('A' <= str[i] && str[i] <= 'Z'){
			k = str[i] - 'A'; 
			str[i] = 'a' + k;
		}
	}
	for (i=0;;i++){
		printf("%c", str[i]);
		if (str[i] == '\n') break;
	}
	return 0;
}

Measured memory_bytes: 1072
#include <bits/stdc++.h>
 
using namespace std;
 
#define sf(n) scanf("%d", &n)
#define pd(n) printf("%.12lf", n)
#define pf(n) printf("%d", n)
#define p() printf("\n")
#define ps() printf(" ")
#define ll long long
#define ull unsigned long long
#define MAX 1e9
const int mod = 1000 * 1000 * 1000 + 7;
#define ex 1e18
#define dbl 2e09
#define qu(q) queue<int> q
#define pqu(q) priority_queue<int> q
#define len(s) s.length()
#define sz(x) int(x.size())
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
#define clr(x) x.clear()
#define LOG(x) std::cout << x << std::endl;
 
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = 3.14159265359;
 
int dx[6] = {-1,-1,0,0,1,1};
int dy[6] = {-1,0,-1,1,0,1};

int n,b;
ll dp[105][100005];
int w[105],v[105];
ll solve(int idx,int curr){
    if(curr==0)
        return 0;
    if(idx==n)
        return 0;
    ll &ret=dp[idx][curr];
    if(~ret)return ret;
    ret=solve(idx+1,curr);
    if(w[idx]<=curr){
        ret= max(ret,solve(idx+1,curr-w[idx])+v[idx]);
    }
    return ret;
}



int main(){
    cin>>n>>b;
    for(int i=0;i<n;++i){
        cin>>w[i]>>v[i];
    }
    memset(dp,-1,sizeof dp);
    cout<<solve(0,b)<<endl;
    return 0;
}

Measured memory_bytes: 82304
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mod 1e9+7
#define all(x) x.begin(),x.end()
int main() {
	ios_base::sync_with_stdio(false);
	cout.tie(0); cin.tie(0);
ll x,k,d;
  cin>>x>>k>>d;
  ll res;
  res=x/d;
  if(abs(res)<k)
  {k-=abs(res);
   x-=d*res;
     if(k%2==0)
       cout<<abs(x);
     else
       if(x>0)
       cout<<abs(x-d);
       else
       cout<<abs(x+d);
   }
  else if(abs(res)==k)
  { x-=d*res;
    cout<<abs(x);}
  else if(abs(res)>k)
  {if(x>0)
       x-=d*k;
   else if(x<0)
      x+=d*k;
   cout<<abs(x);
  }
	
}

Measured memory_bytes: 3624
#include<iostream>
#include<cstdio>
#include<cstring>
#include <fstream>
#include<string>
#include<sstream>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
#include <math.h>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1000000007;
const int INF = 1000000007;
const double EPS = 1e-10;
const double PI = 3.14159265359;
const int N = 200005;
//cout << fixed << std::setprecision(9)
//--------------------------

string s, t;
int dp[3003][3003];
int n, m;

int main()
{
	cin >> s >> t;
	n = s.size();
	m = t.size();

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			if (s[i] == t[j]) {
				dp[i + 1][j + 1] = dp[i][j] + 1;
			}
			else {
				dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
			}
		}

	string ans = "";
	while (n > 0 && m > 0) {
		if (dp[n][m] == dp[n - 1][m]) n--;
		else if (dp[n][m] == dp[n][m - 1]) m--;
		else {
			ans = s[n - 1] + ans;
			n--;
			m--;
		}
	}

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 35456
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <float.h>
#include <random>

#define repeat(i,n) for (int i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
#define debugArrayP(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge].first<< " " << x[hoge].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const int INF = INT_MAX;
const ll MOD = 1e9+7;



int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N,Q;cin>>N>>Q;
  string s;cin>>s;
  s = "?"+s+"?";
  string t(Q,' '),d(Q,' ');
  repeat(i,Q){
    cin>>t[i]>>d[i];
  }
  int ans=N;
  int low=0,high=N+1;
  while(high-low>1){
    int mid=(high+low)/2;
    int cur=mid;
    bool exist=true;
    repeat(i,Q){
      if(s[cur]==t[i]){
        cur += 2*(d[i]=='R')-1;
        if(cur==0){
          exist=false;
          break;
        }
      }
    }
    if(exist){
      high=mid;
    }else{
      low=mid;
    }
  }
  ans -= low;
  low=0,high=N+1;
  while(high-low>1){
    int mid=(high+low)/2;
    int cur=mid;
    bool exist=true;
    repeat(i,Q){
      if(s[cur]==t[i]){
        cur += 2*(d[i]=='R')-1;
        if(cur==N+1){
          exist=false;
          break;
        }
      }
    }
    if(exist){
      low=mid;
    }else{
      high=mid;
    }
  }
  ans -= N-low;
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 952
fh=STDIN
while fh.gets do
  reg = Regexp.new("[+=]")
  s=$_.chomp.split(reg)
  t =("0".."9").to_a.select { |x|
    s0=s[0].gsub(/X/,x)
    s1=s[1].gsub(/X/,x)
    s2=s[2].gsub(/X/,x)
    s0==s0.to_i.to_s and s1==s1.to_i.to_s and s2==s2.to_i.to_s and s0.to_i + s1.to_i == s2.to_i}
  puts (t.length==0) ? "NA" : t[0]
end

Measured memory_bytes: 9544
/*
  #問題：
  


  #分析：
  #オブジェクト：
  
  #条件：
  

  #要求：
  

  #解法：
  
*/

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>     //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>        //do setprecision
#include <ctime>
#include <complex>
#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define uLL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int mod=1000000007;


const unsigned int BIT_FLAG_0 = (1<<0);
const unsigned int BIT_FLAG_1 = (1<<1);
const unsigned int BIT_FLAG_2 = (1<<2);
const unsigned int BIT_FLAG_3 = (1<<3);
const unsigned int BIT_FLAG_4 = (1<<4);
const unsigned int BIT_FLAG_5 = (1<<5);
const unsigned int BIT_FLAG_6 = (1<<6);
const unsigned int BIT_FLAG_7 = (1<<7);


#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
namespace mp = boost::multiprecision;

using Bint = mp::cpp_int;


int main(void){
  vector<int> a(5);
  FOR(i,0,5){
    cin>>a[i];
  }
  int k; cin>>k;
  int maxx=*max_element(a.begin(), a.end());
  int minx=*min_element(a.begin(),a.end());
  if(maxx-minx<=k)   cout<<"Yay!"<<endl;
  else cout<<":("<<endl;

  return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair <int, int> pii;

const int INF = 0x3f3f3f3f;
int n, e, c[1005][1005];
vector <int> adj[1005];

int bfs(int s, int t, vector <int> &p) {
//	cout << "start bfs" << endl;
	fill(p.begin(), p.end(), -1);
	p[s] = s;
	queue <pii> q;
	q.push(mp(s, INF));
	while (!q.empty()) {
		int u = q.front().fi, f = q.front().se;
//		cout << u << ' ' << f << endl;
		q.pop();
		for (int i : adj[u]) {
			if (p[i] == -1 && c[u][i]) {
				p[i] = u;
				int nf = min(f, c[u][i]);
				if (i == t) {
					return nf;
				}
				q.push(mp(i, nf));
			}
		}
	}
	return 0;
}

int maxFlow(int s, int t) {
//	cout << "start mxflow" << endl;
	int rt = 0, nf;
	vector <int> p;
	p.resize(n + 5);
	while (nf = bfs(s, t, p)) {
		rt += nf;
//		cout << ">> " << nf << endl;
		int bt = t;
//		for (int i = 0; i < n; i++) cout << p[i] << ' ';
//		cout << endl;
		while (p[bt] != bt) {
			int v = p[bt];
			c[v][bt] -= nf;
			c[bt][v] += nf;
//			cout << v << ' ' << bt << endl;
			bt = v;
		}
	}
	return rt;
}

int main() {
	cin >> n >> e;
	for (int i = 0; i < e; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		c[u][v] = w;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	cout << maxFlow(0, n - 1) << endl;
}



Measured memory_bytes: 3544
//g++ -std=c++14 test.cpp -o test.out

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <map> 
#include <unordered_map> 
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;
 
#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

//二項係数のクラス mod使わないバージョン
//modを取らないためMAX_COMBはせいぜい50くらいが限度
#define MAX_COMB 50LL
class Comb{
private:
	//i_C_jをcomb[i][j]で表す
	//i<jとなってしまった時は0を返す
	LL comb[MAX_COMB+1][MAX_COMB+1] = {};
public:
	//初期化処理
	Comb(){
		//i_C_0は絶対1
		for(int i=0;i<=MAX_COMB;i++) comb[i][0] = 1LL;
		//パスカルの三角形から各i_C_jを求める
		for(int i=1;i<=MAX_COMB;i++){
			for(int j=1;j<=i;j++){
				comb[i][j] = comb[i-1][j-1]+comb[i-1][j];
			}
		}
	}
	//i_C_jをcomb[i][j]をという形で返す
	//i<jとなってしまった時は0を返す
	LL operator()(LL i,LL j){
		return comb[i][j];
	}
};

LL N,A,B;
LL V[50];

double average = 0;
LL ans = 0;

void solve(){
	Comb c;

	map<LL,LL> counter;//数iのでてきた回数をcounter[i]とする
	for(int i=0;i<N;i++){
		counter[-V[i]]++;
	}

	double took_number = double(A);
	bool flag = false;
	for(auto iter=counter.begin();iter!=counter.end();iter++){
		LL v = -(iter->first);//商品の価値　降順に取り出したいので一度負数にして戻している
		LL num = iter->second;//その商品の数
		if(A-num<=0){
			if(flag){
				ans = c(num,(num>=A)?A:num);
				average += (A*v);
				average /= took_number;
				return;
			}else{
				for(int i=A;i<=B && i<=num;i++)ans += c(num,i);
				average += (A*v);
				average /= took_number;
				return;
			}
		}else{
			flag = true;
			A -= num;
			B -= num;
			average += (num*v);
		}
	}
}

int main(){
    cin >> N >> A >> B;
    for(int i=0;i<N;i++) cin >> V[i];

    solve();

	cout << fixed << setprecision(10) << average << endl;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
N = int(input())
W = [input() for _ in range(N)]
w_list = [W[0]]
for i in range(N-1):
  if W[i][-1] == W[i+1][0] and W[i+1] not in w_list:
    w_list.append(W[i+1])
  else:
    print("No")
    break
else:
  print("Yes")
  

Measured memory_bytes: 3060
use proconio::input;
use std::collections::BTreeMap;

const M: u64 = 1_000_000_007;

fn main() {
    input! {
        n: usize,
        d: [(i64, i64); n],
    }

    let mut z = 0;
    let mut t = BTreeMap::new();
    for e in d {
        if e.0 == 0 && e.1 == 0 {
            z += 1;
        } else if e.0 == 0 {
            *t.entry((0, 1)).or_insert(0) += 1;
        } else if e.1 == 0 {
            *t.entry((1, 0)).or_insert(0) += 1;
        } else {
            let g = num::integer::gcd(e.0, e.1);
            let e = if e.0 < 0 {
                (-e.0, -e.1)
            } else {
                e
            };
            let e = (e.0 / g, e.1 / g);
            *t.entry((e.0, e.1)).or_insert(0) += 1;
        }
    }
    z %= M;

    let mut ans = 1;
    while let Some((&k, &c)) = t.iter().next() {
        t.remove(&k);
        let inv = if k.1 < 0 {
            (-k.1, k.0)
        } else {
            (k.1, -k.0)
        };

        let c2 = modpow(2, c);
        if let Some(invc) = t.remove(&inv) {
            let i2 = modpow(2, invc);
            ans *= c2 + i2 - 1;
        } else {
            ans *= c2;
        }
        ans %= M;
    }
    ans += z + M - 1;
    ans %= M;
    println!("{}", ans);
}

fn modpow(x: u64, y: u64) -> u64 {
    let mut result = 1;
    let mut a = x;
    let mut b = y;
    while b > 0 {
        if b % 2 == 1 {
            result = result * a % M;
        }
        a = a * a % M;
        b /= 2;
    }
    result
}


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

#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define ACC(c) (accumulate(ALL(c), 0LL))
#define PRINT(x) cout << (x) << "\n"

using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;

inline int std_in(int& a) {return scanf("%d", &a);}
auto pint_compare = [](pint x, pint y) -> bool{return (x.first < y.first);};
const int MOD = 1000000007;


signed main(){
    ll N;
    cin >> N;
    ll ans = INT64_MAX;
    for(ll i = 1; i * i <= N; i++){
        if(N % i != 0) continue;
        ans = min(ans, (i - 1) + (N / i - 1));
    }
    PRINT(ans);
    return 0;
}

Measured memory_bytes: 384
"use strict"
function main(arg) {
    var k = arg.trim().split("\n")[0].split(" ").map(Number)[0]
    var a = arg.trim().split("\n")[1].split(" ").map(Number).sort((a,b) => {
        return a-b
    })
    var sum = 0
    var max = 0
    for (var i = 0; i < a.length; i++) {
        var last_index = i - 1
        var x = 0
        var y = 0
        x = a[i]
        if (i === 0) {
            last_index = a.length - 1
            y = a[last_index]
            x = k + a[0]
        } else {
            y = a[last_index]
        }
        var diff = Math.abs(x - y)
        // console.log([x,y])
        // console.log(diff)
        max = Math.max(diff, max)
        // sum += 
        sum += diff
    }
    console.log(sum - max)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

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

using namespace std;

#define LL long int

const int MAX = 1e6;
const int MOD = 1e9+7;



int main(){
    int s;
    vector<int> ans(s + 1,0);
    cin >> s;

    ans[0] = 1;

    for(int i = 0; i <= s; ++i){
        if(i - 3 >= 0){
            ans[i] = ans[i - 1] + ans[i - 3];
            ans[i] %= MOD;
        }
    }

    cout << ans[s] << endl;

    return 0;
}

Measured memory_bytes: 3692
# encoding: "utf-8"

from numpy import array

class Stdin:
    @staticmethod
    def read_line(converter=str):
        return [converter(x) for x in input().split()]
    
    @staticmethod
    def read_lines(n, converter=str):
        result = list()
        for _ in range(n):
            result.append(Stdin.read_line(converter))
        return result
    
    @staticmethod
    def convert(data, converter):
        assert(len(data) == len(converter))
        return tuple(map(lambda x, f: f(x), data, converter))

    @staticmethod
    def convert_lines(datas, n, converter):
        return [converter(data, n, converter) for data in datas]


def main():
    _ = input()
    xs = Stdin.read_line(converter=int)
    ys = array(list(map(lambda x: [x-1, x, x+1], xs))).flatten()
    ys.sort()
    cnt = 0
    m = 0
    prev = ys[0]
    for y in ys:
        if y == prev:
            cnt += 1
        else:
            if m < cnt:
                m = cnt
            prev = y
            cnt = 1
    print(m)

if __name__ == "__main__":
    main()

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

using namespace std;

int main() {
    int a,b,c,x,y;
    cin>>a>>b>>c>>x>>y;
    int ans=0;
    ans+=min(x,y)*min(a+b,2*c);
    int n=abs(x-y);
    ans+=min(n*(x<y?b:a),2*n*c);
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
#define show(obj) {for(auto x:obj)cout<<x<<' ';cout<<endl;}
#define line "----------"
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int main(){
	string s; cin>>s;
	int a,b,c,d;
	a = s[0]-'0';
	b = s[1]-'0';
	c = s[2]-'0';
	d = s[3]-'0';
	if(a+b+c+d == 7){printf("%d+%d+%d+%d=7\n",a,b,c,d);return 0;}
	if(a+b+c-d == 7){printf("%d+%d+%d-%d=7\n",a,b,c,d);return 0;}
	if(a+b-c+d == 7){printf("%d+%d-%d+%d=7\n",a,b,c,d);return 0;}
	if(a+b-c-d == 7){printf("%d+%d-%d-%d=7\n",a,b,c,d);return 0;}
	if(a-b+c+d == 7){printf("%d-%d+%d+%d=7\n",a,b,c,d);return 0;}
	if(a-b+c-d == 7){printf("%d-%d+%d-%d=7\n",a,b,c,d);return 0;}
	if(a-b-c+d == 7){printf("%d-%d-%d+%d=7\n",a,b,c,d);return 0;}
	if(a-b-c-d == 7){printf("%d-%d-%d-%d=7\n",a,b,c,d);return 0;}
	return 0;
}

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

char s[10000+4];

int main()
{
	while(cin >> s){
		int i=0;
		int joi=0,ioi=0;
		while(s[i] != '\0'){
			if(s[i] == 'J'){
				if(s[i+1] == 'O'){
					if(s[i+2] == 'I'){
						joi++;
					}
				}
			}
			if(s[i] == 'I'){
				if(s[i+1] == 'O'){
					if(s[i+2] == 'I'){
						ioi++;
					}
				}
			}
			i++;
		}
		cout << joi << endl;
		cout << ioi << endl;
	}
	return 0;
}

Measured memory_bytes: 0
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

template <ll Modulus = 998244353>
class modint {
 public:
  ll a;

  constexpr modint(const ll x = 0) noexcept : a(x % Modulus) {}
  constexpr ll& value() noexcept { return a; }
  constexpr const ll& value() const noexcept { return a; }
  constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }
  constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }
  constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }
  constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }
  constexpr modint& operator+=(const modint& rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint& operator-=(const modint& rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint& operator*=(const modint& rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint& operator/=(modint rhs) noexcept {
    ll exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
using mint = modint<>;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for_each(begin(A), end(A), [](auto& x) { cin >> x; });

  mint ans = 1;
  // 個数を数える
  // 最大値を覚える
  map<ll, ll> B;
  ll max_num = 0;
  for (ll i = 0; i < N; i++) {
    B[A[i]]++;
    max_num = max(max_num, A[i]);
  }
  // 0は一つのみ。0はi=1のみ。
  if (B[0] != 1 || A[0] != 0) {
    cout << 0 << endl;
    return 0;
  }
  // i= 2からは計算する
  for (ll i = 1; i < max_num; i++) {
    if (B[i] == 0) {
      // 連結にできない。無理
      cout << 0 << endl;
      return 0;
    }
    // 要素数を次の要素数だけ累乗する。
    mint temp = 1;
    for (ll j = 0; j < B[i + 1]; j++) {
      temp *= B[i];
    }
    ans *= temp;
  }

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


Measured memory_bytes: 7296
import java.util.Scanner;


public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String tmp = scan.nextLine();
    int a = Integer.parseInt(tmp);
    String str = scan.nextLine();
    if(a>=3200){
      System.out.println(str);
    }else{
      System.out.println("red");
    }
    scan.close();
  }
}

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

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

int main(){
    int n,h;
    cin>>h>>n;
    
    vector<int> a(n),b(n),dp(h+1,1e9);
    rep(i,n) cin>>a[i]>>b[i];
    
    dp[0]=0;
    rep(i,n)rep(j,h+1)dp[min(j+a[i],h)]=min(dp[min(j+a[i],h)],dp[j]+b[i]);
    cout<<dp[h];
    return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
import numpy as np
from numba import njit


def read():
    D = int(input().strip())
    C = np.fromstring(input().strip(), dtype=np.int32, sep=" ")
    S = np.empty((D, 26), dtype=np.int32)
    for i in range(D):
        s = np.fromstring(input().strip(), dtype=np.int32, sep=" ")
        S[i, :] = s[:]
    M = 1000
    RD = np.random.randint(D, size=(M*2, ), dtype=np.int32)
    RQ = np.random.randint(26, size=(M*2, ), dtype=np.int32)
    DQ = np.stack([RD, RQ]).T
    return D, C, S, M, DQ


@njit
def diff_satisfaction(C, S, d, p, last):
    """d日目にコンテストpを開催するときの、満足度の更新量を求める
    """
    v = 0
    for i in range(26):
        v -= C[i] * (d - last[i])
    v += C[p] * (d - last[p])
    v += S[d, p]
    return v


@njit
def change_schedule(D, C, S, T, d, q, cumsat):
    """d日目のコンテストをqに変更する
    """
    p = T[d]
    dp1, dq1 = -1, -1
    dp3, dq3 = D, D
    for i in range(0, d):
        if T[i] == p: 
            dp1 = i
        if T[i] == q:
            dq1 = i
    for i in range(D-1, d, -1):
        if T[i] == p:
            dp3 = i
        if T[i] == q:
            dq3 = i
    cumsat = cumsat - S[d, p] + S[d, q] - C[p] * (dp3-d) * (d-dp1) + C[q] * (dq3-d) * (d-dq1)
    return cumsat


@njit
def greedy(D, C, S):
    T = np.zeros(D, dtype=np.int32)
    last = -np.ones(26, dtype=np.int32)
    cumsat = 0
    for d in range(D):
        max_p = 0
        max_diff = -999999999

        # select contest greedily
        for p in range(26):
            diff = diff_satisfaction(C, S, d, p, last)
            if diff > max_diff:
                max_p = p
                max_diff = diff
        
        # update schedule
        cumsat += max_diff
        T[d] = max_p
        last[max_p] = d
    return cumsat, T


@njit
def solve(D, C, S, M, DQ):
    cumsat, T = greedy(D, C, S)
    for i in range(M):
        d1, q1 = DQ[i*2, :]
        d2, q2 = DQ[i*2+1, :]
        newsat = cumsat
        newsat = change_schedule(D, C, S, T, d1, q1, newsat)
        t1 = T[d1]
        T[d1] = q1
        newsat = change_schedule(D, C, S, T, d2, q2, newsat)
        t2 = T[d2]
        T[d2] = q2
        if newsat > cumsat:
            cumsat = newsat
        else:
            T[d2] = t2
            T[d1] = t1
    for t in T:
        print(t+1)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))


Measured memory_bytes: 119604
# -*- coding: utf-8 -*-

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict

n,k = map(int, input().split())
p = list(map(int, input().split()))
ans=0
for i in range(k):
    an = min(p)
    ans=ans+an
    a = p.index(an)
    p.pop(a)

print(ans)

Measured memory_bytes: 9696
#
# ???????????¢?´¢
#
n = gets.chomp.to_i
graph = [[]]
n.times do
  graph << gets.chomp.split.map { |s| s.to_i }
end

dis = []
que = []
visited = []

dis[1] = 0
2.upto(n) do |i|
  dis[i] = -1
end
que << 1

# debug
# p graph
until que.empty?
  current_node = que.shift
  visited << current_node
  graph[current_node][1].times do |i|
    next_node = graph[current_node][2+i]
    if dis[next_node] == -1
      dis[next_node] = dis[current_node] + 1
    end
    
    if !(visited.include?(next_node)) and !(que.include?(next_node))
      que << next_node
    end
    # debug
    # p que
  end
end

1.upto(n) do |i|
  puts "#{i} #{dis[i]}"
end

Measured memory_bytes: 8712