akhauriyash/Code-Regression huggingface dataset:
function memoit(f::Function, param)
    if !isdefined(Main, :memoit_cache)
        global memoit_cache = Dict{Function, Dict{Any, Any}}()
    end
    cache = haskey(memoit_cache, f) ? memoit_cache[f] : memoit_cache[f] = Dict()
    haskey(cache, param) ? cache[param] : cache[param] = f(param)
end

macro memo(arg)
    (!(typeof(arg) <: Expr) || !(arg.head == :call)) && error("wrong atgs")
    return quote
        memoit($(esc(arg.args[1])), $(esc(arg.args[2])))
    end
end

read(T) = parse(T, readline())
reads(T) = map(x->parse(T,x),split(readline()))

function dp(n::Int)
    ret = 1000000000
    if (n == 1) ret = 0 end
    if (n > 1) ret = min(ret, ((@memo dp(n-1)) + abs(v[n] -  v[n-1]))) end
    if (n > 2) ret = min(ret, ((@memo dp(n-2)) + abs(v[n] -  v[n-2]))) end
    ret
end

function main()
    n = read(Int)
    global v = reads(Int)
    println(dp(n))
end

main()


Measured memory_bytes: 219616
#include <bits/stdc++.h>
using namespace std;
#define maxN 1000001
#define mod 1000000007
#define maxLL 1000000000000000000
#define PI 3.14159265358979323846264338327950L
typedef long long ll;

bool compared(int a,int b){
	return a < b;
}

int cmp(const void *a,const void *b){
	return(*(int*)b - *(int*)a);
}

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

int LCM(int a,int b){
	return a/__gcd(a,b)*b;
}

int main(){
	ll n;
	ll a[200001];
	cin>>n;
	ll sum=0;
	for(ll i=0;i<n;i++){
		cin>>a[i];
		sum^=a[i];
	}
	for(ll i=0;i<n;i++){
		printf("%lld ",sum^a[i]);
	}
	return 0;
}

Measured memory_bytes: 5316
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define RFOR(i, a, b) for(int i = a; i >= b; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
const int INF = 1e9;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
int main() {
    int x, a;
    cin >> x >> a;
    if (x < a) cout << 0;
    else cout << 10;
}

Measured memory_bytes: 256
import sys
from bisect import bisect_right, bisect_left
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(int(readline()) for _ in range(N))

    LIS = [INF]*N
    A.reverse()
    for a in A:
        i = bisect_right(LIS,a)
        LIS[i] = a
    print(bisect_left(LIS, INF))


if __name__ == '__main__':
    main()



Measured memory_bytes: 13896
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string ha,  ta;
	int n, h = 0 , t = 0;
	cin >> n;
	while (n--)
	{
		cin >> ha >> ta;
		if (ha > ta)h += 3;
		if (ha == ta) { h += 1; t += 1; }
		if (ha < ta)t += 3;
	}
	cout << h << " " << t;
	cout << endl;
	return 0;
}


Measured memory_bytes: 3152
#[allow(unused_imports)]
use std::{cmp::Reverse, collections::*};

#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Chars, Usize1},
};

#[allow(unused_imports)]
use itertools::Itertools;

#[allow(unused_imports)]
use num::Integer;

#[fastout]
pub fn main() {
    input! {
        mut h: usize,
    }

    let mut n = 1;
    let mut c = 0usize;
    while h > 1 {
        c += n;
        n <<= 1;
        h >>= 1;
    }
    c += n;
    println!("{}", c);
}


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

using namespace std;

int main(){
  string s;
  int m;
  int h;
  
  while(1)
    {
      cin >> s;
      
      if(s == "-") break;
      
      cin >> m;
      string str = s;

      for(int i=0;i < m;i++)
	{
	  cin >> h;
	  
	  for(int j=0;j < h;j++)
	    {  
	      s += s[0];
	      s.erase(s.begin());
	    }
	}
      cout << s << endl;
    }
  return 0;
}
      


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

using namespace std;

#define lint long long

int main(){
    int N;
    cin >> N;
    
    vector<int> a(N);
    for(int i = 0; i < N; ++i){
        cin >> a[i];
    }
    
    sort(a.begin(), a.end());
    
    int num;
    
    for(int i = 0; i < N; ++i){
        if(a[i] >= a[N - 1] / 2){
            if(a[i - 1] + a[i] >= a[N - 1]){
                num = a[i - 1];
            } else {
                num = a[i];
            }
            break;
        }
    }
    
    cout << a[N - 1] << " " << num << endl;
}

Measured memory_bytes: 640
//-----by LY02------------------------------------
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
//------------------------------------------------


int st[100010];
int dp[100010];
//------------------------------------------------
int main() {
//------------------------------------------------
//	freopen("In.txt","r",stdin);
//	freopen("Out.txt","w",stdout);
//------------------------------------------------
	ios::sync_with_stdio(0);
	cin.tie(0);
//------------------------------------------------

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

dp[0]=0;
dp[1]=abs(st[0]-st[1]);

for(int i=2;i<n;i++)
{
	dp[i]=min(dp[i-2]+abs(st[i-2]-st[i]) , dp[i-1]+abs(st[i-1]-st[i]));
}
cout<<dp[n-1]<<endl;

//------------------------------------------------
	return 0;
}





//-----by LY02------------------------------------


Measured memory_bytes: 1024
#include<cstdio>
int partition(int A[],int p,int r)
{
    int i=p-1;
    int j=p;
    int x=A[r];
    for(;j<r;j++)
    {
        if(A[j]<=x)
        {
            i++;
            int t;
            t=A[j];
            A[j]=A[i];
            A[i]=t;
        }
    }
    int t;
    t=A[i+1];
    A[i+1]=A[r];
    A[r]=t;
    return i+1;
}
int main()
{
    int n;
    scanf("%d",&n);
    int A[100000];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
    }
    int flag=partition(A,0,n-1);
    for(int i=0;i<n;i++)
    {
    if(i!=0) printf(" ");
    else{};
    if(i==flag) printf("[%d]",A[flag]);
    else printf("%d",A[i]);
    }
    printf("\n");
}



