akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template<class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;


struct union_find {
    vector<ll> par;
    vector<ll> rank;
    vector<ll> count;

    union_find(ll n) : par(n, 0), rank(n, 0), count(n, 1) {
        for(ll i = 0; i < n; ++i){ par.at(i) = i; }
    }

    ll find(ll x){
        if(par.at(x) == x){
            return x;
        }else{
            return par.at(x) = find(par.at(x));
        }
    }

    void unite(ll x, ll y){
        x = find(x);
        y = find(y);
        if(x == y){ return; }
        if(rank.at(x) < rank.at(y)){
            par.at(x) = y;
        }else{
            par.at(y) = x;
            if(rank.at(x) == rank.at(y)){
                rank.at(x)++;
            }
        }

        ll new_count = count.at(x) + count.at(y);
        count.at(find(x)) = new_count;
    }

    bool same(ll x, ll y){
        return find(x) == find(y);
    }

    ll size(ll x){
        return count.at(find(x));
    }
};


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

    vector<array<ll, 3>> xy(n);
    REP(i, n){
        cin >> xy.at(i).at(0) >> xy.at(i).at(1);
        xy.at(i).at(0)--;
        xy.at(i).at(1)--;
        xy.at(i).at(2) = i;
    }
    sort(ALL(xy));

    union_find uf(n);

    {
        set<pair<ll, ll>> ys1;
        REP(i, n){
            const auto& a = xy.at(i);
            const ll idx = a.at(2);
            const ll y = a.at(1);
            auto it = ys1.lower_bound(make_pair(y, 0));
            if(it != ys1.begin()){
                --it;
                uf.unite(it->second, idx);
            }
            if(ys1.size() && ys1.begin()->first < y){
                uf.unite(ys1.begin()->second, idx);
            }

            ys1.insert(make_pair(y, idx));
        }
    }

    {
        set<pair<ll, ll>> ys2;
        RREP(i, n){
            const auto& a = xy.at(i);
            const ll idx = a.at(2);
            const ll y = a.at(1);
            auto it = ys2.lower_bound(make_pair(y, 0));
            if(it != ys2.end()){
                uf.unite(it->second, idx);
            }
            if(ys2.size() && ys2.rbegin()->first > y){
                uf.unite(ys2.rbegin()->second, idx);
            }

            ys2.insert(make_pair(y, idx));
        }
    }

    REP(i, n){
        cout << uf.size(i) << endl;
    }
    return 0;
}


Measured memory_bytes: 25212
import java.util.*;
public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void myout(Object text){System.out.println(text);}
	public static String getStr(){return sc.next();}
	public static int getInt(){return sc.nextInt();}
	public static Long getLong(){return sc.nextLong();}
	public static boolean isNext(){return sc.hasNext();}
	public static void main(String[] args){
		//String tmp = getStr();
		//int tmp = getInt();
		//Long tmp = getLong();
      int N = getInt();
      ArrayList<Integer> list = new ArrayList<Integer>();
      for(int i = 0; i < N; i++){
        list.add(getInt());
      }
      Collections.sort(list,Comparator.reverseOrder());
      //同じ値が2個*2種類、または4個*1種類出現していればひとまず問題ない
      
      ArrayList<Integer> hindo = new ArrayList<Integer>();
      int[] countList = new int[100000];
      Arrays.fill(countList,0);
      int counter = 0;
      for(int i = 0; i < N; i++){
        int tmp = list.get(i);
        if(i == 0){
          hindo.add(tmp);
          countList[0] = 1;
          continue;
        }
        if(hindo.get(counter) == tmp){
          countList[counter]++;
        }else{
          hindo.add(tmp);
          countList[counter + 1]++;
          counter++;
        }
      }
      int fourCount = -1;
      int twoCount1 = -1;
      int twoCount2 = -1;
      Long fourMen = new Long(-1);
      Long twoMen = new Long(-1);
      for(int i = 0; i < N; i++){
        if(countList[i] >= 4){
          Long tmpLong = new Long(hindo.get(i));
          fourMen = tmpLong * tmpLong;
          break;
        }
      }
      for(int i = 0; i < N; i++){
        if(countList[i] >= 2){
          if(twoCount1 == -1){
            twoCount1 = hindo.get(i);
            continue;
          }else if(twoCount2 == -1){
            twoCount2 = hindo.get(i);
            twoMen = new Long(twoCount1) * new Long(twoCount2);
            break;
          }
        }
      }
      if(fourMen != -1 && twoMen != -1){
        myout(Math.max(fourMen, twoMen));
      }else if(fourMen != -1 && twoMen == -1){
        myout(fourMen);
      }else if(fourMen == -1 && twoMen != -1){
        myout(twoMen);
      }else{
        myout(0);
      }
      //myout(list);
      //myout(fourMen);
      //myout(twoMen);
	}
}


Measured memory_bytes: 58952
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int X = sc.nextInt();
		int Y = sc.nextInt();		
		//2行目
		int numsX[]=new int[N];
		for(int a=0;a<N;a++){
			numsX[a]=sc.nextInt();
		}
		//3行目
		int numsY[]=new int[M];
		for(int a=0;a<M;a++){
			numsY[a]=sc.nextInt();
		}
		sc.close();
		Arrays.sort(numsX);
		Arrays.sort(numsY);
		int Z = numsY[0];
		
		if(numsX[N-1]<numsY[0] && X < numsY[0] && numsY[0] <= Y){
			System.out.print("No ");
		}
		System.out.println("War");
			
	}
}

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

