akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define int long long int
#define ldouble long double
#define vin vector<int> 
#define rep(i,a,n) for(i=(a);i<(n);i++)
#define INF (int)1000000007

//etprecision()<<

using namespace std;

void fin(int i,int n,vin &a){
    rep(i,0,n){
        cin>>a.at(i);
    } 
} 

signed main() {
    int i,j;
    int H,W,N;
    cin>>H>>W>>N;
    vin a(N);
    vector<vector<int> > HW(H,vector<int>(W));
    rep(i,0,N){
        cin>>a.at(i);
    }

    int noww=0,nowh=0; //今のh,w座標
    rep(i,0,N){
        if(a.at(i)<=W-noww){
            rep(j,noww,noww+a.at(i)){
                //cout<<"noww "<<noww<<" nowh "<<nowh<<" j "<<j<<" i "<<i<<endl;
                HW.at(nowh).at(j)=i+1;
            }

            noww+=a.at(i);
            if(noww>W-1){
                noww-=(W-1)+1;
                nowh++;
            }
        }
        else{
            int h=(a.at(i)-(W-noww))/W;
            int next=a.at(i)-(W-noww)-W*h;
            //cout<<"a:"<<a.at(i)<<endl;
            rep(j,noww,noww+a.at(i)){
                //cout<<"j:"<<j<<endl;
                HW.at(nowh).at(j%W)=i+1;
                //cout<<(j%W)<<"j%W"<<"noww"<<noww<<"noww+a"<<noww+a.at(i)<<endl;
                if((j+1)%W==0){
                    nowh++;
                    //noww=0;
                }
            }
            noww=next;
        }
        //cout<<noww<<" "<<nowh<<endl;
    }

    rep(i,0,H){
        if(i%2==0){
            rep(j,0,W){
                cout<<HW.at(i).at(j)<<" ";
            }
            //cout<<"aa"<<endl;
        }
        else{
            for(j=W-1;j>=0;j--){
                  cout<<HW.at(i).at(j)<<" ";
            }
            //cout<<"bb"<<endl;
        }
        cout<<endl;
    }    
return 0;
}

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

