akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <algorithm>
#define repu(i,x,y) for (int i=x; i<=y; ++i)
using namespace std;

int n,k,sum;
char s[200100];

int main()
{
    scanf("%d%d%s",&n,&k,s+1),sum=-1;
    repu(i,1,n)
    {
        if (++sum==k)
        {
            repu(j,i,n)
                putchar(((s[j]-'A')^(i&1)^1)+'A');
            repu(j,1,i-1)
                putchar(((j&1)^(i&1)^1)+'A');
            return 0;
        }
        if ((s[i]-'A')^(i&1) && ++sum==k)
        {
            putchar('B');
            repu(j,i+1,n)
                putchar(((s[j]-'A')^(i&1)^1)+'A');
            repu(j,1,i-1)
                putchar(((j&1)^(i&1)^1)+'A');
            return 0;
        }
    }
    if (n&1)
    {
        putchar((k-sum)&1?'A':'B');
        repu(i,2,n)
            putchar('B'-(i&1));
    }
    else
        repu(i,1,n)
            putchar('A'+(i&1));
    return 0;
}

Measured memory_bytes: 512
import java.util.Scanner;

public class Main{
	public static void main(String args[]){

		//??????????????¨???????????????
		Scanner sc = new Scanner(System.in);
		int list [][];

		//????????????????????¨??¨?????????????????????????????????
		int r = sc.nextInt();
		int c = sc.nextInt();
		list = new int[r][c];


		//2?¬???????????????°?????????????????????
		for(int i = 0; i < r; i++){
			for(int j = 0; j < c; j++){
				list[i][j] = sc.nextInt();
			}
		}

		//??????????????¨???

		//???????????¨r???????¨??????¨???
		for(int i = 0; i < r; i++){
			int rsum = 0;
			String a = "";
			for(int j = 0; j < c; j++){
				rsum += list[i][j];
				a += (list[i][j] +" ");
			}
			System.out.println(a + (rsum));
		}

		//c??¨????¨??????¨???

		int sum = 0;
		for(int i = 0 ; i < c; i++){
			int csum = 0;
			for(int j = 0 ; j < r; j++){
				csum += list[j][i];
			}
			System.out.print(csum + " ");
			sum += csum;
		}

		System.out.println(sum);
		sc.close();
	}
}

Measured memory_bytes: 52740
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=list(map(int, input().split()))
b=a[:]
c=a[:]
# a[0]の符号をそのままにした場合の計算
counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+a[i]<=0:
    counter_1+=-S-a[i]+1
    a[i]=-S+1
  elif S>0 and S+a[i]>=0:
    counter_1+=S+a[i]+1
    a[i]=-S-1
  S+=a[i]
