akhauriyash/Code-Regression huggingface dataset:
// {{{
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <vector>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPR(i, n) for(int i = (n); i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()

//#if __cplusplus >= 201103
typedef long long ll;
//#endif
typedef pair<int, int> P;
typedef pair<int, P> IP;

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

const int INF = 100000000;
//#if __cplusplus >= 201103
const ll LINF = 10000000000000000ll;
//#endif
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
// }}}

class UnionFind{
    vector<size_t> m_parents;
    vector<ll> m_ranks;

public:
    UnionFind(size_t n){
        m_parents.resize(n);
        m_ranks.resize(n);
        for(size_t i = 0; i < n; i++){
            m_parents[i] = i;
            m_ranks[i] = 0;
        }
    }

    size_t root(size_t x){
        if(m_parents[x] == x) return x;
        size_t r = root(m_parents[x]);
        m_parents[x] = r;
        return r;
    }

    bool isSame(size_t x, size_t y){
        return root(x) == root(y);
    }

    void unite(size_t x, size_t y){
        size_t rx = root(x);
        size_t ry = root(y);
        if(rx != ry){
            if(m_ranks[rx] < m_ranks[ry]) swap(rx, ry);
            if(m_ranks[rx] == m_ranks[ry]) m_ranks[rx]++;
            m_parents[ry] = rx;
        }
    }
};

int N, K, L;

void init()
{
}

void solve()
{
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> K >> L;
    UnionFind road(N);
    UnionFind train(N);
    REP(i, K){
        int p, q;
        cin >> p >> q;
        p--; q--;
        road.unite(p, q);
    }
    REP(i, L){
        int p, q;
        cin >> p >> q;
        p--; q--;
        train.unite(p, q);
    }

    map<pair<int, int>, int> m;
    REP(i, N){
        int r = road.root(i);
        int t = train.root(i);
        m[mp(r, t)]++;
    }
    REP(i, N){
        int r = road.root(i);
        int t = train.root(i);
        if(i < N-1) cout << m[mp(r, t)] << " ";
        else cout << m[mp(r, t)] << endl;
    }

    return 0;
}

// vim:set foldmethod=marker:


Measured memory_bytes: 18176
def prim():
    global n, color, M, d, p
    d[0] = 0

    while True:
        min_cost = 10000000000
        u = -1

        # まず始点を決める。
        for i in range(n):
            if color[i] != 'BLACK' and d[i] < min_cost:
                min_cost = d[i]
                u = i  # エッジが繋がる中で、最小のもの？？

        if u == -1:
            break

        color[u] = 'BLACK'

        for v in range(n):
            if color[v] != 'BLACK' and M[u][v] != -1:
                if d[v] > M[u][v]:
                    d[v] = M[u][v]
                    p[v] = u
                    color[v] = 'GRAY'

    sum = 0
    for i in range(n):
        if p[i] != -1:
            sum += M[i][p[i]]

    return sum


n = int(input())

color = ['WHITE'] * n
M = []
d = [10000000000] * n  # 重みが最小の辺の重み
p = [-1] * n  # MSTにおけるp[v]の親（どこから来たか？？）

for i in range(n):
    _ = list(map(int, input().split()))
    M.append(_)
# print(A)

print(prim())



Measured memory_bytes: 5896
class SWAG():
    def __init__(self,unit,f):
        self.fold_r,self.fold_l=[unit],[unit]
        self.data_r,self.data_l=[],[]
        self.f=f
    def __refill_right(self):
        temp=[]
        n=len(self.data_l)
        for _ in range(n//2):temp.append(self.popleft())
        for _ in range(n//2,n):self.append(self.popleft())
        for t in temp[::-1]:self.appendleft(t)
    def __refill_left(self):
        temp=[]
        n=len(self.data_r)
        for _ in range(n//2):temp.append(self.pop())
        for _ in range(n//2,n):self.appendleft(self.pop())
        for t in temp[::-1]:self.append(t)
    def append(self,x):
        self.fold_r.append(self.f(self.fold_r[-1],x))
        self.data_r.append(x)
    def appendleft(self,x):
        self.fold_l.append(self.f(self.fold_l[-1],x))
        self.data_l.append(x)
    def pop(self):
        if not self.data_r:self.__refill_right()
        self.fold_r.pop()
        return self.data_r.pop()
    def popleft(self):
        if not self.data_l:self.__refill_left()
        self.fold_l.pop()
        return self.data_l.pop()
    def fold_all(self):
        return self.f(self.fold_r[-1],self.fold_l[-1])
n,k,*p=map(int,open(0).read().split())
c=[0]
for a,b in zip(p,p[1:]):c+=c[-1]+(a<b),
*c,f=[b-a==k-1for a,b in zip(c,c[k-1:])]
x=not f
s_min=SWAG(10**18,min)
s_max=SWAG(0,max)
*_,=map(s_min.append,p[:k-1])
*_,=map(s_max.append,p[1:k])
for i,(a,b,c)in enumerate(zip(p,p[k:],c)):
    f|=c
    s_min.append(p[i+k-1])
    s_max.append(b)
    if not c and(a!=s_min.fold_all()or b!=s_max.fold_all()):x+=1
    s_min.popleft()
    s_max.popleft()
print(x+f)

Measured memory_bytes: 121148
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
 
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
 
    ll n;
    cin >> n;
    vector<ll> arr(n);
    ll sum = 0;
    unordered_map<ll, ll> u;
    for (int i=0;i<n;i++)
        cin >> arr[i], sum += arr[i], u[arr[i]]++;
    
    ll q,b,c;
    cin >> q;
    for (int i=0;i<q;i++) {
        cin >> b >> c;
        ll bsum = u[b]*b;
        ll csum = u[b]*c;
        sum = sum - bsum + csum;
        u[c] += u[b];
        u[b] = 0;
        cout << sum << "\n";
    }
}

Measured memory_bytes: 7796
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Max = 1e3 + 7;
char lst[Max][Max];
int ls[Max][Max];

int main()
{
	ll n, k;
	cin >> n;
	for (int i = 1; i <= n+10; i++)
	{
		if (i*i > n) {
			cout << (i-1)*(i-1); break;
		}
	}
}

Measured memory_bytes: 3144
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

const int mod=1000000007;
int main(){
    ll n,m;
    cin>>n>>m;
    if(n==1){
        cout<<(m+1)%mod<<endl;
        return 0;
    }
    ll k=m/(n-1);
    k%=mod;
    ll mo=(m+1)%mod-((n-1)%mod*k%mod)+mod;
    mo%=mod;
    ll no=((n-1)-mo)%mod+mod;
    no%=mod;
    ll ans=mo*(k+1)%mod*(k+1)%mod+no*k%mod*k%mod;
    cout<<ans%mod<<endl;
    return 0;
}


Measured memory_bytes: 3120
#include <algorithm>
#include <iostream>
#include <string>
#include <random>
#include <cassert>
#include <cstring>
#include <chrono>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

bool Good(string s) {
  int l = 0;
  int r = s.size() - 1;
  while (l < r) {
    if (s[l] != s[r]) return false;
    l++;
    r--;
  }
  return true;
}

int main()
{
  string s;
  cin >> s;
  int n = s.size();
  string s1 = s.substr(0, (n - 1) / 2);
  string s2 = s.substr((n + 3) / 2 - 1);
  bool ok = Good(s) && Good(s1) && Good(s2);
  cout << (ok ? "Yes" : "No") << endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int height = sc.nextInt();
        int width =  sc.nextInt();
        int h = sc.nextInt();
        int w = sc.nextInt();

        int ans = (height * width) - (width * h) - ((height - h) * w);
        System.out.println(ans);
        
    }
}

Measured memory_bytes: 23124
#include<stdio.h>
#define N 100
int main(){
  int a,b,j,i,key;
  int kazu[N];
  scanf("%d",&a);
  for(i=0;i<a;i++){
    scanf("%d",&kazu[i]);
  }
  for(i=1;i<a;i++){
    key=kazu[i];
    b=i-1;
    for(j=0;j<a-1;j++){
      printf("%d ",kazu[j]);
    }
    printf("%d\n",kazu[j]);
    while(b>=0 && kazu[b]>key){
      kazu[b+1]=kazu[b];
      b=b-1;
      kazu[b+1]=key;
    }
  }
  for(j=0;j<a-1;j++){
    printf("%d ",kazu[j]);
  }
  printf("%d\n",kazu[j]);
  return 0;
}
   

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

#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;


int main()
{
    int max = 1299710;
    vector<int> mat(max, 1);
    vector<int> primes;
    for (int i=2; i<sqrt(max); i++) {
        if (mat[i]) {
            for (int j=i*2; j < max; j+=i) mat[j] = 0;
        }
    }
    for (int i=2; i<max; i++) {
        if (mat[i]) primes.push_back(i);
    }

    int k;
    while (cin >> k, k) {
        loop(primes.size(),i) {
            if (k == primes[i]) {
                cout << 0 << endl;
                break;
            } else if (k < primes[i]) {
                cout << primes[i]-primes[i-1] << endl;
                break;
            }
        }
    }
}

Measured memory_bytes: 8384
from itertools import product
n = int(input())
num = ['','3','5','7']
A = set() 
for i in product(num,repeat=9):
    x = "".join(i)
    if len(set(x)) == 3:
        if (int(x) not in A) and (int(x) <= n):
            A.add(int(x))
print(len(A))

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

int main() {
  int N, cnt = 0;
  cin >> N;
  vector<int> vec(N);
  vector<int> cnt_vec(110, 0);
  
  for (int i = 0; i < N; i++) cin >> vec.at(i);
  
  for (int i = 0; i < N; i++){
    cnt_vec.at(vec.at(i))++;
  }
  
  for(int i = 0; i < 110; i++){
    if(cnt_vec.at(i) > 0){
      cnt++;
    }
  }
  
  cout << cnt << endl;
  
  
}






Measured memory_bytes: 256
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, a, b;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> a >> b;
    if((b - a + 1) & 1)
        cout << "Alice\n";
    else
        cout << "Borys\n";
    return 0;
}


Measured memory_bytes: 3656
import sys
n = int(raw_input())
data = [map(int, raw_input().split()) for i in range(n)]
t = 0
x = 0
y = 0
for d in data:
  duration = d[0] - t
  distance = abs(x - d[1]) + abs(y - d[2])
  if (distance > duration) or ((duration - distance) % 2 == 1):
    print 'No'
    sys.exit()
  t = d[0]
  x = d[1]
  y = d[2]
print 'Yes'

Measured memory_bytes: 21508
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define pi 3.1415926535897
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000000
#define inf1 1000000000
#define endl '\n'
pair<ll,pair<ll,ll>> arr[1005];
ll dp[1005][20005];
int main()
{
	fastio;
	#ifdef APNA_IO
		freopen("input.txt" , "rt" ,stdin);
		freopen("output.txt" , "wt" ,stdout);
	#endif
	ll n;
	cin>>n;
	for(ll i=1;i<=n;i++)
	{
		ll a,b,c;
		cin>>a>>b>>c;
		arr[i]=mp(a+b,mp(b,c));
	}
	sort(arr+1,arr+n+1);
	ll ma=arr[n].ff;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=ma;j++)
		{
			dp[i][j]=dp[i-1][j];
			if(j>=arr[i].ff-arr[i].ss.ff && j-arr[i].ff<=0)
			{
				dp[i][j]=max(dp[i][j],dp[i-1][j-arr[i].ff+arr[i].ss.ff]+arr[i].ss.ss);
			}	
		}
	}
	ll ans=0;
	for(ll i=1;i<=ma;i++)
		ans=max(ans,dp[n][i]);
	cout<<ans<<endl;
	return 0;
}

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

