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

/*
#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define rev reverse
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define endl '\\n'

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 1;
const ll P = 31;
const double PI = acos(-1.0);

ll a[N], cnt[N];
int main() {
  //  freopen(    "angle2.in", "r", stdin);
  //  freopen("angle2.out", "w", stdout);
  IOS ll n;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  ll q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    ll x, y;
    cin >> x >> y;
    sum -= cnt[x] * x;
    cnt[y] += cnt[x];

    sum += cnt[x] * y;
    cnt[x] = 0;
    cout << sum << endl;
  }
  return 0;
}
// assert(condition) -> if (condition == false) Runtime Error
// order_of_key(x) -> number of elements smaller than x
// *find_by_order(k) -> k-th element in 0-based indexation


Error: TLE

Fixed submission:

/*
#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define rev reverse
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define endl '\\n'

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 1;
const ll P = 31;
const double PI = acos(-1.0);

ll a[N];
int main() {
  //  freopen(    "angle2.in", "r", stdin);
  //  freopen("angle2.out", "w", stdout);
  IOS ll n;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    ll x, y;
    cin >> x >> y;
    for (int j = 1; j <= n; j++) {
      if (a[j] == x) {
        sum -= a[j];
        a[j] = y;
        sum += a[j];
      }
    }
    cout << sum << endl;
  }
  return 0;
}
// assert(condition) -> if (condition == false) Runtime Error
// order_of_key(x) -> number of elements smaller than x
// *find_by_order(k) -> k-th element in 0-based indexation