# a[0]を1に変えた場合の計算
counter_2=abs(b[0]-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+b[i]<=0:
    counter_2+=-S-b[i]+1
    b[i]=-S+1
  elif S>0 and S+b[i]>=0:
    counter_2+=S+b[i]+1
    b[i]=-S-1
  S+=b[i]
# a[0]を-1に変えた場合の計算
counter_3=abs(c[0]+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+c[i]<=0:
    counter_3+=-S-c[i]+1
    c[i]=-S+1
  elif S>0 and S+c[i]>=0:
    counter_3+=S+c[i]+1
    c[i]=-S-1
  S+=c[i]
print(min(counter_1,counter_2,counter_3))

Measured memory_bytes: 14332
N = int(input())
A = input().split()
B = input().split()

# print(N)
# print(A)
# print(B)

a = A[::-1]
b = B[::-1]
# print(a)
# print(b)

ans = 0
for i in range(len(b)):
    if(int(a[i])>int(b[i])):
       a[i] = int(a[i]) - int(b[i])
       ans += int(b[i])
    else:
        ans += int(a[i])
        remain = int(b[i]) - int(a[i])
        a[i] = 0
        if(int(a[i+1])>remain):
            a[i+1] = int(a[i+1]) - remain
            ans += remain
        else:
            ans += int(a[i+1])
            a[i+1] = 0
print(ans)
        

Measured memory_bytes: 20012
// Sun Apr 19 04:28:54 IST 2020          
#include <bits/stdc++.h>        
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#define forn(i,a,b)        for( i = a ; i < b  ; i++ )
#define debug(x)           cout << #x << " " << x << " -- \n"
#define I__O               ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define int64              long long    
#define MOD                998244353
#define pi                 pair<int,int>
#define vi                 vector <int>
#define pb                 push_back
#define vp                 vector <pair<ll,ll>>
#define endl               cout << "\n"  
#define f1                 first 
#define s1                 second    
#define in                 insert
#define all(x)             (x).begin(),(x).end() 
#define precise(x)         cout << fixed << setprecision(12) << x << '\n'; 
using namespace std ;     
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set ; 
const int N = 2e5+7;  
vector <vector <int>> adj(N);
int tests(){ 
  int n ;
  cin >> n ;
  vector <int> a(n+1);
  for(int i = 2 ; i <=n  ;i++){
    cin >> a[i];
    adj[a[i]].push_back(i);
  } 
  vector <bool> vis(n,false);
  // vector <int> dp(n,0);

  // dfs(0,vis,dp); 
  for(int i = 1 ;i <= n ;i++)
    cout << adj[i].size() << '\n' ;
  return  0; 
}
int32_t main(){
    I__O ;            
    int cases = 1 ; 
    // cin >> cases ;  
    for(int i = 1 ;i  <= cases ; i++ ){   
    // cout << "Case #" << i << ": ";
      tests(); 
    }
    return 0 ; 
}

// -Wall -Weptra -O2
// -pedantic -std=c++11
// -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC

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

const int maxn = 2e5, inf = 1e9;

int n;
char s[maxn + 1];
int a[maxn + 1];
int f[maxn + 1], minf[1 << 26];

int main(void) {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; ++i) {
    a[i + 1] = a[i] ^ (1 << (s[i] - 'a'));
  }
  fill(minf, minf + (1 << 26), inf);
  f[0] = 0;
  minf[0] = 0;
  for (int i = 1; i <= n; ++i) {
    f[i] = minf[a[i]];
    for (int j = 0; j < 26; ++j) {
      f[i] = min(f[i], minf[a[i] ^ (1 << j)]);
    }
    ++f[i];
    minf[a[i]] = min(minf[a[i]], f[i]);
  }
  printf("%d\n", f[n]);
  return 0;
}


Measured memory_bytes: 264064
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    int a1,a2,a3;
    cin>>a1>>a2>>a3;
    int part1 = abs(a1-a2);
    int part2 = abs(a1-a3);
    int part3 = abs(a2-a3);
 
    int maxi = part1>part2?(part1>part3?part1:part3):(part2>part3?part2:part3);
    cout<<maxi<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<list>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
#define chmin(a,b) (a>(b)?a=(b),1:0)
#define repa(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
typedef long int li;

int check(string s,int a){
  rep(i,a+1){
    if(s[i]!=s[i+a+1]) return 0;
  }
  return 1;
}

int main(){
  string s; cin >>s;
  int n=s.size();
  int ans=0;
  rep(i,n/2-1){
    if(check(s,i)) ans=i;
  }
  cout << (ans+1)*2 << "\n";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
 
 
int main() {
    long long int a,b,n;
    cin>>a>>b>>n;
    long long int ans=INT_MIN;
    if(n<b){
    	ans=(a*n)/b;
	}
	else{
		long long int x=b-1;
		ans=(a*x)/b;
	}
    cout<<ans;
    return 0;
}

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

void water(int h[], int n) {
    for (int i = 0; i < n; i++) {
        h[i]--;
    }
}

int count(int h[], int n) {
    int res = 0;
    bool no_water = true;
    for (int i = 0; i < n; i++) {
        if (no_water) {
            if (h[i] > 0) {
                res++;
                no_water = false;
            }
        } else {
            if (h[i] <= 0) {
                no_water = true;
            }
        }
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    int h[n];
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    int max_h = h[max_element(h, h + n) - h];
    int ans = 0;
    for (int i = 0; i < max_h; i++) {
        ans += count(h, n);
        water(h, n);
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
x,y,a,b,c, = map(int, input().split())
xx = list(map(int, input().split()))
yy = list(map(int, input().split()))
rr = list(map(int, input().split()))
 
xx = sorted(xx, reverse=True)[:x]
yy = sorted(yy, reverse=True)[:y]
rr = rr + xx + yy
print(sum(sorted(rr, reverse=True)[:x+y]))

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

using namespace std;

using u128 = __uint128_t;

#define int long long
#define mod 1000000007

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin>>n>>m;
    if(n==0) cout<<0<<endl;
    else {
        if(n==m){
            cout<<"Yes"<<endl;
        } else cout<<"No"<<endl;
    }
}

Measured memory_bytes: 256
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e+5;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b){ a>b?:a=b; }
template<class T, class C> void chmin(T& a, C b){ a<b?:a=b; }

void solve() {
    int n, y;
    int a, b, c;
    cin >> n >> y;

    rep(i, n+1) {
        rep(j, n-i+1) {
            int x = 10000 * i + 5000 * j + 1000 * (n - i - j);
            if (x == y) {
                cout << i << ' ' << j << ' ' << n - i - j << endl;
                return;
            }
        }
    }
    cout << -1 << ' ' << -1 << ' ' << -1 << endl;
}

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


Measured memory_bytes: 256
A,B,K = map(int, input().split())
lcnt = 0
rcnt = 0
ans = []
for i in range(A, min(A + K, B)):
    ans.append(i)

for i in range(max(B - K + 1, A), B + 1):
    ans.append(i)

set_ans = list(set(ans))
set_ans.sort()
for i in set_ans:
    print(i)


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
ll n,x;
void solve(){
    cin>>x;
    ll ans = 100;
    if(ans==x){
        cout<<"0";
        return;
    }
    for(int i=0 ; i<100000 ; i++){
        ans+=ans/ll(100);
        if(ans>=x){
            cout<<i+1;
            return;
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    solve();
}

Measured memory_bytes: 3664
import java.util.Scanner;

public class Main {

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

		int i = 1;

		while (i <= N) {
			i = i*2;
		}

		System.out.println(i/2);
	}

}


Measured memory_bytes: 23764
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

char st[1001][1001];

struct A {P pa;int cc;};


int main(){
    int h,w;cin >> h >> w;
    bool used[h][w];
    queue<A> q;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> st[i][j];
            used[i][j]=false;
            if(st[i][j]=='#'){
                used[i][j] = true;
                q.push({mp(i,j),0});
            }
        }
    }
    int ans = 0;
    while(q.size()){
        A s = q.front();q.pop();
        P p = s.pa;
        int c = s.cc;
        int y = p.fs,x=p.sc;
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i],nx = x + dx[i];
            if(0<=ny&&ny<h&&0<=nx&&nx<w){
                if(!used[ny][nx]){
                    q.push({mp(ny,nx),c+1});
                    used[ny][nx]=true;
                }
            }
        }
        ans = max(ans,c);
    }
    cout << ans << endl;
    return 0;
}


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

#define int long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define endl "\n"
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e10;
const int MOD = 1e9 + 7;

