akhauriyash/Code-Regression huggingface dataset:
H, W, D = map(int, input().split())
positions = [[] for _ in range(H*W)]

for y in range(H):
    position = list(map(int, input().split()))
    for x in range(W):
        positions[position[x]-1] = [x, y]

def get_distance(a, b):
    return abs(positions[a][0] - positions[b][0]) + abs(positions[a][1] - positions[b][1])

answers = [[] for _ in range(D)]
d_length = (H * W) // D + 1
for d in range(D):
    current = d
    d_answers = [0 for _ in range(d_length)]
    idx = 1
    while current + D < H * W:
        d_answers[idx] = get_distance(current, current+D) + d_answers[idx-1]
        current += D
        idx += 1
    answers[d] = d_answers

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    L -= 1
    R -= 1
    extra = L % D
    start = L // D
    end = R // D
    print(answers[extra][end]-answers[extra][start])


Measured memory_bytes: 23480
function Main(input) {
  let sum = 0;
  for(let i = 0; i < parseInt(input) + 1; i++) {
    if(i % 3 !== 0 && i % 5 !== 0) {
      sum += i;
    }
  }
  console.log(sum);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 35244
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int,int>;
const long long inf = 1000000001; // const 書き換え禁止
const double PI = 3.14159265358979323846; //M_PI はライブラリ

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

}

Measured memory_bytes: 3604
#include<bits/stdc++.h>
#define rint register int 
#define ll long long 
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
ll f[1010100],N,T,E,X[1010000];
ll S[1010000];
inline void Modify(int now,int l,int r,int pos,ll d) {
	if(l==r) {S[now]=d;return ;}
	int mid=(l+r)>>1;
	if(pos<=mid) Modify(now<<1,l,mid,pos,d);
	else Modify(now<<1|1,mid+1,r,pos,d);
	S[now]=min(S[now<<1],S[now<<1|1]);
}
inline ll Qry(int now,int l,int r,int x,int y){
	if(x<=l&&r<=y) return S[now];
	ll res=1e18;
	int mid=(l+r)>>1;
	if(x<=mid) res=min(res,Qry(now<<1,l,mid,x,y));
	if(y>mid) res=min(res,Qry(now<<1|1,mid+1,r,x,y));
	return res;
}
int main(){
	cin>>N>>E>>T;
	f[1]=0;
	for(rint i=1;i<=N;++i) {
		cin>>X[i];
	}
	for(rint i=1;i<=N<<2;++i) S[i]=1e18;
	int now=0;
	ll Min=1e18;
	X[N+1]=E;
	Modify(1,1,N,1,-X[1]);
	for(rint i=2;i<=N+1;++i){
		while((now+1<i)&&2*(X[i-1]-X[now+1])>=T) now++,Min=min(Min,f[now]-3*X[now]);
		f[i]=Min+3*X[i-1]+X[i]-X[i-1];
		
		
		if(now+1<i)
		f[i]=min(f[i],Qry(1,1,N,now+1,i-1)+X[i-1]+T+X[i]-X[i-1]);
		Modify(1,1,N,i,f[i]-X[i]);//debug(i);debug(f[i]);
	}
	cout<<f[N+1]+X[1]<<endl;
	return 0;
}

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

using namespace std;

#define ll long long 
#define endl '\n'
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define SIZE 100005

