akhauriyash/Code-Regression huggingface dataset:
num = [[0,i+1] for i in range(100)]
while True:
    try:
        num[int(input())-1][0] += 1
    except:
        num.sort(reverse = True)
        ans = []
        for i in range(100):
            if num[i][0] == num[0][0]:
                ans.append(num[i][1])
            else:
                ans.sort()
                for z in ans:
                    print(z)
                break
        break


Measured memory_bytes: 5600
#include<cstdio>
int n, w[101000];
long long s, K;
int main(){
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&w[i]);
        s += w[i];
    }
    K = 1ll*n*(n+1)/2;
    if(s%K){
        puts("NO");
        return 0;
    }
    s/=K;
    long long t=0;
    for(i=1;i<=n;i++){
        if((w[i%n+1]%n-w[i]%n+n)%n != s%n){
            puts("NO");
            return 0;
        }
        if(w[i%n+1] - w[i] > s){
            puts("NO\n");
            return 0;
        }
        t += (s - (w[i%n+1] - w[i])) / n;
    }
    if(t!=s) puts("NO");
    else puts("YES");
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define GET_ARRAY_SIZE(a)   (sizeof(a)/sizeof(a[0]))
#define LL long long
#define VI vector<int>
#define VL vector<LL>
#define VII vector< vector<int> >
#define VLL vector< vector<LL> >
#define VS vector<string>
#define PII pair<int,int>
#define PIS pair<int,string>
#define PSI pair<string,int>
#define PLL pair<ll,ll>
LL facctorialMethod(int k);
int main(){
    LL n,m,k,rem,a_num,b_num,count;
    LL a_tmp_count,b_tmp_count,a_tmp_sum,b_tmp_sum;
    cin >> n >> m >> k;
    VL a(n);
    VL b(m);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    a.push_back(10e10);
    for(int i = 0; i < m; i++) {
        cin >> b[i];
    }
    b.push_back(10e10);

    b_tmp_sum = 0;
    rep (i, m) b_tmp_sum += b[i];
    int last = m - 1;
    LL ans = 0;
    a_tmp_sum = 0;
    while (b_tmp_sum > k) {
        b_tmp_sum -= b[last];
        last--;
    }
    ans = last + 1;
    rep (i, n) {
        a_tmp_sum += a[i];
        while (a_tmp_sum + b_tmp_sum > k) {
            if (last == -1) break;
            b_tmp_sum -= b[last];
            last--;
        }
        if (a_tmp_sum + b_tmp_sum <= k) {
            if(ans < (i + 1 + last + 1)) {
                ans = (i + 1 + last + 1);
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 7956
H,W,K=map(int,input().split())
ls=[]
for i in range(H):
    ls.append(input())
ld=[]
#f=0
f=-1
#n=1
ns=0
for i in range(H):
    nstmp=0
    ld.append([])
    for j in range(W):
        if ls[i][j]=="#":
            nstmp+=1
            ns+=1
            if nstmp==2:
                #ld[i].append(j)
                #n+=1
                #ld[i].append((j,ns))
                ld[i].append((j,ns-1))#jより小さいところはns-1区画
                nstmp=1
                #n+=1
    #print("j",j)
    if nstmp==1:
        ld[i].append((j+1,ns))
    if not ld[i]:
        if i==0:
            f=0
        elif not ld[i-1]:
            f+=1
        else:
            ld[i]=ld[i-1]
    #n+=1
#if f==1:
    #ld[0]=ld[1]
if f>-1:
    for i in range(f,-1,-1):
        ld[i]=ld[i+1]
#print(ld)
for i in range(H):
    k=0
    for j in range(W):
        if j<ld[i][k][0]:
            print(ld[i][k][1],end=" ")
        else:
            k+=1
            print(ld[i][k][1],end=" ")
    print("")


Measured memory_bytes: 13704
n,a=map(int, input().split())
x=list(map(int, input().split()))
y=[x[i]-a for i in range(n)]
z=max(max(x),a)
dp=[[0 for _ in range(2*n*z+1)] for _ in range(n+1)]
dp[0][n*z]=1
for i in range(n):
    for j in range(2*n*z+1):
        if 0<=j+y[i]<=2*n*z:
            dp[i+1][j]=dp[i][j]+dp[i][j+y[i]]
        else:
            dp[i+1][j]=dp[i][j]
print(dp[-1][n*z]-1)

Measured memory_bytes: 5744
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define mp make_pair

void print(vector<auto> &a)
{
    for (int i = 0; i<a.size(); i++) cout<<a[i]<<' ';
    cout<<endl;
}

void get(vector<auto> &a)
{
    for (int i = 0; i<a.size(); i++) cin>>a[i];
}

const ll p = 998244353;

int mul(int a, int b) {
    return (1LL * a * b) % p;
}

int add(int a, int b) {
    int s = (a+b);
    s = s%p;
    if (s<0) s+=p; 
    return s;
}

int po(int a, int deg)
{
    if (deg==0) return 1;
    if (deg%2==1) return mul(a, po(a, deg-1));
    int t = po(a, deg/2);
    return mul(t, t);
}


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

    string s, t;
    cin>>s>>t;
    int S = s.length();
    int T = t.length();
    vector<vector<int>> dp(S+1, vector<int>(T+1));
    for (int i = 1; i<=S; i++)
    for (int j = 1; j<=T; j++)
    if (s[i-1]==t[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
    else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
    int i = S;
    int j = T;
    string answer;
    while (i>0&&j>0)
    {
        if (s[i-1]==t[j-1]) {answer = s[i-1] + answer; i--; j--;}
        else if (dp[i-1][j]==dp[i][j]) i--;
        else j--;
    }
    cout<<answer;
}

Measured memory_bytes: 35584
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <numeric>

#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
typedef long long int ll;
typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

int main(){
    int n;
    cin>>n;
    int d,x;
    cin>>d>>x;
    rep(i,0,n){
        int p;
        cin>>p;
        for(int i=0;1+i*p<=d;i++)x++;
    }
   cout<<x<<endl;

    
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i,n) for (int i=0; i<(n); ++i)
#define REPR(i,n,m) for (int i=(n); i>=(m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLD = vector<PLD>;

const int INF = numeric_limits<int>::max();

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

    LL n, k; cin >> n >> k;
    
    LL ans = (n-2) / (k-1) + 1;

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3588
import sys
N, Q = map(int, input().split())
s = input()
TD = [sys.stdin.readline().split() for _ in range(Q)]

def checkl(k):
    np = k
    for t, d in TD:
        if s[np] == t:
            if d == 'L':
                np -= 1
            else:
                np += 1
        if np < 0:
            return False
        np = min(np, N-1)
    return True
def checkr(k):
    np = k
    for t, d in TD:
        if s[np] == t:
            if d == 'L':
                np -= 1
            else:
                np += 1
        if np > N-1:
            return False
        np = max(np, 0)
    return True

ok = N
ng = -1
while abs(ok - ng) > 1:
    med = (ok + ng)//2
    if checkl(med):
        ok = med
    else:
        ng = med

L = ok

ok = -1
ng = N
while abs(ok - ng) > 1:
    med = (ok + ng)//2
    if checkr(med):
        ok = med
    else:
        ng = med

R = ok
print(R - L + 1)

Measured memory_bytes: 81444
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC144A
{
    class Program
    {
        static void Main(string[] args)
        {
            int A,B;
            var word = Console.ReadLine().Split(' ');
            A = int.Parse(word[0]);
            B = int.Parse(word[1]);
            if (A > 9 || B > 9)
                Console.WriteLine(-1);
            else
                Console.WriteLine(A * B);

            Console.ReadKey();
        }
    }
}


Measured memory_bytes: 13140
# from quantize import Decimal
X, K, D = list(map(int, input().split()))

dist= (X/D-K)/2
# print(dist)

if -dist > K :
    i = K
elif dist > 0 :
    i = 0
else :
    i = -round(dist)

# print(i)
print(abs(X + i*D - ((K-i)*D)))

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    for(int T = 0; T < t ; T++){
       ll a;
       for(int i = 0; i < 5; i++){
           cin >> a;
           if(a == 0){
               std::cout << i + 1 << std::endl;
               break;
           }
       }
    }
    return 0;
}

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

int main ()
{
	int N, jam, menit, detik;
	cin >> N;
	
	jam = N/3600;
	N = N - (3600*jam);
	menit = N/60;
	N = N - (60*menit);
	detik = N;
	
	cout << jam << ":" << menit << ":" << detik << endl;
	
	return 0;
}

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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63)

#define	INF (1<<30)
//1,073,741,824
//= 536,870,912 *2

#define	MOD 1000000007

#define	Rep0(i, n)	for (auto i=0; i<n; i++)
#define	Rep1(i, n)	for (auto i=1; i<=n; i++)
#define Sort(P)	sort(P.begin(), P.end())
#define Rev(P)	reverse(P.begin(), P.end())

// Struct for each node
struct Node {
	vector<int> to_node;
	bool fix;
	int cost;
};

void	Dijkstra(vector<Node> &node, const int &StartNode) {
	// Initialize (at(0) will not be used)
	Rep0(i, node.size()) {
		node.at(i).fix=false;
		node.at(i).cost=INF;
	}
	// Start
	// Start Node == Cost=0
	node.at(StartNode).cost=0;
	priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
	q.push({node.at(StartNode).cost, StartNode});
	// For all reachable node from the start node (Unreachabel node not included)
	while (!q.empty()) {
		// Pickup the Node with minimum cost
		int c=q.top().at(0);
		int ii=q.top().at(1);
		q.pop();
		// If fixed, skip
		if(node.at(ii).fix)
			continue;
		// Change it to fixed and search for near-by nodes
		node.at(ii).fix=true;
		Rep0(i, node.at(ii).to_node.size()) {
			int o_node=node.at(ii).to_node.at(i);
			// Update cost
			int o_cost=c+1;
			// Process only when smaller
			if (o_cost>=node.at(o_node).cost)
				continue;
			node.at(o_node).cost=o_cost;
			// Push into queue
			q.push({o_cost, o_node});
		}
	}
	return;
}

int	main() {
	int	N;// Node# from 0 to N-1, 1 to N
	int	B;
	cin >> N >> B;
	// Input, From/To(/Cost=1)
	vector<vector<int>>	bridge(B, vector<int> (2));
	Rep0(i, B)
		Rep0(j, 2)
			cin >> bridge.at(i).at(j);
	vector<Node> node(N+1); // both for 0 to N-1, 1 to N
	// Data for each node
	Rep0(i, B) {
		// both for 0 to N-1, 1 to N
		int F=bridge.at(i).at(0);
		int T=bridge.at(i).at(1);
		node.at(F).to_node.push_back(T);
		node.at(T).to_node.push_back(F);
	}
	// Start
	// Start Node == Cost=0
	int StartNode=1;
	Dijkstra(node, StartNode);
	// Result
	/*
	Rep0(i, node.size())
		// node.at(0).cost==INF
		// both for 0 to N-1, 1 to N
		cout <<"Node at " << i <<": " << node.at(i).cost << endl;
	*/
	vector<int> D(N+1, -1);
	bool OK=true;
	for (auto i=2; i<=N; i++) {
		int C=node.at(i).cost;
		if (C>N) {
			OK=false;
			break;
		}
		Rep0(j, node.at(i).to_node.size()) {
			int o_node=node.at(i).to_node.at(j);
			if (node.at(o_node).cost+1==C) {
				D.at(i)=o_node;
				break;
			}
		}
	}
	if (!OK)
		cout <<"No" <<endl;
	else {
		cout <<"Yes" <<endl;
		for (auto i=2; i<=N; i++)
			cout <<D.at(i) <<endl;
	}
	return 0;
}


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

using namespace std;

#define INF 1 << 29
#define LL long long int

struct triple{
    int a;
    int b;
    int c;
    triple(int i,int j,int l):a(i),b(j),c(l){

    }

    bool operator < (const triple &right) const{
        return b == right.b? c < right.c : b < right.b;
    }
};

struct Union_Find{
    int size;
    vector<int> v;

    Union_Find(int n):size(n){
        v = vector<int>(size,-1);
    }

    int parent(int a){
        if(v[a] < 0){
            return a;
        }

        return v[a] = parent(v[a]);
    }

    bool find(int a,int b){
        if(parent(a) == parent(b)){
            return true;
        }else{
            return false;
        }
    }

    void set(int a,int b){
        int pa = parent(a);
        int pb = parent(b);
        if(pa == pb){
            return;
        }

        if(v[pa] > v[pb]){
            v[pb] = v[pa];
            v[pa] = pb;
        }else{
            v[pa] = v[pb];
            v[pb] = pa;
        }

        return;
    }

};

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

    int n,k,l;

    cin >> n >> k >> l;

    vector<int> p(k),q(k),r(l),s(l);

    for(int i = 0; i < k; i++){
        cin >> p[i];
        cin >> q[i];
    }

    for(int i = 0; i < l; i++){
        cin >> r[i];
        cin >> s[i];
    }

    Union_Find road = Union_Find(n);
    Union_Find rail = Union_Find(n);

    for(int i = 0; i < k; i++){
        road.set(p[i]-1,q[i]-1);
    }

    for(int i = 0; i < l; i++){
        rail.set(r[i]-1,s[i]-1);
    }

    vector<triple> a;

    for(int i = 0; i < n; i++){
        a.push_back(triple(i,road.parent(i),rail.parent(i)));
    }

    sort(a.begin(),a.end());

    vector<int> result(n,0);

    int si = 0;
    int tmpb = a[0].b;
    int tmpc = a[0].c;
    int count = 1;

    for(int i = 1; i < n; i++){
        if(tmpb == a[i].b && tmpc == a[i].c){
            count++;
        }else{
            for(int j = si; j < i; j++){
                result[a[j].a] = count;
            }
            si = i;
            tmpb = a[i].b;
            tmpc = a[i].c;
            count = 1;
        }
    }

    for(int j = si; j < n; j++){
        result[a[j].a] = count;
    }

    for(int i = 0; i < result.size(); i++){
        if(i > 0){
            cout << " ";
        }
        cout << result[i];
    }
    cout << endl;
    
    return 0;
}

Measured memory_bytes: 7284
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>

using namespace std;

#define sz(x) (int) x.size()
#define unique(x) x.erase(unique(x.begin(), x.end()), x.end())
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x)) 
#define y1 aksjdaskdjksjfksdjf
#define left kdnvldvoiwejifejg
#define right lkdsjflksdjfdjfk
#define prev asdasfsadjkjsdfjs