int main() {
    int N; cin >> N;
    map<string, int> mp;
    for (int i = 0; i < N; i++) {
        string S; cin >> S;
        mp[S]++;
    }

    for (auto e: {"AC", "WA", "TLE", "RE"}) {
        cout << e << " x " << mp[e] << endl;
    }
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define ll long long
int main(){
  int N,K;
  cin>>N>>K;
  cout<<N%K<<endl;
}

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

using namespace std;

int main()
{
    int n, k, s;
    cin>>n>>k>>s;
    vector<int>Ans;
    for(int i=1 ; i<=k ; i++) Ans.push_back(s);
    int t = 1e9;
    if(t == s) t--;
    for(int i=1 ; i<=n-k ; i++) Ans.push_back(t);
    for(auto X : Ans) cout<<X<<" ";
    
    return 0;
}


Measured memory_bytes: 1784
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
#define MOD 1000000007
#define INF 1<<30
#define LINF (ll)1<<62
#define MAX 510000
#define rep(i,n) for(ll 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 uni(q) unique(all(q)),q.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

ll fact(ll k){
    if(k==0) return 1;
    else return k*fact(k-1);
}

ll comb(ll n, ll k){
    if(k==0) return 1;
    if(k > n/2) k = n - k;
    ll t = 1;
    rep(i,k){
        t *= n;
        n--;
    }
    return t / fact(k);
}

ll power(ll x, ll m){
    if(m == 0) return 1;
    if(m == 1) return x;
    if(m % 2 == 0){
        return power(x*x,m/2);
    }else{
        return power(x*x,m/2)*x;
    }
}

int main(){
    ll n,p; cin >> n >> p;
    vl a(n);
    ll cnt = 0;
    ll ans = 0;
    rep(i,n){
        cin >> a[i];
        if(a[i] % 2 == 1) cnt++;
    }
    ll cnt0 = power(2,n-cnt);
    if(cnt % 2 == p){
        ll t = cnt;
        while(t >= 0){
            ans += cnt0 * comb(cnt,t);
            t -= 2;
        }
    }else{
        ll t = cnt-1;
        while(t >= 0){
            ans += cnt0 * comb(cnt,t);
            t -= 2;
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
using System;
using System.Linq;
public class Hello{
    public static void Main(){
        var input = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        var min = long.MaxValue;
        
        for(var i = 0;i < 3;i++){
            var a = 1L;
            var b = 1L;
            
            for(var j = 0;j < 3;j++){
                if(i == j){
                    a *= input[j] / 2;
                    b *= input[j] - input[j] / 2;
                }else{
                    a *= input[j];
                    b *= input[j];
                }
            }
            min = Math.Min(min,Math.Abs(a - b));
        }
        Console.WriteLine(min);
    }
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  int ans = 0;

  for ( int i = 1; i <= n; i += 2 ) {
    int counter = 0;
    for ( int j = 1; j <= i; j += 2 ) {
      if ( i % j == 0 ) counter++;
    }
    if ( counter == 8 ) ans++;
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 256
n=int(input())
h=input().split()
for i in range(n):
    h[i]=int(h[i])
a=[0 for i in range(n)]
for i in range(n):
    if i>0 and h[n-i]<=h[n-i-1]:
        a[n-i-1]+=a[n-i]+1
print(max(a))

Measured memory_bytes: 68068
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<int> aa(n);
  vector<int> sum(1<<n);
  for(int & i : aa) cin >> i;
  for(int i = 1; i < 1<<n; i++){
  	for(int j = 0; j < n; j++) if(i&(1<<j)) sum[i] += aa[j];
  }
  int ans = 1<<30;
  for(int i = 1; i < 1<<n; i++){
    for(int j = 1; j < 1<<n; j++){
      for(int k = 1; k < 1<<n; k++){
        if((i&j) == 0 && (i&k) == 0 && (j&k) == 0){
			int df = 0;
          	df += abs(sum[i] - a);
          df += abs(sum[j] - b);
          df += abs(sum[k] - c);
          df += 10 * __builtin_popcount(i) - 10;
          df += 10 * __builtin_popcount(j) - 10;
          df += 10 * __builtin_popcount(k) - 10;
          ans = min(ans, df);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
<?php
fscanf(STDIN, "%d %d %d %d", $N, $A, $B, $C);
$arrayL = array();
for ($i = 0; $i < $N; $i ++) {
    $arrayL[$i] = fgets(STDIN);
}
$arrayA = array();
$arrayB = array();
$arrayC = array();
$minMP = 3000;

// 全探索
// N個の竹をどこで使用するか、A,B,C,未使用の4つに分類する
// 11(2):A, 10(2):B, 01(2):C, 00(2):未使用
for ($i = 0; $i < pow(4, $N); $i ++) {
    $arrayA = array();
    $arrayB = array();
    $arrayC = array();
    $counter = 0;
    for ($j = ($N - 1) * 2; $j >= 0; $j = $j - 2) {
        // 11(2)=3(10), 10(2)=2(10), 01(2)=1(10), 00(2)=0(10)
        if (($i & (3 << $j)) === (3 << $j)) {
            $arrayA[] = $arrayL[$counter];
        } elseif (($i & (3 << $j)) === (2 << $j)) {
            $arrayB[] = $arrayL[$counter];
        } elseif (($i & (3 << $j)) === (1 << $j)) {
            $arrayC[] = $arrayL[$counter];
        } elseif (($i & (3 << $j)) === (0 << $j)) {
            // 何もしない
        }
        $counter ++;
    }

    if (count($arrayA) * count($arrayB) * count($arrayC) === 0) {
        continue;
    }

    // A,B,C作成にかかるコスト
    // 延短魔法
    $value = abs(array_sum($arrayA) - $A) + abs(array_sum($arrayB) - $B) + abs(array_sum($arrayC) - $C);
    // 合成魔法
    $value += (count($arrayA) - 1) * 10 + (count($arrayB) - 1) * 10 + (count($arrayC) - 1) * 10;
    if ($minMP > $value) {
        $minMP = $value;
    }
}
echo $minMP;


Measured memory_bytes: 3956
#codinf utf-8
s = list(input())
K = int(input())
N = len(s)
alp = [chr(i) for i in range(97, 97+26)]

for i in range(N):
    idx = alp.index(s[i])
    
    if K + idx >= 26 and idx != 0:
        K = K - ( 26 - idx )
        s[i] = 'a'
    if K == 0:
        break
    
if K > 0:
    id = alp.index(s[N-1])

    id_ = ( id + K ) % 26

    s[N-1] = alp[id_]

print(''.join(s))

Measured memory_bytes: 4152
N = int(input())
a = list(map(int, input().split()))
if N == 1:
    if a[0] != 1:
        print(1)
        exit()
    else:
        print(0)
        exit()
a.sort()
A = 0
c = 0
while True:
    A = a[0]
    if len(a) > 1:
        for i in range(1, len(a)):
            if a[i] != A:
                del a[:i]
                if i >= A:
                    c += i - A
                else:
                    c += i
                break
            elif i == len(a) - 1:
                if i + 1 >= A:
                    c += i + 1 - A
                else:
                    c += i + 1
                print(c)
                exit()
    else:
        print(c + 1)
        exit()


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

int main(void)
{
	int h, w;
	int i, j;
	
	while (1){
		cin >> h >> w;
		
		if (h == 0 && w == 0){
			break;
		}
		
		for (i = 0; i < h; i++){
			if (i == 0 || i == h - 1){
				for (j = 0; j < w; j++){
					cout << "#";
				}
			}
			else {
				cout << "#";
				for (j = 0; j < w - 2; j++){
					cout << ".";
				}
				cout << "#";
			}
			cout << endl;
		}
		
		cout << "\n";
	}
	
	return (0);
}

Measured memory_bytes: 1152
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;

/*---------------------------------------------------*/

int prime[100005];
bool is_prime[10000005];

void sieve(){
  int p=0;
  for(int i=0;i<10000000;i++)is_prime[i]=true;
  is_prime[0]=is_prime[1]=false;
  for(int i=2;i<10000000;i++){
    if(is_prime[i]){
      prime[p++]=i;
      for(int j=2*i;j<=10000000;j+=i)is_prime[j]=false;
    }
    if(p==100000)return;
  }
  //cout<<prime[10000-1]<<endl;
}
  
ll prime_sum(int n){
  ll sum=0;
  for(int i=0;i<n;i++){
    //cout<<prime[i]<<" ";
    sum+=prime[i];
  }
  //cout<<endl;
  return sum;
}

int main(){
  int n;
  sieve();
  while(cin>>n){
    if(n==0)break;
    cout<<prime_sum(n)<<endl;
  }
}

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

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

  ll N; cin >> N;
  ll A; cin >> A;
  vector<ll> X(N); for (ll i = 0; i < N; i++) cin >> X[i];

  vector<vector<vector<ll>>> dp(N + 1, vector<vector<ll>>(N + 1, vector<ll>(2510, 0)));

  dp[0][0][0] = 1;
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      for (ll k = 0; k < 2510; k++) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j + 1][k + X[i]] += dp[i][j][k];
      }
    }
  }

  ll ans = 0;
  for (ll j = 0; j < N; j++) {
    ans += dp[N][j + 1][A * (j + 1)];
  }
  cout << ans << '\n';
  return 0;
}

Measured memory_bytes: 52352
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=1e9+7;
const int N = 2e5+5;


vector<ll>ans,veca,vecb;
vector<int>adj[N];
vector<double>aka;
vector<pair<int,double>>dp[N];
vector<bool>vis;


void init(int n){
    ans.clear();veca.clear();aka.clear();vis.clear();ans.resize(n);
    vecb.clear(),vecb.resize(n);veca.resize(n);vis.resize(n);
    for (int i = 0; i <n; i++) {
        vis[i]=0;
    }
    for (int i = 0; i<=n; i++) {
        adj[i].clear();
    }
    for (int i = 0; i <=n; i++) {
        dp[i].clear();
    }
    for (int i = 0; i < n; i++) {
        ans[i]=0;
    }
}

int check(int arr[]){
    sort(arr,arr+3);
    if(arr[0]==arr[1]||arr[1]==arr[2]||arr[2]==arr[0]) return 0;
    int res=0;
    if(arr[0]+arr[1]>arr[2]) res=1;
    return res;
    
}
bool valid(int a,int b,int c){
    if((a<b)&&(b<c)) return 1;
    return 0;
}
void solve(){
    int n,flag=0;
    cin>>n;
    init(n);
    for (int i = 0; i <n; i++) {
        cin>>veca[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                if(valid(i,j,k)){
                    int arr[3];
                    memset(arr,0,sizeof(arr));
                    arr[0]=veca[i];arr[1]=veca[j];arr[2]=veca[k];
                    flag+=check(arr);
                }
            }
        }
    }
    cout<<flag<<"\n";
    
}

int main(){
   
   
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
        
        
    return 0;
}

Measured memory_bytes: 13032
//A.cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
  int A,B,X;
  cin >> A >> B >> X;
  if(A<=X&&X<=A+B) printf("YES\n");
  else printf("NO\n");
  return 0;
}


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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int H,W;
int C[10][10];
signed main(){
    cin>>H>>W;
    rep(i,10)rep(j,10)cin>>C[i][j];

    rep(k,10)rep(i,10)rep(j,10)chmin(C[i][j],C[i][k]+C[k][j]);

    int ans=0;
    rep(i,H)rep(j,W){
        int a;cin>>a;
        if(a==-1)continue;
        ans+=C[a][1];
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
main()
{
    int a,b,c,d;
    cin>>a>>b>>c;
    if(b>=c)
        cout<<"delicious"<<endl;
    else{

        d=c-b;
        if(d<=a)
            cout<<"safe"<<endl;
        else
            cout<<"dangerous"<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <iostream>

#include <cstring>
#include <vector>
#include <string>
using namespace std;
const int inf = 0x3f3f3f3f;


int n, k , c;




int main(){
    //freopen("in.txt", "r", stdin);
    cin>>n>>k>>c;
    string str;
    cin>>str;
    int last = -inf;
    vector<int> L;
    vector<int> R;

    for(int i = 0; i< n; i++){
        if(str[i]=='x') continue;
        if(last + c < i) L.push_back(i), last = i;
    }
    last = inf;
    for(int i = n-1; i>=0; i--){
        if(str[i]=='x') continue;
        if(last - c > i) R.push_back(i), last = i;
    }

    int len = max(L.size(), R.size());
    vector<int>ans;
    for(int i = 0 ; i< len; ++i){
        if(L[i]==R[len -1  - i]) ans.push_back(L[i]);
    }

    if(ans.size()<=k)
    for(int i = 0; i< ans.size() ; i++){
        cout<< ans[i]+1<<"\n";
    }






    return 0;
}

Measured memory_bytes: 4216
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main(){
	string p;
	 while(1){
	 	cin >> p;
	 	if(p[0]=='0'){
	 		return 0;
	 	}
	 	p += '0';
	 	int a=0,b=0;
	 	for(int t=1;p[t]!='0';t++){
	 		if(p[t]=='A'){
	 			a++;
	 		}
	 		if(p[t]=='B'){
	 			b++;
	 		}
	 	}
	 	if((a==10)&&(b<10)){
	 		a++;
	 	}
	 	if((a<10)&&(b==10)){
	 		b++;
	 	}
	 	if((a>9)&&(b>a)){
	 		b++;
	 	}
	 	if((a>b)&&(b>9)){
	 		a++;
	 	}
	 	printf("%d %d\n",a,b);
	 }
}

Measured memory_bytes: 1176
#include<bits/stdc++.h>
#define ll long long int 
#define w(x) int x; cin>>x; while(x--)
#define pb push_back
#define mp make_pair
#define mod 1000000007
using namespace std;
int main(){
#ifndef ONLINE_JUDGE 
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll n;
	cin>>n;
	string s,t;
	cin>>s>>t;
	for(ll i=0; i<n; i++)
		cout<<s[i]<<t[i];
}

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

using namespace std;

int main()
{
    string s;
    int k;
    cin >> s >> k;

    vector<string> vs;
    for (int i = 0; i < s.size(); ++i) {
        for (int j = 1; i + j <= s.size() && j <= k; ++j) {
            vs.push_back(s.substr(i, j));
        }
    }

    sort(vs.begin(), vs.end());
    vs.erase(unique(vs.begin(), vs.end()), vs.end());
    cout << vs[k-1] << endl;
}

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

int main()
{	
	double a, b, c, d, x, y, s = 0;
	char t;

	cin >> a >> t >> b >> c >> t >> d;

	while (cin >> x >> t >> y) {
		s += abs((c - a) * (y - b) - (d - b) * (x - a)) * 0.5;
		c = x;
		d = y;
	}

	cout << fixed << setprecision(6) << s << endl;

	return 0;
}



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

int main(void){
    int a,b;
    cin >> a >> b;
    int count = 0;
    rep(i,b){
        string s = to_string(i);
        string k = s;        
        reverse(s.begin(),s.end());
        if(s == k) count++;
    }
    cout << count << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;i<(n);i++)
#define REP(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define INF LLONG_MAX - 100
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define yorn(f) puts((f)?"Yes":"No")
#define YORN(f) puts((f)?"YES":"NO")
#define lower_bound lowb
#define upper_bound upb
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> pque;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int lcm(int a,int b){return a*b/gcd(a,b);};
int mod(int a,int b){return (a+b-1)/b;};
int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
template<typename A, size_t N, typename T>
void Fill(A(&array)[N],const T &val){std::fill((T*)array,(T*)(array+N),val);}
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;};
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;};



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

	int n, m;
	cin >> n >> m;
	int a[n], b[m];
	rep(i, n) cin >> a[i];
	rep(i, m) cin >> b[i];

	int ans = 0;

	rep(i, m) {
		int e = i;
		rep(j, n) {
			if(e < m && a[j] == b[e]) e++;
		}
		chmax(ans, e - i);
	}

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



Measured memory_bytes: 3244
#include <iostream>
#include <cmath>
#include <string.h> 
#include <vector>
#include <bits/stdc++.h> 
#include <iomanip>
#include<algorithm> 
#include <string> 
using namespace std;

int main(){
    int n;
    cin>>n;
    long long int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    long long int table[n][n];
    for (int gap = 0; gap < n; ++gap)
    {
        for (int i = 0, j = gap; j < n; ++i, ++j)
        {
            long long int x = ((i+2) <= j) ? table[i+2][j] : 0;
            long long int y = ((i+1) <= (j-1)) ? table[i+1][j-1] : 0;
            long long int z = (i <= (j-2))? table[i][j-2]: 0;
 
            table[i][j] = max(a[i] + min(x, y), a[j] + min(y, z));
        }
    }
   long long int S=0;
   for(int i=0;i<n;i++){ S += a[i];}
   long long int X= table[0][n-1];
   long long int Y = S-X;
   cout<<X-Y;
}


Measured memory_bytes: 70528
#include<bits/stdc++.h>
using namespace std;
int main(){
	double a,b,c,d,e,f;
	while(scanf("%lf %lf %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f)!=EOF){
		double x,y;
		y=(double)(c*d/a-f)/(b*d/a-e);
		x=(double)(c-b*y)/a;
		printf("%.3lf %.3lf\n",x,y);
	}
}

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

using namespace std;

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

const char sp = ' ';
const char cmm = ',';
const int MOD = 1000000007;

using ll = long long;

ll mod(ll a, ll b){return (a%b+b)%b;}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void Judge(bool b){b?Yes():No();}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void JUDGE(bool b){b?YES():NO();}
ll powMod(ll b, ll e, ll m){ll r=1;while(e>0){if(e&1)r=(r%m)*(b%m)%m;b=(b%m)*(b%m)%m;e>>=1;}return r;}
double distance(ll x1, ll y1, ll x2, ll y2){return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));}

template<typename T>
void ppp(T n){cout << n << endl;}

ll dp[101][4][4][4];

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    fill_n(***dp, 101*4*4*4, 0LL);

    dp[0][3][3][3] = 1LL;
    rep(i, n){
        rep(c1, 4){
            rep(c2, 4){
                rep(c3, 4){
                    rep(c4, 4){
                        // aaaa c1 c2 c3 i
                        if(c2 == 0 and c3 == 1 and c4 == 2) continue;
                        if(c2 == 0 and c3 == 2 and c4 == 1) continue;
                        if(c2 == 1 and c3 == 0 and c4 == 2) continue;
                        if(c1 == 0 and c2 == 1 and c4 == 2) continue;
                        if(c1 == 0 and c3 == 1 and c4 == 2) continue;
                        dp[i+1][c2][c3][c4] += dp[i][c1][c2][c3];
                        dp[i+1][c2][c3][c4] %= MOD;
                    }
                }
            }
        }
    }


    ll ans = 0LL;
    rep(i, 4){
        rep(j, 4){
            rep(k, 4){
                ans += dp[n][i][j][k];
                ans %= MOD;
            }
        }
    }

    ppp(ans);


    return 0;
}


Measured memory_bytes: 256
N, Y = map(int,input().split())
for i in range(N + 1):
  for j in range(N - i + 1):
    k = N - i - j
    if(k >= 0):
      if(10000*i + 5000*j + 1000*k == Y):
        print(i, j, k)
        exit()
print(-1, -1, -1)

Measured memory_bytes: 3060
import java.io.*;
import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = Integer.parseInt(sc.next());
    int B = Integer.parseInt(sc.next());
    
    if (A > 9 || B > 9) {
      System.out.println(-1);
    } else {
      System.out.println(A*B);
    }
  }
}

Measured memory_bytes: 22736
// フェルマーの小定理
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	m = 998244353
)

