akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner;
import java.lang.Math;
public class Main {
	public static void main(String[] args){
		final Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[] p=new int[n+1];
		int[][] m=new int[n+1][n+1];
		for(int i=1;i<=n;i++){
			p[i-1]=sc.nextInt();
			p[i]=sc.nextInt();
		}
		for(int i=1;i<=n;i++)m[i][i]=0;
		for(int l=2;l<=n;l++){
			for(int i=1;i<=n-l+1;i++){
				int j=i+l-1;
				m[i][j]=(Integer.MAX_VALUE);
				for(int k=i;k<=j-1;k++){
					m[i][j]=Math.min(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
				}
			}
		}
		System.out.println(m[1][n]);
	}
	
	
}

Measured memory_bytes: 21284
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define USE_LLONG_AS_INT
#ifdef USE_LLONG_AS_INT
#define int long long
#define inf (1ll<<60)
#else
#define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

string s;
vector<string> r;

void dfs(string t,int i,int used){
    if(i==s.length()){
        r.pb(t);
        return;
    }
    char c=s[i];
    if((used>>(c-'a'))&1){
        dfs(t+(char)c,i+1,used);
    }
    if(c!='z'&&!((used>>(c+1-'a'))&1)){
        dfs(t+(char)(c+1),i+1,used|(1<<(c+1-'a')));
    }
}

void run(){
    cin>>s;
    if(s=="#")return;
    dfs("",0,1);
    cout<<r.size()<<endl;
    if(r.size()<=10){
        rep(i,r.size())cout<<r[i]<<endl;
    }
    else{
        rep(i,5)cout<<r[i]<<endl;
        Rep(i,r.size()-5,r.size())cout<<r[i]<<endl;
    }
    r.clear();
    run();
}


Measured memory_bytes: 4408
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define ms(a,x) memset(a,x,sizeof(a))


ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 


int main()
 {
  run;
 ll n;
 cin>>n;
 if(n%1000==0)
 {
     cout<<0<<endl;
 }
  else
  {
      ll ans=n/1000;
      ans++;
     cout<< 1000*ans-n;
  }
 }

Measured memory_bytes: 3576
#include<iostream>
using namespace std;
int main(){
  int N,M;
  cin>>N>>M;
  int C[M]={0};
  for(int i=0; i<N; i++){
    int K;
    cin >> K;
    for(int j=0; j<K; j++){
      int A;
      cin>>A;
      C[A-1]++;
    }
  }
  int count=0;
  for(int i=0; i<M; i++){
    if(C[i]==N) count++;
  }
  cout << count << endl;
  return 0;
}

Measured memory_bytes: 256
//#include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<numeric>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<ctype.h>
#include <fstream>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;
typedef long long ll;//int64
typedef unsigned long long ull;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a*b/gcd(a,b);}
int dy[4]={0,1,0,-1};
//----------program from here----------

int main(){
  int N=0,A=0;
  cin >> N >> A;
  cout << (N%500<=A?"Yes":"No") << endl;

  return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <tuple>
#include <iomanip>
#include <climits>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
//typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define revrep(i, n) for(ll i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);

//const ll INFL = LLONG_MAX;//10^18 = 2^60
const ll INFL = 1LL<<60;
//const int INF = INT_MAX;
const ll INF = 1 << 30;//10^9
ll MOD = 1000000007;
//ll MOD = 998244353;

vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};

void pres(double A, ll x = 20){cout << fixed << setprecision(x) << A << endl;}
void BinarySay(ll x, ll y = 60){rep(i, y) cout << (x>>(y-1-i) & 1); cout << endl;}
ll cnt_bit(ll x){return __builtin_popcountll(x);}

ll pow_long(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2) res *= x;
    x *= x; k /= 2;
  }
  return res;
}

ll pow_mod(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2){
      res *= x; res %= MOD;
    }
    x *= x; x %= MOD;
    k /= 2;
  }
  return res;
}

ll inverse(ll x){return pow_mod(x, MOD - 2);};

ll gcd(ll a, ll b){
    if(b == 0) return a;
    return gcd(b, a % b);
}

ll lcm(ll x, ll y){
  ll res = x / gcd(x, y);
  res *= y;
  return res;
};


/*
//コンビネーション
const int MAXcomb = 200010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
//facはn!,finvは1/n!
//invは逆元
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAXcomb; i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
        finv[i] = finv[i-1] * inv[i] % MOD;
    }
}
ll comb(int n, int k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * finv[k] % MOD * finv[n-k] % MOD;
}

const int MAXkai = 200010;
ll kai_memo[MAXkai];
ll kai(ll N){
  if(kai_memo[N] != 0) return kai_memo[N];
  if(N <= 1) return 1;
  return kai_memo[N] = N * kai(N-1) % MOD;
}
*/


void solve(){
  ll N;
  cin >> N;
  set<ll> A;
  rep(i, N){
    ll a;
    cin >> a;
    A.insert(a);
  }
  if(A.size() == N) cout << "YES" << endl;
  else cout << "NO" << endl;
}


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


Measured memory_bytes: 9600
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll,ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod=1e9+7;
const ll inf=1e16;

#define rep(i,m,n) for(ll i=m;i<n;i++)
#define repr(i,m,n) for(ll i=m-1;i>=n;i--)
#define fi first
#define se second
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(),x.end()
#define gll greater<ll>()

