akhauriyash/Code-Regression huggingface dataset:
from string import digits
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    N, L = map(int, readline().split())
    S = readline().strip()
    *I, = range(N*N)
    LRUD = "LRUD"
    FS = [[], [], [], []]
    for i in range(N):
        base = N*i
        F = I[:]
        for j in range(N):
            F[base+j] = base+((j+1) % N)
        FS[0].append(F)
        F = I[:]
        for j in range(N):
            F[base+j] = base+((j-1) % N)
        FS[1].append(F)

        base = i
        F = I[:]
        for j in range(N):
            F[base+N*j] = base+((j+1) % N)*N
        FS[2].append(F)
        F = I[:]
        for j in range(N):
            F[base+N*j] = base+((j-1) % N)*N
        FS[3].append(F)

    def fast_pow(f, n):
        r = I[:]
        while n:
            if n & 1:
                r = [f[x] for x in r]
            f = [f[x] for x in f]
            n >>= 1
        return r

    S += "$"
    cur = 0
    def number():
        nonlocal cur
        r = 0
        while S[cur] in digits:
            r = 10*r + int(S[cur])
            cur += 1 # digits
        return r

    def expr():
        nonlocal cur
        f0 = I[:]
        while 1:
            if S[cur] == '(':
                cur += 1 # '('
                r1 = expr()
                cur += 1 # ')'
                num = number()
                f1 = fast_pow(r1, num)
            elif S[cur] in LRUD:
                t = LRUD.index(S[cur])
                cur += 1 # LRUD
                num = number()
                f1 = FS[t][num-1]
            else:
                break
            f0 = [f0[x] for x in f1]
        return f0

    f = expr()
    ans = [str(x+1) for x in f]
    for i in range(N):
        write(" ".join(ans[i*N:i*N+N]))
        write("\n")
solve()


Measured memory_bytes: 66492
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for(ll i = 0; i < (n); ++i)
template<class T, class U = size_t> class Dinic{
	struct E{ U to, rev_edge_index; T c; };
	U n; vector<vector<E>> g;
public:
	explicit Dinic(U n) : n(n), g(n) {}
	void add_undirected_edge(U from, U to, T c) { add_edge(from, to, c, c); }
	void add_edge(U from, U to, T c, T c2 = 0) {
		if (from != to) {
			g[from].push_back({ to,   g[to].size(),       c  });
			g[to  ].push_back({ from, g[from].size() - 1, c2 });
		}
	}
	T max_flow(U in, U out) {
		vector<U> d(n);
		auto bfs = [&]() {
			d.assign(n, 0);
			d[in] = 1;
			queue<U> q; q.push(in);
			while (!q.empty()) {
				U v = q.front(); q.pop();
				for (E e : g[v]) {
					if (e.c > 0 && !d[e.to]) {
						d[e.to] = d[v] + 1;
						q.push(e.to);
					}
				}
			}
			return d[out] > 0;
		};
		vector<U> ptr(n);
		function<T(U, T)> dfs = [&](U v, T lim) {
			if (in == v || !lim) return lim;
			T sum = 0;
			for (U &i = ptr[v]; i < g[v].size(); ++i) {
				E &e = g[v][i], &re = g[e.to][e.rev_edge_index];
				if (d[e.to] >= d[v] || re.c <= 0) continue;
				T f = dfs(e.to, min(lim - sum, re.c));
				if (f <= 0) continue;
				re.c -= f;
				e.c  += f;
				sum += f;
				if (sum == lim) break;
			}
			return sum;
		};
		T ans = 0;
		while (bfs()) {
			ptr.assign(n, 0);
			ans += dfs(out, numeric_limits<T>::max() / 8);
		}
		return ans;
	}
};

int main() {
    int v, e;
    cin >> v >> e;
    Dinic<int> g(v);
    REP(i, e) {
        int u, v, c; cin >> u >> v >> c;
        g.add_edge(u, v, c);
    }
    cout << g.max_flow(0, v - 1) << endl;
}

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

void Main() {
  int N, A, B;
  cin >> N >> A >> B;
  int X[N];
  for (int i = 0; i < N; i++) {
    cin >> X[i];
  }

  long long ans = 0;
  for (int i = 1; i < N; i++) {
    if ((long long)(X[i] - X[i - 1]) * A > B) ans += B;
    else ans += (long long)(X[i] - X[i - 1]) * A;
  }

  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}



Measured memory_bytes: 640
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc=new Scanner(System.in);
		
		int n=sc.nextInt();
		int m=sc.nextInt();
		boolean[] ac=new boolean[n];
		int[] wa=new int[n];
		
		for(int i=0;i<n;i++) {
			ac[i]=false;
			wa[i]=0;
		}
		
		for(int i=0;i<m;i++) {
			int sub=sc.nextInt()-1;
			String result=sc.next();
			if(ac[sub]){
				continue;
			}else if(result.equals("AC")) {
					ac[sub]=true;
			}else {
					wa[sub]++;
				}
			}
		int acnum=0;
		int wanum=0;
		for(int i=0;i<n;i++){
			if(ac[i]){
				acnum++;
				wanum+=wa[i];
			}
		}
		System.out.println(acnum+" "+wanum);
		sc.close();
	}
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define Abs(x)((x) < 0 ? (x) * -1 : (x))

