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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())

// {{{
template<typename T>
void printmatrix(vector<vector<T> > ar){
    for (int i = 0; i < (int)ar.size(); i++) {
        for (int j = 0; j < (int)ar[0].size(); j++) {
            cout << ar[i][j];
        }
        cout << endl;
    }
}

template<typename T>
void printvector(vector<T> vec){
    for (int i = 0; i < (int)vec.size(); i++) {
        cout << vec[i] << endl;
    }
}

template<typename T>
void unique(vector<T>& vec){
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

template<typename T>
vector<T> cumsum(vector<T> vec){
    vector<T> newvec((T)vec.size());
    int tmp = 0;
    for (int i = 0; i < (int)vec.size(); i++) {
        tmp += vec[i];
        newvec[i] = tmp;
    }

    return newvec;
}

template<typename T>
T digitsum(T x){
    T y=0;
    while(x){
        y += x % 10;
        x /= 10;
    }

    return y;
}
// }}}
/* vim:set foldmethod=marker: */

int main(int argc, char const* argv[])
{
    int h,w;
    cin >> h >> w;

    vector<vector<char> > board(h+2, vector<char>(w+2 ,'.'));
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            cin >> board[i][j];
        }
    }

    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            int c=0;
            if (board[i][j] == '.') {
                for (int k = -1; k <= 1; k++) {
                    for (int l = -1; l <= 1; l++) {
                        if (board[i+k][j+l] == '#') {
                            c++;
                        }
                    }
                }
                board[i][j] = to_string(c)[0];
            }
        }
    }


    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            cout << board[i][j];
        }
        cout << endl;
    }

    // printmatrix(board);

    return 0;
}

// sortの仕方
// int a[N];
// array<int,N> ar;
// vector<int> v;
//
// sort(a, a+N); // 配列
// sort(ar.begin(), ar.end()); // array
// sort(v.begin(), v.end()); // vector 昇順
// sort(v.begin(), v.end(), greater<int>()); // 降順

// Template
// template <class T>
// T foomax(T x, T y){
//   if (x > y){
//     return x;
//   }else{
//     return y;
//   }
// }

// string を int に変換 stoi("1010")
// int を string に変換 to_string(1010)


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

int main(){
    ll n;
    cin>>n;
    ll a[100000] = {};
    ll b[100000] = {};
    ll count=0;
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n);
    for(ll i=1;i<n;i++)
    {
        if(a[i] == a[i-1])
        {
            b[i] = a[i];count++;
        }
    }
    if(count%2 == 1)count++;
    cout<<n-count;


}

Measured memory_bytes: 1024
def judge(left, right):
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True


s = input()
n = len(s)