int main(){
  int n, m;
  cin >> n >> m;
  int lmax = 0;
  int rmin = 100000;
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    lmax = max(lmax, l);
    rmin = min(rmin, r);
  }
  int ans = (rmin - lmax + 1 > 0) ? rmin - lmax + 1 : 0;
  cout << ans << '\n';
}

Measured memory_bytes: 3600
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9;
const long long LINF = 1e18;
const double PI=3.14159265358979323846;

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

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

    int cnt = 1;
    int maxH = H[0];
    for (int i = 1; i < N; i++) {
        maxH = max(maxH, H[i - 1]);
        if (maxH <= H[i]) {
            cnt++;
        }
    }

    cout << cnt << endl;

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const ll mod=1e9+7;
const ll inf = (ll)1e17;
const ld eps = 1e-12;
const ld PI = 3.14159265358979323846;
ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

const int N=105;

vector<ll>wt(N);
vector<ll>val(N);

ll dp[N][100005];
ll kp(ll W,ll n){
    for(int i=0;i<=n;i++){
        for(int w=0;w<=W;w++){
            if(i==0 || w==0)dp[i][w]=0;
            else if(wt[i]<=w){
                dp[i][w]=max(dp[i-1][w],val[i]+dp[i-1][w-wt[i]]);
            }
            else{
                dp[i][w]=dp[i-1][w];
            }
        }
    }


    return dp[n][W];
    }

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;
    for(int i=1;i<=n;i++){
        cin>>wt[i]>>val[i];
    }
    ll res=kp(t,n);
    cout<<res<<endl;

    return 0;
    }


