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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
#define ll long long
#define int long long
#define INF 1000005
#define ar array
#define fr first
#define sc second
#define wt(x)                                                                  \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define watch(x) cerr << "\\n" << (#x) << " is " << (x) << endl
// #define cerr if(false)cerr
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
}

int power(int a, int b, int mod) {
  a = a % mod;
  if (a == 0) {
    return 1;
  }
  int res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    b >>= 1;
    a = (a * a) % mod;
  }
  return res;
}

int MMI(int p) { return power(p, MOD - 2, MOD); }
struct BIT {
  int N;
  vector<int> bit;
  void init(int n) {
    N = n;
    bit.assign(n + 1, 0);
  }
  void update(int idx, int i) {
    while (idx <= N) {
      bit[idx] = i;
      idx += idx & (-idx);
    }
  }
  int sum(int idx) {
    int ans = 0;
    while (idx > 0) {
      ans += bit[idx];
      idx -= idx & (-idx);
    }
    return ans;
  }
  int prefsum(int l, int r) { return sum(r) - sum(l - 1); }
};
const int SIZE = 1 << 20;
int cnt[SIZE];
int32_t main() {
  c_p_c();
  int n;
  cin >> n;
  int ans = 0;

  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    cnt[x]++;
    ans += x;
  }

  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ans -= cnt[b] * b;
    ans += cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;

    cout << ans << "\\n";
  }

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
#define ll long long
#define int long long
#define INF 1000005
#define ar array
#define fr first
#define sc second
#define wt(x)                                                                  \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define watch(x) cerr << "\\n" << (#x) << " is " << (x) << endl
// #define cerr if(false)cerr
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
}

int power(int a, int b, int mod) {
  a = a % mod;
  if (a == 0) {
    return 1;
  }
  int res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    b >>= 1;
    a = (a * a) % mod;
  }
  return res;
}

int MMI(int p) { return power(p, MOD - 2, MOD); }
struct BIT {
  int N;
  vector<int> bit;
  void init(int n) {
    N = n;
    bit.assign(n + 1, 0);
  }
  void update(int idx, int i) {
    while (idx <= N) {
      bit[idx] = i;
      idx += idx & (-idx);
    }
  }
  int sum(int idx) {
    int ans = 0;
    while (idx > 0) {
      ans += bit[idx];
      idx -= idx & (-idx);
    }
    return ans;
  }
  int prefsum(int l, int r) { return sum(r) - sum(l - 1); }
};

int32_t main() {
  c_p_c();
  int n;
  cin >> n;
  int ans = 0;
  vector<int> cnt(n + 1);
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    cnt[x]++;
    ans += x;
  }

  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ans -= cnt[b] * b;
    ans += cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;

    cout << ans << "\\n";
  }

  return 0;
}