akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll MOD=(ll)1e9+7;
const ll INF=(ll)1e18+10;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }


int main() {
    int Q; cin>>Q;
    ll lsum=0,hsum=0,bsum=0;
    priority_queue<int> low;
    priority_queue<int,vector<int>,greater<>> high;

    int q,a,b;
    for (int i = 0; i < Q; ++i) {
        cin>>q;
        if(q==1) {
            cin >> a >> b;
            bsum += (ll) b;
            if (low.size() <= high.size()) { //lowが増える
                if (low.empty()||a <= low.top()) { //aはlowへ
                    low.push(a);
                    lsum += (ll) a;
                } else { //aはhighへ
                    high.push(a);
                    hsum += (ll) a;
                    int tmp = high.top();
                    hsum -= (ll) tmp;
                    high.pop();
                    low.push(tmp);
                    lsum += (ll) tmp;
                }
            } else { //highが増える
                if (a > low.top()) {
                    high.push(a);
                    hsum += (ll) a;
                } else {
                    low.push(a);
                    lsum += (ll) a;
                    int tmp = low.top();
                    lsum -= (ll) tmp;
                    low.pop();
                    high.push(tmp);
                    hsum += (ll) tmp;
                }
            }
        }else{
            ll x=(ll)low.top();
            ll calc=(x*low.size()-lsum)+(hsum-x*high.size())+bsum;
            cout<<x<<" "<<calc<<endl;
        }
    }

    return 0;
}

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

int main(void){
    string S;
    cin>>S;
    bool finish = false;
    S.erase(S.size()-1);
    while (!finish) {
        if (S.size()%2!=0) {
            S.erase(S.size()-1);
            continue;
        }
        else {
            string a,b;
            a = S.substr(0,S.size()/2);
            b = S.substr(S.size()/2,S.size()/2);
            if (a == b) {
                finish = true;
                break;
            }
            else {
                S.erase(S.size()-1);
                continue;
            }
        }
    }
    cout<<S.size()<<endl;
    return 0;
}

Measured memory_bytes: 256
//  ©   ___MRX___

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

#define ll long long int

int main()
{
    int i, n, x, flag = 1, ans = 0, count;
    string s;
    vector<int> checker1(26);
    vector<int> checker2(26);

    cin >> n >> s;
    x = n;

    while(x > 1) {
        for(i = 0; i < 26; i++) {
            checker1[i] = 0;
            checker2[i] = 0;
        }
        count = 0;

        for(i = 0; i < flag; i++) {
            if(checker1[s[i] - 'a'] == 0) {
                checker1[s[i] - 'a'] = 1;
            }
        }
        for(i = flag; i < n; i++) {
            if(checker2[s[i] - 'a'] == 0) {
                checker2[s[i] - 'a'] = 1;
            }
        }

        for(i = 0; i < 26; i++) {
            if(checker1[i] && checker2[i])
                count++;
        }

        flag++;
        x--;
        ans = max(ans, count);
    }

    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 3624
def solve(n, p, s):
    res = 0
    if p == 2 or p == 5:
        for i in range(n):
            if int(s[i]) % p == 0:
                res += i + 1
        return res

    mod_count = [0] * p
    temp = 0
    pow_10_i_p = [0] * n
    pow_10_i_p[0] = 1
    for i in range(1, n):
        pow_10_i_p[i] = pow_10_i_p[i - 1] * 10 % p
    for i in range(n):
        temp = (temp + pow_10_i_p[i] * int(s[-(i + 1)])) % p
        mod_count[temp] += 1
    res += mod_count[0]
    for i in range(p):
        res += mod_count[i] * (mod_count[i] - 1) // 2
    return res


def main():
    n, p = map(int, input().split())
    s = input()
    res = solve(n, p, s)
    print(res)


def test():
    assert solve(4, 3, "3543") == 6
    assert solve(4, 2, "2020") == 10
    assert solve(20, 11, "33883322005544116655") == 68


if __name__ == "__main__":
    test()
    main()


Measured memory_bytes: 13032
N, L = map(int, input().split())

if L-1 >= 0:
  i = 1
elif L-1 >= -N:
  i = -(L-1)
else:
  i = N

A = (N-1)*(L-1) + sum([n for n in range(1, N+1)]) - i
print(A)

Measured memory_bytes: 9144
#include <stdio.h>

int main(void) {

	int n, min, max, num;
	long long sum;
	int i;
	
	i = 0;
	
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &num);
		if (i == 0){
			min = num;
			max = num;
			sum = num;
		}
		else {
			if (num < min) min = num;
			if (num > max) max = num;
			sum += num;
		}
	}
	
	
	
	printf("%d %d %lld\n", min, max, sum);
	
	return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll mod = 10e9+7;

#define vec vector<int>
#define vecll vector<ll>
#define vpair vector<pair<int, int>>
#define vpairll vector<pair<ll, ll>>

#define rep(i,n)   for(int i=(int)0; i<(int)n; i++)
#define REP(i,m,n) for(int i=(int)m; i<(int)n; i++)
#define all(var)     (var).begin(), (var).end()
#define prt(var)     cout << var << endl
#define prtd(n, var)  cout << fixed << setprecision(n) << var << endl
#define prt2d(n, var1, var2) cout << fixed << setprecision(n) << var1 << " " << var2 << endl

//----------------------------------------------------------------

int main(void) {
  double w, h, x, y;
  cin >> w >> h >> x >> y;

  double mx=w/2.0, my=h/2.0;
  prt2d(1, w*h/2.0, (x==mx && y==my));
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
	int n;
	int debt = 100000;
	cin >> n;
	for(int i=0;i<n;i++){
		debt *= 1.05;
		if( debt % 1000 != 0){
			 debt += 1000;
			 debt -= debt %1000;
		}
		
	}
	
	cout << debt << endl;
	
	return 0;
}