if judge(0, n-1) and judge(0, (n-1)//2-1) and judge((n+3)//2-1, n-1):
    print("Yes")
else:
    print("No")

Measured memory_bytes: 39824
class UnionFind:
    def __init__(self, N):
        self.root = list(range(N + 1))
        self.size = [1] * (N + 1)

    def __getitem__(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x

    def merge(self, x, y):
        x = self[x]
        y = self[y]
        if x == y:
            return
        sx, sy = self.size[x], self.size[y]
        if sx < sy:
            x, y = y, x
            sx, sy = sy, sx
        self.root[y] = x
        self.size[x] += sy

    def same(self,x,y):
        return  self[x] == self[y]

    def find_max(self):
        return max(self.size)


N,Q = map(int, input().split())

uf = UnionFind(N)
merge = uf.merge
same = uf.same

for i in range(Q):
    t,a,b = map(int, input().split())
    if t:
        print(int(same(a,b)))

    else:
        merge(a,b)


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

using namespace std;

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5 + 5;

int N;
int prv[MAXN];
int arr[MAXN];

int main(){
    cin.tie(0);
    cin.sync_with_stdio(0);

    cin >> N;

    arr[0] = -1;

    for(int a = 1, last = 0; a <= N; a++){
        cin >> arr[a];
        prv[a] = last;
        if(arr[a] != 0){
            last = a;
        }
    }

    ll ans = 0;
    for(int a = 1; a <= N; a++){
        int curv = arr[a] | (1 << 30);
        int cur = a;

        int lbnd;
        while(true){
            if(arr[prv[cur]] & curv){
                lbnd = prv[cur] + 1;
                break;
            } else {
                curv |= arr[prv[cur]];
                cur = prv[cur];
            }
        }
        ans += a - lbnd + 1;
    }
    cout << ans;
}


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

int main()
{
	string s;
	cin>>s;

	int cnt = 0,ans=0;
	for(int i = 0; i<3; i++)
	{
		if(s[i]=='R')
			cnt++;
		else
			cnt = 0;
		ans = max(ans,cnt);
	}
	cout<<ans<<endl;

	
	return 0;
}

Measured memory_bytes: 3588
use proconio::{input, fastout};
use proconio::marker::Usize1;
// use itertools::Itertools;

#[fastout]
fn main() {
  input! {
    n: usize,
    mut k: u64,
    a: [Usize1; n],
  };
  let mut visitted = vec![None; n];
  let mut i = 0;
  let mut counter = 0;
  while visitted[i].is_none() {
    visitted[i] = Some(counter);
    i = a[i];
    counter += 1;
    k -= 1;
    if k == 0 {
      println!("{}", i+1);
      return;
    }
  }
  let lp = counter - visitted[i].unwrap();
  k = k % lp;
  // dbg!(lp, k, &visitted, counter, i);
  for _ in 0..k {
    i = a[i];
  }
  println!("{}", i+1);
}


Measured memory_bytes: 7980
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


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


def resolve():
    a_list = list(map(int, input().split()))

    for i, num in enumerate(a_list):
        if num == 0:
            print(i+1)



if __name__ == "__main__":
    resolve()



Measured memory_bytes: 61652
// Date: 2020-03-01

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

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define FIO ios::sync_with_stdio(false);cin.tie(0)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {LL a=1; while(y){ if(y&1) a=a*x%MOD; x=x*x%MOD; y>>=1; } return a;}

int n, a[N];

int main() {
    scanf("%d", &n);
    rep(i, 1, n) scanf("%d", &a[i]);
    sort(a+1, a+1+n);
    int ans = inf;
    rep(b, a[1], a[n]) {
        int tp = 0;
        rep(i, 1, n) tp += (b-a[i])*(b-a[i]);
        chkmin(ans, tp);
    }
    printf("%d\n", ans);

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>

int main(void){
    int a,b,c,i;
    int count = 0;
    scanf("%d %d %d\n", &a, &b, &c);
    
    for(i=a; i<=b; i++){
        if(c%i ==0) count++;
    }
    
    printf("%d\n", count);

    return 0;
    
}

Measured memory_bytes: 600
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <numeric>
#include <array>
using namespace std;
//変数デバッグ
#define DEB(variable) cout << #variable << '=' << variable << endl

//for簡易表記(引数ミス防止)
#define FOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin); (LoopVariable) < (numberOFend); (LoopVariable)++)
#define DEFOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin); (LoopVariable) > (numberOFend); (LoopVariable)--)
#define REP(LoopVariable,numberOFend) for(int LoopVariable = 0;(LoopVariable)<(numberOFend);LoopVariable++)


int main(){

    int n;
    cin>>n;

    array<array<int, 100001>, 2> grid{};
    int tmp;
    array<pair<int,int>,2> max_save{make_pair(-1,0),make_pair(-1,0)};//first_maxは何 second_max_num
    REP(i,n){
        cin >> tmp;
        grid[i & 1][tmp]++;
        if(max_save[i&1].second<grid[i & 1][tmp]){
            max_save[i & 1].first = tmp;
            max_save[i & 1].second = grid[i & 1][tmp];
        }
    }
    sort(grid[0].begin(), grid[0].end(), greater<int>());
    sort(grid[1].begin(), grid[1].end(), greater<int>());
    if(max_save[0].first!=max_save[1].first){
        cout << n - max_save[0].second - max_save[1].second << endl;
    }else{
        cout << min(n - grid[0][0] - grid[1][1], n - grid[0][1] - grid[1][0]) << endl;
    }
    return 0;
}

Measured memory_bytes: 1656
#include<iostream>

using namespace std;

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

	string map[h];

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

	for(int i=1;i<=h;i++)
	{
			cin >> map[i-1];
	}

	
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			int count = 0;

			if(map[i-1][j-1]!='#')
			{
				for(int d=1;d<=8;d++)
				{
					int nx = i + dx[d-1];
					int ny = j + dy[d-1];

					if(nx<1 || nx>h || ny<1 || ny>w)
						continue;
					if(map[nx-1][ny-1]=='#')
						count++;
				}

				map[i-1][j-1]='0'+count;
			}
		}
	}

	for(int i=1;i<=h;i++)
			cout << map[i-1] << endl;
}


Measured memory_bytes: 256
#pragma GCC optimize "03"
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
 
const int N = 1e4 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

int dp[N][102][2];
 
signed main() {
	IOS;
	#ifdef LOCAL
		freopen("input.txt","r", stdin);
		freopen("output.txt","w",stdout);
	#endif
	string k;	int d;
	cin >> k >> d;
	dp[0][0][1] = 1;
	for(int i = 0; i < (int)k.length(); i++){
		for(int x = 0; x < 2; x++){
			for(int d1 = 0; d1 < d; d1++){
				if(x == 0){
					for(int j = 0; j < 10; j++)
						dp[i+1][(d1 + j) % d][x] = (dp[i+1][(d1 + j) % d][0] + dp[i][d1][0]) % mod;
				}
				else{
					int v = k[i]-'0';
					for(int j = 0; j < v; j++)
						dp[i+1][(d1 + j) % d][0] = (dp[i+1][(d1 + j) % d][0] + dp[i][d1][1]) % mod;
					dp[i+1][(d1 + v) % d][1] = (dp[i+1][(d1 + v) % d][1] + dp[i][d1][1]) % mod;
				}
			}
		}
	}
	cout << (dp[k.length()][0][0] + dp[k.length()][0][1] - 1 + mod) % mod;
	return 0;
}

Measured memory_bytes: 8192
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin>>N;
  vector<pair<int,int>>S(N);
  int sum=0;
  for(int i=0;i<N;i++){
    cin>>S.at(i).first;
    if(S.at(i).first%10==0){
      S.at(i).second=1;
    }
    else{
      S.at(i).second=0;
    }
    sum=sum+S.at(i).first;
  }
  if(sum%10!=0){
    cout<<sum<<endl;
  }
  else{
    vector<int>SS;
    for(int i=0;i<N;i++){
      if(S.at(i).second==0){
        SS.push_back(S.at(i).first);
      }
    }
    if(SS.size()==0){
      cout<<0<<endl;
    }
    else{
      sort(SS.begin(),SS.end());
      cout<<sum-SS.at(0)<<endl;
    }
  }
}
    
    


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

// See debug.cc for usage etc
#define DEBUG 0

template <class... Args>
string dbgFormat(const char* fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}

#if DEBUG
  #define DLOG(...)        cerr << dbgFormat(__VA_ARGS__) << endl
  #define DCALL(func, ...) func(__VA_ARGS__)
