akhauriyash/Code-Regression huggingface dataset:
use std::io::stdin;

fn main(){
    let (n, m) = nm();
    let edges = read_edges(n, m);
    println!("{}", solve(&edges));
}

fn _solve(node: usize, edges: &Vec<Vec<i64>>, mut visited: &mut Vec<bool>) -> i64{
    if visited.iter().fold(true, |acc, &x|{acc && x}) {1}
    else{
        let mut ans = 0;
        for next in edges[node].iter(){
            if !visited[*next as usize]{
                visited[*next as usize] = true;
                ans += _solve(*next as usize, edges, visited);
                visited[*next as usize] = false;
            }
        }
        ans
    }
}

fn solve(edges: &Vec<Vec<i64>>) -> i64{
    let mut v: Vec<bool> = Vec::new();
    for _ in 0..edges.len(){v.push(false)}
    v[0] = true;
    _solve(0, edges, &mut v)
}

fn read_edges(n: i64, m: i64)->Vec<Vec<i64>>{
    let mut edges: Vec<Vec<i64>> = Vec::new();
    for _ in 0..n{edges.push(Vec::new())}
    for _ in 0..m{
        let (a, b) = nm();
        edges[(a-1) as usize].push(b-1);
        edges[(b-1) as usize].push(a-1);
    }
    edges
}

#[allow(dead_code)]
fn getline() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn getvec() -> Vec<i64>{
    let line = getline();
    line.split_whitespace().map(|x| x.parse().unwrap()).collect()
}

fn nm() -> (i64, i64){
    let temp: Vec<i64> = getvec();
    (temp[0], temp[1])
}

Measured memory_bytes: 4352
use proconio::{fastout, input};

#[fastout]
fn main() {
    input! {
        n: usize,
        k: usize,
        a: [usize; n],
    }
    let mut len1 = 1;
    let mut first = a[1 - 1];
    let mut second = a[a[1 - 1] - 1];
    while first != second {
        len1 += 1;
        first = a[first - 1];
        second = a[a[second - 1] - 1];
    }

    let mut len2 = 0;
    let mut third = 1;
    while second != third {
        len2 += 1;
        second = a[second - 1];
        third = a[third - 1];
    }

    let mut last = 1;
    let l = if k < len1 + len2 {
        k
    } else {
        len2 + (k - len2) % len1
    };
    for _ in 0..l {
        last = a[last - 1];
    }
    println!("{}", last);
}


Measured memory_bytes: 4824
#include <bits/stdc++.h>
using namespace std;
long long int Inv(long long int a, long long int m) {
  int c[50];
  long long int e[50];  
  c[0] = (m-2) % 2, e[0] = (m-2) / 2; 
  for (int i = 0; i < 49; ++i){
     c[i+1] = e[i] % 2;
     e[i+1] = e[i] / 2;  
  }
  long long int d[50]; 
  d[0] = a; 
  for (int j = 0; j < 49; ++j){
     d[j+1] = (d[j] * d[j]) % m; 
  }
  long long int Q; 
  Q = 1; 
  for(int k = 0; k < 49; ++k){
  if (c[k] == 0) Q = Q; 
  if (c[k] == 1) Q = (Q * d[k]) % m; 
  }
 return Q;
}

int main() {
long long int N; 
  cin >> N; 
long long int M = N * (N + 1) / 2; 
long long int B = 0; 
long long int P = 1000000007; 
long long int A[114514] = {}; 
long long int Fac[114514] = {}; 
Fac[0] = 1; 
for(int i = 1; i <= 100010; i++){
  Fac[i] = (Fac[i-1] * i) % P; 
}
  
for(int i = 0; i <= N; i++){
cin >> A[i]; 
B += A[i]; 
}
long long int C = B - M; 
long long int X, Y; 
for(int i = 0; i <= N; i++){
if(A[i] == C){
X = i; 
break; 
}
}
for(int i = X+1; i <= N; i++){
if(A[i] == C){
Y = i; 
break; 
}
}
long long int R = Y - X; 
for(int K = 1; K <= N+1; K++){
if(R > N - K + 1){
cout << ((((Fac[N+1] * Inv(Fac[K], P)) % P) * 
          Inv(Fac[N + 1 - K], P)) % P) % P << endl; 
}
if(R <= N - K + 1){
cout << (P +(((Fac[N+1] * Inv(Fac[K], P)) % P) * Inv(Fac[N + 1 - K], P)) % P 
         - (((Fac[N-R] * Inv(Fac[K-1], P)) % P) *
            Inv(Fac[N - R - K + 1], P)) % P) % P << endl; 
}
}
}

Measured memory_bytes: 2944
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  scanf("%d",&n);
  vector<vector<int>> days(n,vector<int>(3));
  for(int i=0;i<n;i++){
    for(int j=0;j<3;j++) cin>>days[i][j];
  }
  vector<vector<int>> dp(n,vector<int>(3));
  for(int i=0;i<3;i++) dp[0][i]=days[0][i];
  for(int i=1;i<n;i++){
    for(int j=0;j<3;j++){
      if(j==0) dp[i][j]=days[i][0]+max(dp[i-1][1],dp[i-1][2]);
      else if(j==1) dp[i][j]=days[i][1]+max(dp[i-1][0],dp[i-1][2]);
      else dp[i][j]=days[i][2]+max(dp[i-1][0],dp[i-1][1]);
    }
  }
  cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));
  return 0;
}
                            

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