Measured memory_bytes: 80128
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main() {
    string s;
    getline(cin,s);
    for(int j=0;j<s.size();j++){
        if(s[j]>='a'&&s[j]<='z'){
            s[j]-=32;
        }
}cout<<s<<endl;
    return 0;
}



Measured memory_bytes: 2996
#include <algorithm>
#include <iostream>
#include <math.h>
#define ll long long int
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n)

using namespace std;

int main() {
    int n;
    ll ans = 0;
    cin >> n;
    int a[n], b[n];
    REP(i, n + 1)
    cin >> a[i];
    REP(i, n)
    cin >> b[i];

    REP(i, n) {
        if (a[i] <= b[i]) {
            ans += a[i];
            b[i] -= a[i];
            a[i] = 0;
        } else {
            ans += b[i];
            a[i] -= b[i];
            b[i] = 0;
        }
        if (a[i + 1] <= b[i]) {
            ans += a[i + 1];
            b[i] -= a[i + 1];
            a[i + 1] = 0;
        } else {
            ans += b[i];
            a[i + 1] -= b[i];
            b[i] = 0;
        }
        // if (i == 0) {
        //     if (a[i] >= b[i]) {
        //         ans += b[i];
        //         a[i] -= b[i];
        //         b[i] == 0;
        //     } else {
        //         ans += a[i];
        //         b[i] -= a[i];
        //         a[i] = 0;
        //     }
        // } else if (i == n) {
        //     if (a[i] >= b[i - 1]) {
        //         ans += b[i - 1];
        //         a[i] -= b[i - 1];
        //         b[i - 1] = 0;
        //     } else {
        //         ans += a[i];
        //         b[i - 1] -= a[i];
        //         a[i] = 0;
        //     }
        // } else {
        //     if (a[i] >= b[i - 1]) {
        //         ans += b[i - 1];
        //         a[i] -= b[i - 1];
        //         b[i - 1] = 0;
        //     } else {
        //         ans += a[i];
        //         b[i - 1] -= a[i];
        //         a[i] = 0;
        //     }
        //     if (a[i] >= b[i]) {
        //         ans += b[i];
        //         a[i] -= b[i];
        //         b[i] = 0;
        //     } else {
        //         ans += a[i];
        //         b[i] -= a[i];
        //         a[i] = 0;
        //     }
        // }
    }
    cout << ans << endl;
    return 0;
}


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

using namespace std;

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

typedef long long ll;

const int Inf = 1e9;
const double EPS = 1e-9;

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

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

int bitCount(long bits) {
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;  cin >> s;
    if (s.size() == 2) {
      cout << "ABC0" << s << '\n';
    } else {
      cout << "ABC" << s << '\n';
    }

    return 0;
}


Measured memory_bytes: 256
def main(S):
    if S[0] == 'R':
        if S[1] ==  'R':
            if S[2] == 'R':
                return 3
            else:
                return 2
        else:
            return 1
    else:
        if S[1] == 'R':
            if S[2] == 'R':
                return 2
            else:
                return 1
        else:
            if S[2] == 'R':
                return 1
            else:
                return 0

S = input()
print(main(S))

Measured memory_bytes: 9112
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pii pair<int, int>
#define fr(n) for (int i = 0; i < n; i++)
#define fr1(n) for (int i = 1; i <= n; i++)
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    string s;
    int w;
    cin >> s >> w;
    for (int i = 0; i < s.length(); i += w) cout << s[i];
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
typedef long long int ll;
 
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
 
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define mem(a) memset(a , 0 ,sizeof a)
#define rep(i,a,b) for(int i=a ; i<b ; ++i)
#define deb(x) cout << #x << " - " << x << "\n";
#define deb2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define D2(x) { cerr << "[" << #x << ": "; for(auto it:x) cerr << it << " "; cerr << "]\n"; }
#define clz(x) __builtin_clzll(x)
#define bit(x) __builtin_popcount(x)
//(a / b) + ((a % b) != 0);
 
#define PI 3.141592653589793
const ll MOD = 1e9 + 7;
const ll INF = 1000000000000;
const ll MAX_N = 1e5 + 7;


