akhauriyash/Code-Regression huggingface dataset:
//#include "SaveFuncs.cpp"

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <iomanip>	//cout << fixed << setprecision(桁数);
#include <stdexcept>

using namespace std;
using ll = long long;

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep1(i, n) for(int i=1, i##_len=(n); i<i##_len; i++)
#define rrep(i, n) for(int i=(n)-1; i >= 0; i--)
#define rrep1(i, n) for(int i=(n); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b);
//#define int long long
constexpr int INF = 2000000100;
constexpr int DIV = 1000000007;	// 10e9+7
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> T divUp(T a, T b) { if (a % b == 0) { return a / b; } return a / b + 1; }
string strMulti(string t, int n) { string out = ""; for (int i = 0; i < n; i++) { out += t; } return out; }

/*****/

int Main()
{
	int A, B, C;
	cin >> A >> B >> C;

	int ret = 0;
	ret = C - (A - B);
	if (ret < 0)
		ret = 0;
	cout << ret << endl;

	return 0;
}

/*****/

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

	Main();

	return 0;
}


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

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1;
	for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
	return x * f;
}

const int N = 3005;
int C[N][N], S[N][N], n, P;

int Pow(int x, int t, int P = P)
{
	int res = 1;
	for (; t; t >>= 1, x = 1LL * x * x % P)
		if (t & 1) res = 1LL * res * x % P;
	return res;
}

void init(int n)
{
	for (int i = 0; i <= n; ++i)
	{
		C[i][0] = C[i][i] = 1;
		for (int j = 1; j < i; ++j)
			C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % P;
	}
	
	S[0][0] = 1;
	for (int i = 1; i <= n; ++i)
	{
		S[i][1] = 1;
		for (int j = 1; j <= i; ++j)
			S[i][j] = (S[i - 1][j - 1] + 1LL * j * S[i - 1][j]) % P;
	}
}

int main()
{
	n = read(); P = read();
	init(n + 1);
	
	int ans = 0;
	for (int i = 0; i <= n; ++i)
	{
		int t = 1LL * Pow(2, Pow(2, n - i, P - 1)) * C[n][i] % P;
		int b = Pow(2, n - i);
		int res = 1;
		
		for (int j = 1, c = b; j <= i; ++j)
		{
			res = (res + 1LL * S[i + 1][j + 1] * c % P) % P;
			c = 1LL * c * b % P;
		}
		
		res = 1LL * res * t % P;
		if (i & 1) ans = (ans - res + P) % P;
			else ans = (ans + res) % P;
	}
	
	printf("%d\n", ans);
	
	return 0;
}


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

int H;
int x[101];
int y[101];
int h[101];
int n,m,K;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> n;
    rep(i,n){
        cin >> x[i] >> y[i] >> h[i];
        if(h[i]) m = i;
    }
    
    rep(i,101){
        rep(j,101){
            H = abs(x[m]-i) + abs(y[m]-j) + h[m];
            rep(k,n){
                if(h[k] != max(0,H-abs(x[k]-i)-abs(y[k]-j))) break;
                K = k;
            }
            if(K == n - 1){
                cout << i << " " << j << " " << H << endl;
                return 0;
            }
        }
    }
}

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

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i, start, end) for(int i = start; i < end; i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << (x) << endl;
#define INF (1e9)

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

typedef long long ll;

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

    int a, b, c;
    cin >> a >> b >> c;

    cout << min(a + b, min(a + c, b + c)) << endl;
}

Measured memory_bytes: 256
H,W=map(int,input().split())
A=[input() for _ in range(H)]

ans='Possible'
i=1
while i<H:
    j=1
    while j<W:
        if A[i][j-1]=='#' and A[i-1][j]=='#':
            ans='Impossible'
        j+=1
    i+=1

print(ans)

Measured memory_bytes: 9108
#include <bits/stdc++.h>
#define boost_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define num(a) a-'0'
#define maxs(a,b) if(b>a)a=b
#define mins(a,b) if(b<a)a=b;
#define debug(x) cout<<#x<<":"<<x<<" "
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define vl vector<long long int>
#define v vector
#define p pair
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define ln "\n"	
typedef long long int ll;
typedef long double ld;
typedef double d;
using namespace std;
ll modF=1e9+7;
ll INF=1e18;

void solve(){
	int n;
	scanf("%d",&n);
	vi a(n);
	vi c(n);
	for(int i=0;i < n;i++){
		scanf("%d",&a[i]);
		c[i]=a[i];
	}
	sort(all(c));
	double med = ((double)(c[n/2 - 1] + c[n/2]))/2;
	for(int i = 0;i < n; i ++){
		if(a[i] < med){
			printf("%d\n",c[n/2]);
		}
		else{
			printf("%d\n",c[n/2-1]);
		}
	}
}

int main(){
	boost_io;
	solve();
	return 0;
}

Measured memory_bytes: 4840
import java.util.Scanner;
import java.util.*;


public class Main {
    
    public static void main(String[] args) {
        
        Scanner entrada=new Scanner(System.in);
        
      int count=0;
        int x[]=new int[3];
        int x2[]=new int[3];
        
        for (int i = 0; i < 3; i++) {
            x[i]=entrada.nextInt();
        }
        for (int i = 0; i <2; i++) {
          for (int j = i+1; j<3; j++) {
            x2[count]=x[i]+x[j];
     count++;
          }
          
        }
     
      
        Arrays.sort(x2);
        System.out.println(x2[0]);
    }
    
}


