akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<class T>
struct CumulativeSum {
	vector<T> v;
	CumulativeSum(const vector<T> &vv) : v(vv.size()+1) {
		T sum = 0;
		for ( size_t i = 0; i < vv.size(); i++ ){
			sum += vv[i];
			v[i+1] = sum;
		}
	}
	
	// [l,r) の合計
	T range(int l, int r){
		return v[r]-v[l];
	}
};


ll x, y, a, b, c;


int main(){
	cin >> x >> y >> a >> b >> c;
	
	vector<ll> p(a);
	vector<ll> q(b);
	vector<ll> r(c);
	
	for ( int i = 0; i < a; i++ ){
		cin >> p[i];
	}
	for ( int i = 0; i < b; i++ ){
		cin >> q[i];
	}
	for ( int i = 0; i < c; i++ ){
		cin >> r[i];
	}
	
	auto f = [](ll l, ll r){return r<l;};
	sort(p.begin(),p.end(),f);
	sort(q.begin(),q.end(),f);
	sort(r.begin(),r.end(),f);
	
	// pの大きい方からX個、q の大きい方からY個を混ぜてソートする
	vector<ll> v(x+y);
	copy(p.begin(),p.begin()+x,v.begin());
	copy(q.begin(),q.begin()+y,v.begin()+x);
	sort(v.begin(),v.end(),f);
	
	
	CumulativeSum<ll> cv(v);
	CumulativeSum<ll> cr(r);
	
	ll score = 0;
	for ( int i = 0; i <= c && i <= x+y; i++ ){
		score = max( score, cv.range(0,x+y-i) + cr.range(0,i) );
	}
	
	
	cout << score << endl;
	
	return 0;
}




Measured memory_bytes: 9224
#include <bits/stdc++.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
using namespace std;
typedef double  dbl;
typedef long long int lld;
#define PI 3.14159265358979323
#define foi(i,n) for(i = 0; i < n; i+=1)
#define fod(i,n) for(i=n-1; i >=0; i-=1)
#define MAX std::numeric_limits<lld>::max()
#define MIN std::numeric_limits<lld>::min()
#define FastIO  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int main ()
{
      FastIO
      lld n, i;
      cin >> n;
      string s;
      string t;
      cin >> s;
      cin >> t;
      lld j, k;
      j = 0;
      k = 0;
      foi(i, (2*n))
      {
            if (i % 2 == 0)
            {
                  cout << s[j];
                  j++;
            }
            else
            {
                  cout << t[k];
                  k++;
            }
      }
      return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
string S;int N,cnt;
int main(){
	cin>>N>>S;
	for (int i=0;i<1000;i++){
		int c[3]={i/100,(i/10)%10,i%10};
		int f=0;
		for(int j=0;j<N;j++){
			if(S[j]==('0'+c[f]))f++;
			if(f==3)break;
		}
		if(f==3)cnt++;
	}
	cout<<cnt<<endl;
	return 0;
}


Measured memory_bytes: 3636
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

X = sum([(2*(k+1) - n - 1) * x[k] for k in range(n)])
Y = sum([(2*(k+1) - m - 1) * y[k] for k in range(m)])

print(X * Y % (10**9+7))


Measured memory_bytes: 18576
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int x, y, z;
    cin >> x >> y >> z;
    cout << z << " " << x << " " << y << '\n';
    return 0;
}


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

stack <int> s1; 



int main(){
	int a,b;
	string s;
	
	while(cin>>s){
		if(s=="+"){
			a=s1.top();
			s1.pop();
			b=s1.top();
			s1.pop();
			s1.push(b+a);
		}
		else if(s=="-"){
			a=s1.top();
			s1.pop();
			b=s1.top();
			s1.pop();
			s1.push(b-a);
		}
		else if(s=="*"){
			a=s1.top();
			s1.pop();
			b=s1.top();
			s1.pop();
			s1.push(b*a);
		} 
		else{
			s1.push(atoi(s.c_str()));
		}
	}
	cout<<s1.top()<<endl;
} 

Measured memory_bytes: 3208
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

map<ll, ll> prime_factorization(ll n){
	map<ll, ll> ans;
	ll a = 2;
	while(n >= a*a){
		if(n % a == 0){
			ans[a]++;
			n /= a;
		}else{
			a++;
		}
	}
	ans[n]++;
	
	return ans;
}

int main(){
	ll a, b;
	cin >> a >> b;
	if(a > b) swap(a, b);
	set<ll> cd;
	for(ll i = 1; i * i <= a; i++){
		if(a % i != 0) continue;
		if(b % i == 0){
			cd.insert(i);
		}
		ll j = a / i;
		if(b % j == 0){
			cd.insert(j);
		}
	}
	// for(auto i : cd){
	// 	cerr << i << " ";
	// }
	// cerr << endl;

	vector<ll> cdv(cd.size());
	int id_cdv = 0;
	for(auto i : cd){
		cdv.at(id_cdv) = i;
		id_cdv++;
	}
	vector<bool> use(cdv.size(), true);
	int ans = 0;
	rep(i, cdv.size()){
		if(use.at(i) == true){
			ans++;
			ll now = cdv.at(i);
			if(now == 1) continue;
			rep2(j, i+1, use.size()){
				if(cdv.at(j) % now == 0) use.at(j) = false;
			}
		}
	}
	cout << ans << endl;

	return 0;
}

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

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;

const ll e5 = 1 << 21;
const ll mod = 1000000007;
const ll e3 = 1 << 13;
const ll INF = 1ll << 62;

string w;
string y;
ll ans;
ll b[e5],a[e5];
ll z[e5];