Measured memory_bytes: 1160
import java.util.*;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class Main {
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		int n = parseInt(sc.next());
		int[] v = new int[n];
		for(i=0;i<n;i++) {
			v[i] = parseInt(sc.next());
		}
		sc.close();
		Arrays.sort(v);
		double ans = (double)v[0];
		for(i=1;i<n;i++) {
			ans = (ans + (double)v[i])/2.0;
		}
		out.println(ans);
	}
}


Measured memory_bytes: 21844
import kotlin.math.*

fun main() {
    val (a, b, h, m) = readLine()!!.split(" ").map{ it.toInt() }
    var deg = abs((h*30+m*0.5) - (m*6))
    if(deg >= 180) deg = 360 - deg
    println(sqrt(a*a + b*b - 2*a*b*cos(deg*PI/180)))
}

Measured memory_bytes: 36656
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;



class Program
{
    public const int INF = 1 << 25;

    static void Main()
    {
		int n = int.Parse(Console.ReadLine());
		for (int i = 0; i < n; i++) {
			string[] input = Console.ReadLine().Split(' ');
			int[] a = new int[3];
			for (int j = 0; j < 3; j++) {
				a[j] = int.Parse(input[j]);
			}
			Array.Sort(a);
			if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2]) {
				Console.WriteLine("YES");
			}
			else {
				Console.WriteLine("NO");
			}
		}
	}
}

Measured memory_bytes: 5552
import math
a,b,x=(int(x) for x in input().split())
if x<a*a*b/2:
    print(math.atan(a*b*b/2/x)*180/math.pi)
else:
    print(math.atan(2*(a*a*b-x)/a/a/a)*180/math.pi)

Measured memory_bytes: 3060
#include <cstdio>

int N;
int a[100010];
int cnt[100010];
int res;

void solve()
{
	int i;
	for(i=0; i<N; i++){
		if(a[i]>=100005)
			res ++;
		else{
			cnt[a[i]] ++;
		}
	}
	for(i=0; i<100005; i++){
		if(cnt[i] < i)
			res += cnt[i];
		else{
			res += cnt[i]-i;
		}
	}
	printf("%d\n", res);
}

int main()
{
	int i;
	scanf("%d", &N);
	for(i=0; i<N; i++){
		scanf("%d", &a[i]);
	}
	solve();
	return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int>mp;
int main()
{
	int i,j;
	int n;
	mp["AC"] = 0;
	mp["WA"] = 0;
	mp["TLE"] = 0;
	mp["RE"] = 0;
	scanf("%d",&n);
	while(n--)
	{
		string s;
		cin>>s;
		mp[s]++;
	}
	cout<<"AC x "<<mp["AC"]<<"\n";
	cout<<"WA x "<<mp["WA"]<<"\n";
	cout<<"TLE x "<<mp["TLE"]<<"\n";
	cout<<"RE x "<<mp["RE"]<<"\n";


return 0;
}


Measured memory_bytes: 3656
import java.util.*;
public class Main {
	public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int M = sc.nextInt();
        int sum = 0;

        for(int i = 0; i < M; i++) {
        	sum += sc.nextInt();
        }

        if(sum <= N) {
        	System.out.println(N - sum);
        } else {
        	System.out.println("-1");
        }
    }
}

Measured memory_bytes: 37840
N, K = map(int, input().split())
 
if K % 2 == 1:
  a = N // K
  print(a**3)
else:
  a = N // K
  if K*a + K//2 <= N:
    b = a + 1
  else:
    b = a
  print(a**3 + b**3)

Measured memory_bytes: 9068
#include<stdio.h>

int main(void){
  int D,L;
  int jump=0;
  scanf("%d %d",&D,&L);
  while(D>0){
    if(D>=L){ D-=L; jump++; }
    else{ jump+=D; break; }
  }
  printf("%d\n",jump);
  return 0;
}



Measured memory_bytes: 2112
//#pragma GCC optimize ("-O3","unroll-loops")
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<list>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<'\n';
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()
//#define double long double

using namespace std;

template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int INT_INF = 1e9;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int>P;
const int MAX = 200020;

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;
}
ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}

int dp[404][404];
ll cum[404][404];
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, C; cin >> N >> C;
	vector<int>A(N), B(N);
	REP(i, N) {
		cin >> A[i];
	}
	REP(i, N) {
		cin >> B[i];
	}

	REP(i, 401) {
		REP(k, C + 1) {
			cum[i + 1][k] = bin_pow(i + 1, k);
		}
	}
	REP(k, C + 1) {
		REP(i, 401) {
			cum[i + 1][k] += cum[i][k];
			cum[i + 1][k] %= MOD;
		}
	}
	//FOR(i, 0, N + 1)dp[i][0] = 1;
	dp[0][0] = 1;
	for (int i = 1; i <= N; i++) {
		for (int k = 0; k <= C; k++) {
			for (int m = 0; m <= k; m++) {
				dp[i][k] = add(dp[i][k], mult(dp[i - 1][k-m] , sub(cum[B[i-1]][m] , cum[A[i-1] - 1][m])));
			}
		}
	}
	/*REP(i, N+1) {
		REP(j, C+1) {
			pe(dp[i][j]);
		}cout << endl;
	}*/
	print(dp[N][C]);
}

Measured memory_bytes: 4352
N, T = map(int, input().split())
t = list(map(int, input().split()))
end_time = T + t[0]
sum_time = T
for i in range(1, N):
    sum_time += T - max(0, end_time - t[i])
    end_time = T + t[i]
print(sum_time)