Measured memory_bytes: 2956
def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = int(input())
    b = list(map(int,input().split()))
    res = 0
    for i in range(m):
        l = 0
        r = n
        while l<r:
            mid = int((l+r)/2)
            if b[i]==a[mid]:
                res+=1
                break
            if b[i]>a[mid]:l = mid+1
            else          :r = mid
    print (res)


if __name__ == '__main__':
    main()




Measured memory_bytes: 16712
n,k=list(map(int,input().split()))

if n<k:
    if 2*n>=k:
        if k%2==0:
            print(1)
        else:
            print(0)
    else:
        print(0)
else:
    if k%2==1:
        print((n//k)**3)
    else:
        print((n//k)**3+((n-k//2)//k+1)**3)

Measured memory_bytes: 3060
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
struct segtree{
private:
  vector<ll> node,lazy;
  ll siz;
public:
  segtree(int n){
    siz=1;
    while(siz<=n) siz*=2;
    node.resize(siz*2,0);
    lazy.resize(siz*2,0);
  }
  void eval(ll pos){
    node[pos]+=lazy[pos];
    node[pos]%=mod;
    if(pos<siz-1) {
      lazy[pos*2+1]+=(lazy[pos]*(ll)(1e9+8)/2)%mod;
      lazy[pos*2+2]+=(lazy[pos]*(ll)(1e9+8)/2)%mod;
      lazy[pos*2+1]%=mod;
      lazy[pos*2+2]%=mod;
    }
    lazy[pos]=0;
  }
  void add_q(ll a,ll b,ll x,ll l,ll r,ll pos){
    eval(pos);
    if(b<=l||r<=a) return;
    if(a<=l&&r<=b){
      lazy[pos]+=(x*(r-l));
      lazy[pos]%=mod;
      eval(pos);
      return;
    }
    ll mid=(l+r)/2;
    add_q(a,b,x,l,mid,pos*2+1);
    add_q(a,b,x,mid,r,pos*2+2);
    node[pos]=(node[pos*2+1]+node[pos*2+2])%mod;
  }
  ll sum_q(ll a,ll b,ll l,ll r,ll pos){
    if(b<=l||r<=a) return 0;
    eval(pos);
    if(a<=l&&r<=b){
      return node[pos]%mod;
    }
    ll mid=(l+r)/2;
    return (sum_q(a,b,l,mid,pos*2+1)+sum_q(a,b,mid,r,pos*2+2))%mod;
  }
  void add(ll a,ll b,ll x){
    add_q(a,b,x,0,siz,0);
  }
  ll sum(ll a,ll b){
    return sum_q(a,b,0,siz,0);
  }
};
int main(){
  ll n,a,b;
  cin>>n>>a>>b;
  if(a<b) swap(a,b);
  vector<ll> vec;
  if(n<=25){
    ll as=0;
    vector<ll> v2;
    for(int p=0;p<n;p++){
      ll kn;
      cin>>kn;
      v2.push_back(kn);
    }
    ll max_b=(1<<n);
    for(ll p=0;p<max_b;p++){
      vector<ll> va,vb;
      bool f=true;
      for(int j=0;j<n;j++){
        if(((p>>j)&1)==1) va.push_back(v2[j]);
        else vb.push_back(v2[j]);
      }
      ll asi=va.size(),bsi=vb.size();
      for(int j=0;j<asi-1;j++) {
        if(va[j+1]-va[j]<a) f=false;
      }
      for(int j=0;j<bsi-1;j++) {
        if(vb[j+1]-vb[j]<b) f=false;
      }
      if(f) as++;
    }
    as%=mod;
    cout<<as<<endl;
    return 0;
  }


  vec.push_back(-(((ll)1e18)*2+5));
  for(int i=1;i<=n;i++){
    ll num;
    cin>>num;
    vec.push_back(num);
  }
  vec.push_back(((ll)1e18)*2+5);
  vector<ll> lb(n+5),rb(n+5);
  for(int i=1;i<=n;i++){
    lb[i]=lower_bound(vec.begin(),vec.end(),vec[i]+a)-vec.begin();
  }
  ll las=n+1;
  for(int i=n;i>0;i--){
    rb[i]=las;
    if(vec[i+1]-vec[i]<b) las=i+1;
  }

  segtree dp0(n+5),dp1(n+5);
  dp0.add(1,las+1,1);
  for(ll i=1;i<=n;i++){
    ll plus=dp1.sum(i,i+1);
    if(vec[i+1]-vec[i-1]>=b) plus+=dp0.sum(i,i+1);
    plus%=mod;
    if(vec[i+1]-vec[i]>=a) dp1.add(i+1,i+2,plus);
    ll L=max(i+2,lb[i]),R=rb[i];
    if(L>R) continue;
    dp0.add(L,R+1,plus);
  }
  cout<<(dp0.sum(n+1,n+2)+dp1.sum(n+1,n+2))%mod<<endl;
}


Measured memory_bytes: 10996
#include<algorithm>
#include<cstdio>
#define mxn 500010
#define LL long long
#define mod 998244353
using namespace std;
LL fac[mxn],ifc[mxn];
int n,m,sl,fh,ans;
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
LL C(int x,int y) {return fac[x]*ifc[y]%mod*ifc[x-y]%mod;}
LL _pow(LL k,int i)
{
	static LL t;t=1;
	for(;i;i>>=1,k=k*k%mod)
		if(i&1)
			t=t*k%mod;
	return t;
}
int main()
{
	n=rd();m=rd();if(n>m) swap(n,m);fac[0]=1;
	for(int i=1;i<=m;++i) fac[i]=i*fac[i-1]%mod;
	ifc[m]=_pow(fac[m],mod-2);
	for(int i=m;i;--i) ifc[i-1]=i*ifc[i]%mod;
	for(int i=0,c=1;i<=n;++i,c=mod-c) ans=(ans+c*C(n,i)%mod*C(m,i)%mod*fac[i]%mod*_pow(m+1,n-i)%mod*_pow(n+1,m-i))%mod;
	printf("%d\n",ans);
	return 0;
}

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

int main(){
	long long int a,b,c,d;
	double e;
	cin >>a>>b;
	c=a%b;
	d=(a-c)/b;
	e=(double)a/b;
	cout << fixed<< setprecision(20);
	cout << d<<" "<<c<<" "<<e<<"\n";
}

Measured memory_bytes: 1244
fun main(args : Array<String>) {
    val (n, m) = readLine()!!.split(" ").map{it.toInt()}
    val shops = mutableListOf<Pair<Int, Int>>()

    repeat(n) {
        val (a, b) = readLine()!!.split(" ").map{it.toInt()}
        shops.add(Pair(a, b))
    }

    shops.sortBy { it.first }
    var cnt = 0
    var sum = 0L

    shops.forEach {
        cnt += it.second
        sum += it.first.toLong() * it.second.toLong()

        if (cnt > m) {
            val diff = cnt - m
            cnt -= diff
            sum -= diff.toLong() * it.first.toLong()
        }

        if (cnt == m) {
            println(sum)
            return
        }
    }
}

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

int main() {
  int K, S;
  cin >> K >> S;
  int res = 0;
  for (int a = 0; a <= K; ++a) {
    for (int b = 0; b <= K; ++b) {
      int c = S - a - b; // 総和
      if (0 <= c && c <= K) ++res; // 合計が k に一致したら答えをインクリメント
      
    }
  }
  cout << res << endl;
}

Measured memory_bytes: 256
from collections import defaultdict
def inpl(): return [int(i) for i in input().split()]
class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sumi(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N, M = inpl()
H = defaultdict(lambda: [])
for _ in range(N):
    l, r = inpl()
    H[r-l+1].append(l)
Q = BIT(M+1)
ctr = N
for i in range(1,M+1):
    ctr -= len(H[i]) 
    for j in H[i]:
        Q.add(j, 1)
        Q.add(j+i, -1)
    ans = 0
    for k in range(i,M+1,i):
        ans += Q.sumi(k)
    print(ctr + ans)

Measured memory_bytes: 93016
#include <iostream>
#include <vector>
#include <utility>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin>>n;cin.ignore();
	while(n--)
	{
		string a;
		getline(cin,a);
		for(int i=a.find("Hoshino");i!=string::npos;i=a.find("Hoshino"))
			a[i+6]='a';
		cout<<a<<endl;
	}
}

Measured memory_bytes: 1208
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()

using namespace std;
using ll = int64_t;

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

  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    --p[i];
  }

  int res = 0;
  for (int i = 0; i < n; i++) {
    if (p[p[i]] == i)
      ++res;
  }

  res /= 2;

  cout << res << endl;

  return 0;
}


Measured memory_bytes: 640
import java.util.Scanner;

public class Main {

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

		int h = sc.nextInt();
		int w = sc.nextInt();
		for (; h != 0 || w != 0;) {
			StringBuffer line = new StringBuffer();
			for (int i = 0; i < w; i++) {
				line.append('#'); 
			}
			for (int i = 0; i < h; i++) {
				System.out.println(line);
			}
			System.out.println("");
			
			h = sc.nextInt();
			w = sc.nextInt();
		}
		
		sc.close();
	}
}

Measured memory_bytes: 21012
#define ll long long
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define fo(i , n) for(ll i = 0 ; i < n ; i++)
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<utility>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
ll z = 1000000007 ;
/*ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
ll power(ll a ,ll b){if(b == 0) return 1 ; ll c = power(a , b/2) ; if(b%2 == 0) return ((c*c)%z) ; else return ((((c*c)%z)*a)%z) ;}
ll inverse(ll a ,ll n){return power(a , n-2) ;}
ll max(ll a , ll b){if(a > b) return a ; return b ;}*/
//ios_base::sync_with_stdio(0);
//cin.tie(0); cout.tie(0);
using namespace std ;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	ll n , k , s ;
	cin >> n >> k >> s ;
	vector<ll> v ;
	fo(i , k)
	{
		v.pub(s) ;
	}
	ll t = s+1 ;
	if(t == 1000000001)
		t = 999999999 ;
	for(ll j = k ; j < n ; j++)
		v.pub(t) ;
	fo(i , n)
	{
		cout << v[i] << ' ';
	}
	cout << '\n' ;
	return 0;
}