void solve(){
 vll x(5);
  rep(i,0,5) {
    cin >> x[i];
    if(x[i]==0) cout << i+1 << endl;
  }
  
  
}
int main(){
   cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Measured memory_bytes: 3568
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const ll inf=1e9+7;
const ll INF=1e18;

int main(){
    int n;cin>>n;
    vector<queue<int>> tennis(n);
    int a;
    rep(i,n)rep(j,n-1){
        cin>>a;
        tennis[i].push(a-1);
    }
    vector<queue<int>> match(2);
    rep(i,n)match[0].push(i);
    int ans=0,tmp,res=0;;bool can,all;
    while(1){
        vbo already(n,false);
        can=false;all=true;
        while(match[res].size()){
            int i=match[res].front();match[res].pop();
            if(tennis[i].size()!=0)all=false;
            else continue;
            if(!already[i]){
                tmp=tennis[i].front();
                if(i==tennis[tmp].front()&&!already[tmp]){
                    tennis[i].pop(); tennis[tmp].pop();
                    match[(res+1)%2].push(i);match[(res+1)%2].push(tmp);
                    already[i]=true;already[tmp]=true;
                    can=true;
                }
            }
        }
        if(all){
            cout<<ans<<endl;
            return 0;
        }
        if(!can){
            cout<<-1<<endl;
            return 0;
        }
        ans++;
        res=(res+1)%2;
    }
}

Measured memory_bytes: 7992
# Try AtCoder
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gf() gets.to_f end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
MV4 = [[0, 1], [1, 0], [0, -1], [-1, 0]]
MV8 = MV4 + [[1, 1], [1, -1], [-1, -1], [-1, 1]]
DV7 = 7 + 10 ** 9
############################################################
 
 

def f(s, sum)
    if s == ''
        sum
    else
        tmp = 0
        (1 .. s.length).each do |i|
            tmp += f(s[i .. -1], sum + s[0 ... i].to_i)
        end
        tmp
    end
end


p f(gs, 0)


Measured memory_bytes: 1788
#include<bits/stdc++.h>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> set_t;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define SUM(v) accumulate(v.begin(),v.end(),lli(0))
#define rev(n,d) for(int i=n;i>=d;i--)
#define FOR0(n) for(int i=0;i<n;i++)
#define FOR(a,n) for(int i=a;i<n;i++)
#define FORQ(a,n) for(int i=a;i<=n;i++)
#define inf 1000000009
#define inff 0x3f3f3f3f
#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define endl "\n"
#define mod 1000000007
#define mem(ar,x) memset(ar,x,sizeof ar)
#define present(container, element) (container.find(element) != container.end())
#define max3(a,b,c) return (lli(a)>lli(b)?(lli(a)>lli(c)?a:c):(lli(b)>lli(c)?b:c))
#define max4(a,b,c,d) return (max3(a,b,c)>lli(d)?(max3(a,b,c)):d)
#define min3(a,b,c) return (lli(a)<lli(b)?(lli(a)<lli(c)?a:c):(lli(b)<lli(c)?b:c))
#define min4(a,b,c,d) return (max3(a,b,c)<lli(d)?(max3(a,b,c)):d)
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<int,pii>ppii;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef vector<pii>vpii;
const int N=100005;
int dr8[]={0,0,1,-1,1,1,-1,-1};
int dc8[]={1,-1,0,0,-1,1,-1,1};
int dr4[]={0,1,-1,0};
int dc4[]={1,0,0,-1};
int main(){
	fast
	int n,m;
	cin>>n>>m;
	string s[n+1];
	FOR0(n){
		cin>>s[i];
	}
	lli dp[n+1][m+1];
	mem(dp,0);
	dp[0][0]=1;
	for(int i=0;i<=n;i++){
		for(int j=0;j<m;j++){
			if(s[i][j]=='#') continue;
			if(i==0&&j==0) continue;
			if(i==0){
				dp[i][j]=dp[i][j-1];
			}
			else if(j==0){
				dp[i][j]=dp[i-1][j];
			}
			else{
				dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
			}
		}
	}
	cout<<dp[n-1][m-1];
}



Measured memory_bytes: 9088
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 1000000
typedef long long ll;


int main(){
  int n,m;
  cin>>n>>m;
  vector<pair<ll,ll>> num[MAX];
  ll countP[MAX]={0};
  ll p[MAX];
  ll y[MAX];
  for(int i=1;i<=m;i++){
    cin>>p[i]>>y[i];
    countP[p[i]]++;
    num[p[i]].push_back(make_pair(y[i],i));
  }
  for(int i=1;i<MAX;i++){
    sort(num[i].begin(),num[i].end());
  }
  ll order[MAX];
  for(int i=0;i<MAX;i++){
    for(int j=0;j<countP[i];j++){
      order[num[i][j].second]=j+1;
    }
  }
  for(int i=1;i<=m;i++){
    printf("%06lld%06lld\n",p[i],order[i]);
  }



}


Measured memory_bytes: 44928
#if defined(__GNUC__)
#include <bits/stdc++.h>
// __GNUC__
#else
#include "bits/stdc++.h"
#endif
using namespace std;

#define int long long

#if defined(__GNUC__)
#define __int32 __int32_t
#endif // __GNUC__

#define itn __int32
#define REP(i, n) for(itn (i) = 0; (i) < (n); ++(i))
#define REP2(i, x, n) for(itn (i) = x; (i) < (n); ++(i))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SORT(x) std::sort(ALL(x))
#define RSORT(x, T) std::sort(ALL(x), greater<T>())
#define REVERCE(x) std::reverce(ALL(x))

#define INT_POW(x, y) [](int a, int b) -> int {int c = a; REP(i, b - 1){c *= a;} return c;}(x, y)
#define INT_POWM(x, y, m) [](int a, int b, int m) -> int {int c = a; REP(i, b - 1){c *= a; c %= m;} return c;}(x, y, m)

struct md {
	int y;
	int x;
};

itn main() {
	std::ios::sync_with_stdio(false);

	itn a, n;

	cin >> n >> a;

	if (n % 500 <= a) {
		cout << "Yes";
	}
	else {
		cout << "No";
	}

	return 0;
}

Measured memory_bytes: 256
/*
      author  : nishi5451
      created : 15.08.2020 14:17:41
*/

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


int main(){
    vector<vector<int>> v(4,vector<int>(4));
    vector<vector<bool>> bin(4,vector<bool>(4,false));
    for(int i=1; i<=3; i++) 
        for(int j=1; j<=3; j++) 
            cin >> v[i][j];
    int n; cin >> n;
    for(int i=1; i<=n; i++){
        int b;
        cin >> b;
        for(int j=1; j<=3; j++) 
            for(int k=1; k<=3; k++) 
                if(v[j][k]==b)
                    bin[j][k]=true;
    }
   
    bool ans = false;
    for(int i=1; i<=3; i++){
        bool bingo = true;
        for(int j=1; j<=3; j++){
            if(bin[i][j] == false) bingo = false;
        }
        if(bingo==true) ans = true;
    }
    for(int i=1; i<=3; i++){
        bool bingo = true;
        for(int j=1; j<=3; j++){
            if(bin[j][i] == false) bingo = false;
        }
        if(bingo==true) ans = true;
    }
    
    if(bin[1][1]&&bin[2][2]&&bin[3][3]) ans = true;
    if(bin[1][3]&&bin[2][2]&&bin[3][1]) ans = true;
    
    if(ans) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 3616
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char c = sc.next().charAt(0);

        if(Character.isUpperCase(c)){
            System.out.println("A");
        }else{
            System.out.println("a");
        }
    }
}


