akhauriyash/Code-Regression huggingface dataset:
fn solve(h: usize, w: usize, v: &Vec<Vec<char>>) -> bool {
    let in_area = |y: usize, x: usize| y < h && x < w;
    let paintable = |y: usize, x: usize| {
        let dx = [-1, 0, 1, 0];
        let dy = [0, 1, 0, -1];
        (0..4).any(|i| {
            let yy = y.wrapping_add(dx[i] as usize);
            let xx = x.wrapping_add(dy[i] as usize);
            in_area(yy, xx) && v[yy][xx] == '#'
        })
    };
    v.iter().enumerate().all(|(y, s)| {
        s.iter()
            .enumerate()
            .all(|(x, &c)| c != '#' || paintable(y, x))
    })
}

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    let mut iter = s.split_whitespace().map(|w| w.trim().parse().ok().unwrap());
    let h = iter.next().unwrap();
    let w = iter.next().unwrap();
    let mut v: Vec<_> = Vec::new();
    for _i in 0..h {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).ok();
        let s = s.trim().chars().collect::<Vec<_>>();
        v.push(s);
    }
    let res = solve(h, w, &v);
    println!("{}", if res { "Yes" } else { "No" });
}


Measured memory_bytes: 4352
N=int(input())

friendlist=[0 for i in range(N+2)]

"""
for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(b+1,N+2)):
        friendlist[k]+=1
"""


ab=[[int(i) for i in input().split(" ")] for j in range(N)]
a=[ab[i][0] for i in range(N)]
b=[ab[i][1] for i in range(N)]

a.sort()
b.sort()



for i in range(1,N):
    for j in range(a[i-1],min(a[i],N+2)):
        friendlist[j]+=i
for i in range(a[N-1],N+2):
    friendlist[i]+=N

for i in range(1,N):
    for j in range(b[i-1]+1,min(b[i]+1,N+2)):
        friendlist[j]-=i
for i in range(b[N-1]+1,N+2):
    friendlist[i]-=N
        


#"""

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)
    
        

Measured memory_bytes: 31460
import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    val a = (0 until n).map { sc.next().toLong() }
    println(problem069c(n, a))
}

fun problem069c(n: Int, a: List<Long>): String {
    val fours = a.filter { it % 4 == 0L }
    val twoCount = a.count { it % 2 == 0L && it % 4 != 0L }
    return if (fours.size >= (n - if (twoCount >= 2) twoCount - 1 else 0) / 2) "Yes" else "No"
}

Measured memory_bytes: 49808
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;

    vector<int> v(101);
    for(int i = 2; i <= n; i++)
    {
        int k = i;
        for(int j = 2; j <= n; j++)
        {
            while(k % j == 0)
            {
                v[j]++;
                k /= j;
            }
            if(k == 1) break;
        }
    }
    int count = 0;
    // 1 * 75
    for(int i = 2; i <= n; i++)
    {
        int cand = 1;
        if(v[i] >= 74)
        {
            count++;
        }
    }
    // 3 * 25
    for(int i = 2; i <= n; i++)
    {
        int cand = 1;
        if(v[i] >= 24)
        {
            cand = i;
            for(int j = 2; j <= n; j++)
            {
                if(j != cand && v[j] >= 2)
                {
                    count++;
                }
            }
        }
    }
    // 5 * 15
    for(int i = 2; i <= n; i++)
    {
        int cand = 1;
        if(v[i] >= 14)
        {
            cand = i;
            for(int j = 2; j <= n; j++)
            {
                if(j != cand && v[j] >= 4)
                {
                    count++;
                }
            }
        }
    }
    // 3 * 5 * 5
    for(int i = 2; i <= n; i++)
    {
        int cand = 1;
        if(v[i] >= 2)
        {
            cand = i;
            for(int j = 2; j <= n; j++)
            {
                if(j != cand && v[j] >= 4)
                {
                    for(int k = j+1; k <= n; k++)
                    {
                        if(k != cand && v[k] >= 4)
                        {
                            count++;
                        }
                    }
                }
            }
        }
    }
    cout << count << endl;
}




Measured memory_bytes: 256
#include<stdio.h>
    int main()
    {
    	int n,x,sum=0;
    	scanf("%d",&n);
    	int k=n;
    	while(k--){
    		scanf("%d",&x);
    		sum=sum+x;
    	}
    	printf("%d\n",sum-n);
    	return 0;
    }

Measured memory_bytes: 128
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200010
#define x1 xx1
#define x2 xx2
using namespace std;

struct edge{int x, y, next;}a[N];
int n, l, p[N], x, flag[N], cl, d[N], b[N], ss, f[N], mex[N], x1, x2, size[N];

inline void add(int x, int y){a[++l].x=x; a[l].y=y; a[l].next=p[x]; p[x]=l;}

inline void dfs_circle(int x){
	flag[x]=ss; d[++cl]=x;
	for(int i=p[x]; i; i=a[i].next){
		if(flag[a[i].y]&&flag[a[i].y]<ss)continue;
		if(!flag[a[i].y]){dfs_circle(a[i].y); continue;}
		l=0; int now=cl;
		while(d[now]!=a[i].y){b[++l]=d[now]; now--;}
		b[++l]=a[i].y;
	}
	cl--;
}

inline void dfs_mex(int x){
	size[x]=0;
	for(int i=p[x]; i; i=a[i].next){
		dfs_mex(a[i].y); size[x]++;
	}
	for(int i=0; i<=size[x]; i++)f[i]=0;
	for(int i=p[x]; i; i=a[i].next)if(mex[a[i].y]<=size[x])f[mex[a[i].y]]=1;
	for(int i=0; i<=size[x]; i++)if(!f[i]){mex[x]=i; break;}
}

inline int check(int w){
	mex[b[1]]=w; int x, sum;
	for(int i=2; i<=l+1; i++){
		x=b[i]; sum=size[x]+1;
		for(int j=0; j<=sum; j++)f[j]=0;
		for(int j=p[x]; j; j=a[j].next)if(mex[a[j].y]<=sum)f[mex[a[j].y]]=1;
		for(int j=0; j<=sum; j++)if(!f[j]){mex[x]=j; break;}
	}
	return mex[b[1]]==w?1:0;
}