Measured memory_bytes: 26832
// ABC170B - 

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
	int X, Y;
	cin >> X >> Y;

	int a, b;

	if ((Y - 2 * X)% 2 != 0) {
		cout << "No" << endl;
		return 0;
	}

	b = (Y - 2 * X)/2;
	a = X - b;
	if ((a < 0)||(X < a)) {
		cout << "No" << endl;
		return 0;
	}
	if ((b < 0)||(X < b)) {
		cout << "No" << endl;
		return 0;
	}

	cout << "Yes" << endl;

	return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define ft first
#define sd second
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;
typedef pair<long long, long long> pll;
// scanf("%d %d %d %d", &w, &b, &d, &s); %lld
// printf("%d\n", points);
// const double PI = acos(-1);

bool isPalindrome(string s){
    //cout << s << endl;
    int n = s.size()/2;
    for(int i = 0; i<n; i++){
        if(s[i]!=s[s.size()-1-i])
            return false;
    }
    return true;
}


int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    #else
        fastIO;
    #endif
    string s;
    bool a, b, c;
    cin >> s;
    a = isPalindrome(s);
    b = isPalindrome(s.substr(0, (s.size()-1)/2));
    c = isPalindrome(s.substr(((s.size()+3)/2-1), s.size()));
    //cout << a << " " << b << " " << c << endl;
    if(a && b && c)
        cout << "Yes" << endl;
    else cout << "No" << endl;

        return 0;

}

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

int main(){
    int d;
    cin >> d;
    vector<int> c(26);
    vector<vector<int>> s(d, vector<int>(26));
    vector<int> t(d);
    for(auto &&n : c) cin >> n;
    for(auto &&n1 : s) for(auto &&n2 : n1) cin >> n2;
    for(auto &&n : t){
        cin >> n;
        --n;
    }

    vector<int> l(26);

    int v = 0;

    rep(i, d){
        v += s[i][t[i]];
        l[t[i]] = -1;
        rep(j, 26) v -= c[j] * (++l[j]);
        cout(v);
    }
}


Measured memory_bytes: 3680
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define MOD (1000000007)
#define vi vector<int>
#define vii vector<vi>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

const ll INF = 1LL << 60;

int main(){
  int n,k; cin >> n >> k;
  vi a(n);
  rep(i,n) cin >> a[i];
  std::cout << (n+k-3)/(k-1) << '\n';
}


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

int main(){
    string S;
    cin >> S;
    int zero,one;
    zero = 0;
    one = 0;
    for(int i = 0; i< S.length();i++){
        if(S[i] == '0') zero += 1;
        else one += 1;
    }
    cout << min(one,zero) * 2 << endl;
}

Measured memory_bytes: 512
N=int(input())
A_list=[int(thing) for thing in input().split(" ")]

Suu=sum(A_list)
S=Suu*0.5
import sys

sys.setrecursionlimit(2000)
def my_bisect(l:list,left_true=False):
    def bis(begin:int,end:int):
        # print("{}:{}".format(begin,end))
        if end-begin<=1:
            if begin==0:
                if A_list[0]>S:
                    return 0
                else:return 1
            # 
            # print(sum(A_list[0:begin+1]))
            # print(sum(A_list[0:end+1]))
            if begin==0:
                B=A_list[begin+1]
            else:
                B=sum(A_list[0:begin+1])
            if B>S==True:
                return begin
            else:
                return end

        v=int((end+begin)/2//1)
        # print("v="+str(v))
        res=sum(A_list[0:v+1])>S
        # print(sum(A_list[0:v+1]))
        # 
        # print(res)
        if not res:
            return bis(v,end)
        elif  res:
            return bis(begin,v)
        else:
            print("what")
    return bis(0,len(l)-1)

m=my_bisect(A_list)

# print(m)
if m==0:
    print(A_list[0])
else:
    if m==1:
        nagasa=A_list[0]
    else:
        nagasa=sum(A_list[:m])
    print(min(abs((nagasa+A_list[m])*2-Suu),abs(nagasa*2-Suu)))

Measured memory_bytes: 26060
#include "bits/stdc++.h"
using namespace std;

int main(){
    int n;
    cin >> n;
    int p[n];
    for(int i=0;i<n;i++) cin >> p[i];

    int a=0;

    for(int i=0;i<=n-3;i++){
        if((p[i]<=p[i+1] && p[i+1]<=p[i+2] ) ||  (p[i+1]<=p[i] && p[i+2]<=p[i+1]))a++;
    }
    cout << a << endl;
}

Measured memory_bytes: 256
from collections import Counter 

N, K, Q = [int(s) for s in input().split(' ')]
A = [int(input()) - 1 for s in range(Q)]

c = Counter(A)

sum = sum(c.values())

for i in range(N):
  
  if sum - c[i] < K:
    print('Yes')
  else:
    print('No')

Measured memory_bytes: 11900
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
int main() {
	int N,ans;
	vector<int> t,v;
	cin >> N;

	for (int i = 0; i < N+1; i++)
	{
		int te;
		cin >> te;
		t.push_back(te);
	}

	sort(t.begin(), t.end());

	for (int i = 0; i < N; i++)
	{
		v.push_back((abs(t[i+1] - t[i])));
	}
	/*
	for (int i = 0; i < N; i++)
	{
		cout << v.at(i) << endl;
	}
	*/

	sort(v.begin(), v.end());
	ans = *v.begin();

	while (ans >0) {
		bool can = true;
		for (int i = 0; i < N; i++)
		{
			if (v.at(i) % ans != 0) {
				can = false;
				break;
			}
		}
		if (can) {
			break;
		}
		else {
			ans--;
		}
	}
	
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 1400
#include <iostream>
#include <string>
using namespace std;
int main(void){
    string s,t;
    int x=0,i;
    cin>>s;
    t="CODEFESTIVAL2016";
    for(i=0;i<16;i++){
        if(s[i]!=t[i]) x++;
    }
    cout<<x<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char c = sc.next().charAt(0);
        Map<Character, Character> map = new HashMap<>();
        map.put('A', 'T');
        map.put('T', 'A');
        map.put('G', 'C');
        map.put('C', 'G');

        System.out.println(map.get(c));
    }
}

Measured memory_bytes: 22612
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;


int main() {
	int n;
	cin >> n;
	vector<int> v(n, 0);
	
	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}
	unordered_map<int, int> um;
	for (int i = 0; i < n; i += 2) {
		um[v[i]]++;
	}
	int c1 = n / 2 + n % 2;
	set<pair<int, int>> s1, s2;
	for (auto p : um) {
		s1.insert(make_pair(c1 - p.second, p.first));
	}
	um.clear();
	int c2 = n / 2;
	for (int i = 1; i < n; i += 2) {
		um[v[i]]++;
	}
	for (auto p : um) {
		s2.insert(make_pair(c2 - p.second, p.first));
	}

	int res = min(s1.begin()->first + c2, s2.begin()->first + c1);
	c1 = 0, c2 = 0;
	for (auto it1 = s1.begin(); it1 != s1.end() && c1 < 2; it1++ ) {
		c2 = 0;
		for (auto it2 = s2.begin(); it2 != s2.end() && c2 < 2; it2++) {
			if (it1->second != it2->second) {
				res = min(res, it1->first + it2->first);
			}
			c2++;
		}
		c1++;
	}
	cout << res << endl;
}