Measured memory_bytes: 35788
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
long long n;
std::vector<long long> ct(4);
string stmp;
void solve(){
    cout<<"AC x "<<ct.at(0)<<endl;
    cout<<"WA x "<<ct.at(1)<<endl;
    cout<<"TLE x "<<ct.at(2)<<endl;
    cout<<"RE x "<<ct.at(3)<<endl;

}

int main(){
    cin>>n;
    rep(i,n){
        cin>>stmp;
        if(stmp=="AC") {
            ct.at(0)++;
        }else if(stmp=="WA") {
            ct.at(1)++;
        }else if(stmp=="TLE") {
            ct.at(2)++;
        }else{
            ct.at(3)++;
        }

    }
    solve();
    return 0;
}


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

int main(){
	int a[3],f_count,s_count;
  	f_count = 0;s_count = 0;
  	for(int i = 0;i < 3;i++){
		cin >> a[i];
      	if(a[i] == 5) f_count++;
      	if(a[i] == 7) s_count++;
    }
  
  	if(f_count == 2 && s_count == 1) cout << "YES" << endl;
  	else cout << "NO" << endl;
  
  	return 0;
  
  
  
}

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

int main()
{
  int n;
  cin >> n;
  double ans = 0;
  for (int i = 0; i < n; i ++) {
    double d;
    cin >> d;
    ans += 1.0 / d;
  }
  cout << 1.0 / ans << endl;
}


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

using namespace std;
const int64_t c = 1000000000 + 7;

int main() {
    int n;
    cin >> n;
    vector<int64_t> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int64_t ans = 0;
    int64_t t = 1;
    for (int j = 0; j < 62; j++) {
        int64_t ones = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] & 1) {
                ones++;
            }
            a[i] >>= 1;
        }

        int64_t tmp = ((n - ones) * ones) % c;
        ans += (tmp * (t % c)) % c;
        ans %= c;
        t <<= 1;
    }
    cout << ans << endl;
}


Measured memory_bytes: 2560
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;

int main(){
  int n,k,q,a;
  cin >> n >> k >> q;
  vector<int> v(n,k-q);
  rep(i,q){
    cin >> a;
    v.at(a-1) ++;
  }
  rep(i,n){
    if(v.at(i)>0)cout << "Yes" << endl;
    else cout << "No" << endl;
  }
}

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

namespace AtCoder.Abc
{
    class A_167_Registration
    {
        public static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            // 文字列の入力
            string s = Console.ReadLine();
            string r = Console.ReadLine();
            
            if (Regex.IsMatch(r, s + ".") && s.Length == r.Length - 1)
            {
                string result = "Yes";
                Console.WriteLine(result);
            }
            else
            {
                string result = "No";
                Console.WriteLine(result);
            }

            Console.Out.Flush();
        }
    }
}


Measured memory_bytes: 26736
#include <iostream>
#include <cstdio>
#define N 505
using namespace std;

int H, W, h, w, a[N][N];
int main()
{
	int i, j;
	cin >> H >> W >> h >> w;
	if (H % h == 0 && W % w == 0) {cout << "No"; return 0;}
	cout << "Yes" << endl;
	if (H % h) {
		for (i = 0; i < H; i++) {
			for (j = 0; j < W; j++) a[i][j] = i % h ? -1000 : 1000 * h - 1001;
		}
	} else {
		for (i = 0; i < W; i++) {
			for (j = 0; j < H; j++) a[j][i] = i % w ? -1000 : 1000 * w - 1001;
		}
	}
	for (i = 0; i < H; i++, puts("")) for (j = 0; j < W; j++) printf("%d ", a[i][j]);
    return 0;
}

Measured memory_bytes: 2688
N, M = map(int, input().split())
AB = []
for i in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))

AB = sorted(AB)
cost = 0
tobuy = M
for ab in AB:
    if ab[1] < tobuy:
        cost += ab[0] * ab[1]
        tobuy = tobuy - ab[1]
    else:
        cost += ab[0] * tobuy
        tobuy = 0

    if tobuy == 0:
        break

print(cost)

Measured memory_bytes: 17788
/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.0.2"
+/

import std.algorithm, std.range, std.stdio, std.math, std.conv, core.bitop;

// import dcomp.scanner;
// import dcomp.algorithm;

int n;
struct Edge {
    int to;
    long dist;
}
Edge[][] g;
long md;

T[N] toS(T, int N)(T[N] data){ return data; }

