akhauriyash/Code-Regression huggingface dataset:
#include <iostream>

using namespace std;

struct Node
{
	int parent = -1;
	int left;
	int right;
} node[25];

void preorder(int id) {
	cout << " " << id;
	if (node[id].left != -1) {
		preorder(node[id].left);
	}
	if (node[id].right != -1) {
		preorder(node[id].right);
	}
}

void inorder(int id) {
	if (node[id].left != -1) {
		inorder(node[id].left);
	}
	cout << " " << id;
	if (node[id].right != -1) {
		inorder(node[id].right);
	}
}

void postorder(int id) {
	if (node[id].left != -1) {
		postorder(node[id].left);
	}
	if (node[id].right != -1) {
		postorder(node[id].right);
	}
	cout << " " << id;
}

int main() {
	int n, id, left, right, parent;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> id >> left >> right;
		node[id].left = left;
		node[id].right = right;
		node[left].parent = id;
		node[right].parent = id;
	}

	for (int i = 0; i < n; i++) {
		if (node[i].parent == -1) {
			parent = i;
			break;
		}
	}

	cout << "Preorder\n";
	preorder(parent);
	cout << "\nInorder\n";
	inorder(parent);
	cout << "\nPostorder\n";
	postorder(parent);
	cout << "\n";

	return 0;
}

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

int main()
{
  string s;
  cin>>s;
  if(s.length()>=6)
  {
  if(s[2]==s[3] && s[4]==s[5])
  {
      cout<<"Yes";
  }
  else
  {
    cout<<"No";
  }
  }
  else{ cout<<"No";}
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
#define rep(i, n) for(int i=0;i<(int)(n);i++)

class Cube{
    public:
    int f[6];
    Cube(){}
    void roll_z(){ roll(1, 2, 4, 3);}
    void roll_y(){ roll(0, 2, 5, 3);}
    void roll_x(){ roll(0, 1, 5, 4);}
    void roll(int i, int j, int k, int l){
        int t=f[i]; f[i]=f[j]; f[j]=f[k]; f[k]=f[l]; f[l]=t; 
    }
};

int getRight(Cube c1, int f1, int f2){
    rep(i, 6){
        rep(j, 4){
            if(c1.f[0]==f1 && c1.f[1]==f2) return c1.f[2];
            c1.roll_z();
        }
        if(i%2==0) c1.roll_y();
        else c1.roll_x();
    }
    return -1;
}

int main(){
    Cube c;
    int f1, f2;

    rep(i, 6) cin>>c.f[i];
    int q; cin>>q;

    rep(i, q){
        cin>>f1>>f2;
        cout<<getRight(c, f1, f2)<<endl;
    }
    return 0;
}


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

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

  int ans = 0;
  int a, b;
  for (int i=0; i<N-1; ++i) {
    cin >> b;
    if (i==0) {
      ans += b;
    } else {
      ans += min(a, b);
    }
    a = b;
  }

  cout << ans + a << "\n";
}


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

dp = Array.new(n) { Array.new(2 + 1, 0) }
dp[0][1] = dp[1][2] = 1

1.upto(n - 1) do |i|
  if s[i - 1] != s[i]
    # 1 -> 1
    dp[i][1] = (dp[i - 1][1] >= dp[i - 1][2] ? dp[i - 1][1] : dp[i - 1][2]) + 1
  else
    # 1 -> 2
    dp[i][1] = dp[i - 1][2] + 1
  end

  if s[i - 3..i - 2] != s[i - 1..i]
    # 2 -> 2
    dp[i][2] = (dp[i - 2][1] >= dp[i - 2][2] ? dp[i - 2][1] : dp[i - 2][2]) + 1
  else
    # 2 -> 1
    dp[i][2] = dp[i - 2][1] + 1
  end
end

puts dp[n - 1][1] >= dp[n - 2][2] ? dp[n - 1][1] : dp[n - 2][2]


Measured memory_bytes: 16764
#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define all(v) (v).begin(),(v).end()

#define PII pair<int,int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
};
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} 
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}
#define jeb() fflush(stdout);
//}

int k;
char s[305];
int mod = 998244353;
VI w; 

int memo[305][305][605];

int rob(int pos, int dod, int kara) {
	if (kara < 0) return 0;
	if (pos == 0) {
		if (kara < dod) return 0;
		return 1;
	}
	if (memo[pos][dod][kara] != -1) {
		return memo[pos][dod][kara];
	}
	int ans = 0;
	FOR(i,0,w[pos]+dod+1) {
		ans = ans + rob(pos-1,dod+w[pos]-i, kara-abs(w[pos]-i));
		if (ans >= mod) ans -= mod;
	}
	memo[pos][dod][kara] = ans;
	return ans;
}	

int main() {
	scanf("%s", s);
	read(k);
	int n = strlen(s);
	s[n] = '0';
	n++;
	FOR(i,0,n) {
		if (s[i] == '0') {
			int j = i-1;
			while (j>=0 && s[j] == '1') j--;
			if (j >= 0) w.pb(i-j-1);
			else w.pb(0);
		}
	}
	//debug() << imie(w);
	int kara = 2*k;
	int sum = 0; FOR(j,0,w.size()) sum += w[j];
	int N = w.size();
	kara = min(kara, 2*sum);
	FOR(i,0,N) FOR(j,0,sum+1) FOR(u,0,kara+1) memo[i][j][u] = -1;
	printf("%d\n", rob(w.size() - 1, 0, kara));
}


