akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  sort(ALL(a));
  vector<ll> sums(n+1, 0);
  rep(i, n){
    sums.at(i+1) += sums.at(i) + a.at(i);
  }
  int ans = 1;
  rrep(i, n){
    // cerr << sums.at(i) << " " << sums.at(i+1) << endl;
    if(sums.at(i) * 2 < a.at(i)) break;
    ans++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
	string a, b;
	int ans = 0;
	cin >> a >> b;
	for (int n = 0 ; n < a.size(); n++) {
		if (a.at(n) == b.at(n)) {
			ans = 0;
		}
		else {
			ans = 1;
			break;
		}
	}
	if (ans == 0) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
}

Measured memory_bytes: 3628
#include <iostream>
#include <cstdio>
#include <sstream>

using namespace std;

typedef unsigned int ui;

void solve(ui x) {
    int minus = x & (1U<<31);
    double ret = 0;
    for (int i = 7; i <= 30; i++) {
        if (x & (1U<<i)) {
            ret += (1<<(i-7));
        }
    }
    int last = 0;
    for (int i = 0; i < 7; i++) {
        if (x & (1U<<i)) {
            ret += (1.0 / (1<<(7-i)));
            last = max(last, (7-i));
        }
    }
    if (last == 0) last = 1;
    if (minus) {
        ret = -ret;
    }
    ostringstream fss;
    fss << "%." << last << "f\n";
    //cout << fss.str() << endl;
    printf(fss.str().c_str(), ret);
}

int main() {
    int Q; scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
        ui s; scanf("%x", &s);
        solve(s);
    }
    return 0;
}

Measured memory_bytes: 1296
import sys
input = sys.stdin.readline
from math import gcd
from functools import reduce
from collections import defaultdict


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def is_pairwise_coprime(A):
    n = max(A)
    base = [False for i in range(n+1)]
    for a in A:
        if a > 1 and base[a]:
            return False
        base[a] = True
    
    is_prime = [True for i in range(n+1)]
    for i in range(2, n+1):
        if is_prime[i]:
            include_number = set()
            if base[i]:
                include_number.add(i)
            for j in range(i+i, n+1, i):
                is_prime[j] = False
                if base[j]:
                    include_number.add(j)
            if len(include_number) >= 2:
                return False
    return True


def solve(N, A):
    if is_pairwise_coprime(A):
        return "pairwise coprime"
    if reduce(gcd, A) == 1:
        return "setwise coprime"
    return "not coprime"


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))


Measured memory_bytes: 127916
def main():
  N = int(input())

  d = N % 10

  hon = [2, 4, 5, 7, 9]
  pon = [0, 1, 6, 8]
  bon = [3]

  if(d in hon):
    print("hon")
  elif(d in pon):
    print("pon")
  elif(d in bon):
    print("bon")
  

if __name__ == "__main__":
  main()

Measured memory_bytes: 9116
Array.prototype.aggregate = function(){
 	return this.reduce((a,b)=>(a[b]=(a[b]||0)+1)&&a,{});
}
function Main(input){
	input = input.split("\n");
  var a = input[1].split(" ");
  var b = a.aggregate();
  
  var cnt = 0;
  
  for(var c in b){
  	cnt++;
  }
  
  return cnt==3?"Three":"Four";
}

// If you are interested in the algorithms I used, 
// there is no need to read down from this line.

((b,c,d)=>"undefined"!=typeof require?console.log(Main(require("fs").readFileSync("/dev/stdin","utf8"))):[b=b.split("\n").slice(1,-1).join("\n"),c=function(){try{return Main(b)}catch(f){return f.stack||f.message}}(),d=typeof c,"string"==d||(c=JSON.stringify(c)),$("<form>").append([[10,b],0,[1,d],0,[10,c]].map(f=>$(f?"<textarea>":"<br>",f&&{rows:f[0],cols:40,text:f[1]}))).appendTo("body")])(`
6
G W Y P Y W
`);

Measured memory_bytes: 11468
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define ll long long
#define rep(i,m) for(int i=0;i<(n);i++)
using namespace std;
const int MAX = 100005;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main(){
  ll n,k;
  cin >> n >> k;
  string s; cin >> s;
  ll RL = 0;
  ll LR = 0;
  ll ans  = 0;
  rep(i,n-1){
    if(s[i] == 'R' && s[i+1] == 'R') ans++;
  }
  for(int i=1;i<n;i++){
    if(s[i-1] == 'L' && s[i] == 'L') ans++;
  }
  rep(i,n){
    if(s[i] == 'L' && s[i+1] == 'R') LR++;
    if(s[i] == 'R' && s[i+1] == 'L') RL++;
  }
  ll d1 = LR-RL;
  ll d2 = RL-LR;
  
  ans += 2*min(k,min(LR,RL));
  if(k > min(LR,RL)){
   ll rem = k - min(LR,RL);
  while(d1>0){
    if((s[0]=='L')||(s[n-1]=='R')){
      if(rem > 0){
    ans++; d1--; rem--;
      }
    }
  }
  while(d2>0){
    if((s[n-1]=='L')||(s[0]=='R')){
      if(rem>0){
      }
    ans++; d2--; rem--;
    }
  }
  }
  cout << ans;
}

Measured memory_bytes: 512
import copy
mod = (10 ** 9) + 7
n = int(input())
a = list(map(int, input().split()))
M = sum(a)
a_sum = copy.copy(a)
for i in range(1, n):
    a_sum[i] = (a_sum[i - 1] + a_sum[i]) % mod
# print(a)
# print(a_sum)
ans = 0
for i in range(n):
    ans += a[i] * M
    ans -= a[i] * a_sum[i]
print(ans % mod)


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