int main(){
	scanf("%d", &n);
	l=0; memset(p, 0, sizeof(p));
	for(int i=1; i<=n; i++){scanf("%d", &x); add(x, i);}
	memset(flag, 0, sizeof(flag)); ss=0;
	for(int i=1; i<=n; i++)if(!flag[i]){ss++; cl=0; dfs_circle(i);}
	b[l+1]=b[1];
	memset(flag, 0, sizeof(flag));
	for(int i=1; i<=l; i++)flag[b[i]]=1;
	for(int i=1; i<=l; i++){
		x=b[i]; size[x]=1;
		for(int j=p[x]; j; j=a[j].next)if(!flag[a[j].y]){
			dfs_mex(a[j].y); size[x]++;
		}
	}
	x=b[1];
	memset(f, 0, sizeof(f));
	for(int i=p[x]; i; i=a[i].next)if(!flag[a[i].y])f[mex[a[i].y]]=1;
	x1=x2=-1;
	for(int i=0; i<=n+1; i++)if(!f[i]){
		if(x1==-1)x1=i; else if(x2==-1)x2=i;
	}
	if(check(x1)){printf("POSSIBLE"); return 0;}
	if(check(x2)){printf("POSSIBLE"); return 0;}
	printf("IMPOSSIBLE");
	return 0;
}

Measured memory_bytes: 9600
#include<cstdio>
#include<bitset>
#include<string>
#include<iostream>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int MA = 4040;
const long long mod = 998244353;

bitset<MA> X;
bitset<MA> a[6];
int N;

char ch[MA + 1];

int cnt = 0;

void output(bitset<MA> b, char ch){
	for(int i = 5; i >= 0; --i){
		printf("%d", (int)b[i]);
	}
	printf("%c", ch);
	cnt++;
	if(cnt > 100) exit(0);
}

void input(){
	scanf("%d", &N);
	scanf("%s", ch);
	string str = ch;
	X = bitset<MA>(str);
	for(int i = 0; i < N; ++i){
		char ch[MA + 1];
		scanf("%s", ch);
		string str = ch;
		a[i] = bitset<MA>(str);
	}
}

long long ex[MA];

int getD(bitset<MA> &a){
	int res = -1;
	for(int i = 0; i < MA; ++i){
		if(a[i] == 1){
			res = i;
		}
	}
	return res;
}

void step(bitset<MA> &a, bitset<MA> &b){
	int ta = -1, tb = -1;
//	output(a, ' ');
//	output(b, ' ');
	for(int i = 0; i < MA; ++i){
		if(a[i] == 1){
			ta = i;
		}
		if(b[i] == 1){
			tb = i;
		}
	}
	if(ta < tb) swap(a, b);
	int x = ta - tb;
	a ^= (b << x);
//	output(a, ' ');
//	output(b, '\n');
}

void calc(){
	for(int i = 1; i < N; ++i){
		while(a[0] != a[i]){
			if(a[0] == bitset<MA>()){
				a[0] = a[i];
				break;
			}
			if(a[i] == bitset<MA>()){
				break;
			}
			step(a[0], a[i]);
		}
	}
}

bool cmp(bitset<MA> &a, bitset<MA> &b){
	for(int i = MA - 1; i >= 0; --i){
		if(a[i] > b[i]) return false;
		else if(a[i] < b[i]) return true;
	}
	return true;
}

long long solve(){
	calc();
	ex[0] = 1;
	for(int i = 1; i < MA; ++i){
		ex[i] = ex[i - 1] * 2;
		if(ex[i] > mod) ex[i] -= mod;
	}
	calc();
	//output(a[0], '-');
	int D = getD(a[0]);
	int M = getD(X);
	if(M < D) return 1;
	bitset<MA> tmp;
	long long ans = 0;
	for(int i = M - D; i >= 0; --i){
	//	output(tmp, '\n');
		if(X[i + D] == 1){
			ans += ex[i];
			ans %= mod;
		}
		if(X[i + D] != tmp[i + D]){
			tmp ^= (a[0] << i);
		}
	}
	if(cmp(tmp, X)) ans++;
	ans %= mod;
	ans += mod;
	ans %= mod;
	return ans;
}

int main(){
	input();
	long long ans = solve();
	printf("%lld\n", ans);
	return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#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-1; i < n; i++)
#define INF 2000000000
#define ALL(v) v.begin(), v.end()
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))
#define FILL2D(A,c) fill(A[0], A[0] +sizeof(A)/sizeof(**A),c)
#define FILL3D(A,c) fill(A[0][0], A[0][0] +sizeof(A)/sizeof(***A),c)
//vector<vector<要素の型>> 変数名(要素数1, vector<要素の型>(要素数2, 初期値))
//vector<intなど> 変数名(要素数,初期値)
using namespace std;
typedef long long ll;
int main(){
    ll n,a;
  	a=0;
    cin>>n;
    if (n%2==0){
        n/=2;
        while (n>=5) {
          n/=5;
          a+=n;
          //cout<<"a="<<a<<",n="<<n<<endl;
        }
    }
    cout<<a<<endl;
    
}


Measured memory_bytes: 256
program main
  implicit none
  
  integer :: n,i
  double precision :: x,s = 0.d0,bt = 380000.d0
  character*128 :: u
  
  read(*,*)n
  
  do i = 1, n
    read(*,*)x,u
    if (u == 'JPY') then
      s = s + x
    else
      s = s + x * bt
    end if
  end do
  write(*,*)s
end program main

Measured memory_bytes: 384
N, x = map(int, input().split())
A = list(map(int, input().split()))

B = A[:]
ans = sum(A)

for k in range(1, N):
    B = [min(B[i], A[i - k]) for i in range(N)]
    ans = min(ans, sum(B) + k * x)
print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
  
int main(){
  int n;
  cin >> n;
  int a[n], sum = 0;
  for(int i = 0; i < n; i++) cin >> a[i];
  if(n == 1) sum++;
  for(int i = 0; i < n - 1; i++){
    if(a[i] == a[i+1]) continue;
    if(a[i] < a[i+1]){
      sum++;
      while(i < n - 1 && a[i] <= a[i+1]) i++;
      if(i == n - 2 && a[i] > a[i+1]) sum++;
    }else{
      sum++;
      while(i < n - 1 && a[i] >= a[i+1]) i++;
      if(i == n - 2 && a[i] < a[i+1]) sum++;
    }
  }
  cout << sum << endl;
  return 0;
}

