akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define size_t unsigned long long
#define ll long long
#define rep(i,a) for(int i=0;i<(a);i++)
#define repr(i,a) for(int i=(int)(a)-1;i>=0;i--)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(int)(b)-1;i>=a;i--)
#define ALL(a) a.begin(), a.end()
using namespace std;
int si() { int x; scanf("%d", &x); return x; }
long long sl() { long long x; scanf("%lld", &x); return x; }
string ss() { string x; cin >> x; return x; }
void pi(int x) { printf("%d ", x); }
void pl(long long x) { printf("%lld ", x); }
void pd(double x) { printf("%.15f ", x); }
void ps(const string &s) { printf("%s ", s.c_str()); }
void br() { putchar('\n'); }

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 5;

struct mint {
    int n;
    mint(int n_ = 0) : n(n_) {}
};

mint operator+(mint a, mint b) { a.n += b.n; if (a.n >= MOD) a.n -= MOD; return a; }
mint operator-(mint a, mint b) { a.n -= b.n; if (a.n < 0) a.n += MOD; return a; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

typedef pair<int, int> P;

const ll N = 1e6+5;
string s;
vector<ll> v[N];
ll n;
ll a[2],t[2],b[2];

int main () {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> t[0] >> t[1] >> a[0] >> a[1] >> b[0] >> b[1];
  ll f_dif = t[0] * a[0] - t[0] * b[0];
  ll dif = t[0] * a[0] + t[1] * a[1] - t[0] * b[0] - t[1] * b[1];
  if((f_dif < 0 && dif < 0) ||  (dif > 0 && f_dif > 0)) {
    cout << 0 << endl;
    return 0;
  }
  if(dif == 0) {
    cout << "infinity" << endl;
    return 0;
  }
  dif = abs(dif);
  f_dif = abs(f_dif);
  ll count = f_dif / dif;
  ll result;
  if(f_dif % dif == 0) result = count * 2;
  else result = count * 2 + 1;
  cout << result << endl;
}


Measured memory_bytes: 23680
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
0 0 3 4
1
2 5

output:
3.1200000000 4.1600000000
"""

import sys


def solve(_prj_info):
    for point in _prj_info:
        xp, yp = map(int, point)
        p = xp + yp * 1j
        hypo = p - p1
        prj = p1 + base_vector * project(base_vector, hypo)
        print('{:.10f} {:.10f}'.format(prj.real, prj.imag))

    return _prj_info


def dot(a, b):
    return a.real * b.real + a.imag * b.imag


def project(a, b):
    return dot(a, b) / dot(a, a)


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    base_info = _input[0].split()
    q_num = int(_input[1])
    prj_points = map(lambda x: x.split(), _input[2:])

    x1, y1, x2, y2 = map(int, base_info)
    p1, p2 = x1 + y1 * 1j, x2 + y2 * 1j
    base_vector = p2 - p1
    res = solve(prj_points)

Measured memory_bytes: 7780
def selection(arr)
  n = arr.length
  for i in 0...n
    mini = i
    for j in i...n
      if arr[j][1] < arr[mini][1]
        mini = j
      end
    end
    if i != mini
      arr[i], arr[mini] = arr[mini], arr[i]
    end
  end
  return arr
end
def bubble(arr)
  n = arr.length
  for i in 0...n
    for j in 1..(n - i - 1)
      if arr[n - j][1] < arr[n - j - 1][1]
        arr[n - j], arr[n - j - 1] = arr[n - j - 1], arr[n - j]
      end
    end
  end
  return arr
end
gets.to_i
arr = gets.split(" ").map{|item|item.split(//)}
bub = bubble(arr.dup)
sel = selection(arr.dup)
i = 0
arr.sort_by!{|a|[a[1], i += 1]}
puts bub.map(&:join).join(" ")
if bub == arr
  puts "Stable"
else
  puts "Not stable"
end
puts sel.map(&:join).join(" ")
if sel == arr
  puts "Stable"
else
  puts "Not stable"
end

Measured memory_bytes: 6128
N = int(input())

S = input()

ans = 0


sumR = [0]
sumG = [0]
sumB = [0]

for s in S:
    if s == "R":
        sumR.append(sumR[-1] + 1)
        sumG.append(sumG[-1])
        sumB.append(sumB[-1])
    elif s == "G":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1] + 1)
        sumB.append(sumB[-1])
    elif s == "B":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1])
        sumB.append(sumB[-1] + 1)


for i, s in enumerate(S):
    not_cnt = 0
    for k in range(1, min(i + 1, N - i)):
        L, R = S[i - k], S[i + k]
        if s != L and s != R and L != R:
            not_cnt += 1

    cnt = 0
    if s == "R":
        cnt += sumG[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumG[-1] - sumG[i + 1])
    elif s == "G":
        cnt += sumR[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumR[-1] - sumR[i + 1])
    elif s == "B":
        cnt += sumG[i] * (sumR[-1] - sumR[i + 1])
        cnt += sumR[i] * (sumG[-1] - sumG[i + 1])

    cnt -= not_cnt
    ans += cnt


print(ans)


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

int A[100005], B[100005], BIT[100005];
map<int, int> M;

void update(int i)
{
    for(; i<=100000; i+= (i&(-i)))
        BIT[i]++;
}

int query(int i)
{
    int ans = 0;
    
    for(; i>0; i-= (i&(-i)))
        ans += BIT[i];
    
    return ans;
}

long long func(int N, int K)
{
    for(int i=1; i<=N; i++)
    {
        if(A[i] <= K)
            B[i] = -1;
        else
            B[i] = 1;
    }
    
    M.clear();
    memset(BIT, 0, sizeof(BIT));
    
    M[0];
    
    int k = 0;
    long long ans = 0;
    
    for(int i=1; i<=N; i++)
    {
        B[i] += B[i-1];
        M[ B[i] ];
    }
    
    for(auto &it : M)
        it.second = ++k;
    
    update(M[0]);
    
    for(int i=1; i<=N; i++)
    {
        ans += i - query(M[ B[i] ]);
        update(M[ B[i] ]);
    }
    
    return ans;
}

int main() {

    ios::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);

    int N;
    cin >> N;
    
    for(int i=1; i<=N; i++)
        cin >> A[i];
    
    int left = 1, right = (int)1e9, ans = 0;
    long long subarrays = (long long) N * (N+1) / 2;
    
    while(left <= right)
    {
        int mid = (left + right)/2;
        
        if(func(N, mid) >= (subarrays+2)/2)
            ans = mid, right = mid - 1;
        else
            left = mid + 1;
    }
    
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 8192
#include<bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
//#include<cstring>
//#include<string>
//#include<queue>
//#include<cmath>
//#include<algorithm>
//#define debug1 cout<<"?"<<endl;
//#define debug2 cout<<"?"<<i<<endl;
#define PII pair<int,int>
typedef long long ll;
const int maxn=3e5+9;
const double pi=acos(-1);
const int mod=998244353;
const int INF=0x3f3f3f3f;
const double ep=1e-7;
using namespace std;
int main()
{
	int a[4][4]={0};
	int i,j,b[4][4]={0};
	for(i=1;i<=3;i++)for(j=1;j<=3;j++)scanf("%d",&b[i][j]);
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int x;
		scanf("%d",&x);
		for(i=1;i<=3;i++)for(j=1;j<=3;j++)if(b[i][j]==x)a[i][j]=1;
	}
	for(i=1;i<=3;i++)
	{
		int sum=0;
		for(j=1;j<=3;j++)sum+=a[i][j];
		if(sum==3)
		{
			puts("Yes");return 0;
		}
		sum=0;
		for(j=1;j<=3;j++)sum+=a[j][i];
		if(sum==3)
		{
			puts("Yes");return 0;
		}
	}
	if(a[1][1]+a[2][2]+a[3][3]==3||a[3][1]+a[2][2]+a[1][3]==3)puts("Yes");
	else puts("No");
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main(){
  int n,k;
  cin>>n;
  cin>>k;
  vector<int> h(n+1,0);
  for(int i=1;i<=n;i++){
    cin>>h[i];
  }
  h[0] = 0;
  vector<int> mincost(n+1);
  mincost[1]  = 0;
  mincost[2] = abs(h[2]-h[1]);
  for(int i=3;i<=n;i++){
    int mini = INT_MAX;
    for(int j = i-1;j>=i-k && j>=1 ;j--){
        mini  = min(mincost[j]+ abs(h[i]-h[j]), mini);
    }  
    mincost[i] = mini;
    //mincost[i] = min(mincost[i-1] + (long long int)abs(h[i]-h[i-1]) ,mincost[i-2] + (long long int)abs(h[i]-h[i-2]));
  }
  cout<<mincost[n];
  return 0;
}

Measured memory_bytes: 4052
#include<bits/stdc++.h>
//macros
using namespace std;
typedef long long ll;
typedef pair<int, int > Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef vector<ll> Vll;
#define pb push_back
#define rs resize 
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repll(i, a, b) for(ll i=(a); i<(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define COUT(a) cout << a << endl
#define ALL(a) (a).begin(),(a).end()
const long long int mod = 1e9 + 7;
const double pi = 3.141592653589;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false); 
    double a,b,h,m;
    cin >> a >> b >> h >> m;

    double hour = 30*h+1./2*m;
    double minute = 6*m;
    double ang = hour-minute;
    if(ang<0) ang = -ang;
    if(ang>180) ang = 360-ang;
    //cout << ang << endl;
    double dist = a*a+b*b-2.*a*b*cos(ang*pi/180.);
    cout << setprecision(10) <<sqrt(dist) << endl;
}

Measured memory_bytes: 4168
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int x = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
   
        if(Math.abs(a-x) < Math.abs(b-x)){
            System.out.println("A");
        }else{
            System.out.println("B");
        }
    }       
}

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

public class Solution
{
    public static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var a = vals[0];
        var b = vals[1];
        var c = vals[2];

        if (a == b && b == c)
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}

Measured memory_bytes: 11348
#include<stdio.h>

int main(){
	
	int N;
	scanf("%d",&N);
	int x;
	x = N + 1;
	
	if(N%2 == 0){
		printf("%d",N / 2);
	}
	else{
		printf("%d",x / 2);
	}
	
	
	
	
	return 0;
}

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

using namespace std;

int gcd(int x, int y){
	if(y == 0) return x;
	return gcd(y, x % y);
}

int main(){
	int n, m;
	string s, t;
	cin >> n >> m >> s >> t;
	int g = gcd(n, m);
	bool f = true;
	if(g == 1){
		if(s[0] != t[0]) f = false;
	}else{
		for(int i = 0; i < g; i++){
			if(s[i * (n / g)] != t[i * (m / g)]) f = false;
		}
	}
	if(f){
		cout << (long long)n * m / g << endl;
	}else{
		cout << -1 << endl;
	}

	return 0;
}

Measured memory_bytes: 512
#include <iostream>

using namespace std;

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

    int r_cnt = 0;
    for(int i = 0; i < n; ++i){
        if(s[i] == 'R') r_cnt += 1;
    }
    
    int b_cnt = n-r_cnt;

    if(r_cnt > b_cnt) cout << "Yes" << endl;
    else cout << "No" << endl;
    
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,k;
int main()
{
	scanf("%lld%lld",&n,&k);
	if(n<=k){
		printf("%lld",min(n,k-n));
	} else {
		printf("%lld",min(n%k,k-n%k));
	}
	return 0;
}

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

using namespace std;
using ll = long long;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()

#define READ(x) (cin >> (x))

#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))

int calc_count_choose_2(int n) {
    return (n >= 2) ? (n * (n - 1) / 2) : 0;
}

int main() {
    // get values from input
    int N, M;
    READ(N);
    READ(M);

    // main procedure
    int ans = calc_count_choose_2(N) + calc_count_choose_2(M);

    // output
    WRITE_N(ans);

    return 0;
}


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

int main()
{
    ll a, b, c, d;
    ll ans = - (1e9+1)*(1e9+1);
    // cout << ans;
    cin >> a >> b >> c >> d;
    ans = max(ans, a*c);
    ans = max(ans, a*d);
    ans = max(ans, b*c);
    ans = max(ans, b*d);
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3656
#include<iostream>

using namespace std;

int data[10][10];

void func(int x,int y){
	data[y][x] = 0;
	for(int i = -3 ; i <= 3 ; i ++){
		if(x+i<0 || 9 < x+i) continue;
		if(data[y][x+i] == 1)func(x+i,y);
	}
	for(int j = -3; j <= 3 ; j ++){
		if(y+j<0 || 9 < y+j)continue;
		if(data[y+j][x] == 1)func(x,y+j);
	}
}

void PrintBoard(){
	for(int i = 1; i <= 8 ; i ++){
		for(int j = 1; j <= 8 ; j ++){
			cout<<data[i][j];
		}
		cout<<endl;
	}
}

int main(void){
	int n;
	cin>>n;
	for(int i = 0 ; i < 10 ; i ++){
		for(int j = 0 ; j < 10 ; j ++){
			data[i][j] = 0 ;
		}
	}
	char tmp;
	int x,y;
	for(int k = 1; k <=n ; k++){
		for(int i = 1 ; i <= 8 ; i ++){
			for(int j = 1 ; j <= 8 ; j ++){
				cin>>tmp;
				data[i][j] = tmp-'0';
			}
		}
		cin>>x;
		cin>>y;
		func(x,y);
		cout<<"Data "<<k<<":"<<endl;
		PrintBoard();
	}
	return 0;
}

Measured memory_bytes: 1164
a, b, k = map(int, input().split())
A = -1
B = -1

if a+b <= k:
    A = 0
    B = 0
elif a < k:
    A = 0
    B = b + a - k
elif k <= a:
    A = a - k
    B = b
print(str(A) + " " + str(B))


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

using namespace std;

bool check(string &s, int b, int e) {
    for (int i = b; i < e; ++i) {
        if (s[i] == '#' && s[i + 1] == '#') {
            return 0;
        }
    }
    return 1;
}

bool check3(string &s, int b, int e) {
    for (int i = max(2, b); i <= e; ++i) {
        if (s[i - 1] == '.' && s[i] == '.' && s[i + 1] == '.') {
            return 1;
        }
    }
    return 0;
}

void solve() {
    int n;
    cin >> n;
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    string s;
    cin >> s;
    s = '$' + s;

    if (!check(s, a, c) || !check(s, b, d)) {
        cout << "No";
        return;
    }
    if (c < d) {
        cout << "Yes";
        return;
    }
    cout << (check3(s, b, d) ? "Yes" : "No");
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(20);

    solve();
}


Measured memory_bytes: 720
#define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <climits>
#include <cmath>
#include <utility>
#include <iomanip>
//#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long int;
//// 任意長整数型
//namespace mp = boost::multiprecision;
//using Bint = mp::cpp_int;



int main(int argc, char *argv[])
{
	int a;
	//input
	cin >> a;


	//calc
	int ans = a + a * a + a * a * a;


	//ans
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL

const int N = 100005;
 
// it's swapnil07 ;)
 
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif


void solve(){
    int n; cin>>n;
    int x = n%10;
    if(x == 3){
        cout<<"bon";
    }
    else if(x==1 || x==0 || x==6 || x==8){
        cout<<"pon";
    }
    else{
        cout<<"hon";
    }
}

signed main()
{
    fast
    #ifdef SWAPNIL07
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int t=1; 
    // cin>>t;
    while(t--){
        solve();
        cout<<"\n";
    }
    return 0;
}



Measured memory_bytes: 3648
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;
 
#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
 
ll dp[60][60][3001];//i,j,k  xiまで、j枚選び、合計k
 
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
 
  ll N, A;
  cin >> N >> A;
  vector<ll> x;
  x.pb(0);
  rep(i,N){
    ll inp; cin >> inp;
    x.pb(inp);
  }
 
  rep(i,N)dp[i][0][0]=1;//0枚選んで合計0のやつが全部1通り
 
  for(ll i =1;i<=N;i++){
    for(ll j = 1;j<=N;j++){
      for(ll k = 1;k<=3000;k++){
	if(k<x[i])dp[i][j][k]=dp[i-1][j][k];
	else dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k-x[i]];
      }	
    }
  }
  ll ans =0;
  rep(i,N+1){
    if (i==0) continue;
    ans += dp[N][i][i*A];
  }
  cout << ans << endl;
  
  return 0;
}


Measured memory_bytes: 70528
fn getline() -> String {
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

#[allow(unused_macros)]
macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator,
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display,
{
    for (i, e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}


#[allow(unused_imports)]
use std::cmp::{min, max};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, BTreeMap, HashMap, HashSet};
#[allow(unused_imports)]
use std::collections::btree_map::Entry::{Occupied, Vacant};

fn main() {
    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    macro_rules! print { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); }
    macro_rules! println { () => (writeln!(out).unwrap()); ($($arg:tt)*) => (writeln!(out, $($arg)*).unwrap()); }

    let s = readl!(String).into_bytes();
    let mut cnt = [0_i32, 0, 0];
    for e in s {
        cnt[(e-'a' as u8) as usize] += 1;
    }
    for i in 0..3 {
        for j in 0..i {
            if (cnt[i]-cnt[j]).abs() >= 2 {
                println!("NO");
                return;   
            }
        }
    }
    println!("YES");
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 3100;
int dp[maxn][maxn], n, m;
bool pntr[maxn][maxn];
string s, t;

int main() {
    cin>>s;
    cin>>t;

    n = s.length();
    m = t.length();

    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            if(s[i-1] == t[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else {
                if(dp[i-1][j] > dp[i][j-1]) {
                    dp[i][j] = dp[i-1][j];
                    pntr[i][j] = 0;
                }
                else {
                    dp[i][j] = dp[i][j-1];
                    pntr[i][j] = 1;
                }
            }
        }
    }

    int x = n, y = m;
    string result = "";
    while(x > 0 && y > 0) {
        if(s[x-1] == t[y-1]) {
            result += s[x-1];
            x--;
            y--;
        }
        else {
            if(pntr[x][y] == 0) x--;
            else y--;
        }
    }
    reverse(result.begin(), result.end());
    cout<<result<<"\n";
    return 0;
}


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

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

int fill(vector<vector<bool> >& map, int y, int x){
    if(!map[y][x]) return 0;
    map[y][x] = false;
    for(int i=0; i<8; i++){
        fill(map, y+dy[i], x+dx[i]);
    }
    return 1;
}
        
int main(){
    while(1){
        int w,h;
        cin >> w >> h;
        if(w==0) break;

        vector<vector<bool> > map(h+2, vector<bool>(w+2, false));
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                int tmp;
                cin >> tmp;
                if(tmp==1) map[i][j]=true;
            }
        }

        int count=0;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                count+= fill(map, i,j);
            }
        }
        cout << count << endl;
    }   
    return 0;
}

Measured memory_bytes: 3076
import itertools
import math
import fractions
import functools
n, k = map(int, input().split())
a = []
for i in range(k):
    di = int(input())
    ai = list(map(int, input().split()))
    a.append(ai)

count=set()

for i in range(k):
    for j in range(len(a[i])):
        count.add(a[i][j])

print(n-len(count))

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

using namespace std;
typedef long long ll;

int main() {
	int n;
	cin >> n;
	
	int ans;
	cin >> ans;
	
	for(int i=1; i < n; i++) {
		int x;
		cin >> x;
		ans = __gcd(x,ans);
	}
	
	cout << ans;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i, n) for(int i=0; i<(int)(n); ++i)
const int MX = 1e5+5;
vector<vector<int>> dp(512, vector<int>(MX));


int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n, q;
  cin >> n;
  vector<int> v(n+1), w(n+1);
  for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
  for (int i = 1; i < 512; ++i) {
    for (int j = 1e5; j >= w[i]; --j) {
      dp[i][j] = max(dp[i/2][j], dp[i/2][j-w[i]]+v[i]);
    }
    for (int j = w[i]-1; j >= 0; --j) {
      dp[i][j] = dp[i/2][j];
    }
  }

  cin >> q;
  rep(_, q) {
    int u, l;
    cin >> u >> l;
    vector<int> ids;
    while (u >= 512) {
      ids.push_back(u);
      u >>= 1;
    }
    int ans = 0;
    int sz = ids.size();
    rep(t, (1<<sz)) {
      int sv = 0;
      int sw = 0;
      rep(i, sz) {
        if (t & (1<<i)) {
          sv += v[ids[i]];
          sw += w[ids[i]];
        }
      }
      if (sw <= l) {
        ans = max(ans, sv + dp[u][l-sw]);
      }
    }
    cout << ans << endl;
  }
}

Measured memory_bytes: 205876
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long llong;
const int inf = 1 << 20;
const int mod = 1e9 + 7;

int main() {
   int n;
   cin >> n;
   vector<tuple<int, int, int>> tasks;
   rep(i, n) {
      int a, b;
      cin >> a >> b;
      tasks.push_back(tie(b, a, i));
   }
   sort(tasks.begin(), tasks.end());
   int now = 0;
   rep(i, n) {
      int time = get<1>(tasks[i]);
      int deadline = get<0>(tasks[i]);
      if (now + time > deadline) {
         cout << "No" << endl;
         return 0;
      }

      now += time;
   }
   cout << "Yes" << endl;
   return 0;
}

Measured memory_bytes: 4852
#include <bits/stdc++.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#pragma clang diagnostic pop

using namespace std;

struct dinic_t {
 public:
  explicit dinic_t(int nn_) : graph(nn_, vector<edge_t>()), nn(nn_) {}

  void add_edge(int from, int to, int cap) {
    graph[from].push_back(edge_t(to, cap, static_cast<int>(graph[to].size())));
    graph[to].push_back(
        edge_t(from, 0, static_cast<int>(graph[from].size()) - 1));
  }

  int run(int s, int t) {
    int ret = 0;
    int f;
    while (static_cast<void>(bfs(s)), level[t] >= 0) {
      itr.assign(nn, 0);
      while ((f = dfs(s, t, INT_MAX)) > 0) {
        ret += f;
      }
    }
    return ret;
  }

 private:
  struct edge_t {
    edge_t(int to_, int cap_, int reversed_)
        : to(to_), cap(cap_), reversed(reversed_) {}
    int to;
    int cap;
    int reversed;
  };
  std::vector<std::vector<edge_t>> graph;
  std::vector<int> itr;
  std::vector<int> level;
  int nn;

  void bfs(int s) {
    level.assign(nn, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int const v = q.front();
      q.pop();
      for (const auto& e : graph[v]) {
        if (e.cap <= 0 || level[e.to] >= 0) {
          continue;
        }
        level[e.to] = level[v] + 1;
        q.push(e.to);
      }
    }
  }

  int dfs(int v, int t, int f) {
    if (v == t) {
      return f;
    }
    for (int& i = itr[v]; i < static_cast<int>(graph[v].size()); ++i) {
      edge_t& e = graph[v][i];
      if (e.cap <= 0 || level[v] >= level[e.to]) {
        continue;
      }
      int d = dfs(e.to, t, std::min(e.cap, f));
      if (d > 0) {
        e.cap -= d;
        graph[e.to][e.reversed].cap += d;
        return d;
      }
    }
    return 0;
  }
};

struct position_t {
  int x;
  int y;
};

int64_t solve(int n, std::vector<position_t> rs, std::vector<position_t> bs) {
  dinic_t dinic(n * 2 + 2);
  for (int i = 0; i < n; ++i) {
    dinic.add_edge(n * 2, i, 1);
    dinic.add_edge(n + i, n * 2 + 1, 1);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (rs[i].x >= bs[j].x || rs[i].y >= bs[j].y) {
        continue;
      }
      dinic.add_edge(i, n + j, 1);
    }
  }
  return dinic.run(n * 2, n * 2 + 1);
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  std::vector<position_t> rs(n);
  for (int i = 0; i < n; i++) {
    std::cin >> rs[i].x;
    std::cin >> rs[i].y;
  }
  std::vector<position_t> bs(n);
  for (int i = 0; i < n; i++) {
    std::cin >> bs[i].x;
    std::cin >> bs[i].y;
  }

  auto const res = solve(n, std::move(rs), std::move(bs));
  std::cout << res << std::endl;
  return 0;
}


Measured memory_bytes: 384
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

const int N = 52;

int n, avg;
int in[N];
long long cache[N][N][N * N];


ll dp(int k, int cnt, int sum) {
    if (k == n) {
        if(cnt == 0) return 0;
        if (sum % cnt) return 0;
        if (sum / cnt == avg) return 1;
        return 0;
    }

    ll &rv = cache[k][cnt][sum];
    if(~rv) return rv;

    rv = 0;
    rv += dp(k + 1, cnt, sum);
    rv += dp(k + 1, cnt + 1, sum + in[k]);
    return rv;
}

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

    memset(cache, -1, sizeof cache);
    ll res = dp(0, 0, 0);
    cout << res << endl;
}

Measured memory_bytes: 57344
#include<bits/stdc++.h>
using namespace std;
#define int long long 
long long x[100010],y[100010],z[100010];
signed main(){

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

  long long a,b,c,cnt=0,ans=0,n,ccnt=0,wa=0;
  string s,v;
  char k[100010];

  cin>>s;
  if(s<="2019/04/30"){
    cout<<"Heisei"<<'\n';
  }else{
    cout<<"TBD"<<'\n';
  }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using vc = vector<char>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
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<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;} 
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;



int main()
{
        ll N, K;
        cin >> N >> K;
        ll ans = 0;
        rep(i, 0, N) {
                ll h;
                cin >> h;
                if (h >= K) ++ans;
        }
        cout << ans << endl;
}

Measured memory_bytes: 3624
import java.util.*;
// warm-up
public class Main {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i=0, no=N, p=0, q=0;
		int[] a = new int[N];
		boolean f = true;
		while (no-->0) a[i++] = sc.nextInt();
		Arrays.sort(a);
		for (i=N-1; i>=0; i--) {
			if (f) p+=a[i];
			else q+=a[i];
			f=!f;
		}
		System.out.println(p-q);
		sc.close();		
	}

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

}

Measured memory_bytes: 35844
#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(){
    /*
    int N, K;
    cin >> N >> K;
    */
    string st0, st1;
    cin >> st0 >> st1;
    int f = 0;
    if(st0.at(0) != st1.at(2)) f = 1;
    if(st0.at(1) != st1.at(1)) f = 1;
    if(st0.at(2) != st1.at(0)) f = 1;
    if(f == 0){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
    return 0;
}


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

int main() {
    int n, t;
    cin >> n >> t;
    vector<pair<int, int>> p(n); 
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> p.at(i).first >> p.at(i).second;
        a[i] = p.at(i).first;
        b[i] = p.at(i).second;
    }

    sort(p.begin(), p.end());
    
    vector<vector<long long>> dp(n + 1, vector<long long>(t + 3000 + 1));
    for (int i = 0; i < n; i++) {
        long long a = p.at(i).first, b = p.at(i).second;
        for (int j = 0; j < t; j++) {
            dp.at(i + 1).at(j + a) = max(dp.at(i + 1).at(j + a), dp.at(i).at(j) + b);
        }
        for (int j = 0; j <= t + 3000; j++) {
            dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), dp.at(i).at(j));
        }
    }
    
    long long maxi = 0;
    for (int i = 0; i <= t + 3000; i++) {
        maxi = max(maxi, dp.at(n).at(i));
    }
    cout << maxi << endl;
}

Measured memory_bytes: 141184
use std::io;
use std::io::prelude::*;

fn run(args: &Vec<String>) -> String {
    let mut si = args[1..].to_vec();
    si.sort();

    let mut s = String::new();
    for t in si {
        s = format!("{}{}", s, t);
    }
    s
}

fn main() {
    let stdin = io::stdin();
    let args = stdin.lock().lines().map(|l| l.unwrap()).collect();
    let r = run(&args);
    println!("{}", r);
}


Measured memory_bytes: 636
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <fstream>
#include <cmath>
#include <random>
#include <complex>
#include <functional>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template<class T> inline void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}
template<class T> inline void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}

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

    char C;
    cin >> C;
    C++;
    cout << C << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(),(a).end()
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

int m, n, p[10005], c[555], e[555];
int memo[555][10005];
int s[20005];

int dp(int i, int j)
{
	if (i == n || j >= m) return 0;
	if (memo[i][j] != -1) return memo[i][j];
	int res;
	int sum = s[j + c[i]] - s[j];
	if (sum <= e[i]) res = dp(i + 1, j);
	else
	{
		res = max(dp(i + 1, j), dp(i + 1, j + c[i]) + sum - e[i]);
	}
	return memo[i][j] = res;
}

int main()
{
	ms(memo, -1);
	cin >> m >> n;
	REP(i, m) cin >> p[i];
	REP(i, n) cin >> c[i] >> e[i];
	sort(p, p + m, greater<int>());
	s[0] = 0;
	FOR(i,1, m+1) s[i] = s[i - 1] + p[i-1];
	FOR(i,m+1,20005) s[i] = s[i - 1];
	cout << dp(0, 0) << endl;
	return 0;
}

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

int main() {
  int n,ans;
  cin >> n;
  ans = n*n*n;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef vector<string>vs;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef vector<double>vd;
typedef pair<int,int>pii;
typedef long long ll;
typedef pair<ll,ll>pll;
typedef vector<ll>vl;
#define rrep(i,x,n) for(int i=(x);i<(n);++i)
#define rep(i,x) rrep(i,0,(x))
#define fi first
#define se second
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define pb push_back
#define maxs(a,b) (a)=max(a,b)
#define mins(a,b) (a)=min(a,b)

int main(){
    int N,M;
    while(scanf("%d%d",&N,&M),N||M){
        int mi=M+1;
        vector<pair<pii,int> >V;
        rep(i,N){
            int val;
            scanf("%d",&val);
            int p=(mi+val-1)/val*val;
            if(p>M)p=M/val*val;
            V.pb(pair<pii,int>(pii(p,val),i));
        }
        sort(all(V));
        if(V[0].fi==V[1].fi)printf("%d\n",N);
        else printf("%d\n",V[0].se+1);

    }
    return 0;
}

Measured memory_bytes: 1216
// Sum of Three Integers
#include <iostream>
using namespace std;
int main(){
    int k,s;
    long long ans=0;
    cin>>k>>s;
    for(int a=0;a<=k;a++){
        for(int b=0;b<=k;b++){
            if(s-a-b<=k&&0<=s-a-b) ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
def main():
    n, l = map(int, input().split())
    aa = [l + i for i in range(n)]
    if aa[0] <= 0 <= aa[-1]:
        print(sum(aa))
    elif aa[-1] <= 0:
        print(sum(aa[:-1]))
    else:
        print(sum(aa[1:]))


if __name__ == "__main__":
    main()


Measured memory_bytes: 3060
#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
//問題文および制約はちゃんと確認しよう！
//サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
//とりあえずサンプルを読んでから解法を考えよう？
vector<int> gr[100100],rgr[100100];
int now=0;
int num[100100],out[100100];
void dfs(int v){
	if(num[v]>=0) return;
	rep(i,rgr[v].size()) dfs(rgr[v][i]);
	num[v]=now;now++;
}
int main()
{
	int n,m,a,b;
	cin>>n>>m;
	rep(i,n-1+m){
		cin>>a>>b;
		gr[a].pb(b);
		rgr[b].pb(a);
	}
	memset(num,-1,sizeof(num));
	REP(i,1,n+1) dfs(i);
	REP(i,1,n+1){
		if(rgr[i].size()==0){
			cout<<0<<endl;
		}
		else{
			int so=rgr[i][0];
			rep(j,rgr[i].size()){
				if(num[so]<num[rgr[i][j]]) so=rgr[i][j];
			}
			cout<<so<<endl;
		}
	}
}


Measured memory_bytes: 15104
#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 a, b;
  cin >> a >> b;
  
  
  if (a.size() > b.size()) cout << "GREATER" << endl;
  else if (a.size() < b.size()) cout << "LESS" << endl;
  else {
    REP(i,a.size()) {
      if (a[i] > b[i]) {
        cout << "GREATER" << endl;
        break;
      }
      else if (a[i] < b[i]) {
        cout << "LESS" << endl;
        break;
      }
      if (i == a.size() - 1 && a[i] == b[i]) {
        cout << "EQUAL" << endl;
      }
    }
  }
}

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

int main(void){
    int a, b;
    char op;
    
    while(!cin.eof()){
        cin >> a >> op >> b;
    switch(op){
        case '+':
        cout << a + b << endl;
        break;
        
        case '-':
        cout << a - b << endl;
        break;
        
        case '*':
        cout << a * b << endl;
        break;
        
        case '/':
        cout << a / b << endl;
        break;
        
        case '?':
        default:
        return 0;
    }
    }
}

Measured memory_bytes: 3028
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include <cmath>
#include <limits>
#include<set>
#include <iomanip>
#include <queue>
#include <string>
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF=20000000000;
//setprecision(2) <<

const int MOD = 1000000007;

long long max(long long a,long long b){
	if(a<b)return b;
	else return a;	
}
long long min(long long a,long long b){
	if(a>b)return b;
	else return a;	
}
long long gcd(long long a,long long b){
	if(a<b)swap(a,b);
	if(a%b==0)return b;
	else return gcd(a%b,b);
}

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

long long com(long long n,long long k){
	
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	string s;
	cin>>s;
	reverse(s.begin(),s.end());
	string t="";
	for(int i=8;i<s.length();i++){
		t+=s[i];
	}
	reverse(t.begin(),t.end());
	cout<<t;
}

	


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc111_a
{
    class Program
    {
        public static int NumberChanges(string n)
        {
            int result = 0;

            if (n[0] == '1') result += 900;
            else result += 100;
            if (n[1] == '1') result += 90;
            else result += 10;
            if (n[2] == '1') result += 9;
            else result += 1;

            return result;
        }

        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            Console.WriteLine(NumberChanges(input));

        }
    }
}


Measured memory_bytes: 13140
#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;

int read()
{
	int x=0,f=1;
	char ch=getchar();
	while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar();
	while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return x*f;
}

const int N=131072;
const int S=2000050;

struct data
{
	LL dis,len;

	data (LL dis_=0,LL len_=0){dis=dis_,len=len_;}
}f[S],tmp[N][2];

int st[N],fa[N],a[N],cnt[N];
int son[N][2];
LL lim;
int n,idx;

void dp(int x,LL upp)
{
	if (!son[x][0])
	{
		f[st[x]=++idx]=data(0,0),cnt[x]=1;
		return;
	}
	dp(son[x][0],upp),dp(son[x][1],upp);
	if (!cnt[son[x][0]]||!cnt[son[x][1]]) return;
	st[x]=idx+1;
	int tot[2]={0,0},u=son[x][0],v=son[x][1];
	for (int i=1;i<=cnt[u];++i) f[st[u]+i-1].dis+=a[u],f[st[u]+i-1].len+=a[u];
	for (int i=1;i<=cnt[v];++i) f[st[v]+i-1].dis+=a[v],f[st[v]+i-1].len+=a[v];
	for (int cur=cnt[v],i=cnt[u];i>=1;--i)
	{
		for (;cur&&f[st[v]+cur-1].dis+f[st[u]+i-1].len>upp;--cur);
		if (cur) tmp[++tot[0]][0]=data(f[st[u]+i-1].dis,f[st[v]+cur-1].len);
	}
	for (int cur=cnt[u],i=cnt[v];i>=1;--i)
	{
		for (;cur&&f[st[u]+cur-1].dis+f[st[v]+i-1].len>upp;--cur);
		if (cur) tmp[++tot[1]][1]=data(f[st[v]+i-1].dis,f[st[u]+cur-1].len);
	}
	int ptr[2]={tot[0],tot[1]};
	for (;ptr[0]||ptr[1];)
	{
		data ret;
		if (!ptr[0]||(ptr[1]&&tmp[ptr[0]][0].dis>tmp[ptr[1]][1].dis||tmp[ptr[0]][0].dis==tmp[ptr[1]][1].dis&&tmp[ptr[0]][0].len>tmp[ptr[1]][1].len)) ret=tmp[ptr[1]--][1];
		else ret=tmp[ptr[0]--][0];
		if (!cnt[x]||f[st[x]+cnt[x]-1].len>ret.len) f[st[x]+(cnt[x]++)]=ret;
	}
	idx+=cnt[x];
}

bool judge(LL upp)
{
	idx=0;
	for (int i=1;i<=n;++i) st[i]=cnt[i]=0;
	dp(1,upp);
	return cnt[1];
}

LL binary_search()
{
	LL l=0,r=lim,ret=-1;
	for (LL mid;l<=r;)
	{
		mid=l+r>>1;
		if (judge(mid)) r=(ret=mid)-1;
		else l=mid+1;
	}
	return ret;
}

int main()
{
	//freopen("travel.in","r",stdin),freopen("travel.out","w",stdout);
	n=read();
	for (int i=2;i<=n;++i)
	{
		fa[i]=read(),lim+=a[i]=read();
		if (son[fa[i]][0]) son[fa[i]][1]=i;
		else son[fa[i]][0]=i;
	}
	printf("%lld\n",binary_search());
	//fclose(stdin),fclose(stdout);
	return 0;
}

Measured memory_bytes: 43776
# ABC 080 
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0 for i in range(n)]

N = int(input())
F = []
for i in range(N):
    F.append(getIntList())
#print(F)#
P = []
for i in range(N):
    P.append(getIntList())
#print(P)#

maxProf = -10**9
for n in range(1,2**10):
    a = zeros(10)
    m = n
    for i in range(10):
        a[i] = m % 2
        m //= 2
    prof = 0
    for i in range(N):
        c = 0
        for j in range(10):
            c += a[j]*F[i][j]
        prof += P[i][c]
    if prof>maxProf: maxProf = prof
print(maxProf)

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

int main() {

	int n;
	cin >> n;
	int p[n];
	for (int i=0; i<n; i++) cin >> p[i];
	
	int how = 0;
	for (int i=0; i<n; i++) {
		if (p[i] != i+1) how++;
	}
	
	if (how <= 2) {
		cout << "YES";
	}
	else {
		cout << "NO";
	}
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
	int n,i,max,min;
	int a,j,b;
	
	while(scanf("%d",&n)&&n!=0){
		max=0;
		min=500;
		for(i=0;i<n;i++){
			a=0;
			for(j=0;j<5;j++){
				scanf("%d",&b);
				a+=b;
			}
				a<max?(max=max):(max=a);
				min<a?(min=min):(min=a);
		}
		printf("%d %d\n",max,min);
	}
	return 0 ;
}

Measured memory_bytes: 600
#include<stdio.h>
#define N 100

int main(void){
	int n,s;
	int A[N];
	int i,j,k;
	int p;
	
	scanf("%d",&n);
	
	for(i =0;i<n;i++){
		scanf("%d",&s);
		A[i] = s;
	}

	
	for(i=0;i<n;i++){
		p = A[i];
		j = i-1;
		while(j >= 0 && p < A[j]){
			A[j+1] = A[j];
			j--;
			A[j+1] = p;
		}
		for(j=0;j<n;j++){
			printf("%d",A[j]);
			if(j == n-1)printf("\n");
			else printf(" ");
		}
	}
	
	
	return 0;
}

Measured memory_bytes: 1796
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int N;
    int A, B, C, D;
    string S;

    cin >> N >> A >> B >> C >> D;
    cin >> S;

    bool able;

    if (C < D)
    {
        able = true;

        for (int i = B; i < D - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                able = false;
            }
        }
        S[D - 1] = '#';

        for (int i = A; i < C - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                able = false;
            }
        }
    }
    else if (D < C)
    {
        /*
        //patern1

        bool able1 = true;

        for (int i = B; i < D - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                //注意
                able = false;
                break;
            }
        }

        S[D - 1] = '#';

        for (int i = A; i < C - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                able1 = false;
                break;
            }
        }

        //patern2

        bool able2 = true;
        S[D - 1] = '.';
        S[B - 1] = '#';

        for (int i = A; i < C - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                able2 = false;
                break;
            }
        }

        for (int i = B; i < D - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                able2 = false;
            }
        }

        //result
        if ((able1 == true) || (able2 == true))
        {
            able = true;
        }
        else
        {
            able = false;
        }
        */

        able = true;

        for (int i = A; i < C - 2; i++)
        {
            if ((S[i] == '#') && (S[i + 1] == '#'))
            {
                able = false;
                break;
            }
        }

        if (able == true)
        {
            able = false;
            for (int i = B - 2; i < D - 1; i++)
            {
                if ((S[i] == '.') && (S[i + 1] == '.') && (S[i + 2] == '.'))
                {
                    able = true;
                    //break;
                }
            }
        }
    }

    if (able == true)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }

    return 0;
}

Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <queue>
#define LL long long
#define N 55555
#define maxn 10005
#define INF 0x3f3f3f3f
#define mase(a,b) memset(a,b,sizeof(a))
#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)
#define DS DisputeSegement
using namespace std;
bool isPrime(int fig)
{
    int limit=(int)sqrt(fig)+1;
    for(int i=2;i<limit;i++)
    {
        if(fig%i==0)
        {
            return false;
        }

    }
    return true;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=11;i<=N;i+=10)
    {
        if(isPrime(i))
        {
            printf("%d",i);
            n--;
            printf("%c",n?' ':'\n');
            if(!n)
            {
                break;
            }
        }
    }
    return 0;
}


//Five,Five Everywhere,原创者CSDN@


Measured memory_bytes: 3780
# reference https://maspypy.com/atcoder-%e5%8f%82%e5%8a%a0%e6%84%9f%e6%83%b3-2019-02-09abc-154
from functools import lru_cache

N = int(input())
K = int(input())


@lru_cache(None)
def F(N, K):
    # print("NK", N, K)
    """N以下で0でないものがちょうどK個。0を含める"""
    # assert N >= 0
    if N < 10:
        if K == 0:
            return 1
        if K == 1:
            return N
        return 0  # 求める数字は作れない
    q, r = divmod(N, 10)
    # print("qr", q, r)
    ret = 0
    if K >= 1:  # 1 の位が nonzero (1 の位で 0 でない数字 を消費するので、第 2 引数は K - 1)
        ret += F(q, K-1) * r  # smaller than 1 の位
        ret += F(q-1, K-1) * (9-r)  # bigger than 1 の位
    ret += F(q, K)  # 1の位が zero (1 の位で 0 でない数字 を消費していないので、第 2 引数は K)
    return ret


print(F(N, K))


Measured memory_bytes: 39408
N=int(input())
A=list(map(int,input().split()))
# A = [int(i) for i in input().split()]
# for i in range(N):print(A[i],end='')
# print(end='\n')
for i in range(N):
    v=A[i]
    j=i-1
    while(j>=0 and A[j]>v):
        A[j+1]=A[j]
        j-=1
    A[j+1]=v
    for i in range(N):print(A[i],end=' \n'[i+1==N])
    # print(end='\n')


Measured memory_bytes: 5984