int main() {
    int N, K;
    cin >> N >> K;
    vector<long> H(N);
    rep(i, N){
        cin >> H.at(i);
    }
    if(N < K){
        cout << 0 << endl;
        return 0;
    }
    sort(H.begin(), H.end());
    long sum = accumulate(H.begin(), H.end() - K, 0L);
    cout << sum << endl;
}


Measured memory_bytes: 1792
void main() {
	auto ip = readAs!(ulong[]), N = ip[0], K = ip[1];
	ulong[ulong] P;
	foreach(i; 0..N) {
		auto ip2 = readAs!(ulong[]), a = ip2[0], b = ip2[1];
		P[a] += b;
	}
	auto Q = P.keys.sort();
	ulong tmp;
	foreach(i; Q) {
		tmp += P[i];
		if(tmp >= K) {
			i.writeln;
			return;
		}
	}
}

// ===================================

import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

T[] deepcopy(T)(T[] a) {
	static if(isArray!T) {
		T[] res;
		foreach(i; a) {
			res ~= deepcopy(i);
		}
		return res;
	} else {
		return a.dup;
	}
}

Measured memory_bytes: 11900
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".", "0", 1)
            rec = 0
            for d in [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]:
                nx, ny = i + d[0], j + d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            if rec > 0:
                S[i]=S[i][::-1]
                S[i] = S[i].replace("0", str(rec), 1)
                S[i] = S[i][::-1]




for i in range(H):
    print(S[i])

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

int judge(pair<double, double> p1, pair<double, double> p2){
	double S = p1.first*p2.second - p2.first*p1.second;
	if (0 < S) return 1;
	if (0 > S) return -1;
	if (0 == S) return 0;
}

int main(){
	pair<double, double> A, B, C, P;

	while (cin >> A.first >> A.second >> B.first >> B.second >> C.first >> C.second >> P.first >> P.second){
		pair<double, double> AB, BC, CA, AP, BP, CP;
		AB = { B.first - A.first, B.second - A.second };
		BC = { C.first - B.first, C.second - B.second };
		CA = { A.first - C.first, A.second - C.second };
		AP = { P.first - A.first, P.second - A.second };
		BP = { P.first - B.first, P.second - B.second };
		CP = { P.first - C.first, P.second - C.second };
		
		if (judge(AP, AB) == judge(BP, BC) && judge(BP, BC) == judge(CP, CA) && judge(CP, CA) == judge(AP, AB)){
			cout << "YES" << endl;
		}
		else{
			cout << "NO" << endl;
		}
	}

	return 0;
}


Measured memory_bytes: 3164
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <map>

using namespace std;

using ll = long long;
using P = pair<ll,ll>;

#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = ((ll)(x) - 1); i >= 0; i--)

#define _upgrade ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

//ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

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

const ll mod = 1e9 + 7;
const ll IMF = 1LL<<29;
const double PI = 3.14159265358979323846;


// ****************************************CODE***************************************//


ll n,a,b,c;
//invが階乗、invFが逆元の階乗
ll inv[1010000], invF[1010000];
//pがAが勝つ確率,qがBが勝つ確率
ll p,q;

//modの掛け算
ll mul(ll x, ll y){
        return (x*y) % mod;
}

//繰り返し自乗法
ll pow(ll x, ll y){
        ll r = 1;
        while (y) {
                if (y & 1)r = r * x%mod;
                x = x * x%mod; 
                y >>= 1;
        }
        return r;
}

//逆元を求める
ll rev(ll x){
        return pow(x, mod-2);
}

//コンビネーションの計算をする。
ll comb(ll x, ll y){
        if(y < 0 || x < y) return 0;
        return mul(mul(inv[x], invF[x-y]), invF[y]);
}

//勝つ確率を求める計算
ll totoro(ll x, ll y){
        return pow(p,x) * pow(q,y)%mod*comb(x+y,x)%mod;
}

