akhauriyash/Code-Regression huggingface dataset:
A = int(input())
B = int(input())
C = int(input())
X = int(input())
count = 0
for i in range(A + 1):
    for j in range(B + 1):
        for k in range(C + 1):
            D = X - i * 500
            E = D - j * 100
            F = E - k * 50
            if F == 0:
                count += 1
print(count)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define pb push_back
#define int long long
#define rint register int
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=1e5+10;
const int inf=0x3f3f3f3f;

int n;
int arr[maxn],dp[maxn][2];

signed main(){
	scanf("%lld",&n);
	for(rint i=1;i<=n;++i) scanf("%lld",&arr[i]);
	dp[1][0]=arr[1];
	dp[1][1]=-arr[1];
	for(rint i=2;i<n;++i){
		dp[i][0]=max(dp[i-1][0]+arr[i],dp[i-1][1]-arr[i]);
		dp[i][1]=max(dp[i-1][1]+arr[i],dp[i-1][0]-arr[i]);
	}
	printf("%lld\n",max(dp[n-1][0]+arr[n],dp[n-1][1]-arr[n]));
	return 0;
}

Measured memory_bytes: 2560
import java.util.*;
 
public class Main {
	public static void main(String args[] ) throws Exception {
		
		Scanner sc = new Scanner(System.in);
		long N = Long.valueOf ( sc.nextLine().trim() ) ;

		BitSet bs = createPrimes ( 1000000 ) ; 
		List<Integer> primes = new ArrayList<>();
		for ( int p=bs.nextSetBit(0) ;p>=0 ; p=bs.nextSetBit(p+1) )
			primes.add ( p ) ;

		Map<Long,Integer> map = createFactors ( N , primes ) ;
		int cnt = 0 ;
		for ( long p : map.keySet() ) {
			long v = p ;
			while ( N % v ==0 ) {
				cnt += 1; 
				N /= v ;
				v *= p ;
			}
		}
		System.out.println( cnt ) ;
		
	 }
	public static BitSet createPrimes( int n ){
		BitSet bs = new BitSet();
		bs.set( 2, n ) ;
		for ( int i=bs.nextSetBit(0) ; i>=0 ; i=bs.nextSetBit(i+1) ){
			if ( (long) i * i > bs.length() ) break;
			for ( int k=2 ; (long)k*i < bs.length() ; k++ )
				bs.clear(k*i);
		}
		return bs ;
	}
	public static Map<Long,Integer> createFactors( long n , List<Integer> primes  ) {
		Map<Long,Integer> map = new HashMap<>() ;
		
		for ( int prime : primes ) {
			if ( (long)prime * prime > n ) break ;
			if ( n%prime !=0 ) continue ;
			
			int cnt = 0 ;
			while ( n%prime == 0 ){
				cnt++;
				n /= prime ;
			}
			map.put( (long)prime , cnt ) ;
		}
		if ( n != 1 )
			map.put( n , 1 ) ;
		return map ;
	}
}

Measured memory_bytes: 30316
from collections import Counter

n= int(input())
a = list(map(int, input().split()))

def nibeki(x):
    cnt = 0
    while(x>1):
        x //=2
        cnt += 1
    return 2**(cnt+1)

cntSet = 0
c = Counter(a)
ks = list(c.keys())
ks.sort(reverse=True)

for k in ks:
    pair = nibeki(k)-k
    if k==pair:
        cntSet += c[k]//2

    elif c[k]>0 and c[pair]>0:
        COUNT = min(c[k], c[pair])
        c[k] -= COUNT
        c[pair] -= COUNT
        cntSet +=COUNT

print(cntSet)

Measured memory_bytes: 33296
def d(xs, ys, p)
  if p == Float::INFINITY
    xs.map.with_index {|x, i| (x - ys[i]).abs}.max
  else
    sum = 0
    xs.each.with_index do |x, i|
      sum += (x - ys[i]).abs ** p
    end
    sum ** (1.0 / p)
  end
end

gets
xs = gets.split.map(&:to_i)
ys = gets.split.map(&:to_i)

puts sprintf '%.6f', d(xs, ys, 1)
puts sprintf '%.6f', d(xs, ys, 2)
puts sprintf '%.6f', d(xs, ys, 3)
puts sprintf '%.6f', d(xs, ys, Float::INFINITY)

Measured memory_bytes: 6200
<?php
fscanf(STDIN, "%d", $n);
$aArray = array_map("intval", explode(" ", trim(fgets(STDIN))));

$bArray[0] = 0;
for($i = 1; $i < $n; $i++){
  $bArray[$i] = ($aArray[$i-1] - $bArray[$i-1] / 2) * 2;
}

$bArray[0] = array_sum($aArray) - array_sum($bArray);;
for($i = 1; $i < $n; $i++){
  $bArray[$i] = ($aArray[$i-1] - $bArray[$i-1] / 2) * 2;
}
echo implode(" ", $bArray);

Measured memory_bytes: 20084
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep_bit(n) for (int bit = 0; bit < (1<<n); ++bit)
#define all(x) (x).begin(),(x).end()
#define ll long long
#define ld long double
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define SP <<" "<<
#define CST(x) cout<<fixed<<setprecision(x)
using namespace std;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> Graph;

