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

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define size(v) v.size()

const int N = 100005;
const int MOD = 1e9 + 7;

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (int)clock() / (int)CLOCKS_PER_SEC << "ms\\n"
int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* stuff you should look for
   * int overflow, array bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   */

  int n;
  cin >> n;
  int count[N] = {0};
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    count[x]++;
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    sum += (b - a) * count[a];
    count[b] += count[a];
    count[a] = 0;
    cout << sum << " ";
  }

  printclock;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long
#define pb push_back
#define all(v) v.begin(), v.end()
const int N = 100005;
const int MOD = 1e9 + 7;

/*
// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" |
"<<#z<<": "<<z<<endl #define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" |
"<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl #define trace5(a,
b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" |
"<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
*/
int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* stuff you should look for
   * int overflow, array bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   */
  int n;
  cin >> n;
  multiset<int> s;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    s.insert(x);
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    while (s.find(a) != s.end()) {

      s.erase(s.find(a));
      s.insert(b);
      sum += b - a;
    }
    cout << sum << endl;
  }
}