Measured memory_bytes: 21844
n = int(input())
frames = list(map(int, input().split()))

sum = 0
for i in frames:
    sum += i

mean = sum/len(frames)

diff_lists = []
for j in frames:
    diff = j - mean
    diff_lists.append(abs(diff))

tmp_lists = diff_lists.copy()
tmp_lists.sort()
ans = diff_lists.index(tmp_lists[0])

print(ans)

Measured memory_bytes: 9192
#include <bits/stdc++.h>
using namespace std;
 
#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
 
template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
 
int dp[35][5100];
lli score[35][5100];
int loopCnt[5100];
int P[5100];
int C[5100];
 
void func(int N, int K){
 
    lli ans = -INF;
    REP(i,0,N){
        dp[0][i] = P[i];
        score[0][i] = C[P[i]];
        chmax(ans,score[0][i]);
    }
 
    REP(i,0,33){
        REP(j,0,N){
            dp[i+1][j] = dp[i][dp[i][j]];
            score[i+1][j] = score[i][dp[i][j]]+score[i][j];
        }
    }
 
    REP(i,0,N){
        int now = P[i];
        int cnt = 1;
        lli scoreS = C[now];
        if(cnt<=K)chmax(ans,scoreS);
        while(now!=i){
            now = P[now];
            scoreS += C[now];
    
            cnt++;
            if(cnt<=K)chmax(ans,scoreS);
        }
        loopCnt[i]=cnt+3;
    }
 
        REP(j,0,N){
            REP(i,max(1,K-loopCnt[j]),K+1){
                
                lli sum = 0;
                int now = j;
                REP(k,0,33){
                    if(i&(1LL<<k)){
                        //now = dp[k][now];
                        sum += score[k][now];
                        now = dp[k][now];
                        
                        chmax(ans,sum);
                    }
                }
            }
        }
    
    cout<<ans<<endl;
 
}
 
int main(){
    // cout << fixed << setprecision(5);
 
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    for(int i = 0 ; i < N ; i++){
        scanf("%d",&P[i]);
        P[i]--;
    }
    for(int i = 0 ; i < N ; i++){
        scanf("%d",&C[i]);
    }
    func(N, K);
    return 0;
}

Measured memory_bytes: 5860
n, x = map(int, input().split())
A = list(map(int, input().split()))[::-1]*2
B = A[:n]
ans = float("inf")
for k in range(n):
  t = x*k + sum(B)
  ans = min(ans, t)
  for j in range(n):
    B[j] = min(B[j], A[j+k+1])
print(ans)

Measured memory_bytes: 3188
import sys

n, k = map(int, input().split())
w = [int(sys.stdin.readline()[:-1]) for i in range(n)]
l = max(w)
r = sum(w)

while l < r:
    m = (l + r) // 2
    stack = car = 0
    for i in w:
        stack += i
        if stack > m:
            stack = i
            car += 1
    if car < k:
        r = m
    else:
        l = m + 1
print(l)

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

#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)

#define LL long long
#define Ldouble long double

using namespace std;

int h, w, req;

int ans[505][505];
char str[4] = {'R', 'Y', 'G', 'B'};

int main(){
  scanf("%d %d %d", &h, &w, &req);
  FI(i, 1, h) FI(j, 1, w){
    int d1 = (i + j) % (2 * req);
    if(d1 >= req) ans[i][j]++;
  }
  FI(i, 1, h) FI(j, 1, w){
    int d2 = (i - j + w) % (2 * req);
    if(d2 >= req) ans[i][j] += 2;
  }
  FI(i, 1, h) FI(j, 1, w){
    printf("%c", str[ans[i][j]]);
    if(j == w) printf("\n");
  }
  return 0;
}

Measured memory_bytes: 1536
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segtree{
  vector<ll> datamax;
  vector<ll> datamin;
  vector<ll> delay;
  

  int n;

  void init(int _n){
    n = 1;
    while( n < _n ) n *= 2;
    datamax.resize( 2 * n );
    datamin.resize( 2 * n );
    delay.resize( 2 * n );
    
  }

  void delaycalc(int k){
    datamax[k] = datamax[k] + delay[k];
    datamin[k] = datamin[k] + delay[k];
    if( k + 1 < n ) 
      for(int i=1;i<=2;i++)
	delay[2*k+i] = delay[2*k+i] + delay[k];
    delay[k] = 0;
  }

  void add(int a,int b,ll x,int k,int l,int r){
    delaycalc(k);
    if( r <= a || b <= l ) return;
    else if( a <= l && r <= b ){
      delay[k] = x;
      delaycalc(k);      
    } else {
      add( a, b, x, 2*k + 1, l, (l+r)/2 );
      add( a, b, x, 2*k + 2, (l+r)/2, r );
      datamax[k] = max( datamax[2*k+1], datamax[2*k+2] );
      datamin[k] = min( datamin[2*k+1], datamin[2*k+2] );
    }				
  }
  

  ll qmax(int a,int b,int k,int l,int r){
    delaycalc(k);
    if( r <= a || b <= l ) return 0;
    if( a <= l && r <= b ) {
      return datamax[k] + delay[k];
    } else {
      ll vl = qmax( a, b, 2*k+1, l, (l+r)/2 );
      ll vr = qmax( a, b, 2*k+2, (l+r)/2, r );
      return max(vl,vr);
    }
  }
  
   ll qmin(int a,int b,int k,int l,int r){
    delaycalc(k);
    if( r <= a || b <= l ) return 0;
    if( a <= l && r <= b ) {
      return datamin[k] + delay[k];
    } else {
      ll vl = qmin( a, b, 2*k+1, l, (l+r)/2 );
      ll vr = qmin( a, b, 2*k+2, (l+r)/2, r );
      return min(vl,vr);
    }
  }
 
  
  void add(int a,int b,ll x){
    add( a, b, x, 0,0, n );
  }
  ll qmin(int a,int b){
    return qmin( a,b, 0,0,n);
  }  
  ll qmax(int a,int b){
    return qmax( a,b, 0,0,n);
  }  
};