//条件式 ? 真の場合の文 : 偽の場合の文
//abs(x) = (x の絶対値)
//string S  S,at(i),S += T,S += c,S.size(),S.substr(l)l文字目から最後まで,
///S.substr(l, r)l文字目からl+r-1文字目 n文字目から0個で空列を返せる
//*min_element(all(x)) *max_element(all(x))
//vi x; x.back(),x.front(),x.push_back(),x.pop_back()
//swap(a, b)
//gcd(a, b)最大公約数
//sort(all(x)),reverse(all(x)),sort(x.begin()+l,a.begin()+r)lからrまで
//stack<int> a; a.push(x),a.pop(),a.top(),a.empty()bool
//queue<int> b; b.push(x),b.pop(),b.front(),b.empty()bool,b.size() 要素が小さい順
//priority_queue<int, vector<int>, greater<int>> c,c.top()
//map<int, int> M1; 変数[key] = value; 変数.erase(key); 変数.at(key); 変数.count(key)所属判定　変数.size()
//find(all(x), key)boolソート済みのベクトル
//*lower_bound(all(x),k)以上の最小の要素,*uppper_bound(all(x),k)より大きい最小の要素
//char('0'+cnt) 数字cntからcharへの変換
//int(char-'0') charからintへの変換
int main(){

    int n;
    cin >> n;
    vi l(n);
    rep(i,n){
        cin >> l.at(i);
    }
    sort(all(l));
    int cnt = 0;
    rep(i,n-2)for(int j = i+1; j < n-1; j++){
        auto iter = lower_bound(all(l),l.at(i)+l.at(j));
        int point = iter - l.begin();
        point--;
        cnt += point - j;
    }
    cout << cnt << endl;

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#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++)
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; }