bool is_good(ll from = 0,ll to = w.size()){
  ll w_size = to-from;
  for(ll i = 1;i < w_size;i++){
    if(w_size%i != 0) continue;
    bool f = false;
    y = w.substr(from,i);
    for(ll j = from;j < to;j+=i){
      if(y != w.substr(j,i)){
        f = true;
        break;
      }
    }
    if(!f) return false;
  }
  return true;

}

void solve(){
  ll i = 1,j = 0;
  while(i < w.size()){
    while(i+j < w.size() && w[j] == w[i+j]) j++;
    z[i] = j;
    if(j == 0){
      i++;
      continue;
    }
    ll k = 1;
    while(i+k < w.size() && z[k]+k < j){
      z[i+k] = z[k];
      k++;
    }
    i += k;
    j -= k;
  }
  for(ll i = 0;i < w.size();i++) cerr << z[i] << " ";
  cerr << endl;
  for(ll i = 1;i < w.size();i++){
    for(ll k = 2;z[i] >= (k-1)*i;k++){
      b[i*k-1] = 1;
    }
  }
}

int main(){
  cin >> w;
  // if(w.size() > 4000) return 1;
  for(ll i = 1;i < w.size();i++){
    if(w[0] != w[i]) goto _a;
  }
  cout << w.size() << endl << 1 << endl;
  return 0;
_a:
  if(is_good()){
    cout << 1 << endl << 1 << endl;
    return 0;
  }

  solve();
  swap(a,b);
  reverse(w.begin(),w.end());
  solve();
  reverse(b,b+w.size());


  // for(ll i = 0;i <= w.size();i++) cerr << a[i] << " ";
  // cerr << endl;
  // for(ll i = 0;i <= w.size();i++) cerr << b[i] << " " ;
  // cerr << endl;
  for(ll i = 0;i < w.size()-1;i++){
    if(a[i] <= 0 && b[i+1] <= 0) ans++;
  }
  cout << 2 << endl << ans << endl;


}


Measured memory_bytes: 38148
import java.util.*;
public class Main{
  public static void main(String[] args){
	Scanner sc=new Scanner(System.in);
    int ab=sc.nextInt();
    int bc=sc.nextInt();
    int ca=sc.nextInt();
    if(ab>bc&&ab>ca){
      System.out.println(bc*ca/2);
    }else if(bc>ab&&bc>ca){
      System.out.println(ab*ca/2);
    }else{
      System.out.println(bc*ab/2);
    }

	}
}

Measured memory_bytes: 24148
parseInt(x::String) = parse(Int, x)
parseInt(x::SubString{String}) = parse(Int, x)
parseInt(x::Char) = parse(Int, x)
parseInt(x::Float64) = trunc(Int, x)
parseFloat(x::String) = parse(Float64, x)
parseFloat(x::Int) = trunc(Float64, x)
parseFloat(x::SubString{String}) = parse(Float64, x)
parseFloat(x::Char) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n=parseInt(readline())
    a=parseMap(split(readline()))
    color=[0 for i in 1:9]
    for i in a
        for j in 1:8
            if 400*(j-1)<=i<400*j
                color[j]+=1
            end
        end
        if 3200<=i
            color[9]+=1
        end
    end
    MIN=max(count(i->i>0,color[1:8]),1)
    MAX=count(i->i>0,color[1:8])+color[9]
    println("$MIN $MAX")
end
main()

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

using namespace std;
using ll = int64_t;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using mii = map<int, int>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using tpi = tuple<int, int, int>;

using vqi = vector<queue<int>>;
using vpi = vector<pii>;
using bs = bitset<60>;
using vbi = vector<bs>;

ll gcd(ll a, ll b) {
    if (a < b) {
        swap(a, b);
    }

    /* ユークリッドの互除法 */
    ll r = a % b;
    while (r != 0) {
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

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

constexpr int64_t __pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
    if (p == 0) {
        return 1;
    }
    if (p % 2 == 0) {
        int64_t t = __pow_mod(n, p / 2, mod);
        return (t * t) % mod;
    }
    return (n * (__pow_mod(n, p - 1, mod) % mod)) % mod;
}

constexpr int64_t pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
    int64_t res = __pow_mod(n, p, mod);
    if (res == 0) {
        res = mod;
    }
    return res;
}

void solve(long long N, long long K, std::vector<long long> p) {
    ll sum = 0;
    for (ll i = 0; i < K; i++) {
        sum += (p[i] + 1);
    }

    ll ans = sum;
    for (ll i = K; i < N; i++) {
        sum += p[i] - p[i - K];
        ans = max(ans, sum);
    }

    if (ans % 2 == 0) {
        cout << ans / 2 << endl;
    } else {
        cout << ans / 2 << ".5" << endl;
    }
}

// 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)
int main() {
    long long N;
    std::cin >> N;
    long long K;
    std::cin >> K;
    std::vector<long long> p(N);
    for (int i = 0; i < N; i++) {
        std::cin >> p[i];
    }
    solve(N, K, std::move(p));
    return 0;
}


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

int main() {

    ll n , m ,k ;
    cin >> n >> m >> k ;

    ll i ;
    vector<ll> a(n) , b(m) ;
    for(i=0;i<n;i++) cin >> a[i] ;
    for(i=0;i<m;i++) cin >> b[i] ;

    ll ans = INT_MAX ;

    while(k--) {
        ll A,B,c ;
        cin >> A >> B >> c ;
        A-- , B-- ;

        ans= min(ans,a[A]+b[B]-c) ;
        ans=max(0LL,ans) ;

    }
    sort(a.begin(),a.end()) ;
    sort(b.begin(),b.end()) ;
    cout << min(a[0]+b[0],ans) << endl ;

    return 0 ;
}


