akhauriyash/Code-Regression huggingface dataset:
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int x = sc.nextInt();
        long ans=(x/500)*1000l;
        x%=500;
        ans+=(x/5)*5;
        pw.println(ans);
        pw.flush();
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

}

Measured memory_bytes: 21204
N=gets.to_i
F=[]
N.times{
    F<<gets.split.map(&:to_i)
}
P=[]
N.times{
    P<<gets.split.map(&:to_i)
}
max=-10**10
(1 ... 2**10).each{|i|
    s="%010b" % i
    sum=0
    N.times{|j|
        c=0
        10.times{|k|
            if s[k]=="1" && F[j][k]==1
                c+=1
            end
        }
        sum+=P[j][c]
    }
    if sum>max
        max=sum
    end
}
puts max


Measured memory_bytes: 14148
import copy

N, W = list(map(int, input().split()))
w = [0 for i in range(N)]
v = [0 for i in range(N)]
for i in range(N):
    w[i], v[i] = list(map(int, input().split()))




s = [[0 for i in range(W+1)] for i in range(N)]


for i in range(W+1):
    if i < w[0]:
        s[0][i] = 0
    else :
        s[0][i] = v[0]

for i in range(1,N):
    for j in range(w[i]):
        s[i][j] = s[i-1][j]
    for j in range(w[i], W+1):
        s[i][j] = max(s[i-1][j], s[i-1][j-w[i]] + v[i])
#print(s)
print(s[-1][-1])

Measured memory_bytes: 170760
#include<bits/stdc++.h>
#define fasterthanlight ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define test int T; cin>>T; while(T--)
#define endl "\n"
#define int long long
#define vi vector<int>
#define pb push_back
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define hi "visited\n" //sasta debugging
using namespace std;
 
//author::@whohet-->Het Patel

signed main()
{
	fasterthanlight;
	int n;
	cin>>n;
	if(n>=30)
	{
		cout<<"Yes\n";
	}
	else
		cout<<"No\n";

	//cout << "\n\n"<<"Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
	return 0;
}