Measured memory_bytes: 640
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        Set<Integer> s = new HashSet<Integer>();
        for(int i = 0; i < n; i++)
        {
            int num = Integer.parseInt(sc.next());
            s.add(num);
        }
        
        System.out.println(s.size());
        sc.close();
    }
}

Measured memory_bytes: 21844
#include <stdio.h>

int main(void){
  int a, b;
  double t;
  int i;
  int sum = 0;

  scanf("%d %d %lf", &a, &b, &t);

  for(i = a; i <= t + 0.5; i += a)  sum += b;
  
  printf("%d", sum);
  
}


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

using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debugF(ostream &os) {os << endl;}
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) { os << " " << H; debugF(os, T...); }
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debugF(cout, __VA_ARGS__)
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); os << (i>0 ? " ":"") << v[i++]); return os;}

// class UnionFind{
//   public:
//   vector<int> par;
//   UnionFind(int n) : par(n,-1) {}

//   int root(int v){
//     if(v == -1) return v;
//     return par[v] < 0 ? v : (par[v] = root(par[v]));
//   }

//   void merge(int u, int v){
//     u = root(u); v = root(v);
//     if(u == -1 || v == -1 || (u == v)) return;
//     if(par[u] >= par[v]) swap(u,v);
//     //link v -> u
//     par[u] += par[v];
//     par[v] = u;
//   }

//   int size(int u){
//     return u == -1 ? 0 : -par[root(u)];
//   }
// };

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, i, j, u, v;
    cin >> n;
    //vvi g(n+1);
    ll ans = 0;
    for(v=1;v<=n;v++){
        ans += ll(v) * (n - v + 1);
    }
    for(i=0;i<n-1;i++){
        cin >> u >> v;
        //g[u].push_back(v);
        //g[v].push_back(u);
        if(u > v) swap(u,v);
        ll eS = ll(u) * (n - v + 1);
        ans -= eS;
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 3660
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
    long long a,b;
    cin>>a>>b;
    long long ans=0;
    while(a<=b)
    {
        ans+=1;
        a=a*2;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <string.h>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cassert>
#include <bitset>

using namespace std;
typedef long long ll;

#ifndef INT_MAX
#  define INT_MAX std::numeric_limits<int>().max()
#endif
#ifndef INT_MIN
#  define INT_MIN std::numeric_limits<int>().min()
#endif
#define LL_MAX std::numeric_limits<ll>().max()
#define LL_MIN std::numeric_limits<ll>().min()
#define INF LL_MAX

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
// 配列の初期化
#define ZERO(p) memset(p, 0, sizeof(p))
// 配列の初期化(-1)
#define MINUS(p) memset(p, 1, sizeof(p))

int N;

int rate[9];

int main()
{
    cin >> N;
    ZERO(rate);
    REP(i, N) {
        int a;
        cin >> a;
        if(a >= 1 && a <= 399) { rate[0]++; }
        else if(a >= 400 && a <= 799) { rate[1]++; }
        else if(a >= 800 && a <= 1199) { rate[2]++; }
        else if(a >= 1200 && a <= 1599) { rate[3]++; }
        else if(a >= 1600 && a <= 1999) { rate[4]++; }
        else if(a >= 2000 && a <= 2399) { rate[5]++; }
        else if(a >= 2400 && a <= 2799) { rate[6]++; }
        else if(a >= 2800 && a <= 3199) { rate[7]++; }
        else { rate[8]++; }
    }
    int exist_rate = 0;
    REP(i, 8) {
        if(rate[i] > 0) { exist_rate++; }
    }
//    if(exist_rate == 8) {
//        printf("8 8\n");
//        return 0;
//    }
//    if( (8 - exist_rate) <= rate[8] ) {
//        printf("%d 8\n", exist_rate);
//    }
    if(exist_rate == 0) {
        printf("1 %d\n", rate[8]);
    } else {
        printf("%d %d\n", exist_rate, exist_rate + rate[8]);
    }

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define mod 1000000007
#define INF 10000000000000001
#define F first
//#define S second
#define LB lower_bound
#define UB upper_bound
#define vc vector
#define vll vector<long long>
#define pll pair<long long,long long>
#define pb push_back
#define all(v) v.begin(),v.end()
#define T ll test;cin>>test; while(test--)
#define rep(i,a,n) for(ll i=a;i<(long long)n;++i)
#define repr(i,n,a) for(ll i=n;i>(long long)a;--i)
#define MAX 100005
#define sz(x) (ll)x.size()
#define ins insert
#define mp make_pair
#define int  long long
typedef long long ll;
typedef long double ld;
using namespace std;
ll dp[2002];
ll solve(ll s)
{
	if(s==0) return dp[0]=1;
	if(dp[s]!=-1) return dp[s];
	ll ans=0;
	for(int i=3;i<=s;i++)
	{
		ans=(ans+solve(s-i))%mod;
	}
	return dp[s]=ans;
}


signed  main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	ll n;
	cin>>n;
	memset(dp,-1,sizeof(dp));
	dp[0]=1;
	dp[1]=dp[2]=0;
	cout<<solve(n)<<endl;
}

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

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

// Returns a vector of cumulative sums.
// Let c_sums be the return value.
// - c_sums[k] is the sum of xs over [0, k).
// - c_sums[r] - c_sums[l] is the sum of xs over [l, r).
template<class T> vector<T> CumulativeSumVector(const vector<T>& xs) {
  int n = xs.size(); vector<T> c_sums(n + 1);
  for (int i = 0; i < n; i++) c_sums[i + 1] = c_sums[i] + xs[i];
  return c_sums;
}

vector<int> Encode(const string& s) {
  vector<int> xs(s.length());
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'A') {
      xs[i] = 1;
    }
  }
  return CumulativeSumVector(xs);
}

int Type(int a, int b) {
  a %= 3;
  b %= 3;
  vector<vector<int>> table = {{2, 1, 0}, {0, 2, 1}, {1, 0, 2}};
  return table[a][b];
}

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

  string s1, s2;
  cin >> s1 >> s2;
  auto cs1 = Encode(s1);
  auto cs2 = Encode(s2);

  int q_count;
  cin >> q_count;
  for (int q = 0; q < q_count; q++) {
    int l1, r1, l2, r2;
    cin >> l1 >> r1 >> l2 >> r2;
    l1--;
    l2--;
    
    int a1 = cs1[r1] - cs1[l1];
    int b1 = r1 - l1 - a1;
    int a2 = cs2[r2] - cs2[l2];
    int b2 = r2 - l2 - a2;
    if (Type(a1, b1) == Type(a2, b2)) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}