Measured memory_bytes: 10280
#include <iostream>

using namespace std;

int main() {
    int n;
    int a[1000000];
    bool dp[1000000];

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    int index = 1, counter = 0;
    while (!dp[index]) {
        dp[index] = true;
        index = a[index];
        counter++;

        if (index == 2) {
            cout << counter << endl;
            return 0;
        }
    }

    cout << -1 << endl;
}

Measured memory_bytes: 4140
using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = int.Parse(Console.ReadLine());
        string[] str = Console.ReadLine().Split(' ');
        long[] x = new long[N];
        for(int i=0;i<N;i++){
            x[i] = int.Parse(str[i]);
        }
        long count = 1;
        int c = 0;
        for(int i=0;i<N;i++){
            if(x[i] >= c*2 + 1){
                c++;
            }
            else{
                count = count * (c+1) % Define.mod;
            }
        }
        Fact F = new Fact(N);
        count = count * F.GetFact(c) % Define.mod;
        sb.Append(count+"\n");
    }
}
public static class Define{
    public const long mod = 1000000007;
}
class Fact{
    public long[] f;
    public long[] rf;
    public Fact(int N){
        f = new long[N+1];
        rf = new long[N+1];
        for(int i=0;i<N+1;i++){
            if(i == 0){
                f[i] = 1;
            }
            else{
                f[i] = (f[i-1]*i)%Define.mod;
            }
        }
        for(int i=N;i>=0;i--){
            if(i == N){
                rf[i] = pow(f[N],Define.mod-2);
            }
            else{
                rf[i] = rf[i+1]*(i+1)%Define.mod;
            }
        }
    }
    public long pow(long N,long K){
        if(K == 0){
            return 1;
        }
        else if(K % 2 == 0){
            long t = pow(N,K/2);
            return t*t%Define.mod;
        }
        else{
            return N*pow(N,K-1)%Define.mod;
        }
    }
    public long GetFact(int N){
        return f[N];
    }
    public long GetConv(int N,int R){
        return ((f[N]*rf[R])%Define.mod*rf[N-R])%Define.mod;
    }
}

Measured memory_bytes: 22368
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;


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

	println(min(a* b, c));
}

Measured memory_bytes: 3580
#include<stdio.h>



int map[ 6 ][ 2 ] = {
				{ 1 ,2  },
				{ -1 ,3 },
				{ 1 ,-1 },
				{ 4 ,5  },
				{ 5 ,2  },
				{ 2 ,1  }
		     } ;



int main()
{
	while( 1 )
	{
		char str[ 200 ] ;

		scanf( "%s" ,str ) ;

		if( str[ 0 ] == '#' )
		{
			return 0 ;
		}


		int i ,p = 0 ;
		for( i = 0 ; p != -1 && str[ i ] != '\0' ; ++i )
		{
			p = map[ p ][ str[ i ] - 48 ] ;
		}


		if( p == 5 )
		{
			puts( "Yes" ) ;
		}

		else
		{
			puts( "No" ) ;
		}
	}
}

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

typedef long long int lli;
typedef pair<lli,lli> PII;

#define M 100005
#define ep emplace_back
#define mp make_pair

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

//	freopen("input.txt","rt",stdin);
	
	string s;
	cin>>s;
	
	int n = (int)s.size();
	
	for(int i=0;i<n;i++)
		cout<<"x";
	
}


Measured memory_bytes: 256
import java.util.*

fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val xs = readLine()!!.split(' ').map { it.toInt() }.sorted()

    var ans = 0

    for (i in 0 until n - 2) {
        val a = xs[i]
        for (j in i + 1 until n - 1) {
            val b = xs[j]

            ans += xs.binarySearch(a + b, Comparator { o1, o2 -> if (o1 >= o2) 1 else -1 }, j + 1)
                    .let { if (it < 0) -(it + 1) - 1 else it }
                    .let { it - j }
        }
    }

    println(ans)
}