const int MAXN = 105000;
const int INF = (int) 1e9;

int n;
int c[MAXN][3];
int dp[MAXN][3];

int main() { 

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      scanf("%d", &c[i][j]);
    }
  }

  for (int j = 0; j < 3; j++) {
    dp[0][j] = c[0][j];
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = c[i][j];
      for (int k = 0; k < 3; k++) {
        if (j == k) {
          continue;
        }
        dp[i][j] = max(dp[i][j], c[i][j] + dp[i - 1][k]);
      }
    }
  }

  int ans = dp[n - 1][0];
  ans = max(ans, max(dp[n - 1][1], dp[n - 1][2]));

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 2688
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>
#include <stdio.h>
#include <set>
#include <map>
#include <algorithm>
#include <queue>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

const int inf = 1e9 + 7;
const long long INF = 1e18 + 7;
const int maxn = 2e5 + 7;

int a, b;
char c;

int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(0);
	cout.tie(0);
	cin >> a >> c >> b;
	if (c == '+')
		cout << a + b;
	else
		cout << a - b;
	return 0;
} 

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	int N;
	bool flag = true;
	string W[110];
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> W[i];
	}
	for (int j = 0; j < N; j++) {//被りがないか調べる
		for (int k = j + 1; k < N; k++) {
			if (W[j] == W[k]) {
				cout << "No" << endl;
				return 0;
			}
		}
	}
	for (int n = 0; n < N - 1; n++) {
		if (W[n].back() != W[n + 1].front()) {
			flag = false;
		}
	}
	if (flag) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    int n;
    cin>>n;
    ll a[n];
    ll s[n+1];
    s[0]=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
        s[i+1] = s[i]+a[i];
    }
    ll mx[n][n];
    for(int i=0;i<n;i++){
        for(int j=0;j<n-i;j++){
            ll m = 0;
            for(int k=j;k<j+i;k++){
                ll t = mx[j][k]+mx[k+1][j+i]-s[j]+s[j+i+1];
                if(k==j)m = t;
                else m = m<t?m:t;
            }
            mx[j][j+i]=m;
            //cout<<mx[j][i+j]<<"  ";
        }
       // cout<<endl;
    }
    cout<<mx[0][n-1]<<endl;
    return 0;
}


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

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> Que;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve(){
    int a,b;
    cin >> a >> b;
    if(a*b&1)cout << "Odd";
    else cout << "Even";
    cout << endl;
}

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


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

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define MIN(a,b) ((a)>(b)? (b): (a))
#define MAX(a,b) ((a)<(b)? (b): (a))