Measured memory_bytes: 57852
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
#define Max 1000001
#define inf INT_MAX
#define llinf LONG_LONG_MAX
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define endl '\n'
const ll N=(1ll<<12),mod=1000000007,bit=60;
signed main()
{
    fast;
    ll pro=1,temp,t,i,j,l,r,n,m,mid,z,k,x,y,rem,carry=0,ind,ans=0,mx=-llinf,mn=llinf,cnt=0,curr=0,prev,next,sum=0,flag=1,i1=-1,i2=-1,u,v;
    cin>>n>>k;
    ll dp[N];
    memset(dp,-1,sizeof(dp));
    for(i=0;i<k;i++)
    {
    	cin>>x>>m;
    	ll mask=0;
    	for(j=0;j<m;j++)
    	{
    		cin>>y;
    		--y;
    		mask|=(1ll<<y);
    	}
    	if(dp[mask]==-1)
    	{
    		dp[mask]=x;
    	}
    	else
    	{
    		dp[mask]=min(dp[mask],x);
    	}
    	for(j=0;j<(1ll<<n);j++)
    	{
    		if(dp[j]!=-1)
    		{
    			if(dp[j | mask]==-1)
    			{
    				dp[j | mask]=x+dp[j];
    			}
    			else
    			{
    				dp[j | mask]=min(dp[j | mask],dp[j]+x);
    			}
    		}
    	}
    }
    x=(1ll<<n)-1;
    cout<<dp[x]<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream> //cinなど
#include<string> //文字列
#include<algorithm> //sort など
#include<math.h> //paw(冪乗など)
#include<set> // 集合
#include<map> //写像
#include<stdio.h> //printf
#include<stack>

using namespace std;
int main(){
  int A,B;
  cin>> A >> B;
  if(B % A == 0){
    cout << A+B <<endl;
  }else{
    cout<< B-A<< endl;
  }
return 0;
}


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

using namespace std;

int vertex[10001];
vector <int> adj[10001];
vector <int> v;
long long int res = 0;
int n,a,b;

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

    cin >> n;

    for(int i=0;i<n-1;i++)
    {
    	cin >> a >> b;
    	adj[a].push_back(b);
    	adj[b].push_back(a);
    }

    for(int i=0;i<n;i++)
    {
    	cin >> a;
    	v.push_back(a);
    }

    sort(v.rbegin(),v.rend());

    memset(vertex,-1,sizeof(vertex));

    queue <int> que;
    que.push(1);
    int idx = 0;
    vertex[1] = v[idx++];

    while(!que.empty())
    {
    	int now = que.front();
    	que.pop();

    	for(auto next : adj[now])
    	{
    		if(vertex[next]==-1)
    		{
    			vertex[next] = v[idx++];
    			res += min(vertex[now],vertex[next]);
    			que.push(next);
    		}
    	}
    }

    cout << res << '\n';
    for(int i=1;i<=n;i++)
    {
    	cout << vertex[i] << ' ';
    }
    cout << '\n';
    
    return 0;
}

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

using namespace std;
using ll = long long;