Measured memory_bytes: 1792
n, q = map(int, input().split())
s = input()

l = []
r = []
for i in range(q):
    li, ri = map(int, input().split())
    l.append(li)
    r.append(ri)

acs = [0]
count = 0
idx = 1
while True:
    if idx == n:
        break
    if s[idx-1] == 'A' and s[idx] == 'C':
        count += 1
    acs.append(count)
    idx += 1

for i in range(q):
    print(acs[r[i]-1] - acs[l[i]-1])

Measured memory_bytes: 14168
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <math.h>
#include <complex>
#include <cassert>
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define case(i) cout<<"Case #"<<i<<": "
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pr;
typedef complex<double> com;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const ll INF = 4e18;


int main(){
	int n; cin >> n;
	vector<ll> a(n);
	rep(i, 0, n) cin >> a[i];
	ll ans = 0;
	vector<ll> sum(n+1,0), xo(n+1,0);
	rep(i, 0, n) {
		sum[i + 1] = a[i] + sum[i];
		xo[i + 1] = a[i] ^ xo[i];
	}
	int idx = 1;
	rep(i, 0, n) {
		while (sum[idx] - sum[i] == (xo[idx] ^ xo[i])&&idx<n+1) {
			idx++;
		}
		ans += idx - i - 1;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 4992
#include <stdio.h>
#include <stdlib.h>

void display(int *A, int n, int m, int *G, int cnt) {
	int i;
	printf("%d\n", m);
	for (i = 0; i < m - 1; i++) {
		printf("%d ", G[i]);
	}
	printf("%d\n", G[m - 1]);
	printf("%d\n", cnt);
	for (i = 0; i < n; i++) {
		printf("%d\n", A[i]);
	}
}

int insertionSort(int *A, int n, int g) {
	int i, j, v;
	int cnt = 0;
	for (i = g; i < n; i++) {
		v = A[i];
		j = i - g;
		while (j >= 0 && A[j] > v) {
			A[j + g] = A[j];
			j = j - g;
			cnt++;
		}
		A[j + g] = v;
	}
	return cnt;
}

void shellSort(int *A, int n) {
	int cnt, m, i;
	int *G;

	cnt = 0;
	m = n / 2;
	if (m < 1) m = 1;
	if (m > 100) m = 100;
	G = (int *)malloc(sizeof(int)*m);
	G[0] = n - 1;
	for (i = 1; i < m - 1; i++) {
		G[i] = G[i - 1] / 2;
	}
	G[m - 1] = 1;

	for (i = 0; i < m; i++) {
		cnt += insertionSort(A, n, G[i]);
	}

	display(A, n, m, G, cnt);
}

int main() {
	int n;
	int *A;
	int i;

	scanf("%d", &n);
	A = (int *)malloc(sizeof(int) * n);
	for (i = 0; i < n; i++) {
		scanf("%d", &A[i]);
	}

	shellSort(A, n);

	return 0;
}


Measured memory_bytes: 5708
#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
int main() { 
    int n, x;
    while(cin>>n) {
        int maxv = INT_MIN;
        int minv = INT_MAX;
        while (n--) {
            cin>>x;
            maxv = max(maxv, x - minv);
            minv = min(minv, x);
        }
        cout<<maxv<<endl;
    }

}

Measured memory_bytes: 3096
#include <iostream>
#include <vector>
#include <iomanip>
#include <stack>
#include <array>
#include <deque>
#include <forward_list>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <iterator>
#include <string>
#include <cstring>
#include <cstdlib>
//#include <fstream>
#include <complex>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n;i>0;i--)
#define arep(a,i,n) for(int i=(a);i<(n);i++)
#define SORT(v) sort((v).begin(),(v).end())
#define SORTR(v) sort((v).begin(),(v).end(),greater<int>())
const ll INF = (1LL << 40);
typedef pair<int, int> P;
const int MOD = 1000000007;

int main() {

	string s;
	cin >> s;
	int n = s.size();

	int no = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] != s[n - 1 - i]) {
			no = 1;
			break;
		}
	}

	if (no == 1) {
		cout << "No" << endl;
		return 0;
	}

	no = 0;
	for (int i = 0; i < (n - 1) / 2; i++) {
		if (s[i] != s[(n - 1) / 2 - 1 - i]) {
			no = 1;
		}
	}

	if (no == 1) {
		cout << "No" << endl;
		return 0;
	}

	no = 0;
	for (int i = (n+3)/2-1; i < n; i++) {
		if (s[i] != s[n - 1 - i]) {
			no = 1;
		}
	}

	if (no == 1) {
		cout << "No" << endl;
		return 0;
	}
	else {
		cout << "Yes" << endl;
	}

	return 0;

}

Measured memory_bytes: 256
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
    piece = i+1
    temp = 0
    hoge = []
    #hogehoge = []
    key = 0
    for j in range(N):
        if j+1 > K:
            key = 1
            break
        piece = P[piece-1]
        temp += C[piece-1]
        #hogehoge.append(C[piece-1])
        hoge.append(temp)
        if piece == i+1:
            break
    if key == 1:
        ans = max(ans,max(hoge))
    elif hoge[-1]<=0:
        ans = max(ans,max(hoge))
    else:
        d = K//len(hoge)
        r = K%len(hoge)
        if r != 0:
            peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r]))
        else:
            peak = hoge[-1]*(d-1)+max(hoge)
        ans = max(ans,peak)