long[2][] dfs(int p) {
    if (g[p].empty) {
        return [[0L, 0L]];
    }
    Edge le = g[p][0], re = g[p][1];
    auto lt = dfs(le.to);
    auto rt = dfs(re.to);
    if (lt.length > rt.length) {
        swap(le, re);
        swap(lt, rt);
    }
    long ld = le.dist, rd = re.dist;
    auto buf = Appender!(long[2][])();
    foreach (co; lt) {
        //co[1] + ld + rd + rco[0] <= md
        int idx = binSearch!((i){return co[1]+ld+rd+rt[i][0] > md;})(-1, rt.length.to!int)-1;
        if (idx == -1) continue;
        buf.put([co[0]+ld, rt[idx][1]+rd].toS);
        buf.put([rt[idx][1]+rd, co[0]+ld].toS);
    }
    auto ans = buf.data;
    sort(ans);
    if (ans.empty) return ans;
    long ma = 10L^^15;
    int co = 0;
    foreach (ref e; ans) {
        if (ma <= e[1]) {
            continue;
        }
        ma = min(ma, e[1]);
        ans[co] = e;
        co++;
    }

    return ans[0..co];
}

//md以下にできるか？
bool solve(long _md) {
    md = _md;
    return !dfs(0).empty;
}

int main() {
    auto sc = new Scanner();
    int n;
    sc.read(n);
    g = new Edge[][](n);
    foreach (i; 1..n) {
        int a; long b;
        sc.read(a, b); a--;
        g[a] ~= Edge(i, b);
    }

    writeln(binSearch!solve(-1L, 10L^^12));
    return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

class Scanner {
    import std.stdio : File, readln, stdin;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeString, isDynamicArray;
    import std.algorithm : map;
    File f;
    this(File f = stdin) {
        this.f = f;
    }
    string[] buf;
    bool succ() {
        while (!buf.length) {
            if (f.eof) return false;
            buf = readln.split;
        }
        return true;
    }
    int read(Args...)(auto ref Args args) {
        foreach (i, ref v; args) {
            if (!succ()) return i;
            alias VT = typeof(v);
            static if (!isSomeString!VT && isDynamicArray!VT) {
                v = buf.map!(to!(ElementType!VT)).array;
                buf.length = 0;
            } else {
                v = buf.front.to!VT;
                buf.popFront();
            }
        }
        return args.length;
    }
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;

//[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定
T binSearch(alias pred, T)(T l, T r) {
    while (r-l > 1) {
        T md = (l+r)/2;
        if (!pred(md)) l = md;
        else r = md;
    }
    return r;
}


Measured memory_bytes: 36732
#include<stdio.h>
int main(){
	int a,b;
	scanf("%d %d",&a,&b);
	if(a<b){
		printf("a < b\n");
	} else if (a>b){
		printf("a > b\n");
	} else {
		printf("a == b\n");
	}
return 0;

}





Measured memory_bytes: 2052
//package AtCoder;
import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong();
		long x=sc.nextLong();
		long ans=3*(n-gcd(n,x));
		System.out.println(ans);
	}
	static long gcd(long a,long b)
	{
		if(b==0)
			return a;
		return gcd(b,a%b);
	}

}

Measured memory_bytes: 23892
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PI 3.141592653589793238462643383279502884197169399375105820974

int main(){
    double r;
    cin>>r;
    double ans = 2*r*PI;
    cout<<setprecision(16)<<ans<<"\n";
    return 0;
}


Measured memory_bytes: 3784
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 2e5+5;
int n, par[N], sz[N];
vector<pair<int, int>> x, y;
int find(int x)
{
	return (par[x] == x? x : par[x] = find(par[x]));
}
void Union(int x, int y)
{
	if(sz[x] < sz[y]) swap(x, y);
	sz[x] += sz[y];
	par[y] = x;
}
int main()
{
	iota(par, par+N, 0);
	fill(sz, sz+N, 1);
	cin >> n;
	for(int i=0, a, b; i<n; i++)
	{
		cin >> a >> b;
		x.push_back({a, i});
		y.push_back({b, i});
	}
	sort(x.begin(), x.end());
	sort(y.begin(), y.end());
	priority_queue<pair<int, pair<int, int>>> q;
	for(int i=0; i<n-1; i++) q.push({x[i].f-x[i+1].f, {x[i].s, x[i+1].s}}),
	q.push({y[i].f-y[i+1].f, {y[i].s, y[i+1].s}});
	int ans = 0;
	while(!q.empty())
	{
		auto t = q.top();
		int cost = -t.f;
		int u = find(t.s.f), v = find(t.s.s);
		q.pop();
		if(u == v) continue;
		Union(u, v);
		ans += cost;
	}
	cout << ans << endl;	
		
}

Measured memory_bytes: 8680
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;

int main() {
  string T; cin >> T;
  for (int i = 0; i < T.size(); ++i) {
    if (T[i] == '?') T[i] = 'D';
  }
  cout << T << endl;
}