ll f(ll n) {
  if (n % 2 == 1) return ((n + 1) / 2) % 2;
  return (((n + 2) / 2) % 2) ^ (n + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll A, B;
  cin >> A >> B;
  cout << (f(A - 1) ^ f(B)) << '\n';
  return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N=100+5;
struct Data
{
    int t,c;
}data[MAX_N];
bool cmp(struct Data a,struct Data b)
{
    return a.c<b.c;
}
int main()
{
    int N,T;
    int a,b;
    scanf("%d%d",&N,&T);
    for(int i=0;i<N;i++)
    {
        scanf("%d%d",&a,&b);
        data[i].c=a;
        data[i].t=b;
    }
    sort(data,data+N,cmp);
    for(int i=0;i<N;i++)
    {
        if(data[i].t<=T)
        {
            printf("%d\n",data[i].c);
            return 0;
        }
    }
    printf("TLE\n");
    return 0;
}


Measured memory_bytes: 128
// +build ignore
package main

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

func solve(sc *scanner, wr *bufio.Writer) int {
	A := sc.i()
	B := sc.i()

	if (B+A)%2 == 1 {
		fmt.Println("IMPOSSIBLE")
	} else {
		fmt.Println((B + A) / 2)
	}

	return 0
}

func main() {
	sc := newScanner(os.Stdin)
	wr := bufio.NewWriter(os.Stdout)
	ret := solve(sc, wr)
	wr.Flush()
	os.Exit(ret)
}

// I/O
type scanner struct {
	sc *bufio.Scanner
}

func newScanner(input io.Reader) *scanner {
	sc := bufio.NewScanner(input)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &scanner{sc}
}

func (s *scanner) s() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *scanner) i() int {
	i, e := strconv.Atoi(s.s())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *scanner) f() float64 {
	f, e := strconv.ParseFloat(s.s(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *scanner) bs() []byte {
	return []byte(s.s())
}

func (s *scanner) is(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.i()
	}
	return res
}

func (s *scanner) fs(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.f()
	}
	return res
}


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

ll ans;

void rec(ll acc, string s, int a, int b){
    if(s.size() == a+b){
        ans += acc + stoll(s.substr(a,b));
        return;
    }
    rec(acc + stoll(s.substr(a,b)), s, a+b, 1);
    rec(acc, s, a, b+1);
}

int main(void){
    string s;
    cin >> s;
    rec(0, s, 0, 1);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.*;
class Main{
    public static void main(String[]args){
        Scanner cin=new Scanner(System.in);
        int input_W = cin.nextInt();
        int input_a=cin.nextInt();
        int input_b=cin.nextInt();
        int output;

        if(input_a<input_b){
            if(input_a+input_W<input_b){
                output = input_b-(input_a+input_W);
            }else{
                output = 0;
            }
        }else{
            if(input_b+input_W<input_a){
                output = input_a-(input_b+input_W);
            }else{
                output = 0;
            }
        }
        System.out.println(output);
        }
    }


Measured memory_bytes: 23636
// AtCoder
// ABC B - Crane and Turtle

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <functional>
#include <random>
#include <vector>
#include <chrono>

using namespace std;
//using namespace std::chrono;


int main()
{
	//steady_clock::time_point start = steady_clock::now();
	int X, Y;

	cin >> X >> Y;

	// 計算
	int cnt;
	int ans = 0;
	for (int i = 0; i <= X; i++) {
		cnt = 2*i + 4*(X - i);
		if (cnt == Y) {
			ans = 1;
		} 
	}
	if (ans == 1) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}

	//計算時間集計
	/*
	steady_clock::time_point end = steady_clock::now();
    steady_clock::duration d = end - start;
	cout << "計算時間は" << duration_cast<milliseconds>(d).count() << "ミリ秒です。\n";
	*/
}




Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
int br[4];
int main()
{
	int i,a;
	for(i=0;i<6;i++)
	{
		cin>>a;
		br[a-1]++;
	}
	sort(br,br+4);
	if(br[3]==2 && br[2]==2 && br[1]==1 && br[0]==1)cout<<"YES\n";
	else cout<<"NO\n";
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define INT(name) int name; scanf("%d",&name);
#define VINT(name,n) vector<int> name(n); for(int i = 0;i < n;i++) scanf("%d",&name[i]);

using namespace std;

int main(int argc, char const *argv[]){
	INT(x)
	VINT(data,x)

	for(int i = 1;i < x;i++){
		printf("%d",data[0]);
		for(auto it = data.begin()+1;it < data.end();it++)
    		printf(" %d",*it);
    	printf("\n");
   		int key = data[i];
    	int j = i - 1;
    	while(j >= 0 and data[j] > key){
        	data[j+1] = data[j];
        	j--;
        }
    	data[j+1] = key;
    }
    		printf("%d",data[0]);
		for(auto it = data.begin()+1;it < data.end();it++)
    		printf(" %d",*it);
    	printf("\n");
	return 0;
}

Measured memory_bytes: 3184
class Point:
	def __init__(self, x, y):
		self.x = x
		self.y = y

def ham_dist(p1, p2):
	dx = abs(p1.x - p2.x)
	dy = abs(p1.y - p2.y)

	return dx+dy

n = int(input())
possible = True

pos = Point(0, 0)
t = 0

for i in range(n):
	_t, x, y = map(int, input().split())
	
	dest = Point(x, y)
	dt = _t - t
	dist = ham_dist(pos, dest)

	if dt < dist or (dt - dist) % 2 != 0:
		possible = False
		
	t = _t
	pos = dest

if possible:
	print("Yes")
else:
	print("No")

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define OJ \
        freopen("input.txt","r",stdin); \
        freopen("output.txt","w",stdout);
using namespace std;

int main()
{
//  OJ;
int n,ans=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
  cin>>a[i];
}
for(int i=0;i<n;i++)
{
  if((i+1)%2&&a[i]%2)
  {
    ans++;
  }
}
cout<<ans;
}

Measured memory_bytes: 3580
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(),(A).end()
#define ll long long
const ll mod = 1e9+7;
const ll INF = 2*1e18;
const int inf = 1e9+7;

int main(void){
	ll N,A,B;
	cin >> N >> A >> B;
	cout << max((ll)0,1 + B*(N-2) - A*(N-2)) << endl;
}

Measured memory_bytes: 384
import sys
sys.setrecursionlimit(10**9)


S = input()
N, P = len(S),2019

if P == 2 or P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i + 1
    print(ans)
    sys.exit()

num = [0] * P
num[0] = 1
now, ans = 0, 0
_10 = 1
for i in range(N-1, -1, -1):
    now = (now + int(S[i]) * _10) % P
    _10 *= 10
    _10 %= P
    ans += num[now]
    num[now] += 1

print(ans)


Measured memory_bytes: 9320
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define BIG 1000000010
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
typedef long long int ll;


int main(){
  int n,m;
  ll ans=1;
  cin>>n>>m;
  if(abs(n-m)>1){cout<<0<<endl;return 0;}
    if(n==m) ans*=2;
    rep(i,n){
      (ans*=i+1)%=MOD;
    }
    rep(i,m){
        (ans*=i+1)%=MOD;
    }
  
  cout<<ans<<endl;
  return 0;
}



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

int main() {
	int	N;
	cin >> N;
	for (int i = 1; i < 10; i++)
	{
		if (N % i == 0 && N / i < 10)
		{
			cout << "Yes" << endl;
			return (0);
		}

	}
	cout << "No" << endl;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
int n,t,x,y;
cin>>n;
for(int i=1;i<=n;i++){
cin>>t>>x>>y;
if((x+y>t)||((x+y)%2==0&&t%2==1)||((x+y)%2==1&&t%2==0)){
cout<<"No"<<endl;
return 0;}
}
cout<<"Yes"<<endl;}

Measured memory_bytes: 256
n = int(input())
arr = list(map(int,input().split()))


def req_node(i,belowmin,belowmax):

    thismax = belowmax+arr[i]
    thismin = (belowmin+arr[i])//2+(belowmin+arr[i])%2

    return req_node(i-1,thismin,thismax)

bmin = [arr[-1]]
bmax = [arr[-1]]


belowmin = arr[-1]
belowmax = arr[-1]
for i in range(2,n+2):
    thismax = belowmax+arr[-i]
    thismin = (belowmin)//2+(belowmin)%2+arr[-i]
    bmin.append(thismin)
    bmax.append(thismax)
    belowmax = thismax
    belowmin = thismin



bmin.reverse()
bmax.reverse()

# print(bmin)
# print(bmax)

nodes =  1
ans = 0
flag =True
for i in range(n):
    ans += nodes
    # print(nodes,ans)
    nextmax = 2*(nodes-arr[i])
    nextmin = nodes-arr[i]

    nodes = min(bmax[i+1],nextmax)
    if nextmax < bmin[i+1] or nextmin > bmax[i+1]:
        flag = False
        # print("break")
        break

# print(arr)
if flag:
    # if arr[-1] <= nodes*2:
    #     print(ans+arr[-1])
    # else:
    #     print(-1)
    if n == 0:
        if arr[0] == 0:
            print(0)
        elif arr[0] == 1:
            print(1)
        else:
            print(-1)
    else:
        print(ans + arr[-1])

else:
    print(-1)

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

string s;
ll K;

int main() {
    cin >> s >> K;
    int flag = 1;
    char ans;
    for (int i = 0; i < min(K, (ll)s.size()); i++) {
        if (s[i] - '0' != 1) {
            flag = 0;
            ans = s[i];
            break;
        }
    }
    ans = flag ? '1' : ans;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <cctype>
#include <vector>
using namespace std;
typedef long long int64;
inline int64 read(int f = 1, int64 x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 90+5, P = 1e9+7;
int n, m, ans, q;
int64 f[N];
vector<pair<int64, int64> > s[N];
int main()
{
    for(f[0] = f[1] = 1, n = 2; n <= 90; ++n) f[n] = f[n-1]+f[n-2]; --n;
    s[1].push_back(make_pair(1, 2)), s[1].push_back(make_pair(1, 3)), s[1].push_back(make_pair(1, 4));
    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < s[i].size(); ++j)
        {
            int64 x = s[i][j].second, y = s[i][j].first+x;
            while(y <= f[i]+f[i+3]) s[i+1].push_back(make_pair(x, y)), y += x;
        }
    q = read();
    while(q--)
    {   
        int64 x = read(), y = read(); if(x > y) swap(x, y);
        ans = 0; for(m = 1; f[m+1] <= x&&f[m+2] <= y; ++m);
        if(m == 1) ans = x*y%P;
        else
            for(int i = 0; i < s[m-1].size(); ++i)
            {
                int64 u = s[m-1][i].first, v = s[m-1][i].second;
                if(v <= x) ans = (ans+(y-u)/v)%P;
                if(v <= y) ans = (ans+(x-u)/v)%P;
            }
        printf("%d %d\n", m, ans);
    }
    return 0;
}

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

using namespace std;

int N;
int Count[2000017];

void solve() {
	for (int i = 0; i < 2000017; ++i) {
		if (Count[i] == 0) {
			continue;
		}
		cout << i << " 0" << endl;
	}
}

int main() {
	int a, b, c;
	
	cin >> N;
	for (int i = 0; i < N; ++i) {
		cin >> a >> b;
		c = a + b;
		while (Count[c] != 0) {
			Count[c] = 0;
			++c;
		}
		Count[c] = 1;
	}
	solve();
	
	return 0;
}


Measured memory_bytes: 3880
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//#include <stdbool.h>
//#include <limits.h>
//#include <math.h>

//昇順
int acs(const void *a, const void *b)
{
  return *(int *)a - *(int *)b;
}

int main() {
  int n;
  scanf("%d", &n);
  int a[n], b[n], c[n];
  for (int i=0; i<n; i++)
    scanf("%d", a+i);
  for (int i=0; i<n; i++)
    scanf("%d", b+i);
  for (int i=0; i<n; i++)
    scanf("%d", c+i);
  qsort(a, n, sizeof(int), acs);
  qsort(b, n, sizeof(int), acs);
  qsort(c, n, sizeof(int), acs);
  long ans = 0, sum[n+1];
  int j = 0;
  sum[0] = 0;
  for (int i=0; i<n; i++) {
    while (j<n && a[j]<b[i])
      j++;
    sum[i+1] = sum[i] + j;
  }
  j = 0;
  for (int i=0; i<n; i++) {
    while (j<n && b[j]<c[i]) {
      j++;
    }
    ans += sum[j];
  }
  printf("%ld\n", ans);
  return 0;
}


Measured memory_bytes: 2676
import java.util.*;
import java.lang.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] t = new long[n];
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			t[i] = sc.nextLong();
			a[i] = sc.nextLong();
		}
		for (int i = 1; i < n; i++) {
			long kake = Math.max((t[i - 1] + a[i - 1] + t[i] + a[i] - 1) / (t[i] + a[i]), Math.max((t[i - 1] + t[i] - 1) / t[i], (a[i - 1] + a[i] - 1) / a[i]));
			t[i] *= kake;
			a[i] *= kake;
		}
		System.out.println(t[n - 1] + a[n - 1]);
	}
}

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

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

