akhauriyash/Code-Regression huggingface dataset:
my ($n,$k) = split(/\s+/, <>);
my (@a) = (0, split(/\s+/, <>));
my @visited = ();

my $c = 1;
my $pos = 1;
while (!$visited[$pos]) {
    $visited[$pos] = $c;
    $pos = $a[$pos];
    $c++;
}

$c -= $visited[$pos];

my $current = 1;
while ($k-->0) {
    $current = $a[$current];
    if ($current == $pos) {
	$k %= $c;
    }
}

print "$current\n";


Measured memory_bytes: 32576
#include <iostream>
#include <sstream>
#include <vector>
#include <cassert>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <utility>
#include <tuple>
#include <regex>
#include <map>

using namespace std;
using ull = unsigned long long;

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


	string str = "No"; // or Yes
	if (A > B && A > C && C > B) {
			str = "Yes";
	}
	if (B > A && B > C && C > A) {
		str = "Yes";
	}

	std::cout << str << endl;

	return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1<<20);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
//???????????????????????\??¨??¨??????
vector<pair<int, int> >adj[MAX];

void dijkstra(){
    //?????\??¨
    priority_queue<pair<int, int>,vector<pair<int,int> >,greater<pair<int,int> > >PQ;
    //?????????????¢????
    int color[MAX];

    int d[MAX];
    for(int i = 0; i<n; i++){
        d[i] = INFTY;
        color[i] = WHITE;
    }

    d[0] = 0;
    PQ.push(make_pair(0,0));
    color[0] = GRAY;

    while(!PQ.empty()){
        pair<int, int>f = PQ.top();
        PQ.pop();

        int u = f.second;

        color[u] = BLACK;

        // ??????????°???????????????????????????????????????\
        if(d[u]<f.first)
            continue;

        for(int j = 0;j<adj[u].size(); j++){
            int v = adj[u][j].first;
            if(color[v]==BLACK)continue;
            if(d[v]>d[u]+adj[u][j].second){
                d[v]=d[u]+adj[u][j].second;
                //priority_queue ?????????????????§??????????????????-1
                PQ.push(make_pair(d[v],v));
                color[v]=GRAY;
            }
        }
    }

    for(int i=0;i<n;i++)
        cout<<i<<" "<<(d[i]==INFTY?-1:d[i])<<endl;
}

int main(){
    int k,u,v,c;

    cin>>n;
    for(int i=0;i<n;i++){
        cin>>u>>k;
        for(int j=0;j<k;j++){
            cin>>v>>c;
            adj[u].push_back(make_pair(v,c));
        }
    }

    dijkstra();
}

Measured memory_bytes: 8044
#include <cstdio>
 #include <cstdlib>
 #include <iostream>
 #include <fstream>
 #include <sstream>
 #include <set>
 #include <map>
 #include <vector>
 #include <list>
 #include <algorithm>
 #include <cstring>
 #include <cmath>
 #include <string>
 #include <stack>
 #include <queue>
 #include <bitset>     //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
 #include <cassert>
 #include <iomanip>        //do setprecision
 #include <ctime>
 #include <complex>
 using namespace std;

 #define FOR(i,b,e) for(int i=(b);i<(e);++i)
 #define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
 #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
 #define REP(x, n) for(int x = 0; x < (n); ++x)

 #define ST first
 #define ND second
 #define PB push_back
 #define PF push_front
 #define MP make_pair
 #define LL long long
 #define ULL unsigned LL
 #define LD long double
 #define pii pair<int,int>
 #define pll pair<LL,LL>

 const double pi = 3.14159265358979323846264;
 const int mod=1000000007;

int main(){
	int n,k;
	cin>>n>>k;
	LL a[1111]={};
	FOR(i,1,n+1){
		cin>>a[i];
		a[i]+=a[i-1]*1LL;
	}
	vector<LL> val;
	FOR(i,1,n+1){
		FOR(j,i,n+1){
			val.PB(a[j]-a[i-1]);
		}
	}
	LL ans=0;
	for(int i=40;i>=0;i--)
	{
		LL tmp=1LL<<i;
		vector<LL> val2;
		int cnt=0;
		int l=val.size();
		for(int j=0;j<l;j++)
            {
			if(val[j]&tmp){
				val2.PB(val[j]);
				cnt++;
			}
		}
		if(cnt>=k){
			val=val2;
			ans+=tmp;
		}
	}
	cout<<ans<<endl;
	 return 0;
}


Measured memory_bytes: 6124
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; //10e18+10e17ちょっとくらい
const int MOD = 1000000007;
const int MAX_N = 200100;

int a[200010];
stack<int> q[200010];
unordered_map<int, bool> mp;
signed main()
{
    //  以降 cin の入力元が 'input.txt' になる
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());
    int n, k;
    cin >> n >> k;
    rep(i, n){
        cin >> a[i+1];
    }
    bool flg;
    rep(iii, k){
        flg = true;
        rep(i, n){
            int l = i+1 - a[i+1];
            int r = i+1 + a[i+1];
            l = max(1LL, l);
            l = min(l, n);
            r = max(1LL, r);
            r = min(r, n);
            q[l].push(i+1);
            q[r].push(i+1);
        }
        rep(i, n){
            int now = 0;
            while(!q[i+1].empty()){
                int tmp = q[i+1].top();
                q[i+1].pop();
                if(mp.find(tmp) != mp.end()){
                    mp.erase(tmp);
                    now++;
                }
                else{
                    mp[tmp] = true;
                }
            }
            a[i+1] = now+mp.size();
            if(a[i+1] < n - 1)
                flg = false;
        }
        if(flg)
        {
            if(iii == k-1){
                flg = false;
            }
            break;
        }
    }
    if(flg){
        rep(i, n){
            cout << n << " ";
        }
    }
    else{
        rep(i, n){
            cout << a[i+1] << " ";
        }
    }
    cout << endl;


    return 0;
}

