prithivMLmods/Coder-Stat huggingface dataset:
Problem p02630 (C++)

Buggy submission (status: 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(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();
}


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(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();
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {

  ll n;
  cin >> n;

  vector<ll> a(n);
  vector<ll> d(10001);
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    d[a[i]]++;
  }

  ll q;
  cin >> q;

  ll b, c;
  REP(i, q) {
    cin >> b >> c;
    sum = sum - b * d[b] + c * d[b];
    d[c] += d[b];
    d[b] = 0;

    cout << sum << endl;
  }
}

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

int main() {

  ll n;
  cin >> n;

  vector<ll> a(n);
  vector<ll> d(100001);
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    d[a[i]]++;
  }

  ll q;
  cin >> q;

  ll b, c;
  REP(i, q) {
    cin >> b >> c;
    sum = sum - b * d[b] + c * d[b];
    d[c] += d[b];
    d[b] = 0;

    cout << sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using p = pair<int, int>;

int main() {
  int N;
  cin >> N;

  vector<int> A(100000);
  ll sum = 0;
  rep(i, N) {
    int tmp;
    cin >> tmp;
    A.at(tmp)++;
    sum += tmp;
  }

  int Q;
  cin >> Q;
  vector<vector<int>> bc(Q, vector<int>(2));
  rep(i, Q) { cin >> bc.at(i).at(0) >> bc.at(i).at(1); }

  rep(i, Q) {
    sum += (bc.at(i).at(1) - bc.at(i).at(0)) * A.at(bc.at(i).at(0));
    cout << sum << endl;
    A.at(bc.at(i).at(1)) += A.at(bc.at(i).at(0));
    A.at(bc.at(i).at(0)) = 0;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using p = pair<int, int>;

int main() {
  int N;
  cin >> N;

  vector<int> A(1000000);
  ll sum = 0;
  rep(i, N) {
    int tmp;
    cin >> tmp;
    A.at(tmp)++;
    sum += tmp;
  }

  int Q;
  cin >> Q;
  vector<vector<int>> bc(Q, vector<int>(2));
  rep(i, Q) { cin >> bc.at(i).at(0) >> bc.at(i).at(1); }

  rep(i, Q) {
    sum += (bc.at(i).at(1) - bc.at(i).at(0)) * A.at(bc.at(i).at(0));
    cout << sum << endl;
    A.at(bc.at(i).at(1)) += A.at(bc.at(i).at(0));
    A.at(bc.at(i).at(0)) = 0;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;

int main() {
  int N, Q, a;
  cin >> N;
  vector<int> A(N), count(N);
  rep(i, N) {
    cin >> a;
    A[i] = a;
    ++count[a];
  }
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  ll ans = 0;

  rep(i, N) { ans += A[i]; }

  rep(i, Q) {
    ans -= count[B[i]] * B[i];
    ans -= count[C[i]] * C[i];
    count[C[i]] += count[B[i]];
    ans += count[C[i]] * C[i];
    count[B[i]] = 0;
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;

int main() {
  int N, Q, a;
  cin >> N;
  vector<int> A(N), count(100001);
  rep(i, N) {
    cin >> a;
    A[i] = a;
    ++count[a];
  }
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  ll ans = 0;

  rep(i, N) { ans += A[i]; }

  rep(i, Q) {
    ans -= count[B[i]] * B[i];
    ans -= count[C[i]] * C[i];
    count[C[i]] += count[B[i]];
    ans += count[C[i]] * C[i];
    count[B[i]] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02630 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, k = 0, x, y, q;
  cin >> n;
  long long a[n];
  unordered_map<long long, long long> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    k += a[i];
  }
  cin >> q;

  while (q--) {
    cin >> x >> y;
    if (mp.find(x) != mp.end()) {
      j = mp[x];
      while (mp[x]--)
        mp[y]++;
      k -= x * j;
      k += y * j;
    }
    cout << k << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, k = 0, x, y, q;
  cin >> n;
  long long a[n];
  unordered_map<long long, long long> mp;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    k += a[i];
  }
  cin >> q;

  while (q--) {
    cin >> x >> y;
    if (mp.find(x) != mp.end()) {
      j = mp[x];
      mp.erase(x);
      mp[y] += j;
      k -= x * j;
      k += y * j;
    }
    cout << k << endl;
  }
}
Problem p02630 (C++)

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

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  vector<int> D(100000);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    D.at(A.at(i))++;
  }

  long S = 0;
  for (int i = 0; i < N; i++) {
    S = S + A.at(i);
  }

  int Q;
  cin >> Q;

  // int sw=0;

  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
    S = S + (C.at(i) - B.at(i)) * D.at(B.at(i));
    D.at(C.at(i)) = D.at(C.at(i)) + D.at(B.at(i));
    D.at(B.at(i)) = 0;
    cout << S << endl;
  }
}


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

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  vector<int> D(200000);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    D.at(A.at(i))++;
  }

  long S = 0;
  for (int i = 0; i < N; i++) {
    S = S + A.at(i);
  }

  int Q;
  cin >> Q;

  // int sw=0;

  vector<int> B(Q);
  vector<int> C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
    S = S + (C.at(i) - B.at(i)) * D.at(B.at(i));
    D.at(C.at(i)) = D.at(C.at(i)) + D.at(B.at(i));
    D.at(B.at(i)) = 0;
    cout << S << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a_kind(n + 1, 0);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int inp;
    cin >> inp;
    ++a_kind[inp];
    sum += inp;
  }
  int q;
  cin >> q;
  vector<ll> ans(q);
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += (ll)(-1 * (ll)a_kind[b] * (ll)b) + (ll)((ll)a_kind[b] * (ll)c);
    a_kind[c] += a_kind[b];
    a_kind[b] = 0;
    ans[i] = sum;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1000000009;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a_kind(100001, 0);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int inp;
    cin >> inp;
    ++a_kind[inp];
    sum += inp;
  }
  int q;
  cin >> q;
  vector<ll> ans(q);
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += (ll)(-1 * (ll)a_kind[b] * (ll)b) + (ll)((ll)a_kind[b] * (ll)c);
    a_kind[c] += a_kind[b];
    a_kind[b] = 0;
    ans[i] = sum;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;
}
Problem p02630 (C++)

Buggy submission (status: 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(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;
  }
}


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(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;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> B[i] >> C[i];
  }

  long long sum = 0;
  vector<long long> cnt(10005, 0);
  for (int i = 0; i < N; ++i) {
    cnt[A[i]]++;
    sum += A[i];
  }

  for (int i = 0; i < Q; ++i) {
    long long old_num = cnt[B[i]];
    long long minus = (long long)B[i] * (long long)old_num;
    long long plus = (long long)C[i] * (long long)old_num;
    cnt[B[i]] = 0;
    cnt[C[i]] += (long long)old_num;

    sum = sum - minus + plus;
    cout << sum << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> B[i] >> C[i];
  }

  long long sum = 0;
  vector<long long> cnt(100005, 0);
  for (int i = 0; i < N; ++i) {
    cnt[A[i]]++;
    sum += A[i];
  }

  for (int i = 0; i < Q; ++i) {
    long long old_num = cnt[B[i]];
    long long minus = (long long)B[i] * (long long)old_num;
    long long plus = (long long)C[i] * (long long)old_num;
    cnt[B[i]] = 0;
    cnt[C[i]] += (long long)old_num;

    sum = sum - minus + plus;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;

int main() {
  ll N, Q, s = 0;
  cin >> N;
  map<ll, ll> m;

  rep(i, N) {
    ll t;
    cin >> t;
    if (m.count(t))
      ++m.at(t);
    else
      m[t] = 1;
    s += t;
  }

  cin >> Q;
  rep(i, Q) {
    ll B, C;
    cin >> B >> C;
    s = s - B * m.at(B) + C * m.at(B);
    if (m.count(C))
      m.at(C) += m.at(B);
    else
      m[C] = m.at(B);
    m.erase(B);
    cout << s << endl;
  }
}

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

int main() {
  ll N, Q, s = 0;
  cin >> N;
  map<ll, ll> m;

  rep(i, N) {
    ll t;
    cin >> t;
    if (m.count(t))
      ++m.at(t);
    else
      m[t] = 1;
    s += t;
  }

  cin >> Q;
  rep(i, Q) {
    ll B, C;
    cin >> B >> C;

    if (m.count(B)) {
      s = s - B * m.at(B) + C * m.at(B);
      if (m.count(C))
        m.at(C) += m.at(B);
      else
        m[C] = m.at(B);
      m.erase(B);
    }
    cout << s << endl;
  }
}
Problem p02630 (C++)

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

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

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

int main() {
  ll n, q, b, c, w, ans = 0;
  cin >> n;
  vector<int> a(500010);
  rep(i, n) {
    cin >> w;
    a[w]++;
    ans += w;
  }
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    ans -= a[b] * (b - c);
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
typedef pair<int, int> P;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define rep(i, n) for (lint i = 0; i < (n); i++)
#define INF LLONG_MAX
#define all(x) (x).begin(), (x).end()
#define pb push_back
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> count(10001);
  lint sum = 0;
  rep(i, n) {
    cin >> a.at(i);
    sum += a.at(i);
    count.at(a.at(i))++;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    sum = sum + (b - a) * count.at(a);
    count.at(b) += count.at(a);
    count.at(a) = 0;
    cout << sum << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
typedef pair<int, int> P;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define rep(i, n) for (lint i = 0; i < (n); i++)
#define INF LLONG_MAX
#define all(x) (x).begin(), (x).end()
#define pb push_back
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> count(100001);
  lint sum = 0;
  rep(i, n) {
    cin >> a.at(i);
    sum += a.at(i);
    count.at(a.at(i))++;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    sum = sum + (b - a) * count.at(a);
    count.at(b) += count.at(a);
    count.at(a) = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

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

Buggy submission (status: 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(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;
}


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(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;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  long long sum = 0;
  vector<long long> t(10010, 0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int temp;
    cin >> temp;
    ++t[temp];
    sum += temp;
  }

  int q;
  cin >> q;
  vector<long long> ans(q);
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * t[b];
    ans[i] = sum;
    t[c] += t[b];
    t[b] = 0;
  }

  // for(int i=0;i<q;++i) cout<<ans[i]<<endl;
  for (int i = 0; i < q; ++i)
    printf("%lld \n", ans[i]);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  long long sum = 0;
  vector<long long> t(110010, 0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int temp;
    cin >> temp;
    ++t[temp];
    sum += temp;
  }

  int q;
  cin >> q;
  vector<long long> ans(q);
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * t[b];
    ans[i] = sum;
    t[c] += t[b];
    t[b] = 0;
  }

  // for(int i=0;i<q;++i) cout<<ans[i]<<endl;
  for (int i = 0; i < q; ++i)
    printf("%lld \n", ans[i]);
}
Problem p02630 (C++)

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

int main() {
  map<long long, long long> h;
  long long N;
  cin >> N;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    sum += a;
    h[a]++;
  }
  int Q;
  cin >> Q;
  long long BC[N][2];

  for (int i = 0; i < Q; i++) {
    cin >> BC[i][0] >> BC[i][1];
  }
  for (int i = 0; i < Q; i++) {
    long long B, C;
    B = BC[i][0];
    C = BC[i][1];
    // cout << h[B] << endl;
    // cout << h[C] << endl;
    sum += C * h[B] - B * h[B];
    h[C] += h[B];
    h[B] = 0;
    cout << sum << endl;
  }
}

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

int main() {
  map<long long, long long> h;
  long long N;
  cin >> N;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    sum += a;
    h[a]++;
  }
  int Q;
  cin >> Q;
  long long BC[Q][2];
  for (int i = 0; i < Q; i++) {
    cin >> BC[i][0] >> BC[i][1];
  }
  for (int i = 0; i < Q; i++) {
    long long B, C;
    B = BC[i][0];
    C = BC[i][1];
    // cout << h[B] << endl;
    // cout << h[C] << endl;
    sum += C * h[B] - B * h[B];
    h[C] += h[B];
    h[B] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  long long int a[10005] = {};
  long long int n;
  cin >> n;
  long long int temp;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp]++;
    ans += temp;
  }

  long long int q;
  cin >> q;
  vector<long long int> b;
  vector<long long int> c;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    b.push_back(temp);
    cin >> temp;
    c.push_back(temp);
  }

  for (int i = 0; i < q; i++) {
    ans += a[b[i]] * (c[i] - b[i]);
    cout << ans;
    if (i != q - 1)
      cout << "\n";
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  long long int a[100005] = {};
  long long int n;
  cin >> n;
  long long int temp;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp]++;
    ans += temp;
  }

  long long int q;
  cin >> q;
  vector<long long int> b;
  vector<long long int> c;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    b.push_back(temp);
    cin >> temp;
    c.push_back(temp);
  }

  for (int i = 0; i < q; i++) {
    ans += a[b[i]] * (c[i] - b[i]);
    cout << ans;
    if (i != q - 1)
      cout << "\n";
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

#define pi 3.1415926535

int main() {
  int n;
  cin >> n;

  vector<int> a(10001, 0);
  int tmp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a[tmp]++;
    sum += tmp;
  }

  int q;
  cin >> q;

  vector<ll> ans(q, 0);

  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;

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

    ans[i] = sum;
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

#define pi 3.1415926535

int main() {
  int n;
  cin >> n;

  vector<int> a(100001, 0);
  int tmp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a[tmp]++;
    sum += tmp;
  }

  int q;
  cin >> q;

  vector<ll> ans(q, 0);

  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;

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

    ans[i] = sum;
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> buc(10101);
  ll ans = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    buc[x]++;
    ans += x;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += c * buc[b] - buc[b] * b;
    buc[c] += buc[b];
    buc[b] = 0;
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> buc(101010);
  ll ans = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    buc[x]++;
    ans += x;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += c * buc[b] - buc[b] * b;
    buc[c] += buc[b];
    buc[b] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> num(n + 1);
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    sum += a;
    num[a]++;
  }

  int q = 0;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    std::cin >> b >> c;
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    std::cout << sum << std::endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> num(100000);
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    sum += a;
    num[a]++;
  }

  int q = 0;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    std::cin >> b >> c;
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    std::cout << sum << std::endl;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <unordered_map>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  unordered_map<ll, ll> mp;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    mp[A]++;
    ans += A;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ll n = 0;
    auto ite = mp.begin();
    for (ite; ite != mp.end(); ite++) {
      if (ite->first == B)
        break;
    }
    if (ite != mp.end()) {
      ans += (C - B) * ite->second;
      mp[C] += ite->second;
      ite->second = 0;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
#include <unordered_map>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  unordered_map<ll, ll> mp;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    mp[A]++;
    ans += A;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ll n = 0;
    auto ite = mp.find(B);
    if (ite != mp.end()) {
      ans += (C - B) * ite->second;
      mp[C] += ite->second;
      ite->second = 0;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#define Maxn 5001
#define ll long long
using namespace std;

ll sum;
int num[Maxn];
int a[Maxn];
int main() {
  int n, q, b, c;
  scanf("%d", &n);
  sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
    num[a[i]]++; // a[i]出现的次数
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &b, &c);
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    printf("%lld\n", sum);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#define Maxn 100005
#define ll long long
using namespace std;

ll sum;
int num[Maxn];
int a[Maxn];
int main() {
  int n, q, b, c;
  scanf("%d", &n);
  sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
    num[a[i]]++; // a[i]出现的次数
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &b, &c);
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    printf("%lld\n", sum);
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
/**
 *   Bismillahir Rahmanir Rahim.
 *   Imtiaz_rafi
 *   PCIU, CHITTAGONG
 **/
#include <bits/stdc++.h>
using namespace std;
#define speed                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, begin, end) for (ll i = begin; i <= end; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define pi acos(-1)
// bitshift (1LL<<(3)) *2^3
typedef long long int ll;
typedef vector<ll> vi;
typedef map<ll, ll> m;
typedef map<char, ll> mc;
typedef set<ll> st;
typedef set<char> sc;
const ll mx = 1e5 + 123;
ll binarySearch(ll arr[], ll p, ll r, ll num) {
  if (p <= r) {
    ll mid = (p + r) / 2;
    if (arr[mid] == num)
      return mid;
    if (arr[mid] > num)
      return binarySearch(arr, p, mid - 1, num);
    if (arr[mid] < num)
      return binarySearch(arr, mid + 1, r, num);
  }
  return -1;
}
int main() {
  speed;
  ll t, i, j, a, b, x, y, e = 0, ans = 0, sum = 0;
  cin >> a;
  vi c(a + 5, 0);
  vi d(a);
  rep(i, a) {
    cin >> d[i];
    sum += d[i];
    c[d[i]]++;
  }
  cin >> b;
  while (b--) {
    cin >> x >> y;
    sum += (y - x) * c[x];
    c[y] += c[x];
    c[x] = 0;
    cout << sum << endl;
  }
}


Fixed submission:
/**
 *   Bismillahir Rahmanir Rahim.
 *   Imtiaz_rafi
 *   PCIU, CHITTAGONG
 **/
#include <bits/stdc++.h>
using namespace std;
#define speed                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, begin, end) for (ll i = begin; i <= end; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define pi acos(-1)
// bitshift (1LL<<(3)) *2^3
typedef long long int ll;
typedef vector<ll> vi;
typedef map<ll, ll> m;
typedef map<char, ll> mc;
typedef set<ll> st;
typedef set<char> sc;
const ll mx = 1e5 + 123;
ll binarySearch(ll arr[], ll p, ll r, ll num) {
  if (p <= r) {
    ll mid = (p + r) / 2;
    if (arr[mid] == num)
      return mid;
    if (arr[mid] > num)
      return binarySearch(arr, p, mid - 1, num);
    if (arr[mid] < num)
      return binarySearch(arr, mid + 1, r, num);
  }
  return -1;
}
int main() {
  speed;
  ll t, i, j, a, b, x, y, e = 0, ans = 0, sum = 0;
  cin >> a;
  vi c(mx, 0);
  vi d(a);
  rep(i, a) {
    cin >> d[i];
    sum += d[i];
    c[d[i]]++;
  }
  cin >> b;
  while (b--) {
    cin >> x >> y;
    sum += (y - x) * c[x];
    c[y] += c[x];
    c[x] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N), memo(100000);
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    memo.at(A.at(i))++;
    ans += A.at(i);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int64_t B, C;
    cin >> B >> C;
    ans += memo.at(B) * (C - B);
    memo.at(C) += memo.at(B);
    memo.at(B) = 0;
    cout << ans << endl;
  }
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N), memo(100001);
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    memo.at(A.at(i))++;
    ans += A.at(i);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int64_t B, C;
    cin >> B >> C;
    ans += memo.at(B) * (C - B);
    memo.at(C) += memo.at(B);
    memo.at(B) = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++)

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

