Problem p02630 (C++) — buggy submission, verdict: Runtime Error

// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

template <typename T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &i : a)
    out << i << " ";
  return out;
}
template <typename T, typename U> void chkmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chkmax(T &a, U b) {
  if (a < b)
    a = b;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  vector<int> cnt(1e5 + 2);
  forn(i, 0, n) {
    int x;
    cin >> x;
    cnt[x]++;
  }
  long long s = 0;
  forn(i, 0, sz(cnt)) s += 1LL * i * cnt[i];
  int q;
  cin >> q;
  while (q-- > 0) {
    int b, c;
    cin >> b >> c;
    s -= 1LL * b * cnt[b];
    s += 1LL * c * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << s << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

template <typename T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &i : a)
    out << i << " ";
  return out;
}
template <typename T, typename U> void chkmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chkmax(T &a, U b) {
  if (a < b)
    a = b;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  vector<int> cnt(n + 1);
  forn(i, 0, n) {
    int x;
    cin >> x;
    cnt[x]++;
  }
  long long s = 0;
  forn(i, 0, n + 1) s += 1LL * i * cnt[i];
  int q;
  cin >> q;
  while (q-- > 0) {
    int b, c;
    cin >> b >> c;
    s -= 1LL * b * cnt[b];
    s += 1LL * c * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << s << "\\n";
  }
  return 0;
}