main() {
    ios_base::sync_with_stdio(0);
    int x, y;
    int ans = 0;
    cin >> x >> y;
    if (abs(x - y) <= 1){
        cout << "Brown";
    }
    else{
        cout << "Alice";
    }

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  string S;
  cin >> S;
  vector<int> A(3, 0);
  for(auto&& s:S){
    A[s - 'a']++; 
  }
  sort(A.begin(), A.end());
  if(A[2] - A[0] <= 1) cout << "YES" << endl;
  else cout << "NO" << endl;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ENDL '\n'
#define all(a) begin(a),end(a)
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << ": " << a << ENDL
#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)
#define _ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> ii;
typedef vector<lli> vi;

lli INF = 1e18;

vector<vector<lli>> dp;

vector<lli> val;

lli n;

bool valid(lli u, lli v) {
    return u >= 0 and u < n and v >= 0 and v < n and u <= v;
}

lli solve(lli u, lli v) {
    lli &ans = dp[u][v];
    if(ans != -1) return ans;
    if(u == v) return ans = val[u];
    if(u > v) return ans = 0;
    if(!valid(u + 1, v))
        return ans = val[v] - solve(u, v - 1);
    if(!valid(u, v - 1))
        return ans = val[u] - solve(u + 1, v);
    return ans = max(val[v] - solve(u, v - 1) , val[u] - solve(u + 1, v));
}

int main()
{   _
    cin >> n;
    dp.assign(n, vector<lli>(n, -1));
    val.resize(n); fore(i, 0, n) cin >> val[i];
    cout << solve(0, n - 1) << ENDL;
    ///fore(i, 0, n) { fore(j, 0, n) cout << dp[i][j] << ' '; cout << ENDL;}
    return 0;
}


Measured memory_bytes: 73936
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main{


	public static void main(String[] args){

    	Scanner scan = new Scanner(System.in);

			while(true){
				try{
            int H=scan.nextInt();
			int W=scan.nextInt();
			if(H==0&&W==0)break;

			for(int i=0;i<H;i++){
				for(int j=0;j<W;j++){
					if(i==0||i==H-1||j==0||j==W-1){System.out.print('#');}
					else{System.out.print('.');}
				}System.out.print('\n');
			  }System.out.print('\n');

				}catch(InputMismatchException e){
					System.out.println("??¨??????:"+e);

				}

			}
        	scan.close();


	}

}

Measured memory_bytes: 38088
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int n;
int k;
int q;
int shu;
int main()
{
    scanf("%d%d%d",&n,&k,&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&shu);
        a[shu]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(k-(q-a[i])>0)
            printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
 

Measured memory_bytes: 1024
from itertools import accumulate 

N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

ans = -10**18-1
seen = [False]*N
for i in range(N):
    if seen[i]:
        continue
    loop = []
    now = i
    while seen[now] == False:
        seen[now] = True
        loop.append(C[now])
        now = P[now]
    loop_size = len(loop)
    loop_score = sum(loop)
    cumsum = list(accumulate(loop+loop))
    for j in range(1, min(K, loop_size)+1):
        for l in range(loop_size):
            r = l+j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size))
print(ans)

Measured memory_bytes: 74436
n, m = map(int,input().split())
mod = 10**9 + 7

M = [1]
mul = 1
for i in range(1, 10**5+5):
    mul = (mul * i) % mod
    M.append(mul)

if n-m < -1 or n-m > 1:
    print(0)
elif n-m != 0:
    print((M[n]*M[m]) % mod)
else:
    print((((M[n]*M[m]) % mod) * 2 ) % mod)

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


int a, b;
int h[1001];
int num(int n){
	return n*(n+1)/2;
}

int main(){
	cin >> a >> b;
	h[1]=1;
	for(int i=2;i<1000;i++){
		h[i] = num(i);
		if(b-a==h[i]-h[i-1]) {
			cout << h[i] - b << endl;
			return 0;
		}
	}
	return 0;
}


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

int main() {
  string str;
  getline(cin, str);
 
  for(int i=0; i < str.length(); i++) {
    if     (str.substr(i, 5) == "apple")
    {
        str.replace(i, 5, "peach");i+=4;
    }
    else if(str.substr(i, 5) == "peach")
    {
        str.replace(i, 5, "apple");i+=4;
    }
  }
 
  cout << str << endl;
}

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

using namespace std;

int main(void)
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  double A, B;
  int H, M;
  cin >> A >> B >> H >> M;
  double theta
    = fabs(2 * M_PI * H / 12 + (2 * M_PI * M / 720) - M * M_PI / 30);
  cout << fixed << setprecision(20) << sqrt(A * A + B * B - 2 * A * B * cos(theta));
  return 0;
}


Measured memory_bytes: 4240
n,a=map(int,input().split())
x=list(map(int,input().split()))
y=[i-a for i in x]
y.sort()

ma=max(max(y),-1*min(y))*n
ny=ma*2
dp=[[0]*(ny+1) for _ in range(n+1)]
for i in range(n+1):
  for j in range(ny+1):
    if i==0 and j==ma:
      dp[i][j]=1
    elif i>=1:
      sa=j-y[i-1]
      if sa<0 or sa>ny:
        dp[i][j]=dp[i-1][j]
      else:
        dp[i][j]=dp[i-1][j]+dp[i-1][sa]
print(dp[n][ma]-1)

Measured memory_bytes: 5236
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,m,n) for(int i=m;i<n;i++)
const ll mod=1e9+7;

int main(){
  string s;
  cin>>s;
  int n=s.size();
  int cur=0;
  rep(i,0,n){
    cur=cur+(s[i]-'0');
    cur%=9;
  }
  if(cur==0) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}

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

int main(){
    ll h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, 0, h) cin >> s[i];
    rep(i, 0, w+2) cout << '#';
    cout << endl;
    rep(i, 0, h) cout << '#' << s[i] << '#' << endl;
    rep(i, 0, w+2) cout << '#';
    cout << endl;
    return 0;
}

Measured memory_bytes: 3640
#include <iostream>

using namespace std;

int main() {
  int N, d, D[10]{};
  cin >> N >> d;
  while (cin >> d) D[d] = 1;
  for(int i = N;; ++i) {
    int j{i}, hate{};
    while(j) hate |= D[j%10], j /= 10;
    if (!hate) {
      cout << i << endl;
      break;
    } 
  }
}