const long long INF = 1LL << 60;
typedef long long ll;
const long long MOD = 1000000000 + 7;

//Union-Find tree
struct UnionFind{
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
    vector<int> tree_size;  // treeの大きさ

    UnionFind(int N) : par(N),tree_size(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N;  i++) par[i] = i;
        for(int i = 0; i < N;  i++) tree_size[i] = 1;
    }  

    int root(int x) { // データxが属する木の根を再帰で得る : root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    
    void unite(int x, int y) { //xとyの木を併合
        int rx = root(x); //xの木の根をrx
        int ry = root(y); //yの木の根をyx
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)ときはそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時 : xの根rxをyの根ryにつける
        tree_size[ry] += 1; //根をつけた場合はtree sizeを1上げる
        tree_size[rx] = -1; // 同じ木になったため、xの根は-1
        //tree_size[x] = -1;  //子になった木はtree_sizeを持たない
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    int size(int x){ // データxが属する木のサイズを返す
        int rx = root(x); //xの木の根をrx
        return tree_size[rx];
    }
};

#define F first;
#define S second;

int main(){
    int n;
    cin >> n;
    vector<pair<pair<ll,ll>,int>> x;
    vector<pair<pair<ll,ll>,int>> y;
    rep(i,n){
        ll a,b;
        cin >> a >> b;
        x.push_back({{a,b},i});
        y.push_back({{b,a},i});
    }
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    priority_queue<pair<ll,pair<int,int>>> q;
    rep(i,n-1){
        q.push({-MIN(abs(x[i].first.first - x[i+1].first.first),
        abs(x[i].first.second - x[i+1].first.second)),{x[i].second,x[i+1].second}});
        q.push({-MIN(abs(y[i].first.first - y[i+1].first.first),
        abs(y[i].first.second - y[i+1].first.second)),{y[i].second,y[i+1].second}});
    }
    ll ans = 0;
    UnionFind UF(n);
    while(!q.empty()){
        pair<ll,pair<int,int>> nq;
        nq = q.top();
        q.pop();
        ll cost = -nq.first;
        int i = nq.second.first, j = nq.second.second;
        if(UF.same(i,j)) continue;
        ans += cost;
        UF.unite(i,j);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 13860
#include"bits/stdc++.h"
using namespace std;

void Main() {
 int N;
 cin >> N;
 map<int, int> A;
 long long ans = 0;
 for (int i = 0; i < N; i++) {
   int a;
   cin >> a;
   A[a]++;
   ans += a;
 }

 int Q;
 cin >> Q;
 for (int i = 0; i < Q; i++) {
   int b, c;
   cin >> b >> c;
   ans += (c - b) * A[b];
   A[c] += A[b];
   A[b] = 0;
   cout << ans << endl;
 }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}



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

bool search(int* A, int* Sum, int val, int i_max) {
    int idx = lower_bound(A, &A[i_max] + 1, val) - A;
    if (idx != i_max + 1 && A[idx] == val) return true;
    if (idx <= 1) return false;
    idx--;
    if (Sum[idx] < val) return false;
    if (search(A, Sum, val - A[idx], idx - 1)) return true;
    if (search(A, Sum, val, idx - 1)) return true;
    return false;
}

int main(){
    int n;
    cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }

    sort(A, A + n);

    int Sum[n];
    Sum[0] = A[0];
    for (int i = 1; i < n; i++) {
        Sum[i] = Sum[i-1] + A[i];
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int m;
        cin >> m;
        if (search(A, Sum, m, n - 1)) {
            cout << "yes" << endl;
            continue;
        }
        cout << "no" << endl;
    }

    return 0;
}

Measured memory_bytes: 3100
#include<stdio.h>
int main(){
  int a,B[1000],i,v,j,h;
  scanf("%d",&a);
  for(i=0;i<a;i++){
    scanf("%d",&B[i]);
  }
 
   for (i=0;i<a;i++){
     v = B[i];
     j = i - 1;
     while (j >= 0 && B[j] > v){
       B[j+1] = B[j];
       j--;
       B[j+1] = v;
     }
     for(h=0;h<a;h++){
       if(h==a-1)printf("%d",B[h]);
       else printf("%d ",B[h]);
 }
     printf("\n");
}

   }




Measured memory_bytes: 2760
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = (1 << 30 ) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] ={1,0,-1,0} , dy[4] ={0,1,0,-1};

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

