Problem p02630 (C++)

Buggy submission (status: Runtime Error):
// noobie_corleone
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ld long double

#define ss second
#define ff first

#define fr(i, a, b) for (ll i = a; i < b; i++)
#define f(i, n) fr(i, 0, n)
#define rf(i, b, a) for (ll i = b - 1; i >= a; i--)
#define r(i, n) rf(i, n, 0)

#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof(c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())

#define pi pair<int, int>
#define pll pair<ll, ll>
#define pd pair<ld, ld>
#define endl '\n'

#define PI                                                                     \
  3.141592653589793238462643383279502884197169399375105820974944592307816406286
#define mod 1000000007
#define inf 1000000007
#define eps 0.000000000000001

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vvi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// vector<int>
// adj[200010],value(200010),par(200010),vis(200010,0),dis(200010,0),g(200010,0),col(200010,-1);
// int myrandom (int i) { return std::rand()%i;}

int main() {
  ll n;
  cin >> n;
  vl a(n), cnt(10001, 0);
  ll s = 0;
  f(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    s += a[i];
  }
  ll q;
  cin >> q;
  vl b(q), c(q);
  f(i, q) {
    cin >> b[i] >> c[i];
    s -= (cnt[b[i]] * b[i]);
    s += (cnt[b[i]] * c[i]);
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
    cout << s << endl;
  }
  return 0;
}


Fixed submission:
// noobie_corleone
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ld long double

#define ss second
#define ff first

#define fr(i, a, b) for (ll i = a; i < b; i++)
#define f(i, n) fr(i, 0, n)
#define rf(i, b, a) for (ll i = b - 1; i >= a; i--)
#define r(i, n) rf(i, n, 0)

#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof(c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())

#define pi pair<int, int>
#define pll pair<ll, ll>
#define pd pair<ld, ld>
#define endl '\n'

#define PI                                                                     \
  3.141592653589793238462643383279502884197169399375105820974944592307816406286
#define mod 1000000007
#define inf 1000000007
#define eps 0.000000000000001

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vvi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// vector<int>
// adj[200010],value(200010),par(200010),vis(200010,0),dis(200010,0),g(200010,0),col(200010,-1);
// int myrandom (int i) { return std::rand()%i;}

int main() {
  ll n;
  cin >> n;
  vl a(n), cnt(100001, 0);
  ll s = 0;
  f(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    s += a[i];
  }
  ll q;
  cin >> q;
  vl b(q), c(q);
  f(i, q) {
    cin >> b[i] >> c[i];
    s -= (cnt[b[i]] * b[i]);
    s += (cnt[b[i]] * c[i]);
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
    cout << s << endl;
  }
  return 0;
}