Measured memory_bytes: 3580
import java.util.*;
class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
      	int length = input.nextInt();
      	
      	int[] nums = new int[length];
      
      	for(int i = 0; i < nums.length; i++){
        	nums[i] = input.nextInt();
        }
      
		int[] dp = new int[nums.length];
		dp[1] = Math.abs(nums[0] - nums[1]);
		dp[0] = 0;
		
		for(int i = 2; i < nums.length; i++){
			dp[i] = Math.min(Math.abs(nums[i-1] - nums[i]) + dp[i-1], Math.abs(nums[i-2] - nums[i]) + dp[i-2]);
		}
		
		System.out.println(dp[nums.length -1]);
	}





}


Measured memory_bytes: 51052
#include<bits/stdc++.h>
#include <math.h>
#include <algorithm>
#include <functional>
#include<iostream>
#include <string>
using namespace std;
typedef long long ll;
#define pb pushback
#define fr(i,n) for(int i=0;i<n;i++)
#define fro(i,j,n) for(int i=j;i<n;i++)
#define ifr(i,n) for(int i=n-1;i>=0;i--)


int main() {
    int x,y,z;
	cin >> x>> y;
    z = max(x+y,x-y);
      cout << max(z,x*y) << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using ll  = long long;
#define int ll
using namespace std;
signed main(){
    vector<int>dp(51,1LL),dp2(51,1LL);
    int n,x; cin >> n >> x;
    for(int i = 1; i < dp.size();++i){
        dp[i] = dp[i-1] + dp[i-1] + 3;
        dp2[i] = dp2[i-1] + dp2[i-1] + 1;
    }
    function<int(int,int)>cal = [&](int i,int x) -> int{
        if(x == 0){ return 0;    }
        if(i == 0){ return 1;    }
        if(x >= dp[i-1] + 2){ return  dp2[i-1] + 1 + cal(i-1,x-dp[i-1]-2);}
        else return cal(i-1,x-1);
    };
    cout << cal(n,x) << endl;
}





Measured memory_bytes: 256
s = list(input())
t = list(input())
n = len(s) - len(t) + 1

ans = len(t)
for i in range(n) :
  new_s = s[i:i+len(t)]
  k = 0
  for j in range(len(t)):
    if new_s[j] != t[j] :
      k += 1
  ans = min(ans,k)
print(ans)

Measured memory_bytes: 9076
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i,n) for(int i = 0;i < n;i++)
#define NIL = -1;

int digit(ll x) {
    if (x / 10 == 0) return 1;
    return digit(x / 10) + 1;
}

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

ll lcm(ll a,ll b){
    a / gcd(a,b) * b;
}
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 7;

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


/////////////////////////////////////////////////////////////////////////////////////////
map<int,int> num;

int check(int n){
    while(n){
        if (num[n % 10] != 0){
            return 0;
        }

        n /= 10;
    }

    return 1;
}

int main(){
    int N,k;
    cin >> N >> k;
    for (int i = 0;i < k;i++){
        int d;
        cin >> d;
        num[d]++;
    }

    for (int i = N;i <= N * 10;i++){
        if (check(i)){
            cout << i << endl;
            return 0;
        }
    }
}

Measured memory_bytes: 256
## main

n, m = gets.strip.split(' ').map{|s| s.to_i}

as = n.times.map{gets.strip.to_i}
bs = m.times.map{gets.strip.to_i}

votes = n.times.map{0}

for bj in bs
  for i in (0...n)
    if as[i] <= bj
      votes[i] += 1
      break
    end
  end
end
#p votes

max_v = 0
max_i = nil

for i in (0...n)
  if max_v < votes[i]
    max_v = votes[i]
    max_i = i
  end
end

puts (max_i + 1)

Measured memory_bytes: 6128
N= int(input())
X = list(map(int,input().split()))

pmin = 99999999999999999

bsum = []
for count1 in range(0,N):
    asum = []
    for count2 in range(1,100):
        asum.append(abs(X[count1]-count2)**2)
    bsum.append(asum)


for count3 in range(0,99):
    asum = 0
    for count4 in bsum:
        asum += count4[count3]
    pmin = min(pmin,asum)

print(pmin)

Measured memory_bytes: 3316
#include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
const ll INF=100000000000000001;
struct edge{int to,cost;};
struct pos{
	int x,y,cost;
};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};

P1 d[100001]={};

int main() {

	int k;
	cin>>k;
	rep(i,10){
		d[i+1].first=i+1;
		d[i+1].second=i+1;
	}
	if(k<10){
		cout<<d[k].first<<endl;
		return 0;
	}
	ll s=10,t=1,g=9;
	while(s<=k){
		for(int i=t; i<=g; i++){
			if(d[i].second>0){
				d[s].first=d[i].first*10+d[i].second-1;
				d[s].second=d[i].second-1;
				s++;
			}
			d[s].first=d[i].first*10+d[i].second;
			d[s].second=d[i].second;
			s++;
			if(d[i].second<9){
				d[s].first=d[i].first*10+d[i].second+1;
				d[s].second=d[i].second+1;
				s++;
			}
			if(s>k){
				cout<<d[k].first<<endl;
				return 0;
			}
		}
		t=g+1;
		g=s;
	}

	return 0;
}


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

#define ll long

using namespace std;

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

    ll result = 0;

    result += (s / 500) * 1000;
    s %= 500;

    result += (s / 5) * 5;

    cout << result << endl;
    return 0;
}

Measured memory_bytes: 256
n,C,*H=map(int,open(0).read().split());p=[0];P=[0]
for i in range(1,n):
 h=H[i];g=lambda x,y:(p[P[y]]+H[P[y]]*(H[P[y]]-2*h)-p[P[x]]-H[P[x]]*(H[P[x]]-2*h))/(H[P[x]]-H[P[y]]);
 while len(P)>1and g(1,0)>0:P.pop(0)
 p+=[p[P[0]]+(H[P[0]]-h)**2+C];P+=[i];h=0
 while len(P)>2and g(-1,-2)>g(-2,-3):P.pop(-2)
print(p[-1])