Measured memory_bytes: 149164
# coding: utf-8
card = [input() for _ in range(3)]
ans = {0: 'A', 1: 'B', 2: 'C'}
nxt = {'a': 0, 'b': 1, 'c': 2}
turn = 0
while True:
    if card[turn] == '':
        print(ans[turn])
        break
    t = nxt[card[turn][0]]
    card[turn] = card[turn][1:]
    turn = t


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

int main() {
  int N; cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i);
  map<long long, long long> M;
  for (int i = 0; i < N; i++) {
    if (M.count(A.at(i))) M[A.at(i)]++;
    else M[A.at(i)] = 1LL;
  }
  long long ans = 0LL;
  for (auto p : M) {
    ans += p.second * (p.second - 1) / 2;
  }
  for (int i = 0; i < N; i++) {
    long long tmp = ans;
    long long cnt = M.at(A.at(i));
    tmp -= cnt * (cnt - 1) / 2;
    tmp += max(0LL, (cnt - 1) * (cnt - 2) / 2);
    cout << tmp << endl;
  }
}

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

typedef long long LL;
typedef long double LD;
const int mod = 1000000007;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int f1 = 0;
    while(a > 0 && c > 0){
        c -= b;
        if(c <= 0){
            f1 = 1;
            break;
        }
        a -= d;
        if(a <= 0){
            f1 = 0;
            break;
        }
    }
    if(f1 != 0)cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 3652
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;

typedef long long ll;

/*
const ll M = 1000000007;
vector<ll> fac(3001);
vector<ll> ifac(3001);
ll mpow(ll x, ll n){
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % M;
        x = x*x % M;
        n = n >> 1;
    }
    return ans;
}
 
ll comb(ll a, ll b){
    if(a == 0 && b == 0)return 1;
    if(a < b || a < 0 || b < 0)return 0;
    ll tmp = ifac[a-b]* ifac[b] % M;
    return tmp * fac[a] % M;
}
*/


ll lcm(ll a,ll b){
 
  ll r, x, tmp;
  x = a * b;
  if(a<b){
    tmp = a;
    a = b;
    b = tmp;
  }
 
  r = a % b;
  while(r!=0){
    a = b;
    b = r;
    r = a % b;
  }
  return x/b;
}

ll gcd(ll a ,ll b){
    if(a==0){return b;}
    else if(b==0){return a;}
    else{
    ll tmp,r;
    if(a<b){
        tmp = a;
        a = b;
        b = tmp;
    }
    
    r = a % b;
    while(r!=0){
        a = b;
        b = r;
        r = a % b;
    }
    return b;
    }
}




int main(void){
    const ll M = 1000000007;
    ll ans = 0;
    ll m,d;
    cin >> m >> d;
    for(int i=1;i<=m;i++){
        for(int j=2;j<=(d/10);j++){
            for(int k=2;k<=9&&k<=(d-10*j);k++){
                if(i==j*k){ans++;}
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include<map>
#include <cassert>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
    int N;
    cin >> N;
    int count = 0;
    while (N != 0) {
        if (N % 10 == 7) {
            count++;
        }
        N = N / 10;
    }
    if (count > 0) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }

    return 0;
}



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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
#define rep(i,n) for(i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << endl;}
#define INT_MAX 1<<30
const int mod = 1e9+7;

map<ll,ll> prime_factor(ll n){
    map<ll,ll> mp;
    for(ll i=2; i*i<=n; i++){
        while(n % i == 0){
            mp[i]++;
            n /= i;
        }
    }
    if(n != 1) mp[n] = 1;
    return mp;
}

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll i=2; i<MAX; i++){
        fac[i] = fac[i-1]*i % mod;
        inv[i] = mod - inv[mod%i] * (mod/i) % mod;
        finv[i] = finv[i-1] * inv[i] % mod;
    }
}


ll com(ll n, ll k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

int main(){
    ll n,m; cin >> n >> m;
    auto pr = prime_factor(m);
    comInit();
    ll ans = 1;
    for(auto i : pr){
        ans *= com(i.second+n-1,i.second);
        ans %= mod;
    }
    cout << ans << endl;
}


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

int main(){
  int n;
  cin >> n;
  set<string>S;
  for(int i=0; i<n; i++){
    string si;
    cin >> si;
    S.insert(si);
  }
  cout << S.size();
  return 0;
}

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

int main() {
	long long  n, a;
	long long min = 1000000;
	long long max = -1000000;
	long long sum = 0;

	cin >> n;

	for(int i=0; i < n; i++){
		cin >> a;
		if (a < min) {
			min = a;
		} if(a>max){
			max = a;
		}
		sum += a;
	}
	cout << min << " " << max << " " << sum << endl;
	return 0;
}

Measured memory_bytes: 1164
N = int(input())
h = list(map(int,input().split()))
# N=4
# h=[10, 30, 40, 20]
l = [0]*N
l[1]=abs(h[0]-h[1])
#print(l[0],l[1])
for i in range(2,N):
    l[i] = min(l[i-1]+abs(h[i]-h[i-1]),l[i-2]+abs(h[i]-h[i-2]))
print(l[N-1])



Measured memory_bytes: 13908
n, m, x = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i = 0
while i < len(a) and x >= a[i]:
    x -= a[i]
    i += 1

f = i
j = 0
for p in b:
    j += 1
    x -= p
    
    while x < 0 and i > 0:
        i -= 1
        x += a[i]
    
    if x >= 0: f = max(f, j + i)

print(f)

        

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

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int A, B;
  cin >> A >> B;
  cout << (A - 1) * (B - 1) << endl;
  return 0;
}