#else
  #define DLOG(...)
  #define DCALL(func, ...)
#endif

int main() {
#if !DEBUG
  ios_base::sync_with_stdio(false);
#endif
  cout << setprecision(20);

  ll N, L;
  cin >> N >> L;

  ll sum = (2 * L + N - 1) * N / 2;
  ll answer;
  if (0 < L) {
    answer = sum - L;
  }else if (L + N - 1 < 0) {
    answer = sum - (L + N - 1);
  }else {
    answer = sum;
  }
  cout << answer << endl;

  return 0;
}



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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll MOD = 1e9 + 7;

ll n, m;
ll pos = 1;

map<int, int> r, c;

int main() {
	cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    rep(i, 0, n) {
        int a;
        cin >> a;
        r[a] = i;
    }

    rep(i, 0, m) {
        int b; cin >> b;
        c[b] = i;
    }
        
    ll rd, cd, ra, ca;
    rd = cd = ra = ca = 0;
    for (ll i = n * m; i >= 1; --i) {
        ll x = (r.count(i) ? r[i] : -1), y = (c.count(i) ? c[i] : -1);
        // Problem definitely lies within the single row/col condition
        if (x >= 0 && y >= 0) {
            ++rd, ++cd;
            ++ra, ++ca;
        } else if (x >= 0) {
            ++ra, ++rd;
            // pos *= ca--;
            pos *= cd;
            pos %= MOD;
            if (!pos) break;
        } else if (y >= 0) {
            ++ca, ++cd;
            // pos *= ra--;
            pos *= rd;
            pos %= MOD;
            if (!pos) break;
        } else {
            // # of avail intersections is rows disc * cols - used;
            // what happens if one of these cut off a square for another one? Actually that should be impossible.
            if (rd == 1 || cd == 1) {
                pos = 0;
                break;
            }
            ll avail = max(0ll, (rd * cd) - (n * m - i));
            pos *= avail;
            pos %= MOD;
            if (!pos) break;
        }
        // cout << x << ' ' << y << '\n';
        // cout << i << ' ' << pos << '\n';
    }

    cout << pos;
}


Measured memory_bytes: 3732
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,a=0,b=1;
    cin>>n>>m;
    a=n;b=m;
    for(int i=1;i<b;i++){
        n=n*10+a;
    }
    for(int i=1;i<a;i++)
        m=m*10+b;
    cout<<max(n,m)<<endl;
}


Measured memory_bytes: 256
#!/usr/bin/env python3
import sys


def solve(K: int):
    def num_divide(x):
        # x = 4 return 1, 2, 4
        d_ = []
        for i in range(1, int(x**0.5)+1):
            if x % i == 0:
                d_.append(i)
                if i != x//i:
                    d_.append(x//i)
        return d_

    d = [[0]]
    #make map
    for k in range(1, K+1):
        d.append(set(num_divide(k)))
    def gcd_ab(a, b):
        return max(d[a]&d[b])

    #make map_gcd_ab
    d_ab = [[0]*(K+1) for i in range(K+1)]
    for i in range(1, K+1):
        for j in range(i+1, K+1):
            d_ab[i][j] = gcd_ab(i, j)

    import itertools
    l = list(range(1, K+1))
    comb_abc_list = itertools.combinations_with_replacement(l, 3)

    ans = 0
    for v in comb_abc_list:
        a, b, c = v[0], v[1], v[2]
        v_set = set(v)
        if len(v_set) == 1:
            tmp = a
        elif len(v_set) == 2:
            va, vb = list(v_set)[0], list(v_set)[1]
            min_ab, max_ab = min(va, vb), max(va, vb)
            tmp = d_ab[min_ab][max_ab]
            tmp = 3*tmp
        else:
            min_ab, max_ab = min(a, b), max(a, b)
            gcd_of_ab = d_ab[min_ab][max_ab]
            min_ac, max_ac = min(gcd_of_ab, c), max(gcd_of_ab, c)
            tmp = d_ab[min_ac][max_ac]
            tmp = 6*tmp
        ans += tmp

    print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)

if __name__ == '__main__':
    main()


Measured memory_bytes: 9844
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
using namespace std;
long long MOD = 1e9 + 7;

string S;
int main() {
	cin >> S;
	int counter = 0;
	for (int n = 0; n < 3; n++) {
		if(S[n] == 'o')counter++;
	}
	cout << 700 + counter*100 << endl;
	return 0;
}

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

int main(){
  int a[101];
  int b[20];
  int n = 0;
  int i = 0;
  int max = 0;
  int min = 1000;
  int sum = 0;
  int counter = 0;
  while(1){
    max = 0;
    min = 1000;
    sum = 0;
    cin >> n;
    if(n==0) break;
    for(i=0;i<n;i++){
      cin >> a[i];
      if(max <= a[i]) max = a[i];
      if(min >= a[i]) min = a[i];
      sum += a[i];
    }
    b[counter] = (sum - max - min) / (n - 2);
    counter++;
  }
  for(i=0;i<counter;i++){
    cout << b[i] << endl;
  }
  return 0;
}

Measured memory_bytes: 2956
while true
    n, m = gets.chomp.split.map(&:to_i)
    break if [n, m] == [0, 0]

    records = Array.new(m + 1){[0, []]}
    r = gets.to_i
    r.times{
        time, pc, student, inout = gets.chomp.split.map(&:to_i)
        case inout
        when 0
            records[student][0] -= 1
            records[student][1][-1] << time if records[student][0] == 0
        when 1
            records[student][0] += 1
            records[student][1] << [time] if records[student][0] == 1
        end
    }

    q = gets.to_i
    q.times{
        startt, endt, student = gets.chomp.split.map(&:to_i)
        sum = 0
        records[student][1].each{|st, ed|
            times = (startt <= st) ? [startt, endt, st, ed] : [st, ed, startt, endt]
            sum += ([times[1], times[3]].min - times[2]) if times[1] > times[2]
        }
        puts sum
    }