int main() {
    IOS;
    ll n,e; cin>>n>>e;
    vll adj[MAX_N];
    vll par(MAX_N);
    vll dist(MAX_N,INF),vis(MAX_N);
    ll a,b;
    for(int i=0;i<e;i++){
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    queue<ll>q;
    q.push(1);
    vis[1]=1;
    dist[1]=0;
    ll c=0;
    while(!q.empty()){
        ll node = q.front();
        q.pop();
        for(auto x:adj[node]){
            if(vis[x]==0){
                q.push(x);
                dist[x]=0;
                vis[x]=1;
                par[x]=node;
            }
        }
    }
        bool flag=true;
        for(ll i=1;i<=n;i++){
            if(dist[i]==INF){
                flag=false; break;
            }
        }
        if(flag==false) cout << "No";
        else{
            cout << "Yes\n";
            for(ll i=2;i<=n;i++) cout << par[i] <<"\n";
        }
    return 0;
}

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

using namespace std;

typedef long long ll;

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

	int N;
	cin >> N;

	string S;
	cin >> S;

	vector<int> bl(N+1, 0);
	for(int i = 0; i < N; i++){
		bl[i+1] += bl[i] + (S[i] == '#' ? 1 : 0);
	}

	vector<int> wh(N+1, 0);
	for(int i = N-1; i >= 0; i--){
		wh[i] += wh[i+1] + (S[i] == '.' ? 1 : 0)	;
	}

	int ans = 1e6;
	for(int i = 0; i < N+1; i++){
		ans = min(ans, bl[i] + wh[i]);
	}

	cout << ans << endl;
}


Measured memory_bytes: 2196
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define loop(i, a, b) for(lli i=a; i<b; i++)
#define loopb(i, a, b) for(lli i=a; i>=b; i--)
#define pb push_back
typedef long long int lli;
typedef pair<lli, lli> pii;
typedef vector<lli> vll;
// use to avoid collision
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);
    }
};
// for pair in unordered_map
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
lli power(lli x, lli y){
    lli res = 1;
    x = x ;
    while (y > 0) {
        if (y & 1)
            res = (res*x) ;
        y = y>>1;
        x = (x*x);
    }
    return res;
}
double dp[310][310][310] ;
double calc(lli a, lli b, lli c, lli n){
	if(a + b + c == 0)
		return 0.0;
	else if(dp[a][b][c] >= 0.0)
		return dp[a][b][c];
	double res = 0.0;
	res += n;
	if(a > 0)
		res += a * calc(a-1, b, c, n);
	if(b > 0)
		res += b * calc(a+1, b-1, c, n);
	if(c > 0)
		res += c * calc(a, b+1, c-1, n);
	res *= 1.0 / (a+b+c);
	dp[a][b][c] = res;
	return dp[a][b][c];
}
lli solve(){
	lli n;
	cin>>n;
	lli a=0, b=0, c=0;
	memset(dp,-1,sizeof(dp));
	loop(i, 0, n){
		lli temp;
		cin>>temp;
		if(temp == 1)
			a++;
		else if(temp == 2)
			b++;
		else
			c++;
	} 
	double ans = calc(a, b, c, n);
	cout<<setprecision(15)<<ans<<"\n";
	return 0;
}
int main(){
	fastio;
	lli t;
//	cin>>t;
	t = 1;
	while(t>0){
		t--;
		solve();
	}
}

Measured memory_bytes: 233088
// AOJ 1083
#include<cstdio>
#include<cstring>
#include<map>
#define rep(i,a) for( int i = 0; i != (a); ++i )
#define clr(a,v) memset( (a), (v), sizeof(a) )

int q, lim;
int sz, dat[(1<<20)+2];
int low = (1<<19)-1;
std::map<int, int> num;
int rnum[400000];

void add( int x )
{
	num[x] = sz;
	dat[sz] = 0;
	rnum[sz++-low] = x;
	--lim;

	int k = sz-1;
	while( k )
	{
		++dat[k];
		k = (k-1)>>1;
	}

	return;
}

int find( int x )
{
	int k = 0;

	while( k < low )
	{
		int left = (k<<1)+1,
			right = (k<<1)+2;

		if( x <= dat[left] )
			k = left;
		else
			x -= dat[left], k = right;
	}

	return k;
}

void erase( int k )
{
	++lim;

	while( k )
	{
		--dat[k];
		k = (k-1)>>1;
	}

	return;
}

int main()
{
	while( scanf( "%d%d", &q, &lim ), q|lim )
	{
		clr( dat, 0 );
		clr( rnum, 0 );
		num.clear();
		sz = low;

		rep( i, q )
		{
			int query, x;
			scanf( "%d%d", &query, &x );

			if( !query )
			{
				add( x );

				if( lim < 0 )
					erase( find( 0 ) );
			}
			else if( query == 1 )
				erase( find( x ) );
			else if( query == 2 )
				printf( "%d\n", rnum[find(x)-low] );
			else
				erase( num[x] );
		}

		puts("end");
	}

	return 0;
}

Measured memory_bytes: 25460
#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <string>
#include <algorithm> 
#include <set>
#include <sstream>
#include <bit>
#include <bitset>
#include <iomanip>
#include <queue>
typedef long long int ll;
using namespace std;
int main() {

	int N; cin >> N;
	ll ans=0;
	vector<vector<ll>>data(3, vector<ll>(N)), value(3, vector<ll>(N, 0));
	for (int i = 0; i < N; i++)cin >> data[0][i] >> data[1][i] >> data[2][i];
	for (int i = 0; i < 3; i++)value[i][0] = data[i][0];

	for (int i = 0; i < N - 1; i++) {
		for (int j = 0; j < 3; j++) {
			value[j][i + 1] = max(value[(j+1)%3][i] + data[j][i + 1], value[(j+2)%3][i] + data[j][i + 1]);
		}
	}
	for (int i = 0; i < 3; i++) {
		if (ans < value[i][N - 1])ans = value[i][N - 1];
	}
	cout << ans << endl;
}

Measured memory_bytes: 8188
n,m = map(int,input().split())
a = [input().rstrip() for i in range(n)]
b = list(zip(*a))
for i in range(m):
  b[i] = "".join(b[i])
