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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;

const ll MOD = 998244353;
const ll INF = 1e+9;
const ld PI = acos((long double)(-1));

#define no "no"
#define No "No"
#define NO "NO"
#define yes "yes"
#define Yes "Yes"
#define YES "YES"
#define next '\\n'
#define sp " "
#define print(x) cout << (x) << endl;
#define rep(i, n) for (int(i) = (0); (i) < (n); (++i))
#define FOR(i, start, end) for (int(i) = (start); (i) < (end); (++i))
#define foreach(elem, _list) for (auto &(elem) : (_list))
#define view(_list)                                                            \\
  foreach (e, _list) {                                                         \\
    cout << e << sp;                                                           \\
  }                                                                            \\
  cout << next
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define size(s) (s).size()
#define ALL(vec) (vec).begin(), (vec).end()
#define pb(e) push_back(e)
#define EPS 0.00000001

int main() {
  FAST;

  ll n, q, sum = 0;
  cin >> n;
  vector<ll> a(100010, 0);
  rep(i, n) {
    ll e;
    cin >> e;
    a[e]++;
    sum += e;
  }

  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;

    sum -= a[b] * b;
    sum += a[b] * c;
    a[c] += a[b];
    a[b] = 0;

    print(sum);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;

const ll MOD = 998244353;
const ll INF = 1e+9;
const ld PI = acos((long double)(-1));

#define no "no"
#define No "No"
#define NO "NO"
#define yes "yes"
#define Yes "Yes"
#define YES "YES"
#define next '\\n'
#define sp " "
#define print(x) cout << (x) << endl;
#define rep(i, n) for (int(i) = (0); (i) < (n); (++i))
#define FOR(i, start, end) for (int(i) = (start); (i) < (end); (++i))
#define foreach(elem, _list) for (auto &(elem) : (_list))
#define view(_list)                                                            \\
  foreach (e, _list) {                                                         \\
    cout << e << sp;                                                           \\
  }                                                                            \\
  cout << next
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define size(s) (s).size()
#define ALL(vec) (vec).begin(), (vec).end()
#define pb(e) push_back(e)
#define EPS 0.00000001

int main() {
  FAST;

  int n, q, sum = 0;
  cin >> n;
  vector<int> a(n + 10, 0);
  rep(i, n) {
    int e;
    cin >> e;
    a[e]++;
    sum += e;
  }

  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;

    sum -= a[b] * b;
    sum -= a[c] * c;
    a[c] += a[b];
    a[b] = 0;
    sum += a[c] * c;

    print(sum);
  }
}