func invMod(a int) int {
	b, c, n := 1, 0, m
	for n != 0 {
		q := a / n
		r := a % n
		a, b, c, n = n, c, b-q*c, r
	}
	if a != 1 {
		panic("Not invertible")
	}
	if b < 0 {
		b += m
	}
	return b
}

func mpow(x, y int) int {
	result := 1
	for y != 0 {
		if y&1 == 1 {
			result *= x
			result %= m
		}
		x *= x
		x %= m
		y >>= 1
	}
	return result
}

var (
	fac []int
)

func initFactorialTable(n int) {
	fac = make([]int, n+1)
	fac[0] = 1
	for i := 0; i < n; i++ {
		fac[i+1] = fac[i] * (i + 1) % m
	}
}

func mcomb(n, k int) int {
	if n == 0 && k == 0 {
		return 1
	}
	if n < k || k < 0 {
		return 0
	}
	return (fac[n] * invMod(fac[n-k]) % m) * invMod(fac[k]) % m
}

func main() {
	defer flush()

	N := readInt()
	M := readInt()
	K := readInt()

	initFactorialTable(N)

	result := 0
	for i := 0; i < K+1; i++ {
		t := M * mcomb(N-1, i)
		t %= m
		t *= mpow(M-1, N-1-i)
		t %= m
		result += t
		result %= m
	}
	println(result)
}

