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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int mod = 1000000007;
bool comp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.second == b.second)
    return a.first > b.first;
  else
    return b.second < a.second;
}
int main() {
  fast ll n, q, b, c;
  cin >> n;
  ll a[n + 1];
  vector<ll> cnt(100001, 0);

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  ll sum = 0;
  for (ll i = 0; i < n; i++) {

    sum += a[i];
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    cin >> b >> c;
    ll s = cnt[b];
    ll p = cnt[c];
    sum = sum - (s * b) + (c * s);
    cnt[b] = 0;
    cnt[c] = s + p;

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int mod = 1000000007;
bool comp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.second == b.second)
    return a.first > b.first;
  else
    return b.second < a.second;
}
int main() {
  fast ll n, q, b, c;
  cin >> n;
  ll a[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
      }
      sum += a[i];
    }
    cout << sum << endl;
  }

  return 0;
}