Measured memory_bytes: 2292
#include <stdio.h>
#include <math.h>

#define PI (acos(-1))
#define sq(n) ((n) * (n))

int main(void) {
  int a, b, c;
  double s;
  scanf("%d %d %d\n", &a, &b, &c);
  s = a * b * sin(c * PI / 180) / 2;
  printf("%.10f\n", s);
  printf("%.10f\n", sqrt(sq(a) + sq(b) - 2 * a * b * cos(c * PI / 180)) + a + b);
  printf("%.10f\n", b * sin(c * PI / 180));

  return 0;
}

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

static const int N = 1000;

int lcs(string& X, string& Y) {
	int c[N + 1][N + 1];
	int m = X.length();
	int n = Y.length();
	int maxlen = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	for (int i = 0; i < m + 1; i++)c[i][0] = 0;
	for (int j = 1; j < n + 1; j++)c[0][j] = 0;

	for (int i = 1; i < m + 1; i++) {
		for (int j = 1; j < n + 1; j++) {
			if (X[i] == Y[j])c[i][j] = c[i - 1][j - 1] + 1;
			else {
				c[i][j] = max(c[i - 1][j], c[i][j - 1]);
			}
			maxlen = max(maxlen, c[i][j]);
		}
	}
	return c[m][n];
}