const (
	ioBufferSize = 1 * 1024 * 1024 // 1 MB
)

var stdinScanner = func() *bufio.Scanner {
	result := bufio.NewScanner(os.Stdin)
	result.Buffer(make([]byte, ioBufferSize), ioBufferSize)
	result.Split(bufio.ScanWords)
	return result
}()

func readString() string {
	stdinScanner.Scan()
	return stdinScanner.Text()
}

func readInt() int {
	result, err := strconv.Atoi(readString())
	if err != nil {
		panic(err)
	}
	return result
}

var stdoutWriter = bufio.NewWriter(os.Stdout)

func flush() {
	stdoutWriter.Flush()
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdoutWriter, args...)
}


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

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

    vector<int> L(N+1);

    for(int i=1; i<=N; i++){
        cin >> L.at(i);
    }

    int count = 1;

    int D = 0;

    for(int t=1; t<=N; t++){
        D = D + L.at(t);
        if(D<=X){
            count = count +1;
        }
    }

    cout << count << endl;

    return 0;


}

Measured memory_bytes: 256
// https://atcoder.jp/contests/abc065/tasks/abc065_b
#include <bits/stdc++.h>
using namespace std;

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

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

  vector<int> a(N + 1);
  rep(i, N) cin >> a[i + 1];

  vector<bool> visit(N + 1);
  int k = 1;
  for (int cnt = 1; visit[k] == false; cnt++) {
    // cout << k << " -> " << a[k] << endl;

    visit[k] = true;
    k = a[k];

    if (k == 2) {
      cout << cnt << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Measured memory_bytes: 640
import sys

n = int(input())


#for i in reversed(range(1, n+1)):
#    if i % 2 == 0:
#        print(i)
#        sys.exit()

ans = 1
while ans < n:
    if ans * 2 <= n:    
        ans *= 2
    else:
        print(ans)
        sys.exit()

print(ans)

Measured memory_bytes: 3060
N = int(input())
As = list(map(int, input().split()))

res = [0] * N
for idx, ai in zip(range(N-1, -1, -1), As[::-1]):
    oe = sum([res[i] for i in range(idx, N, idx+1)]) % 2#
    res[idx] = ai ^ oe

print(sum(res))
idxs = []
for i, val in enumerate(res):
    if val:
        idxs += [i + 1]
print(*idxs)

Measured memory_bytes: 14120
/***************************************************************
     File Name: D.cpp
     Created Time: 六  5/ 5 21:06:56 2018
 **************************************************************/
#define MEM0(x) memset(x,0,sizeof(x))
#define MEM1(x) memset(x,-1,sizeof(x))
#define MEMX(x) memset(x,0x3f,sizeof(x))
#include <iostream>
#include <cstring>
#include <vector>

using namespace std;
const int MAXN = 55580;
bool flag[MAXN];
int primes[MAXN / 3], pi;
void GetPrime_1() {
  int i, j;
  pi = 0;
  memset(flag, false, sizeof(flag));
  for (i = 2; i < MAXN; i++)
    if (!flag[i])
    {
      primes[pi++] = i;
      for (j = i; j < MAXN; j += i)
        flag[j] = true;
    }
}
int main() {
  GetPrime_1();
  // cout << pi << endl;
  // for (int i = 1; i < pi; ++i) cout << primes[i] << ' ';
  // cout << endl;
  vector<int> ans;
  for (int i = 0; i < pi; ++i) {
    if (primes[i] % 5 == 1) ans.push_back(primes[i]);
  }
  // for (auto it : ans) cout << it << ' ';
  // cout << endl;
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; ++i) cout << ans[i] << ' ';
    cout << endl;
  }
}