using namespace std;
#define ALL(a)  (a).begin(),(a).end()
//v.push_back(123);
//sort(v.begin(), v.end(),greater<int>());
//for(int x : v) {cout << x << endl;}

//for(int x: v) cout << x << endl;
//vector<vector<char>> v(H, vector<char>(W));
//vector<int> v(1000);
//

long long pow_mod(long long x, long long y, long long MOD){
	long long ret = 1;
	while(y){
		if (y & 1) ret = ret * x % MOD;
		x = x * x % MOD;
		y>>=1;
	}
	return ret;
}

signed main(){
  int N;
  cin>>N;
  vector<int> a;
  int flag[N];
  int tmp;
  rep(i, N){
    cin >> tmp;
    a.push_back(tmp);
  }
  int rakuda[N+1];
  sort(a.begin(), a.end());

  int check=1;


  int sum=0;
  for(int i=0;i<N;i++){
    sum ^=a[i];
  }

  if(sum!=0){
    check=-1;
  }

  if(check==1){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }


  return 0;
}


Measured memory_bytes: 1400
N = int(input())
W = list(map(int,input().split()))
for i in range(N-1):
    W[i+1]+=W[i]
MIN = 10**8
for i in range(N-1):
    if MIN>abs(W[N-1]-2*W[i]):
        MIN = abs(W[N-1]-2*W[i])
print(MIN)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007 

ll op[100][10],n,pro[100][11],jo[10],ans=-1*INF;

void cal(){
	bool ex=true;
	for(ll i=0;i<10;i++){
		if(jo[i]){
			ex=false;
		}
	}
	if(ex){
		return;
	}
	//cout << "k";
	ll sum=0;
	for(ll i=0;i<n;i++){
		ll cnt=0;
		for(ll j=0;j<10;j++){
			if(jo[j]==1&&op[i][j]==1){
				cnt++;
			}
		}
		sum+=pro[i][cnt];
	}
	ans=max(sum,ans);
}
void dep(ll now){
	if(now==10){
		cal();
		return ;
	}
	jo[now]=1;dep(now+1);
	jo[now]=0;dep(now+1);
}
int main() {
	cin>>n;
	for(ll i=0;i<n;i++){
		for(ll j=0;j<10;j++){
			cin>>op[i][j];
		}
	}
	for(ll i=0;i<n;i++){
		for(ll j=0;j<=10;j++){
			cin>>pro[i][j];
		}
	}
	//cout << pro[0][6];
	dep(0);
	cout <<ans;
	// your code goes here
	return 0;
}

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

string mul(string a,string b){
  if(a=="0"||b=="0")return "0";  
  int as=a.size(),bs=b.size();
  int len=as+bs;
  vector<int> A(as),B(bs),C(len);
  for(int i=0;i<as;i++)A[i]=a[as-1-i]-'0';   
  for(int i=0;i<bs;i++)B[i]=b[bs-1-i]-'0';

  for(int i=0;i<as;i++)
    for(int j=0;j<bs;j++)
      C[i+j]+=A[i]*B[j];

  for(int i=0;i+1<len;i++){
    C[i+1]+=C[i]/10;
    C[i]%=10;
  }
  
  string res="";
  for(int i=len-1;i>=0;i--){
    if(res.size()==0&&C[i]==0)continue;
    res.push_back('0'+C[i]);
  }
  return res;
}