#define rep(x, y) for ((x) = 0; (x) < (y); (x)++)
#define repin(x, y) for ((x) = 0; (x) <= (y); (x)++)
#define nep(x, y) for ((x) = (y) - 1; 0 <= (x); (x)--)
#define nepi(x, y, z) for ((x) = (y) - 1; (z) <= (x); (x)--)
#define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++)
#define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++)
#define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z))
#define repis(x, y, z, s) for ((x) = (z); (x) < (y); (x) += (s))
#define repiins(x, y, z, s) for ((x) = (z); (x) <= (y); (x) += (s))
#define repit(x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define nepit(x) for (__typeof((x).rbegin()) it = (x).rbegin(); it != (x).rend(); it++)

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

#define Mem0(x) memset(x, 0, sizeof(x))
#define Mem1(x) memset(x, -1, sizeof(x))

#define Unique(v) v.resize(unique(All(v)) - v.begin())

#define peq(p0, p1)(p0.first == p1.first && p0.second == p1.second)

#define End '\n'
#define Out(x) cout<<(x)<<End
#define OutP(p, sep, end) cout<<p.first<<sep<<p.second<<end

template<typename T> inline bool Max(T &x, const T &y) { return x < y ? x = y, 1 : 0; }
template<typename T> inline bool Min(T &x, const T &y) { return x > y ? x = y, 1 : 0; }
template<typename T> using V = vector<T>;
template<typename T> using VV = V<V<T> >;

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define lb std::lower_bound
#define ub std::upper_bound

#define lbi(v, x) lb(All(v), (x))-v.begin()
#define ubi(v, x) ub(All(v), (x))-v.begin()

static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;

#define LOCAL 0

int main()
{
#if LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif

    cin.tie(0);
    ios::sync_with_stdio(false);
	//std::cout.precision(18);

    string s;

	cin >> s;

	stack<char> st;
	long ans = 0;
	
	long i, len = s.size();

	rep(i, len) {
		if (!st.empty()) {
			if (st.top() != s[i]) {
				ans += 2;
				st.pop();
			} else {
				st.push(s[i]);
			}
		} else {
			st.push(s[i]);
		}
	}

	Out(ans);
	
	return 0;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;

signed main(){
	string s, t; cin >> s >> t;
	vector<vector<ll> > data(26);
	vector<int> cs(26, 0), ct(26, 0);
	ll n = s.size(), m = t.size();
	for(ll i = 0; i < n; i++) data[s[i] - 'a'].push_back(i + 1), cs[s[i] - 'a']++;
	for(int i = 0; i < m; i++) ct[t[i] - 'a']++;
	for(ll i = 0; i < 26; i++){
		if(cs[i] == 0 && ct[i] != 0){
			cout << -1 << endl;
			return 0;
		}
	}
	for(int i = 0; i < 26; i++) data[i].push_back(INF);
	
	ll ans = 0, now = 0;
	for(ll i = 0; i < m; i++){
		ll num = t[i] - 'a';
		ll next = *upper_bound(data[num].begin(), data[num].end(), now);
		if(next == INF) ans += n - now + data[num][0], now = data[num][0];
		else ans += next - now, now = next;
	}
	cout << ans << endl;
}

Measured memory_bytes: 1724
#include<iostream>
using namespace std;
long long arr[100007],brr[100007];
main()
{
    long long n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    brr[2]=max(arr[2]-arr[1],arr[1]-arr[2]);
    for(int i=3;i<=n;i++)
    {
        brr[i]=min(brr[i-1]+max(arr[i-1]-arr[i],arr[i]-arr[i-1]),brr[i-2]+max(arr[i-2]-arr[i],arr[i]-arr[i-2]));
    }
    cout<<brr[n];
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int a[N];
int dp[N];
int n,K;

int func(int index){
    if(index==n-1){
        return 0;
    }
    if(dp[index]!=-1)   return dp[index];
    int ans=2e9;
    for(int k=1;k<=K && index+k<n;k++){
        ans=min(ans,abs(a[index]-a[index+k])+func(index+k));
    }
    return dp[index]=ans;
}
main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(false);cin.tie(NULL);
    cin>>n>>K;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++)    cin>>a[i];
    cout<<func(0)<<"\n";
}

Measured memory_bytes: 12100
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll=long long;
using vi = vector<int>;
using Graph = vector<vi>;
using P = pair<int,int>;
const int MOD=(int)1e9+7;
const int INF=(int)1e9;
const ll LINF=(ll)1e18;

int main(){
    int n;
    cin >> n;
    vi x(n);
    rep(i,n) cin >> x[i];
    vi a = x;
    sort(x.begin(),x.end());
    int less = x[n/2 -1];
    //cout << "less is " << less << endl;
    int gre = x[n/2];
    //cout << "gre is " << gre << endl;
    rep(i,n){
        if(a[i]<gre){
            cout << gre << endl;
        }else{
            cout << less << endl;
        }
    }
}

Measured memory_bytes: 3712
//in the name of god
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (int)2e5+20
#define INF 2e9+20
#define pb push_back
#define F first
#define S second
typedef long long LL;
#define int LL
typedef pair<int,int> pii;
const int delta=(int)1e9+7;
int  n,y; set<int>s;
main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin>>n;
        while(n--){ 
            cin>>y; s.insert(y);
        }
cout<<s.size();
	return cout<<endl,0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int dp[100][100],x[100][100],H,W,n,a,b;