Measured memory_bytes: 44848
/*I MAY NOT GET THE SUCCESS IMMEDIATELY BUT I WILL GET IT FOR SURE*/
#include<bits/stdc++.h>
#define opt std::ios_base::sync_with_stdio(false)
#define I int
#define li		int32_t
#define lli		long long
#define ulli unsigned long long

#define pn			 printf("\n")
#define nl			 cout<<'\n'
#define sf(N)        scanf("%lld",&N)
#define pf(N)        printf("%lld",N)
#define sf1(N1,N2)    scanf("%lld %lld",&N1,&N2)
#define pf1(N1,N2)    printf("%lld %lld",N1,N2)
#define sf2(N1,N2,N3) scanf("%lld %lld %lld",&N1,&N2,&N3)
#define pf2(N1,N2,N3) printf("%lld %lld %lld",N1,N2,N3)

#define sl          cout<<' '
#define ps          printf(" ")

#define rep(i,a,b)	for(i=a;i<b;i++)
#define repr(i,a,b)	for(i=a;i>b;i--)
#define elif		else if
#define mset(a,b)	memset(a,b,sizeof(a))

#define pb		push_back
#define pob		pop_back
#define itr		iterator
#define sz()	size()
#define szof    sizeof
#define lb		lower_bound
#define ub		upper_bound
#define mp		make_pair
#define vlli    vector<lli>
#define plli	pair<lli,lli>
#define vplli	vector<plli >
#define F   	first
#define S		second
#define Dup     erase(unique(V.begin(),V.end()),V.end())

#define bs      binary_search
#define ub      upper_bound
#define lb      lower_bound

#define ALL(V)  V.begin(),V.end()

#define MX1     100000
#define MX2     1000000
#define Inf     1000000000000000
#define mod		1000000007
using namespace std;

lli Power(lli a,lli b)
{
    lli result=1;

    while(b)
    {
        if(b%2)
        {
            result=(result*a)%mod;
        }

        b=b>>1;
        a=(a*a)%mod;
    }

 return result;
}

int main()
{
    opt;

    /*#ifndef ONLINE_JUDGE
    freopen("double_squares.txt","r",stdin);
    freopen("double_squares_out.txt","w",stdout);
    #endif // ONLINE_JUDGE*/

    lli N,i;
    cin>>N;

    map<lli,lli>M;

    rep(i,0,N)
    {
        lli x;
        cin>>x;

        M[x]++;
    }

    lli Q;
    cin>>Q;

    while(Q--)
    {
        lli x;
        cin>>x;

        if(!M[x])
        {
            cout<<"NO";
            exit(0);
        }
        M[x]--;
    }

    cout<<"YES";


 return 0;
}


Measured memory_bytes: 12800
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>

#define INT unsigned long long int
#define FOR1(n) for(int i=0;i<n;i++)
#define FOR2(j, n) for(int j=0;j<n;j++)
#define FOR3(i, m, n) for(int i=m;i<=n;i++)
#define MOD 1000000007
#define MAX_N 100000

using namespace std;

main(){
	int a,b,h;
	cin >> a >> b >> h;
	printf("%d", (a+b)*h/2);
	
}


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

int main(){
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n), b(m);
  for(long long i = 0;  i < n; i++){
    cin >> a.at(i);
  }
  for(long long i = 0; i < m; i++){
    cin >> b.at(i);
  } //input
  
  vector<long long> sa(n+1);	//累積和の準備始め
  vector<long long> sb(m);
  for(long long i = 0; i <= n; i++){
    if(i == 0){
      sa.at(i) = 0;
    }
    else if(i == 1){
      sa.at(i) = a.at(i-1);
    }
    else{
      sa.at(i) = sa.at(i-1) + a.at(i-1);
    }
  }
  for(long long i = 0; i < m; i++){
    if(i == 0){
      sb.at(i) = b.at(i);
    }
    else{
      sb.at(i) = sb.at(i-1) + b.at(i);
    }
    //cout << sb.at(i) << endl;
  }	//累積和の準備終わり(without any problem)
  
  //二分探索始め
  long long ans = 0;
  for(long long i = 0; i <= n; i++){
    long long ok = -1;
    long long ng = m;
    while(ng != ok + 1 && sa.at(i) <= k){
      long long mid = 0;
      mid = (ok + ng)/2;
      if(sa.at(i) + sb.at(mid) <= k){
        ok = mid;
      }
      else{
        ng = mid;
      }
      //cout << sa.at(i) << mid << ok << " " << sb.at(mid) << endl;
      ans = max(ans, ok + i + 1); 
      //cout << i << endl;
    }
  }	//二分探索終わり
 
  cout << ans << endl;
}

Measured memory_bytes: 9568
package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)

	var ret int
	for j := 1; j <= n; j++ {
		ret += g(n, j)
	}

	fmt.Printf("%d\n", ret)
}

func g(n int, x int) int {
	y := n / x
	return y * (y + 1) * x / 2
}


Measured memory_bytes: 1868
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int n, Es = 0, Ws = 0;
string s;
cin>>n;
cin>>s;
  for(int i = 0; i < (int) s.size(); i++) {
    if(s[i]=='E') Es++;
    if(s[i]=='W') Ws++;
  }
int res = n;
int cE = 0, cW = 0;
  for (int i = 0; i < (int) s.size(); i++) {
    int c = cW + Es - cE;
    if (s[i]=='E') c--;
    res = min(res, c);
    if (s[i] == 'E') cE++;
    if(s[i]=='W') cW++;
  }

  cout << res << endl;
  return 0;
}