Measured memory_bytes: 384
#define _CRT_SECURE_NO_WARNINGS

#define N (1<<17)
#define MODULO (1000000007)
#define HA1 (2420609)
#define HA2 (19260817)

#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <vector>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;

inline int GetRand()
{
	int Res = rand()*rand()*rand();
	Res -= rand()*rand();
	Res += rand();
	return Res;
}

int n;
long long a[N];
long long Ans;

int main()
{
	scanf("%d", &n);
	for (int i = 0;i < n;i++)
	{
		scanf("%lld", &a[i]);
	}
	while (true)
	{
		long long Res = 0LL;
		for (int i = 0;i < n;i++)
		{
			long long Tmp = a[i] / (long long)n;
			Res += Tmp;
			a[i] %= (long long)n;
			a[i] -= Tmp;
		}
		if (Res == 0LL)
		{
			break;
		}
		Ans += Res;
		for (int i = 0;i < n;i++)
		{
			a[i] += Res;
		}
	}
	printf("%lld\n", Ans);
	//system("pause");
	return 0;
}



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

int main(void){
  string s = "1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51";
  int K;
  cin >> K;
  int count = 1;
  for(int i = 0; i < s.length(); i++){
    if(s[i] == ',') count++;
    else if(s[i] == ' ') continue;
    else if(count == K) cout << s[i];
  }
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define pii pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define traceloop(x,a) {for(ll i=0;i<x;i++) cerr<<a[i]<<" "; cerr<<endl;}
ll ab(ll x) {return x>0LL?x:-x;}

#define MOD 1000000007

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n,m;
	cin>>n>>m;
	char in[n+1][m+1];
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			cin>>in[i][j];
	int dp[n+1][m+1];
	memset(dp,0,sizeof(dp));
	dp[1][1]=1;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
		{	if(in[i][j]=='#' || (i==1 && j==1))
				continue;

			if(in[i-1][j]=='.')
				dp[i][j]= (dp[i][j] + dp[i-1][j])%MOD;
			if(in[i][j-1]=='.')
				dp[i][j]= (dp[i][j] + dp[i][j-1])%MOD;
		}
	// for(int i=1; i<=n; i++)
	// {
	// 	for(int j=1; j<=m; j++)
	// 		cout<<dp[i][j]<<" ";
	// 	cout<<endl;
	// }
	cout<<dp[n][m];
	return 0;
}
	