Measured memory_bytes: 3692
#include <bits/stdc++.h>
using namespace std;
int N, M; 
int A[55], B[55]; 
vector<int> E[55]; 
int C[55][55]; // 辺と番号の対応
long long used[25]; // 各パスが各辺を使うか
int main(){
	cin >> N; 
	for(int i = 0; i < N; i++){
		for(int j = 0; j < N; j++){
			C[i][j] = -1; 
		}
	}
	for(int i = 0; i < N-1; i++){
		int a, b; cin >> a >> b; a--; b--; 
		E[a].push_back(b); 
		E[b].push_back(a); 
		C[a][b] = i; C[b][a] = i; 
	}
	cin >> M; 
	queue<int> que; 
	for(int i = 0; i < M; i++){
		int u, v; cin >> u >> v; 
		u--; v--; 
		int D[55] = {}; 
		fill(D, D+N, 100); 
		D[u] = 0; 
		que.push(u); 
		while(que.size()){
			int p = que.front(); que.pop(); 
			for(int i = 0; i < E[p].size(); i++){
				if(D[E[p].at(i)] == 100){
					D[E[p].at(i)] = D[p] + 1; 
					que.push(E[p].at(i)); 
				}
			}
		}
		int now = v; 
		for(int t = D[v]; t > 0; t--){
			for(int j = 0; j < E[now].size(); j++){
				if(D[E[now].at(j)] == t-1){
				    int neww = E[now].at(j); 
					used[i] += (1LL << C[now][neww]); 
					now = neww; 
					break; 
				}
			}
		}
	}
	long long ans = 0; 
	for(int i = 0; i < (1 << M); i++){
		long long now = 0; 
		int cnt = 0; 
		for(int j = 0; j < M; j++){
			if((i >> j) & 1){
				cnt ++; 
				now |= used[j]; 
			}
		}
		int white = N-1; 
		for(int j = 0; j < N-1; j++){
			if((now >> j) & 1) white --; 
		}
		if(cnt % 2) ans -= (1LL << white);
		else ans += (1LL << white); 
	}
	cout << ans << endl; 
}

Measured memory_bytes: 384
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = 10**10
mod = 10 ** 9 + 7

class Dinic:
    def __init__(self, v, inf=10**10):
        self.v = v
        self.inf = inf
        self.G = [[] for _ in range(v)]
        self.level = [-1]*v  # 深さ
        self.ite = [0]*v  # DFSでの探索が済んでいるか
    def add_edge(self, fr, to, cap):
        self.G[fr].append([to, cap, len(self.G[to])])
        self.G[to].append([fr, 0, len(self.G[fr])-1])
    def bfs(self, s):  # BFSで深さ決定,sがstart
        self.level = [-1]*self.v  # 必要
        self.level[s] = 0
        Q = deque()
        Q.append(s)
        while Q:
            v = Q.popleft()
            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if e[1]>0 and self.level[e[0]]<0: ###capacity>0かつtoの深さ未定
                    self.level[e[0]] = self.level[v]+1
                    Q.append(e[0])
    def dfs(self, v, t, f):  # DFSで増加パス探索,v開始、t終点、総フローf
        if v==t:
            return f
        for i in range(self.ite[v], len(self.G[v])):
            self.ite[v] = i
            e = self.G[v][i]
            if e[1]>0 and self.level[v]<self.level[e[0]]:
                d = self.dfs(e[0], t, min(f, e[1]))
                if d>0:
                    e[1] -= d  # cap減少
                    self.G[e[0]][e[2]][1] += d  # 逆辺のcap増加
                    return d
        return 0
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.level[t]<0:
                return flow
            self.ite = [0]*self.v  # DFSでの探索が済んでいるか否か
            f = self.dfs(s,t,self.inf)
            while f>0:
                flow += f
                f = self.dfs(s,t,self.inf)

def ctoi(c):
    return ord(c) - ord('a')

N = INT()
S = [input() for _ in range(N)]

ans = []
for i in range(len(ascii_lowercase)):
    D = Dinic(len(ascii_lowercase)+2)
    s = 26
    t = s+1

    deg_in = 0
    deg_out = 0

    for x in S:
        u = ctoi(x[0])
        v = ctoi(x[-1])
        if u == i:
            u = s
            deg_out += 1
        if v == i:
            v = t
            deg_in += 1

        D.add_edge(u, v, 1)

    f = D.max_flow(s, t)
    if f >= deg_out and deg_in > 0:
        ans.append(ascii_lowercase[i])

print(*ans, sep="\n")



Measured memory_bytes: 11052
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repr(i, a, b) for(ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b){ return b?GCD(b, a % b):a; }
inline ll LCM(ll a, ll b){ return a/GCD(a, b)*b; }
inline ll powint(unsigned long long x, ll y){ ll r=1; while(y){ if(y&1) r*=x; x*=x; y>>=1; } return r; }
inline ll powmod(ll x, ll y, ll m = MOD){ ll r=1; while(y){ if(y&1) r*=x; x*=x; r%=m; x%=m; y>>=1; } return r; }
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; }
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    ll h, w, n;
    cin >> h >> w >> n;
    vll a(n), b(n);
    vll ans(10, 0);
    ans[0] = (h-2)*(w-2);
    set<pll> p;
    rep(i, n){
        cin >> a[i] >> b[i];
        p.emplace(a[i], b[i]);
        repr(xi, max(a[i]-2, 1ll), min(a[i], h-2)+1){
            repr(yi, max(b[i]-2, 1ll), min(b[i], w-2)+1){
                ll cnt = 0;
                repr(x, xi, xi+3)repr(y, yi, yi+3){
                    if(p.find(pll(x, y)) != p.end()){
                        cnt++;
                    }
                }
                ans[cnt-1]--;
                ans[cnt]++;
            }
        }
    }
    /*
    sort(ALL(p));
    rep(i, n){
        repr(xi, max(p[i].first-2, 1ll), min(p[i].first, h-2)+1){
            repr(yi, max(p[i].second-2, 1ll), min(p[i].second, w-2)+1){
                ll cnt = 0;
                repr(x, xi, xi+3)repr(y, yi, yi+3){
                    if(binary_search(ALL(p), pll(x, y))){
                        cnt++;
                    }
                }
                ans[cnt-1]--;
                ans[cnt]++;
            }
        }
    }
     */
    rep(i, 10){
        cout << ans[i] << endl;
    }
    return 0;
}

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

#define N 100
int A[N][N];
int u,k,v;

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

	for(i=0;i<n;i++){
		cin >> u >> k;
		for(j=0;j<k;j++){
			cin >> v;
			A[u-1][v-1] = 1;
		}
	}

	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(j) cout << " ";
			cout << A[i][j];
		}
		cout << endl;
	}




return 0;

}