int main(){
	while(true){
		cin>>H>>W;if(H==0)break;
		cin>>n;
		for(int i=0;i<10000;i++){dp[i/100][i%100]=0;x[i/100][i%100]=0;}
		for(int i=0;i<n;i++){cin>>a>>b;x[a][b]=1;}
		dp[0][1]=1;
		for(int i=1;i<=H;i++){
			for(int j=1;j<=W;j++){
				if(x[i][j]==1)continue;
				dp[i][j]=dp[i-1][j]+dp[i][j-1];
			}
		}
		cout<<dp[H][W]<<endl;
	}
	return 0;
}

Measured memory_bytes: 3012
B = [0]

N,M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
for i in range(1,M):
    x = X[i]-X[i-1]
    B.append(x)
B = sorted(B)
B = B[::-1]
print(X[-1]-X[0]-sum(B[:N-1]))

Measured memory_bytes: 13968
N, A, B, C = map(int, input().split())
l = []
for i in range(N):
	l.append(int(input()))
X = []
for i in range(4 ** N):
	num = []
	for j in range(N):
		num.append(i % 4)
		i //= 4
	a, b, c, x = 0, 0, 0, -30
	for k in range(N):
		if num[k] == 0:
			a += l[k]
			x += 10
		elif num[k] == 1:
			b += l[k]
			x += 10
		elif num[k] == 2:
			c += l[k]
			x += 10
	if a * b * c != 0:
		x += abs(A - a) + abs(B - b) + abs(C - c)
		X.append(x)
print(min(X))


Measured memory_bytes: 4980
#include <stdio.h>

int main(void)
{
    int N, A, B;
    int min, max;
    scanf("%d", &N);
    scanf("%d", &A);
    scanf("%d", &B);
    if ( A >= B )
    {
        min = B;
        max = A;
    }
    else
    {
        min = A;
        max = B;
    }
    printf( "%d", min);
    printf( " " );
    printf( "%d", max-(N-min) < 0 ? 0 : max-(N-min) );
    return 0;
}

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

#define rep(i,s,n) for(ll i = (s); i < (n); i++)
#define rep0(i,n) rep(i,0,n)
#define rep1(i,n) rep(i,1,n+1)
#define repR(i,s,n) for(ll i = (n-1); i >= (s); i--)
#define repR0(i,n) repR(i,0,n)
#define repR1(i,n) repR(i,1,n+1)

#define BR "\n"
#define SP " "
#define SHOW(x) for(int i = 0; i < x.size(); i++) { cout << x[i] << SP; } cout << BR;
#define SHOW2(x) for(int j = 0; j < x.size(); j++) { SHOW(x[j]); } cout << BR;
#define fcout cout << fixed << setprecision(18)

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

int a[100];
int cells[100][100]; // r * c

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

  vector<int> G[N];
  rep0(i, M) {
    int a, b;
    cin >> a >> b;
    G[--a].push_back(--b);
  }

  vector<int> ans;
  int maxD = N + 1;
  rep0(i, N) {
    vector<int> d(N, -1);
    vector<int> p(N, -1);
    queue<int> q;
    int tempD = N + 1;

    q.push(i);
    d[i] = 0;
    while(!q.empty()) {
      int c = q.front();
      q.pop();
      rep0(j, G[c].size()) {
        int n = G[c][j];
        if (d[n] == -1) {
          d[n] = d[c] + 1;
          p[n] = c;
          q.push(n);
        } else if (d[n] == 0) {
          p[n] = c;
          tempD = d[c] + 1;
        }
      }
    }

    if (tempD < maxD) {
      maxD = tempD;
      vector<int> ans2(maxD);
      ans2[maxD - 1] = i;
      rep(j, 1, maxD) {
        ans2[maxD - 1 - j] = p[ans2[maxD - j]];
      }
      ans = ans2;
    }
  }

  if (maxD == N + 1) cout << -1 << BR;
  else {
    cout << ans.size() << BR;
    rep0(i, ans.size()) {
      cout << ans[i] + 1 << BR;
    }
  }
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#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 bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main() {
  vector<int> a(5);
  REP(i, 5) cin >> a[i];
  int k; cin >> k;

  REP(i, 5)FOR(j, i+1, 5){
    if(a[j] - a[i] > k){
      cout << ":(" << endl;
      return 0;
    }
  }

  cout << "Yay!" << endl;

  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int main(int argc, char const *argv[])
{
	int N, M;
	vector<int> T;

	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		int temp;
		cin >> temp;
		T.push_back(temp);
	}

	cin >> M;

	for (int i = 0; i < M; ++i)
	{
		int p, x;
		cin >> p >> x;

		int ans = 0;

		for (int j = 0; j < N; ++j)
		{
			if (j == p - 1)
			{
				ans += x;
			} else{
				ans += T[j];
			}
		}
		cout << ans << endl;
	}


	return 0;
}

Measured memory_bytes: 256
/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

int main()
{
    O_O;
    long long i, j, k, l, tst = 0, ans = 0, csno = 0;
    string S;
    cin >> S;
    cout << S.substr(0, 3) << endl;

    return 0;
}

Measured memory_bytes: 3652
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << a * b  / 2 << endl;
	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 REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 1e9


