Problem p02630 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
------------------------------------------CAUTIOUS WARNING
------------------------------------------ This is a personalised Template for
Nitin under MIT Licence Act 1840 : Do not use it for own use. */

// AUTHOR Nitin
//  TASK : Practice
//--------------------------------HEADERS-------------------------------------------------------------//
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

//-------------------------------------------------------Typedefs----------------------------------------------//

typedef long long int ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef pair<string, string> pss;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<pii> vii;

typedef vector<ll> vl;

typedef vector<vl> vvl;

double EPS = 1e-9;

int INF = 1000000005;

long long INFF = 1000000000000000005LL;

double PI = acos(-1);

int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};

int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};

//---------------------------------------------------------Hash
//defines--------------------------------------------------//

#define PII 3.14159265358979323846
#ifdef TESTING
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG
#define VALUE(x)
#define debug(...)
#endif

#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto &(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define T(a)                                                                   \
  ll a;                                                                        \
  cin >> a;
#define TT(t)                                                                  \
  T(t);                                                                        \
  while (t--)
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define f(i, x, y) for (int i = x; i < y; i++)
#define array(a) ll a[n];
#define line "\n";
#define out cout
#define meh <<
//-------------------------------------------------TEMPLATES----------------------------------------------------------//

template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); }
template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); }
void SieveOfEratosthenes(ll n) // O(nloglogn)
{
  ll prime[n];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
bool isPrime(ll n) // O(sqrt(n))
{
  if (n < 2)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
std::vector<ll> generatePrimeFactors(ll n) {
  std::vector<ll> prime;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      prime.pb(i);
      while (n % i == 0)
        n = n / i;
    }
  }
  if (n != 1)
    prime.pb(n);
  return prime;
}
std::vector<ll> generateFactors(ll n) {
  std::vector<ll> fact;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      fact.pb(i);
      if (n / i != i)
        fact.pb(n / i);
      // 24 - 1,2,3,4,6,8,12
    }
  }
  fact.pb(1);
  if (n != 1)
    fact.pb(n);
  sort(fact.begin(), fact.end());
  return fact;
}

ll extendedGCD(ll a, ll b, ll &x, ll &y) {
  // produces correct results for negative integers as well
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1, d;
  d = extendedGCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
ll gcd(ll a, ll b) // O(log min(a,b))
{
  /*
  recursive implementation below
  if(!b) return a;
  return gcd(b,a%b);
  */

  // non-recursive implementation below
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

ll multiply(ll a, ll b, ll m);
ll singlemod(ll a, ll m);
ll modpow(ll x, ll n, ll m) // O(logn)// used for calculating (x^n)%m
{
  if (n == 0)
    return 1;
  ll res = 1;
  while (n) {
    if (n % 2)
      res = multiply(res, x, m);
    x = multiply(x, x, m);
    n = n >> 1;
  }
  return singlemod(res, m);
}
ll fastpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = 1;
  while (n) {
    if (n % 2)
      res = res * x;
    x = x * x;
    n = n >> 1;
  }
  return res;
}
ll modinverse(ll x, ll m) // O(logn)
{
  return modpow(x, m - 2, m);
}
ll add(ll a, ll b, ll m) { return (((a % m) + (b % m)) % m); }
ll substract(ll a, ll b, ll m) {
  //    if (a < b)
  //        swap(a, b);
  return (((a % m) - (b % m) + m) % m);
}
ll multiply(ll a, ll b, ll m) { return (((a % m) * (b % m)) % m); }
ll divide(ll a, ll b, ll m) {
  ll temp = modinverse(b, m);
  return multiply(a, temp, m);
}
bool isprime(ll n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
ll singlemod(ll a, ll m) { return (a % m); }
ll eulerstotientfunction(ll n) // O(sqrt(n))
{
  ll ans = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n = n / i;
      ans -= ans / i;
    }
  }
  if (n > 1)
    ans -= ans / n;
  return ans;
}

ll ncr(ll n, ll k, ll m) {
  if (k > n)
    return 0;
  if (m == 0) {
    ll res = 1;
    k = min(k, n - k);
    for (ll i = 1; i <= k; i++) {
      res *= (n - i + 1);
      res /= i;
    }
    return res;
  } else {
    ll res = 1;
    k = min(k, n - k);
    for (ll i = 1; i <= k; i++) {
      res = multiply(res, n - i + 1, m);
      res = divide(res, i, m);
    }
    return singlemod(res, m);
  }
}
ll kadaneAlgo(const std::vector<ll> &arr) {
  ll size = arr.size();
  ll currmax = arr[0], maxsofar = arr[0];
  for (ll i = 1; i < size; i++) {
    // debug2(currmax,maxsofar);
    currmax = max(arr[i], currmax + arr[i]);
    maxsofar = max(maxsofar, currmax);
  }
  return maxsofar;
}
ll getDigitSum(ll n) {
  ll co = 0;
  while (n > 0) {
    co += n % 10;
    n = n / 10;
  }
  return co;
}