print(ans)


Measured memory_bytes: 81472
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()

def make_divisors(n): # nの約数を列挙、1を除く
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors[1:]

d1 = make_divisors(N-1)
answer = len(d1)
d2 = make_divisors(N)
for x in d2:
    M = N
    while M%x == 0:
        M //= x
    if M%x == 1:
        answer += 1

print(answer)


Measured memory_bytes: 3188
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int a,b,c,sum;
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        sum=a+b+c;
        if(sum>=22)
        {
            cout<<"bust"<<endl;
        }
        else
        {
            cout<<"win"<<endl;
        }
    }
    return 0;
}


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

void solve(long long X, long long A)
{
    if (X < A)
    {
        cout << 0 << endl;
    }
    else
    {
        cout << 10 << endl;
    }
}

// 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)
int main()
{
    long long X;
    scanf("%lld", &X);
    long long A;
    scanf("%lld", &A);
    solve(X, A);
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

void drawFrame( int H, int W)
{
	int i,j;
	
	for( i=0; i<H; i++) {
		for( j=0; j<W; j++) {
			if( 0==i || H-1==i || 0==j || W-1==j ) {
				printf( "#");
			} else {
				printf( ".");
			}
		}
		printf( "\n");
	}
	printf( "\n");
	return;
}

int main( void ) 
{
	int H,W;

	for( ; ; ) {
		scanf( "%d%d", &H, &W ); 
		if( 0==H && 0==W ) { break; }

		drawFrame( H, W );
	}

	return 0;
}

Measured memory_bytes: 572
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }


int main() {
    vector<int> cnt(3);
    int k;
    cin >> cnt >> k;
    int sum = 0;
    rep(i, 3) {
        int use = min(k, cnt[i]);
        sum += use * (i == 0 ? 1 : i == 1 ? 0 : - 1);
        k -= use;
    }
    cout << sum << endl;
}

Measured memory_bytes: 3640
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main() {
	string S;
	cin >> S;
	int n = 0;
	for (auto c : S) {
		if (n % 2 == 0 && c == 'A') ++n;
		else if (n % 2 == 1 && c == 'Z') ++n;
	}
	int m = n / 2;
	if (m == 0) {
		cout << -1 << endl;
	} else {
		for (int i = 0; i < m; ++i) cout << "AZ";
		cout << endl;
	}
}


Measured memory_bytes: 3176
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, x, n) for(int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 50;
const int ININF = 1000000;
using pint = pair<int, int>;
using Edge = pair<long long, pint>; //<cost,<u,v>>の形
vector<Edge> edges;

int N, M, L;
vector<vector<long long>> d;
vector<vector<long long>> d2;

int main() {
    cin >> N >> M >> L;
    d.assign(N, vector<long long>(N));
    d2.assign(N, vector<long long>(N));
    rep(i, 0, N) {
        rep(j, 0, N) {
            if(i == j) {
                d[i][j] = 0LL;
                d2[i][j] = 0LL;
            } else {
                d[i][j] = INF;
                d2[i][j] = INF;
            }
        }
    }
    rep(i, 0, M) {
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        d[a][b] = c;
        d[b][a] = c;
    }
    int Q;
    cin >> Q;
    vector<int> s(Q);
    vector<int> t(Q);
    rep(i, 0, Q) {
        cin >> s[i] >> t[i];
        s[i]--;
        t[i]--;
    }
    rep(k, 0, N) {
        rep(i, 0, N) {
            rep(j, 0, N) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
        }
    }
    long long res = INF;
    // cout << "d" << endl;
    // rep(i, 0, N) {
    //     rep(j, 0, N) { cout << d[i][j] << " "; }
    //     cout << endl;
    // }
    rep(i, 0, N) {
        rep(j, 0, N) {
            if(i != j && L >= d[i][j]) {
                d2[i][j] = 1;
            }
        }
    }
    rep(k, 0, N) {
        rep(i, 0, N) {
            rep(j, 0, N) { d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]); }
        }
    }
    // cout << "d2" << endl;
    // rep(i, 0, N) {
    //     rep(j, 0, N) { cout << d2[i][j] << " "; }
    //     cout << endl;
    // }

    rep(i, 0, Q) {

        if(d2[s[i]][t[i]] != INF) {
            cout << d2[s[i]][t[i]] - 1 << endl;
        } else {
            cout << -1 << endl;
        }
    }
}


Measured memory_bytes: 2688
#include <iostream>
#include <queue>
#include <map>
using namespace std;

struct state{
    int tx;
    int ty;
    int kx;
    int ky;
    int tim;
};