int main() {
    int N,K;
    cin >> N >> K;
    vector<long long int> a(N);
    vector<long long int> max_a(N);
    for (int i=0;i<N; i++){
        cin >> a[i];
        if (i==0){
            max_a[0] = a[i];
        }else{
            if(max_a[i-1]< a[i]){
                max_a[i] = a[i];
            }else{
                max_a[i] = max_a[i-1];
            }
        }

    }

    
    long long int min_cost = LLONG_MAX;

    for (int j=0; j<(1<<N); j++){
        vector<int> S;
        for (int k=0; k<N; k++){
            if (j & (1<<k)){
                S.push_back(k);
            }
        }
        if (S.size() >= K && S[0] == 0){
            long long int max_height = 0;
            long long int cost = 0;
            for (int l=0; l<S.size(); l++){
                if (l == 0){
                    max_height = a[S[0]];
                    continue;
                }
                if(max_height < max_a[S[l]]){
                    if(max_a[S[l]] > max_a[S[l]-1]){
                        max_height = max_a[S[l]];
                        continue;
                    }
                    max_height = max_a[S[l]];
                }

                if (max_height >= a[S[l]]){
                    cost += max_height - a[S[l]] +1;
                    max_height ++;
                }
                else{
                    max_height = a[S[l]];
                }


            }
            if (min_cost > cost){
                min_cost = cost;
            }
        }
    }
    cout << min_cost << endl;


    return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>

using namespace std;
typedef long long LL;
LL dp[110][2];
int n,p;
int main()
{
	cin>>n>>p;
	dp[0][0]=1;
	for (int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		if (x&1) 
		{
			dp[i][0]=dp[i-1][1]+dp[i-1][0];
			dp[i][1]=dp[i-1][1]+dp[i-1][0];
		}
		else 
		{
			dp[i][0]=dp[i-1][0]*2LL;
			dp[i][1]=dp[i-1][1]*2LL;
		}
	}
	cout<<dp[n][p]<<endl;
	return 0;
}

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

int main(){
	int n,m;
	cin>>n>>m;

	vector<int> a(n);
	for(int i = 0; i < m; i++){
		int x,y;
		cin>>x>>y;
		a[y-1] = max(x,a[y-1]);
	}

	int ps = 0;
	int ans = 0;
	for(int i = 0; i < n; i++){
		if(a[i]>ps){
			ps = i;
			ans++;
		}
	}
	cout<<ans;
}

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

#define int long long
#define ll long long
#define f1(i,a,b) for(i=a;i<b;i++)
#define f2(i,a,b) for(i=a;i>=b;i--)
#define endl '\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair

const int mod=1000000007;
int i,j;
int temp;

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    string a,b,c;
    cin>>a>>b>>c;
    int ans=0;
    f1(i,0,n)
    {
        if(a[i]==b[i]&&b[i]!=c[i])
        ans++;
        else if(b[i]==c[i]&&c[i]!=a[i])
        ans++;
        else if(a[i]==c[i]&&c[i]!=b[i])
        ans++;
        else if(a[i]!=b[i]&&b[i]!=c[i])
        ans+=2;
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

double dp[310][310][310];
bool flag[310][310][310];
int N;

double dfs(int c1, int c2, int c3) {
    //cout << c1 << " " << c2 << " " << c3 << endl;
    if (flag[c1][c2][c3]) return dp[c1][c2][c3];
    flag[c1][c2][c3] = true;
    double tmp = 0.0;
    /*
    if(c1 > 0) tmp += dfs(c1 - 1, c2, c3) * c1/N / (1.-(double)(N-c1-c2-c3)/N);
    if(c2 > 0) tmp += dfs(c1 + 1, c2 - 1, c3) * c2/N / (1.-(double)(N-c1-c2-c3)/N);
    if(c3 > 0) tmp += dfs(c1, c2 + 1, c3 - 1) * c3/N / (1.-(double)(N-c1-c2-c3)/N);
    //*/
    ///*
    if(c1 > 0)tmp+=dfs(c1-1,c2,c3)*c1/(c1 + c2 +c3);
	if(c2 > 0)tmp+=dfs(c1+1,c2-1,c3)*c2/(c1+c2+c3);
	if(c3 > 0)tmp+=dfs(c1,c2+1,c3-1)*c3/(c1+c2+c3);
    //*/
	tmp+=(double)N/(c1+c2+c3);
    return dp[c1][c2][c3] = tmp;
}


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    int a[310];
    rep(i, N) cin >> a[i];
    int c[4] = {};
    rep(i, N) c[a[i]]++;
    flag[0][0][0] = true;
    cout << fixed << setprecision(12) << dfs(c[1], c[2], c[3]) << endl;


    return 0;
}


Measured memory_bytes: 256256
package main

import "fmt"

var (
	N, M, X int
	C       []int
	A       [][]int
)

func main() {
	fmt.Scan(&N, &M, &X)
	C = make([]int, 0, N)
	A = make([][]int, N)
	R := make([]int, M)

	for i := 0; i < N; i++ {
		var x int
		fmt.Scan(&x)
		C = append(C, x)
		for j := 1; j <= M; j++ {
			var y int
			fmt.Scan(&y)
			A[i] = append(A[i], y)
		}
	}

	res := rec(0, R)
	if res == 1000000000 {
		fmt.Println(-1)
		return
	} else {
		fmt.Println(res)
	}
}

func rec(i int, R []int) int {
	var res int
	flag := true
	for j := 0; j < M; j++ {
		if R[j] < X {
			flag = false
		}
	}
	if i == N && flag == false {
		res = 1000000000
	} else if flag == true {
		res = 0
	} else {
		t := make([]int, M)
		k := make([]int, M)
		for j, v := range R {
			t[j] = v
			k[j] = v + A[i][j]
		}
		a := rec(i+1, t)
		b := rec(i+1, k)
		if b == 1000000000 {
			res = 100000000
		} else {
			b += C[i]
		}
		if a > b {
			res = b
		} else {
			res = a
		}
	}
	return res
}


Measured memory_bytes: 2628
#include <stdio.h>
 
int main(void)
{
    double a, b, c;
    int rect, hisi;
     
    rect = hisi = 0;
     
    while (scanf("%lf%*c%lf%*c%lf", &a, &b, &c) != EOF){
        if (a * a + b * b == c * c){
            rect++;
        }
        if (a == b && (a * a) - (c / 2) * (c / 2) > 0){
            hisi++;
        }
    }
     
    printf("%d\n%d\n", rect, hisi);
     
    return (0);
}


Measured memory_bytes: 2040
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int,int> pii;
typedef long long ll;
const long long MOD=1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back





int main(){

    int x,n;
    cin>>x>>n;


    set<int> myset;

    rep(i,n){
        int tmp;
        cin>>tmp;
        myset.insert(tmp);
    }


    vector<int> mynumbers;


    for (int i=0;i<=101;i++){
        if (!myset.count(i)){
            mynumbers.pb(i);
        }
    }

    sort(ALL(mynumbers));

    int mymin=1000;
    int answer;


    for (int mynumber:mynumbers){
        if (abs(mynumber-x)<mymin){
            mymin=abs(mynumber-x);

            answer=mynumber;

        }
    }

    cout<<answer<<endl;





}

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

int main() {
    int n, x;
    vector<int> m;
    int num = 0;
    int tmp;

    cin >> n >> x;

    for (int i = 0; i < n; i++) {
        cin >> tmp;
        m.push_back(tmp);
        num += m[i];
    }
    int minx = m[0];

    for(int i = 0; i < (int)m.size(); ++i) {
        if( m[i] < minx )
            minx = m[i];
    }

    x -= num;
    while (x >= minx) {
        x -= minx;
        n++;
    }

    cout << n << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; //10e18+10e17ちょっとくらい
const int MOD = 1000000007;
const int MAX_N = 200100;

signed main()
{
    //  以降 cin の入力元が 'input.txt' になる
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());
    rep(i, 5){
        int t;
        cin >> t;
        if(t==0){
            cout << i+1<<endl;
            break;
        }
    }


    return 0;
}

Measured memory_bytes: 3596
import java.util.Scanner
import scala.util.control.Breaks

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val N, K = sc.nextInt
    var ans = 1
    (1 to N).foreach(_ => {
      ans += math.min(K, ans)
    })

    println(ans)
  }
}

