alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long lli;

int main() {
  lli n, t, p, p1, p2, q = 0, c;
  cin >> n;
  lli a[100000] = {0};
  vector<lli> v;
  for (lli i = 0; i < n; i++) {
    cin >> p;
    v.push_back(p);
    a[p - 1]++;
    q = q + p;
  }
  cin >> t;
  for (lli i = 0; i < t; i++) {
    cin >> p1 >> p2;
    c = a[p1 - 1];
    a[p1 - 1] = a[p1 - 1] - c;
    a[p2 - 1] = a[p2 - 1] + c;
    q = (q - (c * p1)) + (c * p2);
    cout << q << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long lli;

int main() {
  lli n, t, p, p1, p2, q = 0, c;
  cin >> n;
  vector<lli> v;
  for (lli i = 0; i < n; i++) {
    cin >> p;
    v.push_back(p);
    q = q + p;
  }
  cin >> t;
  for (lli i = 0; i < t; i++) {
    cin >> p1 >> p2;
    c = count(v.begin(), v.end(), p1);
    replace(v.begin(), v.end(), p1, p2);
    q = (q - (c * p1)) + (c * p2);
    cout << q << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, q, a, b, c;
  long long sum = 0;
  vector<int> A(100001, 0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    A[a]++;
    sum += a;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &b, &c);
    sum = sum + A[b] * (c - b);
    A[c] += A[b];
    A[b] = 0;
    printf("%lld\\n", sum);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, q, a, b, c;
  long long ans;
  vector<int> A(100001, 0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    A[a]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &b, &c);
    A[c] += A[b];
    A[b] = 0;
    ans = 0;
    for (int i = 0; i < 100001; i++) {
      ans = ans + A[i] * i;
    }
    printf("%lld\\n", ans);
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> cntMemo(100010, 0);
  ll sum = 0;
  rep(i, n) {
    int a;
    cin >> a;
    cntMemo[a]++;
    sum += (ll)a;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (ll)cntMemo[b] * (c - b);
    cntMemo[c] += cntMemo[b];
    cntMemo[b] = 0;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> cntMemo(n + 1, 0);
  ll sum = 0;
  rep(i, n) {
    int a;
    cin >> a;
    cntMemo[a]++;
    sum += (ll)a;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (ll)cntMemo[b] * (c - b);
    cntMemo[c] += cntMemo[b];
    cntMemo[b] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define vec(j, n) vector<int>(j(n))
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n;
  vector<long long> s(100009);
  rep(i, n) {
    int a;
    cin >> a;
    s[a]++;
  }
  cin >> q;
  ll sum = 0;
  rep(i, 100009) { sum += i * s[i]; }

  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum -= b * s[b];
    sum -= c * s[c];
    s[c] += s[b];
    s[b] = 0;
    sum += b * s[b];
    sum += c * s[c];
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define vec(j, n) vector<int>(j(n))
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a.at(i); }
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  rep(i, q) {
    cin >> b.at(i);
    cin >> c.at(i);
  }
  vector<long long> s(q);
  rep(i, q) {
    long long sum = 0;
    rep(j, n) {
      if (b.at(i) == a.at(j)) {
        a.at(j) = c.at(i);
      }
    }

    sum = accumulate(a.begin(), a.end(), 0);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#pragma target("avx")
#pragma optimize("O3")
#include <bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T> using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i, b, e) for (ll i = b; i < e; i++)
#define ALL(vec) vec.begin(), vec.end()
#define PRINT(vec)                                                             \\
  printf("[ ");                                                                \\
  for (auto &i : vec)                                                          \\
    printf("%ld ", i);                                                         \\
  puts("]");
#define SCAN(vec)                                                              \\
  for (auto &i : vec)                                                          \\
  scanf("%ld", &i)
#define fi first
#define se second

const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main() {
  int n;
  scanf("%d", &n);
  ll cnt[100010] = {}, ans = 0;
  REP(i, 0, n) {
    int a;
    scanf("%d", &a);
    cnt[a]++;
    ans += a;
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    ans += cnt[b] * (c - b);
    printf("%ld\\n", ans);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma target("avx")
#pragma optimize("O3")
#include <bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T> using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i, b, e) for (ll i = b; i < e; i++)
#define ALL(vec) vec.begin(), vec.end()
#define PRINT(vec)                                                             \\
  printf("[ ");                                                                \\
  for (auto &i : vec)                                                          \\
    printf("%ld ", i);                                                         \\
  puts("]");
#define SCAN(vec)                                                              \\
  for (auto &i : vec)                                                          \\
  scanf("%ld", &i)
#define fi first
#define se second

const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main() {
  int n;
  scanf("%d", &n);
  ll cnt[10010] = {}, ans = 0;
  REP(i, 0, n) {
    int a;
    scanf("%d", &a);
    cnt[a]++;
    ans += a;
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    ans += cnt[b] * (c - b);
    printf("%ld\\n", ans);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define int long long

using namespace std;

const int N = 1e5 + 10;

int a[N], cnt[N];

signed main() {
  int n;
  cin >> n;
  int res = 0;
  for (int i = 0; i < n; ++i)
    cin >> a[i], cnt[a[i]]++, res += a[i];
  int q;
  cin >> q;

  while (q--) {
    int b, c;
    cin >> b >> c;
    res += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << res << '\\n';
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int a[N];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int q;
  cin >> q;

  while (q--) {
    int b, c;
    cin >> b >> c;
    for (int i = 0; i < n; ++i) {
      if (a[i] == b)
        a[i] = c;
    }
    int res = 0;
    for (int i = 0; i < n; ++i)
      res += a[i];
    cout << res << '\\n';
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long int> A(999991, 0);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A[a] += 1;
    ans += a;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ans -= b * A[b];
    ans += c * A[b];
    A[c] += A[b];
    A[b] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  multiset<int> A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.insert(a);
  }
  int q;
  cin >> q;
  long long int ans = 0;
  for (auto i : A) {
    ans += i;
  }
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    auto k = A.find(b);
    int ln = 0;
    for (; k != A.end(); k++) {
      if (*k != b)
        break;
      ans -= b;
      ans += c;
      ln++;
    }
    A.erase(b);
    for (int j = 0; j < ln; j++) {
      A.insert(c);
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define no cout << "NO" << endl;
#define pb push_back
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i < n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a; i >= b; i--)
#define mod 1000000007
#define gcd(x, y) __gcd(x, y)
using namespace std;
map<ll, ll> mp;
int main() {
  ll n;
  cin >> n;
  ll a[n], i;
  ll sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    sum += (y - x) * mp[x];
    mp[y] += mp[x];
    mp[x] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define no cout << "NO" << endl;
#define pb push_back
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i < n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a; i >= b; i--)
#define mod 1000000007
#define gcd(x, y) __gcd(x, y)
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll sum = 0;
    ll x, y;
    cin >> x >> y;
    for (i = 0; i < n; i++) {
      // sum+=a[i];
      if (a[i] == x) {
        // sum-=a[i];
        a[i] = y;
        sum += a[i];
        // sum-=x;
        //	cout<<a[i]<<endl;
      } else
        sum += a[i];
      //	cout<<sum<<endl;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

const int MX = 100005;

int main() {
  int n, q;
  cin >> n;
  vector<ll> cnt(MX);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll tot = 0;
  rep(i, MX) tot += i * cnt[i];

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

    tot -= b * cnt[b];
    tot -= c * cnt[c];

    cnt[c] += cnt[b];
    cnt[b] = 0;

    tot += c * cnt[c];

    cout << tot << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  cin >> q;

  sort(a.begin(), a.end());

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

    int ans = 0;
    rep(i, n) {
      if (a[i] == b)
        a[i] = c;
      ans += a[i];
    }

    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<long long> count(100001);
  int N;
  long long s = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    count.at(a)++;
    s += a;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    s -= (B - C) * count[B];
    count[C] += count[B];
    count[B] = 0;
    cout << s << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  int sum = 0;
  for (int s : A) {
    sum += s;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    for (int i = 0; i < N; i++) {
      if (A.at(i) != B)
        continue;
      sum -= A.at(i) - C;
      A.at(i) = C;
    }

    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  int b[q];
  int c[q];
  rep(i, q) cin >> b[i] >> c[i];
  vector<int> num(100001);
  ll sum = 0;
  rep(i, n) {
    num[a[i]]++;
    sum += a[i];
  }
  rep(i, q) {
    sum += (c[i] - b[i]) * num[b[i]];
    num[c[i]] += num[b[i]];
    num[b[i]] = 0;
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  int b[q];
  int c[q];
  rep(i, q) cin >> b[i] >> c[i];
  vector<int> sum(n);

  rep(i, q) {
    rep(j, n) {
      if (a[j] == b[i]) {
        a[j] = c[i];
      }
      sum[i] += a[j];
    }
  }
  rep(i, q) cout << sum[i] << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

// ABC171

#include <algorithm>
#include <climits>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <random>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  long long int n, q, buf, buf_1, buf_2, sum = 0;
  cin >> n;
  vector<long long int> vv(100000, 0), l;
  for (int i = 0; i < n; i++) {
    cin >> buf;
    vv.at(buf - 1)++;
    sum += buf;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> buf_1 >> buf_2;
    sum += vv.at(buf_1 - 1) * (buf_2 - buf_1);
    vv.at(buf_2 - 1) += vv.at(buf_1 - 1);
    vv.at(buf_1 - 1) = 0;
    l.push_back(sum);
  }
  for (auto n : l) {
    cout << n << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)


Fixed submission:

// ABC171

#include <algorithm>
#include <climits>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <random>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, q, max = 0, buf, buf_1, buf_2;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> buf;
    v.at(i) = buf;
    if (buf > max)
      max = buf;
  }
  vector<int> vv(max, 0);
  for (auto itr = v.begin(); itr != v.end(); itr++) {
    vv.at(*itr)++;
  }
  long int sum = accumulate(v.begin(), v.end(), 0);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> buf_1 >> buf_2;
    sum = sum - vv.at(buf_1) * (buf_2 - buf_1);
    vv.at(buf_2) += vv.at(buf_1);
    vv.at(buf_1) = 0;
    cout << sum << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define ll long long
using namespace std;
vector<int> a(100005);
void solve() {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += (ll)x;
    a[x]++;
  }
  int q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    sum -= (a[b] * b);
    sum += (a[b] * c);
    a[c] += a[b];
    a[b] = 0;
    cout << sum << "\\n";
  }
}
int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 2];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int sum = 0;
    int b, c;
    cin >> b >> c;
    for (int j = 0; j < n; j++) {
      if (b == a[j]) {
        a[j] = c;
        sum += a[j];
      } else {
        sum += a[j];
      }
    }
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
const int limit = 100000;
int a[limit], b[limit], c[limit], num[limit + 1];
int main(void) {
  int N, Q;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  cin >> Q;
  for (int i = 0; i < Q; i++)
    cin >> b[i] >> c[i];
  long long S = 0;
  for (int i = 0; i < N; i++) {
    S += a[i];
    num[a[i]]++;
  }
  for (int i = 0; i < Q; i++) {
    int ex = num[b[i]];
    S += (c[i] - b[i]) * ex;
    num[b[i]] = 0;
    num[c[i]] += ex;
    cout << S << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
const int limit = 100000;
int a[limit], b[limit], c[limit];
int main(void) {
  int N, Q;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  cin >> Q;
  for (int i = 0; i < Q; i++)
    cin >> b[i] >> c[i];
  long long S = 0;
  for (int i = 0; i < N; i++) {
    S += a[i];
  }
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (b[i] == a[j]) {
        a[j] = c[i];
        S += c[i] - b[i];
      }
    }
    cout << S << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using PP = pair<int, pair<int, int>>;
using T = tuple<string, int, int>;
const string EMP = " ";
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ull> a(1000001);
  ull ans = 0;
  rep(i, n) {
    int aa;
    cin >> aa;
    ans += aa;
    a[aa]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ans -= b * a[b];
    ans += c * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using PP = pair<int, pair<int, int>>;
using T = tuple<string, int, int>;
const string EMP = " ";
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ull> a(n + 1);
  ull ans = 0;
  rep(i, n) {
    int aa;
    cin >> aa;
    ans += aa;
    a[aa]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ans -= b * a[b];
    ans += c * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fi first
#define se second
#define pb push_back
#define mk make_pair
using namespace std;
typedef long long ll;
typedef long double lld;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll fastpowMOD(ll a, ll p, ll MOD);
ll fastpowMOD(ll a, ll p, ll MOD) {
  if (p == 0)
    return 1;
  a %= MOD;
  ll z = fastpowMOD(a, p / 2, MOD);
  z = (z * z) % MOD;
  if (p % 2)
    z = (z * a) % MOD;
  return z;
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

void solve() {
  ll n, q;
  cin >> n;
  vector<ll> a(n, 0);
  map<ll, ll> mp;
  ll s = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
    mp[a[i]]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x1, x2;
    cin >> x1 >> x2;
    s -= x1 * mp[x1];
    s += x2 * mp[x1];
    mp[x2] += mp[x1];
    mp[x1] = 0;
    cout << s << "\\n";
  }
}

int main() {
  fastio;
  int t;
  // cin>>t;
  t = 1;
  for (int i = 1; i <= t; i++)
    solve();
}


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


Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fi first
#define se second
#define pb push_back
#define mk make_pair
using namespace std;
typedef long long ll;
typedef long double lld;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll fastpowMOD(ll a, ll p, ll MOD);
ll fastpowMOD(ll a, ll p, ll MOD) {
  if (p == 0)
    return 1;
  a %= MOD;
  ll z = fastpowMOD(a, p / 2, MOD);
  z = (z * z) % MOD;
  if (p % 2)
    z = (z * a) % MOD;
  return z;
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

void solve() {
  ll n, q;
  cin >> n;
  vector<ll> a(q, 0);
  map<ll, ll> mp;
  ll s = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
    mp[a[i]]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x1, x2;
    cin >> x1 >> x2;
    s -= x1 * mp[x1];
    s += x2 * mp[x1];
    mp[x2] += mp[x1];
    mp[x1] = 0;
    cout << s << "\\n";
  }
}

int main() {
  fastio;
  int t;
  // cin>>t;
  t = 1;
  for (int i = 1; i <= t; i++)
    solve();
}