Measured memory_bytes: 2120
#include <stdio.h>
int main() {
  long int x, i, y=100;
  scanf("%ld", &x);
  for(long int i=1; 1; i++){
    y *= 1.01;
    if(y>=x){
      printf("%ld\n", i);
      break;
    }
  }
  return 0;
}

Measured memory_bytes: 1736
<?php
  list($s) = explode(" ", trim(fgets(STDIN)));
  list($t) = explode(" ", trim(fgets(STDIN)));

  $slen=strlen($s);
  $tlen=strlen($t);

  $ans="";
  for($i=0; $i<$slen-$tlen+1; $i++){
    $isOk=true;
    for($j=0; $j<$tlen; $j++){
      if($s[$i+$j]=="?" || $t[$j]==$s[$i+$j]){
      }else{
        $isOk=false;
        break;
      }
    }
    if($isOk){
      $sok=substr($s,0,$i);
      $sok=$sok.$t;
      $sok=$sok.substr($s,$i+$tlen);
      $sok=str_replace("?","a",$sok);
      if($ans==""){
        $ans=$sok;
      }else{
        if(strcmp($sok, $ans)<0){
          $ans = $sok;
        }
      }
    }
  }

  if($ans=="") {
    echo "UNRESTORABLE".PHP_EOL;
    exit;
  }
  echo $ans.PHP_EOL;
  

Measured memory_bytes: 16232
#include <iostream>

typedef long long ll;

