akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <iomanip>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 100000
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
vector<vector< int> > G;
int N;
vector<int> flag;
vector<int> d;
queue<int> que;

void bfs(int k){
    if(flag[k] == -1){
        que.push(k);
        flag[k] = 0;
        while(!que.empty()){
            int now = que.front(); que.pop();
            if(G[now].size() != 0){
                REP(i,G[now].size()){
                    if(flag[G[now][i]] == -1){
                        flag[G[now][i]] = flag[now] + 1;
                        que.push(G[now][i]);
                    }
                }
            }
        }
    }
}

int main(){
    int n;
    int time = 0;

    
    cin >> n;
    G.resize(n);
    d.resize(n);//最初に発見された時刻
    flag.resize(n,-1);
    REP(i,n){
        int u,k; cin >>u>>k;
        u--;
        if(k != 0){
        REP(j,k){
            int a; cin >>a;
            G[u].push_back(a);
            G[u][j]--;
        }
        }
    }
    //time = 0;
    bfs(0);

    REP(i,n){
        cout << i + 1 << " " << flag[i] << endl;
    }
}



Measured memory_bytes: 3128
#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <string>
#include <set>
#include <map>

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i) {
    if (i * 108 / 100 == n) return !printf("%d\n", i);
  }
  puts(":(");
}


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

using namespace std;

int main()
{
  int n;
  while(cin >> n, n){
    int table[100000] = {0};
    for(int i = 0; i < n; i++){
      int sh, sm, ss, th, tm, ts;
      scanf("%d:%d:%d %d:%d:%d", &sh, &sm, &ss, &th, &tm, &ts);
      table[sh*3600+sm*60+ss]++;
      table[th*3600+tm*60+ts]--;
    }
    for(int i = 1; i < 100000; i++) table[i] += table[i-1];
    cout << *max_element(table, table + 100000) << endl;
  }
  return 0;
}

Measured memory_bytes: 1636
N, M = map(int, input().split())
if ((N - 2) == 0) or ((M - 2) == 0):
    result = 0
elif N == M == 1:
    result = 1
elif N == 1:
    result = M - 2
elif M == 1:
    result = N - 2
else:
    result = (N - 2) * (M - 2)
print(result)


Measured memory_bytes: 2940
x, n = map(int, input().split())
p = list(map(int, input().split()))

MAX = 102
tbl = [False] * MAX
for t in p:
    tbl[t] = True
MIN = MAX
ans = 0
for i in range(MAX):
    if tbl[i]:
        continue
    if MIN > abs(x-i):
        ans = i
        MIN = abs(x-i)

print(ans)

Measured memory_bytes: 9196
n=int(input())
xyh=[]

ansx,ansy,ansh = -1,-1,-1
nonzero=-1

for i in range(n):
    xyh.append(list(map(int,input().split())))
    if xyh[i][2] != 0:      
        nonzero=i

for px in range(101):
    for py in range(101):
        nh = xyh[nonzero][2] + abs(xyh[nonzero][0]-px) + abs(xyh[nonzero][1]-py)
        ok = True

        for nn in range(n):
            if (xyh[nn][2] > 0 and nh != xyh[nn][2] + abs(xyh[nn][0]-px) + abs(xyh[nn][1]-py)): 
                ok=False
            if (xyh[nn][2] == 0 and nh >  abs(xyh[nn][0]-px) + abs(xyh[nn][1]-py)):
                ok=False
        if ok:
            ansx,ansy,ansh = px,py,nh
        
    
print(ansx,ansy,ansh)


Measured memory_bytes: 3188
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FILL(i,n) memset(i,n,sizeof i)
#define X first
#define Y second
#define ET cout << "\n"
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef bbq
#define debug(...) {\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}
#define DB(a,s,e) {for(int _i=s;_i<e;++_i) cerr << a[_i] << " ";cerr << "\n";}
template<typename T>void _do(T &&x){cerr<<x<<endl;}
template<typename T,typename ...S> void _do(T &&x,S &&...t){cerr<<x<<", ";_do(t...);}
template<typename a,typename b> ostream& operator << (ostream &s,const pair<a,b> &p){return s<<"("<<p.X<<","<<p.Y<<")";}
#else
#define debug(...)
#define DB(a,s,e)
#endif

const ll MOD=1e9+7,tw=MOD+1>>1,C=2005;
ll a[200005],b[200005];
ll dp[5000][5000],J[10000],inv[10000],invJ[10000];

int main()
{
    IOS();
    ll n,ans=0,mix=MOD,miy=MOD,mxx=0,mxy=0;
    cin >> n,J[0]=J[1]=inv[1]=invJ[0]=invJ[1]=1;
    for(int i=2;i<=4*C;++i)
        J[i]=J[i-1]*i%MOD,inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD,invJ[i]=invJ[i-1]*inv[i]%MOD;
    for(int i=0;i<n;++i)
        cin >> a[i] >> b[i],++dp[C-a[i]][C-b[i]],mix=min(mix,C-a[i]),miy=min(miy,C-b[i]),mxx=max(mxx,C+a[i]),mxy=max(mxy,C+b[i]);
    for(int i=mix;i<=mxx;++i)
        for(int j=miy;j<=mxy;++j)
            dp[i][j]=(dp[i][j]+dp[i-1][j]+dp[i][j-1])%MOD;
    for(int i=0;i<n;++i)
        ans=(ans+dp[C+a[i]][C+b[i]]+MOD-J[a[i]+a[i]+b[i]+b[i]]*invJ[a[i]+a[i]]%MOD*invJ[b[i]+b[i]]%MOD)%MOD;
    cout << ans*tw%MOD << "\n";
}