end



Measured memory_bytes: 9012
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
using P = pair<int,int>;
const int MOD = 1000000007;

int main() {
    int x,t;
    cin>>x>>t;
    cout<<max(x-t,0)<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define INF 1e9
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main(){
	ll n;
	cin>>n;
	if(n%2==1){
		cout<<0<<endl;
		return 0;
	}

	ll res=0;
	n/=2;
	while(n){
		res+=n/5;
		n/=5;
	}
	cout<<res<<endl;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    int n;
    int k;
    int[] x;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        x = new int[n];
        for (int i = 0; i < n; i++) {
            x[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        int total = 0;
        for (int i = 0; i < n; i++) {
            int dist_b;
            if (x[i] > k) {
                dist_b = x[i] - k;
            } else {
                dist_b = k - x[i];
            }
            if (x[i] > dist_b) {
                total += dist_b * 2;
            } else {
                total += x[i] * 2;
            }
        }
        System.out.println(total);
    }
}

Measured memory_bytes: 23124
#include <stdio.h>

int main(void) {
	
int x,y;
while(1){
scanf("%d %d",&x,&y);
if( x==0 && y==0) break;
if(x<y){
	printf("%d %d\n",x,y);
}else{
	printf("%d %d\n",y,x);
}
}
	return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
using namespace std;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(j, n) for (int j = 0; j < (n); j++)
 
typedef long long ll;
 
int main()
{
	int  a,b,c,d;
	cin>>a>>b>>c>>d;
	cout<<min(a,b)+min(c,d)<<endl;
	return 0;
}

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

int main()
{
    int N;
    cin >> N;
    int A[N];
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
        if (A[i] % 2 == 0)
        { // if even number
            if (A[i] % 3 != 0 && A[i] % 5 != 0)
            {
                cout << "DENIED" << endl;
                return 0;
            }
        }
    }
    cout << "APPROVED" << endl;
    return 0;
}


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

#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(int i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define sz(x) ((int)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair
using ll  = long long;
using vi  = vector<int>;
using vvi = vector<vi>;
using vs  = vector<string>;
using pii = pair<int,int>;
constexpr ll inf = 1ll<<61;
constexpr ll mod = 1e9+7;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>void add(T &a, const T &b){a += b; a %= mod;}

int n;
vvi g;

pii bfs(int s){ // sを根としてbfs
  vi d(n,-1);
  queue<int> q;
  d[s] = 0;
  int maxd = 0;
  int pos = s;
  q.push(s);
  
  while(!q.empty()){
    int next = q.front();
    q.pop();
    
    rep(i, g[next].size()){
      int c = g[next][i];
      dump(c);
      
      if(d[c] == -1){
	d[c] = d[next] + 1;
	if(chmax(maxd,d[c])) pos = c;
	q.push(c);
      }
    }
  }
  dump(pos,maxd);
  return pii(pos,maxd);
}


signed main(){
  cin >> n;
  g.resize(n);
  rep(i,n-1){
    int a,b; cin >> a >> b; a--; b--;
    g[a].pb(b); g[b].pb(a);
  }
  pii a = bfs(0);
  dump(a);
  pii b = bfs(a.first);
  dump(b);
  if(b.second % 3 == 1) cout << "Second" << endl;
  else cout << "First" << endl;
}

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

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

mint dp[61][2][2][2];

int main() {
    ll l, r;
    cin >> l >> r;
    dp[60][0][0][0] = 1;
    // 10^^18は60桁くらい
    for (int i = 59; i >=0; i--) {
        // 1桁目を取り出す
        int lb = l>>i&1;
        int rb = r>>i&1;

        rep(j,2)rep(k,2)rep(s,2) { // preの状態
            mint pre = dp[i+1][j][k][s];
            rep(x,2)rep(y,2) {
                if (x && !y) continue; // x = 1, y = 0
                int nj = j, nk = k, ns = s; // nextの状態
                if (!s && x != y) continue;
                if (x && y) ns = 1;

                // j: L <= X
                if (!j && !x && lb) continue;
                if (x && !lb) nj = 1;

                // k: Y <= R
                if (!k && y && !rb) continue;
                if (!y && rb) nk = 1;

                dp[i][nj][nk][ns] += pre;
            }
        }
    }
    mint ans;
    rep(j, 2)rep(k, 2)rep(s, 2) ans += dp[0][j][k][s];
    cout << ans.x << endl;
}

Measured memory_bytes: 256
import sys
input=sys.stdin.readline
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=[]
for i in range(n):
    l.append(i+1)
import itertools
c=1
for v in itertools.permutations(l,n):
    if p==v:
        pc=c
    if q==v:
        qc=c
    c+=1
print(abs(pc-qc))








Measured memory_bytes: 39408
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, int> c;
    rep(i, 0, n){
        cin >> a[i];
        c[a[i]]++;
    }
    int odd = 0, even = 0;
    for(auto i : c){
        if(i.second % 2) odd++;
        else even++;
    }
    int ans = odd + even;
    if(even % 2) ans--;
    cout << ans << endl;
}

Measured memory_bytes: 7296
N = int(input())
answer=0
if N < 105:
    print(0)
else:
    for i in range(105,N+1,2):
        cnt = 0
        for j in range(1,i+1):
            if i % j == 0:
                cnt += 1
        if cnt == 8:
            answer +=1
    print(answer)

Measured memory_bytes: 3060
#include <stdio.h>
#include <string.h>

#define LEN 100005
typedef struct pp{
  char name[100];
  int t;
}P;


P Q[LEN];
int head,tail,n;

void enqueue(P x){
  Q[tail]=x;
  tail=(tail+1)%LEN;
}

P dequeue(){
  P x=Q[head];
  head=(head+1)%LEN;
  return x;
}

int min(int a,int b){
  if(a<b) return a;
  else return b;
}

int main()
{
  int elaps=0,c;
  int i,q;
  P u;
  scanf("%d %d",&n,&q);
  for(i=1;i<=n;i++){
    scanf("%s",Q[i].name);
    scanf("%d",&Q[i].t);
  }
  head=1,tail=n+1;
  while(head!=tail){
    u=dequeue();
    c=min(q,u.t);
    u.t-=c;
    elaps+=c;
    if(u.t>0) enqueue(u);
    else printf("%s %d\n",u.name,elaps);
  }
  return 0;
}
	  

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

ll h,w,n;
vector<pair<ll,ll>> obs;
ll fact[200001];
ll inv[200001];
ll pow(ll a,ll b)
{
  if(b==1||a==1)
  return a;
  ll res=pow(a,b/2);
  if(b&1)
  return (((res*res)%mod)*a)%mod;

  return (res*res)%mod;
}
ll inverse(ll x)
{
  return pow(x,mod-2);
}
void preprocess()
{
  fact[0]=1;
  inv[0]=1;
  for(int i=1;i<=200000;i++)
  {
    fact[i]=(fact[i-1]*i)%mod;
    inv[i]=inverse(fact[i]);
  }
}
ll nck(ll a,ll b)
{
  return (((fact[a]*(inv[b]%mod))%mod)*(inv[a-b]%mod))%mod;
}
bool cmp(pair<ll,ll>&a,pair<ll,ll>& b)
{
  return (a.first+a.second)<(b.first+b.second);
}
ll pathsbw(pair<ll,ll> a,pair<ll,ll> b)
{
  if(a.first<=b.first&&a.second<=b.second)
  {
    ll down=(b.second-a.second);
    ll right=(b.first-a.first);
    return nck(down+right,right);
  }
  return 0;
}
ll solve()
{
  sort(obs.begin(),obs.end(),cmp);
  ll ways=nck(h+w-2,h-1);
  vector<ll> pathtoobs;
  pathtoobs.push_back(pathsbw({1,1},obs[0]));
  for(int i=1;i<obs.size();i++)
  {
    ll temp=pathsbw({1,1},obs[i]);
    for(int j=i-1;j>=0;j--)
    {
      temp=(mod+temp-(pathtoobs[j]*(pathsbw(obs[j],obs[i])))%mod)%mod;
    }
    pathtoobs.push_back(temp);
  }
  for(int i=0;i<n;i++)
  {
    ways=(mod+ways-(pathtoobs[i]*(pathsbw(obs[i],{h,w})))%mod)%mod;
  }
  return ways;
}
int main()
{
  preprocess();
  cin>>h>>w>>n;
  obs.resize(n);
  for(auto& x: obs)
    cin>>x.first>>x.second;
  cout<<solve()<<endl;
  return 0;
}


Measured memory_bytes: 6836
#define _CRT_SECURE_NO_WARNINGS
#include"bits/stdc++.h"
#ifdef _DEBUG
#define DBG(n) n
#else
#define DBG(n)
#endif
#define INF         1e9
#define INFLL       1e18
#define EPS         1e-9
#define REP(i,n)    for(ll i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n)   for(ll i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n)   for(ll i=(n)-1;i>=0;--i)
#define REPR1(i,n)  for(ll i=(n);i>0;--i)
#define REPC(i,obj) for(auto i:obj)
#define ALL(obj)    (obj).begin(),(obj).end()
#define SETP(n)     cout << fixed << setprecision(n)
using namespace std;
using ll = long long;
template<typename T = ll>inline T in() { T ret; cin >> ret; return ret; }

signed main()
{
	string str;
	while (cin >> str) {
		REP(i, str.size()) {
			if (str[i] != '@') {
				cout << str[i];
			} else {
				REP(j, (int)(str[i + 1] - '0')) {
					cout << str[i + 2];
				}
				i += 2;
			}
		}
		cout << endl;
	}
}


Measured memory_bytes: 3020
function main(arg) {
    var tmp = arg.split("\n");
    var N = parseInt(tmp[0]);
    var A = tmp[1].split(" ").map(n=>parseInt(n))[0];
    var B = tmp[1].split(" ").map(n=>parseInt(n))[1];
    var points = tmp[2].split(" ").map(n=>parseInt(n));
    var Q = [];
    
    Q.push(points.filter(i=> i <= A).length);
    Q.push(points.filter(i=> A < i && i <= B).length);
    Q.push(points.filter(i=> B < i).length);
    
    console.log(Math.min(...Q));
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));