Measured memory_bytes: 3636
/*
　　　  ∧＿∧　やあ
　　 （´・ω・｀)　　　　　/　　　　　ようこそ、バーボンハウスへ。
　　 ／∇y:::::＼　　　 [￣]　　　　　このテキーラはサービスだから、まず飲んで落ち着いて欲しい。
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|　うん、「また」なんだ。済まない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　  仏の顔もって言うしね、謝って許してもらおうとも思っていない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|
　　　　∇　∇　∇　∇　　　／.／|　　　でも、この提出を見たとき、君は、きっと言葉では言い表せない
　　　　┴　┴　┴　┴　／ ／　  |　　　「ときめき」みたいなものを感じてくれたと思う。
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　殺伐としたコンテストの中で、そういう気持ちを忘れないで欲しい
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　そう思って、この提出を投げたんだ。
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 じゃあ、判定を聞こうか。
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define ALL(obj) (obj).begin(),(obj).end()
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i = (b-1);i>=a;i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n) 
#define SIZE(x) ((int)(x).size())
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef pair<int, int> pint;
typedef pair<lint, lint> plint;
typedef vector<lint> vec;
typedef vector<vector<lint>> matrix;
typedef priority_queue<lint> p_que;
typedef priority_queue<lint, vector<lint>, greater<lint>> p_que_rev;
const lint INF = INT_MAX;
const lint LINF = LLONG_MAX;
const lint MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

lint gcd(lint a, lint b) {
    lint r;
    while (b != 0) {
        r = a % b;
        a = b; 
        b = r;
    }
    return a;
}

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

lint power(lint x, lint n, lint mod = MOD) {
    lint ret = 1;
    while(n > 0) {
        if(n & 1){
            (ret *= x) %= mod;
        }
        (x *= x) %= mod;
        n >>= 1;
    }
    return ret;
}

vector<lint> make_power(int n, lint base){
    lint num = 1;
    vector<lint> ret;
    for (int i=0; i<=n; ++i){
        ret.push_back(num);
        num *= base;
    }
    return ret;
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> p(n);
    REP(i, n){
        cin >> p[i];
    }
    bool check = true;
    for (int i=0; i<n; ++i){
        for (int j=i; j<n; ++j){
            swap(p[i], p[j]);
            check = true;
            for (int k=0; k<n-1; ++k){
                if(p[k] > p[k+1]){
                    check = false;
                    break;
                }
            }
            if(check){
                cout << "YES" << endl;
                //debugpair(i, j);
                return 0;
            }
            swap(p[i], p[j]);
        }
    }
    cout << "NO" << endl;
    return 0;
}

Measured memory_bytes: 256
let rec gcd n m = match (max n m) mod (min n m) with
    | 0 -> min n m
    | a -> gcd a (min n m)

let () = Scanf.scanf "%d\n" @@ fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    Printf.printf "%d" @@ Array.fold_left gcd a.(0) a


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

using namespace std;

typedef pair<int, int> P;

int S[200000];
int T[200000];
int X[200000];
int D[200000];
P p[200000];
vector<P> ans;

int main(){
    int N, Q;
    set<int> st;
    cin >> N >> Q;
    for(int i = 0; i < N; i++) {
        cin >> S[i] >> T[i] >> X[i];
        p[i] = P(X[i], i);
    }
    sort(p, p+N);
    for(int i = 0; i < Q; i++) {
        cin >> D[i];
        st.insert(D[i]);
    }
    for(int i = 0; i < N; i++){
        int idx = p[i].second;
        int l = S[idx] - p[i].first;
        int r = T[idx] - p[i].first;
        auto ptrl = st.lower_bound(l);
        auto ptrr = st.lower_bound(r);
        for(auto iter = ptrl; iter != ptrr; iter++){
            if(iter == ptrr) break;
            ans.push_back(P(*iter, p[i].first));
        }
        st.erase(ptrl, ptrr);
    }
    for(auto iter = st.begin(); iter != st.end(); iter++){
        ans.push_back(P(*iter, -1));
    }
    sort(ans.begin(), ans.end());
    for(int i = 0; i < ans.size(); i++){
        cout << ans[i].second << endl;
    }
}

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

int dp[110][100010];


signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, W; cin >> N >> W;
    int w[110], v[110];
    rep(i, N) cin >> w[i] >> v[i];
    rep(i, 110) rep(j, 100010) dp[i][j] = INF;
    dp[0][0] = 0;
    rep(i, N) {
        rep(j, 100010) {
            dp[i + 1][j] = dp[i][j];
            if (j - v[i] >= 0) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
        }
    }
    int ans = 0;
    rep(i, 100010) if (dp[N][i] <= W) ans = max(ans, i);
    cout << ans << endl;
    

    return 0;
}


Measured memory_bytes: 86272
#include<stdio.h>

int count=0;

int merge(int A[], int left, int mid, int right)
{
	int n1=mid-left;
	int n2=right-mid;
	int i,j,k;
	int L[250001];
	int R[250001];
	
	for(i=0;i<=n1-1;i++) L[i]=A[left+i];
	for(i=0;i<=n2-1;i++) R[i]=A[mid+i];
	L[n1]=1000000001;
	R[n2]=1000000001;
	i=0;
	j=0;
	for(k=left;k<=right-1;k++)
	{
		count++;
		if(L[i]<=R[j])
		{
			A[k]=L[i];
			i++;
		}
		else
		{
			A[k]=R[j];
			j++;
		}
	}
}

int mergeSort(int A[], int left, int right)
{
	int mid;
	if(left+1<right)
	{
		mid=(left+right)/2;
		mergeSort(A,left,mid);
		mergeSort(A,mid,right);
		merge(A,left,mid,right);
	}
}

int main(void)
{
	int i,n;
	int S[500000];
	
	scanf("%d",&n);
	for(i=0;i<=n-1;i++) scanf("%d",&S[i]);
	mergeSort(S,0,n);
	for(i=0;i<=n-1;i++)
	{
		if(i) printf(" ");
		printf("%d",S[i]);
	}
	printf("\n%d\n",count);
	return 0;
}

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

int main() {
  string s;
  int a;
  cin >>s;
  if(s[0]=='1') a++;
  if(s[1]=='1')a++;
  if(s[2]=='1')a++;

  cout << a << endl;
  return 0;
}


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

using namespace std;

#define int long long
#define ll long long

ll read()
{
    int x=0;        bool f=0;        char k=getchar();
    while(k< '0'||k> '9') (k=='-')&&(f=1),k=getchar();
    while(k>='0'&&k<='9') x=(x*10)+(k&15),k=getchar();
    return (f)?(-x):(x);
}

const int maxn=1e5+5;
int z[maxn],top;

bool check(ll n)
{
    ll ma=sqrt(n);
    for(int i=2;i<=ma;i++)
    {
        if(!(n%i))
        {
            return 0;
        }
    }
    return 1;
}

signed main()
{
    ll a=read(),b=read();
    ll n=__gcd(a,b),ans=0;
    ll ma=sqrt(n);
    if(ma*ma==n)
    {
        if(check(ma))ans++;
        for(int i=1;i<ma;i++)
        {
            if(!(n%i))
            {
                if(check(i))
                {
                    ans++;
                }
                if(check(n/i))
                {
                    ans++;
                }
            }
        }
    }
    else
    {
        for(int i=1;i<=ma;i++)
        {
            if(!(n%i))
            {
                if(check(i))
                {
                    ans++;
                }
                if(check(n/i))
                {
                    ans++;
                }
            }
        }
    }
    printf("%lld",ans);
}

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

int main(void){
    int n, m;
    cin >> n >> m;
    
    if(n * n <= m){
        while(n <= m){
            if(m % n == 0){
                cout << m / n << endl;
                return 0;
            }
            n++;
        }
    }else{
        int i = m / n;
        while(i >= 1){
            if(m % i == 0){
                cout << i << endl;
                return 0;
            }
            i--;
        }
    }
    
    return 0;
}


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

int main(){
    int N;
    cin>>N;
    vector<int> card(N);
    for(int i=0;i<N;i++){
        cin>>card.at(i);
    }
    sort(card.begin(),card.end());
    reverse(card.begin(),card.end());
    int Alice;
    int Bob;
    Alice=0;
    Bob=0;
    for(int i=0;i<N;i++){
        if(i%2==0){
            Alice+=card.at(i);
        }
        else {
            Bob+=card.at(i);
        }
    }
    cout<<Alice-Bob<<endl;
}

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

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

const int N=2e5+100;

ll dp[100]; ///到i个时最大的价值
int a[100];
int main()
{

    ios::sync_with_stdio(0);cin.tie(0);
    int n;cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    dp[1]=1000;
    for(int i=1;i<n;i++){
        dp[i+1]=max(dp[i],dp[i]/a[i]*a[i+1]+dp[i]%a[i]);
    }
    cout<<dp[n];
    return 0;
}



Measured memory_bytes: 3604
#n=int(input())
#a,b=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

s=list(input())
t=list(input())

n=len(s)
flag=False

for i in range(n):
    last=s.pop()
    s.insert(0,last)
    #print(s)
    if s==t:
        flag=True
        break

if flag:
    print("Yes")
else:
    print("No")

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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            //[summary]A - Double Helix
            var b = Console.ReadLine();

            switch (b)
            {
                case "A":
                    Console.WriteLine("T");
                    break;
                case "T":
                    Console.WriteLine("A");
                    break;
                case "C":
                    Console.WriteLine("G");
                    break;
                case "G":
                    Console.WriteLine("C");
                    break;
            }
        }
    }
}

Measured memory_bytes: 11092
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

@SuppressWarnings("unchecked")
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(true){
			int n = sc.nextInt();
			if(n==0)break;
			int[] in = new int[26];
			int[] out = new int[26];
			List<Integer>[] node = new List[26];
			for (int i = 0; i < node.length; i++) {
				node[i]=new LinkedList<Integer>();
			}
			for (int i = 0; i < n; i++) {
				String s = sc.next();
				out[s.charAt(0)-'a']++;
				in[s.charAt(s.length()-1)-'a']++;
				node[s.charAt(0)-'a'].add(s.charAt(s.length()-1)-'a');
			}
			boolean f = true;
			int s = 0,t=0,same=0;
			for (int i = 0; i < in.length; i++) {
				if(in[i]!=out[i]){
					f=false;
				}
			}
			boolean connected = true;
			isVisited=new boolean[26];
			int size = 0;
			for (int i = 0; i < node.length; i++) {
				if(in[i]>=1){
					size = size(i,node,new boolean[26]);
					break;
				}
			}

			for (int i = 0; i < in.length; i++) {
				if(in[i]>0)--size;
			}

			if(f&&size==0){
				System.out.println("OK");
			} else {
				System.out.println("NG");
			}
		}
	}
	static boolean[] isVisited;
	static int size(int cur,List<Integer>[]node,boolean[] visited){
		if(isVisited[cur])return 0;
		int res = 1;
		isVisited[cur]=true;
		for(Integer next:node[cur]){
			if(isVisited[next])continue;
			res+=size(next,node,visited);
		}
		return res;
		
	}
}

Measured memory_bytes: 49484
#include <stdio.h>
int main(void){
    // Your code here!
    int x,y;
    
    while(1){
        scanf("%d %d",&x,&y);
        
        if(x==0&&y==0) break;
        
        if(x>y){
            printf("%d %d\n",y,x);
        }
        
        else{
            printf("%d %d\n",x,y);
        }
    }
    
    return 0;
}



Measured memory_bytes: 1980
N = int(input()) # 数字を変数に格納
List = list(map(int,input().split())) # 複数の数字をリストに格納

result = 0
tmp = 0

for i in range(N-1):
    if List[i+1] <= List[i]:
        tmp += 1
    else:
        tmp = 0
    if tmp > result:
        result = tmp
        
print(result)

Measured memory_bytes: 14252
#include <stdio.h>


int main()
{
	int X,Y;
	long int a3=100000,a2=200000,a1=300000,s=400000;
	
	scanf("%d%d",&X,&Y);
	long int t=0;
	if(X==3)t+=a3;
	else if(X==2)t+=a2;
	else if(X==1){
		t+=a1;
	}
	if(Y==3)t+=a3;
	else if(Y==2)t+=a2;
	else if(Y==1){
		t+=a1;
	}
	if(X==1&&Y==1)t+=s;
	printf("%ld\n",t);
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\n";
/* clang-format on */