struct Timer{
    chrono::system_clock::time_point start, end;
    Timer(){ start = chrono::system_clock::now(); }
    ~Timer(){
        end = chrono::system_clock::now();
        auto msec = chrono::duration_cast<chrono::milliseconds>(end - start).count();
        cerr<<"time : "<<msec<<" ms"<<endl;
    }
};



void solve(){
    char a,b;
    cin>>a>>b;
    if(a>b) cout<<">"<<endl;
    else if(a==b) cout<<"="<<endl;
    else cout<<"<"<<endl;

}


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    // Timer TM;
    
    solve();

    return 0;
}


Measured memory_bytes: 256
/*
If you want to shine like a sun, first burn like a sun.
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds;
using namespace std;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ll long long int
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define v2i vector<vector<int>>
#define v2l vector<vector<ll>>
#define ppi pair<int,int>
#define ppl pair<ll,ll>
#define vpi vector<ppi>
#define vpl vector<ppl>
#define all(x) x.begin(),x.end()
#define ii int,int 
#define ff first    
#define ss second
#define forn(i,a,b) for(int i=a;i<b;i++)
#define forr(i,a,b) for(int i=a;i>=b;i--)
#define forv(i,m) for(auto i : m)
#define imx INT_MAX
#define imn INT_MIN
#define inf 9000000000000000000
#define minf -inf
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sze size()
#define rvs reverse 
#define itr iterator 
#define pre cout<<fixed<<setprecision(10);
#define umap unordered_map
#define uset unordered_set
#define pi 3.141592653589793
#define MAXN 100005
#define test1(x) cout << #x " = " << x << endl;
#define test2(x,y) cout << #x " = " << x << " " << #y " = " << y << endl;
const ll infinity = 9e18;

/*
#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define MODM(a,b,md) ((a%md)*(b%md))%md
#define MODA(a,b,md) ((a%md)+(b%md))%md
#define ull unsigned long long int
*/