int main() {
	string s1, s2;
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}


Measured memory_bytes: 7128
x1, y1, x2, y2  = map(int, input().split())


x3 = x2 + (y1-y2)
y3 = y2 + (x2-x1)


x4 = x3 + (y2-y3)
y4 = y3 + (x3-x2)

print(x3,y3,x4,y4)

Measured memory_bytes: 2940
using System;
using System.Linq;

namespace diverta2019_B
{
    class Program
    {
        static void Main(string[] args)
        {
            var RGBN = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var R = RGBN[0];
            var G = RGBN[1];
            var B = RGBN[2];
            var N = RGBN[3];

            var ans = 0;

            for (int r = 0; r <= N; r += R)
            {
                for (int g = 0; g <= N; g += G)
                {
                    if (r + g > N) break;
                    if ((N - (r + g)) % B == 0)
                        ans++;
                }
            }
            Console.WriteLine(ans);
        }
    }
}


Measured memory_bytes: 13268
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = int;
using Graph = vector<vector<Edge>>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = 1e9;
bool graph[nmax][nmax];
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n)
{
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            for (size_t k = 0; k < n; k++)
            {
                dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
            }
        }
    }
}

class UnionFind
{
public:
    vector<ll> Parent;

    UnionFind(ll N)
    {
        Parent = vector<ll>(N, -1);
    }
    ll find(ll A)
    {
        if (Parent[A] < 0)
            return A;
        return Parent[A] = find(Parent[A]);
    }

    ll size(ll A)
    {
        return -Parent[find(A)];
    }

    bool Union(ll A, ll B)
    {
        A = find(A);
        B = find(B);
        if (A == B)
        {
            return false;
        }
        if (size(A) < size(B))
            swap(A, B);

        Parent[A] += Parent[B];
        Parent[B] = A;

        return true;
    }
};

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

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    return a / g * b;
}

ll mulMod(ll a, ll b)
{
    return (((a % MOD) * (b % MOD)) % MOD);
}

ll powMod(ll a, ll p)
{
    if (p == 0)
    {
        return 1;
    }
    else if (p % 2 == 0)
    {
        ll half = powMod(a, p / 2);
        return mulMod(half, half);
    }
    else
    {
        return mulMod(powMod(a, p - 1), a);
    }
}

ll ceil(ll a, ll b)
{
    return (a + b - 1) / b;
}

vector<ll> tsort(Graph G)
{
    ll N = G.size();
    vector<ll> in(N);
    for (auto &&edges : G)
    {
        for (auto &&edge : edges)
        {
            in[edge]++;
        }
    }

    queue<int> que;

    for (int i = 0; i < N; i++)
    {
        if (in[i] == 0)
        {
            que.push(i);
        }
    }
    int cnt = 0;
    vector<ll> ans;
    while (!que.empty())
    {
        int v = que.front();
        que.pop();
        ans.push_back(v);
        for (auto &&next : G[v])
        {

            in[next]--;
            if (in[next] == 0)
            {
                que.push(next);
            }
        }
    }
    return ans;
}

void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B)
{
}

int main()
{
    long long N;
    scanf("%lld", &N);
    long long M;
    scanf("%lld", &M);
    Graph G(N);
    for (int i = 0; i < N - 1 + M; i++)
    {
        ll a, b;
        cin >> a >> b;
        --a;
        --b;
        G[a].push_back(b);
    }

    vector<ll> ans(N);

    vector<ll> res = tsort(G);

    ans[res[0]] = 0;
    for (int i = 0; i < N; i++)
    {
        int index = res[i];

        for (auto &&edge : G[index])
        {
            ans[edge] = index + 1;
        }
    }

    for (int i = 0; i < N; i++)
    {
        cout << ans[i] << endl;
    }

    return 0;
}


Measured memory_bytes: 13948
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <string>
#include <cmath>

# define PI 3.14159265358979323846