Measured memory_bytes: 25520
#include <stdio.h>

using namespace std;

int main(){
	int a,b;
	double a_d,b_d;
	scanf("%d %d",&a,&b);
	a_d = static_cast<double>(a);
	b_d = static_cast<double>(b);
	printf("%d %d %.5f\n",a/b,a%b,a_d/b_d);
}







Measured memory_bytes: 2700
#include<iostream>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>//setprecision
#include<math.h>
#include <functional>
#include<climits>
using namespace std;
template <typename T> bool chmax(T &u, const T z) { if (u < z) {u = z; return true;} else return false; }
template <typename T> bool chmin(T &u, const T z) { if (u > z) {u = z; return true;} else return false; }
#define ll  long long 
const ll INF=1LL<<60;
const ll MOD=1000000007;
const int MAX = 5000000;
long long moto[MAX], gyaku[MAX], inv[MAX];
#define rep(i, n)        for(ll i=0; i<(ll)(n); ++i)
void ncr_shokika() {
    moto[0] = moto[1] = 1;
    gyaku[0] = gyaku[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        moto[i] = moto[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        gyaku[i] = gyaku[i - 1] * inv[i] % MOD;
    }
}
long long ncr(int n, int r){
    if (n < r) return 0;
    if (n < 0 || r < 0) return 0;
    return moto[n] * (gyaku[r] * gyaku[n - r] % MOD) % MOD;
}
int main(){
    ll a,b;cin>>a>>b;
    if((a+b)%3!=0){
        cout<<0;
        return 0;
    }
    ll c=a+b;
    ll d=c/3;
    ll e=a-b;
    ll f=(d-e)/2;
    ll g=(d+e)/2;
    ncr_shokika();
    cout<<ncr(d,f)<<endl;
}

Measured memory_bytes: 120872
#include <iostream>

#include <vector>
#include <map>
#include <algorithm>
#include <fstream>
#include<cstdio>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include <cstdlib>
#include <typeinfo>

#define REP(i, n) for(int i=0;i<n;i++)
#define REP2(i, s, n) for(int i=s;i<n;i++)
#define REP_1(i, n) for(int i=1;i<n+1;i++)
#define bitSearch(bit, n) for(int bit = 0; bit < (1 << N); bit++)
using namespace std;

template<class T>
void print(const T &value) {
    std::cout << value << std::endl;
}

void yesno(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; }

void YESNO(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; }

typedef long long ll;
typedef unsigned long ul;
typedef long double ld;

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

//ll INF = 10000000;
//ll mod = 1000000007;//10^9+7

//using Graph = vector<vector<pair<int, int>>>;
//Graph G(100100);

//番号ズレ注意！！
int main() {
    int A, B, C;
    cin >> A >> B >> C;
    print(min(B / A, C));
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int x;
	cin >> x;
	int a = 0;
	while(true){
		if(a*(a+1) >= 2*x){
			cout << a << endl;
			return 0;
		}
		a++;
	}
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
#define rep(i,n) for(int i=0; i<(int)(n);i++)

int main(){
  int n;
  cin >> n;
  double sum = 0;
  int a;
  double f;
  rep(i,n){
    cin >> a;
    f = 1.0/((double)a);
    sum += f;
  }
  cout << fixed << setprecision(10);
  cout << 1.0/sum << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
ll mod = 998244353;
int main() {
	fastio;
	ll n;
	cin>>n;
	vector<ll> a(n,0);
	ll m = 0;
	for(ll i=0;i<n;i++){
		cin>>a[i];
		m = max(m,a[i]);
	}
	
	ll an = 0, res = 0;
	for(ll i=0;i<n;i++){
		if(a[i]<=m/2){
			if(a[i]>an){
				an = a[i];
				res = a[i];
			}
			
		}
		else{
			if(m-a[i]>an){
				an = m-a[i];
				res = a[i];
			}
			
		}
	}
	
	cout<<m<<" "<<res<<"\n";
	
	
	
	
}






Measured memory_bytes: 1024
/* @ by batr (T) */
#include <bits/stdc++.h>
 
#define ll long long
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
 
#define cmp_all(X) (X).begin(), (X).end(), cmp
 
#define B begin()
#define E end()
#define sz size()
#define skip continue
 
#define ppi pair<pair<int, int>, int>
#define pii pair<int, int> 
#define pli pair<ll, int>
#define pcc pair<char, char>

#define F first
#define S second

using namespace std;
 
const int maxn = 1e7 + 17;
const int MAXN = maxn * 4;
const int darr = 1e3 + 17;
const ll INF = 1e17 + 5;
const int mod = 1e9 + 7;
 
void Need_For_Speed () {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int n, mx;
string a, b;

int main () {
	Need_For_Speed();
	cin >> n >> a >> b;
	int mn = n + n;
	for (int i = 1; i <= n; i++) {
		string r1, r2;
		for (int j = 0; j < i; j++) {
			r1 += b[j];
		}
		for (int j = n - i; j < n; j++) {
			r2 += a[j];
		}
		if (r1 == r2) {
			mn = min(mn, n + n - i);
		}
	}
	cout << mn;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\n"

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll N;
  cin >> N;
  vll A(N);
  for(int i = 0; i < N; i++){
    cin >> A.at(i);
  }
  sort(ALL(A), greater<ll>());
  ll ans = A.at(0);
  N -= 2;
  ll num = N / 2;
  for(int i = 1; i <= num; i++){
    ans += 2 * A.at(i);
  }
  if(N % 2 == 1) ans += A.at(num+1);
  cout << ans << endl;
}


Measured memory_bytes: 4780
from collections import deque

def bfs(x, y):
    Max = 0
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    start = [x, y]
    distance = [[41]*W for i in range(H)]
    distance[y][x] = 0
    Q = deque()
    Q.append(start)

    while len(Q) != 0:
        p = Q.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            
            if 0 <= nx < W and 0 <= ny < H:
                if Maze[ny][nx]=="." and distance[ny][nx]==41:
                    Q.append([nx, ny])
                    distance[ny][nx] = distance[p[1]][p[0]] + 1
    
    for i in range(H):
        for j in range(W):
            if distance[i][j]!=41 and  distance[i][j] > Max:
                Max = distance[i][j]
    return Max

H, W = map(int, input().split())
Maze = [None]*H
for i in range(H):
    Maze[i] = list(input())

MaxDistance = 0
for i in range(H):
    for j in range(W):
        if Maze[i][j] == ".":
            d = bfs(j, i)
            if d > MaxDistance:
                MaxDistance = d


print(MaxDistance)


Measured memory_bytes: 3444
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	//	"strings"
)

type UnionFind struct {
	p []int
}

func (u *UnionFind) init(size int) {
	u.p = make([]int, size)
	for i := 0; i < size; i++ {
		u.p[i] = i
	}
}

func (u *UnionFind) root(x int) int {
	if u.p[x] == x {
		return x
	}
	u.p[x] = u.root(u.p[x])
	return u.p[x]
}

func (u *UnionFind) unite(x, y int) {
	rx := u.root(x)
	ry := u.root(y)
	if rx != ry {
		u.p[rx] = ry
	}
}

func (u *UnionFind) same(x, y int) bool {
	rx := u.root(x)
	ry := u.root(y)
	return rx == ry
}

func solve(n, q int, data [][]int) []int {
	ans := []int{}
	return ans
}

func main() {
	sc := NewScanner()

	n := sc.NextInt()
	m := sc.NextInt()

	p := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = sc.NextInt()
		p[i]--
	}
	u := UnionFind{}
	u.init(n)

	for i := 0; i < m; i++ {
		x := sc.NextInt()
		y := sc.NextInt()
		u.unite(x-1, y-1)
	}

	ans := 0
	for i := 0; i < n; i++ {
		if p[i] == i {
			ans++
			continue
		}
		b := u.same(p[i], i)
		if b {
			ans++
		}
	}
	fmt.Printf("%v\n", ans)
}

/*
func main() {

	n := 5
	q := 12
	// command, x, y
	// command ... 0 = unite (merge)
	// command ... 1 = find (if same or not)
	data := [][]int{
		{0, 1, 4},
		{0, 2, 3},
		{1, 1, 2},
		{1, 3, 4},
		{1, 1, 4},
		{1, 3, 2},
		{0, 1, 3},
		{1, 2, 4},
		{1, 3, 0},
		{0, 0, 4},
		{1, 0, 2},
		{1, 3, 0},
	}
	ans := solve(n, q, data)
	fmt.Printf("%+v\n", ans) // output for debug

}
*/

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


Measured memory_bytes: 5504
#include <bits/stdc++.h>
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using ll = long long;
const int INF = 0x3f3f3f3f;
using namespace std;

int n;
double p, dp[3001];

int main()
{
    IOS
    cin >> n;
    dp[0] = 1;
    for (int i = 0; i < n; ++i) {
        cin >> p;
        for (int heads = i+1; heads >= 0; --heads) {
            dp[heads] = dp[heads-1] * p + dp[heads] * (1-p);
        }
    }
    double ans = 0;
    for (int i = n/2+1; i <= n; ++i) {
        ans += dp[i];
    }
    cout.precision(10);
    cout << fixed << ans << endl;

    return 0;
}


Measured memory_bytes: 256
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <stack>
#include <map>
#include <cstring>
#include <unordered_map>
#include <queue>
#include <functional>
#define   mod  998244353
#define   int  long long 
#define   ld   long double
#define   pb   push_back
#define   vi   vector<int> 
#define   co(x)  cout << x << '\n' 
#define   dbg(x)  cerr << #x << " = " << x << '\n' 
#define   bitcount(x)  (int)__builtin_popcount(x) 
#define   sz(x)  (int)x.size()
#define   all(a) a.begin(),a.end() 
#define   ff   first
#define   ss   second
#define   pii  pair<int,int> 
#define   lcm(a,b) (a*b)/__gcd(a,b) 
using namespace std;

int n,m,r;

signed main()
{
    cin >> n >> m >> r ; 
    vi a(r); 
    vector <vi> dis(n+1,vi (n+1,1e9));
    for (int &it: a) cin >> it ; 
    for (int i=1;i<=m;i++){
        int  x, y, c ; 
        cin >> x >> y >> c ; 
        dis[x][y] = dis[y][x] = min(dis[x][y], c) ; 
    }
    for (int k=1;k<=n;k++) {
        for (int i=1;i<=n;i++) {
            for (int j=1;j<=n;j++) {
                dis[i][j] = min( dis[i][j], dis[i][k]+dis[k][j] ) ; 
            }
        }
    }
    int ans = 1e9 ; 
    sort(all(a));
    do {
        int sum = 0;
        for (int i=1;i<r;i++){
            sum += dis[a[i]][a[i-1]];
        }
        ans = min(ans, sum) ; 
    }
    while (next_permutation(a.begin(),a.end()));
    co ( ans ) ; 
    return 0;
}


Measured memory_bytes: 3956
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

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

vector<bool> getPrime(int n) {
	vector<bool> prime(n + 1, true);

	if (n >= 0) prime[0] = false;
	if (n >= 1) prime[1] = false;

	for (int i = 2; i * i <= n; i++) {
		if (prime[i]) {
			for (int j = i + i; j <= n; j += i) {
				prime[j] = false;
			}
		}
	}

	return prime;
}

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

	const int R_MAX = 100000;
	vector<bool> prime = getPrime(R_MAX + 1);

	vector<int> cnt(R_MAX + 1);
	for (int i = 3; i <= R_MAX; i++) {
		if (prime[i] && prime[(i + 1) / 2]) {
			cnt[i]++;
		}
	}
	for (int i = 1; i <= R_MAX; i++) {
		cnt[i] += cnt[i - 1];
	}

	int q;
	cin >> q;

	for (int i = 0; i < q; i++) {
		int l, r;
		cin >> l >> r;

		cout << cnt[r] - cnt[l - 1] << endl;
	}
	return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
#define rep(i,n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define eb emplace_back
using ll=long long;
using vi=vector<ll>;

vi tree[200005],score(200005,0);
void dfs(ll p,ll x){
  for(ll k:tree[p]){
    if(k!=x){
      score[k]+=score[p];
      dfs(k,p);
    }
  }
  return;
}
void solve(){
  ll n,q;
  cin>>n>>q;
  rep(i,n-1){
    ll a,b;
    cin>>a>>b;
    tree[a-1].eb(b-1);
    tree[b-1].eb(a-1);
  }
  rep(i,n) score[i]=0;
  rep(i,q){
    ll p,x;
    cin>>p>>x;
    score[p-1]+=x;
  }
  dfs(0,-1);
  cout<<score[0];
  REP(i,1,n) cout<<" "<<score[i];
  cout<<'\n';
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}


Measured memory_bytes: 24320
n = int(input())
a = list(map(int, input().split()))


sum = 0
for i in range(n):
    sum += a[i]

sum2 = 0
for i in range(n):
    sum2 += a[i]
    x = sum2*2 - sum
    if x >= 0:
        y = sum - (sum2-a[i])*2
        if x <= y:
            print(x)
            break
        else:
            print(y)
            break


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

const int N=1e5 +3;
int a[N];
map<int, int> mp;

int main(){

    int n;
    scanf("%d", &n);
    for(int i=0; i<n; ++i){
        scanf("%d", a+i);
        ++mp[a[i]];
    }

    int ans=0;
    for(auto i:mp){
        if(i.second<i.first)
            ans+=i.second;
        else
            ans+=i.second-i.first;
    }

    printf("%d", ans);

    return 0;
}


Measured memory_bytes: 5120
#include <math.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <sstream>
using namespace std;

int main()
{
	int n, l;
	cin >> n >> l;
	vector<int> v(n);
	int ttl = 0;
	for (int i = 0; i < n; i++) {
		v[i] = l + i;
		ttl += v[i];
	}
	int ans = 0;
	int sa = abs(v[0]);
	for (int i = 1; i < n; i++) {
		if (sa > abs(v[i])) {
			sa = abs(v[i]);
			ans = i;
		}
	}
	cout << ttl - v[ans] << endl;
	return(0);
}





Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <algorithm>

#define all(v) (v).begin(), (v).end()
using namespace std;

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

    if(n == 0)
    {
        printf("0\n");
        return 0;
    }

    vector <int> A;
    while(n != 0)
    {
        if(abs(n)%2 == 0)
        {
            A.push_back(0);
            n /= (-2);
        }
        else
        {
            A.push_back(1);
            n--;
            n /= (-2);
        }
    }

    reverse(all(A));

    for(int i = 0; i < A.size(); i++)
        printf("%d", A[i]);

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

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

long gcd(long a, long b) {
    if (a < b) {
        swap(a, b);
    }

    while (b != 0) {
        a = a % b;
        swap(a, b);
    }

    return a;
}

long lcm(long a, long b) {
    return a * b / gcd(a, b);
}

int main() {
    int N;
    cin >> N;
    vector<int> a(N);
    rep(i, N) {
        cin >> a[i];
    }

    // 累積和
    vector<ll> a_sum(N);
    a_sum[0] = a[0];

    repr(i, 1, N) {
        a_sum[i] = a[i] + a_sum[i - 1];
    }

    ll min = -1;
    rep(i, N - 1) {
        // sunuke's sum of cards
        ll sum_1 = a_sum[i];
        // araiguma's sum of cards
        ll sum_2 = a_sum[N - 1] - a_sum[i];

        ll diff = abs(sum_1 - sum_2);

        if (min == -1 || diff < min) {
            min = diff;
        }
    }

    cout << min << endl;

    return 0;
}

Measured memory_bytes: 2560
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s = in.readLine();
    in.close();
    
    int a_location = 0;
    int z_location = 0;
    for(int i=0; i<s.length(); i++) {
      if(s.charAt(i)=='A') {
        a_location = i+1;
        break;
      }
    }
    
    for(int i=0; i<s.length(); i++) {
      if(a_location != 0 && s.charAt(i)=='Z') {
        z_location = i+1;
      }
    }

    System.out.println(z_location - a_location + 1);
  }
}