segtree S;
int N,Q;
int R[300000];
int main(){
  cin >> N;
  for(int i=0;i<N;i++){
    int s; cin >> s;
    if( i<N/2 ) {
      R[i] = s;
    } else {
      R[N-i-1] -= s;
    }
  }
  S.init( N/2 );
  for(int i=0;i<N/2;i++){
    S.add( i,i+1,R[i] );
  }
  cin >> Q;

  for(int i=0;i<Q;i++){
    int l,r,x; cin >> l  >> r >> x;
    --l; --r;
    if( r < N/2 ){
      S.add( l,r+1, x );
    } else if( l >= N/2 ){
      S.add( N-r-1, N-l, -x );
    } else {
      S.add( l, N/2, x );
      S.add( N-r-1, N/2, -x );
    }
    if( S.qmax( 0, N/2 ) == 0 && S.qmin( 0,N/2 ) == 0 )
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }
  
}

Measured memory_bytes: 16052
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <regex>
#define REP(i,n) for(int i = 0; i < n; i++)
#define FOR(i,j,n) for(int i = j; i < n; i++)
#define RFOR(i,j,n) for(int i = j-1; i >= n; i--)
#define PREC(n) fixed << setprecision(n)
#define print_array(v) REP(__k, v.size()) { cout << v[__k]; if(__k != v.size()-1) cout << " "; else cout << endl; }
#define YesorNo(a) printf(a?"Yes\n":"No\n")
#define _LIBCPP_DEBUG 0
using namespace std;
int64_t MOD = 1000000007;

int digit_sum(int n){
   if(n < 10)  return n;
   return digit_sum(n/10) + n%10;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  bool flag = false;
  if(n % digit_sum(n) == 0) flag = true;
  YesorNo(flag);
  return 0;
}
//templete by private_yusuke

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define pb push_back
#define F first
#define S second

#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define int long long
#define ll long long
const int M=1e9+7;

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

int expo(int x, int y) {int res=1;x=x%M;while(y>0){if(y&1)res=(1ll*res*x)%M;
	y=y>>1;x=(1ll*x*x)%M;} return res;}
	

signed main(){
	gearup;
	int t = 1;
	//cin >> t;
	while(t--){
		int x, y, a, b, c, sum = 0;
		cin >> x >> y >> a >> b >> c;
		int A[a], B[b], xx;
		vector<int> v;
		for(int i = 0 ; i < a ; i++){
			cin >> A[i];		
		}
		for(int i = 0 ; i < b ; i++){
			cin >> B[i];		
		}
		sort(A, A + a, greater<int>());
		sort(B, B + b, greater<int>());
		for(int i = 0 ; i < c ; i++){
			cin >> xx;
			v.pb(xx);		
		}
		for(int i = 0 ; i < x ; i++)
			v.pb(A[i]);
		for(int i = 0 ; i < y ; i++)
			v.pb(B[i]);
		sort(allr(v));
		for(int i = 0 ; i < x + y ; i++)
			sum += v[i];
		cout << sum << "\n";		
	}
	return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;
typedef pair<int, int> Pair;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  vector<int> R(N);
  rep(i, N) cin >> A[i];
  rep(i, N) R[A[i]-1]=i+1;
  rep(i, N) cout << R[i] << " ";
}

Measured memory_bytes: 1536
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int N;
  cin >> N;
  
  vector<int> A(N);
  for( int i = 0; i < N; i++ ) cin >> A.at(i);
  vector<int> B(N);
  for( int i = 0; i < N; i++ ) cin >> B.at(i);
  
  vector<int> C(N);
  for( int i = 0; i < N; i++ ) C.at(i) = i+1;
  
  // Aが何番目か
  int cnt = 1;
  do{
    bool fg = true;
    for( int i = 0; i < N; i++ ){
      if( A.at(i) != C.at(i) ) fg = false;
    }
    if( fg == true ){
      break;
    }else cnt++;
    
  }while( next_permutation(C.begin(), C.end()) );
  
  sort( C.begin(), C.end() );
  int cntB = 1;
  do{
    bool fg = true;
    for( int i = 0; i < N; i++ ){
      if( B.at(i) != C.at(i) ) fg = false;
    }
    if( fg == true ){
      break;
    }else cntB++;
    
  }while( next_permutation(C.begin(), C.end()) );
  
  cout << abs( cnt - cntB ) << endl;
         
    
}
  

  




Measured memory_bytes: 256
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<list>
#include<map>
#include<memory>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<utility>
#include<vector>
//#include<windows.h>
//#include<time.h>