using namespace std;

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

    int ans = 0;

    vector<int> bs;
    for (int i = 0; i < m; ++i) {
        int e;
        cin >> e;
        bs.push_back(e);
    }

    vector< vector<int> > as;
    for (int i = 0; i < n; ++i) {
        vector<int> l;
        for (int j = 0; j < m; ++j) {
            int e;
            cin >> e;
            l.push_back(e);
        }
        as.push_back(l);
    }

    for (int i = 0; i < n; i++) {
        int s = 0;
        for (int j = 0; j < m; j++) {
            s += as[i][j] * bs[j];
        }
        s += c;
        if (s > 0)
            ans += 1;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
import itertools
n=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
S=list(itertools.permutations(P, n))
T=list(itertools.permutations(P, n))
S.sort()
T.sort()
a=0
b=0
for i in range(len(S)):
  if list(S[i])==P:
    a=i+1
for j in range(len(T)):
  if list(T[j])==Q:
    b=j+1
print(abs(a-b))

Measured memory_bytes: 12788
#include<iostream>

using namespace std;

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

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

// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) int(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e15;
const int MOD = 1e9+7;
const ll LINF = 1e18;



signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  ll d,n; cin>>d>>n;
  ll ans=1;
  rep(i,d) ans*=100;
  if(n==100) ans*=101;
  else ans*=n;
  cout<<ans<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<int, int>;

void solve(long long H, long long W, std::vector<std::string> S){
    vector<vector<ll> > rowinfo(H, vector<ll>(W, 0)), colinfo(H, vector<ll>(W, 0));
    for(ll i=0; i< H; i++){
        string myline = S[i];
        ll l=0, r=0;
        for(ll j=0; j<W; j++){
            if(myline.at(j) == '#'){
                if(j>0 && myline.at(r)== '.') 
                    //* rowinfo[i].push_back(pll(l, r));
                    //* update [l, r]
                    for(ll k = l; k <= r; k++){
                        rowinfo[i][k] = r-l+1;
                    }

                l = j+1; r = j+1;
                if(l >= W) break;
            } 
            else {
                r=j;
                if(r == W-1) 
                    //* rowinfo[i].push_back(pll(l, r));
                    //* update [l, r]
                    for(ll k = l; k <= r; k++){
                        rowinfo[i][k] = r-l+1;
                    }
            }
        }
    }
    for(ll i=0; i< W; i++){
        ll l=0, r=0;
        for(ll j=0; j<H; j++){
            if(S[j].at(i) == '#'){
                if(j>0 && S[r].at(i)== '.') 
                    //* colinfo[i].push_back(pll(l, r));
                    //* update [l, r]
                    for(ll k = l; k <= r; k++){
                        colinfo[k][i] = r-l+1;
                    }
                l = j+1; r = j+1;
                if(l >= H) break;
            } 
            else {
                r=j;
                if(r == H-1) 
                    //* colinfo[i].push_back(pll(l, r));
                    //* update [l, r]
                    for(ll k = l; k <= r; k++){
                        colinfo[k][i] = r-l+1;
                    }
            }
        }
    }
    ll cnt = 0;
    for(ll i=0; i< H; i++){
        for(ll j=0; j< W; j++){
            cnt = max(cnt, rowinfo[i][j]+colinfo[i][j]);
        }
    }
    cout << cnt -1 << endl;
    return;
}

int main(){
    long long H;
    scanf("%lld",&H);
    long long W;
    scanf("%lld",&W);
    std::vector<std::string> S(H);
    for(int i = 0 ; i < H ; i++){
        std::cin >> S[i];
    }
    solve(H, W, std::move(S));
    return 0;
}


Measured memory_bytes: 67200
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int a = sc.nextInt();
		int b = sc.nextInt();

		sc.close();

		int na = b - a - 1;

		int ans = (na + 1) * na / 2 - a;

		out.println(ans);

	}

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


Measured memory_bytes: 21844
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#define rep(i, N) for(int i = 0; i < N; ++i)

typedef long long ll;
using namespace std;

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

    ll sum = 0;
    rep(i, m)
    {
        int x;
        cin >> x;
        sum += x;
    }
    int ans = n - sum;

    if (ans < 0)ans = -1;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3620
#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i,a,b) for(lli i=a;i<b;i++) 
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mod2 1000000006
#define inf 1000000000000000000
#define lld long double
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<lli>
#define vii vector<int>
#define is indexed_set
#define eps 0.000001
#define endl '\n'
#define mo 998244353
#define pi 3.141592653589793
#define FF first
#define SS second
using namespace std ;
 
const lli dx4[] = {-1, 0, 1, 0};
const lli dy4[] = {0, 1, 0, -1};
const lli dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const lli dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
 
typedef tree<pll,null_type,less<pll>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); // can give negative values too

#define N 100001

void solve()
{
    string s;
    cin>>s;

    lli n = s.size();

    if(s[n-1] == '1' || s[n-2] == '0')
    {
        cout<<-1;
        return ;
    }

    for(lli i=0;i<=n-2;i++)
    {
        if(s[i] != s[n-2-i])
        {
            cout<<-1;
            return ;
        }
    }

    vector<pll> edges;
    lli node=1,p=2;

    for(lli i=0;i<=n-2;i++)
    {
        edges.pb({node,p});

        if(s[i] == '1') node = p;
        p++;
    }

    for(auto x:edges) cout<<x.ff<<" "<<x.ss<<endl;

}   

int main()
{
////////////////////////////////////////////   
    fastio;
////////////////////////////////////////////
    
    lli t;
    t=1;
    while(t--)
    {
        solve();
    }
}


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

//const ll INF = 1LL << 60;
#define MAX_N 10000000
typedef long long ll;
//ll table[MAX_N][MAX_N] = INF;

/*
ll func(ll m, vector<ll> A){
    ll ans = 0;
    for(auto i : A) ans = ans + m % i;
    return ans;
}

void solve(long long N, std::vector<long long> A){
    ll ans = 0;
    for(int i = 2; i < MAX_N; ++i){
        ans = max(ans, func(i, A));
    }
    cout << ans << endl;
}
*/

void solve(long long N, std::vector<long long> A){
    ll ans = 0;
    for(auto a: A) ans = ans + a - 1;
    cout << ans << endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}


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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 100010
const int MOD = 1000000007;

void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> v(n), s(m);
    for (int i = 0; i < n; i++)cin >> v[i];
    for (int i = 0; i < m; i++)cin >> s[i];

    vector<vector<int>> dp(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (v[i - 1] == s[j - 1])dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] + 1) % MOD;
            else dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] + MOD) % MOD;
        }
    }
    cout << (dp[n][m] + 1) % MOD << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
//    cin >> t;

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



Measured memory_bytes: 31616
n = int(input())
x = 0
l = 0
while x < n:
    l += 1
    x += 26**l
for i in range(1,l):
    n -= 26**i
n -= 1
ans = ""
while len(ans) < l:
    ans += chr(ord("a")+n%26)
    n //= 26
print(ans[::-1])

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