int main()
{
    while(true){
        int X, Y;
        cin >> X >> Y;
        if(X==0&&Y==0)
            break;

        int d[52][52];
        for(int y=0; y<=Y+1; y++)
            for(int x=0; x<=X+1; x++)
                d[y][x] = 1;

        struct state now;
        cin >> now.tx >> now.ty;
        cin >> now.kx >> now.ky;
        now.tim = 0;

        queue<struct state> que;
        que.push(now);

        map<int, bool> memo;

        for(int y=1; y<=Y; y++)
            for(int x=1; x<=X; x++)
                cin >> d[y][x];

        while(true){
            if(que.empty()){
                cout << "NA" << endl;
                break;
            }

            now = que.front();
            que.pop();

            int tk = ((now.tx*100+now.ty)*100+now.kx)*100+now.ky;
            if(memo.find(tk)!=memo.end())
                continue;
            else
                memo[tk] = true;

            if(now.tim>=100){
                cout << "NA" << endl;
                break;
            }

            if(now.tx==now.kx&&now.ty==now.ky){
                cout << now.tim << endl;
                break;
            }

            struct state nxt = now;
            if(d[nxt.ty-1][nxt.tx]==0)
                nxt.ty--;
            if(d[nxt.ky+1][nxt.kx]==0)
                nxt.ky++;
            nxt.tim++;
            que.push(nxt);

            nxt = now;
            if(d[nxt.ty+1][nxt.tx]==0)
                nxt.ty++;
            if(d[nxt.ky-1][nxt.kx]==0)
                nxt.ky--;
            nxt.tim++;
            que.push(nxt);

            nxt = now;
            if(d[nxt.ty][nxt.tx-1]==0)
                nxt.tx--;
            if(d[nxt.ky][nxt.kx+1]==0)
                nxt.kx++;
            nxt.tim++;
            que.push(nxt);

            nxt = now;
            if(d[nxt.ty][nxt.tx+1]==0)
                nxt.tx++;
            if(d[nxt.ky][nxt.kx-1]==0)
                nxt.kx--;
            nxt.tim++;
            que.push(nxt);
        }
    }

    return 0;
}



Measured memory_bytes: 11680
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)

Measured memory_bytes: 89192
#include <bits/stdc++.h>
using namespace std;
int gcd(int m, int n) { return n == 0 ? m : gcd(n, m % n); }
int main()
{
    int N, K; cin >> N >> K;
    vector<int> A(N); for (int& a : A) cin >> a;
    int g = accumulate(begin(A)+1, end(A), A[0], gcd);
    int M = *max_element(begin(A), end(A));
    if (K <= M && K % g == 0) {
        cout << "POSSIBLE" << endl;
    } else {
        cout << "IMPOSSIBLE" << endl;
    }
    return 0;
}


Measured memory_bytes: 640
import numpy as np
n, m = map(int, input().split())
f = lambda:list(map(int, input().split()))
arr = np.zeros((n,2),'i8')
#print(arr)
Q = m
price = 0

for i in range(n):
  p, q = f()
  #print(p,q)
  arr[i] = (p,q)
arr = sorted(arr.tolist())
#print(arr)
while Q>0:
  for i in range(n):
    p = arr[i][0]
    q = arr[i][1]
    if q > Q:
      q = Q
      price += p*q
      #print(price)
      Q -= q
    else:
      price += p*q
      #print(price)
      Q -= q
  
print(price)

Measured memory_bytes: 31448
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
 int main()
{	
lli n,i,j,flag=0;
string s;
cin >> s;
for(i=0;i<s.size()-1;i++){
	if(s[i]!=s[i+1]){
		flag=1;
		break;
	}
}
if(flag==0)cout << "No" <<'\n';
else cout << "Yes"<< '\n';

return 0;}


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

using namespace std;
int main()
{
    long long int n,k,i,ans=0,dis=0;
    cin>>n>>k;
    long long int a[k+1];
    for(i=0;i<k;i++)
    {
        cin>>a[i];
    }

    for(i=0;i<k-1;i++)
    {
        if((a[i+1]-a[i])>dis)
        {
            dis=a[i+1]-a[i];
            }
    if(i==k-2 && dis<(n-a[k-1]+a[0]))
        dis=n-a[k-1]+a[0];
    }
     cout<<n-dis<<endl;
    return 0;
}

Measured memory_bytes: 1792
//ヘッダー
#include<bits/stdc++.h>
using namespace std;

//型定義
typedef long long ll;

//定数
const int INF=1e+9;
const int MOD=1e+9+7;

//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)

// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値