template <int mod>
struct ModInt {
    int val;
    ModInt() : val(0) {}
    ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
    int getmod() { return mod; }
    ModInt &operator+=(const ModInt &p) {
        if ((val += p.val) >= mod) {
            val -= mod;
        }
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if ((val += mod - p.val) >= mod) {
            val -= mod;
        }
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        val = (int)(1LL * val * p.val % mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }
    ModInt operator-() const { return ModInt(-val); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    bool operator==(const ModInt &p) const { return val == p.val; }
    bool operator!=(const ModInt &p) const { return val != p.val; }
    ModInt inverse() const {
        int a = val, b = mod, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }
    ModInt pow(long long n) const {
        ModInt ret(1), mul(val);
        while (n > 0) {
            if (n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }
    friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.val; }
    friend istream &operator>>(istream &is, ModInt &a) {
        long long t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }
    static int get_mod() { return mod; }
};
const int MOD = 1000000007;  // if inv is needed, this shold be prime.
using modint = ModInt<MOD>;

map<ll, modint> res;

//　素因数分解
void prime_factorize(ll prime) {
    if(prime == 1) return;
    ll a = 2;
    while( a*a <= prime){
        if(prime % a == 0){
            res[a] += 1;
            prime /= a;
        }
        else ++a;
    }
    res[prime] += 1;
    return;
}


int main(){
    int n;
    cin >> n;
    modint ans = 1;
    rep2(i, 1, n+1){
        prime_factorize(i);
    }
    for(auto i: res){
        ans *= (i.second+1);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3596
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <cmath>
#include <utility> // pair make_pair
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits> // LLONG_MAX, LLONG_MIN, INT_MIN, INT_MAX
#include <assert.h>

#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
#define int ll
#define INF LLONG_MAX/10
#define MOD 1000000007

using namespace std;
using Pi = pair<int, int>;
using Graph = vector<vector<int> >;
using WeightGraph = vector<vector<Pi> >;

int Input[100100];
vector<int> T;

signed main() {
  int N, K; cin >> N >> K;

  rep(i, N) {
    int x; cin >> x;
    T.push_back(x);
  }
  sort(T.begin(), T.end());
  reverse(T.begin(), T.end());

  // for (auto x: T) {
  //   cout << x << " ";
  // }

  cout << endl;
  int ans = INF;
  rep(i, N) {
    if (i + K - 1 >= N) {
      break;
    } else {
      // cout << "i: " << i << " T[i]: " <<  T[i] <<  " T[i+K-1]: " << T[i+K-1] << endl;
      ans = min(ans, T[i] - T[i+K-1]);
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1400
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
    cin >> n;
    n%=10;
    if(n==2||n==4||n==5||n==7||n==9)
    	cout << "hon";
   	else if(n==3)
    	cout << "bon";
    else 
    	cout << "pon";
    return 0;
}

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

int main(void){
	while(1){
		int h, w;
		cin >> h >> w;
		if(h == 0 && w == 0) break;
		int ansh = 10000000;
		int answ = 10000000;
		int dig = h*h + w*w;

		for(int i=1; i<=150; i++){
			for(int j=i+1; j<=150; j++){
				int tmp = i*i + j*j;
				if(dig < tmp){
					int ansdig = answ*answ + ansh*ansh;
					if(ansdig > tmp){
						//printf("ans%d %d a\n", i, j);
						ansh = i;
						answ = j;
					}else if(ansdig == tmp && ansh > i){
						//printf("ans%d %da b\n", i, j);
						ansh = i;
						answ = j;
					}
				}else if(dig == tmp && h < i){
					int ansdig = answ*answ + ansh*ansh;
					if(ansdig > tmp){
						//printf("ans%d %d\n c", i, j);
						ansh = i;
						answ = j;
					}else if(ansdig == tmp && ansh > i){
						//printf("ans%d %d\n d", i, j);
						ansh = i;
						answ = j;
					}

				}

			}
		}
		printf("%d %d\n", ansh, answ);
	}


	return 0;
}

Measured memory_bytes: 1136
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long LL;


int main(){
//    freopen("test.txt", "r", stdin);
    LL n, m, pre, now;
    scanf("%lld%lld%lld", &n, &m, &pre);
    vector<LL> a(m+2, 0);
    vector<LL> b(m+2, 0);
    for (int i = 1; i < n; i++) {
        scanf("%lld", &now);
        if (now > pre) {
            b[1] += now - pre;
            b[pre + 1] -= now - pre;
            b[now + 1] += now - pre;
            a[pre+1] -= 1;
            a[now+1] += 1;
            b[pre + 1] += now + 1;
            b[now + 1] -= now + 1;
        }
        else { // now < pre
            b[now + 1] += now - pre + m;
            b[pre + 1] -= now - pre + m;
            a[1] -= 1;
            a[now + 1] += 1;
            b[1] += now + 1;
            b[now + 1] -= now + 1;
            a[pre + 1] -= 1;
            b[pre + 1] += now + m + 1;
        }
        pre = now;
    }
    LL ret = a[1] + b[1];
    for (int i = 1; i <= m; i++) {
        a[i] += a[i-1];
        b[i] += b[i-1];
        ret = min(a[i]*(LL)i + b[i], ret);
    }
    cout << ret << endl;
}

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

int main(){
   int H,W;
   
   while(true){
       cin >> H >> W;
       if( H == 0 && W == 0) break;
       for(int i = 1; i <= H ; i++){
           for(int j = 1; j <= W; j++) cout << "#";
           cout << endl;
       }
       cout << endl;
   }
   return 0;
}

Measured memory_bytes: 1152
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;



const int N = 1e6 + 100;

char s[N];
char op[N];
int dir[N];
int main() {
	int n, Q;
	cin >> n >> Q;
	scanf("%s", s + 1);
	char ch[5];
	for(int i = 1; i <= Q; i ++) {
		scanf("%s", ch);
		op[i] = ch[0];
		scanf("%s", ch);
		dir[i] = (ch[0] == 'R');
	}

	int pre_board = 0;
	int suf_board = n + 1;
	int lb = 0, ub = n + 1;
	while(ub - lb > 1) {
		int mid = (lb + ub) / 2;
		int p = mid;
		for(int i = 1; i <= Q; i ++) {
			if(op[i] != s[p]) continue;
			if(dir[i] == 0) p --;
			else p ++;
			if( p < 1 || p > n) break;
		}
		if( p < 1 ) lb = mid;
		else { ub = mid; if(p > n) suf_board = min(suf_board, p); }
	}
	pre_board = max(pre_board, lb);
	
	lb = 0, ub = n + 1;
	while(ub - lb > 1) {
		int mid = (lb + ub) / 2;
		int p = mid;
		for(int i = 1; i <= Q; i ++) {
			if(op[i] != s[p]) continue;
			if(dir[i] == 0) p --;
			else p ++;
			if( p < 1 || p > n) break;
		}
		if( p > n ) ub = mid;
		else { lb = mid; if(p < 1) pre_board = max(pre_board, p); }
	}
	suf_board = min(suf_board, ub);

	cout << max(0, n - (pre_board + (n - suf_board + 1))) << endl;

}


Measured memory_bytes: 3328
import math, bisect
Q = int(input())
N = 10**5 + 1
P = set([2])
for k in range(3,N+1,2):
    f = 1
    for l in range(2,int(math.sqrt(k))+1):
        if k % l == 0:
            f = 0
            break
    if f==1:
        P.add(k)

R = []
for k in range(1,N,2):
    if k in P:
        if (k+1)//2 in P:
            R.append(k)

for _ in range(Q):
    l, r = map(int,input().split())
    print(bisect.bisect_right(R,r)-bisect.bisect_left(R,l))


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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  int a,b,c;
   cin >> a >> b >> c;
   cout << (a<=c&&c<=b?"Yes":"No") << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  string s;
  cin>>s;
  int ans = 0;
  for(int i = 1 ; i < n ; i ++){
  	if(s[i] != s[i-1])ans++;
  }
  ans++;
  cout<<ans<<endl;
}

Measured memory_bytes: 512
#include<stdio.h>

int main()
{
	int x;
	int y;
	while(scanf(" %d %d", &x, &y)){
		
		if(x == 0 && y == 0)break;
		
		if(x < y){
			printf("%d %d\n", x, y);
		}else{
			printf("%d %d\n", y, x);
		}
	}
	
	return 0;
}

Measured memory_bytes: 600
import math
import heapq
import sys
from collections import Counter
import datetime
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

#N,K=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

N=int(input())
W=[input() for i in range(N)]



for i in range(N-1):
    if W[i][-1]!=W[i+1][0] or W.count(W[i])>1:
        print('No')
        sys.exit()

print('Yes')

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

int main(void){
	int a[10000],max=0,mod=0,x,i;
	for(i=0;i<10000;i++)a[i]=0;
	while(scanf("%d",&x)!=EOF){
		if(x>max)max=x;
		a[x]++;
		if(a[x]>mod)mod=a[x];
	}

	for(i=0;i<=max;i++){
		if(a[i]==mod)printf("%d\n",i);
	}
	return 0;
}

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


int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N), F(N);
  ll totalA = 0;
  REP(i,N) {
    cin >> A[i];
    totalA += A[i];
  }
  REP(i,N) {
    cin >> F[i];
  }

  if ( totalA <= K ) {
    cout << "0" << endl;
    return 0;
  }

  sort( A.begin(), A.end() );
  sort( F.begin(), F.end(), greater<ll>() );

  ll smin_l = 1LL << 60;
  ll smin_s = -1;
  ll smin;

  while(smin_l-smin_s > 1) {
    smin = (smin_l + smin_s) / 2;
    ll k0 = K;
    bool ok=true;
//    printf("%lld\n",smin);
    REP(i,N) {
      ll dif = A[i] * F[i] - smin;
//      printf(" %d : %lld\n",i,dif);
      if ( dif > 0 ) {
        k0 -= A[i] - smin / F[i];
      }
      if ( k0 < 0 ) {
        ok = false;
        break;
      }
    }
    if (ok) smin_l = smin;
    else smin_s = smin;
//    printf(" - %lld - %lld - %lld -\n",smin_s,smin,smin_l);
  }

  cout << smin_l << endl;
}


Measured memory_bytes: 3328
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO.Compression;
using System.Linq;
using System.Text;

namespace Atcoder
{
    class Program
    {
        private const long COMDIV = 1000000007;
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            Dictionary<int,List<int>> graph = new Dictionary<int,List<int>>();

            for (int i = 0; i < N; i++)
            {
                var ukv = Console.ReadLine().Split().Select(int.Parse).ToArray();
                var u = ukv[0];
                var k = ukv[1];
                graph[u] = new List<int>();
                for (int j = 0; j < k; j++)
                {
                    graph[u].Add(ukv[2 + j]);
                }
            }

            foreach (var v in graph)
            {
                int[] rowRet = new int[N];
                foreach (var item in v.Value)
                {
                    rowRet[item - 1] = 1;
                }

                Console.WriteLine(string.Join(" ",rowRet));
            }
        }
    }
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
  os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

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

    ll N;
    cin >> N;
    vector<ll> P(N);
    REP(i, N) cin >> P[i];
    vector<pll> Q(N);
    REP(i, N){
    	Q[i].first = P[i];
    	Q[i].second = i;
    }
    sort(Q.rbegin(), Q.rend());
    multiset<ll> st;
    st.insert(-1);
    st.insert(-1);
    st.insert(N);
    st.insert(N);
    ll res = 0;
    REP(i, N){
    	st.insert(Q[i].second);
    	auto itr = st.lower_bound(Q[i].second);
    	--itr;--itr;
    	vector<ll> tmp(5);
    	REP(j, 5){
    		tmp[j] = *itr;
    		++itr;
    	}
    	ll kosu = (tmp[1] - tmp[0]) * (tmp[3] - tmp[2]) + (tmp[2] - tmp[1]) * (tmp[4] - tmp[3]);
    	res += kosu * Q[i].first;
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 7296
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b<a) {a=b; return 1;} return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    vb ans(n, false);
    vi cnt(n, 1);
    ans[0] = true;
    rep(i, m) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        if (ans[x]) {
            ans[y] = true;
            if (cnt[x] == 1) ans[x] = false;
        }
        cnt[x]--;
        cnt[y]++;
    }
    cout << accumulate(ans.begin(), ans.end(), 0) << endl;
}

Measured memory_bytes: 640
/*
  AUTHOR:SOURABH
  CREATED:01:05:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<pll , null_type, less< pll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 1000000007
#define MAX 100005
#define M 32

using namespace std;
using namespace __gnu_pbds;

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

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll t,n,i,j,sum,ans;
    string s;
    t=1;
    //cin>>t;
    W(t--)
    {
        cin>>s;
        if(s=="ARC")
            cout<<"ABC";
        else
            cout<<"ARC";
    }
    return 0;
}



















Measured memory_bytes: 3652
#include <cstdio>
#include <cstring>
using namespace std;

typedef int INT;
const int SIZE = 4194304;
bool HA[SIZE];

inline INT get( char c ) {
    char T[5] = "ACGT";
    for ( INT i = 0; i < 4; ++ i ) {
        if ( c == T[i] ) return i + 1;
    }
    return 0;
}

int main() {
    INT n, sum, t, c;
    while ( scanf("%d", &n) != EOF ) {
        memset( HA, false, sizeof(HA) );
        while ( getchar() != '\n' );
        for ( register INT i = 0; i < n; ++ i ) {
            sum = 0;
            t = 1;
            bool flag = getchar() == 'i';
            while ( ( c = getchar() ) != ' ' );
            while ( ( c = get( getchar() ) ) != 0 ) {
                sum += c * t;
                t *= 4;
            }
            if ( flag ) {
                HA[sum] = true;
            } else {
                puts( HA[sum] ? "yes" : "no" );
            }
        }
    }
    return 0;
}

Measured memory_bytes: 4828
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 200005
#define lint long long
#define trace(x) cout<<#x<<" = "<<x<<'\n'
#define trace2(x, y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<'\n'
#define tracea(arr, i) cout<<#arr<<"["<<i<<"]"<<" = "<<arr[i]<<'\n'
#define print(arr, n) for (int iDx = 0; iDx < (n); iDx++) cout << arr[iDx] << " \n"[iDx == (n)-1]
#define sz(x) (int)(x).size()
using namespace std;
struct ii {
	int a, b;
	bool operator < (ii o) const { return make_tuple(a, b) < make_tuple(o.a, o.b); }
};
struct iii {
	int a, b, c;
	bool operator < (iii o) const { return make_tuple(a, b, c) < make_tuple(o.a, o.b, o.c); }
};
int TC, N, M, K, Q, x, y, z;

bitset<MAXN> bisa[2];

int main() {
	#ifdef FAIRUZI10
	freopen("/home/fairuzi/Documents/CP/input.in", "r", stdin);
	#endif
	
	scanf("%d%d", &N, &M);
	for (int i = 0; i < M; i++) {
		scanf("%d%d", &x, &y);
		if (x == 1) bisa[0][y] = 1;
		if (y == N) bisa[1][x] = 1;
	}
	for (int i = 1; i <= N; i++) {
		if (bisa[0][i] && bisa[1][i]) {
			puts("POSSIBLE");
			return 0;
		}
	}
	puts("IMPOSSIBLE");
	
	return 0;
}


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

const double PI = acos(-1.0);
int main() {
    int A,B,H,M; cin >> A >> B >> H >> M;
    double degree = 30*H + M*0.5 - 6*M;
    double ans = sqrt(pow(A, 2) + pow(B, 2) - 2*A*B*cos(degree*PI/180));
    cout << fixed << setprecision(10) << ans << endl;
}

Measured memory_bytes: 4104
import java.awt.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[] firstT = new int[100001];
        int[] secondT = new int[100001];

        int partLength = n / 2;

        for (int i = 0; i < partLength; i++) {
            firstT[sc.nextInt()]++;
            secondT[sc.nextInt()]++;
        }

        ArrayList<Point> firstL = new ArrayList<>();
        ArrayList<Point> secondL = new ArrayList<>();

        for (int i = 1; i <= 100000; i++) {
            if (firstT[i] > 0) {
                firstL.add(new Point(i, partLength -  firstT[i]));
            }
            if (secondT[i] > 0) {
                secondL.add(new Point(i, partLength -  secondT[i]));
            }
        }
        firstL.sort(Comparator.comparingInt(a -> a.y));
        secondL.sort(Comparator.comparingInt(a -> a.y));

        long ans = Long.MAX_VALUE;

        for(Point f : firstL) {
            for (Point s : secondL) { 
                boolean isSame =  f.x == s.x;
                ans = Math.min(ans, isSame ? f.y + partLength : f.y + s.y);
                if (!isSame) { 
                    break;
                }
            }
        }

        System.out.println(ans);
    }
}

Measured memory_bytes: 53784
#include <bits/stdc++.h>
#define maxk 100
#define maxn 10001
#define mod 1000000007
using namespace std;

int n,k;
string s;
int dp[maxn][maxk][2];

int f( int curr , int sum , int type ) {
	if( curr == n )
		return sum == 0;
	if( dp[curr][sum][type] != -1 )
		return dp[curr][sum][type];
	dp[curr][sum][type] = 0;
	int val = s[curr] - '0';
	if( type == 1 ) {
		for( int i = 0 ; i <= 9 ; i++ )
			dp[curr][sum][type] = ( dp[curr][sum][type] + f( curr+1 , (sum+i)%k , 1 ) ) % mod;
	}
	else {
		for( int i = 0 ; i < val ; i++ )
			dp[curr][sum][type] = ( dp[curr][sum][type] + f( curr+1 , (sum+i)%k , 1 ) ) % mod;
		dp[curr][sum][type] = ( dp[curr][sum][type] + f( curr+1 , (sum+val)%k , 0 ) ) % mod;
	}
	return dp[curr][sum][type];
}

int main() {
	cin >> s >> k;
	n = s.length();
	memset( dp , -1 , sizeof(dp) );
	cout << ( f( 0 , 0 , 0 ) - 1 + mod ) % mod;
}


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

int main() {

    int N;
    cin >> N;
    map<string, int> M;

    rep(i, N) {
        string s;
        cin >> s;
        M[s]++;
    }

    int ma = 0;
    for (auto itr = M.begin(); itr != M.end(); itr++) {
        ma = max(ma, itr->second);
    }

    for (auto itr = M.begin(); itr != M.end(); itr++) {
        if (ma == itr->second)
            cout << itr->first << endl;
    }

    return 0;
}


Measured memory_bytes: 25216
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 5;

int main()
{
  int n, m;
  cin >> n >> m;
  int maxl = 0;
  int minr = INT_MAX;
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    l--;

    maxl = max(maxl, l);
    minr = min(minr, r);
  }
  cout << max(0, minr - maxl) << endl;
  return 0;
}

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

using namespace std;

#define LL long long
#define N 30000

const LL mod=998244353;
LL n,s1,s2,ans,fac[N],ifac[N],f[10005][5005];
char a[N],b[N];

void upd(LL &x,LL y){x=(x+y)%mod;}

LL sqr(LL x){return x*x%mod;}

LL C(LL x,LL y){return fac[x]*ifac[y]%mod*ifac[x-y]%mod;}

LL inv(LL x){return x==1?1:(mod-mod/x)*inv(mod%x)%mod;}

int main(){
	scanf("%s%s",a+1,b+1); n=strlen(a+1);
	fac[0]=1; for (LL i=1;i<=n*2;++i) fac[i]=fac[i-1]*i%mod;
	ifac[n*2]=inv(fac[n*2]); for (LL i=n*2-1;i>=0;--i) ifac[i]=ifac[i+1]*(i+1)%mod;
	for (LL i=1;i<=n;++i)
		if (a[i]=='1'&&b[i]=='1') ++s1;
		else if (a[i]=='1'||b[i]=='1') ++s2;
	s2/=2; f[s1][s2]=1;
	for (int i=s1;i>=0;--i)
		for (int j=s2;j>=0;--j){
			if (i>0) upd(f[i-1][j],f[i][j]*i%mod*j%mod);
			if (j>0) upd(f[i][j-1],f[i][j]*sqr(j)%mod);
		}
	for (int i=0;i<=s1;++i)
		upd(ans,f[i][0]*sqr(fac[i])%mod*C(s1+s2,i)%mod);
	printf("%lld\n",ans);
	
	return 0;
}
			

Measured memory_bytes: 389888
#include <iostream>
#include <cmath>

using namespace std;

#define NMAX 100

struct S1{
	int wx, wy, r;
};

/*点と円 true->点は円の内部*/
bool po_cr(int cx, int cy, int cr, int px, int py){
	return pow((cx - px), 2.0) + pow((cy - py), 2.0) <= pow(cr, 2.0);
}

/*円と線 true->接している*/
bool li_cr(int cx, int cy, int cr,int lx1, int ly1, int lx2, int ly2){

	double A, B, C, CA, AS, BS;
	double d;

	A = pow(cx - lx1, 2.0) + pow(cy - ly1, 2.0);
	B = pow(cx - lx2, 2.0) + pow(cy - ly2, 2.0);
	C = pow(lx1 - lx2, 2.0) + pow(ly1 - ly2, 2.0);
	CA = (lx2 - lx1) * (cy - ly1) - (cx - lx1) * (ly2 - ly1);
	AS = (cx - lx1) * (lx2 - lx1) + (cy - ly1) * (ly2 - ly1);
	BS = (cx - lx2) * (lx2 - lx1) + (cy - ly2) * (ly2 - ly1);

	if(C != 0) d = pow(CA, 2.0) / C;
	else d = 0;

	if(d <= pow(cr, 2.0)){
		if(AS * BS <= 0){
			return 1;
		}
		else if(pow(cr, 2.0) > A || pow(cr, 2.0) > B){
			return 1;
		}
	}
	return 0;
}

int main(){

	int n, m;
	S1 wall[NMAX];
	int tx, ty, sx, sy;
	bool f;

	while(1){

		cin >> n;
		if(n == 0) break;

		for(int i = 0; i < n; i++){
			cin >> wall[i].wx >> wall[i].wy >> wall[i].r;
		}

		cin >> m;

		for(int i = 0; i < m; i++){

			cin >> tx >> ty >> sx >> sy;

			f = true;
			for(int j = 0; j < n; j++){
				if(!po_cr(wall[j].wx, wall[j].wy, wall[j].r, sx, sy)){
					if(li_cr(wall[j].wx, wall[j].wy, wall[j].r, tx, ty, sx, sy)){
						f =  false;
					}
				}
				else if(!po_cr(wall[j].wx, wall[j].wy, wall[j].r, tx, ty)){
					f = false;
				}
			}

			if(f) cout << "Danger" << endl;
			else cout << "Safe" << endl;
		}
	}

}

Measured memory_bytes: 1156
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << "{" <<p.first << ", " << p.second << "}";
  return os;
}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

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