Measured memory_bytes: 900
#include <bits/stdc++.h>
using namespace std;
long long h,a,n,ok,k;
multiset<long long>s;
deque<long long>d;
int main (){
ios::sync_with_stdio(0);
scanf("%lld %lld",&n,&k);
for(int i=0;i<n;i++){
	 scanf("%lld",&h);
	 s.insert(h);
}
multiset<long long>::iterator it;
for(it=s.begin();it!=s.end();it++){
	 d.push_back(*it);
}
for(int i=0;i<n-k;i++){
	 ok=ok+d[i];
}
printf("%lld",ok);
return 0;
}


Measured memory_bytes: 11264
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    }
}


Measured memory_bytes: 23124
#include <stdio.h>
#include <math.h>

void insert(int code);
void find(int code);

/*
A:1
C:2
G:3
T:4
 */

char dictionary[390626];
int dictLen = 0;

main(){

  int i,j,n;
  char command[7];
  char op;
  int code;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s",command);
    j=0; code=0;
    while(j<=8){
      scanf("%c",&op);
      if(op=='\n') break;
      switch(op){
      case 'A':
	code+=1*pow(5,j);
	break;
      case 'C':
	code+=2*pow(5,j);
	break;
      case 'G':
	code+=3*pow(5,j);
	break;
      case 'T':
	code+=4*pow(5,j);
	break;
      default:
	j--;
      }
      j++;
    }
    if(command[0] == 'i'){ // insert
      insert(code);
    }else if(command[0] == 'f'){ // find
      find(code);
    }
  }

  return 0;

}

void insert(int code){
  dictionary[code] = 1;
}

void find(int code){
  if(dictionary[code] == 1) printf("yes\n");
  else printf("no\n");
}

Measured memory_bytes: 724
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
# define ll int64_t
# define str string
# define rep(i,n) for(ll i=0;i<n;i++)
# define rrep(i,n) for(ll i=1;i<=n;i++)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define mod 1000000007
# define PI 3.141592653589793
# define vec vector
#define dump(x) cerr<<#x<<"="<<x<<endl
using namespace std;



#define INF 2000000000
#define MAX_V 10



bool compare_by_b(pair<string,ll> a,pair<string,ll> b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}

bool my_compare(pair<string,ll> a,pair<string,ll> b){
    if(a.first != b.first) return a.first<b.first;
    if(a.second != b.second) return a.second>b.second;
    else return true;
}

ll modpow(ll a,ll n,ll mod1) {
    ll res=1;
    while(n>0){
        if(n&1) res=res*a%mod1;
        a = a*a%mod1;
        n >>= 1;
    }
    return res;
}

ll factorial(ll n){
    ll x=1;
    rrep(i,n) (x*=i)%=mod;
    return x;
}

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

ll lcm(ll a,ll b){
   return a/gcd(a,b)*b;
}
const ll MAX = 510000;
const ll MOD = 1000000007;
 
ll fac[MAX], finv[MAX], inv[MAX];
 
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll 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;
    }
}
 
// 二項係数計算
ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}