#define r(a,b,c) for(int a=b;a<=c;++a)
#define INF 0x7fffffff
#define ll long long

using namespace std;

inline int read()
{
	int a=0,b=1;
	char c=getchar();
	while(c<='0'||c>'9')
	{
		/*
		if(c=='-')
		{
		    b=-1;
		}
		*/
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		a=a*10+c-'0';
		c=getchar();
	}
	return a*b;
}

void write(int n)
{
	/*
	if(n<0)
	{
	    n=-n;
	    putchar('-');
	}
	*/
	if(n>10)
	{
		write(n/10);
	}
	putchar(n%10+'0');
}
int a,t,Max = -1;
char c[105];
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%d%s",&a,c);
	for(int i = 0 ; i < a ; ++ i)
	{
		if(c[i] == 'I')
		{
			t ++;
		}
		else t --;
		Max = max(Max,t);
	}
	if(Max == -1) 
	{
		Max = 0;
	}
	printf("%d",Max);
	return 0;
}


Measured memory_bytes: 256
def factorized(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct

N, P = map(int, input().split())
ans = 1
for t in factorized(P):
    if t[1] >= N:
        ans *= t[0] ** (t[1] // N)
print(ans)

Measured memory_bytes: 3188
//

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <cmath>
#include <stack>

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

using namespace std;

int main(){
  int a, b, c;
  int x, y;
  int ans = 0;

  cin >> a >> b >> c >> x >> y;
  if(x > y){
    int work = y;
    y = x;
    x = work;
    work = b;
    b = a;
    a = work;
  }
  // 作れるだけ作る
  ans += min(a + b, c * 2) * x;
  // 余ってしまった分を作る
  y -= x;
  ans += min(b, c * 2) * y;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>
#include <numeric>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <class T>
vector<T> make_v(size_t size, const T& init){ return vector<T>(size, init); }

template<class... Ts>
auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }



int main() {
    int h, w;
    cin >> h >> w;
    auto v = make_v(3, h+1, 0);
    for (int i = 0; i < h; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < w; ++j) {
            if(s[j] == 'W') v[0][i+1]++;
            if(s[j] == 'B') v[1][i+1]++;
            if(s[j] == 'R') v[2][i+1]++;
        }
    }
    for (int k = 0; k < 3; ++k) {
        partial_sum(v[k].begin(), v[k].end(), v[k].begin());
    }
    int ans = INF<int>;
    for (int i = 1; i <= h-2; ++i) {
        for (int j = i+1; j <= h-1; ++j) {
            ans = min(ans,
                      (v[0][h]-v[0][i])+v[1][i]+v[1][h]-v[1][j]+v[2][j]
                    );
        }
    }
    cout << ans << "\n";
    return 0;
}



Measured memory_bytes: 3160
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<stack>
#include<climits>
#include<cstring>
#include<queue>
#include<complex>
using namespace std;

struct Seg{
    int x1, x2, y1, y2;
    Seg(int x1, int y1, int x2, int y2) :x1(x1), y1(y1), x2(x2), y2(y2) {}
};

bool yparaAsc( const Seg& left, const Seg& right ) {
    return left.x1 != right.x1 ? left.x1 < right.x1 : left.y1 < right.y1;
};

struct Time{
    int x; //x??§?¨?
    int y; //y??§?¨?
    int act; //?????????????????????0 ??§?????????1 ??§?????????
    Time(int x, int y, int act) : x(x), y(y), act(act) {}

    bool operator<( const Time& right ) const {
        return x != right.x ? x < right.x : act < right.act;
    }
};

int segmentIntersection(vector<Seg> &input){
    int n = input.size();
    vector<Seg> xpara, ypara;
    for (int i=0;i<n;i++){
        Seg s = input[i];
        if(s.x1 == s.x2) ypara.push_back(s);
        if(s.y1 == s.y2) xpara.push_back(s);
    }

    sort(ypara.begin(), ypara.end(), yparaAsc);

    vector<Time> Timeline;
    for (int i=0;i<xpara.size();i++){
        Seg s = xpara[i];
        Time t1(s.x1, s.y1, 0), t2(s.x2, s.y1, 1);
        Timeline.push_back(t1);
        Timeline.push_back(t2);
    }
    sort(Timeline.begin(), Timeline.end());

    set<int> st;
    int i=0;
    int j=0;
    int S=0;
    while(1){
        if(i >= ypara.size() && j >= Timeline.size()) break;

        if(i >= ypara.size()){
            if(Timeline[j].act == 0) st.insert(Timeline[j].y);
            if(Timeline[j].act == 1) st.erase(Timeline[j].y);
            j++;
            continue;
        }

        if(j >= Timeline.size()){
            set<int>::iterator a = lower_bound(st.begin(), st.end(), ypara[i].y1);
            set<int>::iterator b = upper_bound(st.begin(), st.end(), ypara[i].y2);
            S += distance(a, b);
            i++;
            continue;
        }

        if((ypara[i].x1 < Timeline[j].x) || (ypara[i].x1 == Timeline[j].x && Timeline[j].act == 1)){
            set<int>::iterator a = lower_bound(st.begin(), st.end(), ypara[i].y1);
            set<int>::iterator b = upper_bound(st.begin(), st.end(), ypara[i].y2);
            S += distance(a, b);
            i++;
        }else{
            if(Timeline[j].act == 0) st.insert(Timeline[j].y);
            if(Timeline[j].act == 1) st.erase(Timeline[j].y);
            j++;
        }
    }
    return S;
}

int main(){
    int n, x1, x2, y1, y2;
    cin >> n;
    vector<Seg> input;
    for (int i=0;i<n;i++){
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 > x2) swap(x1, x2);
        if (y1 > y2) swap(y1, y2);
        Seg s(x1, y1, x2, y2);
        input.push_back(s);
    }

    cout << segmentIntersection(input) << endl;

    return 0;
}