    ll N;
    cin >> N;
    vector<ll> A(N), B(N);
    rep(i,N){
        cin >> A[i];
    }
    rep(i,N){
        cin >> B[i];
    }
    ll sumA = accumulate(all(A), 0LL);
    ll sumB = accumulate(all(B), 0LL);
    if(sumA < sumB){
        cout << -1 << endl;        
        return 0;
    }
    //できる
    vector<ll> d(N);
    rep(i,N)d[i] = A[i] - B[i];
    sort(all(d));
    auto pre_d = d;
    int r = N - 1;
    rep(l, N){
        while(d[l] < 0){
            ll pre_l = d[l];
            ll pre_r = d[r];
            d[l] = min(0LL, pre_l+pre_r);
            d[r] = max(0LL, pre_l+pre_r);
            if(d[r] == 0)r--;
        }
    }
    ll ans = 0;
    rep(i,N)ans += d[i] != pre_d[i];
    cout << ans << endl;
}


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

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

#define     str             string
#define     PB              push_back
#define     FOR(i,a,n)      for(int i=a;i<=n;i++)
#define     FORA(i,a,n)     for(int i=a;i<n;i++)
#define     srt(a)          sort(a.begin(),a.end())
#define     rev(a)          reverse(a.begin(),a.end())

#define     c1(a)           cin>>a
#define     c2(a,b)         cin>>a>>b
#define     c3(a,b,c)       cin>>a>>b>>c
#define     c4(a,b,c,d)     cin>>a>>b>>c>>d