int main(){
  long long n,k;
  cin >> n >> k;

  long long MOD = 1000000007;
  long long ans = 0;
  for (long long i = k; i <= n+1; i++)
  {
    long long l = i*(i-1)/2;
    long long r = i*(2*n-i+1)/2;
    ans += r-l+1;
    ans %= MOD;
  }
  cout << ans << endl;  
}

Measured memory_bytes: 3568
import java.util.Scanner;

public class Main {

	static Scanner scanner;

	public static void main(String[] args) {
	    scanner = new Scanner(System.in);

	    int a=gi();
	    int b=gi();

        //System.out.println(t/x);
        if (b%a==0) {
        	System.out.print(a+b);
        }else {
        	System.out.print(b-a);
        }


	}

	// 文字列として入力を取得
	public static String gs() {
		return scanner.next();
	}

	// intとして入力を取得
	public static int gi() {
		return Integer.parseInt(scanner.next());
	}

	// longとして入力を取得
	public static long gl() {
		return Long.parseLong(scanner.next());
	}

	// doubleとして入力を取得
	public static double gd() {
		return Double.parseDouble(scanner.next());
	}
}

Measured memory_bytes: 35608
#include <stdio.h>
int main()
{
    int f=0,n,s,i,j,k;
    scanf("%d%d",&n,&s);
    for(i=0;i<=s/10000;i++)
    {for(j=0;j<=s/5000;j++)
    {
        k=n-i-j;
        if(i*10000+j*5000+k*1000==s && k>=0 && k<=s/1000)
        {
            f=1;printf("%d %d% d\n",i,j,k);
            break;
        }
    }
    if(f) break;
    }
    if(!f) printf("-1 -1 -1\n");
    return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <unordered_map>
#include <vector>

int main()
{
    std::string s, t;
    std::cin >> s >> t;

    std::unordered_map<char, std::vector<int>> char_map;

    for (int i = 0; i < s.length(); i++) {
        char_map[s[i]].push_back(i);
    }
    for (int i = 0; i < s.length(); i++) {
        char_map[s[i]].push_back(i + s.length());
    }

    long long ans = 0;
    int ptr = 0;

    for (auto c : t) {
        if (!char_map[c].size()) {
            std::cout << -1 << std::endl;
            return 0;
        }

        ptr = *lower_bound(char_map[c].begin(), char_map[c].end(), ptr) + 1;

        if (ptr > s.length()) {
            ptr -= s.length();
            ans += s.length();
        }
    }

    ans += ptr;
    std::cout << ans << std::endl;
}


Measured memory_bytes: 1724
mod = 1000000007


s = raw_input()
n_a = n_ab = n_abc = 0
p = 1

for c in s:
    if c == "A":
        n_a = (n_a + p) % mod
    elif c == "B":
        n_ab = (n_ab + n_a) % mod
    elif c == "C":
        n_abc = (n_abc + n_ab) % mod
    elif c == "?":
        n_abc = (n_abc * 3 + n_ab) % mod
        n_ab = (n_ab * 3 + n_a) % mod
        n_a = (n_a * 3 + p) % mod
        p = (p * 3) % mod

    # print n_a, n_ab, n_abc

print n_abc % mod


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

int main()
{
    //freopen("input.txt","r", stdin);
    //freopen("output.txt","w",stdout);
    string s; cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(i==3) cout<<8;
        else cout<<s[i];
    }
}

Measured memory_bytes: 3636
import java.util.*;
 
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    String S = sc.next();
    
    if(K >= S.length()) {
      System.out.println(S);
    } else {
      System.out.println(S.substring(0, K) + "...");
    }
  }
}

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

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

  int point = 1;
  for(int i=0;i<N;i++){
    if(2 * point > point + K){
      point += K;
    } else {
      point *= 2;
    }
  }

  cout << point << endl;
}


Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author dyominov
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ATOrT solver = new ATOrT();
        solver.solve(1, in, out);
        out.close();
    }

    static class ATOrT {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int a = in.nextInt();
            int b = in.nextInt();
            out.println(n * a < b ? n * a : b);
        }

    }

    static class InputReader implements AutoCloseable {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

        public void close() {
        }

    }
}



Measured memory_bytes: 22996
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;

int main() {
  
  int a,b,c,d;
  string s,ss,sss;
  long long ans = 0;
  string anss = "";
  
  cin >> a >> b >> c;

  if(c >= a && c <= b) anss = "Yes";
  else anss = "No";
  
  cout << anss << endl;

}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){

  int a,b,x,y;
  cin>>a>>b>>x>>y;
  x-=a;y-=b;
  char m[4]={'U','D','L','R'};
  if(y<0){
    swap(m[0],m[1]);
    y*=-1;
  }
  if(x<0){
    swap(m[2],m[3]);
    x*=-1;
  }
  for(int j=0;j<2;j++){
    for(int i=0;i<y;i++) cout<<m[j];
    for(int i=0;i<x;i++) cout<<m[3-j];
  }
  for(int j=0;j<2;j++){
    cout<<m[2+j];
    for(int i=0;i<y+1;i++) cout<<m[j];
    for(int i=0;i<x+1;i++) cout<<m[3-j];
    cout<<m[1-j];
  }
  cout<<endl;
  return 0;

}



Measured memory_bytes: 256
#include <bits/stdc++.h>
 
using namespace std;
 
#define repr(i,a,b) for (int i=a; i<b; i++)
#define rep(i,n) for (int i=0; i<  n; i++)
 
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; }
 