template <class T> void printVector(const std::vector<T> arr) {
  for (auto i : arr)
    cout << i << " ";
  cout << endl;
}
template <class T> void printArray(T arr[], ll n) {
  for (ll i = 0; i < n; i++)
    cout << arr[i] << " ";
  cout << endl;
}
// const int p1 = 13331,m1 = 1e9+9,p2 = 7919, m2 = 1e9+9;
// const int p1 = 97266353,m1 = 972663749,p2 = 97336357, m2 = 1000000007;
// const int p1 = 31 ,m1 = 1e9+7;

// grid cells
// D U R L
ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, 1, -1};

bool is_perfect(int x) {
  long double y = sqrt(x);
  return ((y - floor(y)) == 0);
}
// this is a practice header done during Practice session --

ll nofdigit(ll n) {
  // formula used number of digits in N = log10(N) + 1 .//O(1)
  return floor(log10(n) + 1);
}
ll gp_lastterm(ll a1, ll n, ll r) { return a1 * pow(r, n - 1); }
ll gp_sum(ll a1, ll n, ll r) {
  if (r > 1)
    return (a1 * (pow(r, n) - 1)) / (r - 1);
  else
    return (a1 * (1 - pow(r, n))) / (1 - r);
}
//------------------------------------------------------------------------------------------------------------

void wiley()

{
  T(n)
  array(a) REP(i, n) cin >> a[i];
  ll sum = 0;
  T(q)
  while (q--) {
    T(b)
    T(c)
    REP(i, n) {
      if (a[i] == b) {
        a[i] = c;
      }
    }
    REP(i, n) { sum = sum + a[i]; }
    cout << sum meh line sum = 0;
  }
}

int main() {
  // Fast I/O --- Vroom Vroom
  //	IOS
  ////	TT(t){

  //	read();

  wiley();
  // By Order of Peaky Blinders.
  //	}
  return 0;
}


Fixed submission:
/*
------------------------------------------CAUTIOUS WARNING
------------------------------------------ This is a personalised Template for
Nitin under MIT Licence Act 1840 : Do not use it for own use. */

// AUTHOR Nitin
//  TASK : Practice
//--------------------------------HEADERS-------------------------------------------------------------//
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

//-------------------------------------------------------Typedefs----------------------------------------------//

typedef long long int ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef pair<string, string> pss;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<pii> vii;

typedef vector<ll> vl;

typedef vector<vl> vvl;

double EPS = 1e-9;

int INF = 1000000005;

long long INFF = 1000000000000000005LL;

double PI = acos(-1);

int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};

int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};

//---------------------------------------------------------Hash
//defines--------------------------------------------------//

#define PII 3.14159265358979323846
#ifdef TESTING
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG
#define VALUE(x)
#define debug(...)
#endif

#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto &(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define T(a)                                                                   \
  ll a;                                                                        \
  cin >> a;
#define TT(t)                                                                  \
  T(t);                                                                        \
  while (t--)
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define f(i, x, y) for (int i = x; i < y; i++)
#define array(a) ll a[n];
#define line "\n";
#define out cout
#define meh <<
//-------------------------------------------------TEMPLATES----------------------------------------------------------//

template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); }
template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); }
void SieveOfEratosthenes(ll n) // O(nloglogn)
{
  ll prime[n];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
bool isPrime(ll n) // O(sqrt(n))
{
  if (n < 2)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
std::vector<ll> generatePrimeFactors(ll n) {
  std::vector<ll> prime;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      prime.pb(i);
      while (n % i == 0)
        n = n / i;
    }
  }
  if (n != 1)
    prime.pb(n);
  return prime;
}
std::vector<ll> generateFactors(ll n) {
  std::vector<ll> fact;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      fact.pb(i);
      if (n / i != i)
        fact.pb(n / i);
      // 24 - 1,2,3,4,6,8,12
    }
  }
  fact.pb(1);
  if (n != 1)
    fact.pb(n);
  sort(fact.begin(), fact.end());
  return fact;
}