Measured memory_bytes: 161664
#include <bits/stdc++.h>
template <long long mod>
class Combination
{
    std::vector<long long> fact, finv, inv;
public:
    Combination(int sz)
    {
        fact.resize(sz);
        finv.resize(sz);
        inv.resize(sz);
        fact[0] = 1;
        for (int i = 1; i < sz; ++i)
        {
            fact[i] = fact[i - 1] * i % mod;
        }
        inv[0] = 0;
        inv[1] = 1;
        for (int i = 2; i < sz; ++i)
        {
            inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        }
        finv[0] = 1;
        for (int i = 1; i < sz; ++i)
        {
            finv[i] = finv[i - 1] * inv[i] % mod;
        }
    }
    long long C(long long n, long long k)
    {
        if (n < k)
            return 0;
        if (n < 0 || k < 0)
            return 0;
        return (fact[n] * finv[k] % mod) * finv[n - k] % mod;
    }
    long long P(long long n, long long k)
    {
        if (n < k)
            return 0;
        if (n < 0 || k < 0)
            return 0;
        return fact[n] * finv[n - k] % mod;
    }
    long long H(long long n, long long k)
    {
        return this->C(n + k - 1, k);
    }
    long long getfact(int i)
    {
        return fact[i];
    }
    long long getinv(int i)
    {
        return inv[i];
    }
    long long getfinv(int i)
    {
        return finv[i];
    }
};
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll b, w; cin >> b >> w;
  Combination<MOD> comb(b+w+10);
  vll p1(b+w+2), p2(b+w+2);
  ll po = 1;
  for(int i=0;i<=b+w;++i) {
    po *= comb.getinv(2);
    po %= MOD;
    if(i >= b-1) p1[i+1] = p1[i] + po * comb.C(i, b-1) % MOD;
    p1[i+1] %= MOD;
    if(i >= w-1) p2[i+1] = p2[i] + po * comb.C(i, w-1) % MOD;
    p2[i+1] %= MOD;
  }
  for(int i=0;i<b+w;++i) {
    cout << comb.getinv(2) * (1 + MOD - p1[i] + p2[i]) % MOD << endl;
  }
}


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

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007 
#define irep(i,n) for(int i = 0; i < (int)(n); i++)
#define irep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define lrep(i,n) for(long long  i = 0; i < (long long)(n); i++)
#define lrep2(i,a,n) for(long long i = (long long)(a); i <= (long long)(n); i++)

#define irrep(i,n) for(int i = (int)(n); i > -1; i--)
#define irrep2(i,a,n) for(int i = (int)(n); i >= (int)(a); i--)

typedef long long ll;
typedef std::vector<char> v_char;
typedef std::vector<v_char > v2_char;
typedef std::vector<double> v_double;
typedef std::vector<v_double > v2_double;
typedef std::vector<int> v_int;
typedef std::vector<v_int > v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<v_ll > v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<v_string > v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<v_bool > v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;

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

using namespace std; 

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

    v_ll a(n);
    irep(i, n) {
        cin >> a[i];
        a[i] -= (i+1);
    }
    sort(a.begin(), a.end());
    
    ll b = a[(n+1)/2-1];
    ll ans = 0;
    irep(i, n) {
        ans += abs(a[i] - b);
    }
    cout << ans << endl;
    return 0;
}   
/** atcoder **/


Measured memory_bytes: 1792
#define _USE_MATH_DEFINES
#include <math.h>
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b;
    int count = 0;
    while(a <= b)
    {
        bool flag = true;
        for(int i = 0;i < to_string(a).size() / 2;i++)
        {
            if(to_string(a)[i] != to_string(a)[to_string(a).size() - 1 - i])
            {
                flag = false;
                break;
            }
        }
        if(flag)
        {
            count++;
        }
        a += 1;
    }
    cout << count << "\n";

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts)
{
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v)
{
    for (auto &e : t)
        fill_v(e, v);
}
struct UnionFind
{
    vector<int> data;

    UnionFind(int sz)
    {
        data.assign(sz, -1);
    }

    bool unite(int x, int y)
    {
        x = find(x), y = find(y);
        if (x == y)
            return (false);
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return (true);
    }

    int find(int k)
    {
        if (data[k] < 0)
            return (k);
        return (data[k] = find(data[k]));
    }

    int size(int k)
    {
        return (-data[find(k)]);
    }
    bool same(int x, int y)
    {
        return find(x) == find(y);
    }
};