Measured memory_bytes: 21192
/**
 *    Author:   ElProfesor
 *	       
**/

#include<bits/stdc++.h>

using namespace std;
 
// Competetive Template:
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ldb;
typedef vector <lli> vl;
typedef vector <string> vs;
typedef vector < pair <long, long> > vll;

// Debugging :
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ba  back
#define si size()
#define be begin()
#define en end()
#define le length()
#define mp make_pair 
#define mt make_tuple
#define fi first
#define se second
#define gcd __gcd
#define maxe *max_element
#define mine *min_element
#define deb debug
 
#define forz(i,n) for(lli i=0; i<n; i++)
#define rep(i,k,n) for (lli i = k; i < n; i++)
#define deci(n)  fixed<<setprecision(n)
#define high(n) __builtin_popcount(n)
#define parity(n) __builtin_parity(n)
#define ctz(n)  __builtin_ctz(n)
#define mod 1000000007
#define mod2 998244353
#define kira ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

// Printing Data :
#define pvec(v) forz(i,v.si) cout<<v[i]<<" "; cout<<endl;
#define parr(arr, n) forz(i,n) cout<<arr[i]<<" "; cout<<endl;
#define p1(i) cout<<i<<endl;
#define p2(i, j) cout<<i<<" "<<j<<endl;
#define p3(i, j, k) cout<<i<<" "<<j<<" "<<k<<endl;
#define p4(i, j, k, l) cout<<i<<" "<<j<<" "<<k<<" "<<l<<endl;