int main(){
  while(1){
    int n;
    string X,Y="1";
    cin>>X>>n;
    if(X=="0"&&n==0)break;
    for(int i=0;i<n;i++)Y=mul(Y,X);
   
    reverse(Y.begin(),Y.end());
    
    string t[18]={
      "","Man","Oku","Cho","Kei","Gai","Jo","Jou","Ko","Kan",
      "Sei","Sai","Gok","Ggs","Asg","Nyt","Fks","Mts"
    };

    string ans="";
    for(int i=0;i<18;i++){
    
      string tmp="";
      for(int j=0;j<4;j++){
        if(i*4+j<(int)Y.size())tmp.push_back(Y[i*4+j]);
      }
      if(tmp=="")break;
      reverse(tmp.begin(),tmp.end());
      
      stringstream ss,ss2;
      int k;
      ss<<tmp;
      ss>>k;

      ss2<<k;
      ss2>>tmp;
      
      if(k>0){
        ans = tmp + t[i] + ans;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

Measured memory_bytes: 1244
#include <iostream>
#include <map>

using namespace std;
constexpr int64_t MOD = 1'000'000'007;

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

int main(void) {
    int N;
    cin >> N;
    map<int64_t,int> mp;

    for(int i=1; i<=N; i++) {
        for(auto e: prime_factor(i)) {
            mp[e.first] += e.second;
        }
    }

    int64_t ans = 1;
    for(auto e: mp) {
        ans *= e.second + 1;
        ans %= MOD;
    }

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


Measured memory_bytes: 256
sx,sy,tx,ty = map(int,input().split())
x = tx - sx
y = ty - sy
print("U"*y+"R"*x+"D"*y+"L"*(x+1)+"U"*(y+1)+"R"*(x+1)+"D"+"R"+"D"*(y+1)+"L"*(x+1)+"U")

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

flag=0
for i in range(a-1):
    if N[i+1]>N[i]:
        N[i+1]=N[i+1]-1
    if N[i]>N[i+1]:
        flag=1
    

if flag==0:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 14252
//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

class KMP
{
public:
	string pattern;
	int plen;
	vector<int> table;
	KMP(const string& s) : pattern(s), plen((int)pattern.size()), table(plen + 1) {
		table[0] = -1;
		int j = -1;
		for (int i = 0; i < plen; i++) {
			while (j >= 0 && pattern[i] != pattern[j]) j = table[j];
			if (pattern[i + 1] == pattern[++j]) table[i + 1] = table[j];
			else table[i + 1] = j;
		}
	}
	void search(const string& text, vector<int>& res) {
		int head = 0, j = 0, tlen = (int)text.size();
		while (head + j < tlen) {
			if (pattern[j] == text[head + j]) {
				if (++j != plen) continue;
				res.push_back(head);
			}
			head += j - table[j], j = max(table[j], 0LL);
		}
	}
};

bool ok[500010],checked[500010];
int score[500010];

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

	string s, t;

	cin >> s >> t;

	string ts;
	while (SZ(ts) < SZ(t) + SZ(s) - 1)
	{
		ts += s;
	}

	vector<int> mps;
	KMP kmp(t);
	kmp.search(ts, mps);

	for (auto& p : mps)
	{
		if (SZ(s) <= p)break;
		ok[p] = true;
		//cout << p << endl;
	}

	int ans = 0;
	rep(i, 0, SZ(s))
	{
		if (checked[i])continue;
		checked[i] = true;
		int p = i;
		int res = 0;
		while (ok[p])
		{
			res++;
			p = (p + SZ(t)) % SZ(s);

			if (p == i)
			{
				res = INF;
				break;
			}
			if (score[p] != 0)
			{
				res += score[p];
				break;
			}
			checked[p] = true;
		}
		chmax(ans, res);
		score[i] = res;
	}

	if (ans == INF)
	{
		cout << -1 << endl;
	}
	else
	{
		cout << ans << endl;
	}

	return 0;
}

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

public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int n = scanner.nextInt(), m = scanner.nextInt(), q = scanner.nextInt();
			// 町と町の間の移動最短距離
			int[][] d = new int[n][n];
			IntStream.range(0, n).forEach(i -> {
				Arrays.fill(d[i], Integer.MAX_VALUE >> 1);
				d[i][i] = 0;
			});
			int[] r = new int[q];
			IntStream.range(0, q).forEach(i -> r[i] = scanner.nextInt() - 1);
			IntStream.range(0, m).forEach(i -> {
				int a = scanner.nextInt() - 1;
				int b = scanner.nextInt() - 1;
				int c = scanner.nextInt();
				d[a][b] = d[b][a] = c;
			});
			warshallFloyd(d, n);
			System.out.println(getResult(d, r, -1, new boolean[q], 1));
		}
	}

	/**
	 * 町と町の間の移動最短距離をワーシャル–フロイド法で計算する
	 * 
	 * @param d 町と町の間の移動最短距離の配列
	 * @param n 町の数
	 */
	private static void warshallFloyd(int[][] d, int n) {
		IntStream.range(0, n).forEach(k -> IntStream.range(0, n)
				.forEach(j -> IntStream.range(0, n).forEach(i -> d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]))));
	}

	/**
	 * 解説通りに実装したメソッド
	 * 
	 * @param d       町と町の最短距離の配列
	 * @param r       訪ねる町の配列
	 * @param prev    直前に訪ねた町（存在しないときは-1）
	 * @param visited 町が訪ねたかどうかの配列
	 * @param depth   訪ねた町の数
	 * @return 訪ねる町をすべて訪ねたときの移動距離
	 */
	private static long getResult(final int[][] d, final int[] r, int prev, boolean[] visited, int depth) {
		if (depth > r.length) {
			return 0L;
		}
		long result = Integer.MAX_VALUE;
		for (int i = 0; i < r.length; i++) {
			if (!visited[i]) {
				visited[i] = true;
				if (-1 == prev) {
					result = Math.min(result, getResult(d, r, i, visited, depth + 1));
				} else {
					result = Math.min(result, getResult(d, r, i, visited, depth + 1) + d[r[prev]][r[i]]);
				}
				visited[i] = false;
			}
		}
		return result;
	}
}