int main()
{
    int N, M;
    cin >> N >> M;
    UnionFind uf(N), init(N);
    vll a(M), b(M);
    vll ans(M);
    for (size_t i = 0; i < M; i++)
    {
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        init.unite(a[i], b[i]);
    }
    set<int> cnt;
    for (size_t i = 0; i < N; i++)
    {
        cnt.insert(init.find(i));
    }
    ll start = 0;
    for (auto &&i : cnt)
    {
        start += ((ll)init.size(i) * (ll)(init.size(i) - 1)/2);
    }
    reverse(all(a));
    reverse(all(b));
    for (size_t i = 0; i < M-1; i++)
    {
        if (!uf.same(a[i], b[i]))
        {
            ll apar = uf.find(a[i]),bpar = uf.find(b[i]);
            ll as = uf.size(a[i]),bs = uf.size(b[i]);
            ans[i + 1] = ans[i] + as*bs;
            uf.unite(a[i], b[i]);
        }else
        {
            ans[i+1]=ans[i];
        }
        
    }

    reverse(all(ans));
    for (size_t i = 0; i < M; i++)
    {
        cout <<start - ans[i] << endl;
    }

    return 0;
}

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

const int mod = 1000000007;
const int MAXN = 1010;
typedef long long LL;
void reduce(int & x) { x += x >> 31 & mod; }
int mul(int a, int b) { return (LL) a * b % mod; }
int pow(int a, int b, int res = 1) {
	for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a);
	return res;
}
int f[MAXN], g[MAXN];
int n, A, B, C, D;
int fac[MAXN], inv[MAXN];
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	fac[0] = fac[1] = inv[0] = inv[1] = 1;
	for (int i = 2; i != MAXN; ++i) {
		fac[i] = mul(fac[i - 1], i);
		inv[i] = mul(inv[mod % i], mod - mod / i);
	}
	for (int i = 2; i != MAXN; ++i)
		inv[i] = mul(inv[i - 1], inv[i]);
	std::cin >> n >> A >> B >> C >> D;
	f[0] = 1;
	for (int i = A; i <= B; ++i) {
		memset(g, 0, sizeof g);
		for (int j = C; j <= D; ++j) {
			int sz = i * j, co = pow(inv[i], j, inv[j]);
			for (int k = sz; k <= n; ++k)
				reduce(g[k] += mul(f[k - sz], co) - mod);
		}
		for (int j = 0; j <= n; ++j) reduce(f[j] += g[j] - mod);
	}
	std::cout << mul(f[n], fac[n]) << std::endl;
	return 0;
}


Measured memory_bytes: 256
h,w = gets.chomp.split.map(&:to_i)
ans = 364364893931#INF

h.times do |i|
  a = i * w
  hh = h - i
  #横二つに分割
  bw = hh * (w / 2.0).floor
  cw = hh * (w / 2.0).ceil
  #縦二つに分割
  bh = (hh / 2.0).floor * w
  ch = (hh / 2.0).ceil * w
  wmin = [a,bw,cw].max - [a,bw,cw].min
  hmin = [a,bh,ch].max - [a,bh,ch].min
  min = [wmin,hmin].min
  ans = min if ans > min
  #puts "i=#{i} min=#{min}"
end
#puts
w.times do |i|
  a = i * h
  ww = w - i
  #横二つに分割
  bw = h * (ww / 2.0).floor
  cw = h * (ww / 2.0).ceil
  #縦二つに分割
  bh = (h / 2.0).floor * ww
  ch = (h / 2.0).ceil * ww
  wmin = [a,bw,cw].max - [a,bw,cw].min
  hmin = [a,bh,ch].max - [a,bh,ch].min
  min = [wmin,hmin].min
  ans = min if ans > min
  #puts "i=#{i} min=#{min}"
end

puts ans

Measured memory_bytes: 1788
/*
 * ID: 3001
 * Proble: GPA janken, combination
 */

#include <stdio.h>

int main(void)
{
    int a[500000];
    int c[4001] = {};
    int s[4001] = {};

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

    int i;
    for (i = 0; i < n; i++) {
        int d, e;
        scanf("%1d %*1c %3d", &d, &e);
        a[i] = d * 1000 + e;
    }

    for (i = 0; i < n; i++) 
        c[a[i]]++;

    int k = n - 1;
    for (i = 4000; i >= 0 && k > 0; i--) {
        if (c[i] == 0)
            continue;
        s[i] = k * 3 - (c[i] - 1) * 2;
        k -= c[i];
    }

    for (i = 0; i < n; i++)
        printf("%d\n", s[a[i]]);

    return 0;
}



Measured memory_bytes: 2200
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	int sum =0;
	for(int i=0;i<s.size()/2;i++)
	{
		if(s[i]!=s[s.size()-i-1])sum++;
	}
	cout<<sum<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

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

int main()
{
  ll a, b, x;
  cin >> a >> b >> x;
  ll ue, shita;
  if (a % x == 0) {
    shita = a / x;
  } else {
    shita = a / x + 1;
  }

  if (b % x == 0) {
    ue = b / x;
  } else {
    ue = b / x;
  }

  cout << ue - shita + 1 << endl;
}


Measured memory_bytes: 3608
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
const long long INF = numeric_limits<long long>::max();
int main()
{
    long long X, x, c;
    cin >> X;
    x = 100;
    c = 0;
    for(;;) {
        if (x >= X) {
            break;
        }
        x *= 1.01;
        c++;
    }
    cout << c << endl;
    return 0;
}

Measured memory_bytes: 3632
#include <stdio.h>