typedef pair <lli,lli> pll;
 
// CODE BEGINS :

int main()
{
    kira; 

    lli x; cin>>x;
    lli curr=100;

    lli time=0;

    while(curr<x)
    {
        curr = (lli)(curr*(ldb)1.01);
        time++;
    }

    cout<<time<<endl;

    return 0;
}




















































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

int main(){
  int N;
  int count = 0;
  cin >> N;
  int i,c,j;
  for(i = 1;i <= N;i+=2,c = 0){
    for(int j = 1;j <= i;j++){
      if(i%j==0)
        c++;
    }
    if(c == 8)
      count++;
  }
  cout << count << endl;
  return 0;
}

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


void solve(long long N, std::vector<long long> A){
    ll dp[N] = {};
    dp[0] = A[0];
    dp[1] = A[1];
    if(N%2 ==0){
        
        for(int i=1;i<N/2;i++){
            dp[i*2] = dp[i*2-2] + A[i*2];
            dp[i*2+1] = max(dp[i*2-1] + A[i*2+1],dp[i*2-2]+A[i*2+1]);
        }
        cout << max(dp[N-1],dp[N-2]) << endl;
        return;
    }else{
        dp[2] = A[2];
        for(int i=1;i <N/2;i++){
            ll temp =  max(dp[i*2-1],dp[i*2-2]);
            dp[i*2 +2] = max(temp,dp[i*2])+ A[i*2+2];
            dp[i*2 +1] = temp + A[i*2 +1];
            dp[i*2] = dp[i*2-2] + A[i*2];
        }
        ll ans = max(dp[N-1],dp[N-2]);
        cout << max(ans,dp[N-3]) << endl;
    }

}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}


Measured memory_bytes: 6448
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define COUT(x) cout<<(x)<<endl
#define dump(x)  cout << #x << " = " << (x) << endl;
using ll = long long;
using P = pair<int,int>;
using Graph = vector<vector<int>>;
using M = map<int,int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

int main() {
  int a,b,c,d,e,f;
  cin >> a >> b >> c >> d >> e >> f;
  double maxc = 0.0;
  int ansSuger=0,ansWater=0;
  for(int i=0; 100*a*i<=f; i++){
    for(int j=0; 100*a*i+100*b*j<=f; j++){
      int water=100*a*i+100*b*j;
      if (water == 0) continue;

      for(int k=0; water+c*k<=f; k++){
        for(int l=0; water+c*k+d*l<=f; l++){
          int suger = c*k+d*l;
          int weight = water+suger;
          if (water/100*e < suger) break; // 溶け残る
          double con = 100.0 * suger / weight;
          if (maxc <= con) {
            maxc = con;
            ansWater = water;
            ansSuger = suger;
          }
        }
      }
    }
  }
  cout << ansWater+ansSuger << " " << ansSuger << endl;
  return 0;
}

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

int main(void)
{
    ll N, i, j, dp[100001];

    cin >> N;

    for(i=0; i<=100000; i++){
        dp[i] = i;
    }

    for(i=0; i<N; i++){
        for(j=1; i+j<=N; j*=6){
            chmin(dp[i+j], dp[i]+1);
        }

        for(j=1; i+j<=N; j*=9){
            chmin(dp[i+j], dp[i]+1);
        }
    }
    
    cout << dp[N] << endl;

    return 0;
}

Measured memory_bytes: 1024
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        final int count = in.next().trim().length();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append("x");
        }
        System.out.println(sb);
    }
}


Measured memory_bytes: 21844
#include<stdio.h>
#define NIL -1
#define MAX 500000
int root = NIL;
int key[MAX];
int left[MAX];
int right[MAX];
//int T[MAX];
int p[MAX];
void tree_insert(int z){
  int y = NIL;
  int x = root;
  while(x!=NIL){
    y = x;
    if(key[z] < key[x])x = left[x];
    else x = right[x];
  }
  p[z] = y;
  if(y == NIL)root = z;
  else if(key[z]<key[y])left[y] = z;
  else right[y] = z;
  
}
void preorder(int t){
  if(t==NIL)return;
  printf(" %d",key[t]);
  preorder(left[t]);
  preorder(right[t]);
}

void inorder(int t){
  if(t==NIL)return;
  inorder(left[t]);
  printf(" %d",key[t]);
  inorder(right[t]);
}

void print(){
  inorder(root);
  printf("\n");
  preorder(root);
  printf("\n");
}