Measured memory_bytes: 46636
#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
#include<cmath>
#include<bitset>
typedef long long ll;
#define PB	push_back
#define FOR(n)			for(int i = 0; i < n; ++i)
#define RFOR(n)			for(int i = n; i >= 0; --i)
#define FORR(start,end) for(int i = start; i < end; ++i)
#define COUT(n)			cout << n << " " << flush
#define vi(v)			vector<int> v
#define vii(v)			vector<pair<int,int>> v
#define pii(p)			pair<int,int> p
#define mii(m)			map<int, int> m
#define msi(m)			map<string, int, less<>> m
#define mmsi(m)			multimap<string, int, less<>> m
#define mkp(a,b)		make_pair(a,b)
#define ALL(obj)		obj.begin(), obj.end()
#define PI 3.141592
#define MOD 1e9+7
using namespace std;

void solve() {
	int n;
	map<string, int> poll;
	vector<string> str;
	cin >> n;
	
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		if (poll.find(s) != poll.end()) {
			poll.find(s)->second += 1;
		}
		else {
			poll[s];
		}
	}

	int max = poll.begin()->second;
	for (auto iter : poll) {
		if (iter.second > max)
			max = iter.second;
	}

	for (auto iter : poll) {
		if (iter.second == max)
			str.push_back(iter.first);
	}
	sort(ALL(str));

	for (auto s : str) {
		cout << s << endl;
	}
}

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

Measured memory_bytes: 26868
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int [] arr = new int [11000];
		long sum = 0;
		int max = -1000001;
		int min =  1000001;

		for(int i = 0;i<num;i++){
			arr[i]=sc.nextInt();
			if(arr[i]<min) min=arr[i];
			if(max<arr[i]) max=arr[i];
			sum = (long)arr[i]+sum;
		}
		
				
			
		
	    System.out.println(min+" "+max+" "+sum);
	}
}

Measured memory_bytes: 41744
n, m = gets.split.map(&:to_i)
mod = 1_000_000_007
dp = Array.new(n + 2, 0)
a = []
dp[0] = 1
m.times do |i|
  a[i] = gets.to_i
  dp[a[i]] = -1
end
n.times do |i|
  next if dp[i] == -1
  if dp[i + 1] != -1
    dp[i + 1] += dp[i]
    dp[i + 1] %= mod
  end
  if dp[i + 2] != -1
    dp[i + 2] += dp[i]
    dp[i + 2] %= mod
  end
end
puts dp[n]

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

int n, ans, vis[8], a[3] = {3, 5,7};

void dfs(ll x) {
    if(x > n) {
        return;
    }
    if(vis[3] && vis[5] && vis[7]) {
        ans++;
    }
    rep(i, 3) {
        vis[a[i]]++;
        dfs(10*x+a[i]);
        vis[a[i]]--;
    }
}

int main() {
    cin >> n;
    dfs(0);
    cout << ans;

    cout << endl;
    return 0;
}

    

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<cmath>
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define forl(i,a,b) for (ll i = a; i < b; i++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<int,int> pi;
int main() {
    ll r;
    cin>>r;
    cout<<2.0*3.14*r<<endl;
    

	return 0;
}


Measured memory_bytes: 3792
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define ll long long

int main(void)
{
    signed int a1,a2;
    scanf("%d%d",&a1,&a2);
    if(a1>a2)
        printf("a > b\n");
    else if(a2>a1)
        printf("a < b\n");
    else
        printf("a == b\n");
    return 0;
}




Measured memory_bytes: 2096
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
using namespace __gnu_pbds; 
#define mod 998244353 
#define int long long
#define ll long long
#define pb push_back
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define inp(n) for(i=0;i<n;i++) cin>>A[i];
#define inz(A) memset(A,0,sizeof(A))
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define T 1000005
#define F first
#define S second
#define pi pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define vp vector<pair< int,int> >
#define mpi map<pair<int,int> ,int>
#define PI M_PI
#define lower(s) transform(all(s),s.begin(),::tolower)
#define endl "\n"
typedef tree<int, null_type, less<int>, rb_tree_tag, 
         tree_order_statistics_node_update> pbds;
int add(int a, int b) {a=(a%mod + b%mod)%mod;return a;}
int sub(int a, int b) {a =(a%mod - b%mod + mod)%mod ;return a;}
int mul(int a, int b) {return (a%mod * b%mod) % mod;}

int dp[3001][3001];

 int power(int a,int x){a%=mod;
              int res=1;
		while(x>0){
		if(x&1)
		res=res*a%mod;
		a=a*a%mod;
                    x>>=1;  }
		return res;}
void solve()
{ 
  int n,s,i,j;
  cin >> n >> s;int A[n+5];
  for(i=1;i<=n;i++)cin >> A[i];

  dp[0][0]=power(2,n)%mod;;
  for(i=1;i<=n;i++)
  {
    for(j=0;j<=s;j++)
    {
      dp[i][j]=dp[i-1][j];
      if(A[i]<=j)
      dp[i][j]+=((dp[i-1][j-A[i]])* power(2,mod-2))%mod ;
      dp[i][j]%=mod;
    }
    //cout << endl;
  }
  cout<<dp[n][s]%mod;
}

signed main()
{ boost
  int test=0,t;
    //cin >> t ;
    t=1;
   while(test++ <t){
	  //cout<<"Case #"<<test<<": ";
	   solve();}
  
}

Measured memory_bytes: 73908
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <regex>
#include <numeric>
using namespace std;

/*--- Macro ---*/
#define rep(i, n) for(int i=0; i<(int)n; i++)
#define rep1(i, n) for(int i=1; i<(int)n; i++)
#define mp make_pair
#define pb push_back

/*---  Type ---*/
typedef long long ll;

/*--- Constants ---*/
template <typename T> const T INF = numeric_limits<T>::max() / 10;
const int MOD = (int)1e9 + 7;

// Floating
const double EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(x, y) (abs(a-b) < EPS)

// Util
int max(int a, int b, int c) {return max(a, max(b, c));}
int min(int a, int b, int c) {return min(a, min(b, c));}
ll max(ll x, ll y) {return x > y ? x : y;}
ll min(ll x, ll y) {return x < y ? x : y;}
ll max(ll a, ll b, ll c) {return max(a, max(b, c));}
ll min(ll a, ll b, ll c) {return min(a, min(b, c));}

// -------------------------------------------------------------------------------------



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

  vector<int> a(n);
  rep(i, n) cin >> a[i];

  // レート3200未満について
  int grade_n = 0;
  rep(grade, 8) {
    rep(i, n) {
      if(a[i]/400 == grade) {
        grade_n++;
        break;
      }
    }
  }

  // レート3200以上について
  int colorful = 0;
  rep(i, n) {
    if(a[i] >= 3200) colorful++;
  }

  cout << max(1, grade_n) << " " << grade_n + colorful << endl;

  return 0;
}