void print(){ std::cout << std::endl; }
template <typename Head, typename... Tail> void print(Head&& head, Tail&&... tail)
{
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

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

  print(std::min(A, B), std::max(0LL, A+B-N));

  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main() {
  string s;cin>>s;
  vector<int> vec(s.size());
  rep(i,s.size()) {
    vec[i]=s[i]-'0'; 
  }
  int ans=0;
  for (int i=0;i<vec.size()-1;i++) {
    if (vec[i]==vec[i+1]) {
      vec[i+1]=1-vec[i];
      ans++;
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
#define r(i,n) for(int i = 0; i<n; i++)
using namespace std;
 
int main() {
  string str;
  cin >> str;
  if(str>="2019/05/01")cout << "TBD"<< endl;
  else cout << "Heisei" << endl;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i];}cout<<endl;
#define pprint(A,m,n) rep(j,0,m){print(A[j],n);}
const long mod=1e9+7;
const int size=1e3;
const int INF=1e9;
int main(){
	int N;string s;cin>>N>>s;
	string t=" ";
	rep(i,0,N){
		if(t.back()=='('&&s[i]==')'){
			t.pop_back();
			continue;
		}t.push_back(s[i]);
	}
	int a=0,b=0;
	rep(i,1,t.size()){
		if(t[i]==')') a++;
		else b++;
	}
	rep(i,0,a) cout<<'(';
	cout<<s;
	rep(i,0,b) cout<<')';
	cout<<endl;
}

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/02/23  Problem: 074_arc_a / Link: https://abc062.contest.atcoder.jp/tasks/arc074_a?lang=en  ----- */
/* ------問題------

縦 H ブロック、横 W ブロックの板チョコがあります。 すぬけ君は、この板チョコをちょうど 3 つのピースに分割しようとしています。 ただし、各ピースはブロックの境目に沿った長方形でなければなりません。

すぬけ君は、3 つのピースの面積 (ブロック数) をできるだけ均等にしようとしています。 具体的には、3 つのピースの面積の最大値を Smax、最小値を Smin としたとき、Smax−Smin を最小化しようとしています。 Smax−Smin の最小値を求めてください。

-----問題ここまで----- */
/* -----解説等-----

一つ決めるとあとは全部決まるので、分割の仕方を全探索

----解説ここまで---- */

LL H, W;

LL ans = 0LL;

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

	cin >> H >> W;

	ans = LINF;
	FOR(k, 0, 2) {
		swap(H, W);
		FOR(i, 1, W) {
			LL a = i;
			LL b = (W - i) / 2;
			LL c = W - a - b;

			LL Sx = a*H;
			LL Sy = b*H;
			LL Sz = c*H;
			ans = min(ans, max({ Sx, Sy, Sz }) - min({ Sx, Sy, Sz }));

			LL w = W - a;
			LL bb = H / 2;
			LL cc = H - bb;
			Sy = bb*w;
			Sz = cc*w;
			ans = min(ans, max({ Sx, Sy, Sz }) - min({ Sx, Sy, Sz }));

		}
	}
	cout << ans << "\n";

	return 0;
}


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

#define f first
#define s second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define px pair<db, db>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define input(a) for (auto& el : a) cin >> el;
#define output(a) cout << '!'; for (auto& el : a) cout << el << ' '; cout << '\n'; cout.flush();
#define sz(el) int(el.size())
#define sq(a) ((a) * (a))
#define xll __int128
#define xmin(a,b) a = min(a, b)
#define xmax(a,b) a = max(a, b)
#define sun(a) sort(all(a)); a.resize(unique(all(a)) - a.begin());

using namespace std;

const int INF = 1'000'000'017;
const ll LLINF = 1'000'000'000'000'000'017LL;
const ld inf = 1.0 / 0.0;
const ld eps = 1e-9;
const int MOD = 1e9 + 7;

//mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

//uniform_int_distribution<int> uid(l, r); [l, r]
//int x = uid(rng);

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

    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<int> a;
    for (int i = 0; i < n; ++i) {
        a.eb(int(s[i] - '0') - 1);
    }

    if (count(all(a), 1)) {
        for (int& el : a) {
            if (el == 2) {
                el = 0;
            }
        }
    }

    int res = a[0];
    int m = n - 1;
    while (m) {
        res ^= a[m];
        m = ((m - 1) & (n - 1));
    }

    cout << res;
    return 0;
}


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


int main(){
	int n;cin>>n;
	vector<ll> a(n);
	vector<ll> b(n);
	for(int i = 0; i < n; i++) {
		ll A,B;cin>>A>>B;
		a[i] = A;
		b[i] = B;
	}
	sort(ALL(a));
	sort(ALL(b));
	reverse(ALL(b));
	if (n % 2 == 1){
		ll x = a[n / 2];
		ll y = b[n / 2];
		cout << y - x + 1 << endl;
		return 0;
	}
	else{
		ll x = (a[n/2 - 1] + a[n / 2]);
		ll y = (b[n/2 - 1] + b[n / 2]);
		cout << y - x + 1 << endl;
		return 0;
	}
	return 0;
}

Measured memory_bytes: 6420
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <map>
#include <utility>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define MOD 10000000007    //10億　= 10^9になってる
#define juu 100000               //10万 10e5
#define hyaku 1000000            //100万　10e6
#define mINF -922337200085470000 //llのmax-1桁の小さい方
#define pINF 922337203600070000  //llのmax-1桁のデカい方
#define uINF 1844674407300000000 //ullのmax-1桁してる
#define pi 3.1415926535897932384
ll factor[300];
ll memory[300];
/*素朴法で計算
factorは必ずmemset(factor,0,sizeof(factor));
グローバルであるmemoryに個数を書き込む
memory配列の走査にはfactorの要素を使う*/
void primefac(ll num)
{
    int i = 0, kosuu = 0;
    while (*(factor + i) != 0)
    {
        while (num % *(factor + i) == 0)
        {
            num /= *(factor + i);
            memory[*(factor + i)] += 1;
        }
        if (num == 1)
            break;
        i++;
    }
}
/*グローバルにあるfactorに素数書き込む.個数を返すので配列は-1しろforは<*/
int elast(ll number)
{
    ll tmp = 0;
    int flag = 0;
    *factor = 2;
    for (int i = 3; i <= number; i++)
    {
        flag = 0;
        for (int j = 0; j <= tmp; j++)
        {
            if (i % (*(factor + j)) == 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag)
            continue;
        tmp += 1;
        *(factor + tmp) = i;
    }
    return tmp + 2;
}
/*整数をqsortする為だけのもの*/
long long compare(const long long *a, const long long *b)
{
    if (*a < *b)
        return -1;
    else if (*a > *b)
        return 1;
    return 0;
}
/*intで収まる範囲であってくれ*/
ll nCr(int n, int r)
{
    ll val = 1;
    int i;
    if (r == 0)
        return 1;
    for (i = 0; i < n - r; i++)
    {
        val *= (n - i);
        val /= (i + 1);
    }
    return val;
}
/*intで収まる範囲であってくれ*/
ll nPr(int n, int r)
{
    ll val = 1;
    int i;
    for (i = 0; i < r; i++)
    {
        val *= (n - i);
    }
    return val;
}

void chmin(ll &a, ll b)
{
    if (a > b)
        a = b;
}
void chmax(ll &a, ll b)
{
    if (a < b)
        a = b;
}

/*
qsort(array,length,sizeof(int),(int(*)(const int*, const int*))compare);
使う時これをコピペ
*/
void swapmax(ll&a,ll& b){
	ll temp;
	if(b<a)
	{
		temp = b;
		b = a;
		a = temp;
	}
	}
	
int main()
{
	ll A,B	;
    cin >> A;
    cin >> 	B;
    swapmax(A,B);
    cout << (A!=1?1:B!=2?2:3);
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <math.h>
#include <utility>
#include <vector>
#include <map>
#include <unordered_map>
#include <cstdlib>
#include <stack>
#include <queue>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
using ll = long long;
using ld = long double;
// a>b -> a==b;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
// a<b -> a==b;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int main(void)
{
    ll N, W, i, j, w[110], v[110], dp[110][100100];

    cin >> N >> W;

    for(i=0; i<N; i++){
        cin >> w[i] >> v[i];
    }

    for(i=0; i<110; i++){
        for(j=0; j<100100; j++){
            dp[i][j] = INF;
        }
    }

    dp[0][0] = 0;

    for(i=0; i<N; i++){
        for(j=0; j<100100; j++){
            if(j-v[i] >= 0){
                chmin(dp[i+1][j], dp[i][j-v[i]]+w[i]);
            }

            chmin(dp[i+1][j], dp[i][j]);
        }
    }

    ll ans = 0;

    for(j=0; j<100100; j++){
        if(dp[N][j] <= W){
            ans = j;
        }
    }

    cout << ans << endl;

    return 0;
}

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

const int N = 60;
const int M = N * N * N;
typedef long long ll;

std::vector <int> e[N];
ll w[N], v[N], c[N];
ll dp[2][M];
ll first[N], second[N];
int m[N];

struct frac{
	ll x, y;
	ll num;
	
	frac (ll x = 0, ll y = 0, ll num = 0):x(x), y(y), num(num){}
	
	bool operator < (const frac &f)const{
		return x * f.y > y * f.x;
	}
};

frac f[N];

void dfs(int u){
	w[u] = m[u];
	v[u] = 1;
	for (auto v : e[u]){
		dfs(v);
		w[u] += w[v];
		::v[u] += ::v[v];
	}
}

int main(){
	int n, x, d;
	scanf("%d%d%d", &n, &x, &d);
	for (int i = 1, x; i <= n; ++ i){
		scanf("%d", &m[i]);
		if (i >= 2){
			scanf("%d", &x);
			e[x].push_back(i);
		}
	}
	dfs(1);
	for (int i = 1; i <= n; ++ i){
		c[i] = i == 1 ? x / w[i] : std::min((ll) d, x / w[i]);
		//c[i] = std::min((ll) d, x / w[i]);
		first[i] = std::min(c[i], 50ll);
		second[i] = c[i] - first[i];
	}
	memset(dp, -1, sizeof(dp));
	int now = 0;
	dp[now][0] = 0;
	for (int i = 1; i <= n; ++ i){
		for (int j = 0; j < v[i]; ++ j){
			std::deque <ll> queue;
			for (int k = j; k < M; k += v[i]){
				if (dp[now][k] != -1){
					ll pushvalue = dp[now][k] - (k / v[i]) * w[i];
					while (!queue.empty() && pushvalue < queue.back()){
						queue.pop_back();
					}
					queue.push_back(pushvalue);
				}
				if (!queue.empty()){
					dp[now ^ 1][k] = queue.front() + (k / v[i]) * w[i];
				}
				else{
					dp[now ^ 1][k] = -1;
				}
				int delsit = k - first[i] * v[i];
				if (delsit >= 0 && dp[now][delsit] != -1){
					ll delvalue = dp[now][delsit] - (k / v[i] - first[i]) * w[i];
					if (delvalue == queue.front()){
						queue.pop_front();
					}
				}
			}
		}
		now ^= 1;
	}
	for (int i = 1; i <= n; ++ i){
		f[i] = frac (v[i], w[i], second[i]);
	}
	std::sort(f + 1, f + n + 1);
	ll ans = 0;
	for (int i = 0; i < M; ++ i){
		if (dp[now][i] != -1 && dp[now][i] <= x){
			int y = x - dp[now][i], sum = 0;
			for (int j = 1; j <= n; ++ j){
				int num = std::min(f[j].num, y / f[j].y);
				y -= num * f[j].y;
				sum += num * f[j].x;
			}
			ans = std::max(ans, (ll) i + sum);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 3584
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n := nextInt(sc)
	as := make([]int, n)
	for i := 0; i < n; i++ {
		as[i] = nextInt(sc)
	}
	insertionSort(n, as)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return n
}

func printArray(xs []int) {
	fmt.Println(strings.Trim(fmt.Sprint(xs), "[]"))
}

func insertionSort(n int, xs []int) {
	printArray(xs)
	for i := 1; i < n; i++ {
		x := xs[i]
		for j := i - 1; j >= 0; j-- {
			if xs[j] > x {
				tmp := xs[j]
				xs[j] = x
				xs[j+1] = tmp
			}
		}
		printArray(xs)
	}
}



Measured memory_bytes: 1268
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _1_11_B
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Dice = Console.ReadLine().Split(' ');
            int question_p = int.Parse(Console.ReadLine());
            for (int i = 0; i < question_p; i++)
            {
                string question = Console.ReadLine();
                Question1(Dice,question);
                Question2(Dice,question);
                Question3(Dice,question);
                Question4(Dice,question);
                Question5(Dice,question);
                Question6(Dice,question);
            }
           

        }
        static void Question1(string[] Dice,string question)
        {
            string str = Dice[0] + " " + Dice[3] + " " + Dice[5] + " " + Dice[2] + " " + Dice[0];
            if (str.Contains(question))
            {
                Console.WriteLine(Dice[1]);
            }
        }
        static void Question2(string[] Dice,string question)
        {
            string str = Dice[0] + " " + Dice[2] + " " + Dice[5] + " " + Dice[3] + " " + Dice[0];
            if (str.Contains(question))
            {
                Console.WriteLine(Dice[4]);
            }
        }
        static void Question3 (string[] Dice,string question)
        {
            string str = Dice[1] + " " + Dice[2] + " " + Dice[4] + " " + Dice[3] + " " + Dice[1];
            if (str.Contains(question))
            {
                Console.WriteLine(Dice[0]);
            }
        }
        static void Question4 (string[] Dice,string question)
        {
            string str = Dice[1] + " " + Dice[3] + " " + Dice[4] + " " + Dice[2] + " " + Dice[1];
            if (str.Contains(question))
            {
                Console.WriteLine(Dice[5]);
            }
        }
        static void Question5 (string[] Dice,string question)
        {
            string str = Dice[0] + " " + Dice[1] + " " + Dice[5] + " " + Dice[4] + " " + Dice[0];
            if (str.Contains(question))
            {
                Console.WriteLine(Dice[2]);
            }
        }
        static void Question6 (string[] Dice,string question)
        {
            string str = Dice[0] + " " + Dice[4] + " " + Dice[5] + " " + Dice[1] + " " + Dice[0];
            if (str.Contains(question))
            {
                Console.WriteLine(Dice[3]);
            }
        }
    }
}



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

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

    ll i,j,k,l,m,n,w,x,y,z,p,q,tmp,sum=0;
    ll f=0;
    string str;
    cin>>str;
  rep(i,4) if(str[i]=='2') sum++;
  cout<<sum<<endl;
    return 0;
}


Measured memory_bytes: 3564
// ! 挿入ソート

// ==========
// 入力例
// ==========
// 6
// 5 2 4 6 1 3

// ==========
// 出力例
// ==========
// 5 2 4 6 1 3
// 2 5 4 6 1 3
// 2 4 5 6 1 3
// 2 4 5 6 1 3
// 1 2 4 5 6 3
// 1 2 3 4 5 6
#include <iostream>
using namespace std;

int main()
{
  int n;

  cin >> n;

  int R[n];

  for (int i = 0; i < n; i++)
  {
    cin >> R[i];
  }
  for (int k = 0; k < n; k++)
  {
    cout << R[k];
    if (k == n - 1)
    {
      cout << endl;
    }
    else
    {
      cout << " ";
    }
  }

  int j, value;

  for (int k = 0; k < n - 1; k++)
  {
    j = k + 1;
    value = R[j];
    while (j > 0 && value < R[j - 1])
    {
      R[j] = R[j - 1];
      j--;
    }
    R[j] = value;
    for (int i = 0; i < n; i++)
    {
      cout << R[i];
      if (i == n - 1)
      {
        cout << endl;
      }
      else
      {
        cout << ' ';
      }
    }
  }

  return 0;
}


Measured memory_bytes: 3108
#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define LC(a) (a<<1)
#define RC(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name)  freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FOR(i,start,end) for(int i=start;i<=end;i++)  
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=1e3+5;

int a[N];

int main(){
    int n,ma=1,mi=1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]<a[mi]) 
			mi=i;
        if(a[i]>a[ma]) 
			ma=i;
    }
	printf("%d\n",2*n-1);
    if(abs(a[ma])>=abs(a[mi])){
        for(int i=1;i<=n;i++){
            printf("%d %d\n",ma,i);
        }
        for(int i=2;i<=n;i++){
            printf("%d %d\n",i-1,i);
        }
    }
    else{
        for(int i=1;i<=n;i++){
            printf("%d %d\n",mi,i);
        }
        for(int i=n-1;i>=1;i--){
            printf("%d %d\n",i+1,i);
        }
    }
	return 0;
}