Measured memory_bytes: 10864
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> plp;
const ll INF = 1e9+100;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
const bool debug = 0;
//--------------------------------//
 
int N, K;
 
int main() {
	cin >> N >> K;
	int ans = INF;
	REP(i, 1<<N) {
		ll now = 1;
		REP(j, N) {
			if (i >> j & 1) now = now * 2;
			else now += K;
		}
		chmin(ans, now);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize("Ofast") 

////////////////////////////////////////////////////////////////////////////////

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define bew ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(x) x.begin(),x.end()
#define pb push_back
#define fi first
#define se second
#define mod (long)(1e9+7)
#define ip(ar,n) for(int i=0;i<n;++i)cin>>ar[i];
#define op(ar,n) for(int i=0;i<n;++i)cout<<ar[i]<<" ";
#define test int t;cin>>t;while(t--)
#define tar(x) int x;cin>>x;
#define fil(ar,val) memset(ar,val,sizeof ar)
#define loop(n) for(int i=0;i<n;++i)

////////////////////////////////////////////////////////////////////////////////

typedef vector<int> vi; 
typedef pair<int, int> ii; 
typedef vector<ii> vii; 
typedef set<int> si;
typedef map<int,int> mi;

////////////////////////////////////////////////////////////////////////////////






signed main() {
bew;
int n;
cin>>n;
int ans=n/500*1000;
n-=n/500*500;
cout<<(ans+n/5*5);




return 0;
}

Measured memory_bytes: 768
import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val r = sc.nextInt()
    val d = sc.nextInt()
    val x2000 = sc.nextInt()
    println(problem127b(r, d, x2000))
}

fun problem127b(r: Int, d: Int, x2000: Int): String {
    val dp = (0..10).map { 0 }.toMutableList()
    dp[0] = x2000
    for (i in 1..10) {
        dp[i] = r * dp[i - 1] - d
    }
    return dp.drop(1).joinToString("\n")
}

Measured memory_bytes: 31388
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,count=3;
    cin>>a>>b>>c;
    if(a==b && a==c) cout<<1<<endl;
    else if((a==b && b!=c) || (b==c && a!=b) || (a==c && a!=b) ) cout<<2<<endl;
    else cout<<3<<endl;
}


Measured memory_bytes: 256
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    t = 1
    for i in range(n):
        if i == 0:
            t *= k
        else:
            t *= (k - 1)
    print(t)


if __name__ == '__main__':
    slove()


Measured memory_bytes: 38256
#include<cstdio>

int main(void){
	int a, b, c[4]={0}, i;
	for(i=0; i<3; ++i){scanf("%d%d", &a, &b); ++c[a-1]; ++c[b-1];}
	printf("%s\n", (c[0]*c[1]*c[2]*c[3]-3)?"YES":"NO");
	return(0);
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#include <string>
//#include <boost/multiprecision/cpp_int.hpp>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define llu  long long 
#define fp cout<<fixed<<setprecision(9);
#define ifor(j,n) for(int i=j ;i<n;i++)
#define jfor(k,n) for(ll j=k ;j<n;j++)
#define pb push_back
#define mod 1000000007
using namespace std;
//using namespace boost::multiprecision;

//Compiler version g++ 6.3.0
int search (vector <long long> v,long long m,int n) 
{ 

    int start = 0;
    int ans = -1; 
    while (start <=n) 
    { 
     int mid = (start + n) / 2;
     if (v[mid] <= m) 
         start = mid + 1;
     else
   { 
   ans = mid; 
   n = mid - 1; 

       } 

    } 
    return ans; 
} 

int main()
{
 FAST 

   long long n,d,p,q,count=0;
   cin>>n>>d;
   d=d*d;
   ifor(0,n)
   {
     cin>>p>>q;
     if(((p*p)+(q*q))<=d)
       count++;
   }
   cout<<count;
 
   

    
 
 
        
      
    
        
 
      
         
            
             
       
      
       
    
   
     }   
        
       
        
        
        
       
          
    

Measured memory_bytes: 3644
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <fstream>
#include <cmath>
#include <random>
#include <complex>
#include <functional>
#include <bitset>
#include <sstream>
#include <cassert>
#include <time.h>

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

const ll inf = 1LL << 60;

template<class T> inline void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}
template<class T> inline void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}
void printvec (vec &v) {
    Rep (i, (ll)v.size()) {
        cout << v[i] << " \n"[i+1 == (ll)v.size()];
    }
}

const int MOD = 1e9+7;
class mint {
    ll x;
public:
    mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
    
    mint operator -() const {
        return mint(-x);
    }
    
    mint &operator +=(const mint &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }

    mint &operator -=(const mint &a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }

    mint &operator *=(const mint &a) {
        (x *= a.x) %= MOD;
        return *this;
    }

    mint operator +(const mint &a) const {
        mint res(*this);
        return res += a;
    }

    mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }

    mint operator *(const mint &a) const {
        mint res(*this);
        return res *= a;
    }

    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(MOD - 2);
    }

    mint &operator /=(const mint &a) {
        return (*this) *= a.inv();
    }

    mint operator /(const mint &a) const {
        mint res(*this);
        return res /= a;
    }

    friend ostream &operator<<(ostream& os, const mint &m) {
        os << m.x;
        return os;
    }
};


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

    ll N;
    cin >> N;
    mat a(N, vec(N));
    Rep (i, N) {
        Rep (j, N) {
            cin >> a[i][j];
        }
    }

    vector<vector<mint>> dp(N+1, vector<mint>(1LL<<N, mint(0)));
    dp[0][0] = mint(1);

    vec cnt(1LL<<N, 0);
    Rep (mask, 1LL<<N) {
        Rep (i, N) {
            if (mask & (1LL<<i)) cnt[mask]++;
        }
    }

    Rep (i, N) {
        Rep (mask, 1LL<<N) {
            if (cnt[mask] != i) continue;
            Rep (j, N) {
                if (a[i][j]) {
                    dp[i+1][mask|(1LL<<j)] += dp[i][mask];
                }
            }
        }
    }

    // Rep (i, N+1) {
    //     Rep (j, 1LL<<N) {
    //         cout << dp[i][j] << " \n"[j==(1LL<<N)-1];
    //     }
    // }

    cout << dp[N][(1LL<<N)-1] << "\n";
}

Measured memory_bytes: 380264
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;
        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }
        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt()
        {
            return Integer.parseInt(next());
        }
 
        long nextLong()
        {
            return Long.parseLong(next());
        }
 
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
 
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
 
    public static void main(String[] args)
    {
        FastReader s=new FastReader();
        String str=s.next();
        if(str.charAt(str.length()-1)=='s') System.out.println(str+"es");
        else
        	System.out.println(str+"s");
    }
}

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

using namespace std;

int main(){
    int pedras=0, pulos=0, q=0;

    cin >> pedras >> pulos;

    int custo[100001], resp[100001];
    
    for (int i=0; i<pedras; i++){
        cin >> custo[i];
        resp[i]=-1;
    }

    resp[0]=0;
    for (int i=0; i<pedras; i++){
        for (int j=1; j<=pulos && i+j<pedras; j++){

            if(resp[i+j]==-1){
                q=abs(custo[i+j]-custo[i])+resp[i];
            }else {
                q= min(resp[i+j], abs(custo[i+j]-custo[i])+resp[i]);
            }
            resp[i+j]=q;
        }
    }
    
    // for (int i=0; i<pedras; i++){
    //         cout << resp[i] << endl;
    // }
    cout<< resp[pedras-1] << endl;
}

Measured memory_bytes: 1024
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, y = list(map(int, readline().split()))
    t = 0
    if x == 1 and y == 1:
        print(300000 * 2 + 400000)
    else:
        if x == 1:
            t += 300000
        elif x == 2:
            t += 200000
        elif x == 3:
            t += 100000

        if y == 1:
            t += 300000
        elif y == 2:
            t += 200000
        elif y == 3:
            t += 100000
        print(t)


if __name__ == '__main__':
    solve()


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

int main() {
  int s1s2s3;
  cin >> s1s2s3;
  cout << s1s2s3%10+(s1s2s3-s1s2s3%10)/10%10+((s1s2s3-s1s2s3%10)/10-(s1s2s3-s1s2s3%10)/10%10)/10<< endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define mt              make_tuple
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define test            int t; cin>>t; while(t--)
#define io             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
 


int32_t main(){
  io ; 

      int x;
      cin >> x; 

     if(x>=30){
         cout << "Yes\n";
     }else{
         cout << "No\n"; 
     }

}

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

typedef long long ll;
typedef pair<int, int> P;

const ll mod =  1000000007;  //ansを出力する時は (ans % mod + mod) % mod とする(負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60;   // INFの値は1152921504606846976

vector<int> G[100005];
bool v[100005];



int main()
{
    int n,k;
    cin >> n >> k;
    ll d[100005];
    rep(i,100005) d[i] = -1;
    rep(i,n-1){
        int a,b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    queue<int> q;
    v[0] = true;
    q.push(0);
    d[0] = 0;
    ll ans = k;
    while(!q.empty()){
        int now = q.front(); q.pop();
        ll cnt = k;
        if(d[now] == 0) cnt -= 1;
        else cnt -= 2;
        for(auto next : G[now]){
            if(d[next] == -1){
                q.push(next);
                d[next] = d[now] + 1;
                ans *= cnt;
                ans %= mod;
                cnt--;
            }
        }
    }
    cout << ans << endl;
}


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

using namespace std;

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

  if (!n) return 0;

  vector<int> ary;
  for (int i = n; i >= 1; i--) {
    ary.push_back(i);
  }
  for (int i = 0; i < r; i++) {
    vector<int> tmp;
    int p, c;
    cin >> p >> c;
    for (int j = 0; j < c; j++) {
      tmp.push_back(ary[p - 1 + j]);
    }
    for (int j = p - 2; j >= 0; j--) {
      ary[j + c] = ary[j];
    }
    ary = tmp;
    tmp.clear();
  }
  cout << ary[0] << endl;

  main(); // kikaku ihan dakedo
}

Measured memory_bytes: 3276
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <random>
#include <tuple>
#include <iomanip>
#include <cstring>

using namespace std;

typedef long long ll;
//typedef pair<int,int> Pint;
//typedef pair<ll, ll> P;
//typedef pair<ll, pair<ll, ll>> P;
//typedef tuple<int,int,int> T;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define revrep(i, n) for(ll i = (n-1); i >= 0; i--)
#define pb push_back
#define f first
#define s second
//const ll INFL = 1LL << 60;//10^18 = 2^60
const ll INFL = 1LL << 60;
const int INF = 1 << 30;//10^9
ll MOD = 1000000007;
//ll MOD  = 998244353;
vector<ll> dy = {0, 0, 1, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, -1, 0, 0, 1, -1, 1, -1, 0};

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll pow_long(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2) res *= x;
    x *= x;
    k /= 2;
  }
  return res;
}
ll pow_mod(ll x, ll k){
  x %= MOD; x += MOD; x %= MOD;
  ll res = 1;
  while(k > 0){
    if(k % 2){
      res *= x; res %= MOD;
    }
    x *= x; x %= MOD;
    k /= 2;
  }
  return res;
}
ll inverse(ll x){return pow_mod(x, MOD - 2);};