Measured memory_bytes: 3140
a,b,c = gets.chomp.split(' ').map(&:to_i)
i = 0
result = 'YES'
numbers = []
loop do
  num = (a * i) % b
  break if num == c
  if numbers.include?(num)
    result = 'NO'
    break
  end
  numbers << num
  i += 1
end
print result


Measured memory_bytes: 1788
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
	int x,r=0, res=0;
	cin >> x;
	
	if(x<500){
		res=(x/5)*5;
		cout<<res;
	}else{
		r=x%500;
		res=(x/500)*1000;
		res+=(r/5)*5;
		cout << res;
	}
		
	
	return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main(int argc, char **argv)
{
    int X, Y;
    cin >> X >> Y;
    // a: (i+1, j+2), b: (i+2, j+1)
    // a + 2b = X, 2a + b = Y
    int a = (2 * Y - X) / 3;
    int b = (2 * X - Y) / 3;
    if (a + 2 * b != X || 2 * a + b != Y) {
        cout << 0 << endl;
    }
    else if (a < 0 || b < 0) {
        cout << 0 << endl;
    }
    else if (a == 0 || b == 0) {
        cout << 1 << endl;
    }
    else {
        // calculate (a+b) C (a) mod (10^9 +7)
        int s = a + b;
        //ll fac[s + 1], finv[s + 1], inv[s + 1];
        ll *fac, *finv, *inv;
        fac = new ll[s+1];
        finv = new ll[s+1];
        inv = new ll[s+1];
        fac[0] = 1; fac[1] = 1;
        finv[0] = 1; finv[1] = 1;
        inv[1] = 1;
        for (ll i = 2; i <= s; ++i) {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
        cout << (fac[s] * (finv[a] * finv[b] % MOD) % MOD) << endl;
        delete[] fac;
        delete[] finv;
        delete[] inv;
    }
    return 0;
}


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

int main(){
    long long int n,a[200000],ans,count=0,first=0;
    long long int second=0,first_b=0,second_b=0;
    //int a[3];
    scanf("%lld",&n);
    long long int i;
    for (i = 0; i<n; i++){
        scanf("%lld",&a[i]);
        count = count + a[i];
    }
    i=0;
    while (count/2 > first) {
        first = a[i]+first;
        i++;
    }
    second = count - first;
    
    long long int i_b=n-1;
    while (count/2 > second_b) {
        second_b = a[i_b]+second_b;
        i_b--;
    }
    first_b = count - second_b;
    
    if (llabs(second-first)>llabs(second_b-first_b)){
        ans=llabs(second_b-first_b);
    }else{
        ans=llabs(second-first);
    }
    
    printf("%lld",ans);
    return 0;
    
}


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

void max_self(ll &a,ll b)
{
    a = max(a,b);
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,wt;
    cin>>n>>wt;

    vector<ll> v(n),w(n),dp(wt+1,0);

    for(int i=0 ; i<n ; i++)
        cin>>w[i]>>v[i];

    for(int i=0 ; i<n ; i++)
    {
        for(int j=wt-w[i] , k=wt ; j>=0 ; j-- , k--)
        {

            dp[k]=max(dp[k],dp[j]+v[i]);

            //for(auto i:dp)   cout<<i<<" ";            cout<<endl;
        }
    }
    cout<<dp[wt];
/*
    for(int i=0 ; i<n ; i++)
    {
        for(int j=wt - w[i] ; j>=0 ; j--)
        {
            dp[j+w[i]]  = max(dp[j+w[i]] ,dp[j] + v[i]);
            printf("i=%d j=%d  ",i,j);

        }
    }
*/


}


Measured memory_bytes: 1024
#include<iostream>
#include <string>
#include<vector>
typedef long long ll;

using namespace std;
int main()
{
	int n, k;
	cin >> n >> k;
	ll kotae=k;
	for (int i = 0; i < n - 1; i++) {
		kotae *= (k - 1);
	}
	cout << kotae;
	return 0;
}


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

typedef long long LL;

template<typename T>
void printmatrix(vector<vector<T> > ar){
    for (int i = 0; i < (int)ar.size(); i++) {
        for (int j = 0; j < (int)ar.size(); j++) {
            cout << ar[i][j];
        }
        cout << endl;
    }
}

template<typename T>
void printvector(vector<T> vec){
    for (int i = 0; i < (int)vec.size(); i++) {
        cout << vec[i] << endl;
    }
}

template<typename T>
void unique(vector<T>& vec){
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

template<typename T>
vector<T> cumsum(vector<T> vec){
    vector<T> newvec((T)vec.size());
    int tmp = 0;
    for (int i = 0; i < (int)vec.size(); i++) {
        tmp += vec[i];
        newvec[i] = tmp;
    }

    return newvec;
}


int main(int argc, char const* argv[])
{
    int x,a,b;
    cin >> x >> a >> b;
    cout << (x-a) % b << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;
 
#define REP(a,b,c) for(ll a=b;a<(c);a++)
#define PER(a,b,c) for(ll a=b;a>=(c);a--)
 
inline ll ii(){ ll x; cin >> x; return x; }
inline string is(){ string x; cin >> x; return x; }
inline ld id(){ ld x; cin >> x; return x; }
inline void oi(ll x){ cout << x; }
inline void od(ld x){ cout << fixed << setprecision(10) << x; }
inline void os(string x){ cout << x; }
inline void oe(){ cout << endl; }
inline void oie(ll x){ oi(x); oe(); }
inline void ode(ld x){ od(x); oe(); }
inline void ose(string x){ os(x); oe(); }
inline void maxin(ll &a, ll b){ a=max(a,b); }

int main(){
    string N=is();
    ll x=0;
    for(char c : N){
        x+=c-'0';
    }
    if(x%9==0){
        ose("Yes");
    }else{
        ose("No");
    }
    return 0;
}

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

int r, c, n;

struct P{
	int x, y, id;
	P(){}
	P(int a, int b, int i):x(a), y(b), id(i) {}
	bool operator ==(P const &o) { return id == o.id; }
	int sc()const {
		if (x == 0)return 0;
		if (y == c)return 1;
		if (x == r)return 2;
		return 3;
	}
	bool operator <(P const &o)const {
		int a = sc(), b = o.sc();
		if (a == b){
			if (a == 0)return y < o.y;
			else if (a == 1)return x < o.x;
			else if (a == 2)return y > o.y;
			return x > o.x;
		}
		return a < b;
	}
};

int main(){
	scanf("%d%d%d", &r, &c, &n);
	vector<P> v;
	f(i, 0, n){
		int a, b, cc, d;
		scanf("%d%d%d%d", &a, &b, &cc, &d);
		bool e1 = false, e2 = false;
		if (a == 0 || a == r || b == 0 || b == c)e1 = true;
		if (cc == 0 || cc == r || d == 0 || d == c)e2 = true;
		if (e1 && e2){
			v.push_back(P(a, b, i));
			v.push_back(P(cc, d, i));
		}
	}
	sort(v.begin(), v.end());
	stack<P> st;
	f(i, 0, v.size()){
		if (!st.empty() && st.top() == v[i])st.pop();
		else st.push(v[i]);
	}
	printf("%s\n", st.empty() ? "YES" : "NO");
}

Measured memory_bytes: 4852
N,M=map(int,input().split())
listH=list(map(int,input().split()))
listN=[0]*N
for i in range(M):
  A,B=map(int,input().split())
  if listH[A-1]>listH[B-1]:
    listN[B-1]=-1
  if listH[A-1]<listH[B-1]:
    listN[A-1]=-1
  if listH[A-1]==listH[B-1]:
    listN[A-1]=-1
    listN[B-1]=-1   
ans=0    
for i in listN:
  if i>=0:
    ans+=1
print(ans)

Measured memory_bytes: 20044
#include<stdio.h>
int main()
{
  char s[3]={};
  int i,k;
  scanf("%s",s);
  if(s[0]=='R'&&s[1]=='R'&&s[2]=='R'){
    k=3;}
  else if(s[0]=='R'&&s[1]=='R'&&s[2]=='S'||s[0]=='S'&&s[1]=='R'&&s[2]=='R'){
    k=2;}
  else if(s[0]=='S'&&s[1]=='S'&&s[2]=='S'){
    k=0;}
  else{
    k=1;}
  printf("%d",k);
  return 0;
}

Measured memory_bytes: 1724
n,m = map(int,input().split())
dic = {i+1:set() for i in range(n)}
for i in range(m):
  a,b = map(int,input().split())
  dic[a].add(b)
  dic[b].add(a)
        
ans = 'IMPOSSIBLE'
for i in dic[1]:   
  if n in dic[i]:
    ans = 'POSSIBLE'
    break
    
print(ans)

Measured memory_bytes: 89188
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <stdio.h>
#include <bitset>
#include <limits.h>
#include <complex>
#include <deque>
#include <iomanip>
#include <list>
#include <cstring>
using namespace std;
#define pi pair<int,int>
#define pl pair<long long,long long>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define en cout << endl //セミコロンつけろ
//#define MM 1000000000
//#define MOD MM+7
typedef long long ll;
const int MM = 1e9;
const int MOD = MM+7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
template<typename T>
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
template<typename T>
T LCM(T x,T y){
    T gc = GCD(x,y);
    return x*y/gc;
}
struct edge {
    ll to,cost;
};
/*
const int COM_MAX = 500500;
long long fac[COM_MAX],finv[COM_MAX],inv[COM_MAX];
void init(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < COM_MAX; i++){
        fac[i] = fac[i-1]*i%MOD;
        inv[i] = MOD - inv[MOD%i]*(MOD/i)%MOD;
        finv[i] = finv[i-1]*inv[i]%MOD;
    }
}
long long COM(int n,int k){
    if(n < k || n < 0 || k < 0) return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
*/

int main(){
    int N; cin >> N;
    vector<int> b(N-1);
    for (int i = 0; i < N-1; i++){
        cin >> b[i];
    }
    int ans = b[0];
    for (int i = 1; i < N-1; i++){
        ans += min(b[i-1],b[i]);
    }
    ans += b[N-2];
    cout << ans << endl;
}

Measured memory_bytes: 256
import numpy as np

if __name__ == '__main__':
    N = int(input())
    S = input()
    str_l = np.array(list(S))
    direction_E = (str_l == 'E').astype(int)
    direction_W = (str_l == 'W').astype(int)
    direction = direction_W - direction_E
    min = sum = np.sum(direction_E)
    for i in range(N):
        sum += direction[i]
        if sum < min:
            min = sum
    print(min)


Measured memory_bytes: 21260
#include <stdio.h>
#define N 25
#define NIL -1

typedef struct{
  int p,l,r;
} Node_t;

Node_t T[N];
int n,D[N],H[N];

void setDep(int u, int d){ //??±?????\???
  if(u==NIL) return;
  D[u]=d;
  setDep(T[u].l,d+1);
  setDep(T[u].r,d+1);
}

int setHe(int u){ //????????\???
  int h1=0,h2=0;
  if(T[u].l!=NIL) h1=setHe(T[u].l)+1;
  if(T[u].r!=NIL) h2=setHe(T[u].r)+1;
  if(h1>h2) H[u]=h1;
  else H[u]=h2;
  return H[u];
}

int getSib(int u){ //??????????????±?????????
  if(T[u].p==NIL) return NIL;
  if(T[T[u].p].l!=u&&T[T[u].p].l!=NIL) return T[T[u].p].l;
  if(T[T[u].p].r!=u&&T[T[u].p].r!=NIL) return T[T[u].p].r;
  return NIL;
}

void output(int u){
  int zisu=0;
  printf("node %d: parent = %d, sibling = %d, ",u,T[u].p,getSib(u));
  if(T[u].l!=NIL) zisu++;
  if(T[u].r!=NIL) zisu++;
  printf("degree = %d, depth = %d, height = %d, ",zisu,D[u],H[u]);
  if(T[u].p==NIL) printf("root\n");
  else if(T[u].l==NIL&&T[u].r==NIL) printf("leaf\n"); //???????????????
  else printf("internal node\n");
}

int main(){
  int id,l,r,root=0,i;
  scanf("%d",&n);
  for(i=0;i<n;i++) T[i].p=NIL;
  for(i=0;i<n;i++){
    scanf("%d%d%d", &id,&l,&r);
    T[id].l=l;
    T[id].r=r;
    if(l!=NIL) T[l].p=id;
    if(r!=NIL) T[r].p=id;
  }

for(i=0;i<n;i++) if(T[i].p==NIL) root=i;

setDep(root, 0);
setHe(root);

for(i=0;i<n;i++) output(i);

return 0;
}

Measured memory_bytes: 612
a, b = (int (i) for i in input().split())
kotae = 0
for i in range(a, b+1):
    s = i % 10
    t = i /10000 % 10
    u = i / 10 % 10
    v = i / 1000 % 10
    if (int(s)==int(t) and int(u)==int(v)) :
        kotae+=1

print(kotae)

Measured memory_bytes: 3316
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define maxn 1000000007
#define PI 3.14159265358979323846
#define MAX 1000007 
using namespace std; 

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 
    //cout<<fixed<<setprecision(20);
    ll n,k;
    cin>>n>>k;
    ll sum=0;
    for (int i = 1; i <=n; ++i)
    {
        sum+=k+i-1;
    }
    //cout<<sum<<endl;
    ll sum1=LONG_LONG_MAX;
    ll index=-1;
    for (int i = 1; i <= n; ++i)
    {
        if(sum1>=abs(k+i-1))
        {
            index=i;
        }
        sum1=min(sum1,abs(k+i-1));
    }
    //cout<<sum1<<endl;
    cout<<sum-(k+index-1)<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define inf 1000000009
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main() {
    fastio;
	int n,d,c=0;
	float s,k;
	cin>>n>>d;
	int a[n][d];
	for(int i=0;i<n;i++){
	    for(int j=0;j<d;j++) cin>>a[i][j];
	}
	for(int p=0;p<n-1;p++){
	    for(int i=p+1;i<n;i++){
	        s=0;
	        for(int j=0;j<d;j++){
	            s += (a[p][j]-a[i][j])*(a[p][j]-a[i][j]);
	        }
	        k = sqrt(s);
	        if(k==int(k)) c++;
    	}
	}
	cout<<c<<"\n";
	return 0;
}

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

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

typedef struct item{
  int w,s,v;
} node;

int cmp(const void *a,const void *b){
  node *p=(node *)a;
  node *q=(node *)b;
  return (p->s+p->w)-(q->s+q->w);
}

void run(void){
  int n;
  scanf("%d",&n);
  node *p=(node *)calloc(n,sizeof(node));
  int i,j;
  for(i=0;i<n;i++){
    int w,s,v;
    scanf("%d%d%d",&w,&s,&v);
    p[i]=(node){w,s,v};
  }
  qsort(p,n,sizeof(node),cmp);
  const int m=20000;
  int64 *dp=(int64 *)calloc(m+1,sizeof(int64));
  for(i=0;i<n;i++){
    for(j=p[i].s;j>=0;j--){
      int k=j+p[i].w;
      dp[k]=MAX(dp[k],dp[j]+p[i].v);
    }
  }
  int64 ans=0;
  for(i=0;i<=m;i++) ans=MAX(ans,dp[i]);
  printf("%lld\n",ans);
}

int main(void){
  run();
  return 0;
}


Measured memory_bytes: 380
#include<stdio.h>

int main (){
    int i,j,N,mem;
    int ar[100];
    for(i=0;i<100;i++){
        ar[i]=0;
    }
    
    scanf("%d",&N);
    for(i=0;i<N;i++){
        scanf("%d",&ar[i]);
    }
    for(j=0;j<N;j++){
        printf("%d",ar[j]);
        if(j!=N-1)printf(" ");
    }
    printf("\n");
    
    
    for(i=1;i<N;i++){
        mem=ar[i];
        j=i-1;
        while(j>=0 && ar[j]>mem){
            ar[j+1]=ar[j];
            j--;
        }
        ar[j+1]=mem;
        
        for(j=0;j<N;j++){
            printf("%d",ar[j]);
            if(j!=N-1)printf(" ");
        }
        printf("\n");
    }
}


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

#define REP(i,n) for(i=0;i<n;i++)
#define ll long long

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

    printf("%d",N*N*N);
    
    return 0;
}

Measured memory_bytes: 128
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n, m;
int s[2010], t[2010];

ll dp[2010][2010];

ll solve() {
    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;
                dp[i][j] %= mod;
            }
            dp[i][j] += dp[i][j-1] + dp[i-1][j] + (mod-dp[i-1][j-1]);
            dp[i][j] %= mod;
        }
    }
    return (dp[n][m]+1) % mod;
}

void input() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < m; i++) cin >> t[i];
}

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

    input();
    cout << solve() << endl;
}


Measured memory_bytes: 31744
function main(input) {
    const args = input.split(' ');
    const a = parseInt(args[0], 10);
    const b = parseInt(args[1], 10);
    // let a = 3;
    // let b = 3;
    calc(a,b);
}
function calc(a,b) {
    if ((a * b) % 2 === 0) {
        console.log('Even');
    } else {
        console.log('Odd');
    }
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));
// main();

Measured memory_bytes: 7372