Language: C++

#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
//#define mp make_pair
#define pb push_back
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007
#define F first
#define S second
#define pii pair<int,int>
#define rep(i,n) for(int i=0;i<n;i++)
bool isPowerOfTwo(int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int power(int x, unsigned int y)
{
    int res = 1;

    x = x;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x);

        y = y>>1;
        x = (x*x);
    }
    return res;
}
bool isprime(int x)
{
      if(x<2)
        return false;
    for(int i=2;i*i<=x;i++)
     {
        if (x%i==0)
            return false;
    }
    return true;
}
vector<int>primeFactors(int n)
{
    vector<int>v;
    int x=0;
    while (n % 2 == 0)
    {
        x++;
        n = n/2;
        v.pb(2);
    }
    //if(x>0)
    //v.pb({2,x});
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        int y=0;
        while (n % i == 0)
        {
            y++;
            n = n/i;
            v.pb(i);
        }
        //if(y>0)
        //v.pb({i,y});
    }
    if (n > 2)
        v.pb(n);
    return v;
}

vector<int>leastPrimeFactor(int n)
{
    vector<int>least_prime(n);
    // Create a vector to store least primes.
    // Initialize all entries as 0.
    // We need to print 1 for 1.
    least_prime[1] = 1;

    for (int i = 2; i <= n; i++)
    {
        // least_prime[i] == 0
        // means it i is prime
        if (least_prime[i] == 0)
        {
            // marking the prime number
            // as its own lpf
            least_prime[i] = i;

            // mark it as a divisor for all its
            // multiples if not already marked
            for (int j = 2*i; j <= n; j += i)
                if (least_prime[j] == 0)
                   least_prime[j] = i;
        }
    }
    return least_prime;
}

void solve()
{
int n;
cin>>n;
int a[n];
map<int,int>m;
int s=0;
for(int i=0;i<n;i++)
{
    cin>>a[i];
    s+=a[i];
    m[a[i]]++;
}
int q;
cin>>q;
while(q--)
{
    int x,y;
    cin>>x>>y;
    int z=m[x];
    s-=z*x;
    s+=z*y;
    m[y]+=z;
    m[x]=0;
    cout<<s<<endl;
}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;
//cin>>t;
t=1;
while(t--)
{
solve();
}
}

#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
//#define mp make_pair
#define pb push_back
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007
#define F first
#define S second
#define pii pair<int,int>
#define rep(i,n) for(int i=0;i<n;i++)
bool isPowerOfTwo(int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int power(int x, unsigned int y)
{
    int res = 1;

    x = x;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x);

        y = y>>1;
        x = (x*x);
    }
    return res;
}
bool isprime(int x)
{
      if(x<2)
        return false;
    for(int i=2;i*i<=x;i++)
     {
        if (x%i==0)
            return false;
    }
    return true;
}
vector<int>primeFactors(int n)
{
    vector<int>v;
    int x=0;
    while (n % 2 == 0)
    {
        x++;
        n = n/2;
        v.pb(2);
    }
    //if(x>0)
    //v.pb({2,x});
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        int y=0;
        while (n % i == 0)
        {
            y++;
            n = n/i;
            v.pb(i);
        }
        //if(y>0)
        //v.pb({i,y});
    }
    if (n > 2)
        v.pb(n);
    return v;
}

vector<int>leastPrimeFactor(int n)
{
    vector<int>least_prime(n);
    // Create a vector to store least primes.
    // Initialize all entries as 0.
    // We need to print 1 for 1.
    least_prime[1] = 1;

    for (int i = 2; i <= n; i++)
    {
        // least_prime[i] == 0
        // means it i is prime
        if (least_prime[i] == 0)
        {
            // marking the prime number
            // as its own lpf
            least_prime[i] = i;

            // mark it as a divisor for all its
            // multiples if not already marked
            for (int j = 2*i; j <= n; j += i)
                if (least_prime[j] == 0)
                   least_prime[j] = i;
        }
    }
    return least_prime;
}

void solve()
{
int n;
cin>>n;
int a[n];
map<int,int>m;
int s=0;
for(int i=0;i<n;i++)
{
    cin>>a[i];
    s+=a[i];
    m[a[i]]++;
}
int q;
cin>>q;
while(q--)
{
    int x,y;
    cin>>x>>y;
    int z=m[x];
    s-=z*x;
    s+=z*y;
    m[y]+=z;
    m[x]=0;
    cout<<s<<endl;
}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;
//cin>>t;
t=1;
while(t--)
{
solve();
}
}



Compiler output:
a.cc:155:6: error: redefinition of 'bool isPowerOfTwo(long long int)'
  155 | bool isPowerOfTwo(int n)
      |      ^~~~~~~~~~~~
a.cc:14:6: note: 'bool isPowerOfTwo(long long int)' previously defined here
   14 | bool isPowerOfTwo(int n)
      |      ^~~~~~~~~~~~
a.cc:162:5: error: redefinition of 'long long int power(long long int, long long unsigned int)'
  162 | int power(int x, unsigned int y)
      |     ^~~~~
a.cc:21:5: note: 'long long int power(long long int, long long unsigned int)' previously defined here
   21 | int power(int x, unsigned int y)
      |     ^~~~~
a.cc:177:6: error: redefinition of 'bool isprime(long long int)'
  177 | bool isprime(int x)
      |      ^~~~~~~
a.cc:36:6: note: 'bool isprime(long long int)' previously defined here
   36 | bool isprime(int x)
      |      ^~~~~~~
a.cc:188:12: error: redefinition of 'std::vector<long long int> primeFactors(long long int)'
  188 | vector<int>primeFactors(int n)
      |            ^~~~~~~~~~~~
a.cc:47:12: note: 'std::vector<long long int> primeFactors(long long int)' previously defined here
   47 | vector<int>primeFactors(int n)
      |            ^~~~~~~~~~~~
a.cc:217:12: error: redefinition of 'std::vector<long long int> leastPrimeFactor(long long int)'
  217 | vector<int>leastPrimeFactor(int n)
      |            ^~~~~~~~~~~~~~~~
a.cc:76:12: note: 'std::vector<long long int> leastPrimeFactor(long long int)' previously defined here
   76 | vector<int>leastPrimeFactor(int n)
      |            ^~~~~~~~~~~~~~~~
a.cc:245:6: error: redefinition of 'void solve()'
  245 | void solve()
      |      ^~~~~
a.cc:104:6: note: 'void solve()' previously defined here
  104 | void solve()
      |      ^~~~~
a.cc:272:8: error: redefinition of 'int main()'
  272 | signed main()
      |        ^~~~
a.cc:131:8: note: 'int main()' previously defined here
  131 | signed main()
      |        ^~~~