int main() {
  ll x;
  cin >> x;
  if (x >= 30) cout << "Yes\n";
  else
    cout << "No\n";
  return 0;
}


Measured memory_bytes: 3632
from copy import deepcopy
n = int(input())
a = [int(input()) for i in range(2**n)]
while len(a)!=1:
	b = []
	for i in range(len(a)//2):
		if a[i*2]==a[i*2+1]: b.append(a[i*2])
		else: b.append(max(a[i*2],a[i*2+1])-min(a[i*2],a[i*2+1]))
	a = deepcopy(b)
print(a[0])

Measured memory_bytes: 20196
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
using namespace std;
const ll MAX_N=5e5,MOD=1e9+7;
typedef pair<ll,ll> P;
ll N,A[MAX_N+1],psm[MAX_N+2],dp[MAX_N+1][2];
map<ll,vector<P> > mp;
map<ll,ll> lst;
ll mpw(ll n,ll m){
    ll ret=1;
    while(m){
        if(m&1){
            ret*=n;
            ret%=MOD;
        }
        n*=n;
        n%=MOD;
        m>>=1;
    }
    return ret;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
    ll ans=0;
	cin>>N;
    REP(i,N){
        cin>>A[i+1];
        A[i+1]^=A[i];
    }
    REP(i,N+1){
        psm[i+1]=psm[i];
        if(A[i]==0){
            psm[i+1]++;
        }
    }
    if(A[N]==0){
        REP(i,N){
            if(A[i]!=0){
                if(psm[i]-psm[lst[A[i]]]>0){
                    mp[A[i]].push_back(P(0,psm[i]-psm[lst[A[i]]]));
                    lst[A[i]]=i;
                }
                mp[A[i]].push_back(P(A[i],1));
            }
        }
        for(auto p:lst){
//            cout<<p.first<<' '<<p.second<<endl;
        }
        for(auto &i:mp){
            if(psm[N+1]-psm[lst[i.first]]>0){
                i.second.push_back(P(0,psm[N+1]-psm[lst[i.first]]));
            }
        }
        for(const auto &i:mp){
//            cout<<i.first<<' '<<i.second.size()<<endl;
            dp[0][0]=1;
            dp[0][1]=0;
            REP(j,i.second.size()){
                //cout<<"    "<<i.second[j].first<<','<<i.second[j].second<<endl;
            }
            FOR(j,1,i.second.size()){
                REP(k,2)dp[j][k]=dp[j-1][k];
                if(i.second[j].first==0){
                    dp[j][0]+=dp[j-1][1]*i.second[j].second;
                    dp[j][0]%=MOD;
                }else{
                    dp[j][1]+=dp[j-1][0]*i.second[j].second;
                    dp[j][1]%=MOD;
                }
            }
            ans+=dp[i.second.size()-2][1];
            ans%=MOD;
        }
//        cout<<psm[N+1]<<endl;
        ans+=mpw(2,psm[N+1]-2);
        ans%=MOD;
    }else{
        ll x=A[N];
        dp[0][0]=1;
        FOR(i,1,N+1){
            REP(j,2)dp[i][j]=dp[i-1][j];
            if(A[i]==0){
                dp[i][0]+=dp[i-1][1];
                dp[i][0]%=MOD;
            }else if (A[i]==x){
                dp[i][1]+=dp[i-1][0];
                dp[i][0]%=MOD;
            }
        }
        ans=dp[N-1][0];
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 86784
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cstring>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for(long long i=0; i<n; i++)
#define reps(i, s, e) for(long long i=s; i<e; i++)
#define repr(i, n) for(long long i=n-1; i>=0; i--)
#define reprs(i, s, e) for(long long i=e-1; i>=s; i--)

int main(){

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, q; cin >> n >> q;
  vector<ll> g[n];
  bool isDone[n] = {};
  rep(i, n-1){
    ll a, b;
    cin >> a >> b;
    a--; b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  ll p[q], x[q];
  rep(i, q){
    cin >> p[i] >> x[i];
    p[i]--;
  }

  ll ans[n] = {};
  rep(i, q){
    ans[p[i]] += x[i];
  }
  queue<ll> que;
  que.push(0);
  isDone[0] = true;

  while(!que.empty()){
    ll now = que.front();
    que.pop();

    rep(i, g[now].size()){
      ll nxt = g[now][i];
      if(isDone[nxt]) continue;

      ans[nxt] += ans[now];

      isDone[nxt] = true;
      que.push(nxt);
    }
  }

  rep(i, n){
    cout << ans[i] << " ";
  }

  return 0;
}

Measured memory_bytes: 21232
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <bitset>
#define range(i, r) for(int i=0;i<r;i++)
#define ranges(i, l, r) for(int i=l;i<r;i++)
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int> >
#define vvl std::vector<std::vector<ll> >
#define MODs 1000000007;
#define MODn 1000000009;
typedef long long int ll;
using namespace std;

int main(int argc, char const *argv[]) {
  int N;
  std::cin >> N;
  std::vector<int> L(2*N);
  range(i, 2*N) std::cin >> L[i];
  sort(L.begin(), L.end());
  ll ans = 0;
  range(i, N) ans += L[2*i];
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node
{
	ll v;
	ll weizhi;
};
Node a[5005];
bool cmp(Node a,Node b)
{
	if(a.v>b.v)return true;
	else if(a.v==b.v&&a.weizhi<b.weizhi)return true;
	else return false;
}
ll dp[5005][5005];
long long ans;
ll zuo;
ll you;
ll max(ll a,ll b)
{
	if(a>b)return a;
	else return b;
}
int main()
{
	int n;cin>>n;zuo=1;you=n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].v;
		a[i].weizhi=i;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			dp[i+1][j]=max(dp[i+1][j],dp[i][j]+abs(a[i+j+1].weizhi-n+i)*a[i+j+1].v);if(i+1<=n&&j<=n)ans=max(ans,dp[i+1][j]);
			dp[i][j+1]=max(dp[i][j+1],dp[i][j]+abs(a[i+j+1].weizhi-j-1)*a[i+j+1].v);if(i<=n&&j+1<=n)ans=max(ans,dp[i][j+1]);
		}
	}
	cout<<ans;
}

Measured memory_bytes: 42916
#include "bits/stdc++.h"
#define rep(i,b) for(ll i=0;i<b;i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){cin >> first;IN(rest...);return;}
#define SS(T, ...)      T __VA_ARGS__; IN(__VA_ARGS__);
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
/*--Output/////////////////////////////////////////////////*/
inline int p(void){cout << endl; return 0;}
template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;}
struct ProconInit {
    static constexpr bool AUTOFLUSH = false;
    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL<<(n))
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; }
bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; }
int cnt[2020];
int main() {
    SS(ll,x,y,A,B,C)
    SV(ll,a,A);
    SV(ll,b,B);
    SV(ll,c,C);
    vl s_apple;
    sort(a.rbegin(),a.rend());
    sort(b.rbegin(),b.rend());
    rep(i,x)s_apple.push_back(a[i]);
    rep(i,y)s_apple.push_back(b[i]);
    rep(i,C)s_apple.push_back(c[i]);
    sort(s_apple.rbegin(),s_apple.rend());
    ll ans=0;
    rep(i,x+y)ans+=s_apple[i];
    p(ans);
}

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

