akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <algorithm>
using namespace std;

int compare(int &a, int &b){
    if(a%10 == 0){
        return -1;
    }
    return a%10 > b%10;
}


int main(){
    int lst[5];
    for(int i=0;i<5;i++){
        cin >> lst[i];
    }
    int x = 0;
    sort(lst, lst+5, compare);
    for(int i=0;i<4;i++){
        if(lst[i]%10 == 0){
            x += lst[i];
        }else{
            x += (lst[i]/10+1)*10; 
        }
    }
    x += lst[4];
    cout << x;
    return EXIT_SUCCESS;
}

Measured memory_bytes: 256
//T1
#include<iostream>
#include<cstdio>
#include<map>
#define N 100100
using namespace std;

int n,a[N],b[N],ca[N],cb[N],fa[N],xa,xb,ans,tt;
map<int,int>mm;

int ff(int u){return u==fa[u]?u:fa[u]=ff(fa[u]);}
inline void in(int u){if(!mm.count(u)) mm[u]=++tt;}

int main()
{
	int i,j;
	cin>>n;
	for(i=1;i<=n;i++) scanf("%d",&a[i]),xa^=a[i];
	for(i=1;i<=n;i++) scanf("%d",&b[i]),xb^=b[i];
	for(i=1;i<=n;i++)
	{
		if(a[i]!=b[i])
		{
			ans++;
			in(a[i]),in(b[i]);
			ca[mm[a[i]]]++;
			cb[mm[b[i]]]++;
		}
	}
	if(!ans)
	{
		puts("0");
		return 0;
	}
	in(xa),in(xb);
	ca[mm[xa]]++;
	cb[mm[xb]]++;
	for(i=1;i<=tt;i++)
	{
		if(ca[i]!=cb[i])
		{
			puts("-1");
			return 0;
		}
	}
	for(i=1;i<=tt;i++) fa[i]=i;
	for(i=1;i<=n;i++)
	{
		if(a[i]!=b[i])
		{
			fa[ff(mm[a[i]])]=ff(mm[b[i]]);
		}
	}
	if(xa!=xb) fa[ff(mm[xa])]=ff(mm[xb]);
	for(i=1;i<=tt;i++) if(i==ff(i)) ans++;
	cout<<ans-1;
}

Measured memory_bytes: 8448
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順

int main() {
    int N; cin >> N;
    string S; cin >> S;
    vector<int> d(N);
    int ka = 0, ko = 0;
    int x = 0;
    rep(i,N) {
        if (S.at(i) == '(') ka++; else ko++;
        d.at(i) = ka - ko;
        x = min(x, d[i]);
    }
    
    rep(i, -x) cout << '(';
    cout << S;
    rep(i, d[N-1]-x) cout << ')';
    cout << endl;
}


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

using namespace std;

const int N = 1e5 + 5;
int cnt[N], a[N];

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

    int n, q;
    cin >> n;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i]; 
        cnt[a[i]]++;
        sum += 1ll * a[i];
    }
    cin >> q;
    while (q--) {
        int b, c;
        cin >> b >> c;
        sum = sum - 1ll * cnt[b] * b + 1ll * cnt[b] * c;
        cnt[c] += cnt[b];
        cnt[b] = 0;
        cout << sum << '\n';
    }
}

Measured memory_bytes: 4356
s = input()
ans = [0]*len(s)

lst = []
for i in range(1,len(s)):
    if s[i-1] == 'R' and s[i] == 'L':
        lst.append(i)
index = 0
line = lst[index]

for i in range(len(s)):
    if i < line:
        if (line-i)%2 == 0:
            ans[line] += 1
        else:
            ans[line-1] += 1
    else:
        if s[i-1] == 'L' and s[i] == 'R':
            index += 1
            line = lst[index]
            if (line-i)%2==0:
                ans[line] += 1
            else:
                ans[line-1] += 1
        else:
            if (i-line)%2==0:
                ans[line] += 1
            else:
                ans[line-1] += 1
print(str(ans).replace('[','').replace(']','').replace(',',''))

Measured memory_bytes: 6540
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
line = list(map(str,input().split()))
 
#数字スタック
S = []
 
for tmp_ch in line:
 
    if tmp_ch in ['+','-','*']:
        b = int(S.pop())
        a = int(S.pop())
 
        if tmp_ch == '+':
            S.append(a+b)
        elif tmp_ch == '-':
            S.append(a-b)
        else:
            S.append(a*b)
    else:
        S.append(tmp_ch)
 
print("%d"%(S[0]))


Measured memory_bytes: 6380
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

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

std::vector<std::set<int>> room_list;
std::vector<int> ans;

struct Search {
  int from;
  std::set<int> to;
};