int tree_find(int t){
  int i=0;
  int y=NIL;
  int x=root;
  while(x!=NIL){
    if(key[x]==t)return x;
    y = x;
    if(t < key[x])x = left[x];
    else x = right[x];
  }
  return NIL;
}
///////////////////////////////////////////////////////
void delete(int z){
  int x,y;
  if(left[z]==NIL||right[z]==NIL)y=z;
  else y = tree_successor(z);
  
  if(left[y]!=NIL)x=left[y];
  else x=right[y];
  
  if(x!=NIL)p[x]=p[y];
  
  if(p[y]==NIL)root=x;
  else if(y==left[p[y]])left[p[y]]=x;
  else right[p[y]]=x;

  if(y!=z)key[z]=key[y];

}
////////////////////////////////////////////////////////
int tree_successor(int x){
  int y=NIL;
  if(right[x] != NIL)return tree_minimum(right[x]);
  y = p[x];
  while(y != NIL && x == right[y]){
    x = y;
    y = p[y];
  }
  return y;
}
int tree_minimum(int x){
  while(left[x] != NIL){
    x = left[x];
  }
  return x;
}

int main(){
  
  int i,n,x;
  int z = 0;
  char A[10];
  scanf("%d",&n);
  for(i=0;i<MAX;i++){
    key[i]=NIL;
    left[i]=NIL;
    right[i]=NIL;
    //   T[MAX]=NIL;
    p[i]=NIL;
  }
  for(i=0;i<n;i++){
    scanf("%s", A);
    if(A[0]=='i'){
      scanf("%d",&x);
      key[z]=x;
      tree_insert(z);
      z++;
    }
    else if(A[0]=='p')print();
    else if(A[0]=='f'){
       scanf("%d",&x);
       if(tree_find(x)!=NIL)printf("yes\n");
       else printf("no\n");
    }
    else if(A[0]=='d'){
      scanf("%d",&x);
      x=tree_find(x);
      delete(x);
    }
  }
  
  return 0;
}

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

int main() {
    int n, m;
    cin >> n >> m;
    double a[n];

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

    sort(a, a + n);


    double res = sum / (4.0 * m);

    /*
    cout << sum << endl;
    for(int i = 0; i < n; i++) cout << a[i] << ' ';
    cout << endl;
    cout << res << endl;
    */

    if(a[n - m] >= res) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define rgi register int
#define pii pair<int,int>
#define mkp make_pair
#define fi first
#define se second
#define pb push_back
#define filein(x) freopen(x,"r",stdin)
#define fileout(x) freopen(x,"w",stdout)
using namespace std;
const int maxn=85;
const int mx=81*81;
int n,m,a[maxn][maxn],b[maxn][maxn];
bool f[maxn][maxn][mx<<1];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(rgi i=1;i<=n;++i)
		for(rgi j=1;j<=m;++j)
			cin>>a[i][j];
	for(rgi i=1;i<=n;++i)
		for(rgi j=1;j<=m;++j)
			cin>>b[i][j];
	f[1][1][a[1][1]-b[1][1]+mx]=1,f[1][1][b[1][1]-a[1][1]+mx]=1;
	for(rgi i=1;i<=n;++i)
		for(rgi j=1;j<=m;++j)
			for(rgi k=0;k<(mx<<1);++k)
			{
				int t=a[i][j]-b[i][j];
				if(k+t<(mx<<1)&&k+t>=0)
					f[i][j][k+t]|=f[i-1][j][k],f[i][j][k+t]|=f[i][j-1][k];
				t=b[i][j]-a[i][j];
				if(k+t<(mx<<1)&&k+t>=0)
					f[i][j][k+t]|=f[i-1][j][k],f[i][j][k+t]|=f[i][j-1][k];
			}
	for(rgi i=mx,j=mx;i>=0&&j<mx*mx;--i,++j)
	{
		if(f[n][m][i])
		{
			cout<<mx-i<<endl;
			break;
		}
		if(f[n][m][j])
		{
			cout<<j-mx<<endl;
			break;
		}
	}
	return 0;
}


Measured memory_bytes: 89344
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int min(int a,int b){return (a>b)?b:a;};
int main(){
  int n,t,tmp,num,sum=0,v[10000]={},last;
  vector<int> node[10000];
  cin >> n;
  int a[n],b[n];
  priority_queue<int,vector<int>,greater<int>> c;
  for(int i=0;i<n-1;i++){
    cin >> a[i] >> b[i];
    a[i]--; b[i]--;
    node[a[i]].push_back(b[i]);
    node[b[i]].push_back(a[i]);
  }
  for(int i=0;i<n;i++){
    cin >> t;
    c.push(t);
  }
  while(!c.empty()){
    tmp = c.top();
    c.pop();
    if(c.empty()){
      last = tmp;
      break;
    }
    for(int i=0;i<n;i++){
      int cnt=0,id;
      for(int j=0;j<node[i].size();j++){
        if(node[i][j]!=-1){
          cnt++;
          id = j;
        }
      }
      if(cnt==1){
        v[i] = tmp;
        num = node[i][id];
        node[i][id]=-1;
        for(int j=0;j<node[num].size();j++){
          if(node[num][j]==i){
            node[num][j]=-1;
            break;
          }
        }
        break;
      }
    }
  }
  for(int i=0;i<n-1;i++){
    if(v[a[i]]==0) v[a[i]]=last;
    if(v[b[i]]==0) v[b[i]]=last;
    sum += min(v[a[i]],v[b[i]]);
  }
  cout <<sum << endl;
  for(int i=0;i<n;i++){
    cout << v[i] << " ";
  }
  cout << "\n";
}


Measured memory_bytes: 1152
#include <iostream>
#include<vector>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
bool solve(const vector<string> &w) {
	const int n = w.size();
	for (int i = 0; i < n-1; i++) {
		if (w[i].back() != w[i + 1].front()) {
			return 0;
		}

	}

	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; j++) {
			if (w[i] == w[j]) {
				return false;
			}
		}
	}

	return true;
}
int main() {
	int n;
	cin >> n;
	vector <string> w(n);
	for (int i = 0; i < n; i++) {
		cin >> w[i];
	}

	if (solve(w)) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

	return 0;




}

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