#define     p(a)            cout<<a<<endl

#define     sci1(a)         scanf("%d",&a)
#define     sci2(a,b)       scanf("%d %d",&a,&b)
#define     sci3(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define     sci4(a,b,c,d)   scanf("%d %d %d %d",&a,&b,&c,&d)

#define     pri1(a)         printf("%d\n",a)
#define     pri2(a,b)       printf("%d %d\n",a,b)
#define     pri3(a,b,c)     printf("%d %d %d\n",a,b,c)
#define     pri4(a,b,c,d)   printf("%d %d %d %d\n",a,b,c,d)

#define     intah           int a,b,c,d,e,f,g,h,n

#define     pi              acos(-1)

typedef     long long int   ll;
typedef     vector<int>     vi;
typedef     vector<str>     vs;
typedef     pair<ll,ll>     paii;
typedef     pair<ll,str>    pais;

intah;

int main(){
    sci1(a);

    cout<<3*pow(a,2)<<endl;
}


Measured memory_bytes: 256
from collections import defaultdict
s = input()
d = defaultdict(lambda: 0)
for c in s:
   d[c] += 1

if d["N"] > 0:
   if d["S"] == 0:
      print("No")
      exit()
if d["S"] > 0:
   if d["N"] == 0:
      print("No")
      exit()