ans = 0
ct1 = [[0 for k in range(n-j+1)] for j in range(n+1)]
ct2 = [[0 for k in range(n-j+1)] for j in range(n+1)]
dp = [[10**5 for i in range(n+1)] for j in range(n+1)]
for i in range(m-1):
  b1 = b[i]
  b2 = b[i+1]
  for j in range(n+1):
    ct1[j][0] = 0
    ct2[j][0] = 0
    for k in range(n-j):
      ct1[j][k+1] = ct1[j][k]
      ct2[j][k+1] = ct2[j][k]
      if b1[k+j] == b2[k]:
        ct1[j][k+1] += 1
      if b2[k+j] == b1[k]:
        ct2[j][k+1] += 1
  dp[0][0] = 0
  for i in range(n+1):
    if i < n:
      dp[i+1][0] = 10**6
    for j in range(n+1):
      if i == 0 and j < n:
        dp[i][j+1] = 10**6
      if i <= j:
        cost = ct2[j-i][n-j]
      else:
        cost = ct1[i-j][n-i]
      if j < n:
        dp[i][j+1] = min(dp[i][j+1],dp[i][j]+cost)
      if i < n:
        dp[i+1][j] = dp[i][j]+cost
  ans += dp[-1][-1]
print(ans)

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

using namespace std;
int a[100000+5];
int dp[100000+5];
int n;
int solve(int i)
{
    if(i==n-1) return 0;
    int &ret = dp[i];
    if(ret!=-1) return ret;
    ret = 1e9;
    if(i+1<=n-1)
    {
        ret = min(ret,solve(i+1)+abs(a[i+1]-a[i]));
    }
    if(i+2<=n-1)
    {
        ret = min(ret,solve(i+2)+abs(a[i+2]-a[i]));
    }
    return ret;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    memset(dp ,-1,sizeof dp);

    int ans =   solve(0);

    cout<<ans<<endl;

}


Measured memory_bytes: 4096
N=io.read("n")
a={}
for i=1,N do
  a[i]=io.read("n")
end
table.sort(a,function (a,b) return a>b end)
local k=1
local j=2
local A=0
local B=0
while a[k] do
  A=A+a[k]
  k=k+2
end
while a[j] do
  B=B+a[j]
  j=j+2
end
print(A-B)

Measured memory_bytes: 760
#include<bits/stdc++.h>
#include<string>
#include<math.h>
# define M_PI 3.14159265358979323846
using namespace std;
int main()
{
    int n,m,i,s,a[200001],b[200001];
    cin>>n;

    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(i=1;i<n;i++)
    {
        cin>>a[i];
        b[a[i]]++;
    }
    for(i=1;i<=n;i++)
    {
        cout<<b[i]<<endl;
    }



    return 0;
}


Measured memory_bytes: 5088
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;

string a;

int main(){
    cin >> a;
    int s[10];
    s[0] = a[0]-'0';
    s[1] = a[1]-'0';
    s[2] = a[2]-'0';
    s[3] = a[3]-'0';
    if(s[0]-s[1]-s[2]-s[3] == 7) printf("%d-%d-%d-%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]-s[1]-s[2]+s[3] == 7) printf("%d-%d-%d+%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]-s[1]+s[2]-s[3] == 7) printf("%d-%d+%d-%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]-s[1]+s[2]+s[3] == 7) printf("%d-%d+%d+%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]+s[1]-s[2]-s[3] == 7) printf("%d+%d-%d-%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]+s[1]-s[2]+s[3] == 7) printf("%d+%d-%d+%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]+s[1]+s[2]-s[3] == 7) printf("%d+%d+%d-%d=7", s[0], s[1], s[2], s[3]);
    else if(s[0]+s[1]+s[2]+s[3] == 7) printf("%d+%d+%d+%d=7", s[0], s[1], s[2], s[3]);
    return 0;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(void)
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  unsigned long long int N, K, sum = 0, i;
  cin >> N >> K;
  for (i = K; i <= N + 1; i++){
    sum += ((N + 1 - i) * i + 1);
    sum %= 1000000007;
  }
  cout << sum << '\n';
  return 0;
}


Measured memory_bytes: 3664
// Author: pankaj_m05 
// Problem: D
// Created: 17.05.2020 18:20:52

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define f_in(file) freopen(file, "r", stdin)
#define f_out(file) freopen(file, "w", stdout)
#define sz(a) int((a).size())
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) c.begin(), c.end()
#define tr(c,i) for(typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c,x) (c.find(x) != c.end())  // Set and Maps
#define cpresent(c,x) (find(allc,x) != c.end()) // Vectors
using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int, int > ii;
typedef long long ll;

const int mod = 1e9 + 7;
const int mxN = 1e5;

int n;
vector< int > adj[mxN + 1];
bool vis[mxN + 1];
int sign[mxN + 1] = { 0 };


void bfs() {
	queue<int> q;  
	q.push(1);
	while(!q.empty()) {
		int v = q.front();
		q.pop();
		//cout << v << " ";
		if(!vis[v]) {
			vis[v] = 1;
			for(int u : adj[v]) { 
				q.push(u);
				if(sign[u] == 0) 
					sign[u] = v;
			}
		}
	}
}
                      
int main(void) { 
	fast
	int m;
	bool flag = 1;
	cin >> n >> m;                     
	for(int i = 0; i < m; ++i) {
		int u, v; 
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
		if(u == 1 || v == 1)
			flag = 0;
	}
	if(flag){
		cout << "No" << '\n';
		return 0;
	}
	cout << "Yes" << '\n';
	bfs();
	for(int i = 2; i <= n; ++i) {
		cout << sign[i] << '\n'; 
	}
	return 0;
}                                          

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

const int N = 4;
const int N2 = 16;
const int LIMIT = 45;
const int mx[4] = {0,-1,0,1};
const int my[4] = {-1,0,1,0};

struct Puzzle {
    int p[N2], h, d;
};

Puzzle ans;
int limit, md[N2][N2];