/*
bool compare(int a,int b) {return a > b;}
bool compare1(ppi a,ppi b) {return a.ff > b.ff;}
bool compare2(ppi a,ppi b) {return a.ss < b.ss;}
ll Npower(ll a,ll b) {ll product = 1; while(b){if(b&1){product = product*a;}a = a*a;b = b>>1;} return product;}
ll power(ll a,ll b,ll md) {ll product = 1; while(b){if(b&1){product = MODM(product,a,md);}a = MODM(a,a,md);b = b>>1;} return product%md;}
bool isprime(ll n){if(n < 2) return 0; ll i = 2; while(i*i <= n){if(n%i == 0) return 0; i++;} return 1;}
ll GCD(ll a,ll b) {return b==0 ? a:GCD(b,a%b);}
ll LCM(ll a,ll b) {return (a/GCD(a,b))*b; }
*/

bitset<MAXN> b;

void sieve()
{
	b[2] = 1;

	for(int i = 3; i < MAXN; i+=2)
	{
		b[i] = 1;
	}

	for(ll i = 3; i < MAXN ; i+=2)
	{
		if(b[i])
		{
			for(ll j = i*i; j < MAXN; j+=2*i)
			{
				b[j] = 0;
			}
		}
	}
}


int main()
{
    fast
    sieve();
/*
    #ifndef ONLINE_JUDGE
        freopen("input.txt",  "r",  stdin);
        freopen("output.txt", "w", stdout);
    #endif
//*/

    ll x;
    cin>>x;

    for(int i = x; i < MAXN; i++)
    {
    	if(b[i])
    	{
    		cout<<i;
    		return 0;
    	}
    }    












    return 0;
}	

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

int main() {
  ll N, K;
  cin >> N >> K;

  ll dp[100100];
  ll h[100100];

  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < 100100; ++i) {
    dp[i] = 1LL<<60;
  }

  dp[0] = 0;

  for (ll i = 0; i < N - 1; i++) {
    for (ll j = 1; j <= K; j++) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }

  // for (ll i = 0; i < N; i++) {
  //   cout << dp[i] << endl;
  // }

  cout << dp[N - 1] << endl;

  return 0;
}


Measured memory_bytes: 1792
H, W = map(int, input().split())
a = []
for i in range(H):
    list = [i for i in input()]
    a.append(list)
column = [int(i) for i in range(W)]
row = [int(i) for i in range(H)]
for i in range(H):
    for j in range(W):
        if a[i][j] == "#":
            if i in row:
                row.remove(i)
            if j in column:
                column.remove(j)
# print(column)
# print(row)
for i in range(H):
    printed = False
    for j in range(W):
        if i not in row and j not in column:
            print(a[i][j], end="")
            printed = True
    if printed:
        print("")


Measured memory_bytes: 4596
#include "bits/stdc++.h"
using namespace std;

#define ll long long int
#define str string
#define ALL(a)  (a).begin(),(a).end()
#define P pair<ll,ll>

using Graph = vector<vector<int>>;

const int dy[] = { 0, 0, 1, -1 }, dx[] = { 1, -1, 0, 0 };
ll INFL = 1ll << 60, INF = 1 << 30, mod = 1e9 + 7, Min = INFL, Max = -1;
ll a, b, c, n, sum, A[500010], B[500000], rui[500000], dp[5000];
str S, T, U; bool check = true;	vector<ll> v;