int main() {
  int N, M;
  cin >> N >> M;

  room_list.resize(N + 1);
  ans.resize(N + 1, 0);

  for (int i = 1; i <= M; i++) {
    int a, b;
    cin >> a >> b;
    room_list[a].insert(b);
    room_list[b].insert(a);
  }

  std::queue<Search> queue;
  auto set = room_list[1];
  Search tmp = {1, set};
  queue.push(tmp);

  while (!queue.empty()) {
    auto tgt = queue.front();
    queue.pop();

    for (auto room : tgt.to) {
      if (ans[room] == 0) {
        // 未探索の部屋
        // auto next = room_list[room];
        Search tmp = {room, room_list[room]};
        queue.push(tmp);
        // 探索済みにする(どこから来たか)
        ans[room] = tgt.from;
      }
    }
  }

  auto found = std::find(ans.begin() + 1, ans.end(), 0);
  if (found != ans.end()) {
    // 未到着の部屋がある
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  for (int i = 2; i <= N; i++) {
    cout << ans[i] << endl;
  }
}


Measured memory_bytes: 65332
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <numeric>
#include <cmath>
#include <limits>
#include <bitset> 
#include <iterator> 
#include <map>
#include <queue>
#include <set>
#define rep(i,n) for(int i=0;i<(n); ++i)
using namespace std;
typedef long long ll;


int main()
{
	ll n;
	double sum = 0;
	cin >> n;
	vector <double>a(n);
	rep(i, n)cin >> a[i];
	rep(i, n) {
		a[i] = pow(a[i], -1);
		sum = accumulate(a.begin(), a.end(), 0.0);
	}
	cout << fixed << setprecision(10) << pow(sum,-1) << endl;
}

Measured memory_bytes: 256
P = 10 ** 9 + 7
N, M = gets.split.map(&:to_i)
if (N - M).abs > 1
    puts 0
    exit
end
r = 1
if N == M
    r = 2
end
1.upto(N){|n|
    r *= n
    r %= P
}
1.upto(M){|m|
    r *= m
    r %= P
}
puts r


Measured memory_bytes: 14308
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stdio.h>

using namespace std;
int main(void)
{
    int n, k;

    scanf("%d", &n);
    scanf("%d", &k);

    long long ans = 1;

    ans *= k;

    while (n-- > 1) {
        ans *= k-1;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<typeinfo>
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
typedef long long int ll;
using namespace std;
int main()
{
	int n;
	cin >> n;
	int cntB=0;
	int cntA=0;
	int cntBA=0;
	int ans=0;
	string s;
	for(int i=0; i<n; i++){
		cin >> s;
		if(s.at(0)=='B' && s.at(s.size()-1)=='A'){
			cntBA++;
		}else if(s.at(0)=='B'){
			cntB++;
		}else if(s.at(s.size()-1)=='A'){
			cntA++;
		}
		for(int j=1; j<s.size(); j++){
			if(s.at(j)=='B' && s.at(j-1)=='A'){
				ans++;
			}
		}
	}
//	cout << ans << " " <<cntA << " " << cntB << endl;
	if(cntBA==0){
		ans += min(cntA, cntB);
	}else if(cntA==0 && cntB==0){
		ans += cntBA-1;
	}else{
		ans += cntBA;
		ans += min(cntA, cntB);
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
	#define ll long long
	#define pii pair<int,int>
	#define piii pair<int,pii>
	#define pb push_back
	#define vl vector<ll>
	#define vs vector<string>
	#define MP make_pair
	#define rep(i,n) for(int i=0;i<n;i++)
	#define vi vector<int>
	#define vii vector<pii>
	#define F first
	#define S second
	using namespace std;
	const int N=2e4+5;
	int n,dp[N],x,y,h;//health
	signed main()
	{
		cin>>h>>n;
		rep(i,N-1)dp[i]=1e9;
		dp[0]=0;
		rep(i,n)
		{
			cin>>x>>y;
			for(int j=0;j<=2e4;j++)
			{
				if(j>=x)dp[j]=min(dp[j],dp[j-x]+y);
			}
		}
		int ans=1e9;
		for(int i=h;i<=2e4;i++)ans=min(ans,dp[i]);
		cout<<ans<<endl;
		return 0;
	}

Measured memory_bytes: 384
N, C = map(int, input().split())
hs = list(map(int, input().split()))
lines = [(-2*hs[0], hs[0]**2)]
I = 0
for h in hs[1:]:
    u, v = lines[I]
    while I+1<len(lines):
        uu, vv = lines[I+1]
        if u*h+v < uu*h+vv:
            break
        u, v = uu, vv
        I += 1
    r = u*h+v + h**2+C
    a, b = -2*h, r+h**2
    (e, f) = lines[-1]
    while len(lines)-1:
        (c, d), (e, f) = (e, f), lines[-2]
        if (c-e)*(b-d) < (d-f)*(a-c):
            break
        lines.pop()
    lines.append((a, b))
print(r)

Measured memory_bytes: 90108
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int x1,x2,y1,y2;
int main(){
	scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
	int dx=x2-x1,dy=y2-y1;
	cout<<x2-dy<<' '<<y2+dx<<' '<<x2-dy-dx<<' '<<y2+dx-dy<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <cstdlib>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;
    if (a == b)
        return a;
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}
int main()
{   ll k,st,sum=0;
    cin>>k;
    for(ll i=1;i<=k;i++)
    {
        for(ll j=1;j<=k;j++)
        {
            for(ll h=1;h<=k;h++)
            {
                sum=sum+gcd(gcd(i,j),h);;
            }
        }
    }
    cout<<sum;
    return 0;
}


Measured memory_bytes: 3636
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for(int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

int main(){
  string O, E;
  cin >> O >> E;

  ll n = E.size();

  rep(i, n) cout << O[i] << E[i];

  if(O.size() > n) cout << O[O.size() - 1];

  cout << endl;
  
  return 0;
}

Measured memory_bytes: 256
require 'set'
s = gets.chomp
k = gets.chomp.to_i
ss = Set.new
sl = s.length
sl.times do |i|
        i.upto(sl - 1 > i + k ? i + k : sl - 1) do |j|
                ss.add(s.slice(i..j))
        end
end
sa = ss.to_a
sa.sort!
puts "#{sa[k - 1]}"

Measured memory_bytes: 4728
/***********************
*  Author: xuziyuan    * 
***********************/

#include <bits/stdc++.h>

#define rt0 return 0
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define replet(c) for(char c='a';c<='z';c++) 
#define LL long long
#define pii pair <int,int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
#define sqr(a) ((a)*(a))

using namespace std;

const LL MOD=1e9+7;

int n,x,a[110],val=0,ans;

int main()
{
	ans=1;
	cin>>n>>x;
	rep(i,n) cin>>a[i];
	rep(i,n)
	{
		val+=a[i];
		if(val<=x) ans=i+2;
		else break;
	}
	cout<<ans<<endl;
	rt0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int,P>
#define PI pair<P,int>
#define PP pair<P,P>
#define Yes(f){cout<<(f?"Yes":"No")<<endl;}
#define YES(f){cout<<(f?"YES":"NO")<<endl;}
int Madd(int x,int y) {return (x+y)%M;}
int Msub(int x,int y) {return (x-y+M)%M;}
int Mmul(int x,int y) {return (x*y)%M;}


signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(20);

  int n;
  cin>>n;
  vi a(n);
  rep(i,n){
    cin>>a[i];
  }

  int ans=0;
  int u=1;
  rep(i,70){
    int s=0,t=0;
    rep(j,n){
      if(a[j]&u) s++;
      else t++;
    }
    
    //cout<<i<<' '<<s<<' '<<t<<endl;
    ans=Madd(ans,Mmul((1LL<<i)%M,Mmul(s,t)));
    //cout<<ans<<endl;
    u*=2;
  }

  cout<<ans<<endl;

}

Measured memory_bytes: 2560
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                   \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define oprecision    \
  cout.precision(30); \
  cerr.precision(7);
#define ll long long
#define ld long double
#define ss string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) \
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? i-- : i++))
#define pb push_back
#define pc __builtin_popcount
#define pcll __builtin_popcountll
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
using namespace std;
int n, x, y, ans[5000005];
int main() {
  speed;
  oprecision;
  // code
  cin >> n >> x >> y;
  forn(n) {
    for (int j = i + 1; j <= n; j++) {
      ans[min(j - i, abs(i - x) + 1 + abs(y - j))]++;
    }
  }
  forn(n - 1) cout << ans[i] << " ";
  // endl
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<int> vec(3);
  for (int i = 0; i < 3; i++) {
  	cin >> vec[i];
  }
  sort(vec.begin(), vec.end());
  int res = vec[1] - vec.front();
  res += vec.back() - vec[1];
  cout << res << '\n';
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main(){
	long long n;
	cin>>n;
	memset(a,0,sizeof(a));
	long long p=n;
	for(int i=2;i<=sqrt(n);i++){
		while(p%i==0){
			p=p/i;
			a[i]++;
		}
	}
	if(n==1){
		cout<<0<<endl;
		return 0;
	}
	int sum=0;
	for(int i=2;i<=1000003;i++){
		//cout<<1<<endl;
		int u=sqrt(a[i]*2);
		//cout<<i<<" "<<a[i]<<" "<<u<<endl;
		for(int j=u+5;j>=u-5;j--){
			if(j>=0){
				if(j*(j+1)/2<=a[i]){
					sum+=j;
					break;
				}
			}
		}
	}
	if(p!=1){
		sum++;
	}
	cout<<sum<<endl;
}


Measured memory_bytes: 7592
#include<stdio.h>
#include<iostream>

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

int main() {
  int X,Y;
  cin >> X >> Y;
  int lb = X*2;
  int ub = X*4;
  if(lb <= Y && Y <= ub)
    if(Y%2==0){
      cout << "Yes" << endl;
      return 0;
    }
  cout << "No" << endl;

}

Measured memory_bytes: 3592
use std::cmp::max;
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("filed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n:usize = read();
    let w:usize = read();
    let mut value = vec![0; 110];
    let mut weight = vec![0; 110];
    for i in 0..n {
        value[i] = read();
        weight[i] = read();
    }

    let mut dp = vec![vec![0; 10010];110];
    for i in 0..n {
        for j in 0..w+1 {
            if j >= weight[i] {
                dp[i+1][j] = max(dp[i][j-weight[i]] + value[i], dp[i][j]);
            } else {
                dp[i+1][j] = dp[i][j];
            }
        }
    }
    println!("{}", dp[n][w]);
}


Measured memory_bytes: 9012
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N = I()
A,B = [], []
for n in range(N):
    A.append(LI())
for n in range(N):
    B.append(LI())

#  N = 100
#  A,B = [], []
#  for n in range(N):
#      A.append([random.randint(0,2*N), random.randint(0, 2*N)])
#  for n in range(N):
#      B.append([random.randint(0,2*N), random.randint(0, 2*N)])

B = sorted(B, key=lambda x: x[0])

result = 0
for b in B:
    tmp = [a for a in A if a[0] < b[0] and a[1] < b[1]]
    if not tmp:
        continue
    result += 1
    closest = max(tmp, key=lambda x: x[1])
    A.remove(closest)
print(result)



Measured memory_bytes: 66924
import sys
n,k=list(int(i) for i in input().split())
A=list(int(i) for i in input().split())
# n,k=10,4
# A=[40,10,20,70,80,10,20,70,80,60]
dpmincost=[0 for i in range(n)]
dpmincost[0]=0
for i in range(1,n):
    x=sys.maxsize
    for j in range(1,k+1):
        if i-j>=0:
            x=min(dpmincost[i-j]+abs(A[i]-A[i-j]),x)
    dpmincost[i]=x
print(dpmincost[-1])


Measured memory_bytes: 56928
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define per(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define all(x) x.begin(), x.end()
#define lowbit(x) (x & -x)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int n ; ll MOD ;
vi g[N] ;
ll up[N], down[N] ;

void dfs(int x, int fat) {
	down[x] = 1 ;
	rep(i, 0, siz(g[x]) - 1) {
		int to = g[x][i] ;
		if (to == fat) continue ;
		dfs(to, x) ;
		down[x] = down[x] * (1 + down[to]) % MOD ;
	}
}

void dp(int x, int fat) {
	rep(i, 0, siz(g[x]) - 1) {
		int to = g[x][i] ;
		if (to != fat) up[to] = up[x] ;
	}
	ll pre = 1 ;
	rep(i, 0, siz(g[x]) - 1) {
		int to = g[x][i] ;
		if (to == fat) continue ;
		up[to] = up[to] * pre % MOD ;
		pre = pre * (down[to] + 1) % MOD ;
	}
	ll suf = 1 ;
	reverse(all(g[x])) ;
	rep(i, 0, siz(g[x]) - 1) {
		int to = g[x][i] ;
		if (to == fat) continue ;
		up[to] = up[to] * suf % MOD ;
		suf = suf * (down[to] + 1) % MOD ;
	}
	rep(i, 0, siz(g[x]) - 1) {
		int to = g[x][i] ;
		if (to == fat) continue ;
		up[to] = (up[to] + 1) % MOD ;
		dp(to, x) ;
	}
}

signed main(){
	scanf("%d%lld", &n, &MOD) ;
	rep(i, 1, n - 1) {
        int x, y ; scanf("%d%d", &x, &y) ;
        g[x].pb(y) ; g[y].pb(x) ;
	}
	dfs(1, 0) ;
	up[1] = 1 ;
	dp(1, 0) ;
//	rep(i, 1, n) cout << down[i] << " " << up[i] << endl ;
	rep(i, 1, n) cout << down[i] * up[i] % MOD << endl ;

	return 0 ;
}

/*
写代码时请注意：
	1.ll？数组大小，边界？数据范围？
	2.精度？
	3.特判？
	4.至少做一些
思考提醒：
	1.最大值最小->二分？
	2.可以贪心么？不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构？
	5.统计方案是用dp？模了么？
	6.逆向思维？
*/


Measured memory_bytes: 13824
mod = 10**9 + 7

n = int(input())
a = [int(x) for x in input().split()]
ans = 0
sum = a[n-1]
for i in range(n-2, -1, -1):
    ans = (ans + sum * a[i]) % mod
    sum += a[i]
ans %= mod

print(ans)



Measured memory_bytes: 94224
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h,a;
int main(){
  cin >> h >> a;
  if(h%a==0){
    cout << h/a << endl;
  }
  else{
    cout << h/a + 1 << endl;
  }
  return 0;
}

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)
#define mp make_pair

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int32 n;
    cin >> n;
    vector<vector<pii>> adj(n);
    REP(i,n-1){
        int32 a,b;
        cin >> a >> b;
        a--;b--;
        adj[a].emplace_back(b,i);
        adj[b].emplace_back(a,i);
    }
    queue<int32> que;
    vector<int32> color(n-1,-1);
    que.push(0);
    int32 colorCnt = 0;
    while(!que.empty()){
        int32 x = que.front();que.pop();
        set<int32> used;
        vector<int64> edgeQue;
        REP(i,adj[x].size()){
            if(color[adj[x][i].second] == -1){
                edgeQue.pb(i);
            }else{
                used.insert(color[adj[x][i].second]);
            }
        }
        int32 c = 0;
        REP(i,edgeQue.size()){
            while(used.find(c) != used.end()){
                c++;
            }
            color[adj[x][edgeQue[i]].second] = c;
            c++;
            colorCnt = max(colorCnt, c);
            que.push(adj[x][edgeQue[i]].first);
        }
    }
    cout << colorCnt << endl;
    REP(i,n-1){
        cout << color[i] + 1<< endl;
    }
    return 0;
}

Measured memory_bytes: 8832
#include <stdio.h>

int main(void)
{
	int a,b;
	scanf("%d%d",&a,&b);
	if(a%3 == 0 || b%3 == 0 || (a+b)%3 == 0)
		{printf("Possible");}
	else
		{printf("Impossible");}
	return 0;
}


Measured memory_bytes: 128
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  [h1, m1, h2, m2, k] <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    l = (60 * h2 + m2) - (60 * h1 + m1)
  print $ l - k

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}


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

using namespace std;

const int N = 1e5 + 3;

int k, a[N];
pair<long long, long long> need[N];
int main() {
#ifndef ONLINE_JUDGE
#endif
//	freopen("input.in", "r", stdin);
	scanf("%d", &k);
	for (int i = 0; i < k; ++i) {
		scanf("%d", a + i);
	}
	if (a[k - 1] != 2) {
		puts("-1");
		return 0;
	}
	need[k - 1] = {2, 3};
	for (int i = k - 2; i >= 0; --i) {
		long long mul = (need[i + 1].first + a[i] - 1) / a[i];
		long long left = a[i] * mul;
		
		mul = (need[i + 1].second + a[i]) / a[i];
		long long right = a[i] * mul - 1;
		need[i] = {left, right};
		if (left > need[i + 1].second || right < need[i + 1].first) {
			puts("-1");
			return 0;
		}
	}
	printf("%lld %lld\n", need[0].first, need[0].second);
	return 0;
}



Measured memory_bytes: 2176
/*
  AtCoder Beginner Contest 083
  B - Some Sums  
*/

#include<iostream>
using namespace std;

int N, A, B;

int main() {
  scanf("%d %d %d", &N, &A, &B);

  int cnt = 0;
  for (int i = 1; i <= N; ++i) {
    int buf = i;
    int digit = 0;
    while(buf != 0) {
      digit += buf % 10;
      buf /= 10;
    }
    if (A <= digit && digit <= B) cnt += i;
  }

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


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rep1(i, n) for(int i=1; i<=n; i++)
#define repr(i, n) for(int i=n-1; i>=0; i--)
#define repr1(i, n) for(int i=n; i>=1; i--)
#define all(v) v.begin(),v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
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 MAX = 10000010;

int main() {
    ll n; cin >> n;
    vector<ll> v(MAX, 0);
    rep(i, MAX) v[i+1] = v[i] + i + 1;

    while (n > 0) {
        auto lb = lower_bound(all(v), n);
        cout << lb - v.begin() << endl;
        n -= lb - v.begin();
    }
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define SP << " " 
#define LLi long long int

using namespace std;

int imax=2147483647;
long long int llimax=9223372036854775807;

//int型vectorを出力
void PV(vector<int> pvv) {
	rep(i, pvv.size()) cout << pvv[i] SP;
	cout << endl;
}

//LLi型vectorを出力
void PVL(vector<LLi> pvv) {
	rep(i, pvv.size()) cout << pvv[i] SP;
	cout << endl;
}

int main(){

	int d, ans=0;
	vector<int> c, t, ld, at;
	vector< vector<int> >s;

	cin>> d;

	c.resize(26);
	rep(i, 26) cin>> c[i];
	s.resize(d);
	rep(i, d){
		s[i].resize(26);
		rep(j, 26) cin>> s[i][j];
	}
	t.resize(d);
	at.resize(d);
	rep(i, d){
		int tmp=1, ma=-1;
		rep(j, 26){
			if(ma<s[i][j]){
				tmp=j;
				ma=s[i][j];
			}
		}
		t[i]=tmp+1;
		if(1<=t[i] && t[i]<=26) cout<< t[i] <<endl;
		else cout<< "3" <<endl;
	}
	return 0;
}

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

const int maxn = 200;

int h, w;
char s[maxn][maxn];
int d[maxn][maxn][2];

int main() {
    scanf("%d%d", &h, &w);
    for(int i = 0; i < h; ++i)
        scanf("%s", s[i]);
    memset(d, 0x0f, sizeof(d));
    if(s[0][0] == '.')
        d[0][0][0] = 0;
    else
        d[0][0][1] = 1;
    for(int i = 0; i < h; ++i)
    for(int j = 0; j < w; ++j) {
        if(i == 0 && j == 0)
            continue;
        int k = s[i][j] == '.' ? 0 : 1;
        if(i - 1 >= 0) {
            if(s[i][j] == s[i - 1][j])
                d[i][j][k] = std::min(d[i][j][k], d[i - 1][j][k]);
            if(s[i][j] == '.' && s[i - 1][j] == '#')
                d[i][j][k] = std::min(d[i][j][k], d[i - 1][j][k ^ 1]);
            if(s[i][j] == '#' && s[i - 1][j] == '.')
                d[i][j][k] = std::min(d[i][j][k], d[i - 1][j][k ^ 1] + 1);
        }
        if(j - 1 >= 0) {
            if(s[i][j] == s[i][j - 1])
                d[i][j][k] = std::min(d[i][j][k], d[i][j - 1][k]);
            if(s[i][j] == '.' && s[i][j - 1] == '#')
                d[i][j][k] = std::min(d[i][j][k], d[i][j - 1][k ^ 1]);
            if(s[i][j] == '#' && s[i][j - 1] == '.')
                d[i][j][k] = std::min(d[i][j][k], d[i][j - 1][k ^ 1] + 1);
        }
    }
    printf("%d\n", std::min(d[h - 1][w - 1][0], d[h - 1][w - 1][1]));
    return 0;
}

Measured memory_bytes: 640
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <queue>
#include <ctime>
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <random>
#include <functional>

using namespace std;

#define F first
#define S second
#define pb push_back
#define epr(...) fprintf(stderr, __VA_ARGS__)
#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; 
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")\n"
#define dbv(a) cerr << #a << ": "; for (auto& xxxx: a) cerr << xxxx << " "; cerr << endl;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)a.size()
#define pw(n) (1ll << (n))
#define equal equalll
#define less lesss

typedef double dbl;
typedef long long ll;
const int N = 111;
const int INF = 1.01e9;
typedef vector<int> vi;


int dp[N][2];

int main(){
#ifdef HOME 
    assert(freopen("in", "r", stdin));
#endif
    int n;
    cin >> n;
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        x %= 2;
        if (x % 2 == 1) {
            dp[i + 1][0] += dp[i][0];
            dp[i + 1][1] += dp[i][0] * 2;
        }
        else {
            dp[i + 1][0] += dp[i][0] * 2;
            dp[i + 1][1] += dp[i][0];
        }
        dp[i + 1][1] += dp[i][1] * 3;
    }
    cout << dp[n][1] << endl;

    
    
#ifdef HOME 
    epr("time = %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
#endif
    return 0;
}



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

int main() {
  int N,sum,t;
  sum=0;
  cin >> N;
  t=N;

  for(int i=0 ; i<9 ; i++) {
    sum += N % 10;
    N = N / 10;
  }
  if (t % sum ==0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Measured memory_bytes: 256
import java.util.*;
import java.io.*;
import java.util.function.*;

class Main {
    static Scanner scanner = new Scanner();
    static int mod = 1000000007;
    static int inf = 0x7ffffff;

    void solve() {
        int n = scanner.nextInt();
        String s = scanner.next();
        if (n % 2 == 1) {
            System.out.println("No");
        } else {
            for (int i = 0; i < n / 2; i++) {
                if (s.charAt(i) != s.charAt(i + n / 2)) {
                    System.out.println("No");
                    return;
                }
            }
            System.out.println("Yes");
        }
    }

    public static void main(String[]$) throws Exception {
        new Main().solve();
        System.out.flush();
    }
}

class System {
    static PrintWriter out = new PrintWriter(java.lang.System.out);

    static void exit() {
        java.lang.System.exit(0);
    }
}

class Scanner {
    private BufferedReader reader = new BufferedReader(new InputStreamReader(java.lang.System.in), 0x10000);
    private StringTokenizer tokenizer;

    String next() {
        try {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
        } catch (IOException ignored) {
        }
        return tokenizer.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    char[][] nextCharArray(int n, int m) {
        char[][] a = new char[n][m];
        for (int i = 0; i < n; i++) {
            a[i] = next().toCharArray();
        }
        return a;
    }

    int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    int[] nextIntArray(int n, IntUnaryOperator op) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = op.applyAsInt(nextInt());
        }
        return a;
    }

    long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    long[] nextLongArray(int n, LongUnaryOperator op) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = op.applyAsLong(nextLong());
        }
        return a;
    }
}


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

using namespace std;

typedef long long int ll;
typedef long double ld;

inline void debugMode() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    //#ifndef ONLINE_JUDGE
    //freopen("date.in", "r", stdin);
    //freopen("date.out", "w", stdout);
    //#endif // ONLINE_JUDGE
}
inline void PrintYes() { cout << "Yes"; }
inline void PrintNo() { cout << "No"; }

const double eps = 1e-7;

int sum(int n)
{
    int sum = 0;
    while(n) {
        sum += n % 10;
        n /= 10;
    }

    return sum;
}

int main()
{
	debugMode();

    int n;
    cin >> n;

    if(n % sum(n) == 0)
        PrintYes();
    else
        PrintNo();

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include<algorithm>
#include<string>
#include <cmath>
#include <vector>
#include <map>
#include <cstdio>
#include <iomanip>
#include<set>
#include <numeric>
#pragma region Macros
#define int long long
#define double long double
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define LAST(x) x[x.size()-1]
#define ALL(x) (x).begin(),(x).end()
#define swap(a,b) (a += b,b = a - b,a -= b)
#define DIVCEIL(a,b) ((a+b-1)/b)
//int CHMAX

int FACT(int a) {
	if (a == 0)
		return 1;
	else
		return a * FACT(a - 1);
}
int nPr(int n, int r) {
	int s = n - r + 1;
	int sum = 1;
	for (int i = s; i <= n; i++)
		sum *= i;
	return sum;
}
//int nCr(int n, int r)
int nCr2(int n, int r) {
	return FACT(n) / (FACT(r) * FACT(n - r));
}
int GCD(int a, int b)
{
	if (a < b)
		swap(a, b);
	if (b == 0)
		return a;
	if (a % b == 0)
		return b;
	return GCD(b, a % b);
}
int LCM(int a, int b)
{
	return  a * b / GCD(a, b);
}
int NUMOFDIV(int n) {
	int ans = 0;
	REP(i, n) {
		if (n % i == 0)
			ans++;
	}
	return ans;
}
int CEIL1(int n) {
	return (n + 9) / 10 * 10;
}
int DIGITSUM(int n) {
	int sum = 0, dig;
	while (n) {
		dig = n % 10;
		sum += dig;
		n /= 10;
	}
	return sum;
}
int DIVTIME(int n, int k) {
	int div = 0;
	while (n % k == 0) {
		div++;
		n /= k;
	}
	return div;
}
double LOG(int a, int b) {
	return log(b) / log(a);
}
inline bool BETWEEN(int x, int min, int max) {
	if (min <= x && x <= max)
		return true;
	else
		return false;
}
inline bool PRIMEOR(int x) {
	if (x == 1)
		return false;
	if (x == 2)
		return true;
	if (x % 2 == 0)
		return false;
	double sqrtx = sqrt(x);
	for (int i = 3; i <= sqrtx; i += 2) {
		if (x % i == 0)
			return false;
	}
	return true;
}
bool SQRTOR(int n) {
	if (sqrt(n) == (int)sqrt(n))
		return true;
	else
		return false;
}
// 順位付け
using namespace std;
#pragma endregion
signed main() {
	int N, K,ans=1;
	cin >> N >> K;
	rep(i, N - 1)
		ans *= K - 1;
	cout << ans*K;
}

Measured memory_bytes: 256
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1602&lang=jp
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using dset = pair<int, char>;
queue<dset> qu;

dset sanitize(string s) {
  // cout << s << endl;
  int depth = 0;
  int len = s.length();
  for (depth = 0; depth < len; depth++) {
    // cout << depth << s[depth] << endl;
    if (s[depth] != '.') break;
  }
  // cout << depth << s[depth] << "**" << endl;
  dset res = make_pair(depth, s[depth]);
  // cout << res.first << " " << res.second << endl;
  return res;
}

int cal(char enzn, int a, int b) {
  if (enzn == '+') {
    // cout << a << "+"
    //      << b
    //      << "=" << a + b << endl;
    return a + b;
  } else {
    // cout << a << "*"
    //      << b
    //      << "=" << a * b << endl;
    return a * b;
  }
}

int slove() {
  int res = 0;
  bool init = false;
  dset current_data = qu.front();
  qu.pop();
  dset next_data;
  int current_depth = current_data.first;
  while (!qu.empty()) {
    next_data = qu.front();
    if (next_data.first <= current_depth) {
      // cout << current_depth << " : Return : " << res << endl;
      return res;
    }else{
      if (next_data.second == '+' || next_data.second == '*') {
        int t = slove();
        if (init) {
          res = cal(current_data.second, res, t);
        } else {
          init = true;
          res = t;
        }
        // qu.pop();
      } else {
        if (init) {
          res = cal(current_data.second, res, next_data.second - '0');
        } else {
          init = true;
          res = next_data.second - '0';
        }
        qu.pop();
      }
    }
  }
  return res;
}

int main(void) {
  int n, res;
  while (true) {
    cin >> n;
    res = 0;
    if (n == 0) break;
    // if (!qu.empty()) {
    // cout << "size:" << qu.size() << endl;
    // while (!qu.empty()) {
    //   dset p = qu.front(); qu.pop();
    //   cout << p.first << "," << p.second << endl;
    // }
    // }
    std::queue<dset> empty;
    std::swap(qu, empty);
    for (int i = 0; i < n; i++) {
      string st;
      cin >> st;
      dset nt = sanitize(st);
      if(n == 1) res = nt.second - '0';
      qu.push(nt);
    }
    if(n != 1) res = slove();
    // cout << "ANS : ";
    cout << res <<  endl;
    // if (!qu.empty()) cout << "q: " << qu.front().second << endl;
  }
  return 0;
}



Measured memory_bytes: 3076
a = list(map(int,input().split()))
s = list(input())
kou = 0
LR = 0
RL = 0
for (i,j) in zip(s[:-1],s[1:]):
    if i == j:
        kou += 1
    elif i == 'L' and j == 'R':
        LR += 1
    else:
        RL += 1
kou += min(2*a[1],LR+RL)
print(kou)

Measured memory_bytes: 51056
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	var A, B, C int
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d %d %d", &A, &B, &C)

	if (A + B) >= C {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}

}


Measured memory_bytes: 640
function main(input) {
  const lines = input.split('\n');
  delete lines[lines.length - 1];
  lines.forEach(v => {
    const s = v.split(' ');
    console.log(fn(s[0], s[1]));
  });
}

function fn(n, d) {
  const availableNumberOfCounterPerOne = d * 2 + 1;
  if (n % availableNumberOfCounterPerOne === 0 ) {
    return n / availableNumberOfCounterPerOne;
  }
  return Math.floor(n / availableNumberOfCounterPerOne + 1);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));


Measured memory_bytes: 21064
#include <bits/stdc++.h>
#define rep(i,n) for(int64_t i=0;i < (int64_t)(n);i++)
#define invrep(i,n) for(int64_t i=n-1;i >= 0L;i--)
#define prep(i,n) for(int64_t i=1;i <= (int64_t)(n);i++)
using namespace std;

//////全探索したいときに開けよ////////
vector<vector<int>> fspattern;
vector<int> fspattern_draft;
int fspcnt = 0;

void setfspattern_core(int floor,int number,int degree){
  if(floor==degree){
    fspattern.at(fspcnt) = fspattern_draft;
    fspcnt++;
  }else{
    rep(i,number){
      fspattern_draft.at(floor) = i;
      setfspattern_core(floor+1,number,degree);
    }
  }
}

void setfspattern(int number,int degree){
  int64_t array_num = 1;
  fspcnt = 0;
  rep(i,degree){
    array_num *= number;
  }
  fspattern = vector<vector<int>>(array_num,vector<int>(degree));
  fspattern_draft = vector<int>(degree);
  setfspattern_core(0,number,degree);
}

//////nCrのmodを求めたいときに開けよ/////////
//int64_t univMod = 1000000007;
//int64_t factnum = 100000
//vector<int64_t> modfact(factnum);
//vector<int64_t> modinvfact(factnum);

/*void setmodfact(){
  for(int i=0;i<factnum;i++){
    if(i == 0){
      modfact.at(i) = 1;
    }else{
      modfact.at(i) = (modfact.at(i-1)*i)%univMod;
    }
  }
}

int64_t calcmodpower(int64_t a,int64_t n){
  int64_t res = 1;
  while(n != 0){
    if(n & 1){
      res = (res * a) % univMod;
    }
    a = (a*a) % univMod;
    n = n >> 1;
  }
  return res;
}

int64_t calcinverse(int64_t n){
	return calcmodpower(n,univMod-2);
}

void setmodinvfact(){
  for(int i=0;i<factnum;i++){
    if(i==0){
      modinvfact.at(i) = 1;
    }else{
      modinvfact.at(i) = calcmodpower(modfact.at(i),univMod-2);
    }
  }
}

int64_t calcmodcomb(int64_t n,int64_t r){
  return (((modfact.at(n)*modinvfact.at(r))%univMod)*modinvfact.at(n-r))%univMod;
}

int64_t calcmodpositive(int64_t n){
  return (n + ((abs(n)+univMod-1)/univMod)*univMod)%univMod;
}
  
    */


/*
int64_t calcGCD(int64_t a,int64_t b){
	int64_t p = a;
	int64_t q = b;
	int64_t c = a%b;
	while(c != 0){
		p = q;
		q = c;
		c = p%q;
	}
	return q;
}
*/
struct conn_info{
  int dst;
  int l;
};

int main(){
  int n;
  cin >> n;
  vector<vector<conn_info>> conn(n);
  rep(i,n-1){
    int hoge1,hoge2,hoge3;
    cin >> hoge1 >> hoge2 >> hoge3;
    conn_info hogee;
    hogee.l = hoge3;
    hogee.dst = hoge1-1;
    conn.at(hoge2-1).push_back(hogee);
    hogee.dst = hoge2-1;
    conn.at(hoge1-1).push_back(hogee);
  }
  
  vector<int> color(n);
  rep(i,n){
    color.at(i) = -1;
  }
  queue<int> que;
  que.push(0);
  color.at(0) = 0;
  while(!que.empty()){
    int ww = que.front();
    rep(i,conn.at(ww).size()){
      conn_info dfg = conn.at(ww).at(i);
      if(color.at(dfg.dst) == -1){
        if(dfg.l % 2 == 1){
          color.at(dfg.dst) = !color.at(ww);
        }else{
          color.at(dfg.dst) = color.at(ww);
        }
        //printf("(%d = %d->%d = %d)",ww,color.at(ww),dfg.dst,color.at(dfg.dst));
        que.push(dfg.dst);
      }
    }
    que.pop();
  }
  rep(i,n){
    cout << color.at(i) << endl;
  }
          
        
    
    
    
  
  
    
}

Measured memory_bytes: 6784
import copy
N = int(input())
S = []
for i in range(N):
    S.append(int(input()))

B=copy.copy(S)
B.sort()
if N>2:
	C=int(B[N-1])
	D=int(B[N-2])
	for i in S:
		if int(i)>=C:
			print(D)
		else:
			print(C)

else:
	print(S[1])
	print(S[0])

Measured memory_bytes: 70232
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int H, W;
	cin >> H >> W;
	int A, B;
	cin >> A >> B;

	int X = H / A;
	int Y = W / B;
	cout << H * W - A * B*X*Y << endl;
}


Measured memory_bytes: 3184
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	var d = make([]int, n)
	for i := range d {
		fmt.Scan(&d[i])
	}
	sort.Ints(d)

	var numOfMochi int
	var tmp int
	for i := range d {
		if tmp > d[n-i-1] || tmp == 0 {
			numOfMochi++
		}
		tmp = d[n-i-1]
	}
	fmt.Println(numOfMochi)
}