int main()
{
	
	int a,b;
	
	cin >> a >>b;
	if(a <= 2*b) cout << 0 << endl;
	else cout << a-2*b << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip> 
int main(void) {
    int yes=0;
std::string inp;
std::cin>>inp;
    if(inp[0]=='0'){
        for(int i=0;i<inp.length();i++){
            if(i%2==0&&inp[i]!='0'){yes++;}
            if(i%2!=0&&inp[i]!='1'){yes++;}
        }
        std::cout<<yes<<std::endl;
    }
    if(inp[0]=='1'){
        for(int i=0;i<inp.length();i++){
            if(i%2==0&&inp[i]!='1'){yes++;}
            if(i%2!=0&&inp[i]!='0'){yes++;}
        }
        std::cout<<yes<<std::endl;
    }

}

Measured memory_bytes: 512
n,m = gets.chomp.split(" ").map(&:to_i)

ks = []
m.times do |i|
  ks << gets.chomp.split(" ").map(&:to_i)
end

p_arr = gets.chomp.split(" ").map(&:to_i)

c = 0
# 1 <= n <= 10
(2 ** n).times do |i|
  bi = (i.to_s(2)).reverse + "0" * 10
  arr = []
  n.times do |j|
    arr[j] = bi[j] == "1" ? true : false
  end

  flag = true
  ks.each_with_index do |kss, l|
    k = kss[0]
    s_arr = kss[1..-1]
    count = 0
    s_arr.each do |s|
      count += 1 if arr[s-1]
    end
    p = p_arr[l]
    if count % 2 != p then
      flag = false
      break
    end
  end
  c += 1 if flag
end

puts c



Measured memory_bytes: 1788
#include <stdio.h> 
  int main(void){    
 int A,B;  
 int n;  
 int i;   
int PointA,PointB;      
while(1){       
PointA = 0;   
  PointB = 0;     

  scanf("%d" ,&n);     
  if( n == 0 ) break;   
    for( i = 0 ; i < n ; i++ ){     
    scanf("%d %d" ,&A ,&B);       
    if( A < B ) PointB += A + B;      
         if( A > B ) PointA += B + A;     
    if( A == B ){      
  PointA += A;     
PointB += B;       
  }     
  }    
   printf("%d %d\n" ,PointA ,PointB);  
   }   
  return 0;  
 } 

Measured memory_bytes: 600
s = input()

s1 = int(s[:2])
s2 = int(s[2:])

if 1 <= s1 <= 12 and 1 <= s2 <= 12:
    print('AMBIGUOUS')
elif 1 <= s1 <= 12:
    print('MMYY')
elif 1 <= s2 <= 12:
    print('YYMM')
else:
    print('NA')


Measured memory_bytes: 2940
#include<iostream>
#include<math.h>
using namespace std;
#define PI 3.14
 
int main(){
    double en;
    double i,y,k;
    int n;
    while(cin >> i >> y >> k , i * y * k){
        double min = sqrt (i * i + y * y);
        if(min > sqrt (k * k + y * y) ) min = sqrt (k * k + y * y);
        if(min > sqrt (i * i + k * k) ) min = sqrt (i * i + k * k);
        cin>>n;
        for(int i = 0 ; i < n ; i++){
            double m;
            cin>>m;
            //cout<<1.0*2*m<<" "<<min<<endl;
             if(1.0*min<1.0*m*2)cout << "OK" << endl;
             else cout << "NA" << endl; 
        }
         
    }
 
}

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

int N, Z, W;
vector<int> card;

int cc[2020][2020][2];
int dp(int a, int b, int t) {
    int &ret = cc[a][b][t];
    if(ret != -1) return ret;
    if(max(a, b) == (int)card.size() - 1) return ret = abs(card[a] - card[b]);

    if(t) {
        ret = 0;
        ret = max(ret, dp(a, max(a, b) + 1, 0));
        ret = max(ret, dp(max(a, b) + 1, b, 1));
        return ret;
    }
    else {
        ret = 0;
        ret = min(ret, dp(max(a, b) + 1, b, 1));
        ret = min(ret, dp(a, max(a, b) + 1, 0));
        return ret;
    }
}

int main() {
    scanf("%d %d %d", &N, &Z, &W);

    card.resize(N + 2);
    card[0] = Z;
    card[1] = W;
    for(int i = 2; i < N + 2; i++) {
        scanf("%d", &card[i]);
    }

    memset(cc, -1, sizeof(cc));
    cout << dp(2, 1, 1);
}


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

int main()
{
  i64 n;
  cin >> n;
  i64 cnt[10][10] = {};
  for (i64 i = 1; i <= n; i++)
  {
    string t = to_string(i);
    cnt[t[0] - '0'][t[t.size() - 1] - '0']++;
  }
  i64 ans = 0;
  for (i64 i = 1; i <= n; i++)
  {
    string t = to_string(i);
    ans += cnt[t[t.size() - 1] - '0'][t[0] - '0'];
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;

public class Main{
    public static void main(String[] args) throws Exception{
        new Main().run();
    }
    void run() throws Exception{
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        boolean multiple = false;
        for(int tc = 1, tt = (multiple)?ni(in):1; tc<=tt; tc++)solve(in,out,tc);
        out.flush();
        out.close();
    }
    void solve(FastReader in, PrintWriter out, int TC) throws Exception{
        int n = ni(in), k = ni(in);
        pn(out,(n%k==0)?0:1);
    }
    void p(PrintWriter out, Object o){out.print(o);}
    void pn(PrintWriter out,Object o){out.println(o);}
    int ni(FastReader in){return Integer.parseInt(in.next());}
    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next(){
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        String nextLine(){
            String str = "";
            try{
                str = br.readLine();
            }catch (IOException e){
                e.printStackTrace();
            }   
            return str;
        }
    }
} 


Measured memory_bytes: 23124
import numpy as np
import math

n = int(input())
p = np.array(input().split(),dtype=int)
q = np.array(input().split(),dtype=int)

f = [1]
for i in range(n):
    f.append(f[i]*(i+1))


def banme(a):
    num = [i+1 for i in range(n)]
    now = 1
    for i in range(n):
        now += num.index(a[i])*f[n-i-1]
        num.remove(a[i])
    return now

print(int(math.fabs(banme(p)-banme(q))))


Measured memory_bytes: 12448
#include <cstdio>
#include <string>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <iomanip>
#include <tuple>
#include <queue>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i=a;i<n;++i)
#define repeq(i, a, n) for(int i=a;i<=n;++i)
#define repi(m, itr) for (auto itr = m.begin();itr != m.end();++itr)
#define per(i, a, n) for (int i=n-1; i>=a;--i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()

// 
int H, W;
int a[550][550];
int ans = 0;
int xa[300000], ya[300000], xb[300000], yb[300000];


int main(){
    cin >> H >> W;
    rep(i, 0, 550){
        rep(j, 0, 550){
            a[i][j] = 100;
        }
    }
    rep(i, 1, H+1){
        rep(j, 1, W+1){
            cin >> a[i][j];
        }
    }
    rep(i, 1, H+1){
        rep(j, 1, W+1){
            if(a[i][j]%2 != 0){
                if(a[i][j+1] < 50){
                    a[i][j]--;
                    a[i][j+1]++;
                    xa[ans] = i;
                    ya[ans] = j;
                    xb[ans] = i;
                    yb[ans] = j+1;
                    ans++;
                }else if(a[i+1][j] < 50){
                    a[i][j]--;
                    a[i+1][j]++;
                    xa[ans] = i;
                    ya[ans] = j;
                    xb[ans] = i+1;
                    yb[ans] = j;
                    ans++;
                // }else if(a[i][j-1]<50){
                //     a[i][j]--;
                //     a[i][j-1]++;
                //     xa[ans] = i;
                //     ya[ans] = j;
                //     xb[ans] = i;
                //     yb[ans] = j-1;
                //     ans++;
                // }else if(a[i-1][j]<50){
                //     a[i][j]--;
                //     a[i-1][j]++;
                //     xa[ans] = i;
                //     ya[ans] = j;
                //     xb[ans] = i-1;
                //     yb[ans] = j;
                //     ans++;
                }
            }
        }
    }

    cout << ans << endl;
    if(ans>0){
    rep(i, 0, ans){
        cout << xa[i] << " " << ya[i] << " " << xb[i] << " " << yb[i] << endl;
    }
    }
    
    return 0;
}




Measured memory_bytes: 6272
n, weight = gets.chomp.split(" ").map(&:to_i)
wv = []
n.times do
    wv << gets.chomp.split(" ").map(&:to_i)
end

dp = Array.new(weight+1, -1) # dp[重さ] = 価値
dp[0] = 0

wv.each do |w, v|
    (weight - w).downto(0) do |i|
        next if dp[i] < 0
        nv = dp[i] + v #価値
        nw = i + w #重さ
        dp[nw] = nv if dp[nw] < nv
    end
end

# ng
# (1..weight).each do |w|
#     max = 0
#     (0..(n-1)).each do |i|
#         next if w < wv[i][0]
#         value = dp[w-wv[i][0]] + wv[i][1]
#         max = value if max < value
#     end
#     dp[w] = max
#     end
# end

puts dp.max

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

int main()
{
	long long Q, H, S, D, N;
	cin >> Q >> H >> S >> D >> N;

	H = min( H, 2 * Q );
	S = min( S, 2 * H );
	long long ans = N * S;
	if( D < S * 2 ) ans = N / 2 * D + S * ( N % 2 );

	cout << ans << endl;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin >> n >> m;
    vector <int> a;
    int total = 0;
    for(int i = 0; i<n; i++){
        int tmp = 0;
        cin >> tmp;
        total += tmp;
        a.push_back(tmp);
    }
    double border = (double)total/(double)(4*m);
    sort(a.begin(),a.end());
    reverse(a.begin(),a.end());
    int ans = 0;
    for(int i = 0; i<a.size(); i++){
        if(a[i]>=border){
            ans++;
        }
    }
    if(ans>=m){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    
    #if 0
    for(int i = 0; i<a.size(); i++){
        cout << a[i] << " ";
    }
    cout << endl;
    #endif

    return 0;
}

Measured memory_bytes: 3600
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
using namespace std;

const double pi = acos(-1);

#define nyan cout<<"(=^•ω•^=)"<<endl;
#define int long long

string a,b,c;
int x=-1,y=-1,z=-1;
int ans;

int solution(char i){
    if(i=='a'){
        if(x==a.length()-1){
            ans=1;
            return 0;
        }
        x++;
        solution(a[x]);
    }
    if(i=='b'){
        if(y==b.length()-1){
            ans=2;
            return 0;
        }
        y++;
        solution(b[y]);
    }
    if(i=='c'){
        if(z==c.length()-1){
            ans=3;
            return 0;
        }
        z++;
        solution(c[z]);
    }
    return 0;
}

signed main(){
    cin>>a>>b>>c;
    solution('a');
    if(ans==1)cout<<"A"<<endl;
    if(ans==2)cout<<"B"<<endl;
    if(ans==3)cout<<"C"<<endl;
    return 0;
}

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

int main()
{
     int n,b[101],ans=0,a[101];
     cin>>n;
     for(int i=1;i<n;i++){cin>>b[i];a[i]=b[i];}
     b[0]=a[0]=b[1];
     for(int i=n-1;i>1;i--)
     {
          if(b[i]<b[i-1])
          {a[i-1]=b[i];}
     }
     for(int i=0;i<n;i++)ans+=a[i];
     cout<<ans<<"\n";
}



Measured memory_bytes: 256
//#include<iostream>
//using namespace std;
//int a[200005];
//int main(void)
//{
//    int n,k;
//    cin >> n >> k;
//    for(int i=1;i<=n;i++)
//        cin >> a[i];
//    for(int i=1;i<=n-k+1;i++)
//    {
//        for(int j=i;j<i+k;j++)
//
//    }
//}
#include<iostream>
#include<algorithm>
using namespace std;
int a[200005];
int main(void)
{
    int n,cnt=0;
    bool flag=false;
    cin >> n;
    for(int i=1;i<=n;i++)
        cin >> a[i];
    sort(a+1,a+n+1);
    for(int i=2;i<=n;i++)
        if(a[i-1]==a[i])
        {
            flag=true;
            break;
        }
    if(flag==false)
        cout << "YES";
    else cout <<"NO";
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin() , x.end()
int tt = 1;

void ok() {
  int n,x;
  cin >> n >> x;
  int ans=1;
  vector<int>v(n);
  for(int i=0;i<n;i++){
      cin >> v[i];
  }
  int tm=0;
  for(int i=0;i<n;i++){
      ans+=tm+v[i]<=x;
      tm+=v[i];
  }
  cout<<ans;
}

int32_t main() {


  int t = 1;
  //cin >> t;
  int c = 0;
  while (t--) {
    
    ok();
  }
  return 0;
}


Measured memory_bytes: 256
//Code by 27.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<fstream>
#include<stdlib.h>
#include<set>
#include<climits>
#include<cmath>  
#include<memory.h> 
#include<sstream>
#include<time.h>
#include<iomanip>
using namespace std;
const unsigned long long BIGEST=1000000000000000000+1000000000000000000;
const long long BIGER=1000000000000000000;
const int BIG=1000000000;
const int MOD=1000000007;
const long long LMOD=1000000007;
long long h[1000005];
long long n;
long long f[1000005];
int main()
{
	scanf("%lld",&n);
	for(int i=2;i<=n+1;i++)
	{
		scanf("%lld",&h[i]);
		if(i==2)f[i]=0;
		else if(i==3)f[i]=f[i-1]+abs(h[i]-h[i-1]);
		else f[i]=min(f[i-1]+abs(h[i]-h[i-1]),f[i-2]+abs(h[i]-h[i-2]));
		//cout<<f[i]<<endl;
	}
	cout<<f[n+1];
	return 0;
}

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

int main(){
  int n[110];
  int m[110];
  int N;
  int max =0;
  int count_1=0;
  int count_2=0;
  cin >> N;
  
  for(int i=0; i<=N;i++){
    n[i] = i;
    m[i] = i;
  }
  
  for(int i =1; i<=N; i++){ 
    count_1=0;
    while(true){
      if(n[i]%2 == 0){
        count_1++;
      }else{
        break;
      }
      n[i] = n[i]/2;
    }
    
    if(count_2 <= count_1){
      max = m[i];
      count_2 = count_1;
    }
  }
  cout << max << endl;
    return 0;
}

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

int main() {
	int n, d=100000;
	cin >> n;
	for(int i=0; i<n; i++){
		d *= 1.05;
		int a = d % 1000;
		if(a != 0){
			d -= a;
			d += 1000;
		}
	}
	cout << d << endl;
	return 0;
}


Measured memory_bytes: 3080
#import sys
#input = sys.stdin.readline
from itertools import accumulate
def main():
    N, K = map( int, input().split())
    A = list( map( int, input().split()))
    for i in range(K):
        B = [0]*(N+1)
        for j in range(N):
            d = A[j]
            x = j-d
            y = j+d
            if x < 0:
                B[0] += 1
            else:
                B[x] += 1
            if N-1 < y:
                B[N] -= 1
            else:
                B[y+1] -= 1
        A = list( accumulate(B))
        check = True
        for j in range(N):
            if A[j] < N:
                check = False
        if check:
            break
    print(" ".join( map(str, A[:-1])))
            
if __name__ == '__main__':
    main()


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

int main() {
  string s;
  cin >> s;
  int count =0;
  int count1 =0;
  for (int i=0;i<3;i++){
    if (s.at(i)=='R'){
      count++;
      if (count1<count)
        count1 =count;
    }
    else{
      count=0;
    }
    
    
  }
  cout << count1 << endl; 
}


Measured memory_bytes: 3624
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;


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

    public void solve(String[] args) {
        MyScanner scanner = new MyScanner();

        // ビジネスロジックここから
        int n = Integer.parseInt(scanner.next());
        int ans = 0;
		if(n % 1000 != 0) {
			ans = 1000 - n % 1000;
		} else {
			ans = 0;
		}
        System.out.println(ans);
        
        // ビジネスロジックここまで

    }

    private class MyScanner {
        String[] s;
        int i;
        BufferedReader br;
        String reg = " ";
        MyScanner () {
            s = new String[0];
            i = 0;
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        public String next() {
            try {
                if (i < s.length) return s[i++];
                String line = br.readLine();
                while (line.equals("")) {line = br.readLine();}
                s = line.split(reg, 0);
                i = 0;return s[i++];
            } catch (Exception e) {e.printStackTrace();}
            return null;
        }
        public int nextInt() {
            try {return Integer.parseInt(next());} catch (Exception e) {e.printStackTrace();}
            return -1;
        }
        public double nextDouble() {
            try {return Double.parseDouble(next());} catch (Exception e) {e.printStackTrace();}
            return -1;
        }
        public long nextLong()  {
            try {return Long.parseLong(next());} catch (Exception e) {e.printStackTrace();}
            return -1;
        }
    }
}


Measured memory_bytes: 32668
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! p {
    ($($x:expr),*) => {
        $(println!("{:?}", $x);)*
    };
}
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        #[allow(unused_mut)]
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
#[allow(unused_imports)]
use std::cmp::*;
trait Each: Iterator{
    fn each<F>(self, mut f: F) where
        Self: Sized, F: FnMut(Self::Item),
    {
        self.fold((), move |(), item| f(item));
    }
}
impl<T:Iterator>Each for T{}

fn main(){
    input!{
        s:chars,
    }
    if s.iter().enumerate().all(|(i,&x)|match x{
        'R'|'U'|'D' if i%2==0 => true,
        'L'|'U'|'D' if i%2==1 => true,
        _ => false
    }){
        println!("Yes");
    }else{
        println!("No");
    }
}

Measured memory_bytes: 4352
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
#include <map>
#include <set>
#include <ctype.h>
#include <numeric>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
const ll MOD = 1e9 + 7;
const int INF = 1e9;

int dx4[4] = { 0, -1, 1, 0 };
int dy4[4] = { -1, 0, 0, 1 };
int dx8[8] = { 0, -1, 1, 0, -1, 1, -1, 1 };
int dy8[8] = { -1, 0, 0, 1, -1, -1, 1, 1 };

int main() {
  int n, m; cin >> n >> m;
  vector<int> arr(n);
  for (int i = 0; i < n; ++i) arr[i] = 0;
  for (int i = 0; i < m; ++i) {
    int a, b; cin >> a >> b;
    arr[a - 1]++;
    arr[b - 1]++;
  }

  for (int i = 0; i < n; ++i) {
    cout << arr[i] << endl;
  }
  return 0;
}


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

int main() {
	int card[5];
	int yaku[15] = {0};
	
	while ( ~scanf ("%d,%d,%d,%d,%d",&card[0],&card[1],&card[2],&card[3],&card[4]) ){
		for ( int i = 1; i < 15; i++ ) {
			yaku[i] = 0;
		}
		
		for ( int i = 0; i < 5; i++ ) {
			if ( card[i] == 1 ) yaku[14]++;
			yaku[card[i]]++;
		}
		
		/*for ( int i = 1; i < 15; i++ ) {
			if ( yaku[i] > 0 ) {
				cout << i << " " << yaku[i] << endl;
			}
		}*/
		
		for ( int i = 1; i < 15; i++ ) {
			if ( yaku[i] == 4 ){
				cout << "four card" << endl;
				goto END;
			} else if ( yaku[i] == 3 ) {
				for ( int j = 1; j < 14; j++ ) {
					if ( yaku[j] == 2 ) {
						cout << "full house" << endl;
						goto END;
					}
				}
			} else if ( yaku[i] == 1 ) {
				for ( int j = 1; j < 5; j++ ) {
					if ( yaku[i + j] != 1 )
						break;
					else if ( j == 4 ) {
						cout << "straight" << endl;
						goto END;
					}
				}
			}
		}
		for ( int i = 1; i < 15; i++ ) {
			if ( yaku[i] == 3 ) {
				cout << "three card" << endl;
				goto END;
			} else if ( yaku[i] == 2 ) {
				for ( int j = i + 1; j < 14; j++ ) {
					if ( yaku[j] == 2 ) {
						cout << "two pair" << endl;
						goto END;
					}else if ( j == 13 ){
						cout << "one pair" << endl;
						goto END;
					}
				}
			}
		}
		cout << "null" << endl;
		END:;
	}
}

Measured memory_bytes: 1204
#include <stdio.h>
#include <stdlib.h>

int line[100] = {0};

int main()
{
	int n_polygon = 0;
	int loop = 0;
	int sum = 0;
	int max = 0;

	scanf("%d", &n_polygon);
	for(loop = 0; loop < n_polygon; loop++) {
		scanf("%d", &line[loop]);
		if (line[loop] > max) {
			max = line[loop];
		}
		sum += line[loop];
	}

	//printf("sum=%d, max=%d\n",sum-max, max);
    if ((sum - max) > max) {
    	printf("Yes\n");
    } else {
    	printf("No\n");
    }

	return 0;
}


Measured memory_bytes: 128
import sys
readline = sys.stdin.readline

def solve():
    codes = ['AC', 'WA', 'TLE', 'RE']
    N = int(readline())
    count = {code: 0 for code in codes}
    for i in range(N):
        a = readline().rstrip()
        count[a] += 1
    
    for code in codes:
        print(f'{code} x {count[code]}')


solve()

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

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

using namespace std;
using lint=long long;

int main(){
	int n; scanf("%d",&n);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	sort(a.begin(),a.end());
	lint sum=accumulate(a.begin(),a.end(),0LL);

	int i;
	for(i=n-2;i>=0;i--){
		sum-=a[i+1];
		if(2*sum<a[i+1]) break;
	}
	printf("%d\n",n-i-1);

	return 0;
}


Measured memory_bytes: 640
import java.util.Scanner

import scala.collection.Searching._
import scala.annotation.tailrec
import scala.collection.immutable.Queue

class IUnionFind(val size: Int) {

  private case class Node(var parent: Option[Int], var treeSize: Int)

  private val nodes = Array.fill[Node](size)(new Node(None, 1))

  def union(t1: Int, t2: Int): IUnionFind = {
    if (t1 == t2) return this

    val root1 = root(t1)
    val root2 = root(t2)
    if (root1 == root2) return this

    val node1 = nodes(root1)
    val node2 = nodes(root2)

    if (node1.treeSize < node2.treeSize) {
      node1.parent = Some(t2)
      node2.treeSize += node1.treeSize
    } else {
      node2.parent = Some(t1)
      node1.treeSize += node2.treeSize
    }
    this
  }

  def connected(t1: Int, t2: Int): Boolean = t1 == t2 || root(t1) == root(t2)

  @tailrec
  private def root(t: Int): Int = nodes(t).parent match {
    case None => t
    case Some(p) => root(p)
  }
}


object Main {
  def solve(sc: => Scanner): Unit = {
    val N = sc.nextInt
    val parent = Array.fill(N)(-1)
    val children = Array.fill(N)(Array.fill(0)(0))
    for (_ <- Range(0, N)) {
      children(sc.nextInt) = Array.fill(sc.nextInt)(sc.nextInt)
    }
    for (i <- Range(0, N)) {
      for (x <- children(i)) {
        parent(x) = i
      }
    }
    for (i <- Range(0, N)) {
      var now = i
      var depth = 0
      while (parent(now) != -1) {
        now = parent(now)
        depth += 1
      }
      var typeName = "internal node"
      if (children(i).size == 0) (typeName = "leaf")
      if (depth == 0) (typeName = "root")
      println("node " + i + ": parent = " + parent(i) + ", depth = " + depth + ", " + typeName + ", [" + children(i).mkString(", ") + "]")
    }
  }

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




Measured memory_bytes: 385968
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <string>
#define INF 100000000000
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef pair<ll, double> PD;

int main() {
	string s;
	cin >> s;
	if (s == "SUN") cout << 7 << endl;
	else if (s == "MON") cout << 6 << endl;
	else if (s == "TUE") cout << 5 << endl;
	else if (s == "WED") cout << 4 << endl;
	else if (s == "THU") cout << 3 << endl;
	else if (s == "FRI") cout << 2 << endl;
	else cout << 1 << endl;
}

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

int main(){
    string arr[7] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
    string s;
    cin>>s;
    rep(i,7){
        if(arr[i]==s) cout<<7-i<<endl;
    }
    
    return 0;
}


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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); cerr << endl << "Time elapsed: " << (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms"; return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;

#define PB push_back
#define MP make_pair

#define sd(n) scanf("%lld", &n)
#define pdn(n) printf("%lld\n", n);
#define pds(n) printf("%lld ", n);
#define endl '\n'

#define forn(a, b, i) for (int i = a; i < b; i += 1)
#define all(v) v.begin(), v.end() 

#define print(stuff) cout << stuff << endl
#define len(stuff) (int) stuff.size()

#define int long long
// #define float long double 
#define ll long long
#define ld long double

using vi = vector<int>;
using vb = vector<bool>;
using pii = pair<int, int>;
using mii = map<int, int>;

const int inf = (int) 1e16;
const int upper = (int) 2e5 + 50;
const int M = (ll) 1e9 + 7;
const int mod = (ll) 998244353;
const double eps =  1e-8;
int dist[upper] = {0};
int n;
vector<pii> adj[upper];
void dfs (int v, int p) {
	for (auto i: adj[v]) {
		if (i.first != p) {
			dist[i.first] = dist[v] + i.second;
			dfs(i.first, v);
		}
	}
}
signed main () {
	start_routine();
	speed();
	#ifdef LOCAL
	freopen("input.txt", "r", stdin);	 
	// freopen("output.txt", "w", stdout);
	// freopen("errlog.txt", "w", stderr);
	#endif	
	cin >> n;
	for (int i = 1; i < n; i += 1) {
		int a, b, c; cin >> a >> b >> c;
		adj[a].push_back({b, c});
		adj[b].push_back({a, c});
	}
	dfs(1, 1);
	for (int i = 1; i <= n; i += 1) {
		if (dist[i] & 1) cout << 1;
		else cout << 0;
		cout << endl;
	}
 	end_routine();		
}

Measured memory_bytes: 13056
#include <bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin >>s;
  bool ok=false;
  for(int i=0;i<s.size();i++){
    if(s[i]=='C'){
      for(int j=i;j<s.size();j++){
        if(s[j]=='F')ok=true;
      }
    }
  }
  if(ok)cout <<"Yes"<<endl;
  else cout <<"No"<<endl;
}

Measured memory_bytes: 256
/*
    Code written by Talant I.D.
*/
   
#include <bits/stdc++.h>
   
using namespace std;
   
typedef long long ll;
typedef pair <int, int> pii;
typedef tuple <int, int, int> tiii;
   
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mod (int)1e9+7
#define eps (double)1e-9
#define all(v) v.begin(),v.end()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);
#define curMod 998244353

int f(int n){
	if(n == 1){
		return 1;
	}
	return n*f(n-1);
}

int main(){
		//do_not_disturb
		int n, i, x, y;
		cin >> n;
		double ans = 0;
		vector <pii> v;
		
		for(i = 0; i < n; i++){
			cin >> x >> y;
			v.pb(mp(x, y));
		}
		sort(all(v));
		
		do{
			for(i = 0; i < n-1; i++){
				int x1 = v[i].first, x2 = v[i+1].first, y1 = v[i].second, y2 = v[i+1].second;
				double dis = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
				ans += dis;
			}
		}
		while(next_permutation(all(v)));
		
		cout << precision(10) << (double)ans/f(n);
		
    return 0;
} // What is to be written here?


Measured memory_bytes: 256
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N = ni()
K = ni()


def solve():
    x = 1
    for i in range(N):
        x = min(2 * x, x + K)
    return x


print(solve())


Measured memory_bytes: 3060
/**
   * @Author:Monk78
   * @Topic:longest_path.cpp
**/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll>Vl;
typedef pair<ll,ll>PII;
typedef vector<PII>Vll;
typedef vector<pair<ll,pair<ll,ll> > >Vlll;
typedef priority_queue<ll>PQL;
typedef map<ll,ll>MP;
#define S second;
#define F first;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end));i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define mp make_pair
#define ms(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define mod 1000000009
#define MAX 100005
#define f(i,a,b) for(int i=(a);i<(b);i++)
#define rf(i,a,b) for(int i=(a);i>=b;i--)
#define sl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define vpshVar() int main()
bool visited[MAX+5];
ll dp[MAX+5];
Vl graph[MAX+5];
void dfs(ll node){
    visited[node]=true;
    for(auto it:graph[node]){
            //pl(node);cout<<"->";pl(it);cout<<endl;
        if(!visited[it])dfs(it);
        dp[node]=max(dp[node],1+dp[it]);
    }
}
vpshVar(){
    ll N,M;
    sl(N);sl(M);
    for(ll i=1;i<=M;i++){
            ll num1,num2;
            sl(num1);sl(num2);
            graph[num1].pb(num2);
    }
    /*
    for(ll i=1;i<=N;i++){
            cout<<i<<"->";
        for(auto it:graph[i])cout<<it;
        cout<<endl;
    }
    */
    ms(visited,false);
    ms(dp,-1);
    for(ll i=1;i<=N;i++){
           //cout<<"hello";
        if(!visited[i]){

            dfs(i);
        }
    }
    ll ans=0;
    for(ll i=1;i<=N;i++){
        ans=max(ans,dp[i]);
    }
    pl(ans+1);
return 0;
}


Measured memory_bytes: 8576
#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define mod 1000000007
#define inf 1e18+42
#define endl "\n"
#define pi 3.1415926535897932384626433832795028841971693993751058
#define maxn 100005

#define out1(a) cout<<#a<<" "<<a<<endl
#define out2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
#define out3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl

#define rep(i,a,b) for(int i=a;i<b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
#define fori(it,A) for(auto it=A.begin();it!=A.end();it++)

#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define pq priority_queue
#define all(x) (x).begin(),(x).end()
#define zero(x) memset(x,0,sizeof(x));
#define ceil(a,b) (a+b-1)/b
 
using namespace std;

int binpow(int a, int b) {
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

//START OF CODE ->->->->->->->

void solve()
{
    int a,b,c,d,e;
    cin>>a>>b>>c>>d>>e;
    if ( a == 0 ){
        cout<<"1"<<endl;
    }
    else if( b == 0 ){
        cout<<"2"<<endl;
    }
    else if ( c == 0 ){
        cout<<"3"<<endl;
    }
    else if ( d == 0 ){
        cout<<"4"<<endl;
    }
    else{
        cout<<"5"<<endl;
    }
}

//END OF CODE ->->->->->->->->

signed main()
{
    fast;
    int t = 1;
    // cin>>t;

    while(t--)
    {
        solve();
    }
    return 0;
}



Measured memory_bytes: 3692
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace C_test_For_Atcorder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {

            var N = cin.ReadInt;
            var A = cin.ReadInt;
            var B = cin.ReadInt;
            var sum = 0;
            for (int i = 1; i <= N;i++){
				var ans = 0;
                foreach(var a in i.ToString()){
                    ans += (int)(a - '0');

                }if (A <= ans && ans <= B)
                    sum+= i;
            }
            WriteLine(sum);

        }
    }


    public class ConsoleInput
    {
        private readonly System.IO.TextReader _stream;
        private char _separator = ' ';
        private Queue inputStream;

        public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
        {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue();
        }
        public string Read
        {
            get
            {
                if (inputStream.Count != 0) return (string)inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for (int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return (string)inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); } }
        public int ReadInt { get { return int.Parse(Read); } }
        public long ReadLong { get { return long.Parse(Read); } }
        public double ReadDouble { get { return double.Parse(Read); } }
        public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
    }

}

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

typedef long long ll;

int main(){
    ll N, W;
    cin >> N >> W;
    vector<ll> w(N), v(N);
    for (ll i=0; i<N; i++) cin >> w.at(i) >> v.at(i);

    vector<vector<int>> v2(4, vector<int>(0)), s(4, vector<int>(1, 0));
    for (int i=0; i<N; i++){
        v2.at(w.at(i)-w.at(0)).push_back(v.at(i));
    }
    for (int i=0; i<4; i++) {
        sort(v2.at(i).begin(), v2.at(i).end());
        reverse(v2.at(i).begin(), v2.at(i).end());
        for (int j=0; j<v2.at(i).size(); j++) s.at(i).push_back(s.at(i).at(j) + v2.at(i).at(j));
    }

    ll ans = 0;

    for (int i=0; i<=v2.at(0).size(); i++){
        for (int j=0; j<=v2.at(1).size(); j++){
            for (int k=0; k<=v2.at(2).size(); k++){
                for (int l=0; l<=v2.at(3).size(); l++){
                    ll weight = w.at(0)*i + (w.at(0)+1)*j + (w.at(0)+2)*k + (w.at(0)+3)*l;
                    ll value = s.at(0).at(i) + s.at(1).at(j) + s.at(2).at(k) + s.at(3).at(l);
                    if (weight <= W){
                        ans = max(ans, value);
                    }
                }
            }
        }
    }

    cout << ans << endl;

}

Measured memory_bytes: 256
#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; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
//------------------------------------------------------------------------------
int N;
vector<ll> a;
vector<ll> sum;

ll dp[410][410];

// [l, r)
ll Solve(int l, int r)
{
    assert(r - l > 0);
    if (dp[l][r] >= 0) return dp[l][r];
    if (r - l == 1) return 0;
    if (r - l == 2) return a[l] + a[l + 1];
    ll res = 1000000000000000000LL;
    for (int m = l + 1; m < r; ++m)
    {
        chmin(res, Solve(l, m) + Solve(m, r) + sum[r] - sum[l]);
    }
    return dp[l][r] = res;
}

void _main()
{
    memset(dp, -1, sizeof(dp));
    cin >> N;
    a.resize(N);
    rep(i, 0, N) cin >> a[i];
    sum.assign(N + 1, 0);
    rep(i, 0, N) sum[i + 1] = sum[i] + a[i];
    cout << Solve(0, N) << endl;
}


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

const int N = 100005;

int n, mod;
vector<int> g[N];
int f[N];
int ans[N];

int mul(int x,int y) {
	return (long long) x * y % mod;
}

void dfs(int u,int p) {
	auto it = find(g[u].begin(), g[u].end(), p);
	if (it != g[u].end()) {
		g[u].erase(it);
	}
	f[u] = 1;
	for (int v : g[u]) {
		dfs(v, u);
		f[u] = mul(f[u], f[v] + 1);
	}
}

void dfs2(int u,int up) {
	ans[u] = mul(f[u], up + 1);
	int sz = g[u].size();
	vector<int> pre(sz);
	vector<int> suf(sz);
	int now = 1;
	for (int i = 0; i < sz; ++i) {
		int v = g[u][i];
		now = mul(now, f[v] + 1);
		pre[i] = now;
	}
	now = 1;
	for (int i = sz - 1; ~i; --i) {
		int v = g[u][i];
		now = mul(now, f[v] + 1);
		suf[i] = now;
	}
	for (int i = 0; i < sz; ++i) {
		int v = g[u][i];
		int coef = 1;
		if (i) {
			coef = mul(coef, pre[i - 1]);
		}
		if (i + 1 < sz) {
			coef = mul(coef, suf[i + 1]);
		}
		dfs2(v, mul(coef, up + 1));
	}
} 

int main() {
	scanf("%d %d", &n, &mod);
	for (int i = 2; i <= n; ++i) {
		int u, v; scanf("%d %d", &u, &v);
		g[u].push_back(v), g[v].push_back(u);
	}
	dfs(1, -1), dfs2(1, 0);
	for (int i = 1; i <= n; ++i) {
		printf("%d\n", ans[i]);
	}
}

Measured memory_bytes: 17664
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define ALL(v) ((v).begin(),(v).end())
using namespace std;

int main(){
    long long l,r;
    cin >> l >> r;
    long long ans=2018;
    bool cont = true;
    for(long long i=l; i<r; i++){
        if(cont==false){
            break;
        }
        for(long long j=i+1; j<=r; j++){
            ans = min(ans, (i*j)%2019);
            if((i*j)%2019 == 0){
                cont = false;
                break;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
// coded by zeffy
#pragma GCC optimize("O3","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zeroupper") //Enable AVX
#pragma GCC target("avx","popcnt")  //Enable AVX
#include <x86intrin.h> //SSE Extensions
#include <bits/stdc++.h> 
using namespace std;
#define eb emplace_back
#define mp make_pair
#define hello cout<<"hello"<<"\n"
#define forr(i,a,b) for(int i=a;i<b;i++)
#define it(s)	for(auto itr:s)
#define dvg(s) 	for(auto itr:s)	cout<<itr<<" ";cout<<endl;
#define dbg(s)	cout<<#s<<"= "<<s<<endl;
typedef long long int lli;
typedef unsigned long long int ulli;
const lli INF=(lli)1e17+5;
const ulli MOD=1e8+7;

int main()
{
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    std::cout<< std::fixed;
    std::cout.precision(10);
    
    int n;cin>>n;
    lli a,b,c;
    cin>>a>>b>>c;
    lli dp[3];memset(dp,0,sizeof(dp));
    dp[0]=a;dp[1]=b;dp[2]=c;

    forr(i,1,n)
    {
       cin>>a>>b>>c;
       lli ndp[3];memset(ndp,0,sizeof(ndp));
       ndp[0]=a+max(dp[1],dp[2]);
       ndp[1]=b+max(dp[0],dp[2]);
       ndp[2]=c+max(dp[1],dp[0]);

       dp[0]=ndp[0];
       dp[1]=ndp[1];
       dp[2]=ndp[2];
    }

    cout<<max(dp[0],max(dp[1],dp[2]))<<"\n";
   return 0;
}

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

typedef long long ll;
typedef long double ld;

#define MOD 1000000007
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

ll gcd(ll a, ll b){return b ? gcd(b, a % b) : a;}

int main(){

    ll K;

    cin >> K;

    cout << (K / 2) * (K / 2 + K % 2) << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#define rep(i,n) for(int i = 0; i < (long int)n; i++)
typedef long long ll;
using namespace std;

int main(){
  ll l,r;
  cin >> l >> r;

  ll minimam = 2019;
  for(ll i = l; i < r; i++){
    for(ll j = i+1; j <= r; j++){
      minimam = min(minimam,(i*j)%2019);
      if(minimam == 0){
        cout << minimam;
        return 0;
      }
    }
  }
  cout << minimam;
  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int sosuch();

int main(){
  int a,n,min,sa,i;
  scanf("%d",&n);
  scanf("%d",&a);
  min=a;
  scanf("%d",&a);
  sa = a-min;
  if(min>a) min=a;
  for(i=0;i<n-2;i++){
    scanf("%d",&a);
    if(a-min > sa) sa = a-min;
    if(min>a) min=a;
  }
  printf("%d\n",sa);
return 0;
}



Measured memory_bytes: 2144
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
typedef long long LL;
const int N=200005;
const int MOD=1e9+7;
int add (int x,int y)   {x=x+y;return x>=MOD?x-MOD:x;}
int mul (int x,int y)   {return (LL)x*y%MOD;}
int dec (int x,int y)   {x=x-y;return x<0?x+MOD:x;}
int Pow (int x,int y)
{
	if (y==0) return 1;
	if (y==1) return x;
	int lalal=Pow(x,y>>1);
	lalal=mul(lalal,lalal);
	if (y&1) lalal=mul(lalal,x);
	return lalal;
}
int n;
int a[N];
int num[N];
int D[N],cnt[N];
int suf[N],pre[N];
int Inv (int x)	{return Pow(x,MOD-2);}
int f[2][N];
int lb (int x)	{return x&(-x);}
void modify (int op,int x,int c)	{while (x<=n)	{f[op][x]=add(f[op][x],c);x+=lb(x);}}
int query (int op,int x)	{int lalal=0;while (x>=1)	{lalal=add(lalal,f[op][x]);x-=lb(x);}return lalal;}
int main()
{
	scanf("%d",&n);
	for (int u=1;u<=n;u++) scanf("%d",&a[u]),num[a[u]]++;
	for (int u=n-1;u>=1;u--) num[u]+=num[u+1];
	int S=1;D[0]=1;
	for (int u=1;u<=n;u++)
	{
		cnt[u]=num[u]-(n-u);S=mul(S,cnt[u]);
		if (cnt[u]<=0)	{printf("0\n");return 0;}
		D[u]=D[u-1];
		if (cnt[u]>1)	{D[u]=mul(D[u],mul(cnt[u]-1,Inv(cnt[u])));pre[u]=pre[u-1];}
		else pre[u]=u;
	}
	//printf("%d\n",S);
	suf[n]=n;
	for (int u=n-1;u>=1;u--)
	{
		if (cnt[u]>1)
		{
			if (cnt[u+1]>1) suf[u]=suf[u+1];
			else suf[u]=u;
		}
		else	suf[u]=u;
	}
	int ans=0;
	for (int u=1;u<=n;u++)
	{
		int tmp=dec(query(0,a[u]),query(0,pre[a[u]]-1));
		tmp=mul(mul(D[a[u]],mul(tmp,S)),Inv(2));
		ans=add(ans,tmp);
		modify(0,a[u],Inv(D[a[u]]));
	}
	memset(f[0],0,sizeof(f[0]));
	for (int u=1;u<=n;u++)
	{
		int tmp1=mul(S,dec(query(1,n),query(1,a[u]))),tmp;
		
		tmp=dec(query(0,suf[a[u]]),query(0,a[u]));
		tmp=mul(mul(Inv(D[a[u]]),mul(tmp,S)),Inv(2));
		ans=add(ans,dec(tmp1,tmp));
		
		modify(0,a[u],D[a[u]]);
		modify(1,a[u],1);
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 6528
N, M = map(int, input().split())
PS = []
for i in range(M):
    p, s = input().split()
    PS.append([int(p), s])

ac = [False] * N
pena = [0] * N

for p, s in PS:
    p -= 1
    if ac[p]:
        continue

    if s == 'AC':
        ac[p] = True
    else:
        pena[p] += 1

a_cnt = 0
w_cnt = 0
for i in range(N):
    if ac[i]:
        a_cnt += 1
        w_cnt += pena[i]

print(a_cnt, w_cnt)

Measured memory_bytes: 23464
#include<bits/stdc++.h>
 
using namespace std;
 
int main(){
  long long q,h,s,d,n;
  cin>>q>>h>>s>>d>>n;
  q*=4,h*=2;
 
  long long x=min(s,min(q,h));
 
  if(n==1||x*2<d)cout<<x*n<<endl;
  else cout<<n/2*d+(n%2)*x<<endl;
  
  return 0;
}

Measured memory_bytes: 256