const long long INF = 1LL << 60;
#define PI 3.14159265359 
 
template<typename T>
void remove(std::vector<T>& vector, unsigned int index)
{
    vector.erase(vector.begin() + index);
}
 
int gcd(int a, int b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}
 
long long lcm (int a, int b){
    return  (long long) a*b /gcd(a,b);
}




int main(){
    long long L, R;
    cin >> L >> R;

    long long ans = 2018;
    if(R-L >2020){
        cout << 0 << endl;
    }

    else{
        for(long long i = L; i<R ;i++){
            for(long long j = i+1; j<=R; j++){
                long long a = (i*j)%2019;
                ans = min(ans,a);
            }
        }
    cout << ans << endl;
    }
    

}

Measured memory_bytes: 3636
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int P = sc.nextInt();
    int Q = sc.nextInt();
    int R = sc.nextInt();
    int result = 201;
    if (P+Q < result) {
      result = P+Q;
    }
    if (Q+R < result) {
      result = Q+R;
    }
    if (P+R < result) {
      result = P+R;
    }
    System.out.println(result);
  }
}

Measured memory_bytes: 23892
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem B

if __name__ == '__main__':
    button_count = int(input())
    button_rules = {i + 1: int(input()) for i in range(button_count)}

    count = 1
    result = button_rules[count]

    # HACK: More smarter.
    while (result != 2) and (count < 10 ** 5):
        result = button_rules[result]
        count += 1

    if count < 10 ** 5:
        print(count)
    else:
        print(-1)


Measured memory_bytes: 17892
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    k, x = map(int, input().split())

    left = x-k+1
    right = x + k
    ans = [i for i in range(left, right)]

    print(' '.join(map(str, ans)))

if __name__ == '__main__':
    main()


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll p1[2202020];
ll p10[2202020];
const ll mo=998244353;

template<class V,int NV> class SegTree_2 {
public:
	vector<V> sum,val;
	SegTree_2(){sum.resize(NV*2,0);val.resize(NV*2,0);};
	
	void update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r) return;
		if(x<=l && r<=y) {
			sum[k]=v*p1[r-l]%mo;
			val[k]=v;
		}
		else if(l < y && x < r) {
			if(val[k]>=0) {
				val[2*k]=val[2*k+1]=val[k];
				sum[2*k]=sum[2*k+1]=val[k]*p1[(r-l)/2]%mo;
				val[k]=-1;
			}
			update(x,y,v,l,(l+r)/2,k*2);
			update(x,y,v,(l+r)/2,r,k*2+1);
			sum[k]=(sum[2*k]+sum[2*k+1]*p10[(r-l)/2])%mo;
		}
	}
};
SegTree_2<ll,1<<20> st;
int N,Q,L,R,D;


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	p10[0]=1;
	FOR(i,2201010) {
		p1[i+1]=(p1[i]*10+1)%mo;
		p10[i+1]=(p10[i]*10)%mo;
	}
	
	cin>>N>>Q;
	st.update(0,N,1);
	FOR(i,Q) {
		cin>>L>>R>>D;
		R=N-R;
		L=N-L;
		st.update(R,L+1,D);
		cout<<st.sum[1]<<endl;
	}
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


Measured memory_bytes: 70468
# AOJ 0523: Card GameI
# Python3 2018.6.30 bal4u

while True:
	n = int(input())
	if n == 0: break
	c = [1] * (2*n+1)
	for i in range(1, n+1): c[int(input())] = 0
	m = [n]*2
	t, ba = 0, 0
	while m[0] > 0 and m[1] > 0:
		f = 1
		for i in range(ba+1, 2*n+1):
			if t == c[i]:
				ba = i
				c[i] = 2
				m[t] -= 1
				f = 0
				break
			if i == 2*n: ba = 0
		if f: ba = 0
		t = 1-t
	print(m[1], m[0], sep='\n')


Measured memory_bytes: 5600
N,X = map(int,input().split())
positions_from_X = [abs(int(x)-X) for x in input().split()]

n = min(positions_from_X)

if N==1:
    flg = n
else:
    for d in sorted([i for i in range(1,n+1) if n%i==0], reverse=True):
        flg = d
        for p in positions_from_X:
            if p%d !=0:
                flg=0
                break
        if flg:
            break

print(flg)

Measured memory_bytes: 14228
# 01-BFS

from collections import deque

H, W = map(int, input().split())
CH, CW = map(int, input().split())
DH, DW = map(int, input().split())
# list(文字列)で1文字ごと分解される
S = [list(input()) for _ in range(H)]

q = deque([(CH - 1, CW - 1, 0)])

while q:
    h, w, c = q.popleft()
    if S[h][w] == '.':
        # 既に訪れたマスにはc(魔法を使う回数)を代入する
        S[h][w] = c
        for i in range(-2, 3):
            for j in range(-2, 3):
                if 0 <= h + i < H and 0 <= w + j < W and S[h + i][w + j] == '.':

                    # (i,j) = (0,0)の場合
                    if abs(i) + abs(j) == 0:
                        continue

                    # (i,j) = (1,0), (0,1), (-1,0), (0,-1)の場合
                    elif abs(i) + abs(j) == 1:
                        q.appendleft((h + i, w + j, c))

                    # それ以外の20通りの場合
                    elif abs(i) + abs(j) >= 2:
                        q.append((h + i, w + j, c + 1))

# 更新されていない場合
if S[DH - 1][DW - 1] == '.':
    print(-1)