int main() {
	//Code Line
	cin >> a >> b;
	if (a < 2);
	else {
		sum += (a * (a - 1)) / 2;
	}
	
	if (b < 2);
	else {
		sum += (b * (b - 1)) / 2;
	}
	cout << sum << endl;



	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#include "unistd.h"
#include "string.h"
#include <math.h>
int ans=0;
int main(int argc, char* argv[]){
  long int a=0,b=0,c=0,e[5];
  double d=0;
  scanf("%lf",&d);
  if (d==1000) {
    printf("1000");
    return 0;
  }
  for (double i = d;; --i) {
    for (double j = 2; j <= d+3; j++) {
      double g=pow(i,1.0/j);
      if (fmod(g,1.0)==0) {
        printf("%.0lf",i);
        return 0;
      }
    }
  }
}


Measured memory_bytes: 384
#include<iostream>
#include<string>
using namespace std;
int main(){
	double e,f;
	int ar=0,al=0,br=0,bl=0,cr=0,cl=0,dr=0,dl=0;
	while(cin>>e>>f){
		if(e>=1.1)
			ar++;
		if(e>=0.6&&e<1.1)
			br++;
		if(e>=0.2&&e<0.6)
			cr++;
		if(e<0.2)
			dr++;
		if(f>=1.1)
			al++;
		if(f>=0.6&&f<1.1)
			bl++;
		if(f>=0.2&&f<0.6)
			cl++;
		if(f<0.2)
			dl++;
	}
	cout<<ar<<" "<<al<<endl;
	cout<<br<<" "<<bl<<endl;
	cout<<cr<<" "<<cl<<endl;
	cout<<dr<<" "<<dl<<endl;
    return 0;
}
 

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

int main(){
    string s;
    cin >> s;
    ll n = s.size();
    ll c = 0;
    rep(i, 0, n){
        if(s[i] == '1') c++;
    }
    cout << 2*min(c, n-c) << endl;
    return 0;
}

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

int main() {
  double N;
  cin >> N;
  vector<int> x(N);
  vector<int> y(N);
  int sum=1;
  double kyori=0;
  
  rep(i, N) cin >> x[i] >> y[i];
  rep(i, N) sum *= (i+1);
  
  rep(i, N){
  	rep(j, N){
    	kyori += sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
    }
  }
  //cout << sum << endl;
  cout << fixed << setprecision(10);
  cout << kyori/N << endl;

}

Measured memory_bytes: 256
package main

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

func main() {
	sc.Split(bufio.ScanWords)
	h, w, n := nextInt(), nextInt(), nextInt()
	puts(int(math.Ceil(float64(n) / float64(max(h, w)))))
	wt.Flush()
}

var (
	sc  = bufio.NewScanner(os.Stdin)
	rdr = bufio.NewReaderSize(os.Stdin, 1000000)
	wt  = bufio.NewWriter(os.Stdout)
)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(next())
	return i
}

func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(next(), 64)
	return f
}

func nextInts(n int) []int {
	slice := make([]int, n)
	for i := 0; i < n; i++ {
		slice[i] = nextInt()
	}
	return slice
}

func nextFloat64s(n int) []float64 {
	slice := make([]float64, n)
	for i := 0; i < n; i++ {
		slice[i] = nextFloat64()
	}
	return slice
}

func nextMega() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, _ := rdr.ReadLine()
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func putf(format string, a ...interface{}) {
	fmt.Fprintf(wt, format, a...)
}

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}

func max(nums ...int) int {
	ret := nums[0]
	for _, v := range nums {
		if v > ret {
			ret = v
		}
	}
	return ret
}


Measured memory_bytes: 640
using System;
using System.IO;
using System.Linq;

namespace AtCoder {
	class Program {
		public static void Main(string[] args) {
			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });

			var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
			int n = input[0];
			int x = input[1];
			int y = input[2];
			int[] k = new int[n - 1];
			for (int i = 1; i < n; i++) {
				for (int j = i + 1; j <= n; j++) {
					int ix = Math.Abs(i - x);
					int iy = Math.Abs(i - y);
					int jx = Math.Abs(j - x);
					int jy = Math.Abs(j - y);
					int min = j - i;
					min = min > ix + 1 + jy ? ix + 1 + jy : min;
					min = min > jx + 1 + iy ? jx + 1 + iy : min;
					k[min - 1]++;
				}
			}
			Console.WriteLine(string.Join("\n", k));
			Console.Out.Flush();
		}
	}
}

Measured memory_bytes: 13336
#include<stdio.h>
#include<string.h>