bool visit(Puzzle a,int prev) {
    if(a.h==0) {
        ans = a;
        return true;
    }
    if(a.d + a.h > limit) return false;
    int blank; Puzzle b;
    for(int i=0; i<N2; i++) {
        if(a.p[i]==0) blank=i;
    }
    int bx = blank%N, by = blank/N;

    for(int i=0; i<4; i++) {
        int x=bx+mx[i],y=by+my[i];
        if(x<0 || x>=N || y<0 || y>=N) continue;
        if(abs(prev-i) == 2) continue;
        b = a;
        b.h += md[blank][b.p[y*N+x]-1] - md[y*N+x][b.p[y*N+x]-1];
        swap(b.p[blank],b.p[y*N+x]);
        b.d++;
        if(visit(b,i)) return true;
    }
    return false;
}

void IDA(Puzzle init) {
    for(int i=0; i<N2; i++)
        if(init.p[i]!=0) init.h+=md[i][init.p[i]-1];
    for(limit=init.h; limit <= LIMIT; limit++) {
        if(visit(init,10)) break;
    }
}

int main() {
    Puzzle init;
    init.h = init.d = 0;
    for(int i=0; i<N2; i++)
        for(int j=0; j<N2; j++)
            md[i][j] = abs(i/N-j/N)+abs(i%N-j%N);

    for(int i=0; i<N2; i++) cin >> init.p[i];
    IDA(init);
    cout << ans.d << endl;
    return 0;
}



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

#define pow(n) (n)*(n)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

using namespace std;

/* function */
//  - none - 


/* code */
int main (void) 
{

  /* Variable_Proclamation */
  int N;    // input
  int K;    // input

  int i;    // subscript
  int count;


  /* Input, Array_Proclamation */
  cin >> N >> K;


  /* Initialize */
  //  - none -


  /* Calculation */
  count = K;
  for(i=1; i<=N-1; i++)
  {
    count *= (K-1);
  }


  /* Output */
  cout << count << endl;


  return 0;

}


/* entity */
//  - none -




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