int main(void) {

  int n;
	scanf("%d", &n);
	char s[2][n+1];
	scanf("%s %s", s[0], s[1]);
	long mod = 1000000007;
	int prev = -1;
	long answer = 1;
	for (int i = 0; i < n; i++) {
		if (i != n-1 && s[0][i] == s[0][i+1]) {
			i++;
			if (prev == -1) {
				answer *= 3*2;
			} else if (prev == 1) {
				answer *= 2;
			} else {
				answer *= 3;
			}
			answer %= mod;
			prev = 2;
		} else {
			if (prev == -1) {
				answer *= 3;
			} else if (prev == 1) {
				answer *= 2;
			} else {
				answer *= 1;
			}
			answer %= mod;
			prev = 1;
		}
	}
	printf("%ld\n", answer);

  return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll  a[500000];
ll sum=0;
int main()
{
    int n;
    cin>>n;
    cin>>sum;
    a[0]=sum;
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        sum=sum^a[i];
    }
    for(int i=0;i<n;i++)
    {
        int k=sum^a[i];
        cout<<k<<" ";
    }
   
}

Measured memory_bytes: 5124
#include<iostream>
using namespace std;
int main()
{
	int n,money;
	cin>>n>>money;
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n-i;j++)
		{
			for(int k=0;k<=n-i-j;k++)
			{
				if(i+j+k==n&&10000*i+1000*k+5000*j==money)
				{
					cout<<i<<" "<<j<<" "<<k<<endl;
					return 0;
				}
			}
		}
	}
	cout<<-1<<" "<<-1<<" "<<-1<<endl;
	return 0;
} 



Measured memory_bytes: 256
<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$next = 'a';
$last = '';
$endflg = false;
do{
    switch ($next) {
        case 'a':
            if(strlen($a)===0){
                $endflg=true;
                $last = 'A';
                break;
            }
            $next = substr($a,0,1);
            $a = substr($a,1);
            break;
        case 'b':
            if(strlen($b)===0){
                $endflg=true;
                $last = 'B';
                break;
            }
            $next = substr($b,0,1);
            $b = substr($b,1);
            break;
        case 'c':
            if(strlen($c)===0){
                $endflg=true;
                $last = 'C';
                break;
            }
            $next = substr($c,0,1);
            $c = substr($c,1);
            break;
    }
}while($endflg!=true);
echo $last;
?>

Measured memory_bytes: 3832
// ABC 148-D
// 2019.12.22 bal4u

#include <stdio.h>
#include <ctype.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0; int c;
	c = gc();
//	do c = gc();
//	while (isspace(c));
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

int main()
{
	int i, n, N, x, ans;
	
	x = 1, ans = 0;
	N = n = in();
	while (n--) {
		if (in() == x) ++x;
		else ++ans;
	}
	if (ans == N) puts("-1");
	else printf("%d\n", ans);
	return 0;
}


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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
typedef double DOUBLE;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const ll LL_INF = 1e17 + 10;
const int INF = 1e9 + 10;
const ll MOD = 1e9 + 7;

const int MAXN = 2e5 + 5;

vi g[MAXN];
int p[MAXN];
int v[MAXN];
int used[MAXN];
int top = 1;

void dfs(int s) {
	used[s] = 1;
	v[s] = p[top++];
	REP(i, 0, SZ(g[s])) {
		int to = g[s][i];
		if (!used[to]) {
			dfs(to);
		}
	}
}

void solve(){
	int n, a, b;
	scanf("%d", &n);
	REP(i, 1, n) {
		scanf("%d%d", &a, &b);
		g[a].pb(b);
		g[b].pb(a);
	}
	REPN(i, 1, n) {
		scanf("%d", &p[i]);
	}
	sort(p + 1, p + n + 1);
	reverse(p + 1, p + n + 1);
	dfs(1);
	int sum = 0;
	REPN(i, 2, n) {
		sum += p[i];
	}
	printf("%d\n", sum);
	REPN(i, 1, n) {
		printf("%d ", v[i]);
	}
	putchar('\n');
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}


Measured memory_bytes: 6144
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 998244353


def resolve():
    n = int(input())
    D = list(map(int, input().split()))
    MAX_L = max(D) + 1
    cnt = [0] * MAX_L

    for d in D:
        cnt[d] += 1

    if cnt[0] != 1 or D[0] != 0:
        print(0)
        exit()

    res = 1
    for i in range(1, MAX_L):
        if cnt[i] == 0:
            print(0)
            break
        res *= pow(cnt[i - 1], cnt[i], mod)
        res %= mod
    else:
        print(res)


if __name__ == '__main__':
    resolve()


Measured memory_bytes: 20696
program main
	implicit none
	integer counter, i
	integer(8) X, Y
	read(*, *) X, Y
	counter = 1
	do i = 2, 72
		X = X*2
		if(X <= Y) then
			counter = counter + 1
		else
			exit
		end if
	end do
		write(*, *) counter
end program main

Measured memory_bytes: 512
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<iomanip>
#include<deque>
#include<tuple>
#include<queue>
#include<map>
#include <cstdint>
#include <boost/multiprecision/cpp_int.hpp>


#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define vi vector<int>
#define all(x) (x).begin(),(x).end()
#define Endl endl
#define F first
#define S second

namespace mp = boost::multiprecision;

using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;

int main() {
	string s = "ACGT";
	string t;
	cin >> t;
	int count = 0;
	int ans = 0;
	rep(i, t.size()) {
		if (t[i] == 'A' or t[i] == 'C' or
			t[i] == 'G' or t[i] == 'T') {
			count++;
		}
		else {
			if (count > ans) {
				ans = count;
			} 
			count = 0;
		}
	}
	if (count > ans) {
		ans = count;
	}
	cout << ans << endl;
}


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