Measured memory_bytes: 512
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

int main()
{
    int a,b; cin>>a>>b;
    cout<<(b-1 + a-2)/(a-1);
    return 0;
}


Measured memory_bytes: 256
s = list(input())
t = list(input())
ans = 'No'
if s == t:
    ans = 'Yes'

cnt = 0
while s != t:
    cnt += 1
    s = [s[-1]] + s[:-1]
    if s == t:
        ans = 'Yes'
    
    if cnt > len(s):
        break

print(ans)


Measured memory_bytes: 3060
#include <algorithm>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
using namespace std;

int main() {
    int n = 0;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i=0; i<n; i++) {
        cin >> a.at(i).first;
        a.at(i).second = i+1;
    }
    sort(a.begin(), a.end());
    for (int i=0; i<n; i++) {
        cout << a.at(i).second;
        if (i == n-1) cout << endl;
        else cout << " ";
    }
    return 0;
}


Measured memory_bytes: 1536
#include "bits/stdc++.h"
using namespace std;
#define repr(i,a,b) for(long long i=(long long)(a);i<(long long)(b);i++)
#define rep(i,n) repr(i,0,n)
#define reprrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) reprrev(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define PI 3.1415926535
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<Pll> vPll;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<Pi>> vvPi;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi>> pqlP;
string YN(bool y) { return (y?"YES":"NO"); }
string yn(bool y) { return (y?"Yes":"No"); }
string ON(bool y) { return (y?"OK":"NG"); }
int main(void)
{
	string s, s1, s2;

	cin>>s;

	rep(i, s.size()){
		if(i%2){
			s1.push_back('0');
			s2.push_back('1');
		}else{
			s1.push_back('1');
			s2.push_back('0');
		}
	}

	int ans1=0, ans2=0;

	rep(i, s.size()){
		if(s[i]!=s1[i]) ans1++;
		if(s[i]!=s2[i]) ans2++;
	}

	cout<<min(ans1, ans2)<<endl;
}


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