/*int main(void)
{
    int n;
    cin >> n;
    vector<int> d(n);
    for (int i = 0; i < n; i++)
    {
        cin >> d[i];
    }
    sort(d.begin(), d.end());

    int ans = 0;
    for (int i = 1; i < n; i++)
    {
        if (d[i-1] != d[i]) { ans++; }
    }

    cout << ans << endl;
}*/

int main(void)
{
    int n;
    cin >> n;
    int d[n];
    for (int i = 0; i < n; i++) { cin >> d[i]; }

    int num[110] = {0};
    for (int i = 0; i < n; i++)
    {
        num[d[i]]++;
    }

    int ans = 0;
    for (int i = 0; i <= 100; i++)
    {
        if (num[i] > 0) { ans++; }
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
import java.util.*;

public class Main{
	int INF = 1 << 24;
	int [][] cost;
	int w,h;
	int [] vx = {0,1,0,-1};
	int [] vy = {1,0,-1,0};
	
	class State implements Comparable<State>{
		int nowx, nowy, cost, bit;

		public State(int nowx, int nowy, int cost, int bit) {
			this.nowx = nowx;
			this.nowy = nowy;
			this.cost = cost;
			this.bit = bit;
		}

		@Override
		public int compareTo(State o) {
			return this.cost - o.cost;
		}
		
	}

	private void doit(){
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			w = sc.nextInt();
			h = sc.nextInt();
			char [][] data = new char[h][w];
			for(int i = 0; i < h; i++){
				data[i] = sc.next().toCharArray();
			}
			cost = new int[h][w];
			for(int i = 0; i < h; i++){
				Arrays.fill(cost[i], 1);
			}
			int sx = -1, sy = -1, countM = 0;
			HashMap<Integer, Integer> bitmap = new HashMap<Integer, Integer>();
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){
					if(data[i][j] == 'S'){
						sx = j;
						sy = i;
						data[i][j] = ',';
					}
					if(data[i][j] == '#'){
						set(i, j);
					}
					if(data[i][j] == 'M'){
						bitmap.put(i * h + j, countM++);
					}
				}
			}
			
			PriorityQueue<State> open = new PriorityQueue<Main.State>();
			open.add(new State(sx, sy, 0, 0));
			int [][][] close = new int[h][w][1 << countM];
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){
					Arrays.fill(close[i][j], INF);
				}
			}
			close[sy][sx][0] = 0;
			int ans = -1;
			while(! open.isEmpty()){
				State now = open.poll();
				if(data[now.nowy][now.nowx]== 'G' && now.bit == (1 << countM) -1){
					ans = now.cost;
					break;
				}
				for(int i = 0; i < 4; i++){
					int xx = now.nowx + vx[i];
					int yy = now.nowy + vy[i];
					int nextcost = now.cost + cost[now.nowy][now.nowx];
					int nextbit = now.bit;
					if(! isOK(xx, yy))continue;
					if(data[yy][xx] == 'M'){
						int ind = bitmap.get(yy * h + xx);
						nextbit = nextbit | (1 << ind); 
					}
					if(close[yy][xx][nextbit] <= nextcost) continue;
					open.add(new State(xx, yy, nextcost, nextbit));
					close[yy][xx][nextbit] = nextcost;
				}
			}
			System.out.println(ans);
		}
	}

	private void set(int y, int x) {
		cost[y][x] = INF;
		for(int i = -2; i <= 2; i++){
			for(int j = -2; j <= 2; j++){
				int xx = x + j;
				int yy = y + i;
				if(! isOK(xx, yy)) continue;
				int res = Math.max(Math.abs(x - xx), Math.abs(y - yy));
				int dis = 4 - res;
				if(dis == 4) continue;
				cost[yy][xx] = Math.max(cost[yy][xx], dis);
			}
		}
	}

	private boolean isOK(int xx, int yy) {
		if(0 <= xx && xx < w && 0 <= yy && yy < h)return true;
		return false;
	}

	private void debug(Object... o) {
		System.out.println("debug = " + Arrays.deepToString(o));
	}

	public static void main(String[] args) {
		new Main().doit();
	}
}

Measured memory_bytes: 34832
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <queue>
#include <deque>
#include <algorithm>
#include <stack>
#include <functional>
#include <sstream>
#define rep(i,j,n) for(i=j;i<(n);i++)
#define rrep(i,j,n) for(i=j;i>(n);i--)
typedef long long int lli;
#define N 1000000007
#define M 998244353

using namespace std;

int main(){
    lli n,a,Max=0,Middle,Min=N;
    double Mid;
    vector<lli> data;
    lli i,j;
    cin>>n;
    rep(i,0,n){
        cin>>a;
        data.push_back(a);
        Max=max(Max,a);
        Min=min(Min,a);
    }
    Mid=(double)Max/2.0;
    Middle=Min;
    rep(i,0,n){
        if(abs(data[i]-Mid)<abs(Mid-Middle)){
            if(data[i]!=Max){
                Middle=data[i];
            }
        }
    }
    cout<<Max<<" "<<Middle<<endl;
}


Measured memory_bytes: 1400
#include <stdio.h>
#include <math.h>
int main(void)
{
	int a, b, c;
	double S, L, h, pi;
	pi = 3.141592653589793;
	scanf("%d %d %d", &a, &b, &c);
	S = 0.5*a*b*sin(c*pi / 180);
	L = a + b + sqrt(a*a + b*b - 2*(a * b)*cos(c*pi / 180));
	h = b*sin(c*pi / 180);
	printf("%f\n%f\n%f\n", S, L, h);
}