if d["W"] > 0:
   if d["E"] == 0:
      print("No")
      exit()
if d["E"] > 0:
   if d["W"] == 0:
      print("No")
      exit()

print("Yes")

Measured memory_bytes: 65088
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<b)return 0; a=b; return 1;}



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

    string s;
    cin>>s;
    int n = s.size();
    bool pos = true;
    for(int i=0;i<n;i++){
        if(s[i] != s[n-1-i])pos=false;
    }
    for(int i=0;i<(n-1)/2;i++){
        if(s[i] != s[(n-1)/2 - 1 - i]) pos = false;
    }
    for(int i=(n+3)/2-1;i<n;i++){
        if(s[i] != s[n - i + (n+3)/2-1 -1])pos=false;
    }
    if(pos)cout << "Yes"<<endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mpp make_pair
using namespace std;

int main()
{
	ll a,b;
	cin>>a>>b;

	if(a>=1 && a<=9 && b>=1 && b<=9)
		cout<<a*b;
	else
		cout<<-1;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<string.h>
#include <iomanip>
#include <sstream>
using namespace std;

int main() {
  int N;
  bool ans = true;

  cin >> N;
  vector<int> a(N);
  for(int i = 0; i < N; i++){
    cin >> a[i]; 
  }
  for(int i = 0; i < N; i++){
    if(a[i] % 2 == 0){
      if(a[i] % 3 != 0 && a[i] % 5 != 0){
        ans = false;
      }
    }
  }
  if(ans){
    cout << "APPROVED" << endl;
    return 0;
  }else{
    cout << "DENIED" << endl;
    return 0;
  }
}


Measured memory_bytes: 256
package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scan(&s)
	// sの末尾から一致する単語を消していく
	ans := false
	for {
		var tmp = s
		l := len(tmp)
		switch {
		case 0 == l:
			ans = true
			break
		case 6 < l:
			if tmp[len(tmp)-7:] == "dreamer" {
				tmp = tmp[:len(tmp)-7]
				s = tmp
				continue
			}
			fallthrough
		case 5 < l:
			if tmp[len(tmp)-6:] == "eraser" {
				tmp = tmp[:len(tmp)-6]
				s = tmp
				continue
			}
			fallthrough
		case 4 < l:
			if tmp[len(tmp)-5:] == "dream" || tmp[len(tmp)-5:] == "erase" {
				tmp = tmp[:len(tmp)-5]
				s = tmp
				continue
			}
			fallthrough
		default:
			break
		}
		if tmp == s {
			break
		}
	}
	if ans {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}


Measured memory_bytes: 1280
n = int(input())


def dfs(x):
  if int(x) > n:
    return 0
  ret = 0
  if all(x.count(c) >= 1 for c in '753'):
    ret += 1
  for c in '753':
    ret += dfs(x + c)

  return ret


print(dfs('0'))


Measured memory_bytes: 2940
#include <stdio.h>
 
int main(void){
	int MAX;
	int MIN;
	long long int ALL=0;
	int n,a;
	int i;
	scanf("%d",&n);
	for(i=0;i<n;i++){
	scanf("%d",&a);
	if(i==0){
		MAX=a;MIN=a;//初期
	}
	if(a>MAX){
		MAX=a;//aがMAXより大きいとき
	}
		if(a<MIN){
		MIN=a;//aがMINより小さいとき
	}
	ALL+=a;
	}
	printf("%d %d %lld\n",MIN,MAX,ALL);
    return 0;
}


Measured memory_bytes: 2120
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cout << #x << " = " << (x) << endl;
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; }
#define int long long