int main(){
    // 制約見て！！！デカかったらlong longにすること！
    // 54
    int N;
    cin >> N;
    int l,r,ans=0;
    rep(i,N){
        cin>>l>>r;
        ans += (r-l)+1;
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)
#define debug(var) cout << "[" << #var << "] " << var << endl

const int INF = 1<<29;
const double EPS=1e-9;
const int MOD=998244353;

signed main() {
    int a;
    cin >> a;
    cout << a + a * a + a * a * a;
}

Measured memory_bytes: 3592
/******************************************************************************

                              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 sumnum(int k){
  int b=0;
  while(k>0){
 b+= k%10;
    k/=10;
}
  return b;
}
int main(){
  int d;
  cin >> d;
  if(d%sumnum(d)==0){
    cout << "Yes" << endl;
  } else{
    cout << "No" << endl;
  }
}


Measured memory_bytes: 256
#include <cstdio>
using namespace std;
char G[8][8];
bool row[8], col[8], dpos[15], dneg[15];
void dfs(int cur) {
	if (cur == 8) {
		for (int i = 0; i < 8; i++) {
	    	for (int j = 0; j < 8; j++) {
	    		putchar(G[i][j]);
			}
			putchar('\n');
		}
	}
	else {
		if (row[cur]) dfs(cur + 1);
		else {
			for (int i = 0; i < 8; i++) {
				if (!col[i] && !dpos[i + cur] && !dneg[cur - i + 7]) {
					row[cur] = true;
					col[i] = true;
					dpos[i + cur] = true;
					dneg[cur - i + 7] = true;
					G[cur][i] = 'Q';
					dfs(cur + 1);
					row[cur] = false;
					col[i] = false;
					dpos[i + cur] = false;
					dneg[cur - i + 7] = false;
					G[cur][i] = '.';
				}
			}
		}
	}
}
int main() {
    for (int i = 0; i < 8; i++) {
    	for (int j = 0; j < 8; j++) {
    		G[i][j] = '.';
		}
	}
	int k;
	scanf("%d", &k);
	while (k--) {
		int x, y;
		scanf("%d%d", &x, &y);
		G[x][y] = 'Q';
		row[x] = true;
		col[y] = true;
		dpos[x + y] = true;
		dneg[x - y + 7] = true;
	}
	dfs(0);
	return 0;
}


Measured memory_bytes: 2700
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <array>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <utility>
#include <functional>
#include <random>

using namespace std;
using lli = long long int;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0)
{ v = vector<T>(n+o); for (lli i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (lli i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

string s;
int main() {
  cin >> s;
  lli n = s.size();
  lli cnt[30], d=n%3; fill(begin(cnt), end(cnt), 0);
  for (lli i=0; i<n; ++i) {
    cnt[s[i]-'a']++;
  }
  for (lli i=0; i<=26; ++i) {
    if (cnt[i] > n/3) {
      if (cnt[i] == n/3+1 && d>0) {
        --d;
      } else {
        cout << "NO\n"; return 0;
      }
    }
  }
  cout << "YES\n";
  return 0;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
#define repr(i, a, b) for (int i = a; i >= (b); --i)
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1000000007;

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

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

int main() {
  int N;
  cin >> N;
  vector<int> T(N);
  rep(i, N) cin >> T[i];
  int M;
  cin >> M;
  vector<int> P(M), X(M);
  rep(i, M) {
    cin >> P[i] >> X[i];
    P[i]--;
  }

  int sum = 0;
  rep(i, N) {
    sum += T[i];
  }

  rep(i, M) {
    int ans = sum - T[P[i]] + X[i];
    cout << ans << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
  string a,b;cin>>a>>b;
  for(int i = 0; a.size() > i; i++){
    if(a[i] != b[a.size()-1-i]){
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}

Measured memory_bytes: 256
s = str(input())
K = int(input())
L = [0 for k in range(len(s))]

for k in range(len(s)):
    L[k] = (123-ord(s[k]))%26

now = 0
while K > 0 and now < len(s):
    if K >= L[now]:
        K -= L[now]
        L[now] = 0
        now += 1
    else:
        now += 1

ans = ""
for k in range(len(s)):
    if L[k] == 0:
        ans += "a"
    else:
        ans += chr(123-L[k])
print(ans[:-1]+chr(ord(ans[-1])+K%26))


Measured memory_bytes: 4348
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8


void mainmain(){
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	int ans = max(0, min(b, d) - max(a, c));
	cout << ans << endl;
}



}
int main() try{
//	ios::sync_with_stdio(false); cin.tie(0);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}
catch(...){}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;
#define INF = 1000000100;

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

int main(){
    ll A,B;
    cin >> A >> B;
    cout << A*B/gcd(A,B) << endl;
    return 0;
}


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

#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define PER(j,n,a) for (int j=n-1;j>=a;j--)
#define REP(i,a,n) for (int i=a;i<n;i++)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define show(x) cout<<(x)<<endl
#define UM unordered_map
#define PB push_back
#define V vector
#define EPS 1e-8
typedef unsigned long ul;
typedef long long ll;
typedef string str;
/*FIN*/

int main() {
	FAST;
	ll x,m;
	ll n;
	cin>>n>>x>>m;
	V<ll> u(m,-1), a(m);
	ll p,sz = 0,ans = 0;
	while(n>0 and u[x]==-1) {
		ans += x;
		u[x] = sz;
		a[sz] = x;
		sz++, n--;
		p = x;
		x = (x*x)%m;
	}
	if(n>0) {
		if(p==x)
			ans += n * x;
		else {
			ll csum = 0;
			ll cl = u[p] - u[x] + 1;
			REP(i,u[x],u[p]+1) csum += a[i];
			ans += (n/cl)*csum;
			n %= cl;
			REP(i,u[x],u[x]+n) ans += a[i];
		}
	}
	show(ans);
	return 0;
}



Measured memory_bytes: 4792
#include<bits/stdc++.h>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main(void) {
    string S;
    cin >> S;

    cout << S;
    
    if(S.back() == 's') {
        cout << "es" << endl;
    } else {
        cout << "s" << endl;
    }
    return 0;
}