Measured memory_bytes: 27120
//Code by 27.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<fstream>
#include<stdlib.h>
#include<set>
#include<climits>
#include<cmath>  
#include<memory.h> 
#include<sstream>
#include<time.h>
#include<iomanip>
using namespace std;
const unsigned long long BIGEST=1000000000000000000+1000000000000000000;
const long long BIGER=1000000000000000000;
const int BIG=1000000000;
const int MOD=1000000007;
string st;
int main()
{
	cin>>st;
	long long s=0;
	int g=0;
	for(int i=0;i<st.size();i++)
	{
		if(st[i]=='W')s+=i-(g++);
	}
	cout<<s;
	return 0;
}

Measured memory_bytes: 640
#include<stdio.h>
int main(void)
{
        int n,p[1000],i,max,s=0;
        scanf("%d",&n);
        scanf("%d",&max);
        s=s+max;
        for(i=1;i<n;i++){
                scanf("%d",&p[i]);
                s=s+p[i];
                if(p[i]>max) max=p[i];
        }
        printf("%d\n",s-max/2);
        return 0;
}


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

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

    int sum =0;
    for(int i=0;i<N;i++){
        sum += W[i];
    }
    int min = 10000;
    for(int t=0;t<N;t++){
        int S1 =0;
        for(int j=0;j<t;j++){
            S1 += W[j];
        }
        int S2 = sum-S1;
        if(min > abs(S1-S2)){
            min = abs(S1-S2);
        }
    }
    cout << min << endl;

}

Measured memory_bytes: 256
x,k,d=map(int,input().split())
x=abs(x)
kai=x//d
if k < kai:
    print(x-d*k)
else:
    k-=kai
    if k%2 ==0:
        y=x-(d*kai)
    else:
        y=x-(d*(kai+1))
    print(abs(y))

Measured memory_bytes: 9204
#include<stdio.h>

int main(void)
{
	int i,j,n,num,cards[4][13] = {0}; //????????????????¬??????????????????? S->H->C->D
	char mark;

    scanf("%d", &n); 
    while(n)
    {
        scanf(" %c %d", &mark, &j);
         
        if(mark == 'S') i = 0;  
        else if(mark == 'H') i = 1; 
        else if(mark == 'C') i = 2;
        else  i = 3;  
         
        cards[i][j-1] = 1;
        n--;   
    }
	
	for(i=0;i<4;i++){
		for(j=0;j<13;j++){
			if(cards[i][j] == 0){
				if(i == 0){
					printf("S %d\n",j+1);
				} else if(i == 1){
					printf("H %d\n",j+1);				
				} else if(i == 2){
					printf("C %d\n",j+1);				
				} else {
					printf("D %d\n",j+1);				
				}
			}
		}
	}

	return 0;
}

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

#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define all(a) (a).begin(),(a).end()
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}

string x(const string &a, const string &b){
  string ans(a.size(),'0');
  rep(i,a.size()){
    if(a[i] == b[i]) ans[i] = '0';
    else ans[i] = '1';
  }
  return ans;
}

signed main(){
  int n, m;
  while(cin >> n >> m, n){
    vector<string> b(n);
    rep(i,n) cin >> b[i];

    map<string,int> dp; // dp[i]:集合iのときの個数
    string zero(m,'0');
    dp[zero] = 0;
    rep(i,n){
      map<string,int> next = dp;
      for(auto &p : dp)
	chmax(next[x(p.first,b[i])],p.second+1);
      dp = move(next);
    }
    cout << dp[zero] << endl;
  }
}


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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  int Alice = 0, Bob = 0;
  for (int i = 0; i < N; i++) {
    if (i % 2 == 0) {
      Alice += A.at(i);
    }
    else {
      Bob += A.at(i);
    }
  }
  cout << Alice - Bob << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 ll INF = 1LL<<60;

int main()
{
  ll h,n;cin>>h>>n;
  int sum=0,hoge=0;
  for(ll i=0;i<n;i++){
    cin>>hoge;
    sum+=hoge;
  }
  string ans = sum>=h ? "Yes":"No";
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef pair<pii,pii> pippi;
typedef tuple<ll,ll,ll> tl;
typedef pair<double,double> pdd;
typedef vector<vector<ll>> mat;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll mod5=1000000009;
ll inf=1LL<<61;
int iinf=1<<30;
double pi=3.141592653589793238462643383279L;
double eps=1e-8;
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
#define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++)
#define mrep(itr,mp) for(auto& itr:mp)
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
//#define endl "\n"
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
#define umh unordered_map<int,int,custom_hash>
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    ll c=gcd(a,b);
    return a*b/c;
}
ll Pow(ll n,ll k){
    if(k<0)return 0;
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
    now%=md;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
ll popcount(ll n){
    ll ret=0;
    ll u=n;
    while(u>0){
        ret+=u%2;
        u/=2;
        
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll n,m;cin>>n>>m;
    ll k;cin>>k;
    ll a[n],b[m];
    rep(i,0,n)cin>>a[i];
    rep(i,0,m)cin>>b[i];
    ll ans=0;
    ll sum=0;
    ll now=n-1;
    rep(i,0,n){
        if(sum+a[i]<=k)sum+=a[i];
        else{
            now=i-1;
            break;
        }
    }
    ll sum2=0;
    ans=now+1;
    rep(i,0,m){
        sum2+=b[i];
        while(now>=0&&sum+sum2>k){
            sum-=a[now];
            now--;
        }
        if(sum2+sum>k)break;
        Max(ans,i+1+now+1);
    }
    cout<<ans<<endl;
}


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

const int N = (1e5) + 5;
char s[N];

// claudio van
int main() {
  scanf("%s", s);
  int n = strlen(s);
  int cnt1 = 0, cnt2 = 0;
  int val1 = 0, val2 = 1;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0' + val1) {
      cnt1++;
    }
    val1 ^= 1;
    if (s[i] == '0' + val2) {
      cnt2++;
    }
    val2 ^= 1;
  }
  printf("%d\n", min(cnt1, cnt2));
  return 0;
}