using namespace std;
typedef long long int ll;
void f(ll n ){
  char c;
  cout<< "? "<<n*2<<endl;
  cin>>c;
  if(c=='Y'){
    cout<<"! "<<n<<endl;
  }else{
    f(n*10);
  }
  return;
}

void h(ll n,ll m){
  ll a = (n+m)/2;
  if( m-n==2){
    cout <<"! "<<a<<endl;
    return;
  }
  
  char c;
  cout<<"? "<<a*10<<endl;
  cin>>c;
  if(c=='Y'){
    h(n,a+1);
  }else{
    h(a,m);
  }
  return;
}
    
    


void g(ll n ){
  char c;
  cout<< "? "<<n<<endl;
  cin>>c;
  if(c=='Y'){
    h(n,n*10);
  }else{
    g(n/10);
  }
  return;
}
  


int main() {
  char c;
  cout << "? 1000000000"<<endl;
  cin>>c;
  if( c=='Y'){
    f(1);
  }else{
    g(100000000);
  }
  

  return 0;
}


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

vector<int> org_graph[100005];
int three_d[3][100005];

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

    rep(i, m)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        org_graph[u].push_back(v);
    }

    int s, t;
    cin >> s >> t;
    s--, t--;

    //  make new graph
    queue<pair<int, int>> que;
    que.push(make_pair(s, 0));
    bool ok = false;
    int ans;
    // cout << "***" << endl;
    while(que.size() > 0)
    {
        pair<int, int> elem = que.front();
        que.pop();
        int now = elem.first;
        int d = elem.second;

        if (now == t and d % 3 ==0)
        {
            ok = true;
            ans = d;
            break;
        }

        for (auto next: org_graph[now])
        {
            if (three_d[(d + 1) % 3][next] != 0) continue;
            three_d[(d + 1) % 3][next] = d + 1;
            que.push(make_pair(next, d + 1));
            // cout << next + 1<< ' ' << d + 1<< endl;
        }
        // cout << "** " << que.size() << endl;
    }
    if (ok)
        cout << ans / 3 << endl;
    else
        cout << -1 << endl;

    return 0;
}