ll extendedGCD(ll a, ll b, ll &x, ll &y) {
  // produces correct results for negative integers as well
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1, d;
  d = extendedGCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
ll gcd(ll a, ll b) // O(log min(a,b))
{
  /*
  recursive implementation below
  if(!b) return a;
  return gcd(b,a%b);
  */

  // non-recursive implementation below
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

ll multiply(ll a, ll b, ll m);
ll singlemod(ll a, ll m);
ll modpow(ll x, ll n, ll m) // O(logn)// used for calculating (x^n)%m
{
  if (n == 0)
    return 1;
  ll res = 1;
  while (n) {
    if (n % 2)
      res = multiply(res, x, m);
    x = multiply(x, x, m);
    n = n >> 1;
  }
  return singlemod(res, m);
}
ll fastpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = 1;
  while (n) {
    if (n % 2)
      res = res * x;
    x = x * x;
    n = n >> 1;
  }
  return res;
}
ll modinverse(ll x, ll m) // O(logn)
{
  return modpow(x, m - 2, m);
}
ll add(ll a, ll b, ll m) { return (((a % m) + (b % m)) % m); }
ll substract(ll a, ll b, ll m) {
  //    if (a < b)
  //        swap(a, b);
  return (((a % m) - (b % m) + m) % m);
}
ll multiply(ll a, ll b, ll m) { return (((a % m) * (b % m)) % m); }
ll divide(ll a, ll b, ll m) {
  ll temp = modinverse(b, m);
  return multiply(a, temp, m);
}
bool isprime(ll n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
ll singlemod(ll a, ll m) { return (a % m); }
ll eulerstotientfunction(ll n) // O(sqrt(n))
{
  ll ans = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n = n / i;
      ans -= ans / i;
    }
  }
  if (n > 1)
    ans -= ans / n;
  return ans;
}

ll ncr(ll n, ll k, ll m) {
  if (k > n)
    return 0;
  if (m == 0) {
    ll res = 1;
    k = min(k, n - k);
    for (ll i = 1; i <= k; i++) {
      res *= (n - i + 1);
      res /= i;
    }
    return res;
  } else {
    ll res = 1;
    k = min(k, n - k);
    for (ll i = 1; i <= k; i++) {
      res = multiply(res, n - i + 1, m);
      res = divide(res, i, m);
    }
    return singlemod(res, m);
  }
}
ll kadaneAlgo(const std::vector<ll> &arr) {
  ll size = arr.size();
  ll currmax = arr[0], maxsofar = arr[0];
  for (ll i = 1; i < size; i++) {
    // debug2(currmax,maxsofar);
    currmax = max(arr[i], currmax + arr[i]);
    maxsofar = max(maxsofar, currmax);
  }
  return maxsofar;
}
ll getDigitSum(ll n) {
  ll co = 0;
  while (n > 0) {
    co += n % 10;
    n = n / 10;
  }
  return co;
}

template <class T> void printVector(const std::vector<T> arr) {
  for (auto i : arr)
    cout << i << " ";
  cout << endl;
}
template <class T> void printArray(T arr[], ll n) {
  for (ll i = 0; i < n; i++)
    cout << arr[i] << " ";
  cout << endl;
}
// const int p1 = 13331,m1 = 1e9+9,p2 = 7919, m2 = 1e9+9;
// const int p1 = 97266353,m1 = 972663749,p2 = 97336357, m2 = 1000000007;
// const int p1 = 31 ,m1 = 1e9+7;

// grid cells
// D U R L
ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, 1, -1};

bool is_perfect(int x) {
  long double y = sqrt(x);
  return ((y - floor(y)) == 0);
}
// this is a practice header done during Practice session --

ll nofdigit(ll n) {
  // formula used number of digits in N = log10(N) + 1 .//O(1)
  return floor(log10(n) + 1);
}
ll gp_lastterm(ll a1, ll n, ll r) { return a1 * pow(r, n - 1); }
ll gp_sum(ll a1, ll n, ll r) {
  if (r > 1)
    return (a1 * (pow(r, n) - 1)) / (r - 1);
  else
    return (a1 * (1 - pow(r, n))) / (1 - r);
}
//------------------------------------------------------------------------------------------------------------

void wiley()

{
  map<int, int> mp;
  ll n, sum = 0, x, y;
  cin >> n;
  ll a[n];
  REP(i, n) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  REP(i, q) {
    cin >> x >> y;
    mp[y] += mp[x];
    sum -= (mp[x] * x);
    sum += (mp[x] * y);
    mp[x] = 0;
    cout << sum << endl;
  }
}

int main() {
  // Fast I/O --- Vroom Vroom
  //	IOS
  ////	TT(t){

  //	read();

  wiley();
  // By Order of Peaky Blinders.
  //	}
  return 0;
}