Measured memory_bytes: 5120
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):
    if N % K == 0:
        print(0)
        return
    print(min(N, abs((N % K) - K)))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for(ll i = (k); i < (n); i++)
#define FORe(i, k, n) for(ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for(ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for(ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

int main(void){
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> mp;
  REP(i, n) cin >> a[i], mp[a[i]]++;
  ll k = mp.size();
  if(k%2){
    cout << k << endl;
  }else {
    cout << k-1 << endl;
  }
  return 0;
}

Measured memory_bytes: 7296
#include <iostream>

using namespace std;

long long int X, Y;

int main(void) {


	cin >> X >> Y;

	long long int num = X;
	int count = 0;

	for (; num <= Y; num *= 2) {
		count++;
	}

	cout << count << endl;

	//system("pause");
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define fr(i,k,N) for(ll i=k; i<N;i++)
#define rep(i,N) for(ll i=0; i<N;i++)
#define ALL(v) v.begin(),v.end()
#define mp make_pair
using namespace std;
template <class X> void pr(X test){cout<<test<<endl;}
template <class X> void prr(X test){for(auto it:test){cout<<it<<endl;}}
template <class X> void prrr(X test){int f=0;for(auto it:test){cout<<(f++!=0?" ":"")<<it;}cout<<endl;}
template<typename X>
istream&operator>>(istream&i,vector<X>&v){for(X&x:v)i>>x;return i;}

class UF { // union find from https://github.com/kartikkukreja/blog-codes (MIT)
    // [usage]
    // auto uf = new UF(n);
    // uf->merge(l, r); // 0 <= l, r <= n-1
    // ...
public:
    int *id; // inner id
    int cnt; // number of all vartices
    int *sz; // sz[i] means size of i's set
    UF(int N)   {
        cnt = N;
        id = new int[N];
        sz = new int[N];
        for(int i=0; i<N; i++) {
            id[i] = i;
            sz[i] = 1;
        }
    }
    ~UF() {
        delete [] id;
        delete [] sz;
    }
    int find(int p) {
        int root = p;
        while (root != id[root]) root = id[root];
        while (p != root) {
            int newp = id[p];
            id[p] = root;
            p = newp;
        }
        return root;
    }
    void merge(int x, int y) {
        int i = find(x);
        int j = find(y);
        if (i == j) return;
        
        if (sz[i] < sz[j]) {
            id[i] = j;
            sz[j] += sz[i];
        } else {
            id[j] = i;
            sz[i] += sz[j];
        }
        cnt--;
    }
    bool connected(int x, int y) {
        return find(x) == find(y);
    }
    int count() {
        return cnt;
    }
};

// return sorted 約数list 1 <= x <= n
vector<ll> divv(ll n) {
  vector<ll> v;
  for(ll i = 1; i*i <= n; i++) {
    if(n%i == 0) {
      v.push_back(i);
      if(i*i != n) v.push_back(n/i);
    }
  }
  sort(ALL(v));
  return v;
}

int main() {
    int h,w;
    string s;
    cin>>h>>w;
    pr(max(max(h+w,h-w),h*w));
    return 0;
}


Measured memory_bytes: 256
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 解説通りに実装したソースコード
 */
public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int n = scanner.nextInt();
			// aに出現するiの値の個数をc[i]、bに出現するiの値の個数をd[i]
			int[] c = new int[n + 1], d = new int[n + 1];
			Arrays.fill(c, 0);
			Arrays.fill(d, 0);
			IntStream.range(0, n).forEach(i -> c[scanner.nextInt()]++);
			int[] b = IntStream.range(0, n).map(i -> {
				int k = scanner.nextInt();
				d[k]++;
				return k;
			}).toArray();
			// 可能かどうかを判定
			for (int i = 1; i <= n; i++) {
				if (c[i] + d[i] > n) {
					System.out.println("No");
					return;
				}
			}
			// aに出現するi以下の値の個数をc[i]、bに出現するi以下の値の個数をd[i]
			IntStream.rangeClosed(1, n).forEach(i -> {
				c[i] += c[i - 1];
				d[i] += d[i - 1];
			});
			// bをc[i]−d[i−1] の最大値の分だけ右にずらす
			int max = IntStream.rangeClosed(1, n).map(i -> c[i] - d[i - 1]).max().getAsInt();
			int[] result = IntStream.range(0, n).map(i -> b[(i + n - max) % n]).toArray();
			System.out.println("Yes");
			System.out.println(Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
		}
	}
}