Measured memory_bytes: 3584
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int a = Integer.parseInt(line[0]);
        int b = Integer.parseInt(line[1]);
        int C = Integer.parseInt(line[2]);
        double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2) - 2*a*b*Math.cos(Math.toRadians(C)));
        double S = a*b*Math.sin(Math.toRadians(C))*0.5;
        double L = a + b + c;
        double h = 2*S/a;

        System.out.println(S + "\n" + L + "\n" + h);
    }
}

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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define REP(i, n) FOR(i, 0, n)

struct Result {
    long long length;
    int index;
};

Result expand2(const string& s, int i, int target);

Result expand1(const string& s, int i, int target) {
    if (isalpha(s[i])) {
        int start = i, length = 0;
        for (; isalpha(s[i]); ++i) ++length;
        if (target < length)
            throw s[start + target];
        return (Result){length, i};
    }
    if (isdigit(s[i])) {
        int n_repeat = 0;
        for (; isdigit(s[i]); ++i)
            n_repeat = 10*n_repeat + (s[i]-'0');
        if (s[i] == '(') {
            Result r = expand2(s, i+1, target);
            assert(s[r.index] == ')');
            if (n_repeat * r.length > target) {
                expand2(s, i+1, target % r.length);
                assert(false);
            }
            return (Result){n_repeat * r.length, r.index + 1};
        }
        if (isalpha(s[i])) {
            if (target < n_repeat)
                throw s[i];
            return (Result){n_repeat, i + 1};
        }
        assert(false);
    }
    assert(false);
}

Result expand2(const string& s, int i, int target) {
    long long length = 0;
    for (;;) {
        Result r = expand1(s, i, target);
        length += r.length;
        target -= r.length;
        i = r.index;
        if (s[i] == '\0' || s[i] == ')')
            return (Result){length, i};
    }
}

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

    string s; int i;
    while (cin>>s>>i, s!="0"||i!=0) {
        s += '\0';
        try {
            expand2(s, 0, i);
            cout << "0\n";
        } catch (char ch) {
            cout << ch << '\n';
        }
    }
}

Measured memory_bytes: 3108
#include<iostream>

using namespace std;

int main(){
	int n, m, i, j;
	char chg[1000000], word[100000];
	while (true){
		cin >> n;
		if(n == 0) break;
		for(i = 0; i < n * 2; i ++){
			cin >> chg[i];
		}
		cin >> m;
		for(i = 0; i < m; i ++){
			cin >> word[i];
			for(j = 0; j < m; j ++){
				if(j % 2 == 0){
					if(word[i] == chg[j]){
						word[i] = chg[j + 1];
						break;
					}
				}
			}
		}

		for(i = 0; i < m; i ++){
			cout << word[i];
		}
		cout << endl;
	}
	return 0;
}

Measured memory_bytes: 3168
cube = list(map(int, input().split()))
a = input()
for i in a:
    if i == 'N':
        cube = [cube[1],cube[5],cube[2],cube[3],cube[0],cube[4]]
    if i == 'E':
        cube = [cube[3],cube[1],cube[0],cube[5],cube[4],cube[2]]
    if i == 'W':
        cube = [cube[2],cube[1],cube[5],cube[0],cube[4],cube[3]]
    if i == 'S':
        cube = [cube[4],cube[0],cube[2],cube[3],cube[5],cube[1]]
print(cube[0])
        


Measured memory_bytes: 5600
#include <iostream>
#include <vector>
#include <deque>
#include <cmath>
#include <map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include "iomanip"

using namespace std;

#include <cstdio>
#include <cstring>


int main() {

    int a, b, c;
    cin >> a >> b >> c;
    cout << min(b / a, c);

}

Measured memory_bytes: 256
#include <stdio.h>

int main(){
  int i,ret;
  ret = 0;
  char str1[] = "CODEFESTIVAL2016";
  char str2[20];
  scanf("%s",str2);
  for(i=0;i<16;i++){
    if(str1[i]!=str2[i]){
      ret++;
    }
  }
  printf("%d\n",ret);
  return 0;
}

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

using namespace std;
#define  ll long long
#define ld long double
#define f first
#define s second
const int N = 1e7+5;

ll n , a[N] , c;