Measured memory_bytes: 256
#include <iostream>

#define lo long long
using namespace std;

lo a,b,c;
int main()
{
    cin>>a>>b>>c;

    if (a==b && b==c)
    {
        if (a%2==0) cout<<-1<<endl;
        else cout<<0<<endl;
        return 0;
    }

    lo ans=0;
    while(a%2==0 && b%2==0 && c%2==0)
    {
        lo d,e,f;
        d=(a+b)/2;
        e=(b+c)/2;
        f=(c+a)/2;
        a=d; b=e; c=f;
        ans++;
    }

    cout<<ans<<endl;

    return 0;
}


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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def topological_sort(N: int, edges: list) -> list:
    """ トポロジカルソート(頂点数、辺集合(DAG, 0-indexed)) """

    # ここからトポロジカルソート準備
    incnts = [0] * N
    outnodes = [[] for i in range(N)]
    for i in range(len(edges)):
        # 流入するノード数
        incnts[edges[i][1]] += 1
        # 流出先ノードのリスト
        outnodes[edges[i][0]].append(edges[i][1])
    # 流入ノード数が0であるノードのセットS
    S = set()
    for i in range(N):
        if incnts[i] == 0:
            S.add(i)
            # 流入ノード0のノードは始点に出来るので、最長距離を1にしておく
            dist[i] = 1

    # ここからトポロジカルソート
    L = []
    # 暫定セットが空になるまでループ
    while S:
        # 暫定セットから結果リストへ1つ入れる
        L.append(S.pop())
        # 確定させたノードから流出するノードでループ
        for node in outnodes[L[-1]]:
            # 流入ノード数を1減らす
            incnts[node] -= 1
            # 流入ノードが0なら暫定セットへ
            if incnts[node] == 0:
                S.add(node)
    # 閉路があって正しくソート出来なかった場合
    if len(L) != N:
        return []
    # ソートされた頂点のリストを返却
    return L