int main(){
    int L;
    cin>>L;
    vector<tuple<int,int,int>> G(60);

    int tmp=L;
    int cnt=-1;
    while(tmp!=0){
        tmp/=2;
        cnt++;
    }
    
    REP(i,cnt){
        G[2*i]=make_tuple(i+1,i+2,pow(2,i));
        G[2*i+1]=make_tuple(i+1,i+2,0);
    }
    int N,M;
    int tmp2;
    N=cnt+1;
    L-=pow(2,cnt);
    tmp2=pow(2,cnt);
    int cnt2=0;
    int cnt3=2*cnt;
    int cnt_before=cnt;
    
    while(L!=0){
        tmp=L;
        cnt2=-1;
        while(tmp!=0){
            tmp/=2;
            cnt2++;
        }
        G[cnt3]=make_tuple(cnt2+1,cnt_before+1,tmp2);
        cnt++;
        cnt3++;
        L-=pow(2,cnt2);
        tmp2+=pow(2,cnt2);
    }
    

    REP(i,60){
        int a,b,c;
        a=get<0>(G[i]);
        b=get<1>(G[i]);
        c=get<2>(G[i]);
        if(a==0){
            M=i;
            break;
        }
    }
    cout<<N<<" "<<M<<endl;
    REP(i,M){
        int a,b,c;
        a=get<0>(G[i]);
        b=get<1>(G[i]);
        c=get<2>(G[i]);
        cout<<a<<" "<<b<<" "<<c<<endl;
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ios::sync_with_stdio(false);
	int n,k;
	cin>>n>>k;
	int a[n+1],i;
	for (i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	int c[n+1];
	sort(a+1,a+n+1);
	for (i=1;i<=n;i++)
	{
		c[i]=a[i+1]-a[i];
		//cout<<c[i]<<" ";
	}
	//cout<<endl;
	ll cnt,ans;
	cnt=0;
	ans=1000000000000000;
	for (i=1;i<=n-k+1;i++)
	{
		cnt=0;
		for (int j=0;j<=k-2;j++)
		{
			cnt+=c[i+j];
		}
		if (cnt<ans)
		{
			ans=cnt;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define rep(i, n) for(int (i)=0 ;i <(n); ++(i))
#define REP(i,m,n) for(int (i) = (m); (i) <= (n); ++(i))
#define yorn(f) puts((f)?"Yes":"No")
#define YORN(f) puts((f)? "YES":"NO")
typedef long long ll;
using namespace std;

int f(int n) {
  if(n%2 ==0) return n/2;
  else return 3*n+1;
}
int main() {
  int s;
  cin >> s;
  unordered_map<int, int> mp;
  bool flag = true;
  int cnt = 0;
  while(flag) {
    mp[s]++;
    for(auto p : mp) {
      if(p.second == 2) {
        flag = false;
        break;
      }
    }
    s = f(s);
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxN = 1e5 + 5;
ll p[maxN], s, N;

ll myabs(ll num) {
	return (num < 0) ? -num : num;
}
int main() {
	scanf("%lld", &N);
	for(int i = 1; i <= N; ++i) scanf("%lld", &p[i]);
	for(int i = 1; i <= N + 1; ++i) s += myabs(p[i - 1] - p[i]); 
	for(int i = 1; i <= N; ++i)
	printf("%lld\n", s - myabs(p[i] - p[i - 1]) - myabs(p[i] - p[i + 1]) + myabs(p[i - 1] - p[i + 1]));
	return 0;
}

Measured memory_bytes: 2048
from itertools import accumulate
N, Q = map(int, input().split())
S = input()


A = [0, 0]
for i in range(1, N):
    if S[i - 1] + S[i] == 'AC':
        A.append(1)
    else:
        A.append(0)


A_acc = list(accumulate(A))
for q in range(Q):
    l, r = map(int, input().split())
    ans = A_acc[r] - A_acc[l]
    print(ans)


Measured memory_bytes: 8580
(defvar N)
(setf N (read))

(defun range (m n &optional l)
  (if (< n m) l
      (range m (1- n) (cons n l)) ))

(defun sum_digit (N)
  (reduce #'(lambda (sum s)
                    (+ sum (digit-char-p s)))
          (write-to-string N) :initial-value 0))

(let ((a (read))
      (b (read)))
     (defun in[a.b] (x) (and (<= a x) (<= x b))))

(princ
 (reduce #'+
         (remove-if-not
          (lambda (x) (in[a.b] (sum_digit x)))
         (range 1 N))
 :initial-value 0 ))

Measured memory_bytes: 10600
/******************************************
*******************************************
*******************************************
	PREPARED BY - SOMESH AWASTHI 
	CHANDIGARH UNIVERSITY
*******************************************
*******************************************
*******************************************/

#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;

int main()
{
	io
	#ifdef SOMU
		clock_t startTime = clock();
		freopen("input.txt","r",stdin);
	#endif

	int n, m;
	cin >> n >> m;
	char mat[n][m];
	fr(i, 0, n) {
		fr(j, 0, m) {
			cin >> mat[i][j];
		}
	}
	ll dp[n][m];
	memset(dp, 0, sizeof(dp));
	dp[0][0] = 1;
	fr(i, 0, n) {
		fr(j, 0, m) {
			if(mat[i][j] == '.') {
				if(i > 0) {
					dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
				}
				if(j > 0) {
					dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
				}
			}
			// cout << dp[i][j] << " ";
		}
		// cout << '\n';
	}
	cout << dp[n - 1][m - 1];

	#ifdef SOMU
		cout << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
	#endif
	return 0;
}

Measured memory_bytes: 9088
#include<stdio.h>
#define N 100
#define step1 1
#define step2 2
#define step3 3

void dfs(void);
void dfs_visit(int);

int M[N][N],n;
int step[N],fvisit[N],lvisit[N],tt;
int main(){

  int i,j,nood,next,num;

  scanf("%d",&n);

  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      M[i][j] = 0;
    }
  }

  for(i=0;i<n;i++){
    scanf("%d %d",&nood, &next);
    nood--;
    for(j=0;j<next;j++){
      scanf("%d",&num);
      num--;
      M[nood][num]=1;
    }
  }

  dfs();
  
  return 0;
}

void dfs(){
  int u;
  
  for(u=0;u<n;u++) step[u]=step1;
  tt=0;

  for(u=0;u<n;u++){
    if(step[u] == step1) dfs_visit(u);
  }

  for(u=0;u<n;u++){
    printf("%d %d %d\n",u+1,fvisit[u],lvisit[u]);
  }
}

void dfs_visit(int u){
  int v;
  step[u]=step2;
  fvisit[u]=++tt;
  for(v=0;v<n;v++){
    if(M[u][v]==0) continue;
    if(step[v]==step1){
      dfs_visit(v);
    }
  }
  step[u]=step3;
  lvisit[u]=++tt;
}



Measured memory_bytes: 2052
using Yorukatsu049.Questions;
using Yorukatsu049.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Yorukatsu049.Questions
{
    /// <summary>
    /// ABC081 B
    /// </summary>
    public class QuestionA : AtCoderQuestionBase
    {
        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var n = inputStream.ReadInt();
            var a = inputStream.ReadIntArray();
            yield return a.Min(CountDiv2);
        }

        int CountDiv2(int n)
        {
            var count = 0;
            while (n % 2 == 0)
            {
                n /= 2;
                count++;
            }
            return count;
        }
    }
}


namespace Yorukatsu049
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionA();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
        }
    }
}

#region Base Classes

namespace Yorukatsu049.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }

}