//どう辺をはって、最短経路問題を考えるか
void solve() {
	int n, m; cin >> n >> m;
    vector<P> pairs;
    rep(i, n) {
        pairs.pb(P(i, -1)); //別の点を作る
    }
    int p[200200], q[200200], c[200200];
    rep(i, m) {
        cin >> p[i] >> q[i] >> c[i];
        --p[i]; --q[i];
        pairs.pb(P(p[i], c[i]));
        pairs.pb(P(q[i], c[i]));
    }

    sort(all(pairs));
    pairs.erase(unique(all(pairs)), pairs.end());
    int sz = pairs.size();
    map<P, int> mp; //逆引き
    rep(i, sz) {
        mp[pairs[i]] = i;
    }

    vector<vector<P>> g(sz);
    //駅での乗り換え
    rep(i, sz) {
        if(pairs[i].sc == -1) continue;
        int station = pairs[i].fr;
        int out = mp[P(station, -1)];
        g[i].pb(P(out, 0));
        g[out].pb(P(i, 1)); //別に余分に辺を張る分には構わない
    }
    //路線間の移動
    rep(i, m) {
        int s = mp[P(p[i], c[i])], t = mp[P(q[i], c[i])];
        g[s].pb(P(t, 0));
        g[t].pb(P(s, 0));
    }
    int start = mp[P(0, -1)], goal = mp[P(n - 1, -1)];
    vector<int> dist(sz, INF);
    deque<int> deq;
    deq.push_back(start);
    dist[start] = 0;
    while(deq.size()) {
        int x = deq.front(); deq.pop_front();
        for(auto& y: g[x]) {
            int to = y.fr, cost = y.sc;
            if(dist[x] + cost < dist[to]) { //01BFSでは、暫定より短いものが得られたなら更新
                if(cost == 0) {
                    dist[to] = dist[x];
                    deq.push_front(to);
                } else {
                    dist[to] = dist[x] + 1;
                    deq.push_back(to);
                }
            }
        }
    }
    cout << (dist[goal] == INF ? -1 : dist[goal]) << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
    return 0;
}

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

class Mo{
    public:
    int N,Q,ans=0;
    struct query{int l,r;};
    vector<int>data,order;
    vector<query>q;

    vector<int>cnt;
    void add(int idx){ans+=(++cnt[data[idx]]==1);}
    void del(int idx){ans-=(--cnt[data[idx]]==0);}

    Mo(int N,int Q):N(N),Q(Q),data(N+1),order(Q),q(Q),cnt(N+1,0){}

    void input_data(){for(int i=0;i<N;i++)scanf("%d",&data[i]);}

    /*[l, r) 0-idx*/
    void input_query(){for(int i=0;i<Q;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].l--;}}

    vector<int>solve(){
        //iota(begin(order), end(order), 0);
        for(int i=0;i<Q;i++)order[i]=i;

        //sort query
        sort(order.begin(),order.end(),
        [&](const int a,const int b){
            return ((q[a].l/(int)sqrt(N))!=(q[b].l/(int)sqrt(N))?q[a].l<q[b].l:q[a].r<q[b].r);
        });

        vector<int>answer(Q);//answer for query
        int L,R,i;
        for(L=R=i=0;i<Q;i++){
            //add
            while(L>q[order[i]].l)add(--L);
            while(R<q[order[i]].r)add(R++);
            //del
            while(L<q[order[i]].l)del(L++);
            while(R>q[order[i]].r)del(--R);
            answer[order[i]] = ans;
        }
        return answer;
    }
};