Measured memory_bytes: 3496
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m=0,sum=0,rem;
    bool done = false;
    cin>>n;
  int a[n];
  for(int i=0;i<n;i++){cin>>a[i];m=max(m,a[i]);}
  for(int i=0;i<n;i++)
  {
      if(a[i]==m && !done)
      {
          rem = a[i]%2;
          a[i]/=2;
          done=true;
      }
      sum+=a[i];
  }
  if(rem)cout<<float(sum)+0.5<<endl;
  else
  cout<<sum<<endl;
}


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

 int main()
{
 int N; cin >> N;
	vector<int> A(N);vector<int> B(N);vector<int> C(N-1);
	for (int i = 0; i < N; ++i) 
    {
       	cin >> A[i];
      	--A[i];
 	}
   for (int i = 0; i < N; ++i)
     	cin >> B[i];
   for (int i = 0; i < N; ++i)
   		cin >> C[i];
   
   int ans = 0;
   for (int i = 0; i < N; ++i)
   {
       ans += B[A[i]];
       if (i > 0 && A[i] == A[i-1]+1) 
       ans += C[A[i-1]];
 	}
 
	cout << ans << endl;
 }

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

int main(){
        int k,s;    cin>>k>>s;
        int c=0;
        rep(i, k+1){
                rep(j, k+1){
                        int z=s-(i+j);
                        if(z<=k && z>=0)
                        c++;
                }
        }
        cout<<c<<endl;



}

Measured memory_bytes: 3612
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#endif

#define ITER(c)         __typeof__((c).begin())
#define FOREACH(it, c)  for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)        __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)       REPEAT(i, 0, n)
#define REPEAT(i, k, n) for (int i = (k); i < (n); ++i)
#define REVREP(i, n)    for (int i = ((n)-1); i >= 0; --i)
#define FIND(c, n)      std::find((c).begin(), (c).end(), (n));
#define BSEARCH(c, n)   std::binary_search((c).begin(), (c).end(), (n))
#define LLPOW(p1,p2)    ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)      (sizeof(A)/sizeof((A)[0]))
#define MIN(p1,p2)      (((p1)<(p2))?(p1):(p2))
#define MAX(p1,p2)      (((p1)>(p2))?(p1):(p2))
#define CIN_NO_SYNC     do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)       fgets((p), sizeof(p), stdin)
#define CHOMP(p)        do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n') *_q = 0; } while(0)

typedef long long ll;
typedef unsigned long long ull;

#ifdef _DEBUG
#define pf printf
#define pr(n) do { cout << #n " = " << (n) << endl; } while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#endif

namespace roman {
struct romantbl_t {
    const char *rom;
    int num;
} table[] = {
    { "M",  1000,   },
    { "CM", 900,    },
    { "D",  500,    },
    { "CD", 400,    },
    { "C",  100,    },
    { "XC", 90,     },
    { "L",  50,     },
    { "XL", 40,     },
    { "X",  10,     },
    { "IX", 9,      },
    { "V",  5,      },
    { "IV", 4,      },
    { "I",  1,      },
};

map<int,string> num2rom;
map<string,int> rom2num;

void romaninit(){
    for(int i = 0; i < sizeof(table)/sizeof(table[0]); i++){
        rom2num.insert(make_pair(table[i].rom, table[i].num));
        num2rom.insert(make_pair(table[i].num, table[i].rom));
    }
}

int roman2num(string rom){
    int len = rom.length();
    int t = 0, p = 0;
    for(int i = len - 1; i >= 0; i--){
        string c = rom.substr(i,1);
        int n = rom2num[c];
        if(n < p){ t -= n; } else { t += n; }
        p = n;
    }
    return t;
}

string num2roman(int num){
    string t;
    for(int i = 0; i < sizeof(table)/sizeof(table[0]); i++){
        int n = table[i].num;
        while(num >= n){
            t += num2rom[n]; num -= n;
        }
    }
    return t;
}
}

using namespace roman;

int main(){
    string s;
    romaninit();
    while(getline(cin, s)){
        cout << roman2num(s) << endl;
    }
    return 0;
}

Measured memory_bytes: 1192
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict
def primefactor(n):
     ans=defaultdict(int)
     for i in range(2,int(n**0.5+1)):
         while n%i==0:
             ans[i] += 1
             n //= i
     if n>1:
         ans[n]=1
     return dict(ans)

from itertools import product
from functools import reduce

def main():
    N=int(input())
    d=primefactor(N)

    ans = N - 1
    for v in product(*[range(i+1) for i in d.values()]):
        a = reduce(lambda x, y: x*y, [x**y for x, y in zip(d.keys(), v)])
        b = N // a
        ans = min(a-1 + b-1, ans)

    print(ans)

# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc144","c",main)




Measured memory_bytes: 3692
<?php
$d = trim(fgets(STDIN));
$ans = "";
switch ($d) {
 	case '22':
 		$ans = "Christmas Eve Eve Eve";
 		break;
 	case '23';
 		$ans = "Christmas Eve Eve";
 		break;
 	case '24';
 		$ans = "Christmas Eve";
 		break;
 	case '25';
 		$ans = 'Christmas';
 		break;
 }
 echo $ans."\n";

?>

Measured memory_bytes: 4604
#include<bits/stdc++.h>
#include <cmath>
#include<unordered_set>
#define ll long long  
#define PI 3.14159265358979323846
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb emplace_back
#define watch5(a,b,c,d,e) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d << " | "<<#e<<": "<<e<<endl;
#define watch4(a,b,c,d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define watch3(a,b,c) cerr <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<endl;
#define watch2(a,b) cerr <<#a<<": "<<a<<" | "<<#b<<": "<<b<< endl;
#define watch(a) cerr <<#a<<": "<<a<<endl;
#define F first
#define S second
#define mod 1000000007
using namespace std;

vector<int> adj[200005];
int vis[200005];

int dfs(int i){
  int cnt = 1;
  vis[i] = 1;
  for(auto it : adj[i]){
    if(!vis[it]){ 
      cnt += dfs(it);
    }
  }
  return cnt;
}

int min(int a, int b){
  return a<b?a:b;
}