Measured memory_bytes: 384
from sys import stdin, setrecursionlimit
from collections import defaultdict
from math import gcd

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


MOD = 1000000007
N, *AB = map(int, stdin.buffer.read().split())
d = defaultdict(int)
zeros = 0
for a, b in zip(AB[::2], AB[1::2]):
    if a == 0 and b == 0:
        zeros += 1
    else:
        g = gcd(a, b)
        a //= g
        b //= g
        if a < 0:
            a *= -1
            b *= -1
        if a == 0 and b == -1:
            b = 1
        d[(a, b)] += 1

ans = 1
free = 0
keys = sorted(d.keys())
for a, b in keys:
    n = d[(a, b)]
    if b > 0:
        if (b, -a) in d:
            m = d[(b, -a)]
            ans = ans * (pow(2, n, MOD) + pow(2, m, MOD) - 1) % MOD
        else:
            free += n
    else:
        if (-b, a) not in d:
            free += n

ans = (ans * pow(2, free, MOD) + zeros - 1) % MOD
print(ans)


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

int main(){
  int H,W;

  while(1){
    cin >> H >> W;
    
    if(H==0 && W==0){
      break;
    }

    for(int j=0; j<H; j++){
      for(int i=0; i<W; i++){
        if(j==0 || j==H-1 || i==0 || i==W-1){
          cout << "#";
        }else{
          cout << ".";

        }
      }
      cout << endl;
    }
    

    cout << endl;
  }
  
  return(0);
}

Measured memory_bytes: 1152
package main

import (
	"fmt"
)

func main() {
	var n,r int
	fmt.Scan(&n,&r)
	
	ir := 0
	if n >= 10 {
		ir = r
	} else {
		ir = r + 100 * ( 10 - n )
	}
	fmt.Println(ir)
}

Measured memory_bytes: 512
n = int(input())
bt = 0
bx = 0
by = 0
for _ in range(n):
  t, x, y = map(int, input().split())
  dt = abs(x - bx) + abs(y - by)
  if dt > t - bt or dt % 2 != (t - bt) % 2:
    print("No")
    break
  bt = t
  bx = x
  by = y
else:
  print("Yes")

Measured memory_bytes: 8968
from collections import deque
import sys
input = sys.stdin.readline

def main():
    n = int(input())

    def calc_idx(x, y):
        if x > y:
            x, y = y, x
        return x + y*(y-1)//2

    # create graphs & calc indegree
    n_game = n * (n-1) // 2
    edge = [[] for i in range(n_game)]
    indegree = [0] * n_game
    for i in range(n):
        ai = list(map(int, input().split()))

        for j in range(1, n-1):
            g1 = calc_idx(i, ai[j-1]-1)
            g2 = calc_idx(i, ai[j]-1)
            edge[g1].append(g2)
            indegree[g2] += 1


    ans = 0
    cand = deque([(i, 1) for i in range(n_game) if indegree[i] == 0])
    n_ts = 0
    while len(cand) > 0:
        u, day = cand.popleft()
        ans = max(ans, day)
        n_ts += 1
        for v in edge[u]:
            indegree[v] -= 1
            if indegree[v] == 0:
                cand.append((v, day+1))

    if n_ts != n_game:
        ans = -1

    print(ans)

if __name__ == '__main__':
    main()


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

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
string to_str(ll x){string res; while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return res;}
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 end



int main(){
   int n,e,t; scanf("%d%d%d",&n,&e,&t);
   vector<int> a(n+1,0); rep(i,1,n+1)scanf("%d",&a[i]);
   ll dp[100010]; rep(i,0,n+1)dp[i]=INF; dp[0]=0;
   int idx=0; ll mi=INF;
   rep(i,1,n+1){
      while((a[i]-a[idx+1])*2>=t){
         chmin(mi,dp[idx]-2*a[idx+1]); idx++;
      }
      chmin(dp[i],min(mi+a[i]*2LL,dp[idx]+t));
   }
   printf("%lld\n",dp[n]+e);
   return 0;
}

Measured memory_bytes: 1408
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<map>
#define ll long long
#define MM(arr,x) memset(arr,x,sizeof(arr))
#define INF 10000000
using namespace std;
int main()
{
	string s;
	cin>>s;
	ll sum=0;
	for(int i=0;i<s.length();i++)
	{
		sum+=s[i]-'0';
		sum=sum%9;
	}
	if(sum%9==0||sum==0) cout<<"Yes\n";
	else cout<<"No\n";
	return 0;
}

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

using namespace std;
typedef long long ll;