int main(void){
        cin >> n >> a >> b >> c;
        //階乗
        inv[0] = 1;
        for(ll i = 1; i <= 1000000; i++){
                inv[i] = mul(inv[i-1],i);
        }
        //逆元の階乗
        invF[1000000] = rev(inv[1000000]);
        for(ll i = 1000000; i >= 1; i--){
                invF[i-1] = mul(invF[i],i);
        }

        //それぞれが勝つ確率
        p = mul(rev(a+b),a);
        q = mul(rev(a+b),b);

        //それぞれが何回出したら勝つかの確率を合計する。
        ll res = 0;
        for(ll i = 0; i < n; i++){
                res = (res+totoro(n-1,i)*(n-1+i+1)%mod*p)%mod;
                res = (res+totoro(i,n-1)*(n-1+i+1)%mod*q)%mod;
        }

        //2人が勝つ確率から、期待値を求める。
        ll ans;
        ans = res*(a+b+c)%mod*rev(a+b)%mod;

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


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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c)  (c).begin(),(c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(),(c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for(ll i= (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x)  cerr << #x << " = " << (x) << endl


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    int s; cin >> s;
    bool A[1000000];
    int ans = 1;
    while(!A[s]) {
        ans++;
        A[s] = true;
        if(s % 2 == 0) {
            s /= 2;
        } else {
            s = 3 * s + 1;
        }
    }
    cout << ans << "\n";
}

Measured memory_bytes: 3652
#include<iostream>
#include<utility>
using namespace std;
int partition(int*,int,int);

int main(){
	int n;
	int A[100001];
	cin >> n;
	for(int i=0;i<n;i++) cin >> A[i];
	int res = partition(A,0,n-1);
	for (int i = 0; i < n; ++i)
	{
		if(i == res) cout << "[" << A[i] << "] ";
		else if (i == n-1) cout << A[i] << endl;
		else cout << A[i] << " ";
	}
	return 0;

}

int partition(int A[],int p,int r){
	int x = A[r];
	int i = p-1;
	for(int j = p;j<=r;j++){
		if(A[j] > x){

		}else{
			i++;
			swap(A[i],A[j]);
		}
	}
	return i;
}

Measured memory_bytes: 3456
/*i-=-1*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define in(n) for(int i=0;i<n;i++)
#define inj(n) for(int j=0;j<n;j++)
#define itn int
#define ll long long 
#define ull unsigned ll
#define int ll int
#define S second 
#define F first 
#define mod 1000000007
#define globalbool 2000000
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
using namespace std;
using namespace std::chrono;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	auto start = high_resolution_clock::now();
	int a,b;
	cin>>a>>b;
	if(a>b)
	{
		in(a)
			cout<<b;
	}
	else
	{
		in(b)
			cout<<a;
	}

	auto stop = high_resolution_clock::now();
    //auto duration = duration_cast<microseconds>(stop - start); 
  
    //cout << "Time taken by function: "
    //<< duration.count() << " microseconds" << endl;
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	read  = bufio.NewReader(os.Stdin)
	write = bufio.NewWriter(os.Stdout)
)

func main() {
	defer write.Flush()

	var L int64
	fmt.Fscan(read, &L)

	l := float64(L) / 3.0
	fmt.Fprintln(write, l*l*l)
}


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

int main() {
  int N;
  cin>>N;
  vector<int> h(N);
  for(int i=0;i<N;i++) cin>>h[i];
  vector<int> c(N);
  long ans=0;
  c[1]=abs(h[1]-h[0]);
  for(int i=2;i<N;i++){
    c[i]=min(c[i-1]+abs(h[i]-h[i-1]),c[i-2]+abs(h[i]-h[i-2]));
  }

  cout<<c[N-1]<<endl;




  }


Measured memory_bytes: 1024
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    cnt = [0] * (N + 1)
    for i in range(1, N + 1):
        for j in range(i, N + 1, i):
            cnt[j] += 1

    ans = 0
    for i in range(1, N + 1):
        ans += i * cnt[i]

    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 145904
#include<cstdio>
#include<cmath> 
using namespace std;
long long n,ans;
int s;
int main(){
    scanf("%lld",&n);
    for(int i=sqrt(n);i>0;--i)
        if(n%i==0){
            ans=n/i;
            break;
        }
    while(ans>0){
        ++s;
        ans/=10;
    }
    printf("%d\n",s); 
    return 0;
}	 

Measured memory_bytes: 128
#include<iostream>
#include<stdio.h>
using namespace std;
 
int main(){
    while(1){
    int x,y;
    cin >> x >>y;
    if(x==0&&y==0)
    {break;} 
    int i, j;
    for ( i = 0; i < x; i++ ){
    for ( j = 0; j < y; j++ ){
        if(0<i&&i<x-1&&0<j&&j<y-1){
            printf(".");
        } else {
            printf("#");
        }}
    printf("\n");
    }
	printf("\n");}
    return 0;
	}


Measured memory_bytes: 1112
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
    REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}

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; }



int main(int argc, const char * argv[]) {
    
   
    
    int a,b; cin >> a >> b;
    cout << (a+b+1)/2 << endl;
    
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;

  cout << (n/2 + n%2) << endl;
  return 0;
}


Measured memory_bytes: 256
x, n = [int(_) for _ in input().rstrip("\n").split(" ") if _]
if n > 0:
  p = set([int(_) for _ in input().rstrip("\n").split(" ")])
else:
  p = set([])
for diff in range(51):
    if not (x - diff) in p:
        print((x - diff))
        break
    if not (x + diff) in p:
        print((x + diff))
        break



Measured memory_bytes: 9196
#include<iostream>
#include<vector>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
int main(void){
    int N ;
    cin >> N ;
    vector<int> a(N,0);

    for(int i = 0; i < N ; i++)
    cin >> a[i] ;

    sort(a.begin(), a.end() );
    int b ,count ;
    count = 1;
    b = a[0] ;
    for(int i = 0; i< N - 1; i++){
      if(a[i+1]!=b){
          count ++;
      }

      b = a[i+1];


    }

    cout << count << endl;
    return 0;
}

Measured memory_bytes: 256
n = int(input())
a = [0]
a += [int(_) for _ in input().split()]
a += [0]

b = []
total = 0
pos = 0
for i in range(n + 1):
  total += abs(a[i] - a[i + 1])
  pos = a[i + 1]

for i in range(1, n + 1):
  sum = total - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a [i]) + abs(a[i - 1] - a[i + 1])
  print(sum)
  

Measured memory_bytes: 14048
#include <bits/stdc++.h>
using namespace std;
int main() {
	long long X;
	cin >> X;
	long long y = X / 11 * 2;
	if (X % 11 == 0) ;
	else if (X % 11 <= 6) y += 1;
	else y += 2;
	cout << y << endl;
}


Measured memory_bytes: 764
S = input()

L = len(S) - 1
cand = []

for bit in range(1 << L):
    wh = []
    for i in range(L):
        if (bit & (1 << i)):
            wh.append(i)
    tmp = []
    prev = 0
    for i in wh:
        tmp.append(int(S[prev:i+1]))
        prev = i+1
    tmp.append(int(S[prev:]))
    cand.append(sum(tmp))
print(sum(cand))

Measured memory_bytes: 3064
import java.util.*;

public class Main {
	static int max;
	static int[][] dp;
	static int m;
	static Key[] keys;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		m = sc.nextInt();
		max = 0;
		keys = new Key[m + 1];
		for (int i = 1; i <= m; i++) {
			keys[i] = new Key(sc.nextInt());
			int x = sc.nextInt();
			for (int j = 0; j < x; j++) {
				keys[i].add(sc.nextInt());
			}
			max += keys[i].cost;
		}
		int all = 0;
		for (int i = 0; i < n; i++) {
			all += pow2(i);
		}
		dp = new int[m + 1][all + 1];
		int cost = dfs(m, all);
		if (cost > max) {
			cost = -1;
		}
		System.out.println(cost);
	}
	
	static int dfs(int idx, int k) {
		if (k == 0) {
			return 0;
		}
		if (idx == 0) {
			return max + 1;
		}
		if (dp[idx][k] != 0) {
			return dp[idx][k];
		}
		int ans = Math.min(dfs(idx - 1, k), keys[idx].cost + dfs(idx - 1, k - (k & keys[idx].open)));
		dp[idx][k] = ans;
		return ans;
	}
	
	static class Key {
		int cost;
		int open;
		
		public Key(int cost) {
			this.cost = cost;
			this.open = 0;
		}
		
		public void add(int a) {
			open += pow2(a - 1);
		}
	}
	
	static int pow2(int x) {
		if (x == 0) {
			return 1;
		} else {
			return 2 * pow2(x - 1);
		}
	}
}


Measured memory_bytes: 50764
import queue
K = int(input())

q = queue.Queue()

x = 0
for i in range(1, 10):
  q.put(i)
  
x = 1
while x != K :
  x += 1
  y = q.get()
  z = y % 10
  if z != 0:
    q.put(10 * y + z - 1)
  q.put(10 * y + z)
  if z != 9:
    q.put(10 * y + z + 1)
  
ans = q.get()
print(ans)

Measured memory_bytes: 12480
x, y = map(int, input().split())
a=y-2*x

if(a<0):
  print("No")
elif(4*x<y):
  print("No")
elif(x==y):
  print("No")
elif(y<x):
  print("No")
else:
  if(a%2==0):
    print("Yes")
  else:
    print("No")

Measured memory_bytes: 9188
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define show(x) {for(auto i: x){cout << i << " ";} cout<<endl;}
#define showm(m) {for(auto i: m){cout << m.x << " ";} cout<<endl;}
typedef long long ll;
typedef pair<int, int> P;
ll gcd(int x, int y){ return y?gcd(y, x%y):x;}
ll lcm(ll x, ll y){ return (x*y)/gcd(x,y);}

const int max_n = 100050;
int baket[max_n] = {0};

int main()
{
    int n;
    cin >> n;
    vector<int> a;
    rep(i, n){ int tmp; cin >> tmp; a.push_back(tmp);}

    rep(i, n){
        baket[a[i]]++;
    }

    int max_ans = 0;
    rep(i, max_n){
        int ans = 0; 
        if (i != 0){
            ans += baket[i-1] + baket[i] + baket[i+1];
        } else {
            ans += baket[0] + baket[1];
        }
        max_ans = max(ans, max_ans);
    }
    cout << max_ans << endl;
}



Measured memory_bytes: 1144
def f_lis_on_tree(INF=float('inf')):
    # 参考: https://note.com/tanon_cp/n/n51ee6db8f5b2
    import sys
    sys.setrecursionlimit(10**7)
    import bisect
    N = int(input())
    A = [0] + [int(i) for i in input().split()]  # 以下、1-origin で考える
    Edges = [[int(i) for i in input().split()] for j in range(N - 1)]

    graph = [[] for _ in range(N + 1)]
    for a, b in Edges:
        graph[a].append(b)
        graph[b].append(a)

    ans = [0] * (N + 1)
    visited = [False] * (N + 1)
    visited[1] = True  # 根は訪問済
    lis = [INF] * (N + 1)
    changes = []

    def dfs(v):
        # 行きがけの処理
        # LIS の更新する場所を二分探索で求める
        pos = bisect.bisect_left(lis, A[v])
        changes.append((pos, lis[pos]))  # 更新した要素とその値を記録しておく
        lis[pos] = A[v]
        ans[v] = bisect.bisect_left(lis, INF)  # INF が現れるまでの要素数が v での解

        # 次の頂点へ
        for child in graph[v]:
            if not visited[child]:
                visited[child] = True
                dfs(child)

        # 帰りがけの処理 (頂点 v で更新した LIS の値を復元)
        pos, val = changes.pop()
        lis[pos] = val

    dfs(1)
    return ' '.join(map(str, ans[1:]))

print(f_lis_on_tree())

Measured memory_bytes: 311088
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};

int main() {
    int a, b; cin >> a >> b;
    int h[1000] = {0};
    rrep(i, 1, 999 + 1) {
        h[i] += h[i - 1];
        h[i] += i;
    }
    rrep(i, 2, 999 + 1) {
        if (h[i] - b == h[i - 1] - a) {
            cout << h[i] - b << endl;
            return 0;
        }
    }
}
    


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

using namespace std;

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

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define DEBUG_ARR(v,n) cout<<#v<<":";REP(i,n)cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int x,y,a,b,c;
    cin>>x>>y>>a>>b>>c;
    vl p(a), q(b), r(c);
    REP(i,a){
        cin>>p[i];
    }
    REP(i,b){
        cin>>q[i];
    }
    REP(i,c){
        cin>>r[i];
    }
    sort(ALL(p), greater<ll>());
    sort(ALL(q), greater<ll>());
    sort(ALL(r), greater<ll>());

    vl v;
    REP(i,x){
        v.emplace_back(p[i]);
    }
    REP(i,y){
        v.emplace_back(q[i]);
    }

    sort(ALL(v));

    int ridx =0;
    REP(i,min(c,x+y)){
        if(v[i] < r[ridx]){
            v[i] = r[ridx];
            ridx++;
        }
    }

    //DEBUG_VEC(v);
    ll ans = accumulate(ALL(v),0LL);
    cout << ans << endl;
    return 0;
}

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

int main(){
  int u;
  cin>>u;
  ll ac=0,wa=0,tle=0,re=0;
  string o;
  for(int i=0;i<u;i++){
    cin>>o;

    if(o=="AC")ac++;
    else if(o=="WA")wa++;
    else if(o=="TLE")tle++;
    else if(o=="RE")re++;
  }
  cout<<"AC x "<<ac<<endl;
  cout<<"WA x "<<wa<<endl;
  cout<<"TLE x "<<tle<<endl;
  cout<<"RE x "<<re<<endl;
}

Measured memory_bytes: 3624
A,B,C,D,E,F = map(int,input().split())

water,sugar = -1,-1
t = 0
for i in range(31):
    for j in range(31):
        for p in range(F//C):
            for q in range(F//D):
                k = i*A + j*B
                pq = p*C + q*D
                if k == 0:
                    break

                if k*100 + pq > F:
                    break

                if k*E >= pq:
                    if t <= (100*pq)/(100*k + pq):
                        t = (100*pq)/(100*k + pq)
                        water,sugar = 100*k+pq,pq

print(water,sugar)


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

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

    vector<ll> a(A),b(B);
    rep(i,A) cin>>a[i];
    rep(i,B) cin>>b[i];
    
    ll minv= *min_element(a.begin(),a.end())+ *min_element(b.begin(),b.end());
    rep(i,m){
        ll x,y,c;
        cin>>x>>y>>c;
        ll sum;
        sum=a[x-1]+b[y-1]-c;
        minv=min(minv,sum);
    }
    cout<<minv<<"\n";
    return 0;
}

Measured memory_bytes: 4784
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p,r):
        if A[j] <= x:
            i = i+1
            A[i],A[j] = A[j],A[i]
    A[i+1],A[r] = A[r],A[i+1]
    for j,k in enumerate(A):A[j] = str(k)
    A[i+1] = "["+A[i+1]+"]"
    return i+1

n = int(input())
A = [int(i) for i in input().split()]
ans = ""
partition(A,0,n-1)
for i in A:ans+=i+" "
print(ans[:-1])


Measured memory_bytes: 16364
s = int(input())

a = [s]
for i in range(10**6):
    if a[i]%2 == 0:
        ai = a[i]// 2
    else:
        ai = 3*a[i]+1
    if ai in a:
        ans = i + 2
        break
    a.append(ai)
print(ans)

Measured memory_bytes: 9128
//
//  main.cpp
//  sss
//
//  Created by 吉井　元 on 2019/09/04.
//  Copyright © 2019 吉井　元. All rights reserved.
//

#include <iostream>


using namespace std;

int main(){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    cout << a*b -c*b - d*a + c*d << endl;
    
    
}




Measured memory_bytes: 256
/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/

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

#define io ios_base::sync_with_stdio(false)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
#define mod 1000000007
#define pii pair<int, int>
#define PI 2*acos(0.0)

int main()
{
    io;
    set<string>se;
    int n;
    cin>>n;
    while(n--)
    {
        string s;
        cin>>s;
        se.insert(s);
        /*int h = 0, l = s.size();
        for(int i = 1; i<=l; i++)
            h += i*((int)s[i-1]-96);
        has[h] = 1;*/
    }
    cout << se.size();
    return 0;
}