Measured memory_bytes: 2112
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);

        int h = sc.nextInt();
        int n = sc.nextInt();
        Magic[] magic = new Magic[n];

        for (int i = 0; i < n; i++) {
            magic[i] = new Magic(sc.nextInt(), sc.nextInt());
        }

        long[] dp = new long[h + 1];

        //  i = 使う魔法のindex, j = 与えるダメージの合計
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= Math.min(magic[i].attack, h); j++) {
                if (dp[j] == 0) {
                    dp[j] = magic[i].consume;
                } else {
                    dp[j] = Math.min(dp[j], magic[i].consume);
                }
            }
            for (int j = 0; j <= h; j++) {
                // このindexはダメージのはみ出しを丸める
                int index = Math.min(j + magic[i].attack, h);
                if (dp[j] == 0) {
                    dp[index] = magic[i].consume;
                } else if (dp[index] == 0) {
                    dp[index] = dp[j] + magic[i].consume;
                } else {
                    // i種類目の魔法を使った場合と使わなかった場合の答えを比較する
                    dp[index] = Math.min(dp[index], dp[j] + magic[i].consume);
                }

                if (index == h) {
                    break;
                }
            }
        }

        System.out.println(dp[h]);
    }

    private static class Magic {
        private int attack;
        private int consume;

        private Magic(int attack, int consume) {
            this.attack = attack;
            this.consume = consume;
        }
    }
}