Measured memory_bytes: 256
loop do
  m, n = gets.split.map(&:to_i)
  break if m == 0
  pos = []
  ret = Array.new(m){|i|i+1}
  (1..n).map{|x|a=[[:Fizz][x%3],[:Buzz][x%5]]*'';a==''?x.to_s: a}.zip(n.times.map{gets.chomp}).map.with_index{|(a,b),i| i if a!=b}.compact.unshift(0).each_cons(2).with_index{|(a,b),i| pos << (b-a)%(m-i)}
    pos[0,m-1].reduce(0){|s,t| ret.slice!(t=(s+t)%ret.size); t-1}
    puts ret.join(' ')
end

Measured memory_bytes: 14228
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 605;

int n, dp[MAXN][MAXN]; char g[MAXN][MAXN];

int main(){
	int i, j, ans = 0;
	scanf("%d", &n);
	for(i = 0; i < n; ++ i){
		scanf("%s", g[i]);
		for(j = 0; j < n; ++ j)
			g[i][j + n] = g[i][j];
	}
	for(i = 0; i < n; ++ i)
		for(j = 0; j < 2 * n; ++ j)
			g[n + i][j] = g[i][j];
	for(i = 2 * n - 1; i >= 0; -- i)
		for(j = 2 * n - 1; j >= 0; -- j){
			int &v = dp[i][j], lim = dp[i + 1][j + 1];
			for(v = 0; v <= lim && i + v < 2 * n && j + v < 2 * n; ++ v)
				if(g[i + v][j] != g[i][j + v])
					break;
		}
	for(i = 0; i < n; ++ i)
		for(j = 0; j < n; ++ j)
			if(dp[i][j] >= n)
				++ ans;
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 2048
#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 200009;
const int MOD = 1e9 + 7;
int main(){
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false),cout.tie(0),cin.tie(0);
  int n;cin>>n;
  int64_t sum=0;
  map<int,int64_t> mp;
  for(int i=0;i<n;++i){
    int a;cin>>a;
    sum+=a;
    mp[a]++;
  }
  int q;cin>>q;
  while(q--){
    int b,c;cin>>b>>c;
    if(!mp.count(b)||mp[b]==0){
      cout<<sum<<'\n';
    }else{
      sum-=mp[b]*b;
      sum+=mp[b]*c;
      mp[c]+=mp[b];
      mp[b]=0;
      cout<<sum<<"\n";
    }
  }
  
  return 0;
}
//2020.06.23 18:09:19


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

#define repi(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(x) (x).begin(), (x).end()

struct UF {
  int n;
  vector< int > par;
  UF(int n) : n(n), par(n, -1) {}
  int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
  int size(int x) { return -par[find(x)]; }
  bool same(int a, int b) { return find(a) == find(b); }
  void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if(a == b) return;
    if(par[a] < par[b]) swap(a, b);
    par[b] += par[a];
    par[a] = b;
  }
  void clear() { par.assign(n, -1); }
};

int main() {
  int n,k,l,p,q,r,s;
  cin>>n>>k>>l;

  UF uf1(n), uf2(n);

  rep(i, k) {
    cin >> p >> q;
    uf1.unite(p-1, q-1);
  }

  rep(i, l) {
    cin >> r >>s;
    uf2.unite(r-1,s-1);
  }

  map<pair<int, int>, int> m;

  rep(i, n) {
    m[make_pair(uf1.find(i), uf2.find(i))]++;
  }

  rep(i, n) {
    cout << m[make_pair(uf1.find(i), uf2.find(i))];
    if(i+1==n)
      cout << endl;
    else cout << " ";
  }
}


Measured memory_bytes: 13568
//
//  main.cpp
//  A
//
//  Created by Hashizo on 2019/11/05.
//  Copyright © 2019 Hashizo. All rights reserved.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <regex>
#include <cmath>

using namespace std;
 
#undef LONG_MAX
#undef LONG_MIN
 
#define LONG_MAX    9223372036854775807
#define LONG_MIN    (-LONG_MAX  -1)
 
#define ll long long
 
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
 
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

int main(int argc, const char * argv[]) {

	ll r;
	cin >> r;

	cout << r*r << endl;
	return 0;
}


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

using namespace std;
vector<int> adj[100000];
bool visited[100000]={};
int h[100000];
int ans = 0;
void dfs(int here){
    visited[here] = true;
    bool check = true;
    for(auto i: adj[here]){
        if(!visited[i]){
            dfs(i);
        }
        if(h[i]>=h[here]) check = false;
    }
    if(check){
        ans++;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin>>n>>m;
    int a,b;
    for(int i=0; i<n; i++) cin>>h[i];
    for(int i=0; i<m; i++){
        cin>>a>>b;
        adj[a-1].push_back(b-1);
        adj[b-1].push_back(a-1);
    }
    for(int i=0; i<n; i++){
        if(!visited[i]){
            dfs(i);
        }
    }
    cout<<ans;
    return 0;
}

Measured memory_bytes: 10864
class Vertex:
	def __init__(self):
		self.c = 0
		self.vertices = []
	def add(self, u):
		self.vertices.append(u)

def dfs(vertices):
	c = 0
	for i in range(len(vertices)):
		c += 1
		if vertices[i].c == 0:
			vertices[i].c = c
			stack = []
			stack.append(i)
			while len(stack) != 0:
				cur = vertices[stack.pop()]
				for j in range(len(cur.vertices)):
					u = cur.vertices[j]
					if vertices[u].c == 0:
						vertices[u].c = c
						stack.append(u)
	return

n, m = map(int, input().split())
vertices = []
for i in range(n):
	vertices.append(Vertex())
for i in range(m):
	u1, u2 = map(int, input().split())
	vertices[u1].add(u2)
	vertices[u2].add(u1)

dfs(vertices)

q = int(input())
for i in range(q):
	u1, u2 = map(int, input().split())
	if vertices[u1].c == vertices[u2].c:
		print('yes')
	else:
		print('no')



Measured memory_bytes: 39040
s = input()
t = input()
n,m = len(s),len(t)
dp = [[0]*3010 for i in range(3010)]
for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1)
        dp[i+1][j+1] = max(dp[i+1][j+1],dp[i+1][j])
        dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j+1])
