Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define drrep(i, cc, n) for (int i = (cc); i >= (n); --i)
#define rrep(i, n) drrep(i, 0, n - 1)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \
  rep(i, v.size()) if (i == 0) cout << v[i];                                   \
  else cout << " " << v[i];                                                    \
  cout << endl;
#define arrprint(v, n)                                                         \
  rep(i, n) if (i == 0) cout << v[i];                                          \
  else cout << " " << v[i];                                                    \
  cout << endl;
#define matprint(v, n, m)                                                      \
  rep(i, n) {                                                                  \
    rep(j, m) { cout << v[i][j] << " "; }                                      \
    cout << endl;                                                              \
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  rep(i, q) cin >> b[i] >> c[i];
  vector<int> cnt(n + 1);
  ll ans = 0;
  rep(i, n) {
    cnt[a[i]]++;
    ans += a[i];
  }
  rep(i, q) {
    ll minus = (ll)b[i] * cnt[b[i]];
    ll add = (ll)c[i] * cnt[b[i]];
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
    ans = ans + add - minus;
    cout << ans << endl;
  }

  // cout << ans << endl;
  //  printf("%.10f\n",ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define drrep(i, cc, n) for (int i = (cc); i >= (n); --i)
#define rrep(i, n) drrep(i, 0, n - 1)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \
  rep(i, v.size()) if (i == 0) cout << v[i];                                   \
  else cout << " " << v[i];                                                    \
  cout << endl;
#define arrprint(v, n)                                                         \
  rep(i, n) if (i == 0) cout << v[i];                                          \
  else cout << " " << v[i];                                                    \
  cout << endl;
#define matprint(v, n, m)                                                      \
  rep(i, n) {                                                                  \
    rep(j, m) { cout << v[i][j] << " "; }                                      \
    cout << endl;                                                              \
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  rep(i, q) cin >> b[i] >> c[i];
  vector<int> cnt(100005);
  ll ans = 0;
  rep(i, n) {
    cnt[a[i]]++;
    ans += a[i];
  }
  rep(i, q) {
    ll minus = (ll)b[i] * cnt[b[i]];
    ll add = (ll)c[i] * cnt[b[i]];
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
    ans = ans + add - minus;
    cout << ans << endl;
  }

  // cout << ans << endl;
  //  printf("%.10f\n",ans);

  return 0;
}