ll mod = 1e9+7;
ll dp[100000][2];

vector<int> node[100000];

void dfs(int p, int par = -1) {
  dp[p][0] = dp[p][1] = 1;
  for (int i : node[p]) {
    if (i == par) continue;
    dfs(i, p);
    (dp[p][0] *= dp[i][1]+dp[i][0]) %= mod;
    (dp[p][1] *= dp[i][0]) %= mod;
  }
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    node[a].push_back(b);
    node[b].push_back(a);
  }
  dfs(0);
  cout << (dp[0][0]+dp[0][1])%mod << endl;
}


Measured memory_bytes: 12800
#include <stdio.h>

int main(void)
{
	int i, j;
	
	for (i = 1; i < 10; i++){
		for (j = 1; j < 10; j++){
			printf("%dx%d=%d\n", i, j, i * j);
		}
	}
	
	return (0);
}

Measured memory_bytes: 524
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#define mod 1000000007
typedef long long ll;
using namespace std;

// c[i][j]:=先頭がi,末尾がjであるような数の個数
int c[10][10];

int main(){
  int n; cin>> n;
  for(int i=1;i<=n;i++){
    int t = i;
    int mi = t % 10;
    int mx;
    while(t > 0){
      mx = t;
      t /= 10;
    }
    c[mx][mi]++;
  }
  
  int ans = 0;
  for(int i=0;i<10;i++){
    for(int j=0;j<10;j++){
      ans += c[i][j] * c[j][i];
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;
using namespace std;

int main(int argc, char* argv[])
{
    int n,k;
    scanf("%d%d", &n, &k);
    int flag[10]={0};
    int i;
    for(i=0; i<k; i++) {
        int tmp;
        scanf("%d", &tmp);
        flag[tmp]=1;
    }
    for(i=n; i<100000; i++) {
        int curr=i;
        int bad=0;
        while(curr) {
            if(flag[curr%10]) {
                bad=1;
                break;
            }
            curr/=10;
        }
        if(bad==0) {
            printf("%d\n", i);
            return 0;
        }
    }

    return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <list>
#define rep(i,n) for(int i=0; i<n; ++i)
using namespace std;

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



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

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

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); }

template<typename T> void fill_all(T& arr, const T& v) { arr = v; }
template<typename T, typename U> void fill_all(T& arr, const U& v) { for (auto& i : arr) fill_all(i, v); }

template<typename T>auto&operator<<(ostream&s,const vector<T>&v){s<<"[";bool a=1;for(auto e:v){s<<(a?"":" ")<<e;a=0;}s<<"]";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const pair<T,U>&p){s<<"("<<p.first<<","<<p.second<<")";return s;}
template<typename T>auto&operator<<(ostream&s,const set<T>&st){s<<"{";bool a=1;for(auto e:st){s<<(a?"":" ")<<e;a=0;}s<<"}";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const map<T,U>&m){s<<"{";bool a=1;for(auto e:m){s<<(a?"":" ")<<e.first<<":"<<e.second;a=0;}s<<"}";return s;}
#define DUMP(x) cout<<#x<<" = "<<(x)<<endl

struct edge { int to, cost; };

const LL LINF = 1LL<<60;
const int IINF = 1<<30;
const LL MOD = 1e9+7;

int main()
{
   vector<int> a(5);
   for (int i = 0; i < 5; ++i) {
      cin >> a[i];
   }

   vector<int> b(5);
   for (int i = 0; i < 5; ++i) {
      b[i] = ((-a[i] % 10) + 10) % 10;
      a[i] += b[i];
   }
   sort(b.rbegin(), b.rend());

   int ans = accumulate(a.begin(), a.end(), 0LL);
   ans -= b[0];
   cout << ans << endl;

   return 0;
}


Measured memory_bytes: 256
cnt = int(input())
dif_max = -1000000000
v_min = 1000000000

for i in range(cnt):
    buf = int(input())
    if i > 0:
        dif_max = dif_max if buf - v_min < dif_max else buf - v_min
    v_min = buf if buf < v_min else v_min
print(dif_max)

Measured memory_bytes: 7664
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define mxe max_element
#define mne min_element
#define bg begin()
#define nd end()

void swap(int &a,int &b)
{
    int t=a;
    a=b;
    b=t;
    return ;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll t,n,m;
    
    string a,b;
    cin>>a>>b;
    
    int c=0;
    
    if(a.compare(b)==0)cout<<c<<"\n";
    else
    {
        n=a.length();
        for(int i=0;i<n;i++)
        {
            if(a[i]!=b[i])c++;
        }
        cout<<c<<"\n";
    }

    
    
    
    return 0;
}
 
 
 
 
 




Measured memory_bytes: 3796
#include <bits/stdc++.h>
#include <unordered_map>
typedef long long ll;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " \n"[i == n-1];}
#define printp(x,n) for(ll i = 0; i < n; i++){ cout << "(" << x[i].first << ", " << x[i].second << ") "; } cout << endl;
#define INF (1e18)
using namespace std;
const ll MOD = 1e9 + 7;
typedef pair<ll, ll> lpair;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll D;
    cin >> D;
    cout << "Christmas";
    rep(i,0,25-D){
        cout << " Eve";
    }
    cout << endl;
}


Measured memory_bytes: 256
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
point_start_with = [0 for i in range(n)]
roop_count = [1 for i in range(n)]
for i in range(n):
    now = (i + 1)
    now_score = 0
    while p[now - 1] != (i + 1):
        now = p[now - 1]
        now_score += c[now - 1]
        roop_count[i] += 1
    point_start_with[i] = now_score