using graph = vector<vector<ll>>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll n,m;
    cin>>n>>m;
    priority_queue<ll> a;
    rep(i,n){
        ll num;
        cin>>num;
        a.push(num);
    }
    
    rep(i,m){
        ll sum=a.top();
        a.pop();
        a.push(floor(sum/2));
    }
    ll ans=0;
    rep(i,n){
        ans+=a.top();
        a.pop();
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1400
#include<iostream>

using namespace std;

int main() {
	int H[10000], W[10000];

	for (int i = 0; i < 9999; i++) {
		cin >> H[i] >> W[i];
		if (H[i] == 0 && W[i] == 0) {
			break;
		}
	}

	for (int j = 0; j < 9999; j++) {
		for (int k = 0; k < H[j]; k++) {
			for (int l = 0; l < W[j]; l++) {
				cout << "#";
			}
			cout << endl;
		}

		if (H[j] == 0 && W[j] == 0) {
			break;
		}
		
		cout << endl;
	}

	return 0;
}


Measured memory_bytes: 3092
n, k = map(int, input().split())
m = list(map(int, input().split()))
T = [0] * (n + 1)

b = 0
for i, a in enumerate(m):
    b += a - 1
    b %= k
    T[i + 1] = b

tmp = {}
count = 0

for i, b in enumerate(T):
    if i >= k:
        tmp[T[i - k]] -= 1

    if b in tmp:
        count += tmp[b]
        tmp[b] += 1
    else:
        tmp[b] = 1

print(count)

Measured memory_bytes: 40484
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

K = readInt()
d = defaultdict(int)
ans=0
for a in range(1,K+1):
	for b in range(1,K+1):
		x = math.gcd(a,b)
		for c in range(1,K+1):
			ans+=math.gcd(x,c)

print(ans)

Measured memory_bytes: 9732
#include<stdio.h>
int main(){
	int A, B, b = 0, c = 0, a;
	scanf("%d", &A);
	for (int j = 1; j <= A; j++){
		a = j % 2;
		b = 0;
		if (a == 1){
			for (int i = 1; i <= j; i++){
				if (j % i == 0) b += 1;
			}
			if (b == 8) c += 1;
		}
	}
	printf("%d", c);
}

Measured memory_bytes: 128
#include <iostream>
using namespace std;
int K;
int main() {
  cin >> K;
  int a[34] = {0, 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
  cout << a[K] << endl;
}

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

int main(void){
    long long N;cin>>N;
    std::vector<long long>A(N+1); // AのサイズをN+1にする
    for(long long i=0;i<N;i++){
        cin>>A[i];
    }
		A[N]=1000000007; // Aの最後に他の要素と一致しない要素(番兵)を入れる
    sort(A.begin(),A.end());
    long long ans=0;
    long long tmp=0;
    for(long long i=1;i<N+1;i++){ // 回す回数を1回増やす
        if(A[i]==A[i-1]){
            tmp++;
            // if(i==N-1){ // この処理が不要になる
            //     if(tmp%2==0){
            //         ans++;
            //     }    
            // }
        }
        else{
            if(tmp%2==0){
                ans++;
            }
            tmp=0;
        }
    }
    // if(A[N-1]!=A[N-2]){ // この処理も不要になる
    //     ans++;            
    // }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 4044
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#define rep(i,n) for(ll i=0; i<n; i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll mod=1e9+7;

int main(){
	int N;
	cin >> N;
	vi X(N);
	int sum=0;
	rep(i,N){
		cin >> X[i];
		sum+=X[i];
	}

	int ave = round((double)sum/N);
	int ans=0;
	rep(i,N){
		ans+=(X[i]-ave)*(X[i]-ave);
	}

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

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,l;
	cin>>n>>l;
	string s[n];
	for (int i = 0; i < n; ++i)
	{
		cin>>s[i];
	}
	sort(s,s+n);
	for (int i = 0; i < n; ++i)
	{
		cout<<s[i];
	}
	cout<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  string line;
  getline(cin,line);
  vector<int> v;
  int p=0;      
  while((p=line.find("peach",p)) != -1) {
     v.push_back(p);
     p++;
  }
  while((p=line.find("apple")) != -1) {
    line.replace(p,5,"peach");
  }
  FOR(i,v) {
    line.replace(*i,5,"apple");
  }
  cout << line << endl;
}

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

int unionF1[200001], unionF2[200001], rank1[200001], rank2[200001];
int n, k, l;

void init() {
	for (int i = 1; i <= n; i++) {
		unionF1[i] = i;
		unionF2[i] = i;
	}
}

int find(int u, int x) {//uの方のunionfindで根を見つける
	int* uni;
	if (u == 0) {
		uni = unionF1;
	}
	else {
		uni = unionF2;
	}

	if (x == uni[x]) {
		return uni[x];
	}
	else {
		return uni[x] = find(u, uni[x]);
	}
}

void unite(int u, int x, int y) {//uの方のunionfindで集合を合体させる
	int *uni, *rank;
	if (u == 0) {
		uni = unionF1;
		rank = rank1;
	}
	else {
		uni = unionF2;
		rank = rank2;
	}

	if (rank[find(u,x)] >= rank[find(u,y)]) {
		rank[find(u, x)]++;
		uni[find(u, y)] = find(u, x);
	}
	else {
		rank[find(u, y)]++;
		uni[find(u, x)] = find(u, y);
	}
}

bool same(int u, int x, int y) {
	return find(u, x) == find(u, y);
}

int main() {
	cin >> n >> k >> l;
	init();
	for (int i = 0; i < k; i++) {
		int p, q;
		cin >> p >> q;
		unite(0, p, q);
	}
	for (int i = 0; i < l; i++) {
		int r, s;
		cin >> r >> s;
		unite(1, r, s);
	}
	map<pair<int, int>, int>m;
	for (int i = 1; i <= n; i++) {
		//cout << "0:" << find(0, i) << " 1:" << find(1, i) << endl;
		pair<int, int> p;
		p.first = find(0, i); p.second = find(1, i);
		m[p]++;
	}
	for (int i = 1; i <= n; i++) {
		//cout << "0:" << find(0, i) << " 1:" << find(1, i) << endl;
		pair<int, int> p;
		p.first = find(0, i); p.second = find(1,i);
		cout << m[p] << " ";
	}
}

Measured memory_bytes: 13696
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define v vector
#define mp make_pair
typedef long long ll;
typedef pair<int, int > pii;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
// D

int N;
v<pii> adj[100000];
int col[100000];

void dfs(int x, int p, int d){
	col[x] = d%2;
	for(auto e : adj[x]) if(e.first!=p)
		dfs(e.first, x, d+e.second);
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> N;
	FOR(i,0,N-1){
		int a,b,l;
		cin >> a >> b >> l;
		a--, b--;
		l %= 2;
		adj[a].push_back({b,l});
		adj[b].push_back({a,l});
	}
	dfs(0,-1,0);
	FOR(i,0,N) cout << col[i] << "\n";
}



Measured memory_bytes: 8448
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    string first_s;
    string first,second;
    cin>>s;
    first_s = s;
    reverse(s.begin(),s.end());
    int n = s.size();
    string new_first,new_second;
    for (int i = 1;i<=(n-1)/2;i++){
        first+=s[i-1];
    }
    for (int i = (n+3)/2;i<=n;i++){
        second+=s[i-1];
    }
    new_first = first;
    new_second = second;
    reverse(first.begin(),first.end());
    reverse(second.begin(),second.end());
    if (first==new_first && second==new_second && first_s==s)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
struct Cake{
	int w;
	int h;
};

int main() {
	int n, w, h;
	while (cin >> n >> w >> h, n | w | h){
		vector<Cake> vca;
		vca.push_back({ w, h });
		REP(i, n){
			int p, s;
			cin >> p >> s;
			s = s % (vca[p - 1].w + vca[p - 1].h);
			if (s < vca[p - 1].w){
				int w1 = min(s, vca[p - 1].w - s);
				int w2 = max(s, vca[p - 1].w - s);
				int h = vca[p - 1].h;
				vca.erase(vca.begin() + p - 1);
				vca.push_back({ w1, h});
				vca.push_back({ w2, h});
			}
			else{
				int w = vca[p - 1].w;
				int h1 = min((s - w), vca[p - 1].h - (s - w));
				int h2 = max((s - w), vca[p - 1].h - (s - w));
				vca.erase(vca.begin() + p - 1);
				vca.push_back({ w, h1 });
				vca.push_back({ w, h2 });
			}

		}

		vector<int> vi;
		REP(i, vca.size())vi.push_back(vca[i].w*vca[i].h);
		SORT(vi);
		REP(i, vi.size()){
			cout << vi[i] ;
			if (i != vi.size() - 1)cout << " ";
		}
		cout << endl;
	}
}

Measured memory_bytes: 1216
r, c = map(int, input().split())
cells = [[0 for i in range(c+1)] for j in range(r+1)]

for i in range(r):
  row = list(map(int, input().split()))
  for j in range(c):
    cells[i][j] = row[j]
    cells[i][c] += cells[i][j]
    cells[r][j] += cells[i][j]
    cells[r][c] += cells[i][j]

for i in range(r+1):
  for j in range(c):
    print(cells[i][j], end=" ")
  print(cells[i][c])


Measured memory_bytes: 6216
// ayy
// ' lamo
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld; //CARE
typedef complex<ld> pt;
#define fi first
#define se second
#define pb push_back
const ld eps=1e-8;
const int inf=1e9+99;
const ll linf=1e18+99;
const int P=1e9+7;







const int N=500<<10;
int n;
char s[N];

int max3(int a,int b,int c) {
	return max(a,max(b,c));
}
int g(int L,int R,bool flag=0) {
	for(;L<=R && s[L]=='0';) ++L;
	for(;R>=L && s[R]=='0';) --R;
	if(L>R) return 0;

	if(!flag) {
		for(int i=L+1;i<=R;i++) {
			if(s[i]=='0' && s[i-1]=='0') {
				return g(L,i-2,1)+g(i+1,R);
			}
		}
	}


	vector<int> v;
	int ct=0;
	assert(s[L]=='1' && s[R]=='1');
	for(int i=L;i<=R;i++) {
		if(s[i]=='0') {
			if(ct) v.pb(ct), ct=0;
		} else {
			++ct;
		}
	}
	assert(ct);
	v.pb(ct);

	int a=0,b=0,c=-N;
	// a: need one to shoot left
	// b: leave none
	// c: leave one to shoot right
	for(int x:v) {
		int d,e,f;
		d=max(b+x,a+x-1);
		e=max(a,c+x);
		f=max(x>=2 ? a : b,c+x-1);

		e=max(e,f);
		d=max(d,e);


		a=d,b=e,c=f;
		// cerr<<a<<","<<b<<","<<c<<endl;
	}
	return b;
}


int32_t main() {
	scanf("%d%s",&n,s);
	printf("%d\n",g(0,n-1));
}













Measured memory_bytes: 6528
result = []
while True:
    a,op,b = input().split()
    
    if op == '?':
        break
    elif op == '+':
        result.append(int(a) + int(b))
    elif op == '-':
        result.append(int(a) - int(b))
    elif op == '*':
        result.append(int(a) * int(b))
    elif op == '/':
        result.append(int(int(a) / int(b)))
        
for i in result:
    print(i)

Measured memory_bytes: 7616
#define _USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstdio>
#include <complex>
#include <numeric>
#include <string.h>
#include <random>
#include <cmath>
#define rep(i,n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD = 1000000007;
//const ll MOD = 998244353
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int x;
bool dp[100001];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> x;
    dp[0] = true;
    for (int i = 100; i <= x; i++) {
        for (int j = 0; j < 6; j++) {
            if (i < 100+j) continue;
            dp[i] |= dp[i-100-j];
        }
    }
    cout << dp[x] << endl;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pb push_back
#define ff first
#define ss second
#define ll long long
#define ld long double
typedef pair<ll,ll> pll;
typedef pair<ld,pll> ppll;
typedef vector <pll> vi;
#define out(x) return cout<<x,0;
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define lc(n) 2*n+1
#define rc(n) 2*n+2
#define fill(a,b) memset((a),(b),sizeof((a)))
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
vi v;
ll n, m;
ll dp[10005][1005];

ll slove ( ll cost , ll i ){
	if ( cost <= 0 ) return 0;
	if ( i >= n ) return 1000000000LL;
	if ( dp[cost][i] != -1 ) 
		return dp[cost][i];
	ll x = 0 , y = 0 , z = 0;
	x = v[i].ss + slove( cost-v[i].ff , i );
	y = v[i].ss + slove( cost-v[i].ff , i+1 );
	z = slove( cost , i+1 );
	dp[cost][i] = min({x , y , z});
	return dp[cost][i];
}

int main() {
    FAST;
    ll i, k, x = 0, y = 0, t, z = 0, ans = 0, j, c = 0;
    cin >> m >> n;
    fill ( dp , -1 );
    x = 0;
    for ( i = 0 ; i < n ; i ++ ){
    	cin >> y >> z;
    	v.pb({y , z});
    }
    cout << slove ( m , 0 );
}

Measured memory_bytes: 79488
chars = {'A': 'a', 'B': 'b', 'C': 'c'}
line = [int(x) for x in input().split(' ')]
s = input()
i = 1
for c in s:
    if i == line[1]:
        print(chars[c], end='')
    else:
        print(c, end='')
    i = i + 1
print()

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

int main() {
  int N, K;
  string S, A;
  cin >> N >> K >> S;
  A = S.at(K-1);
  
  if (A == "A") {
    S.at(K-1) = 'a';
  }
  if (A == "B") {
    S.at(K-1) = 'b';
  }
  if (A == "C") {
    S.at(K-1) = 'c';
  }
  
  cout << S << endl;
}

Measured memory_bytes: 256
#include<algorithm>
#include<array>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<iso646.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;

#define FOR(counter,initial_value,condition) for(int counter = initial_value; counter < condition;counter++)

int main()
{
	int n;
	cin >> n;
	cout << "ABC" << std::setfill('0') << std::setw(3) << n << endl;
}

Measured memory_bytes: 764