Measured memory_bytes: 74136
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
int main()
{
    ll a,b,c;
    scanf("%lld%lld%lld",&a,&b,&c);
    ll h=a/b;
    if(a%b!=0)
        h++;
    printf("%lld\n",h*c);
    return 0;
}


Measured memory_bytes: 3824
#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
static const int MAX = 100000;
static const int NIL = -1;

std::vector<int> G[MAX];
int color[MAX];
int n;

void dfs(int r, int c) {
    std::stack<int> S;
    S.push(r);
    color[r] = c;
    while ( !S.empty() ) {
        int u = S.top(); S.pop();
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if (color[v] == NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assignColor() {
    int id = 1;
    for (int i = 0; i < n; i++) color[i] = NIL;
    for (int j = 0; j < n; j++) {
        if (color[j] == NIL) dfs(j, id++);
    }
}

int main() {
    int m, s, t, q;
    
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &s, &t);
        G[t].push_back(s);
        G[s].push_back(t);
    }
    
    assignColor();
    
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &s, &t);
        if (color[s] == color[t]) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
    
    return 0;
}



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

int main() {
  string s, t;
  cin >> s >> t;
  int a, b;
  cin >> a >> b;
  string u;
  cin >> u;

  if (s == u) {
    a--;
  } else {
    b--;
  }

  cout << a << ' ' << b << endl;
  return 0;
}

Measured memory_bytes: 256
n = int(input())
sAry = []

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i in range(n):
    alphAry = [0] * 26
    s = list(input())
    for alph in s:
        if alph == 'a':
            alphAry[0] += 1
        elif alph == 'b':
            alphAry[1] += 1
        elif alph == 'c':
            alphAry[2] += 1
        elif alph == 'd':
            alphAry[3] += 1
        elif alph == 'e':
            alphAry[4] += 1
        elif alph == 'f':
            alphAry[5] += 1
        elif alph == 'g':
            alphAry[6] += 1
        elif alph == 'h':
            alphAry[7] += 1
        elif alph == 'i':
            alphAry[8] += 1
        elif alph == 'j':
            alphAry[9] += 1
        elif alph == 'k':
            alphAry[10] += 1
        elif alph == 'l':
            alphAry[11] += 1
        elif alph == 'm':
            alphAry[12] += 1
        elif alph == 'n':
            alphAry[13] += 1
        elif alph == 'o':
            alphAry[14] += 1
        elif alph == 'p':
            alphAry[15] += 1
        elif alph == 'q':
            alphAry[16] += 1
        elif alph == 'r':
            alphAry[17] += 1
        elif alph == 's':
            alphAry[18] += 1
        elif alph == 't':
            alphAry[19] += 1
        elif alph == 'u':
            alphAry[20] += 1
        elif alph == 'v':
            alphAry[21] += 1
        elif alph == 'w':
            alphAry[22] += 1
        elif alph == 'x':
            alphAry[23] += 1
        elif alph == 'y':
            alphAry[24] += 1
        elif alph == 'z':
            alphAry[25] += 1
    sAry.append(alphAry)

#print(sAry)

resultAry = [51] * 26
for s in sAry:
    for i in range(26):
        if s[i] < resultAry[i]:
            resultAry[i] = s[i]

#print(resultAry)

answer = ''
for i in range(26):
    for j in range(resultAry[i]):
        answer += alphabet[i]

print(answer)

Measured memory_bytes: 3192
#include <stdio.h>
int main(void){
  int max = 0, max2 = -1, n, a[200000] = {0};
  scanf("%d", &n);getchar();
  for(int i = 0; i < n; i++){
    scanf("%d", &a[i]);getchar();
    if(a[i] > max){
      max2 = max;
      max = a[i];
    }else if(max2 < a[i]){
      max2 = a[i];
    }
  }
  
  for(int i = 0; i < n; i++){
    if(a[i] == max){
      printf("%d\n", max2);
    }else{
      printf("%d\n", max);
    }
  }
  return 0;
}

Measured memory_bytes: 2304
#include<bits/stdc++.h>
using namespace std;
#define fo(i,a,n) for(i=a;i<n;i++)
#define ll long long
#define deb(x) cout << #x << "=" << x << endl
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define sortrev(x) sort(all(x),greater<int>())
#define pi 3.1415926535897932384626

typedef pair<int,int> pii;
typedef pair<ll, ll> pl;
typedef vector<int>	vi;
typedef vector<ll>	vl;
typedef vector<pii>	vpii;
typedef vector<pl>	vpl;
typedef vector<vi>	vvi;
typedef vector<vl>	vvl;
typedef vector<double> vd;


const int mod = 100000007;
const int inf = 100000005;
const int N = 200005, M = N;
int i,j,n,m;

ll cnt , idx;
vi temp;