# 更新されている場合
else:
    print(S[DH - 1][DW - 1])


Measured memory_bytes: 246996
def gcd(a,b)
  if a % b == 0
    b
  elsif b % a == 0
    a
  elsif a < b
    gcd(a, b % a)
  else
    gcd(a % b, b)
  end
end

n = gets.to_i
ary = gets.split.map(&:to_i)

p ary.reduce { |a,b| gcd(a,b) }


Measured memory_bytes: 8452
#include <iostream> // cout, endl, cin
#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 <math.h> // sqrt
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> data(N);
    for (int i = 0; i < N; i++) {
        cin >> data.at(i);
    }
    sort(data.begin(),data.end());
    int sum = 0;
    int ans = 0;
    for (int x : data) {
        ans += x * x;
    }
    for (int i = data.at(0); i < data.at(N - 1) + 1; i++) {
        sum = 0;
        for (int j = 0; j < N; j++) {
            if(data.at(j) < i) {
                sum += (i - data.at(j)) * (i - data.at(j));
            }
            else {
                sum += (data.at(j) - i) * (data.at(j) - i);
            }
        }
        ans = min(ans, sum);
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-9;
const long double PI=acos(-1.0);
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
long double ABS(long double a){return max(a,-a);}
char in[110];
int main(){
	int a;scanf("%d",&a);
	scanf("%s",in);int ret=0;
	for(int i=1;i<a;i++){
		set<char>L;
		set<char>R;
		for(int j=0;j<a;j++){
			if(j<i)L.insert(in[j]);
			else R.insert(in[j]);
		}
		int cnt=0;
		for(int j=0;j<a;j++){
			if(L.count('a'+j)&&R.count('a'+j))cnt++;
		}
		ret=max(ret,cnt);
	}
	printf("%d\n",ret);
}

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define no cout<<"No"<<endl
#define NO cout<<"NO"<<endl
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};


ll mpow(ll m, ll p){
  ll power = m;
  ll res = 1;
  while(p>0){
    if(p&1)
      res = res*power%MOD;
    power = (power*power)%MOD;
    p /= 2;
  }

  return res;
}

ll mod_inv(ll m){
  return mpow(m, MOD-2);
}

struct COM{
  vll fact, fact_inv;
  COM(ll n): fact(n+1,1), fact_inv(n+1,1){
    for(ll i=1; i<=n; i++)
      fact[i] = fact[i-1]*i%MOD;

    fact_inv[n] = mod_inv(fact[n]);
    for(ll i=n; i>=1; i--)
      fact_inv[i-1] = fact_inv[i]*i%MOD;
  }

  ll calc(ll n, ll k){
    if(k<0 || n<k)
      return 0;
    ll res = fact[n]*fact_inv[n-k]%MOD*fact_inv[k]%MOD;
    return res;
  }
};


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

  ll n,m;
  cin>>n>>m;


  COM com(m);

  ll ans=0;
  REP(i,n+1){
    ll remind=com.fact[m-i]*com.fact_inv[m-n]%MOD;
    ll now=com.calc(n,i)*remind%MOD*pow(-1,i%2);
    ans=(ans+now+MOD)%MOD;
  }

  ans=com.calc(m,n)*com.fact[n]%MOD*ans%MOD;
  cout<<ans<<endl;

}



Measured memory_bytes: 11024
sx,sy,tx,ty=map(int,input().split())
x,y=tx-sx,ty-sy

if x>=0 and y>=0:
  ans=["U"]*y+["R"]*x+["D"]*y+["L"]*x+\
  	  ["L"]+["U"]*(y+1)+["R"]*(x+1)+["D"]+\
      ["R"]+["D"]*(y+1)+["L"]*(x+1)+["U"]
elif x>=0 and y<0:
  y=abs(y)
  ans=["D"]*y+["R"]*x+["U"]*y+["L"]*x+\
  	  ["L"]+["D"]*(y+1)+["R"]*(x+1)+["U"]+\
      ["R"]+["U"]*(y+1)+["L"]*(x+1)+["D"]
elif x<0 and y>=0:
  x=abs(x)
  ans=["U"]*y+["L"]*x+["D"]*y+["R"]*x+\
  	  ["R"]+["U"]*(y+1)+["L"]*(x+1)+["D"]+\
      ["L"]+["D"]*(y+1)+["R"]*(x+1)+["U"]
elif x<0 and y<0:
  x,y=abs(x),abs(y)
  ans=["D"]*y+["L"]*x+["U"]*y+["R"]*x+\
  	  ["R"]+["D"]*(y+1)+["L"]*(x+1)+["U"]+\
      ["L"]+["U"]*(y+1)+["R"]*(x+1)+["D"]
      
      
print("".join(ans))

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

int main() {
    int ans = 9999999;
    int N;
 string S;
 cin>>N>>S;
    vector<int> sW(N+1,0);
    vector<int> sE(N+1,0);
 for(int i=0;i<N;i++){
     sW[i+1]+=sW[i];
     sE[i+1]+=sE[i];
     if(S[i] =='W')
         sW[i+1]++;
     if(S[i] == 'E')
         sE[i+1]++;
 }
 int sum;
 for(int i=0;i<N;i++){
 sum = sW[i]+sE[N]-sE[i+1];
 if(ans>sum){
     ans = sum;
 }
 }
 cout<<ans<<endl;
}

Measured memory_bytes: 3076
import java.io.*;
import java.util.*;

public class Main{
    static public void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //String[] buf = reader.readLine().split(" ");
        String s = sc.next(), t = sc.next();
        System.out.println(t+s);
    }
}

