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

#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define multis multiset<int, greater<int>> gquiz1;
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

void ayushmehta651() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  ayushmehta651();
  int n;
  cin >> n;
  int a[n];
  vector<int> freq(100069, 0);
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    freq[a[i]]++;
    sum += a[i];
  }

  int q;
  cin >> q;

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

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

    freq[c] += freq[b];
    freq[b] = 0;

    cout << sum << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define multis multiset<int, greater<int>> gquiz1;
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

void ayushmehta651() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
}

int32_t main() {
  ayushmehta651();
  int n;
  cin >> n;
  int a[n];
  vi v;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    v.pb(a[i]);
  }
  int q;
  cin >> q;

  while (q--) {
    int b, c;
    cin >> b >> c;
    int sum = 0;

    replace(v.begin(), v.end(), b, c);

    for (auto it3 : v)
      sum += it3;

    cout << sum << '\\n';
  }
  return 0;
}