int a, b, c;

int main () {
	cin >> a >> b >> c ;
	if (a==b) {
		cout << c << endl;
	} else if (a==c) {
		cout << b << endl;
	} else {
		cout << a << endl;
	}
}

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

int main(){
  int n;
  cin >> n;
  vector<int> a(n);

  for (int i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  
  long long ans=0;
  int right=0,sum=0;
  for(int left=0;left<n;left++){
    while(right<n && (sum+a[right])==(sum^a[right])){
      sum+=a[right];
      right++;
    }
    ans+=right-left;
    sum-=a[left];
  }
  cout << ans << endl;  
}

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

using namespace std;

int N, A, B;
long long MOD = 1000000007;

long long modmult(long long a, long long b){
    return (a * b) % MOD;
}

long long modminus(long long a, long long b){
    if(a-b < 0){
        return a - b + MOD;
    } else {
        return a - b;
    }
}

long long modadd(long long a, long long b){
    return (a + b) % MOD;
}

long long fastpowmod(long long _x, long long _n)
{
	long long ret = 1;
	while(0 < _n)
	{
		if((_n % 2) == 0)
		{
			_x *= _x;
            _x %= MOD;
			_n >>= 1;
		}
		else
		{
			ret *= _x;
            ret %= MOD;
			--_n;
		}
	}
	return ret;
}

long long factmod[2*100000 + 1] = {0};
void factmod_init(){
    factmod[0] = 1;
    factmod[1] = 1;
    for(int i = 2; i <= 2 * 100000; i++){
        factmod[i] = modmult(factmod[i-1], i);
    }
}

// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= MOD;
    if (u < 0) u += MOD;
    return u;
}

long long factmod2(long long n){
    long long ret = 1;
    for(long long i = 1; i <= n; i++){
        ret *= i;
        ret %= MOD;
    }
    return ret;
}

long long complx(long long n, long long r){
    return modmult(modmult(factmod[n], modinv(factmod[r], MOD)), modinv(factmod[n-r], MOD));
}

long long nprmod(long long n, long long r){
    long long ret = 1;
    for(long long i = 0; i < r; i++){
        ret *= n - i;
        ret %= MOD;
    }
    return ret;
}

long long complx2(long long n, long long r){
    return modmult(nprmod(n, r), modinv(factmod[r], MOD));
}

int main(){
    factmod_init();
    cin >> N >> A >> B;
    cin.ignore();

    if(N <= 0) return 0;

    long long ans = fastpowmod(2, N);
    ans = modminus(ans, 1); // 1つもない場合を除く
    long long a_cmp = complx2(N, A);
    long long b_cmp = complx2(N, B);
    ans = modminus(modminus(ans, a_cmp), b_cmp);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin >>s;
  long long m=0,n=0;
  for(int i=0;i<s.size();i++){
    if(s[i]=='W'){
      m+=i-n;
      n++;
    }
  }
  cout << m << endl;
}

Measured memory_bytes: 640
fun main (args: Array<String>) {
  readLine()!!
  val a = readLine()!!.split(' ').map(String::toInt)
  val min = a.min()!!
  val max = a.max()!!
  val costs = mutableListOf<Int>()
  for (i in min..max) {
    costs.add(a.sumBy { Math.pow((it - i).toDouble(), 2.0).toInt() })
  }
  println(costs.min())
}

Measured memory_bytes: 38252
/* cerberus97 - Hanit Banga */

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int N = 1e2 + 10;

int b[N];

int main() {
	fast_cin();
	int n; cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> b[i];
	}
	vector<int> ops;
	for (; n >= 1; --n) {
		int p = 0;
		for (int i = 1; i <= n; ++i) {
			if (b[i] == i) {
				p = i;
			}
		}
		if (p == 0) {
			cout << -1 << endl;
			return 0;
		}
		ops.pb(p);
		for (int i = p; i <= n; ++i) {
			b[i] = b[i + 1];
		}
	}
	reverse(ops.begin(), ops.end());
	for (auto &i : ops) {
		cout << i << '\n';
	}
}

Measured memory_bytes: 256
use std::io::Read;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    let s = iterator.next().unwrap();
    let left: usize = s[0..2].parse().unwrap();
    let right: usize = s[2..4].parse().unwrap();

    if left >= 1 && left <= 12 && right >= 1 && right <= 12 {
        return "AMBIGUOUS".to_string();
    }
    if left >= 1 && left <= 12 {
        return "MMYY".to_string();
    }
    if right >= 1 && right <= 12 {
        return "YYMM".to_string();
    }
    return "NA".to_string();
}


Measured memory_bytes: 2168
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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

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

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
int A[10];

int main()
{
    rep(i, 0, 3)
    {
        cin >> A[i];
    }
    sort(A, A + 3, greater<int>());
    cout << A[0] * 10 + A[1] + A[2] << endl;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void) {
	int n;
	int nn;
	int a,b,cd;
	long long count;
	while(scanf("%d",&n)==1) {
		nn=(n<=1000?n:1000);
		count=0;
		for(a=0;a<=nn;a++) {
			for(b=0;b<=nn;b++) {
				cd=n-a-b;
				if(cd<0)break;
				else if(cd>2000)continue;
				if(cd<=1000)count+=cd+1;
				else {
					int over=cd-1000;
					count+=1000-over+1;
				}
			}
		}
		printf("%lld\n",count);
	}
	return 0;
}

Measured memory_bytes: 596