Measured memory_bytes: 22740
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    string S;
    cin >> S;

    int cnt = 1;
    char ct = S[0];
    repd(i, 1, S.size()) {
        if (ct != S[i]) {
            ct = S[i];
            cnt++;
        }
    }

    cout << cnt - 1 << endl;
    return 0;
}

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

#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,x,n) for(int i=x; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define all(X) (X).begin(),(X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

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

template<class A, size_t N, class T> void Fill(A (&a)[N], const T &v){ fill( (T*)a, (T*)(a+N), v ); }

template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"; return os;}
template<class T,size_t n> ostream& operator<<(ostream &os, const array<T,n> &t) {os<<"{"; rep(i,n) {os<<t[i]<<",";} os<<"}"; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const ll INF = 1e9+7;


int main(){
	ios_base::sync_with_stdio(false);
	int N, M;
	int A[195];

	cin >> N >> M;
	rep(i,N) cin >> A[i];

	int sum = 0;
	rep(i,N) sum += A[i];

	int cnt = 0;
	rep(i,N) if(A[i] * 4 * M >= sum) cnt++;

	cout << (cnt >= M ? "Yes" : "No") << endl;

	return 0;
}

Measured memory_bytes: 256
def check
  while line = STDIN.gets.chomp
    break unless line.empty?
  end

  a,b,p = line.split ' '

  begin
    coin_a = Integer(a)
    coin_b = Integer(b)
    prod = Integer(p)
  rescue
    return false
  end

  return false unless (1<=coin_a && coin_a<=500)
  return false unless (1<=coin_b && coin_b<=500)
  return false unless (1<=prod && prod<=1000)

  return (coin_a+coin_b)>=prod
end


def main
 print check() ? 'Yes' : 'No'
end

main()


Measured memory_bytes: 1788
N, M = map(int, input().split(' '))
ac_set = set()
wa_cnt_list = [0] * N
ac_cnt, wa_cnt = 0, 0
for i in range(M):
    num, res = input().split(' ')
    num = int(num) - 1
    if num not in ac_set:
        if res == 'AC':
            ac_cnt += 1
            wa_cnt += wa_cnt_list[num]
            ac_set.add(num)
        else:
            wa_cnt_list[num] += 1
print(ac_cnt, wa_cnt)

Measured memory_bytes: 18044
package main

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

var N int
var M int

func main() {
	N = nextInt()
	M = nextInt()
	F := make([]int, M)
	for i := 0; i < N; i++ {
		K := nextInt()
		for j := 0; j < K; j++ {
			KX := nextInt()
			F[KX-1] += 1
		}
	}
	ans := 0
	for i := 0; i < M; i++ {
		if F[i] == N {
			ans += 1
		}
	}
	fmt.Printf("%v", ans)

}

func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}

func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}

var nextReader func() string

func init() {
	nextReader = NewScanner()
}

func NewScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}

func Min(a int, b int) int {
	if a > b {
		return b
	}
	return a
}

func MaxIn2Values(a int, b int) int {
	if a > b {
		return a
	}
	return b
}

func MinIn2Values(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func Abs(a int) int {
	if a < 0 {
		return a * -1
	}
	return a
}


Measured memory_bytes: 640
#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int h,w,n;
    cin>>h>>w>>n;
    int a[n];
    int ans[h+1][w+1];
    for(int i=0;i<n;i++)cin>>a[i];
    int cnt=0,i=0;
    int x=0,y=0;
    int d=1;
    while(true){
        if(y==h)break;
        if(x==-1){d=1;y++;x=0;}
        if(x==w){d=-1;y++;x=w-1;}
        ans[y][x]=i+1;
        cnt++;
        if(cnt==a[i]){i++;cnt=0;}
        x+=d;
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}


Measured memory_bytes: 384
a = gets.chomp.to_i
    b = gets.chomp.to_i
    c = gets.chomp.to_i
    d = gets.chomp.to_i
    ans = 0
    ans+=b*c if a>b
    ans+=a*c if a <=b
    ans += (a-b)*d if a>b
    puts ans

Measured memory_bytes: 1788
from collections import defaultdict
n,t = map(int,input().split())
a = [int(i) for i in input().split()]
dct = defaultdict(int)
mv = mk = 0
for i in range(n)[::-1]:
    k = mv-a[i]
    if k > 0: dct[k] += 1
    mk = max(mk,k)
    mv = max(mv,a[i])
print(dct[mk])

Measured memory_bytes: 20960
n = int(input())
arr = list(map(int,input().split()))
arr.sort(reverse = True)
k=2
som=arr[0]
if n%2==0:
    for i in range(1,n):
        if k==n:
            break
        som+=2*arr[i]
        k+=2
else:
    for i in range(1,n-1):
        if k==n-1:
            break
        som+=2*arr[i]
        k+=2
    som+=arr[i]
print(som)

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;

#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MAX=100005;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#define dbgmint(n,arr) rep(i,n){cerr<<arr[i].x<<" ";}cerr<<endl;
#define out(a) cout<<a<<endl
#define out2(a,b) cout<<a<<" "<<b<<endl
#define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second

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

template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }

template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,v.size())i>>v[j];return i;}

// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
    int len=v.size();
    for(int i=0; i<len; ++i) {
        s<<v[i];
        if(i<len-1) s<<"	";
    }
    return s;
}

// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
    int len=vv.size();
    for(int i=0; i<len; ++i) {
        s<<vv[i]<<endl;
    }
    return s;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<std::setprecision(10);

    string s;
    cin>>s;
    string ans;
    rep(i,s.length()){
        if(s[i]=='0' || s[i]=='1'){
            ans += s[i];
        }else{
            if(ans.length()>0){
                ans.erase(ans.begin()+ans.length()-1);
            }
        }
    }
    cout<<ans<<endl;
    return  0;
}


Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <cstdlib>
#include <utility>
#include <map>

using namespace std;

long long int uc(long long int a, long long int b){
    if(b % a == 0){
        return a;
    }else{
        return uc(b % a, a);
    }
}

int main(){
    long long int n, x;
    cin >> n >> x;
    long long int p[n + 1];
    long long int dp[n];
    for(int i = 0; i < n + 1; i++){
        if(i == n){
            p[n] = x;
        }else{
            cin >> p[i];
        }
    }
    sort(p, p+n+1);
    for(int i = 0; i < n; i++){
        dp[i] = p[i + 1] - p[i];
    }
    sort(dp, dp+n);
    long long int ans = 0;
    if(n == 1){
        ans = dp[0];
    }else{
        for(int i = 0; i < n - 1; i++){
            if(i == 0){
                ans = uc(dp[i], dp[i+1]);
            }else{
                ans = uc(ans, dp[i+1]);
            }
        }
    }
    cout << ans;
    
}


Measured memory_bytes: 1792
#include <cstdio>

using namespace std;

int main(){
	while(true){
		int L;
		scanf("%d", &L);
		if(L == 0){ break; }
		int answer = 0, sum = 0;
		for(int i = 1; i <= 12; i++){
			int M, N;
			scanf("%d%d", &M, &N);
			sum += (M - N);
			if(answer == 0 && sum >= L){ answer = i; }
		}
		if(answer == 0){
			puts("NA");
		}else{
			printf("%d\n", answer);
		}
	}
	return 0;
}

Measured memory_bytes: 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int descending_compare(const void *a, const void *b){
    if (*(int*)a > *(int*)b){
        return -1;
    }else if (*(int*)a == *(int*)b){
        return 0;
    }else{
        return 1;
    }
}

int ascending_compare(const void *a, const void *b){
    if (*(int*)a < *(int*)b){
        return -1;
    }else if (*(int*)a == *(int*)b){
        return 0;
    }else{
        return 1;
    }
}


int lower_bound(int *a, int n, int key){
    int left, mid, right;
    left = 0, right = n;
    mid = (left + right)/2;
    while ((left+1 != mid || mid+1 != right) && mid != left){
        if (key > a[mid]){
            left = mid;
        }else{
            right = mid+1;
        }
        mid = (left + right)/2;
    }
    if (a[left] >= key)return left;
    if (a[mid] >= key)return mid;
    if (a[right] >= key)return right;
    return n;
}

//greatest common divisor
unsigned long  gcd(unsigned long x, unsigned long y){
    if (y == 0){ 
        return x;
    }else if (x > y){
        return gcd(y, x % y);
    }else{
        return gcd(x, y % x);
    }
}



long long factorial(int x){
    long long rtn = 1;
    int i;
    for (i = x; i > 1; i--){
        rtn = (rtn*i);
    }
    return rtn;
}

int main(void){
    char a, b;
    scanf("%c %c", &a, &b);
    if (a == 'H'){
        printf("%c\n", b);
    }else if(b == 'H'){
        printf("D\n");
    }else{
        printf("H\n");
    }

    return 0;
}


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

vector<vector <long long>> dp(301, vector<long long>(90000));
vector<vector <long long>> dph(301, vector<long long>(90000));
const long long md = 998244353;

long long add(long long a, long long b) {
	return (a % md + b % md) % md;
}

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

	int M = 0;
	for (int i = 0; i < N; i++) {
		cin >> a[i];
		M += a[i];
	}

	long long harf = 0;
	if (M % 2 == 0) {
		dph[0][0] = 1;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < dph[0].size(); j++) {
				if (dph[i][j] != 0) {
					dph[i + 1][j] = add(dph[i + 1][j], dph[i][j]);
					dph[i + 1][j + a[i]] = add(dph[i + 1][j + a[i]], dph[i][j]);
				}
			}
		}
		harf = dph[N][M / 2];
	}

	M = (M - 1) / 2;
	dp[0][0] = 1;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < dp[0].size(); j++) {
			if (dp[i][j] != 0) {
				dp[i + 1][j] = add(dp[i + 1][j], dp[i][j]);
				dp[i + 1][j] = add(dp[i + 1][j], dp[i][j]);
				dp[i + 1][j + a[i]] = add(dp[i + 1][j + a[i]], dp[i][j]);
			}
		}
	}

	long long outcase = 0;
	for (int i = M + 1; i < dp[0].size(); i++) {
		outcase += dp[N][i];
	}

	outcase = outcase - harf;
	if (outcase < 0) outcase += md;
	outcase = (outcase * 3) % md;

	long long all = 1;
	for (int i = 0; i < N; i++) {
		all *= 3;
		all %= md;
	}

	long long ans = all - outcase;
	if (ans < 0) ans += md;

	cout << ans << endl;
}

Measured memory_bytes: 426560
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <time.h>
int main(){
    int h,a;
    scanf("%d %d",&h,&a);
    if(h%a==0){
        printf("%d",h/a);
        return 0;
    }
    printf("%d",h/a+1);
    return 0;
}

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

ll n,m,x,y;
ll w[100001],h[100001];

