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

/*
 ▓█████▄  ▄████▄  ▓██   ██▓ ██▓███   ██░ ██ ▓█████  ██▀███
▒██▀ ██▌▒██▀ ▀█   ▒██  ██▒▓██░  ██▒▓██░ ██▒▓█   ▀ ▓██ ▒ ██▒
 ░██   █▌▒▓█    ▄   ▒██ ██░▓██░ ██▓▒▒██▀▀██░▒███   ▓██ ░▄█ ▒
 ░▓█▄   ▌▒▓▓▄ ▄██▒  ░ ▐██▓░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█  ▄ ▒██▀▀█▄
 ░▒████▓ ▒ ▓███▀ ░  ░ ██▒▓░▒██▒ ░  ░░▓█▒░██▓░▒████▒░██▓ ▒██▒
  ▒▒▓  ▒ ░ ░▒ ▒  ░   ██▒▒▒ ▒▓▒░ ░  ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░
  ░ ▒  ▒   ░  ▒    ▓██ ░▒░ ░▒ ░      ▒ ░▒░ ░ ░ ░  ░  ░▒ ░ ▒░
  ░ ░  ░ ░         ▒ ▒ ░░  ░░        ░  ░░ ░   ░     ░░   ░
    ░    ░ ░       ░ ░               ░  ░  ░   ░  ░   ░
  ░      ░         ░ ░
*/
// focus less on how much problem you should solve within a fixed time and focus
// more on getting the problem solved and learning something from it
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll unsigned long long
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define sedes set<ll, greater<ll>>
#define seasc set<ll>
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map<ll, ll>
#define map map<ll, ll>
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define mems(a, i) memset(a, i, sizeof(a))
#define endl '\\n'
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define printx(x) printf("%lld\\n", x);
#define printxy(x, y) cout << x << y << endl;
#define printxyz(x, y, z) cout << x << y << z << endl;
#define deba(a) cout << #a << a << endl;
#define debab(a, b) cout << #a << a << #b << b << endl;
int main() {
  fast;
  ll n, q, sum = 0;
  cin >> n;
  ll a[n + 1];
  ll h = 100010;
  vi ele(h + 1, 0);
  rep(i, 0, n) cin >> a[i], sum += a[i], ele[a[i]]++;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    sum = sum - ele[b] * b;
    sum += ele[b] * c;
    ele[c] += ele[b];
    ele[b] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

/*
 ▓█████▄  ▄████▄  ▓██   ██▓ ██▓███   ██░ ██ ▓█████  ██▀███
▒██▀ ██▌▒██▀ ▀█   ▒██  ██▒▓██░  ██▒▓██░ ██▒▓█   ▀ ▓██ ▒ ██▒
 ░██   █▌▒▓█    ▄   ▒██ ██░▓██░ ██▓▒▒██▀▀██░▒███   ▓██ ░▄█ ▒
 ░▓█▄   ▌▒▓▓▄ ▄██▒  ░ ▐██▓░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█  ▄ ▒██▀▀█▄
 ░▒████▓ ▒ ▓███▀ ░  ░ ██▒▓░▒██▒ ░  ░░▓█▒░██▓░▒████▒░██▓ ▒██▒
  ▒▒▓  ▒ ░ ░▒ ▒  ░   ██▒▒▒ ▒▓▒░ ░  ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░
  ░ ▒  ▒   ░  ▒    ▓██ ░▒░ ░▒ ░      ▒ ░▒░ ░ ░ ░  ░  ░▒ ░ ▒░
  ░ ░  ░ ░         ▒ ▒ ░░  ░░        ░  ░░ ░   ░     ░░   ░
    ░    ░ ░       ░ ░               ░  ░  ░   ░  ░   ░
  ░      ░         ░ ░
*/
// focus less on how much problem you should solve within a fixed time and focus
// more on getting the problem solved and learning something from it
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ll unsigned long long
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define sedes set<ll, greater<ll>>
#define seasc set<ll>
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map<ll, ll>
#define map map<ll, ll>
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define mems(a, i) memset(a, i, sizeof(a))
#define endl '\\n'
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define printx(x) printf("%lld\\n", x);
#define printxy(x, y) cout << x << y << endl;
#define printxyz(x, y, z) cout << x << y << z << endl;
#define deba(a) cout << #a << a << endl;
#define debab(a, b) cout << #a << a << #b << b << endl;
int main() {
  fast;
  ll n, q, sum = 0;
  cin >> n;
  ll a[n];
  vi ele(n + 1, 0);
  rep(i, 0, n) cin >> a[i], sum += a[i], ele[a[i]]++;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    sum = sum - ele[b] * b;
    sum += ele[b] * c;
    ele[c] += ele[b];
    ele[b] = 0;
    cout << sum << endl;
  }
  return 0;
}