Measured memory_bytes: 36300
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main()
{
    int N; cin >> N;
    map<string, int> str2NumMap
    {
        { "AC", 0 },{ "WA", 0 },{ "TLE", 0 },{ "RE", 0 },
    };
    for (int i = 0; i < N; ++i)
    {
        string str; cin >> str; str2NumMap[str]++;
    }
    vector<string> strs{ "AC", "WA", "TLE", "RE" };
    for (const auto &str : strs)
    {
        cout << str << " x " << str2NumMap[str] << endl;
    }
}

Measured memory_bytes: 3632
#include <stdio.h>

int main(){
  int S[1000000];
  int T[1000000];
  int n;
  int q;
  int c=0;
  int m;
  int r;
  int l;
  int i,j;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&S[i]);
  }

  scanf("%d",&q);
  for(i=0;i<q;i++){
    scanf("%d",&T[i]);
  }

  for(i=0;i<q;i++){
    r=n;
    l=0;
    while(l<r){
      m=(r+l)/2;
      if(T[i]==S[m]){
	c++;
	break;
      }
      if(T[i]>S[m]){
	l=m+1;
      }
      else if(T[i]<S[m]){
	r=m;
      }
    }
  }

  printf("%d\n",c);

  return 0;
}

Measured memory_bytes: 3044
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#include <cmath>
#include <cstdio>