void solve() {
   ll n;
   cin>>n;

   unordered_map<ll,ll>mp;
   ll x , ans = 0;
   fo(i,0,n) cin>>x , mp[x]++ , ans+=x;
   ll q,b,c;
   cin>>q;

   while(q--) {
     cin>>b>>c;
     ans-=(mp[b] * b);
     ans+=(mp[b] * c);
     mp[c]+=mp[b];
     mp[b] = 0;
     cout<<ans<<"\n";
   }
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int t = 1;
    //cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}




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

int h[200005] = {0};
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    sort(h, h+n);
    for (int i = 0; i < n-1; i++) {
        
        if (h[i] == h[i + 1]) {
            cout << "NO" << endl;
            return 0;
        }

    }
    
    cout << "YES" << endl;;
}


Measured memory_bytes: 1024
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
    do {                                                                       \
    } while (0)
#define dump(x)                                                                \
    do {                                                                       \
    } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

template <class T> inline T sqr(T x) { return x * x; }

void solve() {
    int n, M;
    cin >> n >> M;
    vector<vector<pair<int, int>>> es(n);
    int l, r, d;
    REP(i, 0, M) {
        cin >> l >> r >> d;
        --l;
        --r;
        es[l].pb(r, d);
        es[r].pb(l, -d);
    }
    map<int, int> m;
    REP(i, 0, n) {
        if (m.count(i) == 0) {
            m[i] = 0;
        }
        deque<int> q;
        for (auto &p : es[i]) {
            int dst = p.first;
            int d = p.second;
            if (m.count(dst) != 0) {
                if (m[dst] != m[i] + d) {
                    cout << "No" << endl;
                    return;
                }
                continue;
            }
            m[dst] = m[i] + d;
            q.push_back(dst);
        }
        while (!q.empty()) {
            int idx = q.front();
            q.pop_front();
            for (auto &p : es[idx]) {
                int dst = p.first;
                int d = p.second;
                if (m.count(dst) != 0) {
                    if (m[dst] != m[idx] + d) {
                        cout << "No" << endl;
                        return;
                    }
                    continue;
                }
                m[dst] = m[idx] + d;
                q.push_back(dst);
            }
        }
    }
    cout << "Yes" << endl;
}

signed main() {
    solve();

    return 0;
}

Measured memory_bytes: 13428
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var a, b int
	fmt.Scan(&a, &b)

	ans := 0
	for i := a; i <= b; i++ {
		s := strconv.Itoa(i)
		if s[0] == s[4] && s[1] == s[3] {
			ans++
		}
	}
	fmt.Println(ans)
}


Measured memory_bytes: 1024
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

int main(){

	int l;
	char s[1001];
	int d[4] = {0,0,0,0};

	for (int i = 0; i < 1001; i++)s[i] = 0;
	
	cin >> s;
	l = strlen(s);
	
	for (int i = 0; i < l; i++){
		if (s[i] == 'S')d[0]++;
		else if (s[i] == 'N')d[1]++;
		else if (s[i] == 'E')d[2]++;
		else if (s[i] == 'W')d[3]++;
	}
	//printf("%d %d %d %d\n", d[0], d[1], d[2], d[3]);
	if (((d[0] > 0 && d[1] > 0) || (d[0] == 0 && d[1] == 0)) && ((d[2] > 0 && d[3] > 0) || (d[2] == 0 && d[3] == 0)))printf("Yes\n");
	else printf("No\n");

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <map>
const int MOD = 1e9+7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

ll mod2(ll x){
	ll coun = 0;
	while (x % 2 == 0){
		x /= 2;
		coun++;
	}
	return coun;
}


int main(){
	int n;
	cin >> n;
	vector<ll> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];

	ll ans = 0;
	for (int i = 0; i < n; i++){
		ans += mod2(a[i]);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3652
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
#define all(x) (x).begin(), (x).end()  // sortなどの引数を省略
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define max3(x, y, z) max(x, max(y, z))

#ifdef _MSC_FULL_VER  //デバッグ出力
#define dout cout
#define debug() if (true)
#define check(x) std::cout << "★" << #x << "の値:" << (x) << endl
#define pass(x) std::cout << "☆" << x << endl
#else
#define dout \
  if (false) cout
#define debug() if (false)
#define check(x) \
  if (false) cout << "★" << #x << "の値:" << (x) << endl
#define pass(x) \
  if (false) cout << "☆" << x << endl
#endif
using namespace std;
int main() {
  double a,b,c;
  cin >> a >> b >> c;
  c /= max(a,b);
  cout << ceil(c);
}
/*
//よく使うやつ
int N;
cin >> N;
vector<int> list(N);
for (int i = 0; i < N; i++) {
cin >> list[i];
}
// for文
for (int i = 0; i < N; i++) {
}
//配列
vector<int> list;
list.at(i)
vector<vector<int>> list(10, vector<int>(10));
sort(all(list));
sort(all(list), greater<int>());
//配列の内容表示
dout << "====listの内容====\n";
for (int i = 0; i < N; i++) dout << list[i] << endl;
dout << "====ここまで====\n";
*/


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP0(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define RREP0(i, n) for (int i = (n)-1; i >= 0; --i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define RREP1(i, n) for (int i = (n); i >= 1; --i)

typedef long long LL;
typedef pair<int, int> pii;

const int INTINF = int(1e9) + 1;
const LL LLINF = LL(1e18) + 1;
long double eps = 1.0E-14;
#define pow10(n) int(1e##n + n)

void solve()
{
    stack<char> stk;
    string S;
    cin >> S;

    REP0 (i, S.length())
    {
        if (!stk.empty() and stk.top() == 'S' and S[i] == 'T')
        {
            stk.pop();
            continue;
        }

        stk.push(S[i]);
    }

    cout << stk.size() << endl;
}

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(20);
    solve();
    return 0;
}


Measured memory_bytes: 720