int main()
{
    int N;
    cin >> N;
    
    bool flag = false;
    
    for (int i = 1; i<=9; i++){
        int ans;
        if (N % i == 0){
            ans = N /i;
            if(ans < 10){
                flag = true;
            }
        }
    }
    
    if(flag){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
    return 0;
}


Measured memory_bytes: 384
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
void chmax(int &A, int B) {A = max(A, B);}

int dp[60][60][10];
const int INF = 1 << 28;
int main() {
  int N, M; cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  for(int i=0; i<N; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  
  fill(dp[0][0], dp[60][0], -INF);

  dp[0][M][0] = 0;
  for(int i=0; i<N; i++) {
    for(int j=0; j<=M; j++) {
      for(int k=0; k<=8; k++) {
        if(dp[i][j][k] == -INF) continue;

        for(int x=0; x<=min(j, 8); x++) {
          int br = x;
          int dk = k;

          for(int p=0; p<=br; p++) {
            for(int q=0; q<=min(dk, 8-p); q++) {
              int cost = 0;
              if(p + q == 0) {
                cost = C[i];
              }
              else {
                cost = A[i] * p + B[i] * q;
              }

              chmax(dp[i+1][j-x][x], dp[i][j][k] + cost);
            }
          }
        }
      }
    }
  }

  int ans = -INF;
  for(int i=0; i<=M; i++) {
    for(int j=0; j<10; j++) {
      chmax(ans, dp[N][i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 3252
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int w[105];
int s[105];
int ans=10000000;
int main(void){
    int n; cin>>n;
    cin>>w[0]; s[0]=w[0];
    for(int i=1;i<n;i++){
        cin>>w[i];
        s[i]=s[i-1]+w[i];
    }
    for(int i=0;i<n;i++){
        ans=min(ans,abs((s[n-1]-s[i])-(s[i])));
        //cout<<abs((s[n-1]-s[i])-(s[i]))<<endl;
    }
    cout<<ans<<endl;
}


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

using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define REP(i,n) for(ll i = 0;i < (ll)n;i++)
#define REPD(i,n) for(ll i = (ll)n - 1;i >= 0;i--)
#define ALL(x) (x).begin(),(x).end()
#define FILL(n,first_dimension_size,value) fill((ll*)n,(ll*)(n+first_dimension_size),value)
#define INF (1 << 29)
#define MOD 1000000007
//int dx[4] = {1,0,0,-1};
//int dy[4] = {0,1,-1,0};


int main(){
  
  int n;
  ll res = 0;
  cin >> n;
  vector<ll> a(n+1),b(n);
  REP(i,n+1)cin >> a[i];
  REP(i,n)cin >> b[i];
  REP(i,n){
    if(a[i]-b[i] >= 0){
      res += b[i];
    }else{
      res += a[i];
      b[i] -= a[i];
      if(a[i+1]-b[i] >= 0){
        a[i+1] -= b[i];
        res += b[i];
      }else{
        res += a[i+1];
        a[i+1] = 0;
      }
    }
  }

  cout << res;

  return 0;
}


Measured memory_bytes: 1792
n,k = map(int,input().split())
if n >= k:
    answer = (n-k+1)/n
else:
    answer = 0.0

for i in range(1,min(n+1,k)):
    j = 1
    while True:
        if i*(2**j) >= k:
            answer += (0.5**j)/n
            break
        else:
            j += 1

print(answer)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (int)(n); i++ )
using namespace std;
int findSumOfDigits(int n) {
  int sum = 0;
  while (n > 0) { // n が 0 になるまで
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
int main(){
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    long long e, f , g , h;
    e = a * c;
    f = a * d;
    g = b * c;
    h = b * d;
    vector<long long> x(4);
    x.at(0) = e;
    x.at(1) = f;
    x.at(2) = g;
    x.at(3) = h;
    sort(x.begin(), x.end());
    cout << x.at(3) << endl;
}

Measured memory_bytes: 3636
n = int(input())
l = list(map(int,input().split()))
l.sort()
a = l[-1]
j = 0
while l[j] < int((a+1)/2):
    j += 1
else:
    if l[j] - int((a+1)/2) < int((a+1)/2) - l[j-1]:
        b = l[j]
    else:
        b = l[j-1]
print("{} {}".format(a,b))

Measured memory_bytes: 14428
/*
RAone00
Rohit sharma
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < ll, ll > pll;
typedef vector < vector < ll > > matrix;
typedef vector < ll > vll;

#define pb push_back
#define debug(x) cout << (#x) << " is " << (x) << endl
#define fast_io() ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)

const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX;
const ll N = 1e5 + 10;

int main() {
    fast_io();
     int a, s;
     cin >> a >> s;
     if(a == s) {
       cout << "Yes\n";
     } else {
       cout << "No\n";
     }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int n,a[10],b[10],ans;
int p;

void saiki(int ima,int sum){
  if(sum==p) ans++;
  if(ima==n||sum>=p) return;
  for(int i=0;i<=b[ima];i++) saiki(ima+1,sum+a[ima]*i);
}

int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    int m;
    cin>>m;
    while(m--){
      cin>>p;
      ans=0;
      saiki(0,0);
      cout <<ans<<endl;
    }
  }
  return 0;
}

Measured memory_bytes: 1160
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    default = 0

    c = [[0] * 10 for _ in range(10)]

    for i in range(1, n + 1):
        i = str(i)
        c[int(i[0])][int(i[-1])] += 1

    ans = 0
    for i in range(10):
        for j in range(10):
            ans += c[i][j] * c[j][i]

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Measured memory_bytes: 40428
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int,int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define dbg(x) cout<<#x<<": "<<x<<endl
#define clr(x,v) memset(x, v, sizeof(x))
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 1e6 + 5;

ll mul(ll a, ll b)
{
    ll sg = b / abs(b);
    b = abs(b);
    ll res = 0;
    while(b > 0)
    {
        if(b & 1)
            res = (res + a) % mod;
        a = (a + a) % mod;
        b = b / 2;
    }
    res *= sg;
    res = (res + mod) % mod;
    return res;
}

void cp()
{
    int n, k;
    cin >> n >> k;
    vector<ll> arr(n);
    for(ll &x : arr)
        cin >> x;
    ll res = 1;
    sort(all(arr));
    if(arr.back() == 0 && (k & 1))
    {
        cout << 0;
        return;
    }
    if (arr.back() <= 0 && (k & 1))
    {
        for (int i = n - 1; i >= n - k; i--)
            res = (res % mod * (arr[i] * 1LL + mod) % mod + mod) % mod;
        cout << res;
        return;
    }
    int i = 0;
    int j = n - 1;
    if (k & 1)
    {
        res = (arr[j]) % mod;
        j--;
        k--;
    }
    k /= 2;
    for (int it = 0; it < k; it++)
    {
        ll lp = arr[i] * 1LL * arr[i + 1];
        ll rp = arr[j] * 1LL * arr[j - 1];
        if (lp > rp)
        {
            res = mul(res, (lp + mod) % mod);
            i += 2;
        }
        else
        {
            res = mul(res, (rp + mod) % mod);
            j -= 2;
        }
    }
    cout << res;
}

int main()
{
    FASTIO;
    int t;
    t = 1;
    // cin >> t;
    while(t--)
    {
        cp();
    }
    return 0;
}

Measured memory_bytes: 4764
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <complex>
#include <algorithm>
#include <deque>

using namespace std;
using i64 = int_fast64_t;
using u64 = uint_fast64_t;
using i32 = int_fast32_t;
using u32 = uint_fast32_t;

#define int i64

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; }
const int INF = 1LL << 60;

signed main() {
	string buf;
	cin >> buf;

	deque<char> s(buf.size());
	for(size_t i = 0; i < buf.size(); i++) s[i] = buf[i];

	int q;
	cin >> q;

	int t, f;
	char c;
	size_t h = 0;
	for(size_t i = 0; i < q; i++) {
		cin >> t;
		if(t == 1) {
			h ^= 1;
		} else {
			cin >> f >> c;
			switch(f) {
			case 1:
				if(h == 0) s.push_front(c);
				else s.push_back(c);
				break;
			case 2:
				if(h == 0) s.push_back(c);
				else s.push_front(c);
			}
		}
	}

	for(size_t i = 0; i < s.size(); i++) {
		if(h == 0) cout << s[i];
		else cout << s[s.size() - i - 1];
	}
	cout << endl;
	
	return 0;
}


Measured memory_bytes: 896
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    int N, K, S;
    cin >> N >> K >> S;
    int A[N];
    for(int i = 0; i < K; i++){
            A[i] = S;
    }
    if(S != 1e9){
        for(int i = K; i < N; i++){
            A[i] = 1e9;
        }
    }else{
        for(int i = K; i < N; i++){
            A[i] = 1;
        }
    }
    for(int i = 0; i < N-1; i++){
        cout << A[i] << " ";
    }
    cout << A[N-1] << endl;
    return 0;
}

Measured memory_bytes: 1664
#include <cstdio>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;

int main(void) {
  int a, b, h;
  scanf("%d%d%d", &a, &b, &h);
  printf("%d\n", (a+b)*h/2);
  return 0;
}



Measured memory_bytes: 128
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<utility>

using namespace std;

using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
const ll INF = (1e+18)+100;
const ll mod = 1000000007;
#define rep(i, n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define repl(i, l, n) for(int i=l;i<n;++i)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define stop char nyaa;cin>>nyaa;

typedef long double ld;
const ld eps = 1e-8;

void solve() {
	ll t1, t2;
	cin >> t1 >> t2;
	ll a[2], b[2];
	rep(i, 2)cin >> a[i];
	rep(i, 2) {
		cin >> b[i];
	}
	ll s1 = t1 * a[0] + t2 * a[1];
	ll s2 = t1 * b[0] + t2 * b[1];
	if(s1==s2){
		cout << "infinity" << endl; return;
	}
	if (s1 > s2) {
		rep(i, 2)swap(a[i], b[i]);
		swap(s1, s2);
	}
	ll x = s2 - s1;
	ll z = (a[0] - b[0])*t1;
	if (z < 0) {
		cout << 0 << endl; return;
	}
	ll ans = (z / x) * 2;
	ans++;
	if (z%x == 0)ans--;
	cout << ans << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	solve();
	//stop
	return 0;
}

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

int main() {
  
  string s; cin >> s;
  
  bool flag = true; int ans = 100;
  
  for (int i = 0; i < s.size() - 1; i++) {
    
    if (s.at(i) != s.at(i + 1)) flag = false;
    
  }
  
  if (flag) { cout << 0 << endl; return 0; }
  
  for (int i = 0; i < s.size(); i++) {
    
    int count = 0;
    
    char target = s.at(i);
    
    string tmp = s, shaped = "";
    
    while (true) {
      
      count++;
      
      for (int j = 0; j < tmp.size() - 1; j++) {
        
        if (tmp.at(j) == target || tmp.at(j + 1) == target) {
          shaped = shaped + target;
        }
        
        else {
          shaped = shaped + tmp.at(j);
        }
        
      }
      
      string rightStr = "";
      
      for (int j = 0; j < shaped.size(); j++) rightStr = rightStr + target;
      
      if (rightStr == shaped) break;
      
      else { tmp = shaped; shaped = ""; }
      
    }
    
    ans = min(ans, count);
    
  }
  
  cout << ans << endl;
  
}

Measured memory_bytes: 3592
#include<bits/stdc++.h>
 
#define rep(i,n) for(ll i = 0;i < n;++i)
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
typedef pair<int,int> P;
 
const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9+7;
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};

vector<pair<ll,ll>> f(ll n){
    vector<pair<ll,ll>> p;
    for(ll i = 2;i * i<=n;++i){
        if(n%i == 0){
            ll tmp = 0;
            while(n%i == 0){
                n/=i;
                tmp++;
            }
            p.push_back(make_pair(i,tmp));
        }
    }
    if(n != 1) p.push_back(make_pair(n,1));
    return p;
}



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

    ll n,a,b;
    cin >> n >> a >> b;
    ll ans = 0;
    if(a%2 == b%2){
        ans = abs(a-b);
        ans /= 2;
    }else {
        ll t;
        t = min(n-a,b-1);
        if(t == n-a) {
            ans += n-b;
            ans++;
            ll tmp = n - (a+ans);
            tmp /= 2; 
            ans += tmp;
        }else {
            ans += a-1;
            ans++;
            ll tmp = (b-ans) - 1;
            tmp /= 2;
            ans += tmp;
        }
    }
    cout << ans << endl;
    cout << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <cmath>   
#include<cctype>
#include<string>
#include<set>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility> 
#define all(x) (x).begin(),(x).end()
#define rep(i,m,n) for(int i = m;i < n;++i)
#define pb push_back
#define fore(i,a) for(auto &i:a)
#define rrep(i,m,n) for(int i = m;i >= n;--i)
#define INF INT_MAX/2
using namespace std;
using ll = long long;
using R = double;
const ll inf = 1LL << 50;
const ll MOD = 1e9 + 7;
struct edge { ll from; ll to; ll cost; };

string dp[10101];

void chmax(string &a, string b) {
	if (a == "-1")a = b;
	else if (a.size() < b.size())a = b;
	else if (a.size() == b.size() && a < b)a = b;
}

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

	vector<int>num = {0,2,5,5,4,5,6,3,7,6};
	vector<int>li;

	rep(i, 0, m) {
		int A;
		cin >> A;
		li.pb(A);
	}

	rep(i, 0, 10101)dp[i] = "-1";
	dp[0] = "";

	rep(i, 0, 10010) {
		if (dp[i] == "-1")continue;
		rep(j,0,li.size()) {
			int temp = li[j];
			chmax(dp[i+num[temp]],dp[i] + to_string(temp));
		}
	}

	cout << dp[n] << endl;


	return 0;
}

Measured memory_bytes: 34048
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
using namespace std;
const long long mod = 1'000'000'007;

int main(){
  long long N,M;cin>>N>>M;
  vector<long long> a(M); for(auto &e:a) cin>>e;
  if (M==0) {
    a.push_back(0);
  }

  vector<long long> c(N+1);
  c[0] = 1;
  c[1] = 1; 
  long long ng = 0;
  if(a[ng]==1) { c[1] = 0; ng++; }

  for(long long i=2;i<=N;++i) {
    if (i==a[ng]) {
      c[i] = 0;
      if (c[i-1]==0) {
        cout << 0 << endl;
        return 0;
      }
      ng++;
    }
    else {
      c[i] = (c[i-1] + c[i-2]) % mod;
    }
  }

  cout << c[N] << endl;
  return 0;
}



Measured memory_bytes: 1792
#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

#pragma GCC diagnostic warning "-std=c++11"

string a, b;

int main() {
  cin >> a >> b;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  cout << (a < b ? "Yes" : "No") << '\n';
  return 0;
}



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

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x,y) CPP_CAT_I(x,y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x,y) x ## y

using i8  = int8_t;
using u8  = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end) for(i64 i = (start), CPP_CAT(i,xxxx_end)=(end); i < CPP_CAT(i,xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for(i64 i = 1; i <= n; ++i)
#define RREP(i, n) for(i64 i = n - 1; i >= 0 ; --i)
#define RREPS(i, n) for(i64 i = n; i >= 1; --i)

#define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c))

#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define pb push_back
#define mp make_pair

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

template<typename C>
i64 SIZE(const C& c) { return static_cast<i64>(c.size()); }

template<typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }


struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;


int main() {
    i64 X, Y; cin >> X >> Y;
    for (i64 nx = 0; nx <= X; nx++) {
        i64 ny = X - nx;
        if (2 * nx + 4 * ny == Y) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 3608