int32_t main(){
	IOS;
  int n, m;
  cin >> n >> m;
  for(int i = 1; i <= m; i++){
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  int ans = 0;
  for(int i = 1; i <= n; i++){
    if(!vis[i]){
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}																				  

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

int main() {
	int n, k;
	cin >> n >> k;
	deque<int> dq;
	rep(i, n) {
		int v;
		cin >> v;
		dq.push_back(v);
	}
	int ans = 0;
	for(int a = 0; a <= min(n, k); a++) {
		
		// a: 左から何個取り出すか
		
		for(int b = 0; a+b <= min(n, k); b++) {
			vector<int> my; // 取り出したもの
			deque<int> now = dq;
			rep(i, a) {
				my.push_back(now.front());
				now.pop_front();
			}
			rep(i, b) {
				my.push_back(now.back());
				now.pop_back();
			}
			
			// b: 右から何個取り出すか
			int tmp = 0; 
			sort(all(my));
			for(int i = 0; i < min(k-a-b, a+b); i++) {
				if(my[i]<0) my[i] = 0;
			}
			rep(i, my.size()) {
				tmp += my[i];
			}
			ans = max(ans, tmp);
			
			// cout << a << " " << b << " : ";
			// rep(i, my.size()) cout << my[i] << " ";
			// cout << endl;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
s = gets.chomp
n = s.size

def rec(s, n, ans)
  return ans += eval(s) if n == 0
  chars = s.chars
  if chars[n..-1] != []
    str = (chars[0..n-1] + ['+'] + chars[n..-1]).join
    ans = rec(str, n-1, ans)
  end
  ans = rec(s, n-1, ans)
end

puts rec(s, n, 0)

Measured memory_bytes: 1916
#include<stdio.h>
long buffer[100000000];
void merge(long a[],long left,long right){
    if(left<right){
        long axis=(left+right)/2,i=left,p=0,q=left,r=0;
        merge(a,left,axis);
        merge(a,axis+1,right);
        while(i<=axis)buffer[p++]=a[i++];
        while(i<=right && r<p)a[q++]=(a[i]<=buffer[r])?a[i++]:buffer[r++];
        while(r<p)a[q++]=buffer[r++];
    }
}
int main(void){
    long N,X;
    scanf("%ld%ld",&N,&X);
    long Xi[N];
    for(int i=0;i<N;i++){
        scanf("%ld",&Xi[i]);
    }
    for(int i=0;i<N;i++){
        Xi[i]=Xi[i]-X;
        if(Xi[i]<0){
            Xi[i]=-1*Xi[i];
        }
    }
    merge(Xi,0,N-1);
    long D=Xi[0];
    long c=0;
    long a=0;
    while(c!=1){
        for(int i=0;i<N;i++){
            a=0;
            if((Xi[i]%D)!=0){
                a=1;
                D-=1;
                break;
            }
        }
        if(a==0){c=1;}
    }
    printf("%ld\n",D);
}


Measured memory_bytes: 1280
k=int(input())
cnt=9
lst=[1,2,3,4,5,6,7,8,9]
if k<=9:
	print(lst[k-1])
else:
	tmp=[]
	while True:
		for i in lst:
			if i%10-1>=0:
				tmp.append(i*10+i%10-1)
			tmp.append(i*10+i%10)
			if i%10+1<=9:
				tmp.append(i*10+i%10+1)
		if cnt+len(tmp)>=k:
			break
		cnt+=len(tmp)
		lst=tmp
		tmp=[]
	print(tmp[k-cnt-1])


Measured memory_bytes: 10660
#include<bits/stdc++.h>
#define N 41
using namespace std;
typedef long long ll;
ll n,m,a,b,p,q,d[N];
vector<ll> v[2],x[2];

int main(){
  cin>>n>>m>>a>>b>>p>>q;
  ll ans=m;
  if(a==1&&b==1){
    ll ti=min(n,m/(p+q));
    ans=min(ans,min(m-ti*(p+q),abs(min(ti+1,n)*(p+q)-m)));
  }
  ll A=1,B=1;
  for(int i=0;i<min((ll)N,n);i++){
    d[i]=p*A+q*B;
    if(i<min((ll)N,n)/2)v[0].push_back(d[i]);
    else v[1].push_back(d[i]);
    if(d[i]>m)break;
    A*=a,B*=b;
  }
  for(int i=0;i<2;i++){
    v[i].push_back(0);
    for(int j=0;j<(1<<v[i].size());j++){
      ll sum=0;
      for(int k=0;k<v[i].size();k++){
	if(!(j&(1<<k)))continue;
	sum+=v[i][k];
      }
      x[i].push_back(sum);
    }
    sort(x[i].begin(),x[i].end());
  }
  for(int i=0;i<x[0].size();i++){
    ll search=m-x[0][i];
    vector<ll>::iterator r=lower_bound(x[1].begin(),x[1].end(),search);
    if(r!=x[1].end())ans=min(ans,(*r)-search);
    if(r!=x[1].begin())ans=min(ans,search-(*(r-1)));
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 52272
#include<stdio.h>
int main(){
	char t[99];
	double s[99],v;
	int p=0,f,c;
	while(1){
		c=scanf("%[^ \n]",t);
		if(c==-1)
			return 0;
		if(c==0)
			return 1;
		f=0;
		if(t[1]==0) {
			if(t[0]=='+')
				s[p-2]+=s[p-1],p--;
			else if(t[0]=='-')
				s[p-2]-=s[p-1],p--;
			else if(t[0]=='*')
				s[p-2]*=s[p-1],p--;
			else if(t[0]=='/')
				s[p-2]/=s[p-1],p--;
			else
				f=1;
		} else
			f=1;
		if(f) {
			if(sscanf(t,"%lf",&v)==-1)
				return 1;
			s[p++]=v;
		}
		c=getchar();
		if(c=='\n')
			printf("%f\n",s[--p]+0.);
		else if(c==' ')
			;
		else
			return 1;
	}
	return 0;
}

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

#define mk make_pair
#define sc second
#define fr first
#define pb emplace_back
#define all(s) s.begin(), s.end()
#define sz(s) ( (int)s.size() )
#define Scan(a) scanf ("%I64d", &a)
#define scan(a) scanf ("%d", &a)

using namespace std;

const int inf = (int)1e9 + 7;
const int N = (int)2e5 + 7;

int n;
int cn;

int f (int x) {
      int cnt = 0;
      for (int i = 1; i <= n; i ++) {
            if (x % i == 0) cnt ++;
      }
      return cnt;
}
main () {
      cin >> n;

      for (int i = 2; i <= n; i ++) {
            if (i % 2 == 1 && f(i) == 8) cn ++;
      }
      cout << cn << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int n, ac[200005], ans = 300005;
string s;

int main(){
    cin >> n >> s;
    for(int i = 0; i < n; i++){
        ac[i+1] = ac[i] + (s[i] == '#');
    }
    for(int i = 0; i <= n; i++){
        ans = min(ans, ac[i]*2 - ac[n] + n - i);
    }
    printf("%d\n", ans);
}

Measured memory_bytes: 1284
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <locale>
#include <iostream>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template<typename T>
vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args>
auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename()  const { return "YES"; }
  string_type do_falsename() const { return "NO"; }
};

void solve(ll N, ll K, vector<ll> a, vector<ll> b) {
  vector<pair<int,int>> vec;
  REP(i,N) vec.emplace_back(a[i], b[i]);
  sort(ALL(vec));
  for (auto p: vec) {
    K -= p.second;
    if (K <= 0) {
      cout << p.first << endl;
      return;
    }
  }
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
	ll K;
	ll N;
	scanf("%lld", &N);
	vector<ll> a(N-1+1);
	vector<ll> b(N-1+1);
	scanf("%lld", &K);
	for (int i = 0 ; i <= N-1 ; i++) {
	  scanf("%lld", &a[i]);
	  scanf("%lld", &b[i]);
	}
	solve(N, K, a, b);
	return 0;
}


Measured memory_bytes: 4472
#include <bits/stdc++.h>
#define rep(i,m,n) for(int i=m; i<n; i++)
#define co(n) cout << n << endl
using namespace std;
int main(){
  int a, b;
  cin >> a >> b;
  if(b%a==0) co(a+b);
  else co(b-a);
  
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define nl '\n' 
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long int
#define pii pair <int,int>
ll mod=1000000007;
int main()
{
	fast;
	ll a,b,c,d;cin>>a>>b>>c>>d;
	ll ans=a*c;
	ans=max(ans,a*d);
	ans=max(ans,b*c);
	ans=max(ans,b*d);
	cout<<ans;

}

Measured memory_bytes: 3620
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;

#define FOR(i,n) for (int (i) = 0; (i) < (n); i++)
typedef long long ll;

int N;
//aがコストでbが締め切り
//締め切りでソートして順々に詰め込んでいく
pair<int, int> T[200000];

int main() {
    cin >> N;
    int a,b;
    for (int i = 0; i < N; i++) {
        cin >> a >> b;
        T[i] = make_pair(b,a);
    }
    sort(T,T+N);
    
    int now = 0;
    FOR(i,N) {
        now += T[i].second;
        if (now > T[i].first) {
            cout << "No\n";
            return 0; 
        }
    }
    cout << "Yes\n";
}

Measured memory_bytes: 1792
//Name: Tree Reconstruction
//Level: 3
//Category: グラフ,Graph
//Note:

/**
 * まず、入次数と出次数がともに1の頂点は、単なる辺と変わらないので取り除いてよい。
 * この操作をしたあとのグラフで考えると、ある辺eが復元可能な条件は
 * ・eが出ている頂点について、e以外の辺の容量がすべて判明している
 * ・eが入っていく頂点について、e以外の辺の容量がすべて判明している
 * のどちらか（強連結性により、孤立していなければどの頂点も1以上の入次数、出次数をもつため、必ず復元に寄与できる）。
 * これを繰り返し適用することで全体が復元できる必要がある。
 *
 * 上記の条件から、1つの頂点につき、高々1つの辺しか復元できないことが分かる。
 * また、復元を開始するためのポイントとして、初期状態で1辺を除いてすべての辺の容量が判明している頂点が少なくとも1つ必要である。
 * したがって、取り除ける辺数は高々V-1個であることが分かる。
 * 実際、このような選び方は、二部マッチングを用いることで求められる
 * （左の頂点集合を辺、右の頂点集合を頂点として、辺の結ぶ2頂点に対して辺を張ればよい）。
 *
 * この問題では、選び方は関係ないので、入次数と出次数がともに1の頂点を取り除いてから残った辺数と頂点数を数えればよい。
 * グラフ全体が連結とは限らないため、各連結成分ごとにカウントする必要があることに注意が必要だが、連結成分ごとに(頂点数-1)個の辺を除去できるので、
 * 連結成分の個数だけ数えて後からまとめて引けばよい。
 * オーダーは O(N+M)。
 */

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct DisjointSet/*{{{*/
{
  vector<int> parent;

  int root(int x)
  {
    if (parent[x] < 0) {
      return x;
    } else {
      parent[x] = root(parent[x]);
      return parent[x];
    }
  }

  explicit DisjointSet(int n) : parent(n, -1) {}

  bool unite(int x, int y)
  {
    const int a = root(x);
    const int b = root(y);
    if (a != b) {
      if (parent[a] < parent[b]) {
        parent[a] += parent[b];
        parent[b] = a;
      } else {
        parent[b] += parent[a];
        parent[a] = b;
      }
      return true;
    } else {
      return false;
    }
  }

  bool find(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -parent[root(x)]; }
};/*}}}*/

bool solve() {
    int N, M;
    if(!(cin >> N >> M)) return false;
    if(!N && !M) return false;

    int vs = N, es = M;
    DisjointSet ds(N);
    vector<int> in(N, 0), out(N, 0);
    for(int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        out[a]++;
        in[b]++;
        ds.unite(a, b);
    }
    for(int i = 0; i < N; ++i) {
        if(in[i] == 1 && out[i] == 1) {
            --vs;
            --es;
        }
    }
    int comps = 0;
    for(int i = 0; i < N; ++i) {
        if(ds.root(i) == i) {
            ++comps;
        }
    }
    cout << es - vs + comps << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);

    while(solve()) ;
    return 0;
}

Measured memory_bytes: 1208
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;

#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
ll done[100001], freq[100001];
int main(void)
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, m, i, sum = 0, c = 0;
    cin>>n>>m;
    string s;
    ll num;
    for(i=0; i<m; i++) {
    	cin>>num>>s;
    	if(s == "AC" && !done[num]) {
    		c++;
    		sum += freq[num];
    		done[num] = 1;
    	}
    	else {
    		freq[num]++;
    	} 
    }
    cout<<c<<" "<<sum<<"\n";
}

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

using namespace std;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  while (true) {
    C -= B;
    if (C <= 0) {
      printf("Yes\n");
      break;
    }

    A -= D;
    if (A <= 0) {
      printf("No\n");
      break;
    }
  }
  return 0;
}


Measured memory_bytes: 3628
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>

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)
signed main(){
  int n,k;cin>>n>>k;
  int res=1;
  rep(i,n){
    if(i==0)res*=k;
    else res*=k-1;
  }
  cout<<res<<endl;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;



int main(void){
    ll a,b;
    cin>>a>>b;
    ll la = 25*a;
    if(la%2==0)la = la/2;
    else la = la/2 + 1;
    ll ra = 25*(a+1);
    if(ra%2==0)ra = ra/2-1;
    else ra = ra/2 ;
    ll lb = 10*b;
    ll rb = 10*(b+1)-1;
    if(ra<lb){
        cout<<-1<<endl;
    }
    else{
        if(rb<la){
            cout<<-1<<endl;
        }
        else{
            for(ll i = la;i<=ra;i++){
                if(i>=lb){
                    if(i<=rb){
                        cout<<i<<endl;
                        return 0;
                    }
                }
            }
            cout<<-1<<endl;
        }
    }
    return 0;

}


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

int main(){
  int N;
  cin >> N;
  list<pair<int, int>> E(N-1);
  vector<int> count(N, 0);
  for(auto& e : E){
    cin >> e.first >> e.second;
    e.first--;
    e.second--;
    count[e.first]++;
    count[e.second]++;
  }
  vector<int> C(N);
  for(auto& c : C){
    cin >> c;
  }
  sort(C.begin(), C.end());
  vector<int> ans(N, C.back());
  int candidate = 0;
  for(int ii=0; ii<N-1; ii++){
    if(count[candidate]!=1){
      candidate = distance(count.begin(), find(count.begin(), count.end(), 1));
    }
    ans[candidate] = C[ii];
    auto it = find_if(E.begin(), E.end(), [&candidate](const pair<int, int>& e){
      return e.first == candidate || e.second == candidate;
    });
    count[it->first]--;
    count[it->second]--;
    candidate = it->first + it->second - candidate;
    E.remove(*it);
  }

  int score = 0;
  for(auto& c : C){
    score += c;
  }
  score -= C.back();
  cout << score << endl;
  for(int ii=0; ii<N; ii++){
    if(ii != 0){
      cout << " ";
    }
    cout << ans[ii];
  }
  cout << endl;
  return 0;
}


Measured memory_bytes: 768
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
	string s;
	cin>>s;
	if(s.length()<4)
		cout<<"No\n";
	else
	{
		if(s[0]=='Y' && s[1]=='A' && s[2]=='K' && s[3]=='I')
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
	return 0;
}

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

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define RREP(i, N) for (int i =(int)N-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

int main(){
  int a,b;
  cin>>a>>b;
  cout<<6-a-b<<endl;
}


Measured memory_bytes: 256
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 100010
// #define int long long
using namespace std;
void write(int x){if(x<0){putchar('-');x=-x;}if(x>9) write(x/10);putchar(x%10+'0');}
int read(){int d=0,w=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')
w=-1;for(;c>='0'&&c<='9';c=getchar())d=(d<<1)+(d<<3)+c-48;return d*w;}
void wln(int x){write(x);putchar('\n');}
void wrs(int x){write(x);putchar(' ');}
set<int> st[maxn];
map<pair<int,int>,int> mp;
queue<pair<int,int>> q;
int n,f[maxn],ans;
int find(int x)
{
	return x==f[x]?x:f[x]=find(f[x]);
}
signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	n=read();
	for(int i=1;i<(n<<1)-1;i++)
	{
		int x=read(),y=read();
		st[x].insert(y);
		st[y].insert(x);
		mp[make_pair(x,y)]++;
		mp[make_pair(y,x)]++;
		if(mp[make_pair(x,y)]==2) q.push(make_pair(x,y));
	}
	for(int i=1;i<=n;i++)
		f[i]=i;
	for(;!q.empty();)
	{
		pair<int,int> tmp=q.front();
		q.pop();
		int x=find(tmp.first),y=find(tmp.second);
		st[x].erase(y);
		st[y].erase(x);
		if(st[x].size()<st[y].size()) swap(x,y);
		for(auto v:st[y])
		{
			st[x].insert(v);
			st[v].insert(x);
			st[v].erase(y);
			mp[make_pair(v,y)]=mp[make_pair(y,v)]=0;
			mp[make_pair(x,v)]++;
			mp[make_pair(v,x)]++;
			if(mp[make_pair(x,v)]==2) q.push(make_pair(x,v));
		}
		f[y]=x;
		st[y].clear();
	}
	for(int i=1;i<=n;i++)
		ans+=f[i]==i;
	puts(ans>1?"NO":"YES");
	return 0;
}


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

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

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
//定数
#define INF 1000000000 //10^9:極めて大きい値,∞
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first      //pairの一つ目の要素
#define S second　   //pairの二つ目の要素

int main(){
    int n,k;
    cin>>n>>k;
    vector<int> h(n);
    rep(i,n) cin>>h[i];
    sort(h.begin(),h.end(),greater<int>());
    ll ans=0;
    for(int i=k;i<n;i++) ans+=*(h.begin()+i);
    cout<<ans<<endl;
    return 0;
}

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

int phrase[5] = {5,7,5,7,7};

int main() {

    while(1){

        int n;

        cin >> n;

        if(n==0) break;

        vector<string> w;

        for(int i=0; i<n; i++){
            string input;
            cin >> input;
            w.push_back(input);
        }

        int w_pos = 0;

        while(1){

            int w_itr = w_pos, nowp = 0, pcount = 0;

            while(1){
                if(w[w_itr].size() + pcount > phrase[nowp]){
                    nowp = -1;
                    break;
                }
                else if(w[w_itr].size() + pcount == phrase[nowp]){
                    nowp++;
                    if(nowp == 5){
                        break;
                    }
                    pcount = 0;
                }
                else{
                    pcount += w[w_itr].size();
                }
                w_itr++;
            }

            if(nowp == 5){
                break;
            }

            w_pos++;


        }

        cout << w_pos + 1 << endl;

    }

    return 0;
}

Measured memory_bytes: 3084
/*================================================================
*
*   创 建 者： badcw
*   创建日期： 2020/6/20 20:08
*
================================================================*/
#include <bits/stdc++.h>

#define ll long long
using namespace std;

const int maxn = 360;
const int mod = 1e9+7;
ll qp(ll a, ll n, ll mod = ::mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main(int argc, char* argv[]) {
    int n;
    scanf("%d", &n);
    int res = 360 / __gcd(360, n);
    printf("%d\n", res);
    return 0;
}

Measured memory_bytes: 3784
#include <bits/stdc++.h>
 
using namespace std;
 
#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define RSORT(x) sort((x).rbegin(),(x).rend())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back
#define chmax(x,y) x = max(x,y)

 
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<vector<int> > VVI;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<bool> VB;
typedef vector<LL> VL;
typedef vector<vector<long long>> VVLL;
typedef vector<set<int>> VSI;
const int MOD = 1e9+7;
const int INF = 1e9;
const LL LINF = 1e18;
int gcd(int a, int b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

int lcm(int a, int b)
{
   return a * b / gcd(a, b);
}

int main() {
    int n,q;
    cin >> n;
    VI a(n);
    rep(i,n) cin >> a[i];
    cin >> q;
    VI b(q);
    VI c(q);

    vector<int> p(200000);

    rep(i,q) cin >> b[i] >> c[i];
    SORT(a);

    rep(i,n) {
        p[a[i]]++;
    }
    LL ans = 0;

    rep(i,n) ans += a[i];

    rep(i,q) {
        if(p[b[i]] > 0) {
            int k = c[i] - b[i];
            ans += k * p[b[i]];
            p[c[i]] += p[b[i]];
            p[b[i]] = 0;
        }
        cout << ans << endl;
    }


}


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

#define ll long long
#define INF 99999999
#define INF_LL 1LL << 60
#define MOD (ll)1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, a, n) for(int i = a; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()

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; }

void put_double(double obj){printf("%.12f\n",obj);};

template < typename T > std::string to_string( const T& n )
{
    std::ostringstream stm ;
    stm << n ;
    return stm.str() ;
}

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

    REP(i, 1, N+1)
    {
        if( (int)(i*1.08) == N )
        {
            cout << i << endl;
            return 0;
        }
    }
    cout << ":(" << endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
#define INF (1<<28)
typedef long long Int;

int N, B[64], C[64];
string A[64], D[64][64];
int g[64][64], vis[64];

void collect(vector<int>& q, int at) {
    if (vis[at]) return ;
    vis[at] = 1;
    q.push_back(at);
    rep (i, N) if (g[at][i]) collect(q, i);
}

int n, a, r[64], p[64];
Int f[64];

void prepare(vector<int>& q) {
    n = q.size();
    a = 0;
    rep (i, n) r[i] = q[i];
    rep (i, n) {
        p[i] = B[r[i]];
        f[i] = 0;
        rep (j, n) if (g[r[i]][r[j]]) f[i] |= 1LL<<j;
    }
}

void rec(int k, Int u) {
    int s = 0;
    rep (i, n) if (u&(1LL<<i)) s += p[i];
    if (s < a) return ;
    if (k == n) a = s;
    else {
        rep (i, k) if (!(u&(1LL<<i)) && !(f[i]&u)) return ;
        const int mask = (1LL<<n) - 1;
        if (u&(1LL<<k)) rec(k+1, u&(mask-f[k]));
        if (r[k]) rec(k+1, u&(mask-(1LL<<k)));
    }
}

int solve() {
    memset(vis, 0, sizeof(vis));
    int ans = 0;
    rep (i, N) if (!vis[i]) {
        vector<int> q;
        collect(q, i);
        prepare(q);
        rec(0, (1LL<<q.size())-1);
        ans += a;
    }
    return ans;
}

int main() {
    for (;;) {
        cin >> N;
        if (N == 0) return 0;
        map<string, int> of;
        rep (i, N) {
            cin >> A[i] >> B[i] >> C[i];
            of[A[i]] = i;
            rep (j, C[i]) cin >> D[i][j];
        }
        memset(g, 0, sizeof(g));
        rep (i, N) rep (j, C[i]) g[i][of[D[i][j]]] = 1;
        printf("%d\n", solve());
    }
}

Measured memory_bytes: 1396
using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[] str = Console.ReadLine().Split(' ');
		int a = int.Parse(str[0]);
		int b = int.Parse(str[1]);
		int c = int.Parse(str[2]);
		int count = 0;
		for(var i=a;i<=b;i++){
			if(c % i == 0){
				count += 1;
			}
		}
		Console.WriteLine(count);
	}
}


Measured memory_bytes: 9240
package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var graph map[int]map[int]bool

func initGraph(x int) {
	graph = make(map[int]map[int]bool, x)
}

func add(from, to int) {
	if _, ok := graph[from]; !ok {
		graph[from] = make(map[int]bool)
	}
	graph[from][to] = true
}

func main() {
	var n, x, y int
	fmt.Scan(&n, &x, &y)

	initGraph(n)
	add(x, y)
	add(y, x)
	for i := 1; i < n; i++ {
		add(i, i+1)
		add(i+1, i)
	}

	ans := make([]int, n+1)
	for i := 1; i <= n; i++ {
		seen := make([]int, n+1)
		seen[i] = 1
		ls := list.New()
		ls.PushBack(i)
		for ls.Len() > 0 {
			v := ls.Remove(ls.Front()).(int)
			for t := range graph[v] {
				if seen[t] > 0 {
					continue
				}
				seen[t] = seen[v] + 1
				ans[seen[t]-1]++
				ls.PushBack(t)
			}
		}
	}

	var wr = bufio.NewWriter(os.Stdout)
	for i := 1; i < n; i++ {
		wr.WriteString(strconv.Itoa(ans[i]/2) + "\n")
	}
	wr.Flush()
}


Measured memory_bytes: 5504
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] science = new int[4];
        int[] sciety = new int[2];

        for(int i=0;i<6;i++){
            if(i<4) science[i] = sc.nextInt();
            else sciety[i-4] = sc.nextInt();        
        }

        for(int i=0;i<4;i++){
            for(int j=i+1;j<4;j++){
                if(science[i]>science[j]){
                    int x = science[i];
                    science[i] = science[j];
                    science[j] = x;
                }
            }
        }
        if(sciety[0]>sciety[1]){
            int y = sciety[0];
            sciety[0] = sciety[1];
            sciety[1] = y;
        }
        System.out.println(science[1]+science[2]+science[3]+sciety[1]);
    }
}


Measured memory_bytes: 26208
def xor(n):
    if n == 0: return 0
    x = 0
    if ((n + 1) // 2) % 2 != 0: x = 1
    if n % 2 == 0: x ^= n
    return x

a, b = map(int, input().split())
print(xor(a - 1) ^ xor(b))


Measured memory_bytes: 38384
using System;

namespace abc137B
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string[] str = Console.ReadLine().Split();
            int A = int.Parse(str[0]);
            int B = int.Parse(str[1]);
            int C = B - (A - 1);
            int D = B + (A - 1);
            int E = 2 * A - 1;
            int[] table = new int[E];
            for(int p = 0; p < E; p++)
            {
                table[p] = C + p;
            }
            for(int p = 0; p < E; p++)
            {
                Console.Write(table[p] + " ");
            }
        }
    }
}