Measured memory_bytes: 6912
#include <bits/stdc++.h>
using namespace std;
const int maxn=500005;
const int mod=998244353;
int fact[maxn],inv[maxn],n,m;
inline int qpow(int x,int y)
{
	int res=1;
	while(y)
	{
		if(y&1)res=1LL*res*x%mod;
		x=1LL*x*x%mod;
		y>>=1;
    }
    return res;
}
inline void add(int &x,int y)
{
	x+=y;
	if(x>=mod)x-=mod;
}
inline int C(int x,int y)
{
	if(x<y)return 0;
	return 1LL*fact[x]*inv[y]%mod*inv[x-y]%mod;
}
int main()
{
	scanf("%d%d",&n,&m);
	fact[0]=1;
	for(int i=1;i<=500001;++i)fact[i]=1LL*fact[i-1]*i%mod;
	inv[500001]=qpow(fact[500001],mod-2);
	for(int i=500000;i>=0;--i)inv[i]=1LL*inv[i+1]*(i+1)%mod;
	int ans=0;int nowm=qpow(m+1,n),nown=qpow(n+1,m),invm=qpow(m+1,mod-2),invn=qpow(n+1,mod-2);
	for(int i=0;i<=min(n,m);++i)
	{
		add(ans,1LL*(i&1?mod-1:1)*C(n,i)%mod*C(m,i)%mod*fact[i]%mod*nowm%mod*nown%mod);
		nowm=1LL*nowm*invm%mod;nown=1LL*nown*invn%mod;
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 4096
#include <iostream>
#include <math.h>
using namespace std;

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

int N, M;
int digit[5];

int Meq0()
{
    if (N == 1) return 0;
    else return (int)pow(10, N - 1);
}

int Solve3()
{
    if (digit[1] == 0) digit[1] = 1;
    return digit[1] * 100 + digit[2] * 10 + digit[3];
}

int Solve2()
{
    if (digit[1] == 0) digit[1] = 1;
    return digit[1] * 10 + digit[2];
}

int Solve1()
{
    return digit[1];
}

void Solve()
{
    int ans;
    if (M == 0){
        ans = Meq0();
    } else {
        switch (N){
            case 3: ans = Solve3(); break; 
            case 2: ans = Solve2(); break; 
            case 1: ans = Solve1(); break;
        }
    }
    cout << ans << endl;
}

void Init()
{
    bool set[5];
    rep(i, 5) { digit[i] = 0; set[i] = false; }

    cin >> N >> M;
    int in0, in1;
    rep(i, M) {
        cin >> in0 >> in1;
        if (in0 == 1 && in1 == 0 && N != 1){ cout << "-1" << endl; exit(0); } // N = 1の場合は最上位桁が0でも可
        if (set[in0] == true && digit[in0] != in1){ cout << "-1" << endl; exit(0); }
        digit[in0] = in1;
        set[in0] = true;
    }
}

void Clean(){}

int main(){ Init(); Solve(); Clean(); return 0; }


Measured memory_bytes: 256
def main(argv)
  (n, k) = gets.chomp.split(' ').map(&:to_i)
  h = gets.chomp.split(' ').map(&:to_i)
  
  puts h.count{|x| x >= k}.to_s
end

if self.to_s == 'main' then
  main(ARGV)
end

Measured memory_bytes: 8196
import java.util.*;
public class Main {
    public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	int[] k = new int[10000];
	while(true) {
	    int n = scan.nextInt();
	    boolean ok = false;
	    if(n == 0) break;
	    int cnt = 0;
	    for(int i = 0; i < n; i++) {
		k[i] = scan.nextInt();
		if(k[i] >= 1) cnt++;
		if(k[i] >= 2) ok = true;
	    }
	    if(ok != true) {
		System.out.println("NA");
	    }else{
		System.out.println(cnt +1);
	    }
	}
    }
}

Measured memory_bytes: 158824
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int a,b,x;
	while(cin>>x>>a>>b)
	{
		int ax=abs(x-a);
		int bx=abs(x-b);
		if(ax>bx) cout<<"B\n";
		else cout<<"A\n";
	}
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <bitset>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef long long LL;

//べき剰余
long long modpow(long long x, long long n, long long m) {
	long long ans = 1;
	while (0 < n) {
		if (n % 2 == 0)x = x * x%m, n >>= 1;
		else ans = ans * x%m, n--;
	}
	return ans;
}

vector<long long> v = { 2,7,61 };

//ミラー-ラビン素数判定法
bool Miller_Rabin(long long n) {
	if (n == 2)return true;
	if (n < 2 || n % 2 == 0)return false;

	long long d = n - 1, s = 0;
	while (d % 2 == 0)
		d /= 2, s++;


	for (auto a : v) {
		if (a == n)return true;
		bool flag = false;
		long long r = modpow(a, d, n);
		if (r == 1 || r == n - 1)flag = true;
		for (int j = 0; j < s; j++) {
			r = modpow(r, 2, n);
			if (r == n - 1)flag = true;
		}
		if (!flag)return false;
	}
	return true;
}

int main() {
	while (true) {
		int a, b, n;
		cin >> a >> b >> n;
		if (a == 0 && b == 0 && n == 0)break;

		int cnt = 0, ans;
		for (int i = a; true; i += b) {
			if (Miller_Rabin(i))cnt++;
			if (cnt == n) { ans = i; break; }
		}
		cout << ans << endl;
	}
}


Measured memory_bytes: 3024
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

int n,m;
int a[105];

int main()
{
	scanf("%d%d",&n,&m);
	int odd1 = 0,odd2 = 0;
	for (int i = 1;i <= m;i++)
	{
		scanf("%d",&a[i]);
		if (a[i] & 1)
		{
			if (!odd1)
				odd1 = i;
			else if (!odd2)
				odd2 = i;
			else
			{
				puts("Impossible");
				return 0;
			}
		}
	}
	if (odd1)
		swap(a[odd1],a[1]);
	if (odd2)
		swap(a[odd2],a[m]);
	for (int i = 1;i <= m;i++)
		printf("%d ",a[i]);
	printf("\n");
	if (m == 1)
	{
		if (a[1] == 1)
		{
			puts("1");
			printf("1\n");
		}
		else
		{
			puts("2");
			printf("%d %d\n",a[1] - 1,1);
		}
		return 0;
	}
	printf("%d\n%d ",m - (a[m] == 1),a[1] + 1);
	for (int i = 2;i < m;i++)
		printf("%d ",a[i]);
	if (a[m] > 1)
		printf("%d",a[m] - 1);
	printf("\n");
	return 0;
}

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

ULL GCD(ULL a, ULL b) { return b ? GCD(b, a % b) : a; }

struct Problem {

    UL N;
    pair<LL, LL> A[200000];
    map<pair<LL, LL>, UL> Q;
    static const ULL M = 1000000007;
    ULL P[200001];
    ULL iP[200001];

    pair<LL, LL> inv(pair<LL, LL> a) {
        swap(a.first, a.second);
        if (a.first < 0) a.first *= -1;
        else a.second *= -1;
        return a;
    }

    void Solve() {
        scanf("%d", &N);
        ULL C = 0;
        rep(i, N) {
            scanf("%lld%lld", &A[i].first, &A[i].second);
            if (A[i] == make_pair(0ll, 0ll)) { C++; continue; }
            if (A[i].first == 0)
                A[i].second = 1;
            else if (A[i].second == 0)
                A[i].first = 1;
            else {
                LL g = GCD(abs(A[i].first), abs(A[i].second));
                A[i].first /= g;
                A[i].second /= g;
                if (A[i].first < 0) {
                    A[i].first *= -1;
                    A[i].second *= -1;
                }
            }
            Q[A[i]]++;
        }
        ULL ans = 1;
        P[0] = 1; for (UL i = 1; i <= 200000; i++) P[i] = P[i - 1] * 2 % M;
        iP[0] = 1; for (UL i = 1; i <= 200000; i++) iP[i] = iP[i - 1] * 500000004 % M;
        for (auto& q : Q) {
            auto p = q.first;
            UL c = q.second;
            auto i = inv(p);
            ULL tmp = P[c] - 1;
            if (p < i) if (Q.find(i) != Q.end()) {
                tmp = tmp * iP[Q[i]] % M;
            }
            ans = (ans + ans * tmp) % M;
        }
        ans = (ans + M - 1 + C) % M;
        printf("%lld\n", ans);
    }
};
int main() {
    unique_ptr<Problem> p(new Problem());
    p->Solve();
    return 0;
}


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

int max(int a,int b){
  if(a>b) return a;
  else return b;
}
int absolute(int a,int b){
  if(a-b<0) return -(a-b);
  else return a-b;
}
int main(){
  int n,m;
  cin >> n >> m;
  
  cout << (n-1) * (m-1) << endl;
}

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

using namespace std;

#define inf 1<<30
#define llong long long
#define usd unsigned
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define fn fill_n
typedef pair<int, int> P;
typedef struct data{
	int now;
	int cost;
}data;
int nw[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0};
int nh[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};




int main(){
	int n; cin >> n;
	int sum = 0;
	int m = n;
	while(m){
		sum += m%10;
		m/=10;
	}
	if(n%sum){
		cout << "No" << endl;
	}else{
		cout << "Yes" << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int q[26],p[26];
int main(){
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++){
        q[s[i]-'a']++;
    }
    for(int i=0;i<26;i++){
        if(q[i]==0){
            cout<<(char)(i+'a');
            return 0;
        }
    }
    cout<<"None"<<endl;
}


Measured memory_bytes: 512
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(void) {
    string S;
    cin >> S;
    int N = S.size();
    vector<string> v = {"dream", "dreamer", "erase", "eraser"};

    reverse(S.begin(), S.end());
    for(auto &e: v) {
        reverse(e.begin(), e.end());
    }

    int now = 0;
    while(now < N) {
        bool flag = false;
        for(auto e: v) {
            if(S.substr(now, e.size()) == e) {
                now += e.size();
                flag = true;
            }
        }
        if(flag) continue;
        cout << "NO" << endl;
        return 0;
    }

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


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

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  
  ll ans = 0;
  int l = 0, r = 0;
  ll xo = a[0], xp = a[0];
  while (r < n) {
	if (xo == xp) {
      ++r;
      xo ^= a[r];
      xp += a[r];
    }
    else {
      ans += r-l;
      xo ^= a[l];
      xp -= a[l];
      ++l;
    }
  }
  cout << ans + (ll)(r-l)*(ll)(r-l+1)/2LL << endl;
  return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
const long long mod = pow(10, 9) + 7;
const long long INF = 1LL << 60;
template <class T> bool chmin(T& a, T b){if (a > b) {a = b; return true;} return false;}
template <class T> bool chmax(T& a, T b){if (a < b) {a = b; return true;} return false;}
int divCeil(int A, int B) {return (A + (B - 1)) / B;}
long long divCeil(long long A, int B) {return (A + (B - 1)) / B;}
long long divCeil(int A, long long B) {return (A + (B - 1)) / B;}
long long divCeil(long long A, long long B) {return (A + (B - 1)) / B;}

int main()
{
  long long a, b, x;
  cin >> a >> b >> x;
  
  long long ans;
  if (a == 0) ans = b / x + 1;
  else ans = (b / x + 1) - ((a - 1) / x + 1);
  
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp make_pair
#define sz(x) ((int)(x).size())
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define all(x) x.begin(),x.end()
#define y1 omg
#define INF 1000000010
#define FOR(x, n) for(int x=0; x<n; x++)
#define ini(x, n) memset(x, n, sizeof(x));
#define ctoi(x) (x - '0')
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

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

int n, ans;
vi v(200010);
unordered_map<int, int> M;

int main() {
	fastio;
	cin >> n;
	FOR(i, n) {
		cin >> v[i + 1];
		M[v[i + 1]]++;
	}
	for (int i = 1; i <= n; i++) {
		int flag = 1;
		for (ll j = 1; j * j <= v[i]; j++) {
			if (v[i] % j == 0) {
				//v[i]/j, j
				if (j == v[i] && M[j] >= 2 || j != v[i] && M[j] >= 1 || v[i]/j == v[i] && M[v[i]/j] >= 2 || v[i]/j != v[i] && M[v[i]/j] >= 1) {
					flag = 0;
					break;
				}
			}
		}
		if (flag) ans++;
	}
	cout << ans;
}

Measured memory_bytes: 32484
package main

import "fmt"

func main() {
	var w, a, b int
	fmt.Scan(&w, &a, &b)
	if abs(a-b) < w {
		fmt.Println(0)
		return
	}
	fmt.Println(abs(a-b) - w)
}

func abs(n int) int {
	if n < 0 {
		return -n
	}
	return n
}


Measured memory_bytes: 512
#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;

const long long inf = (int) 1e18;
const int N = 100005;

long long dp[N];
vector <int> adj[N];

long long dfs(int u) {
  if (dp[u] != -1) return dp[u];
  long long ans = 0;
  for (int v : adj[u]) {
    ans = max(ans, 1 + dfs(v));
  }
  return dp[u] = ans;
}

int main() {
  memset(dp, -1, sizeof dp);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  cout << *max_element(dp, dp + n + 1) << endl;
  return 0;
}


Measured memory_bytes: 8576
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>

int main(void) {
    int x;

    for(int i = 1; i <= 5; i++) {
        scanf("%d", &x);
        if(x == 0) {
            printf("%d\n", i);
            return 0;
        }
    }

    return 0;
}


Measured memory_bytes: 1732
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <queue>
#include <cmath>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int mod = 1000000007;

// 桁dp
// dp[i][j] i:何桁目か j:i桁目まで入力値と一致しているか(=0)、入力値未満が確定しているか(=1)どうか
// 2つ目のindexは、状態を表す
int dp[100005][2];

int main(){
    string s;
    cin >> s;
    int n = s.size();
    
    // (i,j)=(0,0)の場合を1通りとする。
    // 0桁目から見て、入力値と一致している状態
    dp[0][0] = 1;
    
    for(int i = 0; i < n; ++i){     // i+1桁目の場合の数について調べる
        // a+b=0の場合、(a,b)=(0,0)の1通り
        if(s[i] == '0'){
            // a+bと入力値のi+1桁目が等しければ、i桁目の結果がそのままi+1桁目に反映される
            dp[i + 1][0] = dp[i][0];        // (*1通り)
            dp[i + 1][1] = dp[i][1];        // (*1通り)
        }
        else{       // s[i]=='1'
            // この時点で入力された値未満が確定するので、i桁目の結果の両方がj=1に反映される
            dp[i + 1][1] = (dp[i][0] + dp[i][1]) % mod;
        }
        
        // デバッグ用
        cerr << dp[i + 1][0] << " " << dp[i + 1][1] << "  ";
        
        // a+b=1の場合、(a,b)=(0,1),(1,0)の2通り
        if(s[i] == '0'){
            // i桁目まで一致(j=0)してたならば、ここで入力値より大きくなるので、候補から除外する
            // i桁目までで未満が確定(j=1)していたならば、(i,j=1)の結果を(i+1,j=1)に反映させる
            // +=演算子は参照が返るので、この書き方が成立
            (dp[i + 1][1] += dp[i][1] * 2 % mod) %= mod;
        }
        else{       // s[i]=='1'
            // a+bと入力値のi+1桁目が等しければ、i桁目の結果がそのままi+1桁目に反映される
            (dp[i + 1][0] += dp[i][0] * 2 % mod) %= mod;
            (dp[i + 1][1] += dp[i][1] * 2 % mod) %= mod;
        }
        
        // デバッグ用
        cerr << dp[i + 1][0] << " " << dp[i + 1][1] << endl;
    }
    
    cout << (dp[n][0] + dp[n][1]) % mod << endl;
}

Measured memory_bytes: 1280
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

int main(){
    int n;
    while(cin>>n&&n!=0){
        int cnt=0;
        for(int i=0;i*i<=n;i++){
            for(int j=i;j*j+i*i<=n;j++){
                for(int k=j;k*k+j*j+i*i<=n;k++){
                    for(int l=k;l*l+j*j+k*k+i*i<=n;l++){
                        int sum=i*i+j*j+k*k+l*l;
                        if(sum==n)
                            cnt++;
                    }
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

Measured memory_bytes: 864
#include<bits/stdc++.h>
# define ll long long int
#define mod 1000000007 
using namespace std;
int a[100005];
int dp[100005];
ll n,k;
ll fun(int id){
	if (id==n){
		return 0;
	}
	if (dp[id]!=-1){
		return dp[id];
	}
	ll y=INT_MAX;
	for (int i=id+1;i<=min(n,id+k);i++){
		y=min(y,abs(a[i]-a[id])+fun(i));
	}
	dp[id]=y;
	return y;
}
int main(){
    memset(dp,-1,sizeof(dp));
	cin>>n>>k;
	for (int i=1;i<=n;i++){
		cin>>a[i];
	}
	cout<<fun(1);
	
	
	
	
}

	
	


Measured memory_bytes: 5760
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		StringBuilder str = new StringBuilder();
		String[] in = br.readLine().split(" ");
		int[] arr = new int[n];
// 		while(t--!=0){
		    
// 		}
        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int curr = n-1;
        int ans = 0;
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Math.min(dp[i-2]+Math.abs(arr[i-2]-arr[i]),dp[i-1]+Math.abs(arr[i-1]-arr[i]));
        }
        System.out.println(dp[n-1]);
	}
}


Measured memory_bytes: 33492
S=input()

num={}
for i in range(len(S)):
    num[i]=0

for i in range(1,len(S)):
    even=1
    odd=1
    if S[i]=='L' and S[i-1]=='R':

        j=1
        while(1):
            if i+j>(len(S)-1):
                break
            if S[i+j]=='L':
                if j%2==1:
                    odd+=1
                else:
                    even+=1
            else:
                break
            j+=1
        
        j=1
        while(1):
            if i-1-j<0:
                break
            if S[i-1-j]=='R':
                if j%2==1:
                    even+=1
                else:
                    odd+=1
            else:
                break
            j+=1

        num[i]=even
        num[i-1]=odd

for i in range(len(S)):
    print(num[i],end=' ')
print('')

Measured memory_bytes: 15212
W,H,x,y = map(int,input().split())

if x == 0 or x == W:
  tmp1 = H-y
  tmp2 = y
  print((tmp1+tmp2)*W/2, 0)
elif y == 0 or y == H:
  tmp1 = W-x
  tmp2 = x
  print((tmp1+tmp2)*H/2, 0)
elif x == W/2 and y == H/2:
  print(W*H/2, 1)
else:
  tmp1 = W-x
  tmp2 = H-y
  if tmp1 == x:
    print(x*H, 0)
  elif tmp2 == y:
    print(y*W, 0)
  else:
    a = (tmp2-y)/(tmp1-x)
    b = -a*x+y
    if 0 <= b <= H:
      print((b+(H-b))*w/2, 0)
    else:
      tmp = -b/a
      print((tmp+(W-tmp))*H/2, 0)
    

Measured memory_bytes: 9204
<?php
$n = (int)trim(fgets(STDIN));

$tree = [];
$result = [];
for ($i=1; $i <= $n; $i++) { 
	$tree[$i] = [];
	$result[$i] = null;
}
for ($i=1; $i < $n; $i++) { 
	$input = explode(' ', trim(fgets(STDIN)));
	$u = (int)$input[0];
	$v = (int)$input[1];
	$w = (int)$input[2];
	$tree[$u][] = [$v, $w];
	$tree[$v][] = [$u, $w];
}
$result[1] = true;
dfs(1);

$result_num = [];
for ($i=1; $i <= $n ; $i++) { 
	if ($result[$i]) {
		$result_num[] = 1;
	}else{
		$result_num[] = 0;
	}
}
echo implode(PHP_EOL, $result_num);

function dfs($num){
	global $tree;
	global $result;
	foreach ($tree[$num] as $next) {
		if ($result[$next[0]] === null) {
			if ($next[1] % 2 === 0) {
				$result[$next[0]] = $result[$num];
			}else{
				$result[$next[0]] = !$result[$num];
			}
			dfs($next[0]);
		}
	}
}

Measured memory_bytes: 138612
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,M;
  cin>>N>>M;
vector<int>vacation(M);
  for(int i=0;i<M;i++){
  	cin>>vacation.at(i);
    }
  int count=0;
    for(int i=0;i<M;i++){
  	 count+=vacation.at(i);
    }
 if(count<=N){
 cout<<N-count;
 }
  else{
  cout<<"-1";
  }
}

Measured memory_bytes: 3620
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int MOD=1e9+7;
int32_t main() {
    double d,t,s;
    cin>>d>>t>>s;
    if(d/s<=t){
        cout<<"Yes";
    } else cout<<"No";
    return 0;
}

Measured memory_bytes: 3708
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;

int card[50];

int main(void){
	while(1){
		string str;
		getline(cin,str);
		stringstream ss(str);
		vector<string> v;
		string buffer;
		int sum=0,cnt=0;
		while(getline(ss,buffer,' '))v.push_back(buffer);
		if(v[0]=="0")break;
		for(int i=0;i<v.size();i++){
			stringstream iss(v[i]);
			iss >> card[i];
			if(card[i]==1)cnt++;
			if(card[i]>=2 && card[i]<=9)sum+=card[i];
			if(card[i]>=10)sum+=10;
		}
		if(sum>21)printf("0\n");
		else{
			int maxp=0;
			for(int i=0;i<=cnt;i++){
				int p=sum+1*i+11*(cnt-i);
				if(p>21)continue;
				maxp=max(maxp,p);
			}
			printf("%d\n",maxp);
		}
	}
	return 0;
}

Measured memory_bytes: 1252