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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (int(i) = (int)(a); (i) < (int)(n); ++(i))
#define FORe(i, a, n) for (int(i) = (int)(a); (i) <= (int)(n); ++(i))
#define rFOR(i, a, n) for (int(i) = (int)(n)-1; (i) >= (int)(a); --(i))
#define rFORe(i, a, n) for (int(i) = (int)(n); (i) >= (int)(a); --(i))
#define REP(i, n) FOR((i), 0, (n))
#define REPe(i, n) FORe((i), 1, (n))
#define REPo(i, n) FORe((i), 0, (n))
#define rREP(i, n) rFOR((i), 0, (n))
#define rREPe(i, n) rFORe((i), 1, (n))
#define rREPo(i, n) rFORe((i), 0, (n))
#define rep(i, n) FOR((i), 0, (n))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fi first
#define se second
#define dup(x, y) (((x) + (y)-1) / (y))
#define vc(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using T = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
using vp = vector<P>;
using vt = vector<T>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  vi cnt(100005);
  REP(i, n) cnt[a[i]]++;
  ll ans = 0;
  REP(i, 100005) { ans += i * cnt[i]; }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ll x = (c - b) * cnt[b];
    ans += x;
    cout << ans << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, n) for (int(i) = (int)(a); (i) < (int)(n); ++(i))
#define FORe(i, a, n) for (int(i) = (int)(a); (i) <= (int)(n); ++(i))
#define rFOR(i, a, n) for (int(i) = (int)(n)-1; (i) >= (int)(a); --(i))
#define rFORe(i, a, n) for (int(i) = (int)(n); (i) >= (int)(a); --(i))
#define REP(i, n) FOR((i), 0, (n))
#define REPe(i, n) FORe((i), 1, (n))
#define REPo(i, n) FORe((i), 0, (n))
#define rREP(i, n) rFOR((i), 0, (n))
#define rREPe(i, n) rFORe((i), 1, (n))
#define rREPo(i, n) rFORe((i), 0, (n))
#define rep(i, n) FOR((i), 0, (n))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fi first
#define se second
#define dup(x, y) (((x) + (y)-1) / (y))
#define vc(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using T = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
using vp = vector<P>;
using vt = vector<T>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  vi cnt(10005);
  REP(i, n) cnt[a[i]]++;
  ll ans = 0;
  REP(i, 10005) { ans += i * cnt[i]; }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ll x = (c - b) * cnt[b];
    ans += x;
    cout << ans << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }

  return 0;
}