int main(void){
	
	char s[200000];
	
	scanf("%s",s);
	int sentou,matubi,len=strlen(s),i;
	
	
	
	for(i=0;i<len;i++){
		if(s[i]=='A'){
		sentou=i+1;
		break;
		}
	}
	
	for(i=len-1;i>=0;i--){
		if(s[i]=='Z'){
		matubi=i+1;
		break;
		}
	}
	
	
	printf("\n%d\n\n",matubi-sentou+1);
	
	
	return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

char S[2001];
int D[4002], P[4002], n[4002], press[4002];
pair<int, int> E[4002];
int H[4002];
void dfs(int A, int from, int head) {
	if (D[head]) return;
	int ret1 = 0;
	int ret2 = 0;
	int ret3 = 0;
	int saidai = 0;
	for (int i = H[A]; i; i = E[i].second) {
		if (E[i].first != from) {
			dfs(E[i].first, A, i);
			ret1 += P[i] + n[i];
			ret2 += n[i];
			saidai = max(saidai, 2 * (P[i] + n[i]) - 2 * press[i]);
		}
	}
	if (saidai - ret1 < 0) ret3 = ret1 / 2;
	else ret3 = ret1 - saidai / 2;
	if (S[A] == '1') ret2++;

	D[head] = 1;
	P[head] = ret1;
	n[head] = ret2;
	press[head] = ret3;
}

const int cm = 1 << 15;
char cn[cm], *ci = cn, ct;
inline int getint() {
	int A = 0;
	while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}

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


	fread_unlocked(cn, 1, cm, stdin);
	int N = getint();
	rep1(i, N) S[i] = *ci++;
	ci++;

	int kazu = 0;
	rep1(i, N) if (S[i] == '1') kazu++;
	if (kazu == 1) {
		co(0);
		return 0;
	}

	int EN[2001] = {};
	for (int i = 1; i < 2 * N - 2; i += 2) {
		int a = getint(), b = getint();
		EN[a]++;
		EN[b]++;
		E[i] = mp(a, H[b]);
		H[b] = i;
		E[i + 1] = mp(b, H[a]);
		H[a] = i + 1;
	}

	bool OK = 0;
	int saishou = 1e9;
	rep1(j, N) {
		if (EN[j] > 1) {
			int p = 0;
			int saidai = 0;
			for (int i = H[j]; i; i = E[i].second) {
				dfs(E[i].first, j, i);
				p += P[i] + n[i];
				saidai = max(saidai, 2 * (P[i] + n[i]) - 2 * press[i]);
			}
			if (saidai <= p && p % 2 == 0) {
				OK = 1;
				saishou = min(saishou, p / 2);
			}
		}
	}

	if (OK) co(saishou);
	else co(-1);

	Would you please return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
ld L,a,b,c;
int main(void){
    cin>>a>>b>>c;
    if(a*a*b/2>=c)L=2*c/(a*b*b);
    else{c=a*a*b-c;L=a*a*a/(2*c);}
    cout<<setprecision(10)<<90-atan(L)*180/M_PI<<endl;
}


Measured memory_bytes: 384
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    m, d = na()
    n, s = na()

    if s == 1:
        print(1)
    else:
        print(0)


if __name__ == '__main__':
  main()

Measured memory_bytes: 3060
fn main() {
    let (x, y, z) = read_tuple::<i32>();
    println!("{} {} {}", z, x, y);
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}
fn read_tuple<T: std::str::FromStr + Copy>() -> (T, T, T) {
    let temp = read_vec();
    (temp[0], temp[1],temp[2])
}



Measured memory_bytes: 4352
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(e, i) for (auto e: i)
#define all(x) x.begin(), x.end()


using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
typedef long long ll;


// DFS Ver
int main() {
#ifdef AZUKI_LOCAL
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
#endif
    int num;
    while (cin >> num) {
        cout << (num + num * num + num * num * num) << endl;
    }

    return 0;
}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
#define fix10 cout << fixed << setprecision(10);
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define MOD 1000000007
#define INF 1e9

int main(){
  int n,m,r;
  cin >> n >> m >> r;
  vector<vector<int>> dis(n,vi(n,INF));
  vector<int> s(r);
  rep(i,r){
    cin >> s.at(i);
    s.at(i)--;
  }
  sort(s.begin(),s.end());
  //rep(i,r) cout << s.at(i) << " ";
  rep(i,n) dis.at(i).at(i) = 0;
  rep(i,m){
    int a,b,c;
    cin >> a >> b >> c;
    dis.at(a-1).at(b-1) = c;
    dis.at(b-1).at(a-1) = c;
  }
  rep(k,n){
    rep(i,n){
      rep(j,n){
        dis.at(i).at(j) = min(dis.at(i).at(j) , dis.at(i).at(k) + dis.at(k).at(j));
      }
    }
  }
  int ans = INF;
  while(next_permutation(s.begin(),s.end())){
    int d = 0;
    rep(i,r-1){
      d += dis.at(s.at(i)).at(s.at(i+1));
    }
    ans = min(ans,d);
  }
  cout << ans << endl;
 // rep(i,r) cout << s.at(i) << " ";
}

Measured memory_bytes: 384
from collections import deque

def solve():
    H,W = map(int,input().split())
    C = list(map(int,input().split()))
    D = list(map(int,input().split()))

    S = ["##" + input() + "##" for _ in range(H)]
    for i in range(2):
        S.insert(0,"#"*(W+4))
        S.append("#"*(W+4))

    INF = float("inf")
    cost = [[INF for _ in range(W+4)] for _ in range(H+4)]
    cost0 = deque()
    ans = -1

    cost0.append((C[0]+1, C[1]+1, 0))
    cost[C[0]+1][C[1]+1] = 0

    move = [(1,0), (0,1), (-1,0), (0,-1)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)]+move]

    cost1 = deque()

    while cost0:
        h,w,c = cost0.popleft()
        cost1.append((h,w,c))

        for i, j in move:
            dh = h+i
            dw = w+j
            if S[dh][dw] == "." and c < cost[dh][dw]:
                cost[dh][dw] = c
                cost0.appendleft((dh, dw, cost[dh][dw]))
        
        if len(cost0) == 0:
            while cost1:
                h, w, c = cost1.popleft()
                for i, j in warp:
                    dh = h+i
                    dw = w+j
                    if S[dh][dw] == "." and c+1 < cost[dh][dw]:
                        cost[dh][dw] = c+1
                        cost0.append((dh,dw, cost[dh][dw]))
        
        if cost[D[0]+1][D[1]+1] != INF:
            ans = cost[D[0]+1][D[1]+1]

    print(ans)

if __name__ == "__main__":
    solve()

Measured memory_bytes: 24732
#include <bits/stdc++.h>    
//#include <ext/pb_ds/assoc_container.hpp> 
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//using namespace __gnu_pbds; 
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long 
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);} 
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}

signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf    
    #ifndef ONLINE_JUDGE    
           freopen("input.txt","r",stdin);
           freopen("output.txt","w",stdout);
    #endif//*/
    int n;
    cin>>n;
    int a[n];
    for (int i = 0; i < n; ++i)
    {
        cin>>a[i];
    }
    int cur = 0 ;
    int ans = 0;

    for (int i = 0; i < n; ++i)
    {
        cur = max(cur,a[i]);
        ans+=(cur-a[i]);
    }

    cout<<ans;
    printclock;
    return 0;
}

Measured memory_bytes: 5368
import sys
input = sys.stdin.readline
n = int(input())
s = list(map(int,input().split()))
t = []
for i in range(n):
    t.append([s[i],i])

t.sort()
k = []
for i in range(n):
    k.append(str(t[i][1]+1))

