Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
Hey there stalker....

█▀▀▄░░░░░░░░░░░▄▀▀█
░█░░░▀▄░▄▄▄▄▄░▄▀░░░█
░░▀▄░░░▀░░░░░▀░░░▄▀
░░░░▌░▄▄░░░▄▄░▐▀▀
░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█
░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█
▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█
█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀
░▀▄░░▀░░▀▀▀░░▀░░░▄█▀
░░░█░░░░░░░░░░░▄▀▄░▀▄
░░░█░░░░░░░░░▄▀█░░█░░█
░░░█░░░░░░░░░░░█▄█░░▄▀
░░░█░░░░░░░░░░░████▀
░░░▀▄▄▀▀▄▄▀▀▄▄▄█▀

Like what you see?
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define pii pair<ll, ll>
#define vi vector<ll>
#define present(container, element)                                            \\
  (container.find(element) != container.end()) /* For set n maps */
#define cpresent(container, element)                                           \\
  (find(all(container), element) != container.end()) /* For vectors */
#define all(a) (a).begin(), (a).end()
#define invect(data, n, commands)                                              \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.pb(tmp);                                                              \\
    commands                                                                   \\
  }
#define inset(data, n, commands)                                               \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.I(tmp);                                                               \\
    commands                                                                   \\
  }
#define F first
#define S second
#define I insert
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define trav(a, x) for (auto &a : x)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define lcase(a) (char)tolower(a)
#define ll long long int
#define lld long double
#define endl "\\n"
#define PI 3.14159265358979323844
#define MOD 1000000007
#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mod 1e9 + 7
/*	For Debugging	*/
#define DEBUG cerr << "\\n>>>I'm Here<<<\\n" << endl;
#define display(x)                                                             \\
  trav(a, x) cout << a << " ";                                                 \\
  cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;

int main() {
  IOS

      int TESTS = 1;
  //  cin >> TESTS;
  while (TESTS--) {
    ll n, sum = 0;
    cin >> n;
    vi v(n);
    ll count[100005] = {0};
    rep(i, 0, n) {
      cin >> v[i];
      count[v[i]]++;
      sum += v[i];
    }

    ll q;
    cin >> q;
    while (q--) {
      ll b, c;
      cin >> b >> c;

      sum += (c - b) * count[b];
      count[c] += count[b];
      count[b] = 0;

      cout << sum << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
Hey there stalker....

█▀▀▄░░░░░░░░░░░▄▀▀█
░█░░░▀▄░▄▄▄▄▄░▄▀░░░█
░░▀▄░░░▀░░░░░▀░░░▄▀
░░░░▌░▄▄░░░▄▄░▐▀▀
░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█
░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█
▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█
█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀
░▀▄░░▀░░▀▀▀░░▀░░░▄█▀
░░░█░░░░░░░░░░░▄▀▄░▀▄
░░░█░░░░░░░░░▄▀█░░█░░█
░░░█░░░░░░░░░░░█▄█░░▄▀
░░░█░░░░░░░░░░░████▀
░░░▀▄▄▀▀▄▄▀▀▄▄▄█▀

Like what you see?
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define pii pair<ll, ll>
#define vi vector<ll>
#define present(container, element)                                            \\
  (container.find(element) != container.end()) /* For set n maps */
#define cpresent(container, element)                                           \\
  (find(all(container), element) != container.end()) /* For vectors */
#define all(a) (a).begin(), (a).end()
#define invect(data, n, commands)                                              \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.pb(tmp);                                                              \\
    commands                                                                   \\
  }
#define inset(data, n, commands)                                               \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.I(tmp);                                                               \\
    commands                                                                   \\
  }
#define F first
#define S second
#define I insert
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define trav(a, x) for (auto &a : x)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define lcase(a) (char)tolower(a)
#define ll long long int
#define lld long double
#define endl "\\n"
#define PI 3.14159265358979323844
#define MOD 1000000007
#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mod 1e9 + 7
/*	For Debugging	*/
#define DEBUG cerr << "\\n>>>I'm Here<<<\\n" << endl;
#define display(x)                                                             \\
  trav(a, x) cout << a << " ";                                                 \\
  cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;

int main() {
  IOS

      int TESTS = 1;
  //  cin >> TESTS;
  while (TESTS--) {
    ll n;
    cin >> n;
    vi v(n);
    rep(i, 0, n) { cin >> v[i]; }
    ll q;
    cin >> q;
    while (q--) {
      ll x, y, sum = 0;
      cin >> x >> y;
      for (int i = 0; i < n; i++) {
        if (v[i] == x) {
          v[i] = y;
        }
        sum += v[i];
      }
      cout << sum << endl;
    }
  }
  return 0;
}