vector<vector<ll> > v(100001);

int main() {
    cin>>n>>m;
    for(ll i=1;i<=n;i++)
        cin>>h[i];
    for(ll i=1;i<=m;i++)
    {
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    ll o=0,ans=0;
    for(ll i=1;i<=n;i++)
    {
        o=0;
        for(ll j=0;j<v[i].size();j++)
            if(h[i]<=h[v[i][j]])
            {
                o=1;
                break;
            }
        if(o)
            continue;
        else
            ans++;
    }
    cout<<ans;
}


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

using namespace std;

bool dp1[3001];
bool dp2[3001];


int main(){
    int A, B, C, D, E, F, i, j;
    cin >> A >> B >> C >> D >> E >> F;

    dp1[0] = true;
    dp2[0] = true;
    for(i = A; i <= F; i++) dp1[i] |= dp1[i-A];
    for(i = B; i <= F; i++) dp1[i] |= dp1[i-B];
    for(i = C; i <= F; i++) dp2[i] |= dp2[i-C];
    for(i = D; i <= F; i++) dp2[i] |= dp2[i-D];

    int a1 = 1, a2 = 0;
    for(i = 1; i * 100 <= F; i++) {
        for(j = 0; 100 * i + j <= F; j++) {
            if(j > i*E || !dp1[i] || !dp2[j]) continue;
            int v1 = 100 * i + j, v2 = j;
            if(a2*v1 <= a1*v2) {
                a1 = v1;
                a2 = v2;
            }
        }
    }

    printf("%d %d\n", a1, a2);

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

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

  vector<int> mochi;
  int curr;
  for (int i = 0; i < n; i ++) {
    cin >> curr;
    mochi.push_back(curr);
  }

  sort(mochi.begin(), mochi.end());

  set<int> s(mochi.begin(), mochi.end());
  cout << s.size() << "\n";
}


Measured memory_bytes: 256
import sys

N, X, M = map(int, input().split())

cnt = [0] * M
accu = [X]
cnt[X] = 1
for i in range(2,min(N+1, M+3)):
    s = (accu[-1] ** 2) % M
    if cnt[s] == 0:
        cnt[s] = i
        accu.append(s)
    else:
        c = i - cnt[s] 
        su = sum(accu[cnt[s]-1:])
        ans = sum(accu[:cnt[s]-1])
        N -= (cnt[s] - 1)
        n = N // c
        ans += n * su
        N %= c
        ans += sum(accu[cnt[s]-1:cnt[s]+ N -1])
        print(ans)
        sys.exit()

print(sum(accu))
    
    
    


Measured memory_bytes: 13540
#include<bits/stdc++.h>
using namespace std;
int main(){
    vector<int> v(6);
    vector<int> m(10);
    for(int i=0; i<v.size(); i++){
        cin >> v[i];
    }
    m.push_back(v[1]-v[0]);
    m.push_back(v[2]-v[0]);
    m.push_back(v[3]-v[0]);
    m.push_back(v[4]-v[0]);
    m.push_back(v[2]-v[1]);
    m.push_back(v[3]-v[1]);
    m.push_back(v[4]-v[1]);
    m.push_back(v[3]-v[2]);
    m.push_back(v[4]-v[2]);
    m.push_back(v[4]-v[3]);
    sort(m.begin(), m.end(), greater<int>());
    if(m[0]<=v[5]){
        cout << "Yay!" << endl;
    }else{
        cout << ":(" << endl;
    }
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> p;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, n) for(int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for(int i = ((int)(n)-1); i >= 0; i--)
#define srep(i, from, to) for(int i = from; i < (int)(to); i++)
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template <class T> bool chmax(T &a, const T &b) {
  if(a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if(b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<vector<int>> g;
vector<ll> counter;
vector<ll> result;

void rec(int now, int prev, ll sum) {
  sum += counter[now];
  result[now] = sum;

  for(auto next : g[now]) {
    if(next == prev) continue;
    rec(next, now, sum);
  }

  sum -= counter[now];
}

int main() {
  int q;
  cin >> n >> q;
  g = vector<vector<int>>(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  counter = vector<ll>(n);
  result = vector<ll>(n);
  rep(i, q) {
    int p;
    ll x;
    cin >> p >> x;
    counter[p - 1] += x;
  }

  rec(0, -1, 0);

  rep(i, n) {
    cout << result[i] << endl;
  }

  return 0;
}


Measured memory_bytes: 25856
import java.util.*;

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 d = sc.nextInt();
        int count = 0;
        for(int i = 0;i<=100;i++){
            if(Math.max(a,c) <= i && Math.min(b,d) > i){
                count++;
            }
        }
        System.out.println(count);
    }
}

Measured memory_bytes: 21716
/* -> Written by <-
   -----------
  |K_A_Z_A_M_A|
  |___________|
  |    _    |
  |   (^_^)   |
  |  /( | )\  |
  |____|_|____|
    H O A N G
*/

#define Task ""
#define F first
#define S second
#define push_back pb
#define bit(x, i) (x >> i) & 1
#define inf 1e9 + 7
#define INF 1e18 + 7
#define all(x) x.begin(), x.end()

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <string>
#include <sstream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>

using namespace std;

typedef long long ll;

const int MOD = 998244353;
const double eps = 1e-6;
const int maxn = 1e5 + 5;

int n, m;
vector <int> a[maxn];
int par[maxn];

void dfs(int u)
{
    for (auto v : a[u])
        if (v != par[u])
        {
            if (par[v]) continue;
            par[v] = u;
            dfs(v);
        }
}

void Solve()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i ++)
    {
        int u, v;
        cin >> u >> v;
        a[u].pb(v);
        a[v].pb(u);
    }
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
        if (par[i] == 0)
        {
            cnt ++;
            dfs(i);
        }
    cout << cnt - 1;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    if(fopen(Task".inp", "r"))
    {
        freopen(Task".inp","r",stdin);
        freopen(Task".out","w",stdout);
    }
    int test_case = 1;
//    cin >> test_case;
    while (test_case --)
        Solve();
    return 0;
}


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

int main()
{
    string chk;
    int ac, wa, tle;
    int N;
    ac = 0;
    wa = 0;
    tle = 0;

    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> chk;
        if (chk == "AC")
        {
            ac++;
        }
        if (chk == "WA")
        {
            wa++;
        }
        if (chk == "TLE")
        {
            tle++;
        }
    }
    cout << "AC x " << ac << endl;
    cout << "WA x " << wa << endl;
    cout << "TLE x " << tle << endl;
    cout << "RE x " << N - ac - wa - tle << endl;
    return (0);
}


Measured memory_bytes: 3600
import java.util.*;
 
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] chars = new int[n][26];
		for(int i=0; i<n; i++){
			String s = sc.next();
			for(int j=0; j<s.length(); j++){
				chars[i][ (int)(s.charAt(j)-'a') ]++;
			}
		}
		
		String ans="";
		for(int i=0; i<26; i++){
			int min = Integer.MAX_VALUE;
			for(int j=0; j<n; j++){
				min = Math.min(min,chars[j][i]);
			}
			
			for(int j=0; j<min; j++){
				ans += (char)(i+'a');
			}
		}
		System.out.println(ans);
		return;
	}
}

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