q = " ".join(k)
print(q)

Measured memory_bytes: 28452
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;
typedef long long ll;

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

    if (a >= 13) cout << b << endl;
    else if (a >= 6) cout << b/2 << endl;
    else cout << 0 << endl;

    return 0;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
int A,B,C ;
cin >> A>> B>> C;
if (A>=B&&A>=C && B>=C)
  cout <<  ((A*10)+B)+C;
else
if (B>=A&&B>=C && C>=A)
  cout <<  ((B*10)+C)+A;
else
if (C>=A&&C>=B && B>=A)
  cout <<  ((C*10)+B)+A;
else
if (B>=A&&B>=C && A>=C)
  cout <<  ((B*10)+A)+C;

else
if (C>=A&&C>=B&& A>=B)
  cout <<  ((C*10)+A)+B;
else
if (A>=C&&A>=B && C>=B)
  cout <<  ((A*10)+C)+B;


    return 0;
}


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

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define 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 ALL(v) (v).begin(), (v).end()

typedef pair<ll, ll> P;
map<ll, ll> mp;
int main(){
    string s;
    cin >> s;
    reverse(ALL(s));

    vector<string> a(4);
    a[0] = "dream";
    a[1] = "dreamer";
    a[2] = "erase";
    a[3] = "eraser";
    REP(i, 4) reverse(ALL(a[i]));

    int idx = 0;
    while(idx < s.length()) {
        if(s.substr(idx, 5) == a[0]) idx+= 5;
        else if(s.substr(idx, 7) == a[1]) idx+= 7;
        else if(s.substr(idx, 5) == a[2]) idx+= 5;
        else if(s.substr(idx, 6) == a[3]) idx+= 6;
        else {
            cout << "NO" << endl;
            return 0;
        }
    }

    cout << "YES" << endl;


    return 0;
}


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

using namespace std;

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

    int64_t leastCommonMultiple = 1;

    // O(N) = 10^5
    for (int i = 1; i <= B; i++) {
        leastCommonMultiple = A * i;
        if (leastCommonMultiple % B == 0) {
            break;
        }
    }

    cout << leastCommonMultiple << endl;

    return 0;
}

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

// Define
using ll = int64_t;
using ul = uint64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
const ll INF = LONG_MAX;
const ul MAX = ULONG_MAX;
const char nl = '\n';

#define elif else if
#define def inline auto
#define run __attribute__((constructor)) def _

// Debug
#define debug(x) cerr << (x) << " (L:" << __LINE__ << ")" << '\n'

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i < _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i > _##i; --i)
#define rep(i, n) inc(i, 0, n)
#define be(c) begin(c), end(c)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)

// Speed
run() { fasten, fout(10); }
#pragma GCC optimize("-O3")
#pragma GCC target("avx")

// Math
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

signed main(){
    int X, Y;
    cin >> X >> Y;
    cout << X+Y/2 << nl;
}


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

int main() {
	double v;
	while (cin >> v) {
		double t = v / 9.8;
		double d = 4.9*t*t;
		for (int i = 1;; i++) {
			if (5 * i - 5 >= d) {
				cout << i << endl;
				break;
			}
		}
	}
}

Measured memory_bytes: 3144
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define um unordered_map
#define all(x) (x).begin(),(x).end()
#define forn(i,n) for(int i=0;i<int(n);++i)
#define for1(i,n) for(int i=1;i<=int(n);++i)
#define che(x) cout<<#x<<" having "<<x<<endl 
using namespace std;
typedef long double ld;typedef long long ll;
typedef vector<int> vi;typedef vector<pair<int, int>> vpi;typedef vector<vi> vvi;
typedef vector<ll> vll;typedef vector<pair<ll, ll>> vpll;typedef vector<vll> vvll;
template<class T>bool uin(T &a,T b){return a>b?(a=b,true):false;}
template<class T>bool uax(T &a,T b){return a<b?(a=b,true):false;}
const ll MOD=1e9+7,INF=1e18;
/********************************************************************************/
vll v[200001];
ll a[200001];
ll n,q;

void go(ll ver, ll baap, ll val)
{
    a[ver]+=val;
    forn(i,v[ver].size()){

        if(v[ver][i]==baap) continue;
        else go(v[ver][i],ver,a[ver]);

    }


}


int32_t main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    cout.precision(10);cout<<fixed;
 

    cin>>n>>q;
    
    ll x,y;
    for1(i,n-1){
        cin>>x>>y;
        v[x-1].eb(y-1);
        v[y-1].eb(x-1);
    }
    for1(i,q){
        cin>>x>>y;
        a[x-1]+=y;
    }

    go(0,-1,0);


    forn(i,n) cout<<a[i]<<" ";
    cout<<endl;

    return 0;
}

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

const int N = 1e5 + 5;

int n, k, dp[N], h[N];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  for(int i = 1; i <= n; i++) cin >> h[i];
  memset(dp, 63, sizeof dp);
  dp[1] = 0;
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= k; j++) {
      if(i+j <= n) dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j] - h[i]));
    }
  }
  cout << dp[n] << "\n";
}


Measured memory_bytes: 1024
import sys
sys.setrecursionlimit(10 ** 6)


def dfs(u):
    for v, w in edge[u]:
        if trail[v] is None:
            trail[v] = (trail[u] + w) % 2
            dfs(v)


N = int(input())
trail = [None] * N
edge = [[] for _ in range(N)]

for i in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    w %= 2
    edge[u].append((v, w))
    edge[v].append((u, w))

trail[0] = 0
dfs(0)

for t in trail:
    print(t)


Measured memory_bytes: 76464