N = INT()

# 各試合を一意な値とするためのマッピング
mapping = list2d(N, N, -1)
cnt = 0
for i in range(N):
    for j in range(i+1, N):
        mapping[i][j] = cnt
        cnt += 1

A = []
for i in range(N):
    tmp = []
    for a in LIST():
        a -= 1
        a, b = min(a, i), max(a, i)
        # 各試合に一意な値を振って頂点とする
        tmp.append(mapping[a][b])
    A.append(tmp)

# 試合数で作ったグラフの辺集合と隣接リスト
MAXV = N*(N-1)//2
edges = []
nodes = [[] for i in range(MAXV)]
for i in range(N):
    for j in range(1, N-1):
        edges.append((A[i][j-1], A[i][j]))
        nodes[A[i][j-1]].append(A[i][j])

# トポロジカルソート(この時、始点となる頂点は初期値の距離1にして、閉路の検出も行う)
dist = [-1] * MAXV
res = topological_sort(MAXV, edges)
if not len(res):
    print(-1)
    exit()

# 依存性に従って、最長パスを更新していく
for u in res:
    for v in nodes[u]:
        dist[v] = max(dist[v], dist[u]+1)

print(max(dist))


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

const int MAX = 100000;
int N, n;
vector<int> graph[MAX];
int label[MAX];

int dfs(int node){
	label[node] = -1;
	int used = 0, more = 0;
	for (int i = 0; i < graph[node].size(); ++i){
		int next = graph[node][i];
		if (!label[next]){
			int next_used = dfs(next);
			more |= used & next_used;
			used |= next_used;
		}
	}

	more |= more >> 1;
	more |= more >> 2;
	more |= more >> 4;
	more |= more >> 8;
	more |= more >> 16;
	++more;

	int possible = (~used) & (-more);
	label[node] = possible & (-possible);

	used |= label[node];
	used &= -label[node];
	return used;
}