//最大公約数
ll gcd(ll a, ll b){
    if(b == 0) return a;
    return gcd(b, a % b);
}

//最小公倍数
ll lcm(ll x, ll y){return x / gcd(x, y) * y;};

ll kai_mod(ll x){
  if(x == 0) return 1;
  return x * kai_mod(x-1) % MOD;
}

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


int main(){
  int M, D;
  cin >> M >> D;
  ll ans = 0;
  for(int i = 1; i <= M; i++){
    for(int j = 1; j <= D; j++){
      int d1 = j % 10;
      int d2 = j / 10;
      if(d1 < 2 || d2 < 2) continue;
      if(d1 * d2 == i) ans++;
    }
  }
  cout << ans << endl;
















}


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

int main() {
  int n;
  cin >> n;
  if(n == 1){
    cout << "Yes" << endl;
    cout << 2 << endl;
    cout << 1 << " " << 1 << endl;
    cout << 1 << " " << 1 << endl;
    return 0;
  }
  rep(k,2,n+1){
    if(k*(k-1)/2 == n){
      cout << "Yes" << endl;
      cout << k << endl;
      vector<vector<int>>data(k);
      int cnt = 1;
      rep(i,0,k-1) rep(j,i+1,k){
        data[i].push_back(cnt);
        data[j].push_back(cnt);
        cnt++;
      }
      rep(i,0,k){
        cout << data[i].size() << " ";
        rep(j,0,data[i].size()){
          cout << data[i][j];
          if(j != data[i].size()-1) cout << " ";
          else cout << "" << endl;
        }
      }
      return 0;
    }
    if(k*(k-1)/2 > n) break;
  }
  cout << "No" << endl;
}

Measured memory_bytes: 4272
ans = 0
n = int(input())
s = input()

for x in range(10):
    if s.find(str(x)) > -1:
        tmp = s[s.find(str(x)) + 1:]
        for y in range(10):
            if tmp.find(str(y)) > -1:
                tmp2 = tmp[tmp.find(str(y)) + 1:]
                for z in range(10):
                    if tmp2.find(str(z)) > -1:
                        ans += 1

print(ans)


Measured memory_bytes: 9324
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define all(x) (x).begin(),(x).end()
#define INF 10000000
using ll = long long ;
int main(){
    int n;
    cin >> n;
    vector<ll> vec;
    rep(i,n){
        ll l;
        cin >> l;
        vec.push_back(l); 
    }
    sort(all(vec));
    ll minc,maxc;
    ll ans = 0;
    for(int i=0;i<n-2;i++){
        for(int j=i+1;j<n-1;j++){
            ll a = vec[i];
            ll b = vec[j];
            maxc = a+b;
            int lessthanC = lower_bound(vec.begin(), vec.end(), maxc) - vec.begin()-1;
            if (lessthanC > j){
                ans += lessthanC-j;
            }
        }
    }
    
    cout << ans << endl;
}

Measured memory_bytes: 256
n, y = map(int, input().split())
y //= 1000
m = y // 10
for mc in range(m, -1, -1):
  g = (y - 10 * mc) // 5
  for gc in range(g, -1, -1):
    sc = y - 10 * mc - 5 * gc
    if mc + gc + sc == n:
      print(mc, gc, sc)
      quit()
print(-1, -1, -1)

Measured memory_bytes: 3064
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>

using namespace std;

long long MOD = 1000000007;

long long n,x;

int main(){
  cin>>n>>x;
  long long ans = x + (n-x);

  long long a = x;
  long long b = (n-x);

  while(1){
    if(a > b){
      long long tmp = a;
      a = b;
      b = tmp;
    }
    if(a == 0){
      ans -= b;
      break;
    }

    ans += 2LL*a*(b/a);
    b = b%a;
  }

  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 256
M, D = map(int, input().split())
ans = 0
for i in range(1,M+1):
  for j in range(2,10):
    if i % j == 0 and 2 <= i//j < 10:
      if 10*j + i//j <= D:
        # print(i,j,i//j)
        ans += 1
print(ans)

Measured memory_bytes: 9064
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    vector<ll> b(n - 1);
    rep(i, n - 1) cin >> b[i];
    vector<ll> v(n);
    v[0] = b[0];
    v[n - 1] = b[n - 2];
    rep(i, n - 2) {
        v[i + 1] = min(b[i], b[i + 1]);
    }
    cout << accumulate(all(v), 0LL) << endl;
    return 0;
}


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