# 多分0でいい
ans = -(10**9 * 5000)
for i in range(n):
    # iからスタートする場合
    score = 0
    remain_move = 0
    cand_score = 0
    if point_start_with[i] + c[i] > 0 and k > roop_count[i]:
        # 一周以上する可能性がある場合
        # n週して、残りの動ける回数
        # print("Yes")
        round_count = (k // roop_count[i]) - 1
        remain_move = k - round_count * roop_count[i]
        cand_score = round_count * ( point_start_with[i] + c[i] )
    else:
        remain_move = min(k,roop_count[i])
        score = -(10**9 * 5000)
        cand_score = 0
    now = i
    for j in range(remain_move):
        now = p[now] - 1
        cand_score += c[now]
        score = max(score, cand_score)
    # print("start : {}  score : {}".format(i,score))
    ans = max(score, ans)
print(ans)


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

using namespace std;

const int maxn = 1e2+10;
const int maxm = 1e5+10;

typedef long long ll;

int w[maxn], c[maxn];
ll dp[maxm];

void solve(int n, int m)
{
	for (int i = 1; i <= n; i++)
		for (int j = m; j >= w[i]; j--)
			dp[j] = max(dp[j], dp[j-w[i]]+(ll)c[i]);
}

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

	for (int i = 1; i <= n; i++)
		cin >> w[i] >> c[i];

	solve(n, m);

	cout << dp[m] << "\n";
}

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


namespace straight_line
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            while ((line = Console.ReadLine()) != null)
            {
                var ab = line.Split().Select(int.Parse).ToArray();
                int a = ab[0];
                int c = 0;
                for (int b = 1; b < a + 1; b++)
                {
                    c += b + 1;
                }
                c -= a - 1;
                Console.WriteLine(c);
            }

        }
    }
}

Measured memory_bytes: 9464
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007

ll n,m;
vector<ll>ch[200010];
ll gr[200010];
ll cnt;
void dep(ll now){
	for(ll i=0;i<ch[now].size();i++){
		ll next=ch[now][i];
		if(gr[next]!=-1){
			continue;
		}
		gr[next]=cnt;
		dep(next);
	}
}
int main() {
	cin>>n>>m;
	for(ll i=0;i<m;i++){
		ll x,y;
		cin>>x>>y;
		x--;y--;
		ch[x].push_back(y);
		ch[y].push_back(x);
	}
	for(ll i=0;i<n;i++){
		gr[i]=-1;
	}
	for(ll i=0;i<n;i++){
		if(gr[i]!=-1){
			continue;
		}
		gr[i]=cnt;
		dep(i);
		cnt++;
	}
	map<ll,ll>ans;
	ll fa=0;
	for(ll i=0;i<n;i++){
		ll now=gr[i];
		ans[now]++;
		fa=max(ans[now],fa);
	}
	cout <<fa;
	// your code goes here
	return 0;
}