int main() {
  int n; cin >>n;
  pair<int,int> max1(0,0), max2(0,0);
  int buf;
  for (int i=0; i<n; i++){
    cin >>buf;
    if (buf>max1.second){
      max2 = make_pair(max1.first,max1.second);
      max1 = make_pair(i,buf);
    }else if(buf>max2.second){
      max2 = make_pair(i,buf);
    }
  }
  for (int i=0; i<n; i++){
    if (i==max1.first){
      cout <<max2.second;
    }else{
      cout <<max1.second;
    }
    cout <<endl;
  }
  
  //cout <<ans;
  //cout <<endl;
}

Measured memory_bytes: 1664
#include <stdio.h>
int main(int argc, char *argv[])
{
	int n;
	scanf("%d",&n);
	long long i=1;
	long long cc=1;
	//int flag=0;
	while(n--)
	{i=i*(cc++);i=i%1000000007;}
 	printf("%lld\n",i);
 	//if(flag) printf("\nPrint the answer modulo 109+7.\n");
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
int main(void){
   int N;
   cin >> N;
   vector<double> v(N);
   for(int i = 0; i < N; i++){
       cin >> v[i];
   }
   sort(v.begin(),v.end());
   double A = (v[0] + v[1])/2;
   for(int i = 2; i < N; i++){
       A = (A + v[i])/2;
   }
   cout << A << endl;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    int a[n];
    int x[n][n], y[n][n];
    for(int i = 0; i < n; i++){
        scanf("%d",&a[i]);
        for(int j = 0; j < a[i]; j++)
            scanf("%d%d",&x[i][j],&y[i][j]);
    }
    int ans = 0;
    for(int bit = 1; bit < (1 << n); bit++){
        int flag = 1;
        int count = 0;
        for(int i = 0; i < n && flag; i++){
            if(bit & (1 << i)){
                count ++;
                for(int j = 0; j < a[i] && flag; j++){
                    if((bit & (1 << (x[i][j] - 1))) != (y[i][j] << (x[i][j] - 1)))
                        flag = 0;
                }
            }
        }
        if(flag && ans < count)
            ans = count;
    }
    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 128
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int main() {
	string a, b;
	cin >> a >> b;
	int count = 0;
	for (int i = 0; i < 3; i++) {
		if (a[i] == b[i])
			count++;
	}
	cout << count;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <utility>
#include <tuple>
#include <vector>
#include <list>

        int N, M;
using pair_t = std::pair<int, int>;
std::vector<pair_t> ab;

void sort(std::vector<pair_t>& v)
{
        std::sort(ab.begin(), ab.end(),
                [](const pair_t& x, const pair_t& y) {
                        if (x.second < y.second)
                                return true;
                        if (x.second > y.second)
                                return false;
                        return (x.first < y.first);
                }
        );
}

int main()
{
        std::cin >> N >> M;
        for (int i = 0; i < M; ++i) {
                int a, b;
                std::cin >> a >> b;
                ab.push_back({a, b});
        }
        sort(ab);

        int n = 0;
        while (!ab.empty()) {
                ++n;
                auto x = ab.front();
                auto it = std::remove_if(ab.begin(), ab.end(), [&x](const pair_t& y){ return y.first < x.second; });
                ab.erase(it, ab.end());
                sort(ab);
        }
        std::cout << n << "\n";

        return 0;
}

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

 
 int main(){
 	char S[30],T[30];
 	int A, B;
 	char U[30];
 	scanf("%s %s", &S, &T );
 	scanf("%d %d", &A, &B );
 	scanf("%s", &U);
 	int com = strcmp(U,S);
 	int com2 = strcmp(U,T);
 	if(com == 0){
 		printf("%d %d", A-1, B);
	 }
	else if(com2 == 0){
		printf("%d %d", A, B-1);
	}
	return 0;
 }
 

Measured memory_bytes: 3160