int main(){
  int n;
  while(cin >> n){
    int cnt=0;
    
    for(int a=0;a<10;a++){
      for(int b=0;b<10;b++){
	for(int c=0;c<10;c++){
	  for(int d=0;d<10;d++){
	    if(a+b+c+d==n){
	      cnt++;
	    }
	  }
	}
      }
    }

    cout << cnt << endl;
  }
}

Measured memory_bytes: 1160
#include <stdio.h>
int n, m, a, b, c, d;
long long x[100000];
long long k;
int s[100000];
int t[100000];
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lld", x + i);
		s[i] = i;
	}
	for (int i = n - 1; i; i--)x[i] -= x[i - 1];
	scanf("%d%lld", &m, &k);
	for (int i = 0; i < m; i++) {
		scanf("%d", &a);
		b = s[a - 1];
		s[a - 1] = s[a];
		s[a] = b;
	}
	for (int i = 1; i < n; i++) {
		if (t[i])continue;
		c = s[i];
		d = 1;
		while (c != i) {
			c = s[c];
			d++;
		}
		b = k % d;
		c = i;
		for (int j = 0; j < b; j++) {
			c = s[c];
		}
		b = i;
		for (int j = 0; j < d; j++) {
			t[b] = c;
			b = s[b];
			c = s[c];
		}
	}
	k = 0;
	for (int i = 0; i < n; i++) {
		k += x[t[i]];
		printf("%lld\n", k);
	}
}

Measured memory_bytes: 3228
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>


using ll =  long long;

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

using namespace std;

int main(){

    string s;
    cin >> s;
    
    if(s[2]==s[3]&&s[4]==s[5])
        cout << "Yes";
    else
        cout << "No";
    
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 100000 + 5;
int h[N],vis[N];

int main(){
	int n,m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		cin >> h[i];
	}
	for(int i = 1; i <= m; i++){
		int a,b;
		cin >> a >> b;
		if(h[a] <= h[b])
			vis[a] = 1;
		if(h[a] >= h[b])
			vis[b] = 1;
	}
	int ans = 0;
	for(int i = 1; i <= n; i++){
		if(!vis[i])
			ans++;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 4416
#include <stdio.h>

int main()
{   int n;
    scanf("%d",&n);
    int p[n],c=0;
    for(int i=0;i<n;i++){
        scanf("%d",&p[i]);
        if(p[i]!=i+1)c++;
    }
    if(c<=2){
        printf("YES");
    }else{
        printf("NO");
    }
    return 0;
}


Measured memory_bytes: 128
from math import sqrt, ceil, factorial
from collections import defaultdict


def prime_factors(n):
    i = 2
    factors = defaultdict(int)
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors[i] += 1
    if n > 1:
        factors[n] += 1
    return factors


N, M = [int(elem) for elem in input().split(' ')]

# 素因数分解
prime_numbers = prime_factors(M)
sum = 1
fac_N_m1 = factorial(N - 1)

for value in prime_numbers.values():
    bunshi = 1
    saisho = value + N - 1
    for _ in range(value):
        bunshi *= saisho
        saisho -= 1
    sum *= (bunshi // factorial(value))
print(sum % (10**9 + 7))


Measured memory_bytes: 4408
-- haskell
import Data.List
import Debug.Trace

main = getContents >>= (putStrLn . show . ans)

ans :: String -> Int
ans str = maximum $  0 : [y - 1 | (x,y) <- zip hist [0,1..] , x + 1 >= y]
  where
    hist = take (n + 10) $ scanl (+) 0 diff_table
    diff_table = zipWith (-) as_table bs_table
    as_table = tail $ countList as_counted 0
    bs_table = countList bs_counted 0
    as_counted = map (\x -> (head x, length x)) . group $ as_sorted
    bs_counted = map (\x -> (head x, length x)) . group $ bs_sorted
    as_sorted = sort as
    bs_sorted = sort bs
    as = map head l
    bs = map (head . tail) l
    n = length l
    l = map (map read . words) . tail . lines $ str


countList :: [(Int, Int)] -> Int -> [Int]
countList [] _ = repeat 0
countList ((x,y):xs) i =
  if x == i then y : (countList xs (i + 1)) else 0 : (countList ((x,y) : xs) (i + 1))

Measured memory_bytes: 121860
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Graph=vector<vector<int>>;
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

ll power(ll a,ll b){
  ll res=1;
  while(b>0){
    if(b&1) res=res*a%MOD;
    a=a*a%MOD;
    b>>=1;
  }
  return res;
}

int main(){
  ll S,X,Y;
  cin>>S;
  
  if(S%power(10,9)==0){
    Y=S/power(10,9);
    X=0;
  }
  else{
    Y=S/power(10,9)+1;
    X=Y*power(10,9)-S;
  }
  
  cout<<0<<" ";
  cout<<0<<" ";
  cout<<power(10,9)<<" ";
  cout<<1<<" ";
  cout<<X<<" ";
  cout<<Y<<endl;
  return 0;
}

Measured memory_bytes: 256