#define REP(i,a,n) for(int i=a;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define vsort(v) sort(v.begin(),v.end())
#define asort(a,n) sort(a, a + n)
#define vi vector<int>
#define vd vector<double>

using namespace std;
int counter;
string str;
bool isKaibun(int start, int end){
    /*
    REP(i, start, end + 1){
        cout << str.at(i);
    }
    cout << start << "," << end << endl;
    */
    if(end - start + 1 <= 1) return true;
    if(str.at(start) != str.at(end)){
        if(str.at(start) == 'x'){ //前がx
            //str += "x"; //末尾にx追加
            start++;
        }else{ //前がxじゃないとき
            if(str.at(end) != 'x'){
                return false;
            }
            //str = "x" + str;//先頭にx追加
            end--;
        }
        counter++;
    }else{
        start++;
        end--;
    }
    //cout << next << "," << end << endl;
    //str = str.substr(start, end);
    
    //cout << str << endl;
    return isKaibun(start, end);
}

int main(){
    ios::sync_with_stdio(false);
    cin >> str;
    counter = 0;

    if(isKaibun(0, str.size() - 1)){
        cout << counter << "\n";
    }else{
        cout << -1 << "\n";
    }

    return 0;
}


Measured memory_bytes: 512
x = input()
y= []
for i in range(x+1):
	if i == 0:
		continue
	if i % 3 == 0:
		y.append(str(i))
	else:
		z = i
		while 1:
			if z % 10 == 3:
				y.append(str(i))
				break
			z /= 10
			if z == 0:
				break
print " " + " ".join(y)

Measured memory_bytes: 4712
from collections import Counter
n = int(input())
s = []
ans = []

for i in range(n):
    s.append(input())

c = Counter(s)
c_s = c.most_common()
m = c.most_common()[0][1]

for j in range(len(c_s)):
    if c_s[j][1] != m:
        break
    else:
        ans.append(c_s[j][0])

ans.sort()
for t in ans:
    print(t)

Measured memory_bytes: 59376