int main(void) {
  int n;
  cin >> n;
  ll tot = 0;
  vector<ll> cnt(n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    tot += a;
  }

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

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

int main(void) {
  int n;
  cin >> n;
  ll tot = 0;
  vector<ll> cnt(100005);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    tot += a;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    tot += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << tot << endl;
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, f, n) for (ll i = (f); (i) < (n); i++)
#define repe(i, f, n) for (ll i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;

ll INF = 1LL << 60;
using G = vector<map<int, int>>;

int main() {
  int N;
  cin >> N;
  vector<ll> num(10);
  ll acum = 0;
  rep(i, 0, N) {
    ll tmp;
    cin >> tmp;
    num[tmp]++;
    acum += tmp;
  }

  int Q;
  cin >> Q;
  vector<pair<int, int>> q(Q);
  rep(i, 0, Q) cin >> q[i].first >> q[i].second;

  rep(i, 0, Q) {
    int B = q[i].first;
    int C = q[i].second;
    num[C] += num[B];
    acum = acum + (num[B] * (C - B));
    num[B] = 0;
    cout << acum << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, f, n) for (ll i = (f); (i) < (n); i++)
#define repe(i, f, n) for (ll i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;

ll INF = 1LL << 60;
using G = vector<map<int, int>>;

int main() {
  int N;
  cin >> N;
  vector<ll> num(100001);
  ll acum = 0;
  rep(i, 0, N) {
    ll tmp;
    cin >> tmp;
    num[tmp]++;
    acum += tmp;
  }

  int Q;
  cin >> Q;
  vector<pair<int, int>> q(Q);
  rep(i, 0, Q) cin >> q[i].first >> q[i].second;

  rep(i, 0, Q) {
    int B = q[i].first;
    int C = q[i].second;
    num[C] += num[B];
    acum = acum + (num[B] * (C - B));
    num[B] = 0;
    cout << acum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: 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;
  int a;
  vector<int> t(n, 0);
  ll s = 0;
  rep(i, n) {
    cin >> a;
    t[a]++;
    s += a;
  }

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

  return 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;
  int a;
  int t[100010];
  rep(i, 100010) { t[i] = 0; }
  ll s = 0;
  rep(i, n) {
    cin >> a;
    t[a]++;
    s += a;
  }

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

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int main() {
  vector<int> num(10001, 0);
  vector<long long> ans;
  int n, k, q, b, c;
  long long sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    num[k]++;
    sum += k;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    k = num[b];
    num[b] = 0;
    num[c] += k;
    sum += (long long)((long long)c - (long long)b) * (long long)k;
    ans.emplace_back(sum);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << "\n";
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int main() {
  vector<int> num(100001, 0);
  vector<long long> ans;
  int n, k, q, b, c;
  long long sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    num[k]++;
    sum += k;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    k = num[b];
    num[b] = 0;
    num[c] += k;
    sum += (long long)((long long)c - (long long)b) * (long long)k;
    ans.emplace_back(sum);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << "\n";
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
// By Zank100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define din(t)                                                                 \
  long long t;                                                                 \
  cin >> t;
#define in(t) cin >> t;
ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); }
ll modexp(ll b, ll e) {
  ll res = 1;
  for (; e; b = b * b % mod, e >>= 1)
    if (e & 1)
      res = res * b % mod;
  return res;
}
ll modinv(ll a) {
  ll x = mod - 2;
  return modexp(a, x);
}
ll power(ll x, ll y) {
  ll temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else {
    if (y > 0)
      return x * temp * temp;
    else
      return (temp * temp) / x;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  din(n) ll a[n];
  ll sum = 0, max = 0;
  for (ll i = 0; i < n; i++) {
    in(a[i]);
    sum += a[i];
  }
  ll hash[10001] = {0};
  for (ll i = 0; i < n; i++)
    hash[a[i]]++;
  din(q);
  vector<ll> ans;
  for (ll i = 0; i < q; i++) {
    din(b);
    din(c);
    sum -= hash[b] * b;
    sum += hash[b] * c;
    hash[c] += hash[b];
    hash[b] = 0;
    ans.push_back(sum);
  }
  for (ll i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
// By Zank100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define din(t)                                                                 \
  long long t;                                                                 \
  cin >> t;
#define in(t) cin >> t;
ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); }
ll modexp(ll b, ll e) {
  ll res = 1;
  for (; e; b = b * b % mod, e >>= 1)
    if (e & 1)
      res = res * b % mod;
  return res;
}
ll modinv(ll a) {
  ll x = mod - 2;
  return modexp(a, x);
}
ll power(ll x, ll y) {
  ll temp;
  if (y == 0)
    return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else {
    if (y > 0)
      return x * temp * temp;
    else
      return (temp * temp) / x;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  din(n) ll a[n];
  ll sum = 0, max = 0;
  for (ll i = 0; i < n; i++) {
    in(a[i]);
    sum += a[i];
  }
  ll hash[100001] = {0};
  for (ll i = 0; i < n; i++)
    hash[a[i]]++;
  din(q);
  vector<ll> ans;
  for (ll i = 0; i < q; i++) {
    din(b);
    din(c);
    sum -= hash[b] * b;
    sum += hash[b] * c;
    hash[c] += hash[b];
    hash[b] = 0;
    ans.push_back(sum);
  }
  for (ll i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <memory.h>
using namespace std;
int a[10001];
int main() {
  memset(a, 0, sizeof(a));
  int n;
  cin >> n;
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    count += b;
    a[b]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m, n;
    cin >> m >> n;
    a[n] += a[m];
    int c = a[m];
    a[m] = 0;
    count = count + c * (n - m);
    cout << count << endl;
  }
}

Fixed submission:
#include <iostream>
#include <memory.h>
using namespace std;
int a[1000001];
int main() {
  memset(a, 0, sizeof(a));
  int n;
  cin >> n;
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    count += b;
    a[b]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m, n;
    cin >> m >> n;
    a[n] += a[m];
    int c = a[m];
    a[m] = 0;
    count = count + c * (n - m);
    cout << count << endl;
  }
}
Problem p02630 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  sum = accumulate(a.begin(), a.end(), 0LL);
  ll q;
  cin >> q;
  vector<ll> b(n, 0), c(n, 0);
  for (ll i = 0; i < q; i++)
    cin >> b[i] >> c[i];

  vector<ll> dp(1000000, 0);
  for (ll i = 0; i < n; i++) {
    dp[a[i]] += 1;
  }

  ll ans = sum;
  for (ll i = 0; i < q; i++) {
    ans = ans - (b[i] * dp[b[i]]) + (c[i] * dp[b[i]]);
    dp[c[i]] = dp[c[i]] + dp[b[i]];
    dp[b[i]] = 0;
    cout << ans << endl;
  }

  return 0;
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  sum = accumulate(a.begin(), a.end(), 0LL);
  ll q;
  cin >> q;
  vector<ll> b(q, 0), c(q, 0);
  for (ll i = 0; i < q; i++)
    cin >> b[i] >> c[i];

  vector<ll> dp(1000000, 0);
  for (ll i = 0; i < n; i++) {
    dp[a[i]] += 1;
  }

  ll ans = sum;
  for (ll i = 0; i < q; i++) {
    ans = ans - (b[i] * dp[b[i]]) + (c[i] * dp[b[i]]);
    dp[c[i]] = dp[c[i]] + dp[b[i]];
    dp[b[i]] = 0;
    cout << ans << endl;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1000);
  rep(i, n) cin >> a[i];
  ll q;
  cin >> q;
  ll sum = 0;
  rep(i, n) sum += a[i];
  vector<ll> cnt(n + 1000, 0);
  rep(i, n) { cnt[a[i]]++; }
  rep(i, q) {
    // rep(i,n) cout << cnt[i] << " ";
    // cout << endl;
    ll b, c;
    cin >> b >> c;
    ll d = c - b;
    // bの数を数える
    sum += cnt[b] * d;
    cout << sum << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
}

/*
Ctrl+@ でコンソールを開け、以下で実行。
g++ d.cpp
./a.out

サンプルのダウンロードは oj d https://atcoder.jp/contests/abc171/tasks/abc171_d
テストは oj t
*/

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1000);
  rep(i, n) cin >> a[i];
  ll q;
  cin >> q;
  ll sum = 0;
  rep(i, n) sum += a[i];
  vector<ll> cnt(100007, 0);
  rep(i, n) { cnt[a[i]]++; }
  rep(i, q) {
    // rep(i,n) cout << cnt[i] << " ";
    // cout << endl;
    ll b, c;
    cin >> b >> c;
    ll d = c - b;
    // bの数を数える
    sum += cnt[b] * d;
    cout << sum << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
}

/*
Ctrl+@ でコンソールを開け、以下で実行。
g++ d.cpp
./a.out

サンプルのダウンロードは oj d https://atcoder.jp/contests/abc171/tasks/abc171_d
テストは oj t
*/