Measured memory_bytes: 13140
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

int main(int argc, char* argv[])
{
    int a,b;
    scanf("%d%d", &a, &b);

    printf("%d %d %.10lf\n", a/b, a%b, (double)a/b);
    return 0;
}



Measured memory_bytes: 2732
a,b,c,d = map(int, input().split())
i = 0
while a > 0 and c > 0:
  if i % 2 == 0:
    c -= b
    i += 1
  else:
    a -= d
    i += 1
if a <= 0:
  print('No')
else:
  print('Yes')

Measured memory_bytes: 9116
<?php
$answer = 0;
$tmp = 1;
fscanf(STDIN, '%d', $cnt);
$bricks = explode(' ', trim(fgets(STDIN)));
for ($i=0; $i < $cnt; $i++) { 
    $target = $tmp;
    if ($bricks[$i] == $target) {
        $tmp++;
    } else {
        $answer++;
    }
}

echo ($tmp > 1 ? $answer : -1) . "\n";

Measured memory_bytes: 18932
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#include <sstream>
#include <iterator>

using namespace std;

using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

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

    vector<int> arr(n);

    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    i64 a = 0, b = 0;

    for (int i = 0; i < n; i++)
        a += arr[i];

    i64 gap = abs(a - 2 * arr[0]);

    for (int i = 0; i < n - 1; i++)
    {
        a -= arr[i];
        b += arr[i];

        gap = min(gap, abs(a - b));
    }

    printf("%lld\n", gap);

    return 0;
}