Measured memory_bytes: 19204
#include <cstdlib>
#include <set>
#include <cstdio>
#include <algorithm>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;

int nans[1000];

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m), n + m){
    int atime = m;
    int anani = m + 1;
    int a     = -1;

    REP(i,n){
      scanf("%d", &nans[i]);
    }
    REP(i,n){
      int nan = nans[i];
      int t = 0;

      t = nan * ((atime + nan) / nan);
      if(t > m) t -= nan;
      if(t < 0) t += nan;
      if(t > m){ a = i; break; }

      //printf("t: %d\n", t);

      if(atime > t){
	atime = t;
	anani = nan;
	a     = i;
      }else if(atime == t){
	if(anani > nan){
	  atime = t;
	  anani = nan;
	  a     = i;
	}else if(anani == nan){
	  atime = t;
	  anani = nan;
	  a     = n - 1;
	}
      }
    }
    printf("%d\n", a + 1);
  }
  return 0;
}

Measured memory_bytes: 0
n, m = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]

if m == 0:
    ans = 0
else:
    ans = 0
    for i in range(8):
        a = []
        for z in x:
            r = 0
            for j in range(3):
                r += z[j] * (1 if i&1<<j == 0 else -1)
            a.append(r)
        a.sort(reverse=True)
        ans = max(ans, sum(a[:m]))