#復元パート
res = ""
i,j = n,m
while(i>0 and j>0):
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -=1
    else:
        res = s[i-1]+res
        i -=1
        j -=1
print(res)


Measured memory_bytes: 115164
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	REP(i, v.size()) { if (i)os << " "; os << v[i]; }return os;
}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
	REP(i, v.size()) { if (i)os << endl; os << v[i]; }return os;
}

const ll INF = 1LL << 60;
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

#define int long long

inline void my_io() {

	std::ios::sync_with_stdio(false);

	std::cin.tie(0);

	cout << fixed << setprecision(10);

}

signed main() {
	ll n, k;
	ll ans = 0;
	ll sum = 0;
	
	cin >> n >> k;

	vector<pair<ll, ll>> v(n, make_pair(0, 0));
	set<ll> s;
	stack<ll> st;

	REP(i, n) {
		cin >> v[i].second >> v[i].first;
	}

	sort(v.begin(), v.end(), greater<pair<ll, ll>>());

	REP(i, k) {
		sum += v[i].first;
		if (s.count(v[i].second)) {
			st.push(v[i].first);
		}
		s.insert(v[i].second);
	}

	ans += sum + s.size() * s.size();

	FOR(i, k, n) {
		if (st.empty()) {
			break;
		}
		if (s.count(v[i].second)) {
			continue;
		}
		sum -= st.top();
		sum += v[i].first;
		s.insert(v[i].second);
		ans = max(ans, sum + (ll)s.size() * (ll)s.size());
		st.pop();
	}

	cout << ans << endl;
}

Measured memory_bytes: 4992
N=int(input())
a=list(map(int,input().split()))
def u(x,y):
    if x%y==0:
        return y
    else:
        return u(y,x%y)
b=list(range(N))
b[0]=a[0]
for i in range(1,N):
    b[i]=u(b[i-1],a[i])
print(int(b[N-1]))

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

int main(){
    int N ; cin >> N;
    int a = N % 10;
    int b = ((N-a) % 100) / 10; 
    int c = (N-a-10*b) / 100;
    if (a == 7 or b == 7 or c == 7) cout << "Yes" << endl;
    else cout << "No" << endl;
}



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

int d[5555][5555];
int main() {
	int n, m = 0;
	string s;
	cin >> n >> s;
	for (int i = n; i--;) {
		for (int j = n; --j > i;) {
			if (s[i] == s[j])
				d[i][j] = min(d[i + 1][j + 1] + 1, j - i);
			m = max(m, d[i][j]);
		}
	}
	cout << m << endl;
	return 0;
}


Measured memory_bytes: 108928
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> P;
#define int long long
#define override_rep(i, l, r, mes, ...) mes
#define rep1(i, n) for (int i = 0; i < n; i++)
#define rep2(i, l, r) for (int i = l; i < r; i++)
#define rep(...) override_rep(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#ifdef DEBUG
std::ostream& dout = cout;
#else
stringstream dout;
#endif
template<typename T, typename U>
istream& operator>>(istream& in, pair<T, U>& p) {
    in >> p.first >> p.second;
    return in;
}
signed main(signed argc, char* argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    const int MOD = 1000000007;
    int n;
    cin>>n;
    int a=(n+2)/3,b=(n-a+1)/2,c=n-a-b;
    vector<vector<int>> graph(n);
    rep(i,n-1){
        int in1,in2;
        cin>>in1>>in2;
        graph[in1-1].push_back(in2-1);
        graph[in2-1].push_back(in1-1);
    }
    vector<int> ans(n,-1),is_visit(n);
    ans[0]=3*a-2;a--;
    stack<P> st;
    int A=0,B=0;
    st.push({0,0});
    while(!st.empty()){
        P it=st.top();st.pop();
        is_visit[it.first]=it.second%2;
        if(it.second%2==0)A++;
        else B++;
        rep(i,graph[it.first].size()){
            int next=graph[it.first][i];
            if(ans[next]!=-1)continue;
            if(it.second%2==0){
                if(b==0){
                    ans[next]=3*c;
                    c--;
                }
                else{
                    ans[next]=3*b-1;
                    b--;
                }
            }
            else{
                if(a==0){
                    ans[next]=3*c;
                    c--;
                }
                else{
                    ans[next]=3*a-2;
                    a--;
                }
            }
            st.push({next,it.second+1});
        }
    }
    if(B*2<A){
        a=(n+2)/3,b=(n-a+1)/2,c=n-a-b;
        rep(i,n){
            if(is_visit[i]%2==1){
                cout<<c*3;
                c--;
            }
            else{
                if(a!=0){
                    cout<<a*3-2;
                    a--;
                }
                else if(b!=0){
                    cout<<b*3-1;
                    b--;
                }
                else{
                    cout<<c*3;
                    c--;
                }
            }
            if(i+1!=n)cout<<' ';
            else cout<<endl;
        }
    }
    else if(A*2<B){
        a=(n+2)/3,b=(n-a+1)/2,c=n-a-b;
        rep(i,n){
            if(is_visit[i]%2==0){
                cout<<c*3;
                c--;
            }
            else{
                if(a!=0){
                    cout<<a*3-2;
                    a--;
                }
                else if(b!=0){
                    cout<<b*3-1;
                    b--;
                }
                else{
                    cout<<c*3;
                    c--;
                }
            }
            if(i+1!=n)cout<<' ';
            else cout<<endl;
        }
    }else {
		rep(i, n) {
			cout << ans[i];
            if(i+1!=n)cout<<' ';
			if (i + 1 == n) cout << endl;
		}
	}
}


Measured memory_bytes: 19176