int main(){
	scanf("%d", &N);
	for (int i = 1; i < N; ++i){
		int x, y;
		scanf("%d%d", &x, &y);
		x--,y--;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	dfs(0);
	int maximum = *max_element(label, label+N), result = 0;
	while (maximum){
		++result;
		maximum >>= 1;
	}
	printf("%d\n", result-1);

	return 0;
}

Measured memory_bytes: 10624
n,a,b,c, = map(int,input().split())
l = list(int(input()) for i in range(n))
ans = 10 ** 6
for i in range(4 ** n):
    m = i
    j = 0
    x,xp,y,yp,z,zp = 0,0,0,0,0,0
    while m > 0:
        p = m % 4
        if p == 1:
            x += l[j]
            xp += 1
        elif p == 2:
            y += l[j]
            yp += 1
        elif p == 3:
            z += l[j]
            zp += 1
        else:
            pass
        m = m // 4
        j += 1
    if xp != 0 and yp != 0 and zp != 0:
        t = abs(a-x) + abs(b-y) + abs(c-z) + ((xp-1)*10) + ((yp-1)*10) + ((zp-1)*10)
        ans = min(ans,t)
print(ans)

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

using namespace std;
using ll = long long;

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

int main() {
  int h, w;
  cin >> h >> w;
  int s[h][w];
  rep(i, h) rep(j, w) {
    char c;
    cin >> c;
    s[i][j] = (c == '#');
  }

  int score[h][w];
  rep(i, h) rep(j, w) score[i][j] = 0;

  // left
  rep(i, h) {
    int l = -1;
    rep(j, w) {
      if (s[i][j])
        l = -1;
      else {
        ++l;
        score[i][j] += l;
      }
    }
  }

  // right
  rep(i, h) {
    int l = -1;
    rep(j, w) {
      if (s[i][w - j - 1])
        l = -1;
      else {
        ++l;
        score[i][w - j - 1] += l;
      }
    }
  }

  // up
  rep(j, w) {
    int l = -1;
    rep(i, h) {
      if (s[i][j])
        l = -1;
      else {
        ++l;
        score[i][j] += l;
      }
    }
  }

  // down
  rep(j, w) {
    int l = -1;
    rep(i, h) {
      if (s[h - i - 1][j])
        l = -1;
      else {
        ++l;
        score[h - i - 1][j] += l;
      }
    }
  }

  int ans = 0;
  rep(i, h) {
    rep(j, w) {
      if (s[i][j]) continue;
      ans = max(ans, score[i][j] + 1);
    }
  }

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


Measured memory_bytes: 34748
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define V_SUM_MAX 100000
#define N_MAX 100
#define W_MAX 10000000000

ll dp[V_SUM_MAX + 1][N_MAX]; 
bool v[V_SUM_MAX + 1][N_MAX]; 
  
// Function to solve the recurrence relation 
ll solveDp(int r, int i, ll* w, int* val, ll n) 
{ 
    // Base cases 
    if (r <= 0) 
        return 0; 
    if (i == n) 
        return W_MAX; 
    if (v[r][i]) 
        return dp[r][i]; 
  
    // Marking state as solved 
    v[r][i] = 1; 
  
    // Recurrence relation 
    dp[r][i] 
        = min(solveDp(r, i + 1, w, val, n), 
              w[i] + solveDp(r - val[i], 
                             i + 1, w, val, n)); 
    return dp[r][i]; 
} 
  
// Function to return the maximum weight 
int maxWeight(ll* w, int* val, ll n, ll c) 
{ 
  
    // Iterating through all possible values 
    // to find the the largest value that can 
    // be represented by the given weights 
    for (int i = V_SUM_MAX; i >= 0; i--) { 
        if (solveDp(i, 0, w, val, n) <= c) { 
            return i; 
        } 
    } 
    return 0; 
} 
  
int main()
{
	ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    ll n,w;
    cin>>n>>w;
    int val[n];
    ll wt[n];
    for(int i=0;i<n;i++)
    cin>>wt[i]>>val[i];
    cout<<maxWeight(wt,val,n,w);
}
    

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

using namespace std;

//#define FILE_IO

int N;
int sum[5][1005];

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    scanf("%d", &N);
    for(int j = 1; j <= 2; j++)
        for(int i = 1; i <= N; i++)
        {
            int x;
            scanf("%d", &x);
            sum[j][i] = sum[j][i - 1] + x;
        }

    int ans = 0;
    for(int i = 1; i <= N; i++)
        ans = max(ans, sum[1][i] + sum[2][N] - sum[2][i - 1]);
    printf("%d\n", ans);

    return 0;
}


Measured memory_bytes: 256
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N,M=rl()
X=rl()
X.sort()
d = [X[i+1]-X[i] for i in range(M-1)]
d.sort()
to = M - N
print(sum(d[0:to]) if to>0 else 0)


Measured memory_bytes: 13968
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

char s[3][3];

int main(){
	for(int i=0;i<3;i++){
		scanf("%s",&s[i]);
	}
	printf("%c",s[0][0]);
	printf("%c",s[1][1]);
	printf("%c",s[2][2]);
	
	return 0;
}



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


void solve(long long A, long long B){
    cout<<A * B<<endl;
}

int main(){
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    solve(A, B);
    return 0;
}


Measured memory_bytes: 3764
import sys
import numpy as np
import numba
from numba import njit
i8 = numba.int64

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
X = np.array(list(read().rstrip()), np.int64) - ord('0')

@njit((i8, ), cache=True)
def popcount(n):
    ret = 0
    while n:
        ret += n & 1
        n >>= 1
    return ret


@njit((i8, ), cache=True)
def f(n):
    t = 0
    while n:
        t += 1
        n %= popcount(n)
    return t

@njit(cache=True)
def main(X):

    popX = X.sum()
    MOD1 = popX + 1
    MOD2 = popX - 1
    if popX == 1:
        MOD2 = 1
    pow1 = np.ones_like(X)
    pow2 = np.ones_like(X)
    for n in range(1, len(X)):
        pow1[n] = 2 * pow1[n - 1] % MOD1
        pow2[n] = 2 * pow2[n - 1] % MOD2

    X1, X2 = 0, 0
    for x in X:
        X1 = (2 * X1 + x) % MOD1
        X2 = (2 * X2 + x) % MOD2

    ans = np.empty(N, np.int64)
    for i in range(N):
        x = X[N - 1 - i]
        if x == 0:
            nxt = (X1 + pow1[i]) % (popX + 1)
            ans[i] = 1 + f(nxt)
        elif x == 1:
            if popX == 1:
                ans[i] = 0
            else:
                nxt = (X2 - pow2[i]) % (popX - 1)
                ans[i] = 1 + f(nxt)
    return ans[::-1]

ans = main(X)
print('\n'.join(map(str, ans.tolist())))

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

#define mod 1000000007

using namespace std;
 
int main(void){
	
	int N, odd = 0, even = 0;
	bool ans = false;
	
	cin >> N;
	
	long long int A[N];
	
	for(int i=0; i<N; i++)
		cin >> A[i];
	
	for(int i=0; i<N; i++){
		if(A[i]%2 == 0)
			even++;
		else
			odd++;
	}
	
	if(N%2 == 0){
		if(even%2 == 0)
			ans = true;
	}else{
		if(even%2 != 0)
			ans = true;
	}
	if(ans)
		cout << "YES";
	else
		cout << "NO";
	
	return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  ll a, b, c;
  cin >> a >> b >> c;
  c -= (a + b);
  ll a1 = 4 * a * b, a2 = c * c;
  if (c >= 0 && a1 < a2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Measured memory_bytes: 3588
#include<stdio.h>
#include<stdlib.h>
const short square[] = {0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,729,784,841,900,961,1024,1089,1156,1225,1296,1369,1444,1521,1600,1681,1764,1849,1936,2025,2116,2209,2304,2401,2500,2601,2704,2809,2916,3025,3136,3249,3364,3481,3600,3721,3844,3969,4096,4225,4356,4489,4624,4761,4900,5041,5184,5329,5476,5625,5776,5929,6084,6241,6400,6561,6724,6889,7056,7225,7396,7569,7744,7921,8100,8281,8464,8649,8836,9025,9216,9409,9604,9801,10000,10201,10404,10609,10816,11025,11236,11449,11664,11881,12100,12321,12544,12769,12996,13225,13456,13689,13924,14161,14400,14641,14884,15129,15376,15625,15876,16129};

int compare_short(const void *a, const void *b){
    return *(short*)a - *(short*)b;
}

int n, a, i, j;
int d, k;
short z;
short x[10][10];
void *f;

int main(void){
	scanf("%d%d", &n, &d);
	for(i=0; i<n; i++){
		for(k=0; k<d; k++){
			scanf("%hd", &x[i][k]);
		}
	}
	for(i=0; i<n; i++){
		for(j=i+1; j<n; j++){
			z=0;
			for(k=0; k<d; k++){
				z += (x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
			}
			f=bsearch(&z, square, sizeof(square)/sizeof(short), sizeof(short), compare_short);
			if(f!=NULL){a++;}
		}
	}
	printf("%d\n", a);
}


Measured memory_bytes: 128