int main(){
  char b;
  cin >> b;
  if(b=='A'){
    cout << 'T' << endl;
  }
  else if(b=='T'){
    cout << 'A' << endl;
  }
  else if(b=='G'){
    cout << 'C' << endl;
  }
  else{
    cout << 'G' << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <string>
#include <sstream>
#include <cmath>
#include <climits>
#include <algorithm>
#include <iterator>
#include <stdio.h>
#include <memory.h>

using namespace std;

//#define DEBUG_MODE

#ifdef DEBUG_MODE
#include <fstream>
//#include "prettyprint.hpp"
#endif // DEBUG_MODE

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<PII> VII;

#define A1D(T)              vector<T>
#define A2D(T)              vector<vector<T> >
#define sz(x)               ((int)(x).size())
#define maxof(x)            numeric_limits<x>::max()
#define minof(x)            numeric_limits<x>::min()
#define lowbit(x)           ((x)&-(x))
#define log2(x)             (log((double)x)/log(2.0))
#define _FORu(T,i,l,h,k)    for(T i=(l);i<=(h);i+=(k))
#define _FORd(T,i,h,l,k)    for(T i=(h);i>=(l);i-=(k))
#define FOR(i,n)            _FORu(int,i,0,n,1)
#define FORu1(i,l,h)        _FORu(int,i,l,h,1)
#define FORuk(i,l,h,k)      _FORu(int,i,l,h,k)
#define FORd1(i,h,l)        _FORd(int,i,h,l,1)
#define FORdk(i,l,h,k)      _FORd(int,i,h,l,k)
#define FOREACH(e,s)        for(__typeof__((s).begin()) e=(s).begin();e!=(s).end();++e)
#define IO_OPT              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifdef DEBUG_MODE
#define debug(...)          fprintf(stderr, __VA_ARGS__)
#define DEBUG(x)            cerr << '>' << #x << ':' << x << endl << flush;
#else
#define debug(...)
#define DEBUG(x)
#endif // DEBUG_MODE

const double PI      = acos(-1.0);
const double E       = exp(1.0);
const double EPS     = 1e-8;
const int HALF_INF   = 0x3f3f3f3f;

#define MOD 1000000007

class Solver
{
public:
    void Solve()
    {
        int n;
        scanf("%d", &n);
        LL i = 1, ans = 1;
        while(i <= n) {
            ans = (ans * i) % MOD;
            i++;
        }
        printf("%lld\n", ans);
    }
};

int main()
{
#ifdef DEBUG_MODE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w+", stdout);
#endif // DEBUG_MODE

    Solver * solver = new Solver();
    solver->Solve();

    return 0;
}


Measured memory_bytes: 256
import sys
import numpy as np
from math import ceil as C, floor as F, sqrt
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
 
ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

s = S()
t = S()
print('Yes' if s == t[:-1] else 'No')

Measured memory_bytes: 27056
#include <stdio.h>
#define MAX 100

int count = 0;

void print_array(int *A, int N) {
  int i;
  for(i = 0; i < N - 1; i++) {
    printf("%d ", A[i]);
  }
  printf("%d\n", A[i]);
}

void selectionSort(int *A, int N) {
  int i, j, minj, v;
  for(i = 0; i < N; i++) {
    minj = i;
    for(j = i; j < N; j++) {
      if(A[j] < A[minj]) minj = j;
    }
    if(minj != i) {
      v = A[i];
      A[i] = A[minj];
      A[minj] = v;
      count++;
    }
  }
}

int main() {
  int n, i, a[MAX];
  scanf("%d", &n);
  for(i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  selectionSort(a, n);
  print_array(a, n);
  printf("%d\n", count);
  return 0;
}



Measured memory_bytes: 1956