print(ans)


Measured memory_bytes: 3316
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int main() {
    int N,MAX=0,SUM=0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int L;
        cin >> L;
        MAX = max(MAX, L);
        SUM += L;
    }
    if (SUM > 2 * MAX) { cout << "Yes\n"; }
    else { cout << "No\n"; }


    return 0;
}

Measured memory_bytes: 256
#include <cstdio>

int main()
{
    int P, Q, R;
    scanf("%d %d %d", &P, &Q, &R);
    int ans;
    if (P < Q) {
        if (Q < R) {
            ans = P + Q;
        } else {
            ans = P + R;
        }
    } else {
        if (P < R) {
            ans = P + Q;
        } else {
            ans = Q + R;
        }
    }

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


Measured memory_bytes: 128
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int x,cnt,ans,p,k,t;
int main()
{
	scanf("%d %d",&x,&t);
	
	if(x <= t)
		printf("0\n");
	else
		printf("%d\n",x-t);
	return 0;
}

Measured memory_bytes: 256
/*
confirm 0LL and 1LL
confirm cornercases such as 0
confirm times of cin < 10^6
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
template <typename T>
using Vec1 = vector<T>;
template <typename T>
using Vec2 = vector<Vec1<T> >;
#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for(ll (i) = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for(ll (i) = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for(ll (i) = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <typename T>
bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;}
template <typename T>
ll pow2(const T n){return (1LL << n);}
template <typename T>
void cosp(const T n){cout << n << ' ';}
void co(void){cout << '\n';}
template <typename T>
void co(const T n){cout << n << '\n';}
template <typename T1, typename T2>
void co(pair<T1, T2> p){cout << p.fs << ' ' << p.sc << '\n';}
template <typename T>
void co(const Vec1<T> &v){for(T i : v) cosp(i); co();}
template <typename T>
void co(initializer_list<T> v){for(T i : v) cosp(i); co();}
template <typename T>
void ce(const T n){cerr << n << endl;}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
constexpr int INF = 1e9+1;
constexpr ll LINF = 1e18+1;
constexpr ll MOD = 1e9+7;
//constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-11;
const ld PI = acos(-1);

bool g(ll n, ll k){
	while(n % k == 0) n /= k;
	n -= n / k * k;
	return n == 1;
}

Vec f(ll n){
	Vec res;
	res.pb(n);
	REP(i, 2, n - 1){
		if(n % i == 0){
			if(g(n / i, i)) res.pb(i);
			if(g(i, n / i)) res.pb(n / i);
		}
		if(i * i >= n) break;
	}

	REP(i, 1, n - 1){
		if((n - 1) % i == 0){
			if(i != 1) res.pb(i);
			if((n - 1) / i != 1) res.pb((n - 1) / i);
		}
		if(i * i >= n) break;
	}

	return res;
}

int main(void){
	ll n;
	cin >> n;

	Vec ans = f(n);

	unordered_set<ll> ust;
	for(ll i : ans) ust.insert(i);
	co(ust.size());

	return 0;
}


Measured memory_bytes: 256
constexpr bool isDebugMode = true;
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b , a % b) : b;}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }

ll gv;
ll op(ll u, ll v){return max(u, v);};
ll e(){return -INF;}
bool f(ll x){return x < gv;}

int main(){
    ll N, Q; cin >> N >> Q;
    vector<ll> a(N);
    rep(i, N) cin >> a[i];
    vector<ll> ans;
    segtree<ll ,op, e> seg(a);
    rep(i, Q){
        ll q; cin >> q;
        if(q == 1){
            ll x, v; cin >> x >> v;
            seg.set(--x, v);
        }else if(q == 2){
            ll x, v; cin >> x >> v;
            ans.push_back(seg.prod(--x, v));
        }else{
            ll x, v; cin >> x >> v;
            gv = v;
            ans.push_back(seg.max_right<f>(--x) + 1);
        }
    }
    debug(ans);
    return 0;
}

Measured memory_bytes: 11324
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
#define inf 1000000007

int main(void){
    int a, b;
    cin >> a >> b;
    int ans = max({a+b, a-b, a*b});
    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 764
# include <stdio.h>

int main(){
  int n;
  scanf("%d", &n);
  
  int s1, s2, s3, s4;
  s1 = n/1000;
  s2 = (n/100)%10;
  s3 = (n/10)%10;
  s4 = n%10;
  if((s1==s2 && s2==s3)||(s2==s3 && s3==s4)){
    printf("%s\n","Yes");
  }else{
    printf("%s\n","No");
  }
  return 0;
}

Measured memory_bytes: 128
from heapq import heappush, heappop, heapify

N, M = list(map(int, input().split()))
A = [-a for a in list(map(int, input().split()))]
heapify(A)

for i in range(M):
    a = heappop(A)
    heappush(A, a/2)

print(-sum(list(map(int, A))))

Measured memory_bytes: 14180
import sys
from collections import deque

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

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


INF = 10**18


def bfs(N, v0, goal, edge):

    dp = [[-1 for _ in range(3)] for _ in range(N)]
    dp[v0][0] = 0
    q = deque()
    q.append((v0, 0))

    while q:
        v, cnt = q.popleft()

        if cnt == 2:
            nx_cnt = 0
        else:
            nx_cnt = cnt + 1

        for nv in edge[v]:
            if dp[nv][nx_cnt] == -1:
                q.append((nv, nx_cnt))
                if cnt == 0:
                    dp[nv][nx_cnt] = dp[v][cnt] + 1
                else:
                    dp[nv][nx_cnt] = dp[v][cnt]
        # print(v, dp)

    return dp[goal][0]


def main():

    N, M = in_nn()
    edge = [[] for _ in range(N)]

    for i in range(M):
        u, v = in_nn()
        u, v = u - 1, v - 1
        edge[u].append(v)

    S, T = in_nn()
    S, T = S - 1, T - 1
    ans = bfs(N, S, T, edge)

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 97632
#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 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/10/06  Problem: abc112 C / Link: __CONTEST_URL__  ----- */
/* ------問題------



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



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

using cs = const vector<int>&;
PLL solve(int CX, int CY, cs x, cs y, cs h) {
	int N = SZ(x);
	LL H = -1;
	FOR(i, 0, N) {
		if (H == -1 && h[i]) {
			H = abs(CX - x[i]) + abs(CY - y[i]) + h[i];
		}
	}
	assert(H != -1);
	bool flag = 1;
	FOR(i, 0, N) {
		LL Hs = max(H - (abs(CX - x[i]) + abs(CY - y[i])), 0LL);
		if (Hs != h[i])flag = 0;
	}
	return PLL(H, flag);
}


int main() {

	int N; cin >> N;
	vector<int> x(N), y(N), h(N);
	FOR(i, 0, N) {
		cin >> x[i] >> y[i] >> h[i];
	}

	FOR(xc, 0, 101) {
		FOR(yc, 0, 101) {
			PLL res = solve(xc, yc, x, y, h);
			if (res.second) {
				cout << xc << " " << yc << " " << res.first << endl;
				return 0;
			}
		}
	}

	return 0;
}


Measured memory_bytes: 256
#include <iostream>

int main() {
    int N; std::cin >> N;
    int ret = 0;
    for (int i = 0; i < N; i++) {
        int l, r;
        std::cin >> l >> r;
        ret += (r - l + 1);
    }

    std::cout << ret << std::endl;
    return 0;
}

Measured memory_bytes: 256
fun main(args: Array<String>) {
    val N = readInputLine().toInt()
    val edges = Array(N + 1) { mutableListOf<Int>() }

    repeat(N - 1) {
        val (a, b) = readInputLine().split(" ").map { it.toInt() }
        edges[a].add(b)
        edges[b].add(a)
    }

    val redNodes = mutableListOf<Int>()
    val blueNodes = mutableListOf<Int>()

    fun dfs(isRed: Boolean, node: Int, parent: Int) {
        if (isRed) {
            redNodes.add(node)
        } else {
            blueNodes.add(node)
        }

        for (e in edges[node]) {
            if (e == parent) {
                continue
            }
            dfs(!isRed, e, node)
        }
    }

    dfs(false, 1, 0)

    val ans = IntArray(N)

    when {
        redNodes.size > N / 3 && blueNodes.size > N / 3 -> {
            var oneNodeCnt = 0
            var twoNodeCnt = 0
            var threeNodeCnt = 0
            for (r in redNodes) {
                if (oneNodeCnt * 3 < N) {
                    ans[r - 1] = oneNodeCnt++ * 3 + 1
                } else {
                    ans[r - 1] = ++threeNodeCnt * 3
                }
            }
            for (b in blueNodes) {
                if (twoNodeCnt * 3 + 1 < N) {
                    ans[b - 1] = twoNodeCnt++ * 3 + 2
                } else {
                    ans[b - 1] = ++threeNodeCnt * 3
                }
            }
        }
        redNodes.size <= N / 3 -> {
            var threeNodeCnt = 0
            var otherNodeCnt = 0
            for (r in redNodes) {
                ans[r - 1] = ++threeNodeCnt * 3
            }
            for (b in blueNodes) {
                if (threeNodeCnt * 3 + 2 < N) {
                    ans[b - 1] = ++threeNodeCnt * 3
                } else {
                    ans[b - 1] = otherNodeCnt / 2 * 3 + otherNodeCnt % 2 + 1
                    otherNodeCnt++
                }
            }
        }
        else -> {
            var threeNodeCnt = 0
            var otherNodeCnt = 0
            for (b in blueNodes) {
                ans[b - 1] = ++threeNodeCnt * 3
            }
            for (r in redNodes) {
                if (threeNodeCnt * 3 + 2 < N) {
                    ans[r - 1] = ++threeNodeCnt * 3
                } else {
                    ans[r - 1] = otherNodeCnt / 2 * 3 + otherNodeCnt % 2 + 1
                    otherNodeCnt++
                }
            }
        }
    }

    println(ans.joinToString(" "))
}

fun readInputLine(): String {
    return readLine()!!
}


Measured memory_bytes: 118436
n=gets.to_i
button=n.times.map{gets.to_i}
count=1
last=button[0]
if last==2
    puts count
    exit
end
1000000.times.each do
    last=button[last-1]
     count+=1
     
    if last==2
    puts count
    exit
    end
   
end
puts -1

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

using namespace std;

int main() {
    int n,k,a[10000];
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int sum=0;
    sort(a,a+n);
    for(int i =0;i<k;i++)
    {
        sum +=a[i];
    }
    cout<<sum<<endl;
}


Measured memory_bytes: 3648
#include <iostream>
#include <vector>
#include <string>

using namespace std;

#define all(x) (x).begin(),(x).end()

int main() {
    int h,w;
    cin >> h >> w;
    vector<string> a(h);
    vector<bool> disw(w,false);

    for (int i=0; i<h; i++) cin >> a[i];

    auto c = [](const char& c){return c == '.';};
    for (int i=h-1; i>=0; i--) {
        if (all_of(all(a[i]), c)) {
            a.erase(a.begin()+i);
        }
    }

    h = a.size();

    for (int i=0; i<w; i++) {
        bool f = true;
        for (int j=0; j<h; j++) {
            if (a[j][i] == '#') f = false;
        }
        disw[i] = f;
    }

    for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
            if (!disw[j])
                cout << a[i][j];
        }
        cout << endl;
    }
}

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

int main(){
  int n,k;
  char s[60];
  cin >> n >> k;
  for (int i =0;i<n;i++) cin >> s[i];
  if (s[k-1] == 'A') s[k-1] = 'a';
  if (s[k-1] == 'B') s[k-1] = 'b';
  if (s[k-1] == 'C') s[k-1] = 'c';
  for (int i =0;i<n;i++) cout << s[i];
}

Measured memory_bytes: 256
import java.util.*;

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

        long n = sc.nextLong();

        System.out.println(base26(n));

    }

    static String base26(long n) {
        StringBuilder buf = new StringBuilder();
        while (n != 0) {
            buf.append((char) ((n - 1) % 26 + 'a'));
            n = (n - 1) / 26;
        }
        return buf.reverse().toString();
    }

}


Measured memory_bytes: 35624
a,b,c,d=map(int, input().split())
import fractions, math
e=c*d//fractions.gcd(c,d)
cx=b//c
dx=b//d
ex=b//e

cy=(a-1)//c
dy=(a-1)//d
ey=(a-1)//e


print(b-cx-dx+ex -a+cy+dy-ey+1)

Measured memory_bytes: 5104
#include <stdio.h>

int main(){
	int x,y,z,ary[5][4][11];
	for (x=1;x<5;x++){
		for (y=1;y<4;y++){
			for (z=1;z<11;z++){
			ary[x][y][z]=0;
			}
		}
	}
	int i,n,b,f,r,v;
	scanf ("%d",&n);
	for (i=0;i<n;i++){
		scanf ("%d %d %d %d",&b,&f,&r,&v);
		if (ary[b][f][r]==0){
		ary[b][f][r]=v;
		}
		else {
		ary[b][f][r]=ary[b][f][r]+v;
		}
	}
	int a=0,c=0;
	for (x=1;x<5;x++){
		for (y=1;y<4;y++){
			for (z=1;z<11;z++){
				printf (" %d",ary[x][y][z]);
			}
			if (c!=12){
			printf ("\n");
				c++;
			}
		}
		if (a!=3){
			printf ("####################\n");
			a++;
		}
	}
	return 0;
}

Measured memory_bytes: 612
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define fr first
#define se second
#define ull unsigned long long
#define getbit(x,i) (x>>i)&1

void solve(){
    ll n;cin>>n;
    vector<ll> a(n);
    ll sum=0;
    map<ll,ll> mp;
    for(ll i=0;i<n;i++){
        cin>>a[i];
        sum+=a[i];
        mp[a[i]]++;
    }
    ll q;cin>>q;
    while(q--){
        ll b,c;cin>>b>>c;
        ll temp = mp[b];
        sum-=b*temp;
        mp[b]=0;
        sum+=temp*c;
        mp[c]+=temp;
        cout<<sum<<endl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll qu=1;
    // cin>>qu;
    for(ll cas=1;cas<=qu;cas++){
        // cout<<"Case #"<<cas<<": ";
        solve();
    }
}

Measured memory_bytes: 10092
H, W = map(int, input().split())
field = [input() for i in range(H)]
verti_lamp = [[0]*W for i in range(H)]
horiz_lamp = [[0]*W for i in range(H)]


def verti_search(y, x):
    count = 0
    down = 1
    while y+down <= H-1 and field[y+down][x] =='.':
        count += 1
        down += 1
    y += down
    return count, y


def horiz_search(y, x):
    count = 0
    right = 1
    while x+right <= W-1 and field[y][x+right] =='.':
        count += 1
        right += 1
    x += right
    return count, x


for y in range(H):
    x = 0
    while x <= W-1:
        if field[y][x] == '#':
            x += 1
            continue
        else:
            horiz, nx = horiz_search(y, x)
            for t in range(x, nx):
                horiz_lamp[y][t] = horiz
            x = nx

for x in range(W):
    y = 0
    while y <= H-1:
        if field[y][x] == '#':
            y += 1
            continue
        else:
            verti, ny = verti_search(y, x)
            for t in range(y, ny):
                verti_lamp[t][x] = verti
            y = ny

ans = 0
for y in range(H):
    for x in range(W):
        candidate = verti_lamp[y][x] + horiz_lamp[y][x] + 1
        ans = max(ans, candidate)

print(ans)


Measured memory_bytes: 123352
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);
  int a; cin >> a;
  string s; cin >> s;
  if(a >= 3200) cout << s << endl;
  else cout << "red" << endl;
}


Measured memory_bytes: 256
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A,B=[0],[0]
for i in range(n):
    A.append(A[i]+a[i])
for i in range(m):
    B.append(B[i]+b[i])
ans,j = 0,m
for i in range(n+1):
    if A[i]>k:
        break
    while B[j] >k-A[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)

Measured memory_bytes: 47348