int main()
{
    int N;
    cin >> N;
    vector<ll> a(N), b(N);
    for(int i=0;i<N;++i){
        cin >> a[i];
    }
    for(int i=0;i<N;++i){
        cin >> b[i];
    }

    ll suma=0, sumb=0;
    for(int i=0;i<N;++i){
        if(a[i] < b[i]){
            sumb += (b[i] - a[i]) / 2;
        }
        else{
            suma += a[i] - b[i];
        }
    }

    if(suma <= sumb){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}


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

#define INF (1<<29)

int main(){
  int e;
  while(cin >> e, e){
    int ans = INF;
    for(int z = 0 ; z*z*z <= e ; z++){
      for(int y = 0 ; y*y +z*z*z <= e ; y++){
	int x = e-(z*z*z+y*y);
	ans = min(ans, x+y+z);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Measured memory_bytes: 1160
fun main(args: Array<String>) {
  val n = Integer.parseInt(readLine() ?: "0")
  val alist = readLine()?.split(" ") ?: listOf()
  val a = alist.map{Integer.parseInt(it)}
  
  val l = IntArray(n)
  val r = IntArray(n)
 
  for (i in 0 until n) {
    l[i] = if (i == 0) {
      gcd(a[0], 0)
    } else {
      gcd(a[i], l[i - 1])
    }
  }
 
  for (i in n-1 downTo 0) {
    r[i] = if (i == n - 1) {
      gcd(a[i], 0)
    } else {
      gcd(a[i], r[i + 1])
    }
  }
 
  var ans = 0
  for (i in 0 until n) {
    val d = when (i) {
      0 -> r[i + 1]
      n - 1 -> l[i - 1]
      else -> gcd(l[i - 1], r[i + 1])
    }
    ans = Math.max(ans, d)
  }
  
  println(ans)
}

fun gcd(a: Int, b: Int): Int {
    if (b == 0) return a
    return gcd(b, a % b)
}

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

void searchMinProbNum(vector<int> &p, vector<long>& c, int &minProbNum, int tmpProbNum, int goal, int const D) {
  
  for (int i = 0; i < D; i++) {
    int tmp = p.at(i);
    if (tmp == 0) {
      continue;
    }
    
    int point = tmp * (i + 1) + c.at(i);
    if (goal <= point) {
      point = 0;
      int j = 1;
      while (j < tmp) {
        point += (i + 1);
        if (goal <= point) {
          break;
        }
        j++;
      }
      minProbNum = min(tmpProbNum + j, minProbNum);
      //cout << i << " " << probNum << endl;
      continue;
    }
    
    p.at(i) = 0;
    searchMinProbNum(p, c, minProbNum, tmpProbNum + tmp, goal - point, D);
    p.at(i) = tmp;
  }
}  

int main() {
  int D;
  long long G;
  cin >> D >> G;
  G /= 100;
  
  vector<int> p(D);
  vector<long> c(D);
  for (int i = 0; i < D; i++) {
    long b;
    cin >> p.at(i) >> b;
    c.at(i) = b / 100; 
  }
  
  int minProbNum = 1000;
  searchMinProbNum(p, c, minProbNum, 0, G, D);
  cout << minProbNum;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int a,c,n;
int p,q;
int rec(int i,int last,int s,int c,int d){
  int x=0,y;
  if(c*q==d*p) return 1;
  if(i>=n) return 0;
  for(y=last;s*y<=a;y++){
    if(c*y*q+d*q<=p*d*y)
      x+=rec(i+1,y,s*y,c*y+d,d*y);
  }
  return x;
}
int main(){
  cin >> p >> q >> a >> n;
  while(p!=0||q!=0||a!=0||n!=0){
    cout << rec(0,1,1,0,1) << endl;
    cin >> p >> q >> a >> n;
  }
  return 0;
}

Measured memory_bytes: 1160
#include <set>
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <queue>

inline int getInt(){ int s; scanf("%d", &s); return s; }

typedef long long ll;

using namespace std;

int check(int a, int m){
  vector<int> visit(m);
  int now = 0;

  while(!visit[now]){
    visit[now] = 1;
    now = (now + a) % m;
  }

  return accumulate(visit.begin(), visit.end(), 0);
}

int main(){
  int m = getInt();
  int n = getInt();
  int a = getInt();
  int b = getInt();
  int c = getInt();
  int d = getInt();

  a %= m;
  b %= n;
  c %= m;
  d %= n;

  int acnt = check(a, m);
  int bcnt = check(b, n);

  ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
  ll ans2 = 0;

  vector<int> h(m, -1);
  int hh = 0;
  int xx = 0;
  int yy = 0;

  while(h[xx] == -1){
    h[xx] = yy;
    xx = (xx + a) % m;
    yy = (yy + b) % n;
    hh++;
  }

  int ym = ans1 / hh;
  int yd = n / ym;

  xx = c;
  yy = d;

  while(true){
    ans2++;

    if(ans2 == 300 * n){
      int ccnt = check(c, m);
      int dcnt = check(d, n);
      ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);
      break;
    }

    if(h[xx] != -1){
      if((yy - h[xx] + n) % yd == 0)
        break;
    }

    xx = (xx + c) % m;
    yy = (yy + d) % n;
  }

  printf("%lld\n", ans1 * ans2 - 1);

  return 0;
}

Measured memory_bytes: 1144
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) { REP(i,n) cout<<v[i]<< ' '; cout << endl; }

void inline TorF(bool tf, const string &trueMessage, const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

int main() {
  int N; cin>>N;
  double ans = 0.0;
  REP(i, N) {
    double a; cin>>a;
    ans	+= (1.0	/ a);
  }
  ans =	1.0 / ans;
  printf("%0.10f\n", ans);
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
signed main(){
  int a,b,c,d;cin>>a>>b>>c>>d;
  bool flag;
  while(true){
    c-=b;
    if(c<=0){
      flag=true;
      break;
    }
    a-=d;
    if(a<=0){
      flag=false;
      break;
    }
  }
	if(flag){
    cout<<"Yes"<<endl;
  }
  else{
    cout<<"No"<<endl;
  }
  return 0;
}


Measured memory_bytes: 3632
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#define f(i,n) for(int i=0;i<n;i++)
#define mod 10007
#define chk(d) cout<<d<<endl
using namespace std;


int main()
{ int n,m,sum=0;
  cin>>n;
  vector<int>v;
  f(i,n)
  {
      int a;
      cin>>a;
      v.push_back(a);
  }
  int dp[n];
  f(i,n)dp[i]=mod;
  dp[0]= 0;
    dp[1] = abs(v[1]-v[0]);
  for(int i = 1;i<n-1;i++)
  {
      dp[i+1] = min(dp[i]+abs(v[i]-v[i+1]), dp[i-1] + abs(v[i-1]-v[i+1]));
     // dp[i+1] = min(dp[i]+abs(v[i-1]-v[i+1])-abs(v[i]-v[i-1]),dp[i+1]);

  }
 // f(i,n)cout<<dp[i]<<" ";
 cout<<dp[n-1];

}



Measured memory_bytes: 1144
import sys
input = sys.stdin.buffer.readline

K = int(input())

A, B = map(int, input().split())


def main(K, A, B):
    for i in range(A, B + 1):
        if i % K == 0:
            print('OK')
            sys.exit()
    print('NG')


if __name__ == '__main__':
    main(K, A, B)


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

struct dice{
  int face[6];
};

int main(){
  dice f;
  int dmy;
  string s;
  for(int i=0;i<6;i++) cin >>f.face[i];
  cin >>s;
  for(int i=0;i<s.size();i++){
    switch(s[i]){
    case 'N':
      dmy=f.face[0];
      f.face[0]=f.face[1];
      f.face[1]=f.face[5];
      f.face[5]=f.face[4];
     f.face[4]=dmy;
        break;
    case 'S':
      dmy=f.face[0];
      f.face[0]=f.face[4];
      f.face[4]=f.face[5];
      f.face[5]=f.face[1];
      f.face[1]=dmy;
      break;
    case 'E':
      dmy=f.face[0];
      f.face[0]=f.face[3];
      f.face[3]=f.face[5];
      f.face[5]=f.face[2];
      f.face[2]=dmy;
      break;
    case 'W':
      dmy=f.face[0];
      f.face[0]=f.face[2];
      f.face[2]=f.face[5];
      f.face[5]=f.face[3];
      f.face[3]=dmy;
      break;
    }
  }
  cout <<f.face[0]<<endl;
  return 0;}

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

int main(){
	long long int a,b,c,d;
	cin>>a>>b>>c>>d;
	long long int x = (b/c) - ((a-1)/c);
	long long int y = (b/d) - ((a-1)/d);
	long long int g = __gcd(c,d);
//g 	cout<<g<<endl;
	long long int gcd = c*d/g;
	long long int z = (b/gcd) - ((a-1)/gcd);
	long long int ans = b-a+1-x-y+z;
	cout<<ans;
}

Measured memory_bytes: 256
import java.util.*;

public class Main{
	private static final Scanner scan = new Scanner(System.in);

	public static void main(String[] args){
		int n = scan.nextInt();
		int[] x = new int[n];
		int[] y = new int[n];
		for(int i = 0; i < n; i++){
			x[i] = scan.nextInt();
		}
		for(int i = 0; i < n; i++){
			y[i] = scan.nextInt();
		}
		System.out.printf("%f\n", calcD(x, y, 1));
		System.out.printf("%f\n", calcD(x, y, 2));
		System.out.printf("%f\n", calcD(x, y, 3));
		System.out.printf("%f\n", calcD(x, y, Double.POSITIVE_INFINITY));
	}

	public static double calcD(int[] x, int[] y, double p){
		if(p == Double.POSITIVE_INFINITY){
			double max = 0;
			for(int i = 0; i < x.length; i++){
				max = Math.max(max,Math.abs(x[i] - y[i]));
			}
			return max;
		} else{
			double sig = 0;
			for(int i = 0; i < x.length; i++){
				sig += Math.pow(Math.abs(x[i] - y[i]), p);
			}
			double d = Math.pow(sig, 1 / p);
			return d;
		}
	}
}

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

# define REP(i,n) for (int i=0;i<(n);++i)
# define rep(i,a,b) for(int i=a;i<(b);++i)
# define p(s) std::cout << s ;
# define pl(s)  std::cout << s << endl;
# define printIf(j,s1,s2) cout << (j ? s1 : s2) << endl;
# define YES(j) cout << (j ? "YES" : "NO") << endl;
# define Yes(j) std::cout << (j ? "Yes" : "No") << endl;
# define yes(j) std::cout << (j ? "yes" : "no") << endl;
# define all(v) v.begin(),v.end()
# define showVector(v) REP(i,v.size()){p(v[i]);p(" ")} pl("")
template<class T> inline bool chmin(T &a, T b){ if(a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b){ if(a < b) { a = b; return true;} return false;}
typedef long long int ll;
typedef pair<ll,ll> P_ii;
typedef pair<double,double> P_dd;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}

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

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}


const int MOD = 1000000007;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;

void addM(long long &a, long long b) {
    a += b;
    if (a >= MOD) a -= MOD;
}

void mulM(long long &a, long long b) {
    a = ((a%MOD)*(b%MOD))%MOD ;
}

// a^b mod M
long myPow(long a,long b,int M) {
    long ret = 1;
    long tmp = a;
    while(b>0) {
        if((b&1)==1) ret = (ret * tmp) % M;
        tmp = (tmp * tmp) % M;
        b = b>>1;
    }
    return ret;
}

 // nCk mod M
int nCk(int n,int k,int M) {
    long ret = 1;
    int mi = min(k, n-k);
    for(int i=1;i<=mi;i++) {
        ret = (ret * myPow(i,M-2,M)) % M;
    }
    for(int i=n-mi+1;i<=n;i++) {
        ret = (ret * i) % M;
    }
    return (int)ret;
};

vector<pair<long long, long long> > prime_factorize(long long n) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= n; ++p) {
        if (n % p != 0) continue;
        int num = 0;
        while (n % p == 0) { ++num; n /= p; }
        res.push_back(make_pair(p, num));
    }
    if (n != 1) res.push_back(make_pair(n, 1));
    return res;
}


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

    REP(i,K){
        if(i%2==0){
            if(a%2==1) a--;
            a /= 2;
            b += a; 
        } else {
            if(b%2==1) b--;
            b /= 2;
            a += b; 
        }
    }

    printf("%d %d\n",a,b);

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int n,m,t,x[100010];
long long f[100010],k;
int main()
{
	int i,j;
	scanf("%d%d%d",&n,&m,&t);
	for(i=1;i<=n;i++)
		scanf("%d",&x[i]);
	for(i=1,j=0,k=1e18;i<=n;i++)
	{
		for(;2*(x[i]-x[j+1])>t;j++)
			k=min(k,f[j]-2*x[j+1]);
		f[i]=min(k+2*x[i],f[j]+t);
	}
	cout<<f[n]+m<<"\n";
	return 0;
}

Measured memory_bytes: 1408