#endregion

#region Extensions

namespace Yorukatsu049.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);
    }
}

#endregion

Measured memory_bytes: 13396
#include <iostream>

int main(){
  int N;
  while(std::cin>>N&&N>0){
    int sum=0,max=-1,min=1001;
    for(int i=0;i<N;++i){
      int s;
      std::cin>>s;
      sum += s;
      max = std::max(s,max);
      min = std::min(s,min);
    }
    std::cout<<(sum-max-min)/(N-2)<<std::endl;
  }
  return 0;
}

Measured memory_bytes: 2972
#include<bits/stdc++.h>
#include<set>
#include<map>
#include<bitset>
#define rep(i,a,b)  for(i=a;i<b;i++)
#define Sort(a)	sort(a.begin(),a.end())
#define rev(a)	reverse(a.begin(),a.end())
#define all(a)	(a).begin(),(a).end()
#define vll vector<long long int>
#define lli long long int
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define db double
#define M 1000000007
#define pi 3.1415926535897932384626433832795028841971
#define INF 1e18

#define MAX 200005

using namespace std;
lli min(lli a,lli b){return a<b?a:b;}
lli min(lli a,lli b,lli c){a=min(a,b);a=min(a,c);return a;}
lli max(lli a,lli b){return a>b?a:b;}
lli max(lli a,lli b,lli c){a=max(a,b);a=max(a,c);return a;}
lli dsum(lli n){lli ans=0;while(n){ans+=(n%10);n/=10;}return ans;}
lli power(lli a,lli b){lli res=1;while(b){if(b&1) res=res*a;a=a*a;b>>=1;}return res;}
lli power(lli a,lli b,lli m){if(b==0)	return 1;if(b==1)	return a%m;lli res=1;a=a%m;while(b){if(b&1){res=(res*a)%m;}a=((a%m)*(a%m))%m;b>>=1;}return res;}
lli Ceil(lli a,lli b){lli res=a/b;if(a%b!=0)	res++;return res;}

template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

lli dxx[]={1,1,0,-1,-1,-1,0,1};
lli dyy[]={0,1,1,1,0,-1,-1,-1};
lli dx[]={1,0,-1,0};
lli dy[]={0,1,0,-1};

struct pll{
	lli x,y;
};
bool comp(pll a,pll b){
	if(a.x==b.x)	return a.y<b.y;
	else	return a.x<b.x;
}
bool ok(lli x,lli n)
{
	lli c=0;
	vll v;
	while(x>0){
		c++;
		v.pb(x%10);
		x/=10;
	}
	Sort(v);
	if(c==n){
		if(v[0]==0&&v[v.size()-1]==9)	return true;
		return false;
	}
	if(v[v.size()-1]==9)	return true;
	return false;
	
}
void solve(lli the)
{
	char ch;
	db aa,bb,cc,dd,xx;
	string s1,s2,s3,str;
    lli i,j,k,a,b,c,d,n,m,l,r,x,y,z,low,high,mid,sum=0,ans=0,temp,t;
    c=0;
    cin >> n;
    ans=power(10,n,M);
    ans%=M;
    ans+=power(8,n,M);
    ans%=M;
    ans-=power(9,n,M);
    ans%=M;
    ans+=M;ans%=M;
    ans-=power(9,n,M);
    ans%=M;
    ans+=M;ans%=M;
    cout << ans;
    
    
}















int main()
{
	long long int t=1,c=1;
	// scanf("%lld",&t);
	while(t--){
		solve(c);
		c++;
	}
	return 0;
}
/* UJJWAL KESHRI */

Measured memory_bytes: 3560
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <regex>
#include <iomanip>
#include <climits>
#include <utility>
#include <queue>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind
{
public:
    UnionFind(int N) : par(N)
    {
        for (ll i = 0; i < N; i++)
        {
            par[i] = i;
        }
    }

    int root(int x)
    {
        if (par[x] == x)
            return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry)
            return;
        par[rx] = ry;
    }

    bool same(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

private:
    vector<int> par;
};

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

ll fact(ll n)
{
    if (n == 0)
    {
        return 1;
    }
    if (n == 1)
    {
        return 1;
    }
    return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k)
{
    if (k == 0)
    {
        return 1;
    }
    if (k == 1)
    {
        return n;
    }

    if (k % 2 == 0)
    {
        ll tmp = pow_fast(n, k / 2LL);
        return tmp * tmp % MOD;
    }
    else
    {
        ll tmp = pow_fast(n, k / 2LL);
        tmp *= tmp;
        tmp %= MOD;
        return tmp * n % MOD;
    }
}

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

struct Dish
{
    ll time;
    ll taste;
};

int main()
{
    ll N;
    cin >> N;
    map<ll, ll> mp;
    ll sum = 0;

    for (ll i = 0; i < N; i++)
    {
        ll ai;
        cin >> ai;
        mp[ai] += 1;
        sum += ai;
    }
    
    ll Q;
    cin >> Q;
    
    for (ll i = 0; i < Q; i++)
    {
        ll bi, ci;
        cin >> bi >> ci;
        mp[ci] = mp[bi] + mp[ci];
        sum += (ci - bi) * mp[bi];
        mp[bi] = 0;
        cout << sum << endl;
    }
}

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

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))

#define INF 2e9
#define INF_LL 1LL<<60
#define ll long long
#define MAX_V
const double PI=3.14159265358979323846;

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int i=0;i<(n-1);i++) cout << (vec)[i] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << endl;}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain; 

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

    if(n == 1 && m == 1) {
        ans = 1;
    } else if (n == 1) {
        ans = m-2; 
    } else if (m == 1) {
        ans = n-2; 
    } else {
        ans = (m-2) * (n-2);
    }

    cout << ans << endl;

    return 0;
}
 