Measured memory_bytes: 22344
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
int main()
{
    ll arr[100];
    arr[1] = 1;
    arr[0] = 2;
    for(int i=2; i<=86; i++){
        arr[i] = arr[i-1] + arr[i-2];
    }
    int n;
    cin>>n;
    cout<<arr[n]<<endl;
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define i64 long long int
#define ran 200020
int n;i64 a[ran],b[ran],L[ran],R[ran];

int ace[ran],w[ran];
int ancestor(int x){return x-ace[x]?ace[x]=ancestor(ace[x]):x;}

void proc(i64*a,i64*res){
	res[1] = 0;
	w[1] = 12345678;
	for(int i=1; i<=n; i++)ace[i] = i;
	for(int i=2; i<=n; i++){
		res[i] = res[i-1];
		w[i] = 0;
		if(a[i] <= a[i-1]){
			for(long long int x = a[i]*4; x <= a[i-1]; x*=4)
				w[i] ++;
		}else{
			for(long long int x = a[i-1]; x < a[i]; x *= 4){
				int to = ancestor(i-1);
				res[i] += i - to;
				if(--w[to] == 0)ace[to] = to-1;
			}
		}
		if(w[i] == 0)ace[i] = i-1;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i=1; i<=n; i++){
		cin >> a[i];
		b[i] = a[i];
	}
	proc(b,L);
	reverse(a+1, a+n+1);
	for(int i=1; i<=n; i++)b[i] = a[i];
	proc(b,R);
	reverse(R+1, R+n+1);
	i64 res = (i64)1e18;
	for(int i=0; i<=n; i++)res = min(res, (L[i] + R[i+1]) * 2 + i);
	cout << res << endl;
	return 0;
}


Measured memory_bytes: 8192
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <queue>
using namespace std;
int main() {
    int n, q;
    string s;
    cin >> n >> q >> s;
    vector<int> l(q), r(q);
    for(int i=0; i<q; i++){
        cin >> l[i] >> r[i];
    }

    vector<int> ans(n, 0);
    for(int i=1; i<n; i++){
        if(s[i-1] == 'A' && s[i] == 'C'){
            ans[i] = ans[i-1] + 1;
        }else{
            ans[i] = ans[i-1];
        }
    }

    for(int i=0; i<q; i++){
        int tmpA = ans[r[i] -1] - ans[l[i]-1];
        printf("%d\n", tmpA);
    }
}

Measured memory_bytes: 2176
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <climits>
#include <cassert>
#include <algorithm>
#include <utility>

using namespace std;
using Int = long long int;
using UInt = unsigned long long int;
using VI = vector<long long int>;
using VVI = vector<vector<long long int> >;

// 二つの値の最小値を求める関数
Int Min(const Int a, const Int b) {
    if (a <= b) {
        return a;
    } else {
        return b;
    }
}
// 二つの値の最大値を求める関数
Int Max(const Int a, const Int b) {
    if (a >= b) {
        return a;
    } else {
        return b;
    }
}

int main(void) {
    Int w, h, n;
    cin >> w >> h >> n;
    Int sx, sy, ex, ey;
    sx = 0;
    sy = 0;
    ex = w;
    ey = h;
    Int x, y, a;
    for (Int i = 0; i < n; ++i) {
        cin >> x >> y >> a;
        if (a == 1) {
            sx = Max(sx, x);
        } else if (a == 2) {
            ex = Min(ex, x);
        } else if (a == 3) {
            sy = Max(sy, y);
        } else if (a == 4) {
            ey = Min(ey, y);
        }
    }
    //cout << sx << " " << ex << " " << sy << " " << ey << endl;
    cout << Max(0, (ex - sx)) * Max(0, (ey - sy)) << endl;
    return 0;
}



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

using namespace std;


void solve() {
    int n,a,b;
    cin >> n >> a >> b;
    if (a + b > n+1) {
        cout << -1; return;
    }
    if (a < (n+b-1)/b) {
        cout << -1; return;
    }
    vector<int> res(n+1);
    int x = n;
    for (int i=a;i>0;i--,x--) {
        res[i] = x;
    }
    if (b > 1) {
        int r = a;
        int len = (n-a)/(b-1);
        int rem = (n-a)%(b-1);
        while (x) {
            int act_len = len + (rem>0?1:0); rem--;
            assert(act_len <= x);
            int l = r+1;
            r = r + act_len;
            for (int i = r; i >= l; i--) {
                res[i] = x--;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << res[i] << ' ';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}


Measured memory_bytes: 3328
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

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

    static class BCountBalls {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            long n = Long.parseLong(in.next());
            long a = Long.parseLong(in.next());
            long b = Long.parseLong(in.next());

            long r1 = n / (a + b);
            long r2tmp = n % (a + b);
            long r2 = Math.min(r2tmp, a);

            out.append(String.valueOf(r1 * a + r2));
        }

    }
}



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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

int N;
ll a[10010], b[10010], c[10010];

int main() {
	cin >> N;

	ll s = 0;

	rep(i, N) cin >> a[i];
	rep(i, N) cin >> b[i];
	rep(i, N) {
		c[i] = a[i] - b[i];
		s += (-c[i]);
	}

	if (s < 0) {
		puts("No");
		return 0;
	}

	ll z1 = s, z2 = s;

	rep(i, N) {
		if (c[i] >= 0) {
			z1 -= c[i];
		} else {
			ll t = abs(c[i]);
			if (t & 1) {
				z2 -= (t+1) / 2;
				z1--;
			} else {
				z2 -= t / 2;
			}
		}

		if (z1 < 0 || z2 < 0) {
			puts("No");
			return 0;
		}
	}

	puts("Yes");
	
	return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int a = s.length();
    int b[26]={0},flag=0;
    for(int i=0;i<a;i++)
    {
        int temp = int(s[i])-int('a');
        b[temp]=1;
    }
    for(int i=0;i<26;i++)
    {
        if(b[i]==0)
        {
            cout<<char(i+int('a'));
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        cout<<"None";
    }
    return 0;
}


Measured memory_bytes: 512
#include <stdio.h>

int main(void){

  int n,x,count;
  register int i,j,s;

  while(1){
    count = 0;
    scanf("%d %d",&n,&x);
    if((n == 0) && (x == 0)){
      break;
    }
    for(i = 1;i <= n;i++){
      for(j = i;j <= n;j++){
        for(s = j;s <= n;s++){
          if((i != j) && (j != s)){
            if((i + j + s) == x){
              count++;
            }
          }
        }
      }
    }
    printf("%d\n",count);
  }
  return 0;
}

Measured memory_bytes: 600
n, y = map(int, input().split())

y=y/1000
c=y-n

if c<0 or 10*n<y: # 例外処理
    print(-1, -1, -1)
else:
    X=-1
    Y=-1
    Z=-1
    for x in range(n+1):
        for y in range(n+1):
            if x+y>n:
                break
            if 9*x+4*y==c:
                X=x
                Y=y
                Z=n-x-y
                break
        if X>=0: break
    print(X, Y, Z)

Measured memory_bytes: 3064
#[allow(unused_imports)]
use {
    itertools::Itertools,
    proconio::{fastout, input, marker::*},
    std::cmp::*,
    std::collections::*,
    std::io::Write,
    std::ops::*,
};

fn floor_sum(n: i64, m: i64, mut a: i64, mut b: i64) -> i64 {
    let mut ans = 0;
    if a >= m {
        ans += (n - 1) * n * (a / m) / 2;
        a %= m;
    }
    if b >= m {
        ans += n * (b / m);
        b %= m;
    }
    let y_max = (a * n + b) / m;
    let x_max = y_max * m - b;
    if y_max == 0 {
        return ans;
    }
    ans += (n - (x_max + a - 1) / a) * y_max;
    ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
    ans
}

#[fastout]
fn main() {
    input! {
        t: usize,
        query: [(i64,i64,i64,i64); t]
    }
    for (n, m, a, b) in query {
        println!("{}", floor_sum(n, m, a, b));
    }
}


Measured memory_bytes: 6904
#include <stdio.h>
    #include <math.h>
    #define min(x,y) (x<y?x:y)
    int main()
    {
        typedef long long ll;
        ll x,sum[200009],n,ans,minn;
        scanf("%lld",&n);
        sum[0]=0;
        ll i=1;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            sum[i]=sum[i-1]+x;
        }
        ans=sum[n];
        minn=fabs(2*sum[1]-sum[n]);
        for(i=2;i<=n-1;i++)
        {
            minn=min(minn,fabs(2*sum[i]-sum[n]));
        }
        printf("%lld\n",minn);
        return 0;
    }


Measured memory_bytes: 1664