int N,Q,l,r;
int main(){
    scanf("%d%d",&N,&Q);
    Mo mo(N,Q);
    mo.input_data();
    mo.input_query();
    auto ans=mo.solve();
    for(int i=0;i<Q;i++)printf("%d\n",ans[i]);
    return 0;
}

Measured memory_bytes: 15484
n = int(input())
a_l = [ int(input()) for _ in range(n) ]

# N 1
ans = 0
next_idx = 0
for _ in range(n):
    ans += 1
    next_idx = a_l[next_idx]-1
    if next_idx == 1:
        break
if next_idx != 1:
    ans = -1
print(ans)

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

#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, s, e) for (ll i = s; i < e; i++)
#define Forr(i, s, e) for (ll i = s; i > e; i--)
#define pb push_back
#define ll long long
#define mp make_pair
int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout << fixed << setprecision(8);
    ll n;
    cin >> n;
    ll h[n];
    ll cost[n];
    For(i, 0, n)
    {
        cin >> h[i];
        cost[i] = 0;
    }
    cost[1] = abs(h[1] - h[0]);
    For(i, 2, n)
    {
        ll a = abs(h[i] - h[i - 1]) + cost[i - 1];
        ll b = abs(h[i] - h[i - 2]) + cost[i - 2];
        cost[i] = min(a, b);
    }
    cout << cost[n - 1] << endl;
    return 0;
}

Measured memory_bytes: 5152
#include <bits/stdc++.h>
#define rep(i, m, n) for(int i = m; i < (n); i++)
#define rrep(i,m,n) for(int i = m; i >= n; i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(int i = 0; i < n; i++){ cout << (x[i]) << " ";} cout << endl;
#define printa2(x,m,n) for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cout << x[i][j] << " ";} cout << endl;}
#define printp(x,n) for(int i = 0; i < n; i++){ cout << "(" << x[i].first << ", " << x[i].second << ") "; } cout << endl;
#define INF (1e18)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
typedef struct{
    ll to;
    ll cost;
} edge;
typedef pair<ll, ll> lpair;
ll N,K;
ll a[55][55];
ll ans[55][55] = {};
ll tmp[55][55] = {};
void multiple1(){
    rep(i,0,N){
        rep(j,0,N){
            tmp[i][j] = 0;
        }
    }

    rep(i,0,N){
        rep(j,0,N){
            rep(k,0,N){
                tmp[i][j] += a[i][k] * ans[k][j] % MOD;
                tmp[i][j] %= MOD;
            }
        }
    }
    rep(i,0,N){
        rep(j,0,N){
            ans[i][j] = tmp[i][j];
        }
    }
}
void multiple2(){
    rep(i,0,N){
        rep(j,0,N){
            tmp[i][j] = 0;
        }
    }

    rep(i,0,N){
        rep(j,0,N){
            rep(k,0,N){
                tmp[i][j] += a[i][k] * a[k][j] % MOD;
                tmp[i][j] %= MOD;
            }
        }
    }
    rep(i,0,N){
        rep(j,0,N){
            a[i][j] = tmp[i][j];
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> K;
    rep(i,0,N){
        rep(j,0,N){
            cin >> a[i][j];
        }
    }
    rep(i,0,N){
        ans[i][i] = 1;
    }

    rep(i,0,61){
        if((K>>i) & 1){
            multiple1();
        }
        multiple2();
    }
    ll cnt = 0;
    rep(i,0,N){
        rep(j,0,N){
            cnt += ans[i][j] % MOD;
            cnt %= MOD;
        }
    }
    print(cnt);

}


Measured memory_bytes: 384
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};

int N,H,W;
const int L=1e5+10;
ll R[L+1],C[L+1],A[L+1];

int main(){
  cin>>N>>H>>W;
  for(int i=0;i<N;i++) cin>>R[i]>>C[i]>>A[i];

  vector<tuple<ll,int,int>> v;
  for(int i=0;i<N;i++) v.push_back(mkt(A[i],R[i],C[i]));
  sort(v.begin(),v.end());
  reverse(v.begin(),v.end());

  DisjointSet us(H+W+1);
  ll ans=0;
  for(int i=0;i<N;i++){
    ll c,a,b;
    c=get<0>(v[i]);
    a=get<1>(v[i]);
    b=get<2>(v[i]);

    b+=H;
    if(us.same(a,b)){
      if(us.same(0,a)) continue;
      else{
        us.unite(0,a);
        ans+=c;
      }
    }else{
      us.unite(a,b);
      ans+=c;
    }
  }

  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 6512
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}

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

  ll K ; cin >> K;
  vector<ll> A;

  ll l = 0, r = 0;
  ll a , b;
  rep(i,K){
    ll inp; cin >> inp;
    r+=inp;
    A.pb(inp);
  }
  r = 10e14;
  a = l; b = r;
  while(b-a>1){
    ll t = (a+b)/2;

    rep(i,K) {
      ll sub = t%A[i];
      t-=sub;
    }
    if(t<=2)a=((a+b)/2);
    else b=(a+b)/2;
  }
  ll M = a;

  a = l; b = r;
  while(b-a>1){
    ll t = (a+b)/2;

    rep(i,K) {
      ll sub = t%A[i];
      t-=sub;
    }
    if(t>=2)b=((a+b)/2);
    else a=(a+b)/2;  
  }
  ll m = b;

  if(m>M) cout <<"-1" << endl;
  else cout << m << " " << M << endl;
  //cout << fixed << setprecision(16) << ans << endl;
  return 0;
}


Measured memory_bytes: 1528