Measured memory_bytes: 256
#include <stdio.h>

int main(void) {
    int a, b, c, d, n;
    while (scanf("%d", &n) != EOF) {
        int count=0;
        for (a = 0; a <= 9; a++) {
            for (b = 0; b <= 9; b++) {
                for (c = 0; c <= 9; c++) {
                    for (d = 0; d <= 9; d++) {
                        if (a + b + c + d == n) count++;
                    }
                }
            }
        }
        printf("%d\n", count);
    }
    return 0;
}


Measured memory_bytes: 2048
#include<iostream>
#include<numeric>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,from,to) for(int i=from;i<to;i++)
#define repd(i,from,till) for(int i=from;i>=till;i--)
#define waste ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define inf 1e9+1
#define mod 1e9+7
#define inf1 1e18+1
#define pie 3.14159265358979323846
#define N 100005
using namespace std;

void solve(){
	string s;
	cin>>s;
	char p=s[0],q;
	int cnt=1,cnt1=1,cnt2=0;
	rep(i,1,4){
		if(s[i]==p){
			cnt1++;
		}
		else{
			if(cnt2==0||(s[i]==q&&cnt2==1))
			cnt2++;
		}
		if(s[i]!=p&&cnt2==1){
			q=s[i];
			cnt++;
		}
	}
	if(cnt==2&&cnt1==2&&cnt2==2){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
}
int main()
{
	waste;
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


Measured memory_bytes: 256
a, b = map(int, input().split())
if a == 1:
    if b != 1:
        print("Alice")
        exit()
    else:
        print("Draw")
        exit()
elif b == 1:
    if a != 1:
        print("Bob")
        exit()
    else:
        print("Draw")
        exit()
 
if  a < b:
    print("Bob")
elif a > b:
    print("Alice")
else:
    print("Draw")

Measured memory_bytes: 9140
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;

void main() {
    int n, m;
    int[] a;

    scan(n, m);
    a = readln.split.to!(int[]);

    int t = 2*(n - 1 - m);

    if (t > n) {
        writeln("Impossible");
        return;
    }

    import std.typecons : Tuple;
    alias Pair = Tuple!(int, "val", int, "idx");

    auto arr = new Pair[](n);

    foreach (i ; 0 .. n) {
        arr[i] = Pair(a[i], i);
    }

    arr.sort();

    auto idx = new int[](n);

    foreach (i, ari ; arr) {
        idx[ari.idx] = i.to!int;
    }

    auto uf = UnionFind(n);

    foreach (i ; 0 .. m) {
        int xi, yi;
        scan(xi, yi);
        uf.unite(idx[xi], idx[yi]);
    }

    debug {
        foreach (i; 0 .. n) {
            writefln("node:%d -> root:%d", i, uf.findRoot(i));
        }
    }

    auto r = new int[](n);
    r[] = n + 1;

    foreach_reverse (i ; 0 .. n) {
        r[uf.findRoot(i)] = i;
    }

    debug {
        writeln(r);
    }

    auto b = new bool[](n);

    foreach (i ; 0 .. n) {
        if (t <= 0) break;
        if (r[i] < n) {
            b[r[i]] = 1;
            t--;
        }
    }

    debug {
        writefln("%(%b %)",b);
    }

    foreach (i ; 0 .. n) {
        if (t <= 0) break;
        if (!b[i]) {
            b[i] = 1;
            t--;
        }
    }

    debug {
        writefln("%(%b %)",b);
    }

    long ans;

    foreach (i ; 0 .. n) {
        if (b[i]) ans += arr[i].val;
    }

    writeln(ans);
}



struct UnionFind {
    private {
        int _size;
        int[] _parent;
    }

    this(int N) 
    in {
        assert(N > 0);
    }
    body {
        _size = N;
        _parent = new int[](_size);
        foreach (i ; 0 .. _size) {
            _parent[i] = i;
        }
    }

    int findRoot(int x)
    in {
        assert(0 <= x && x < _size);
    }
    body {
        if (_parent[x] != x) {
            _parent[x] = findRoot(_parent[x]);
        }

        return _parent[x];
    }

    bool same(int x, int y)
    in {
        assert(0 <= x && x < _size);
        assert(0 <= y && y < _size);
    }
    body {
        return findRoot(x) == findRoot(y);
    }

    void unite(int x, int y)
    in {
        assert(0 <= x && x < _size);
        assert(0 <= y && y < _size);
    }
    body {
        int u = findRoot(x);
        int v = findRoot(y);

        if (u == v) return;

        _parent[v] = u;
    }
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


Measured memory_bytes: 5372
package main

import (
	"fmt"
	"io"
	"math"
	"os"
)

func calcPow(b, t float64) float64 {
	i := 0.0
	for {
		if b * math.Pow(2, i) >= t {
			return i
		}
		i++
	}
}

func solve(w io.Writer, r io.Reader) {
	var N, K float64
	fmt.Scan(&N, &K)

	var min float64
	if N < K {
		min = N
	} else {
		min = K - 1
	}

	ans := 0.0
	for i := 0.0; i < min ; i++ {
		p := calcPow(i+1, K)
		ans += math.Pow(0.5, p)/N
	}
	if min < N {
		ans += (N - min)/N
	}

	fmt.Fprintln(w, ans)
}

func main() {
	solve(os.Stdout, os.Stdin)
}

Measured memory_bytes: 512