Measured memory_bytes: 1024
#include<iostream>
#include<string>
using namespace std;
int main(){
  int n;
  cin >> n;
  for(int k=0;k<n;k++){
    string order,str,buf;
    cin >> order >> str;
    for(int i=order.size()-1;i>=0;i--){
      
      int s=str.size();
      switch(order[i]) {
      case 'J' :
	buf=str;
	for(int j=0;j<s;j++)
	  str[j+1]=buf[j];
	str[0]=buf[s-1];
	break;
      case 'C' : 
	buf=str;
	for(int j=0;j<s;j++)
	  str[j]=buf[j+1];
	str[s-1]=buf[0];
	break;
      case 'E' :
	buf=str;
	for(int j=0;j<s/2;j++){
	  str[j]=buf[j+s-s/2];
	}
	for(int j=s-s/2;j<s;j++){
	  str[j]=buf[j-s+s/2];  
	}
	
	break;
      case 'A' :
	buf=str;
	for(int j=0;j<s/2;j++){
	  str[j]=buf[s-1-j];
	  str[s-1-j]=buf[j];
	}
	break;
      case 'P' :
	for(int j=0;j<s;j++){
	  if(str[j]=='0') str[j]='9';
	  else if('0'<str[j]&&str[j]<='9') str[j]--;
	}
	break;
      case 'M' :
	for(int j=0;j<s;j++){
	  if(str[j]=='9') str[j]='0';
	  else if('0'<=str[j]&&str[j]<'9') str[j]++;
	}
	break;
      }
    }
    cout << str << endl;
  }
  return 0;
}

Measured memory_bytes: 1208