void pre()
{
    for(int i=1; i<N; i++)
    {
        for(int j=i; j<N; j+=i)
            a[j]++;
    }
}

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

    cin >> n;

    pre();

    ll ans = 0;
    for(ll i=1; i<=n; i++)
    {
        ans += i*a[i];
    }
    cout << ans << "\n";

    return 0;
}

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

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORTASC(c) sort((c).begin(),(c).end())
#define SORTDESC(c,t) sort((c).begin(),(c).end(), std::greater<t>());
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define VMAX(a) *max_element(ALL(a))
#define VMIN(a) *min_element(ALL(a))
#define OUT(a) cout << (a) << endl;
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double PI  = acos(-1.0);
const long long MOD = 1000000007;

template <class T, class S>
using umap = std::unordered_map<T, S>;
using ll = long long;
using namespace std;

int main() {
    ll n, r = 0, g = 0, b = 0;
    string s;

    cin >> n >> s;

    REP(i, n) {
        if (s[i]=='R'){
            r++;
        } else if (s[i]=='G') {
            g++;
        } else {
            b++;
        }
    }

    ll cond1 = r*g*b;
    ll cond2 = 0;
    REP(i, ceil(n/2.0)-1){
        REP(j, n-2*(i+1)){
            if (s[j]!=s[j+i+1] && s[j+i+1]!=s[j+2*(i+1)] && s[j+2*(i+1)]!=s[j]){
                cond2++;
            }
        }
    }

  	cout << cond1-cond2 << endl;

    return 0;
}

Measured memory_bytes: 3600
#include <iostream>
#include <cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
    int l,r,n,sum;
    while(~scanf("%d",&n))
    {
        sum=0;
        while(n--)
        {
            cin>>l>>r;
            sum+=abs(l-r)+1;
        }
        cout<<sum<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
import java.util.*;

class Main {
        public static void main(String args[]) {
                Scanner sc = new Scanner(System.in);
                while (true) {
                        int T = sc.nextInt();
                        if (T==0) {
                                break;
                        }
                        int N = sc.nextInt();
                        for (int i = 0; i < N; i++) {
                                T += (sc.nextInt() - sc.nextInt());
                        }
                        if (T <= 0) {
                                System.out.println("OK");
                        }else{
                                System.out.println(T);
                        }
                }
        }
}

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

bool isAcgt(char s) {
    if(s == 'A' || s == 'C' || s == 'G' || s == 'T') {
        return true;
    }
    else {
        return false;
    }
}
 
int main() {
    string S;
    cin >> S;

    int max = 0;
    int num = 0;

    for(int i = 0; i < S.size(); i++) {
        if(isAcgt(S[i])) {
            num += 1;

            if(i == S.size() - 1) {
                if(max < num) {
                    max = num;
                }
            }
        }
        else {
            if(max < num) {
                max = num;
            }
            num = 0;
        }
    }

    cout << max;
    
    cout << endl;
    return 0;
}

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

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

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

  vector<int> dp(n + 1, INT_MAX);
  dp[0] = -1;
  for(int i = 0; i < n; ++i) {
    int idx = lower_bound(dp.begin(), dp.end(), a[i]) - dp.begin();
    dp[idx] = a[i];
  }

  for(int i = n; i >= 0; --i) {
    if(dp[i] != INT_MAX) return cout << i << endl, 0;
  }
}


Measured memory_bytes: 3556
class in{struct It{int it;const bool rev;explicit constexpr It(int it_, bool rev=false):it(it_),rev(rev){}int operator*(){return it;}bool operator!=(It& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const It i,n;public:explicit constexpr in(int n):i(0),n(n<0?0:n){}explicit constexpr in(int i,int n):i(i,n<i),n(n){}const It& begin(){return i;}const It& end(){return n;}};

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

/* requirement
   Val : Val(), copy / move constructor
   Upd, Map : Val operator() (Val, Val)
*/
template<class Val, class Upd, class Map>
struct SegTreeLazy {
    int n;
    vector<Val> data, lazy;
    vector<bool> isLazy;
    Upd upd;
    Map mp;
    const Val Lim; // identity mapping
    explicit SegTreeLazy(int n_, const Val& lim = Val())
        : upd(Upd()), mp(Map()), Lim(lim) { init(n_);}
    void init(int n_) { // 0 init
        for(n = 1; n < n_; n <<= 1);
        data.assign(2 * n - 1, Lim);
        lazy.assign(2 * n - 1, Val());
        isLazy.assign(2 * n - 1, false);
    }
    // lazy propagation
    inline void setLazy(int cn, int mul, Val x) {
        isLazy[cn] = true;
        lazy[cn] = upd(lazy[cn], x);
        data[cn] = upd(data[cn], x * mul);
    }
    inline void push(int cn, int cl, int cr) {
        if(isLazy[cn] == false) return;
        int mid = (cl + cr) / 2;
        setLazy(cn * 2 + 1, mid - cl, lazy[cn]);
        setLazy(cn * 2 + 2, cr - mid, lazy[cn]);
        lazy[cn] = Val();
        isLazy[cn] = false;
    }
    // operation : upd [ql, qr) by x
    // current node : cn [cl, cr)
    void updrange(int ql, int qr, const Val& x) {
        updrange(ql, qr, x, 0, 0, n);
        return;
    }
    void updrange(int ql, int qr, const Val& x, int cn, int cl, int cr) {
        if(qr <= cl || cr <= ql) return;
        if(ql <= cl && cr <= qr) {
            setLazy(cn, cr - cl, x);
            return;
        }
        push(cn, cl, cr);
        updrange(ql, qr, x, cn * 2 + 1, cl, (cl + cr) / 2);
        updrange(ql, qr, x, cn * 2 + 2, (cl + cr) / 2, cr);
        data[cn] = mp(data[cn * 2 + 1], data[cn * 2 + 2]);
    }
    Val query(int ql, int qr) {
        return query(ql, qr, 0, 0, n);
    }
    Val query(int ql, int qr, int cn, int cl, int cr) {
        if(qr <= cl || cr <= ql) return Lim;
        if(ql <= cl && cr <= qr) return data[cn];
        push(cn, cl, cr);
        Val a = query(ql, qr, cn * 2 + 1, cl, (cl + cr) / 2);
        Val b = query(ql, qr, cn * 2 + 2, (cl + cr) / 2, cr);
        return mp(a, b);
    }
};

template<class T> // requirement : operator +
struct adder {
    T operator ()(const T& a, const T& b) { return a + b;}
};

using i64 = long long;
using SegTree = SegTreeLazy<i64, adder<i64>, adder<i64>>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    SegTree seg(n);
    for(int _ : in(q)) {
        int p, u, v;
        cin >> p >> u >> v;
        --u;
        if(p) {
            cout << seg.query(u, v) << '\n';
        }
        else {
            i64 x;
            cin >> x;
            seg.updrange(u, v, x);
        }
    }
    return 0;
}

Measured memory_bytes: 7208
import java.util.*;
public class Main {
  
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int M = sc.nextInt();