Measured memory_bytes: 26552
#include<iostream>
#include<math.h>
using namespace std;
int n,m;
#define N 100010

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        if(m%n==0) cout<<m/n<<endl;//特殊情况
        else
        {
            for(int i=m/n;i>=1;i--)
            {
                if(m%i==0&&m/i>n)
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int MOOD=1000*1000*1000+7;
bool isSquare( long long n) {
  for ( long long i = 0; i < n / 2 + 2; i++) {
    if (i * i == n) {
      return true;
    }
  }
  return false;
}
int main()
{
	int n, d;
	cin>>n>>d;
	vector<vector<long long>>A(n, vector<long long>(d));
	for(int i=0; i<n; i++)
	for(int j=0; j<d; j++)
	{
		cin>>A[i][j];
	}
	long long counter=0;
	for(int i=0; i<n; i++)
	for(int j=i+1; j<n; j++)
	{
		long long sum=0;
		for(int s=0; s<d; s++)
		{
			sum+=(A[j][s]-A[i][s])*(A[j][s]-A[i][s]);
		}
		if(isSquare(sum))counter++;
	}
	cout<<counter<<endl;
}


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

int main() {
    int n, l; cin >> n >> l;
    vector<string> s(n);
    rep(i, n) {
        cin >> s[i];
    }
    sort(s.begin(),s.end());
    rep(i,n) {
        cout << s[i];
    }
    cout << endl;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

const LL mod = 1000000007;

int main()
{
	LL N;
	in >> N;
	std::vector<LL>A(N);
	rep(i, N) in >> A[i];

	std::map<LL, LL>cnt;
	rep(i, N) ++cnt[A[i]];

	if ((N % 2 == 0 && cnt[0] > 0) || (N % 2 == 1 && cnt[0] != 1))
	{
		out << 0 << std::endl;
		return 0;
	}

	std::vector<LL>fact(N + 1);
	fact[0] = 1;
	for (LL i = 1; i <= N; ++i) fact[i] = (fact[i - 1] * i) % mod;

	LL ans = 1;
	if (N % 2 == 0)
	{
		for (LL i = 1; i < N; i += 2) (ans *= fact[cnt[i]]) %= mod;
		for (LL i = 0; i < N; i += 2)
		{
			if (cnt[i] > 0)
			{
				ans = 0;
				break;
			}
		}
	}
	else
	{
		for (LL i = 0; i < N; i += 2) (ans *= fact[cnt[i]]) %= mod;
		for (LL i = 1; i < N; i += 2)
		{
			if (cnt[i] > 0)
			{
				ans = 0;
				break;
			}
		}
	}
	out << ans << std::endl;
}


Measured memory_bytes: 8064
import java.util.Scanner;

public class Main {
    private final Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        System.out.println(Math.min(c, b / a));
    }
}


Measured memory_bytes: 25428
#include <bits/stdc++.h>
using namespace std;
const int maxn = 404;
int c[maxn][4];
void rotate(int id){
	int p0 = c[id][0];
	for(int j = 0; j < 3; j++) c[id][j] = c[id][j + 1];
	c[id][3] = p0;
}
long long calc(int id){
	long long val = 0;
	for(int k = 0; k < 4; k++) val = val * 1000 + c[id][k];
	return val;
}
long long has[maxn][4];
void ins(int id){
	for(int rot = 0; rot < 4; rot++){
		has[id][rot] = calc(id);
		rotate(id);
	}
	sort(has[id], has[id] + 4);
}
int get_msk(int id, long long vals[4]){
	int i = 0, j = 0, msk = 0;
	while(i < 4 && j < 4){
		if(has[id][i] == vals[j]) msk |= 1<<j, j++;
		else if(has[id][i] < vals[j]) i++;
		else j++;
	}
	return msk;
}
long long need[4];
int main(){
	int n; cin >> n;
	for(int i = 0; i < n; i++) for(int j = 0; j < 4; j++) cin >> c[i][j];
	for(int i = 0; i < n; i++){
		ins(i);
	}
	long long ans = 0;
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		for(int rotj = 0; rotj < 4; rotj++){
			int prot[4] = {};
			for(int k = 0; k < 4; k++){
				int nik = (k + 1) % 4;
				int njk0 = (5 - k) % 4;
				int njk1 = (4 - k) % 4; 
				need[k] = 0;
				for(int v : {c[i][nik], c[i][k], c[j][njk0], c[j][njk1]}){
					need[k] = need[k] * 1000 + v;
				}
				c[n][0] = c[i][nik];
				c[n][1] = c[i][k];
				c[n][2] = c[j][njk0];
				c[n][3] = c[j][njk1];
				for(int tk = 0; tk < 4; tk++){
					if(calc(n) == need[k]) prot[k]++;
					rotate(n);
				}
			}
			sort(need, need + 4);
			vector<int> cnt(1<<4);
			for(int k = 0; k < n; k++) if(k != i && k != j){
				int msk = get_msk(k, need);
				cnt[msk]++;
			}
			vector<long long> f(1<<4, 0), nf(1<<4, 0);
			f[0] = 1;
			for(int k = 1; k < 1<<4; k++){
				if(cnt[k]){
					nf = f;
					for(int sk = k; sk; sk = k & (sk - 1)){
						assert((k&sk) == sk);
						long long ways = 1;
						int bc = __builtin_popcount(sk);
						for(int z = 0; z < bc; z++) ways *= (cnt[k] - z);
						if(ways > 0){
							for(int msk = 0; msk < 1<<4; msk++){
								if((msk&sk) == 0){
									nf[msk|sk] += f[msk] * ways;
								}
							}
						}
					}
					f = nf;
				}
			}
			for(int k = 0; k < 4; k++) f[15] *= prot[k];
			ans += f[15];
			rotate(j);
		}
	}
	cout << ans / 3 << endl;
	return 0;
}


Measured memory_bytes: 764
input = STDIN.readline.split()
n = input[0].to_i
k = input[1].to_i
td_list = STDIN.readlines
td_list.map!{|line|line.split()}
hash_list = []
for td in td_list do
    t = td[0]
    d = td[1].to_i
    hash_list.push(Hash[:t, t, :d, d])
end
hash_list.sort! do |a, b|
    a[:d] <=> b[:d]
end
hash_list.reverse!
memo = 0
answer = hash_list[Range.new(0, k, true)]
counter = Hash.new(0)
for td_a in answer do
    counter[td_a[:t]] += 1
    memo += td_a[:d]
end
twice = answer.select do |twice_td|
    counter[twice_td[:t]] > 1
end
twice.sort! do |a, b|
    a[:d] <=> b[:d]
end
rest = hash_list[Range.new(k, n, true)].select do |rest_td|
    counter[rest_td[:t]] == 0
end
counts = counter.size
max = memo + (counts * counts)

twice.each_index do |i|
    while rest.size > 0 do
        p = rest.shift
        if counter[p[:t]] == 0 then
            counter[p[:t]] += 1
            memo = memo - twice[i][:d] + p[:d]
            counts += 1
            sum = memo + (counts * counts)
            if max < sum then 
                max = sum
            end
            break
        end
    end
end

puts(max)


Measured memory_bytes: 50432
#include<stdio.h>

int main() {
	int H,W,i,j;

	scanf("%d %d", &H, &W);
	while(H!=0||W!=0){

		for (i = 1; i <= H;i++) {
			if (i == 1 || i == H) {
				for (j = 0; j < W; j++) printf("#");
			}
			else {
				printf("#");
				for (j = 2; j < W; j++) printf(".");
				printf("#");
			}
			printf("\n");
		}
		printf("\n");
		scanf("%d %d", &H, &W);
	}

	return 0;
}


Measured memory_bytes: 2048
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

int main() {
  int n;
  cin >> n;
  multiset <int> ms;
  map <int, int> cnt;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ms.insert(x);
    cnt[x]++;
  }
  while (!ms.empty()) {
    int x = *(--ms.end());
    ms.erase(--ms.end());
    cnt[x]--;
    int pw = 1;
    while (pw <= x) pw *= 2;
    if (cnt[pw - x]) {
      cnt[pw - x]--;
      ans++;
      ms.erase(ms.find(pw - x));
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 18944
N = int(input())
ls = [list(map(int, input().split())) for _ in range(N)]

a = 0
for i in range(N):
    if ls[i][0] == ls[i][1]:
        a += 1
        if a == 3:
            print("Yes")
            break
    else:
        a = 0
else:
     print("No")

Measured memory_bytes: 9200
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f3f;
const int maxn = 2e5+500;
ll T,n,m;
vector<ll> num[maxn];
ll numb[maxn];
ll f[maxn];
int main() {
    cin>>n>>m;
    for(int i=0;i<n-1;i++){
    	int a,b;
    	cin>>a>>b;
    	num[a].push_back(b);
    	f[b] = a;
	}
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		numb[a]+=b;
	}
	ll sum = 0;
	for(int i=1;i<n+1;i++){
		numb[i]+=numb[f[i]];
		cout<<numb[i]<<endl;
	}
    return 0;
}



Measured memory_bytes: 16512
#define NDEBUG

#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <utility>
#include <random>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <tuple>
#include <set>
#include <map>

// [0, max)
#define FOR0(var, max) for (sl (var) = 0; (var) < (max); ++(var))
// [min, max)
#define FORi(var, min, max) for (sl (var) = (min); (var) < (max); ++(var))
// [min, max)
#define FORi_INV(var, min, max) for (sl (var) = (max) - 1; (var) + 1 > (min); --(var))

#define FORITER(var, iter) for (auto (iter) = (var).begin(); (iter) != (var).end(); (iter)++)
#define FORITER_INV(var, iter) for (auto (iter) = (var).rbegin(); (iter) != (var).rend(); (iter)++)

// a < b < c
#define LTLT(a, b, c) (((a) < (b)) && ((b) < (c)))
// a <= b < c
#define LELT(a, b, c) (((a) <= (b)) && ((b) < (c)))
// a < b <= c
#define LTLE(a, b, c) (((a) < (b)) && ((b) <= (c)))
// a <= b <= c
#define LELE(a, b, c) (((a) <= (b)) && ((b) <= (c)))

#ifndef NDEBUG
#  define MASSERT(cond) m_assert(cond, __LINE__, #cond);
#else
#  define MASSERT(...)
#endif

using namespace std;

using uc = unsigned char;
using ui = unsigned int;
using ul = unsigned long long int;
using ull = __uint128_t;

using sc = signed char;
using si = signed int;
using sl = signed long long int;
using sll = __int128_t;

using ld = long double;

void m_assert(const bool& cond, const sl& line, const char *condstr) {
  if (!cond) {
    cerr << "Assertion Failed: " << condstr << " at line " << line << endl;
    exit(1);
  }
}

namespace std {
  template <>
  struct is_integral<__int128_t> {
    constexpr static bool value = true;
  };
};

static string S;

void solve(void) {
  cout << ((S[2] == S[3] && S[4] == S[5]) ? "Yes" : "No") << endl;
}

int main(void) {
  cin >> S;
  solve();
  return 0;
}

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

#define pb push_back
#define int ll

using ll=long long;
using pii=pair<int,int>;
using vpii=vector<pii>;
using vvpii=vector<vpii>;
using vi=vector<int>;
using vvi=vector<vi>;

const int MAXL = 12;
const int DIG = 10;

// entry invariant: it's in dp[l][d]
void findin(const vvi& dp, int k, int l, int d) {
  cout << d;
  if (l == 0)
    return;
  if (d > 0) {
    if (k <= dp[l-1][d-1])
      return findin(dp, k, l-1, d-1);
    k -= dp[l-1][d-1];
  }
  if (k <= dp[l-1][d])
    return findin(dp, k, l-1, d);
  k -= dp[l-1][d];
  if (d < DIG-1) {
    if (k <= dp[l-1][d+1])
      return findin(dp, k, l-1, d+1);
  }
  assert(false);
}

void find(const vvi& dp, int k) {
  int cnt = 0;
  for (int l=0; l<MAXL; ++l) {
    for (int d=1; d<DIG; ++d) {
      if (cnt + dp[l][d] < k) {
        cnt += dp[l][d];
        continue;
      }

      return findin(dp, k-cnt, l, d);
    }
  }
}

signed main() {
    ios_base::sync_with_stdio(false);

  vvi dp(MAXL, vi(DIG));
  dp[0] = vi(DIG, 1);
  // kind of: dp[0][0] = 0;
  for (int l=1; l<MAXL; ++l) {
    for (int d=0; d<DIG; ++d) {
      dp[l][d] = dp[l-1][d];
      if (d > 0)
        dp[l][d] += dp[l-1][d-1];
      if (d < DIG-1)
        dp[l][d] += dp[l-1][d+1];
    }
  }
  //int sum=0;
  //for (int l=0; l<MAXL; ++l) {
    //for (int d=1; d<DIG; ++d)
      //sum += dp[l][d];
  //}
  //cerr << "counted " << sum << " lunluns for MAXL=" << MAXL << endl;

  int k;
  cin >> k;
  find(dp, k);
  cout << endl;
}


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

#define vvi vector<vector<int>>
#define V 100010
#define ll long long int

#define MOD 1000000007

vector<int> visited(V, V);

int main() {

    int N;
    ll K;
    cin >> N >> K;

    vvi to(V);
    int u, v;
    for (int i = 0; i < N-1; i++) {
        cin >> u >> v;
        to[u-1].push_back(v-1);
        to[v-1].push_back(u-1);
    }

    queue<int> n;
    set<int> visited;

    n.push(0);

    ll ans = K;
    ll num;

    while (!n.empty()) {
        v = n.front();
        visited.insert(v);
        n.pop();
        if (v == 0) {
            num = 1;
        }
        else {
            num = 2;
        }

        for (int i : to[v]) {
            if (visited.find(i) == visited.end()) {
                ans = ans * (K - num) % MOD;
                if (ans <= 0) {
                    cout << 0 << endl;
                    return 0;
                }
                num++;
                n.push(i);
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 11256
#include "iostream"
#include <vector>
using namespace std;

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

    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            if (N == i * j) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }

    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (long long i = 0; i < (long long) (n); ++i)
#define pb emplace_back
#define endl '\n'

template<typename T> void _dbg(const char*_s,T _h) { cerr<<_s<<" = "<<_h<<"\n"; }

template<typename T, typename... Ts> void _dbg(const char*_s,T _h,Ts... _t) {
    for(int _b=0;((_b+=*_s=='(')-=*_s==')')!=0||*_s!=',';)cerr<<*_s++;
    cerr<<" = "<<_h<<",";_dbg(_s+1,_t...);
}

#define int long long

#ifdef LOCAL
    #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
    #define dbg(...)
#endif

const int N = 500 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

int n, m;
int a[N];

bool read() {
    if (!(cin >> n >> m))
        return false;

    forn (i, n)
        cin >> a[i];

    return true;
}

void solve(int id = 0) {
    int sum = accumulate(a, a + n, 0ll);
    int cnt = 0;

    forn (i, n)
        if (a[i] * 4 * m >= sum)
            ++cnt;

    if (cnt >= m)
        cout << "Yes\n";
    else
        cout << "No\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    srand(time(nullptr));

    int T;
    T = 1;

    while (read())
        solve(T++);

    return 0;
}



Measured memory_bytes: 256
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <list>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(d) string(1,d)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

#define N_MAX (1<<18)
#define INF 1000000000000LL

int nn_mx;
long long dat_mx[(2 * N_MAX) - 1];

void init_mx(int n1) {
  nn_mx = 1;

  while (nn_mx < n1)nn_mx *= 2;

  for (int i = 0; i < 2 * nn_mx - 1; i++) {
    dat_mx[i] = -INF;
  }
}

void update_mx(int k, long long a) {
  k += nn_mx - 1;
  dat_mx[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat_mx[k] = max(dat_mx[k * 2 + 1], dat_mx[k * 2 + 2]);
  }
}

long long query_mx(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)return -INF;

  if (a <= l && r <= b) {
    return dat_mx[k];
  }
  else {
    long long vl = query_mx(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query_mx(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main() {
  ll ans = 0;
  ll n;
  cin >> n;
  ll d[200010];
  clr(d, 0);
  init_mx(n);
  rep(i, 0, n) {
    update_mx(i, 0);
  }
  vector<ll> v;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    a--;
    d[a] = i;
  }
  rep(i, 0, n) {
    ll p = d[i];
    ll l = 0;
    ll r = n - 1;
    // up
    if (p != n - 1) {
      long long low = p;
      long long high = n - 1;

      while (low < high) {
        long long mid = (high + low + 1) >> 1;
        if (query_mx(p, mid + 1, 0, 0, nn_mx) == 0) {
          low = mid;
        }
        else {
          high = mid - 1;
        }
      }
      r = low;
    }
    // down
    if (p != 0) {
      long long low = 0;
      long long high = p;

      while (low < high) {
        long long mid = (high + low) >> 1;

        if (query_mx(mid, p + 1, 0, 0, nn_mx) == 0) {
          high = mid;
        }
        else {
          low = mid + 1;
        }
      }
      l = low;
    }
    update_mx(p, 1);
    ans += (i + 1) * (r - p + 1) * (p - l + 1);
  }
  printf("%lld\n", ans);
  return 0;
}


Measured memory_bytes: 5888
n, k = map(int, input().split())
x = list(map(int, input().split()))

ans = 10 ** 9
for l in range(0, n - k + 1):
  r = l + k - 1
  a = abs(x[r]) + abs(x[r] - x[l])
  b = abs(x[l]) + abs(x[r] - x[l])
  ans = min(ans, min(a, b))

print(ans)


Measured memory_bytes: 85496
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <sstream>
#include <bitset>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, x, y;
int ans[maxn];

int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","r",stdin);
    n = readint();
    x = readint();
    y = readint();
    repd(i, 1, n - 1)
    {
        repd(j, i + 1, n)
        {
            int xx = min(j - i, abs(i - x) + abs(j - y) + 1);
            // printf("%d %d %d\n", i, j, xx );
            ans[xx]++;
        }
    }
    repd(i, 1, n - 1)
    {
        printf("%d%c", ans[i], i == n ? '\n' : '\n' );
    }
    return 0;
}




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

int main()
{
    
    int n;
    while (cin >> n) {
        int ans = 0;
        for (int i = 0; i < 600; i += n) {
            ans += n*i * i;
        }
        cout << ans << endl;
    }
    
    return 0;
}

Measured memory_bytes: 3052
using System;
using System.Linq;
using System.Collections.Generic;

namespace AtCoder_BeginerContest
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputstr = Console.ReadLine();
            var nums = inputstr.Split(' ').Select(int.Parse).OrderByDescending(n => n);
            Console.WriteLine(nums.ElementAt(0) * 10 + nums.ElementAt(1) + nums.ElementAt(2));
        }
    }
}

Measured memory_bytes: 13396
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,s,n) for(int i=s;i<n;i++)
#define rep_bit(n) for (int bit = 0; bit < (1<<n); ++bit)
#define all(x) (x).begin(),(x).end()
#define ll long long
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
using namespace std;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpi;
typedef vector<pair<ll,ll> > vpl;
typedef vector<vi> Graph;

//条件式 ? 真の場合の文 : 偽の場合の文
//abs(x) = (x の絶対値)
//string S  S,at(i),S += T,S += c,S.size(),S.substr(l)l文字目から最後まで,
///S.substr(l, r)l文字目からl+r-1文字目 n文字目から0個で空列を返せる
//*min_element(all(x)) *max_element(all(x))
//vi x; x.back(),x.front(),x.push_back(),x.pop_back()
//swap(a, b)
//gcd(a, b)最大公約数
//sort(all(x)),reverse(all(x)),sort(x.begin()+l,a.begin()+r)lからrまで
//stack<int> a; a.push(x),a.pop(),a.top(),a.empty()bool
//queue<int> b; b.push(x),b.pop(),b.front(),b.empty()bool,b.size() 要素が小さい順
//priority_queue<int, vector<int>, greater<int>> c,c.top()
//map<int, int> M1; 変数[key] = value; 変数.erase(key); 変数.at(key); 変数.count(key)所属判定　変数.size()
//find(all(x), key)boolソート済みのベクトル
//*lower_bound(all(x),k)以上の最小の要素,*uppper_bound(all(x),k)より大きい最小の要素
//'0'+cnt 数字cntからcharへの変換


int main(){

    ll n,m;
    cin >> n >> m;
    vi ans(n),pena(n);
    rep(i,m){
        int p;
        string s;
        cin >> p >> s;
        --p;
        if(ans.at(p)) continue;
        if(s == "AC") ans.at(p) = 1;
        else pena.at(p)++;
    }
    int AC = 0,PENA = 0;
    rep(i,n){
        if(ans.at(i)) {
            AC++;
            PENA += pena.at(i);
        }
    }
    cout << AC << " " << PENA << endl;

    return 0;
}


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

using ll    = long long;
using ull   = unsigned long long;
using veci  = vector<int>;
using vecll = vector<ll>;
#define EPS (1e-7)
#define INF (1e9)
#define PI  (acos(-1))
#define FOR(i,a,b) for(int i = (a);i < (b); ++i)
#define REP(i,n) for(int i= 0;i<(int)(n);++i)
#define pb push_back

veci enum_div(int n){
	veci ret;
	ret.pb(1);
	for(int i=1;i*i<n;i++){
		if(n%i==0){
			ret.pb(i);
			if(i != n/i){ret.pb(n/i);}
		}
	}
	ret.pb(n);
	return ret;
}
int gcb(int a,int b){return ((b == 0)? a : gcb(b,a%b));}
int gcd(int a,int b){return (a*b) / gcb(a,b);}

int main(){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    if(a+b > c+d){
        cout << "Left" << endl;
    }else if(a+b == c+d){
        cout << "Balanced" << endl;
    }else{
        cout << "Right" << endl;
    }
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
//#include<stdlib.h>
//#include<string.h>
//#include<algorithm>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout);
#define pb push_back
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define sf(x) scanf("%d",&x)
#define sf2(x,y) scanf("%d %d",&x,&y)
#define sf3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sl2(x,y) scanf("%lld %lld",&x,&y)

#define sl3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define sd(x) scanf("%lf",&x);
#define pLL pair<long long,long long>
#define pDB pair<double,double>
#define ff first
#define sn second
#define PRINT_CASE  printf("Case %d: ",tc++)
#define PRINT_CASENL  printf("Case %d:\n",tc++)
//#define mx 100005
#define lnd tree[ind<<1]
#define rnd tree[(ind<<1)+1]
#define cnd tree[ind]
#define lndp b,(b+e)>>1,(ind<<1)
#define rndp ((b+e)>>1)+1,e,(ind<<1)+1
#define IN(a,x,y) (a>=x && a<=y)
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
/// int other=mask^((1<<n)-1);
typedef long long ll;
typedef long long int lln;
typedef pair<int,int> pii;
ll INF=1<<28;
//typedef long long lld;
const double PI=acos(-1.0);
//int fx[]={1,-1,0,0}; //direction array
//int fy[]={0,0,1,-1};
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int knight[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1};
const long double EPS=1e-7;
//#define INF 10000

struct compare
{
  bool operator()(const pair<int,pii>& l, const pair<int,pii>& r)
  {
      if(l.ff==r.ff)
      {
          return l.sn.ff>r.sn.ff;
      }
      return l.ff<r.ff;
  }
};
// bool operator<(pair<int,pii> l, pair<int,pii> r)
//  {
//      if(l.ff==r.ff)
//      {
//          cout<<"ok "<<endl;
//          return l.sn.ff<r.sn.ff;
//      }
//      return l.ff>r.ff;
//  }

/////////////////////////////////////template sesh/////////////////////////////////////////////////////

///code goes from here
const int mx=300015;
///
int mod=1e9+7;
ll arr[mx];
int parent[mx];
int find_parent(int u)
{
    if(parent[u]==u)
        return u;
    return parent[u]=find_parent(parent[u]);
}
void unionn(int u,int v)
{
    int x=find_parent(u);
    int y=find_parent(v);
    if(x!=y)
    {
        parent[y]=x;
    }
}
int vis[mx];
int main()
{
//    SPEED;
    int t,tc=1;
    t=1;
    while(t--)
    {
        int n,m;
        sf2(n,m);
        int x,y;
        for(int i=0;i<=n;i++)
        {
            parent[i]=i;

        }
        for(int i=0;i<m;i++)
        {
            sf2(x,y);
            unionn(x,y);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            vis[find_parent(i)]++;
        }
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,vis[i]);
        }
        cout<<ans<<endl;

    }



    return 0;
}



Measured memory_bytes: 5320
// abc095_a
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  string s;
  int res = 700;
  cin >> s;
  for (int i = 0; i < 3; i++) {
    if (s[i] == 'o') {
      res += 100;
    }
  }
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 256
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
llint gya(llint x){
	llint ans=1;
	for(int h=0;h<30;h++){
		if((mod-2)&(1<<h)){ans*=x;ans%=mod;}
		x*=x;x%=mod;
	}
	return ans;
}
int main(void){
	llint i,n;cin>>n;
	llint kai=1,ans=0;
	for(i=1;i<=n;i++){kai*=i;kai%=mod;}
	vector<llint>rui(n+1);
	for(i=1;i<=n;i++){
		rui[i]=gya(i)*kai +rui[i-1];
		rui[i]%=mod;
	}
	for(i=0;i<n;i++){
		llint A;cin>>A;
		llint bai=(mod+rui[i+1]+rui[n-i]-kai)%mod;
		ans+=A*bai;ans%=mod;
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 1024
N = int(input())
ans = 10**5

def base(x, n):
  A = 0
  while x != 0:
    A += x % abs(n)
    if n < 0:
      x = - ( (-x)//n )
    else:
      x //= n
  return A

for i in range(N+1):
  ans = min(ans, base(i, 6) + base(N-i, 9))

print(ans)

Measured memory_bytes: 9108
#include <bits/stdc++.h>
#define inf 1000000000 //1E+9
#define mod 1000000007
using namespace std;

int main(){
	int n,q;cin>>n>>q;
	string s;cin>>s;
	int l[q],r[q];for(int i=0;i<q;i++){cin>>l[i]>>r[i]; l[i]--;r[i]--;}
	vector<int> ruiseki(n,0);
	for(int i=0;i<n-1;i++){
		if(s[i]=='A'&&s[i+1]=='C')ruiseki[i+1]=ruiseki[i]+1;
		else ruiseki[i+1]=ruiseki[i];
	}


	//for(int i=0;i<n;i++)cout<<ruiseki[i]<<endl;
	
	for(int i=0;i<q;i++){
		if(l[i]==0)cout<<ruiseki[r[i]]<<endl;
		else cout<<ruiseki[r[i]]-ruiseki[l[i]]<<endl;
	}
	
    return 0;
}



Measured memory_bytes: 2176
import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val n = sc.nextInt()
  val m = sc.nextInt()
  val ab = (1..m).map { 
    val ai = sc.nextInt()
    val bi = sc.nextInt()
    Pair(ai, bi)
  }
  val map = mutableMapOf<Int, Int>()
  for (abi in ab) {
    val ai = abi.first
    val bi = abi.second
    val aiC = map.getOrElse(ai) { 0 }
    map.put(ai, aiC + 1)
    val biC = map.getOrElse(bi) { 0 }
    map.put(bi, biC + 1)
  }

  val ans = mutableListOf<Int>()
  for (i in 1..n) {
    val tmp = map.getOrElse(i) { 0 }
    ans.add(tmp)
  }

  println(ans.joinToString("\n"))

}


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

void reconstruction(int left,int right);
int find(int c);
vector<int> post;

int n,*pre,*in,i;

int main(){
	cin>>n;
	pre = new int[n]; in = new int[n];
	for(int i=0;i<n;i++) cin>>pre[i]; for(int i=0;i<n;i++) cin>>in[i];

	reconstruction(0,n);
	for(int i=0;i<n;i++){
		if(i) cout<<" ";
		cout<<post[i];
	}
	cout<<endl;

	delete [] pre; delete [] in;
	cin.sync(); cin.get();
}

void reconstruction(int left,int right){
	if(left>=right) return;
	if(i<n){
		int c = pre[i++];
		int m = find(c);
		reconstruction(left,m);
		reconstruction(m+1,right);
		post.push_back(c);
	}
}

int find(int c){
	for(int i=0;i<n;i++)
		if(c==in[i]) return i;
}

Measured memory_bytes: 3144
#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<math.h>

using namespace std;

const long long MOD = 1000000007;

int main(){
	int a,b;
	cin >> a >> b;
	int now,cnt=0;
	if(b<=1){
		cout << cnt << endl;
		return 0;
	}else{
		now=1;
		while(1){
			if(now>=b)break;
			now--;
			now+=a;
			cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}


Measured memory_bytes: 256
//#include "pch.h"
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <iomanip>
#include <math.h> 
#include <stack>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <tuple>
#include <cctype>
#include <ctype.h>
#include <set>
#include <sstream>
#include <time.h>
#include <numeric>
using namespace std;
//#define int long long
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
#define rrep(i,s,n) for(int i = (n)-1;i>=(s);i--)
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a,b) a=min((a),(b))
#define chmax(a,b) a=max((a),(b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i,j) ((i<0 && j<0) || (i>0 && j>0)) ? true : false 
typedef long long ll;
typedef pair<int, int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265359;
int ca[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

//r のときは i 回目のじゃんけんで筐体がグーを出すことを、s のときはチョキを出すことを、p のときはパー
ll N, K, R, S, P;
string T, mohan;
vector<bool>ngsitahe(101000, false);
vector<bool>nguehe(101000, false);
vector<ll>nglist;
ll maxn = 0;

int func(int index, string str, ll sum) {

	for (int i = index; i < T.length(); i++) {
		if (T[i] == 'r') {
			if (str.length() >= K && str[str.length() - K] == 'p') {
				if (i + K < T.length() && T[i + K] != 'p') {
					str += 's';
					//	func(i + 1, str, sum);
					//	str.pop_back();
				}
				else if (i + K < T.length() && T[i + K] != 's') {
					str += 'r';
					//func(i + 1, str, sum);
				}
				else {
					str += 'r';
				}
			}
			else {
				str += "p";
				sum += P;
			}
		}
		else if (T[i] == 's') {
			if (str.length() >= K && str[str.length() - K] == 'r') {
				if (i + K < T.length() && T[i + K] != 'r') {
					str += 'p';
					//func(i + 1, str, sum);
					//str.pop_back();
				}
				else if (i + K < T.length() && T[i + K] != 'p') {
					str += 's';
					//func(i + 1, str, sum);
				}
				else {
					str += 's';
				}
			}
			else {
				str += "r";
				sum += R;
			}
			
		}
		else if (T[i] == 'p') {
			if (str.length() >= K && str[str.length() - K] == 's') {
				if (i + K < T.length() && T[i + K] != 's') {
					str += 'r';
					//	func(i + 1, str, sum);
					//	str.pop_back();
				}
				else if (i + K < T.length() && T[i + K] != 'r') {
					str += 'p';
					//func(i + 1, str, sum);
				}
				else {
					str += 'p';
				}
			}
			else {
				str += "s";
				sum += S;
			}
		}
		
	}

	maxn = max(maxn, sum);

	return 0;
}

signed main() {
	cin >> N >> K >> R >> S >> P >> T;

	string str;

	func(0, str, 0);

	cout << maxn << endl;


	return 0;
}

Measured memory_bytes: 640
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int x = sc.nextInt();

        int count = 0;
        for (int ia = 0; ia <= a; ia++) {
            for (int ib = 0; ib <= b; ib++) {
                for (int ic = 0; ic <= c; ic++) {
                    if (500 * ia + 100 * ib + 50 * ic == x) {
                        count++;
                    }
                }
            }
        }

        System.out.println(count);
   }
}


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

int main(){
    int n;
    cin >> n;
    int a[110000];
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    int q;
    cin >> q;
    for(int i=0; i<q; i++){
        int k;
        cin >> k;
        cout << lower_bound(a, a+n, k) - a << " " << upper_bound(a, a+n, k) - a << endl;
    }
    return 0;
}


Measured memory_bytes: 3492