    List<List<Integer>> nodes = new ArrayList<>();
    for (int i = 0; i < 3*(N+1); i++) {
      nodes.add(new ArrayList<>());
    }

    int[] minDist = new int[3*(N+1)];
    for (int i = 0; i < minDist.length; i++) {
      minDist[i] = -1;
    }

    for (int i = 0; i < M; i++) {
      int u = sc.nextInt();
      int v = sc.nextInt();
      nodes.get(3*u).add(3*v+1);
      nodes.get(3*u+1).add(3*v+2);
      nodes.get(3*u+2).add(3*v);
    }

    int S = sc.nextInt();
    int T = sc.nextInt();
    
    minDist[3*S] = 0;

    // BSF
    LinkedList<Integer> queue = new LinkedList<>();
    queue.add(3*S);
    while (!queue.isEmpty()) {
      int n = queue.remove();
      if (n == 3*T) {
        break;
      }
      List<Integer> dsts = nodes.get(n);
      for (int dst : dsts) {
        if (minDist[dst] == -1 || minDist[dst] > minDist[n] + 1) {
          queue.add(dst);
          minDist[dst] = minDist[n] + 1;
        }
      }
    }
    int ans = minDist[3*T] == -1 ? -1 : minDist[3*T] / 3;
    System.out.println(ans);
  }
}


Measured memory_bytes: 124728
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	string s;
	cin>>s;
	ll tmp = s.length();
	if(s[0]=='A')
	{ ll tp = 0;
		ll sm = 0;
		for(ll i=2;i<tmp-1;i++)
		{
			if(s[i] == 'C')
				tp++;
			if(int(s[i])>=97&&int(s[i])<=122)
                sm++;
		}
		if(int(s[1])>=97&&int(s[1])<=122)
			sm++;
		if(int(s[tmp-1])>=97&&int(s[tmp-1])<=122)
			sm++;
		if(tp == 1 &&sm== tmp-2 )
			cout<<"AC"<<endl;
		else
			cout<<"WA"<<endl;
	}else
	cout<<"WA"<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e3+5;

int dis[sz], ans[sz];
vector <int> g[sz];

void bfs(int s, int n)
{
    queue <int> q;
    q.push(s);
    for(int i=1; i<=n; i++) dis[i] = sz;
    dis[s] = 0;
    while(!q.empty()) {
        int u = q.front();
        for(int v : g[u]) {
            if(dis[u] + 1 < dis[v]) {
                dis[v] = dis[u] + 1;
                q.push(v);
            }
        }
        q.pop();
    }
    for(int i=s+1; i<=n; i++) ans[dis[i]]++;
}

int main()
{
    int n,x,y;
    cin >> n >> x >> y;
    for(int i=1; i<n; i++) {
        int j = i+1;
        g[i].push_back(j);
        g[j].push_back(i);
    }
    g[x].push_back(y);
    g[y].push_back(x);

    for(int i=1; i<=n; i++) bfs(i, n);

    for(int i=1; i<n; i++) printf("%d\n", ans[i]);
}


Measured memory_bytes: 384
n,k = [int(i) for i in input().split()]
r,s,p = [int(i) for i in input().split()]
t = [i for i in input().split()[0]]
ts = [t[0+i::k] for i in range(k)]
for t in ts:
  for i in range(len(t)-1):
    if t[i] == t[i + 1]:
      t[i + 1] = 'n'
  for i in range(len(t)):
    if t[i] == 'r':
      t[i] = p
    if t[i] == 's':
      t[i] = r
    if t[i] == 'p':
      t[i] = s
    if t[i] == 'n':
      t[i] = 0
sc = [sum(t) for t in ts]
print(sum(sc))

Measured memory_bytes: 19688