prithivMLmods/Coder-Stat huggingface dataset:
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() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> num(100005);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    num[a[i]]++;
  }
  int q;
  cin >> q;
  vector<int> b(n), c(n);
  rep(i, q) cin >> b[i] >> c[i];

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

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() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> num(100005);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    num[a[i]]++;
  }
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  rep(i, q) cin >> b[i] >> c[i];

  rep(i, q) {
    sum += num[b[i]] * (c[i] - b[i]);
    num[c[i]] += num[b[i]];
    num[b[i]] = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

using ll = long long;

int main() {
  int N;
  scanf("%d", &N);
  static ll dat[100001];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int A;
    scanf("%lld", &A);
    dat[A]++;
    ans += A;
  }

  int Q;
  scanf("%d", &Q);
  for (int q = 0; q < Q; q++) {
    int B, C;
    scanf("%d%d", &B, &C);
    ll diff = dat[B] * (C - B);
    ans += diff;
    if (B != C) {
      dat[C] += dat[B];
      dat[B] = 0;
    }
    printf("%lld\n", ans);
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>

using ll = long long;

int main() {
  int N;
  scanf("%d", &N);
  static ll dat[100001];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll A;
    scanf("%lld", &A);
    dat[A]++;
    ans += A;
  }

  int Q;
  scanf("%d", &Q);
  for (int q = 0; q < Q; q++) {
    int B, C;
    scanf("%d%d", &B, &C);
    ll diff = dat[B] * (C - B);
    ans += diff;
    if (B != C) {
      dat[C] += dat[B];
      dat[B] = 0;
    }
    printf("%lld\n", ans);
  }

  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 < (int)(n); i++)

int main() {
  int N, Q, B, C, T;
  long long Sum = 0;
  cin >> N;
  vector<int> A(100000, 0);
  rep(i, N) {
    cin >> T;
    A.at(T)++;
    Sum += T;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    Sum += A.at(B) * (C - B);
    A.at(C) += A.at(B);
    A.at(B) = 0;
    cout << Sum << endl;
  }
}

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

int main() {
  int N, Q, B, C, T;
  long long Sum = 0;
  cin >> N;
  vector<int> A(100001, 0);
  rep(i, N) {
    cin >> T;
    A.at(T)++;
    Sum += T;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    Sum += A.at(B) * (C - B);
    A.at(C) += A.at(B);
    A.at(B) = 0;
    cout << Sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<int> count(10001, 0);
  vector<int> v(n);
  long long sum = 0;
  for (auto i : v) {
    cin >> i;
    sum += i;
    count[i]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    count[c] += count[b];
    sum += (c - b) * count[b];
    count[b] = 0;
    cout << sum << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<int> count(100001, 0);
  vector<int> v(n);
  long long sum = 0;
  for (auto i : v) {
    cin >> i;
    sum += i;
    count[i]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    count[c] += count[b];
    sum += (c - b) * count[b];
    count[b] = 0;
    cout << sum << endl;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n;
  cin >> n;
  vector<ll> num(10010, 0);
  ll ans = 0;

  rep(i, n) {
    ll a;
    cin >> a;
    num[a]++;
    ans += a;
  }

  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll v = num[b];
    num[b] = 0;
    num[c] += v;
    ans -= v * b;
    ans += v * c;
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n;
  cin >> n;
  vector<ll> num(100010, 0);
  ll ans = 0;

  rep(i, n) {
    ll a;
    cin >> a;
    num[a]++;
    ans += a;
  }

  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ll v = num[b];
    num[b] = 0;
    num[c] += v;
    ans -= v * b;
    ans += v * c;
    cout << ans << endl;
  }
}
Problem p02630 (C++)

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

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll n, a, sum, num[N], q, b;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a, sum += a, num[a]++;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    sum += (b - a) * num[a];
    num[b] += num[a];
    num[a] = 0;
    cout << sum << 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 RREP(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<int> v(10000 + 1);
  ll sum = 0;
  REP(i, n) {
    int a;
    cin >> a;
    v[a]++;
    sum += a;
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<int> v(100000 + 1);
  ll sum = 0;
  REP(i, n) {
    int a;
    cin >> a;
    v[a]++;
    sum += a;
  }

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

  return 0;
}
Problem p02630 (C++)

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

int main() {

  int n;
  cin >> n;
  vector<int64_t> a(10001, 0);
  int64_t sum = 0;
  for (int i = 0; i < n;
       i++) { // 1~10000の各数を数列がそれぞれいくつ含むかを調べるための配列
    int x;
    cin >> x;
    a.at(x)++;
    sum += x; // 数列の和
  }

  int q;
  cin >> q;
  vector<int64_t> answer(q, 0);
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a.at(b); // 数列の和を更新
    answer.at(i) = sum;
    a.at(c) += a.at(b);
    a.at(b) = 0;
  }

  for (int64_t ans : answer) {
    cout << ans << endl;
  }
}

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

int main() {

  int n;
  cin >> n;
  vector<int64_t> a(100001, 0);
  int64_t sum = 0;
  for (int i = 0; i < n;
       i++) { // 1~10000の各数を数列がそれぞれいくつ含むかを調べるための配列
    int x;
    cin >> x;
    a.at(x)++;
    sum += x; // 数列の和
  }

  int q;
  cin >> q;
  vector<int64_t> answer(q, 0);
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a.at(b); // 数列の和を更新
    answer.at(i) = sum;
    a.at(c) += a.at(b);
    a.at(b) = 0;
  }

  for (int64_t ans : answer) {
    cout << ans << endl;
  }
}
Problem p02630 (C++)

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

typedef long long int ll;
typedef long double ld;

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pll pair<ll, ll>
#define MP make_pair
#define ff first
#define ss second
#define PB push_back
#define mod 1000000007
#define lp(i, start, end) for (ll i = start; i <= end; i++)
#define deb1(a) cout << #a << " = " << (a) << endl;
#define deb2(a, b)                                                             \
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << endl;
#define deb3(a, b, c)                                                          \
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << ", " << #c       \
       << " = " << (c) << endl;

ll modu(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % mod;
    b /= 2;
    a = (a * a) % mod;
  }
  return ans;
}

int main() {
  fastio ll n, sum = 0;
  cin >> n;
  ll dp[50000] = {0};
  ll a[n];
  lp(i, 0, n - 1) {
    cin >> a[i];
    sum += a[i];
    dp[a[i]]++;
  }
  ll q;
  cin >> q;
  lp(i, 0, q - 1) {
    ll x, y;
    cin >> x >> y;
    if (dp[x]) {
      sum += dp[x] * (y - x);
      dp[y] += dp[x];
      dp[x] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}


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

typedef long long int ll;
typedef long double ld;

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pll pair<ll, ll>
#define MP make_pair
#define ff first
#define ss second
#define PB push_back
#define mod 1000000007
#define lp(i, start, end) for (ll i = start; i <= end; i++)
#define deb1(a) cout << #a << " = " << (a) << endl;
#define deb2(a, b)                                                             \
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << endl;
#define deb3(a, b, c)                                                          \
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << ", " << #c       \
       << " = " << (c) << endl;

ll modu(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % mod;
    b /= 2;
    a = (a * a) % mod;
  }
  return ans;
}

int main() {
  fastio ll n, sum = 0;
  cin >> n;
  ll dp[100005] = {0};
  ll a[n];
  lp(i, 0, n - 1) {
    cin >> a[i];
    sum += a[i];
    dp[a[i]]++;
  }
  ll q;
  cin >> q;
  lp(i, 0, q - 1) {
    ll x, y;
    cin >> x >> y;
    if (dp[x]) {
      sum += dp[x] * (y - x);
      dp[y] += dp[x];
      dp[x] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++)

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

int main(void) {
  ll N;
  cin >> N;
  vector<ll> vc(100000, 0);
  ll total = 0;
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    vc.at(A) += 1;
    total += A;
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    ll B, C;
    cin >> B >> C;
    ll tmp_b = vc.at(B);
    total -= tmp_b * B;
    vc.at(B) = 0;
    total += C * tmp_b;
    vc.at(C) += tmp_b;
    cout << total << endl;
  }
  return 0;
}

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

int main(void) {
  ll N;
  cin >> N;
  vector<ll> vc(100001, 0);
  ll total = 0;
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    vc.at(A) += 1;
    total += A;
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    ll B, C;
    cin >> B >> C;
    ll tmp_b = vc.at(B);
    total -= tmp_b * B;
    vc.at(B) = 0;
    total += C * tmp_b;
    vc.at(C) += tmp_b;
    cout << total << endl;
  }
  return 0;
}
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import Counter

input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)


Fixed submission:
import sys
from collections import Counter

input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in adict:
        print(sumA)
        continue

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

ans = 0
n = [0] * 10005
for i in range(N):
    n[A[i]] += 1
    ans += A[i]
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    ans += c * n[b] - b * n[b]
    print(ans)
    n[c] += n[b]
    n[b] = 0


Fixed submission:
N = int(input())
A = list(map(int, input().split()))

ans = 0
n = [0] * 100005
for i in range(N):
    n[A[i]] += 1
    ans += A[i]
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    ans += c * n[b] - b * n[b]
    print(ans)
    n[c] += n[b]
    n[b] = 0
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<long long> f(n, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    f[A[i]]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    int diff = c - b;
    sum += diff * f[b];
    f[c] += f[b];
    f[b] = 0;
    cout << sum << "\n";
  }
}


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

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  vector<long long> f(1e5 + 1, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    f[A[i]]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    int diff = c - b;
    sum += diff * f[b];
    f[c] += f[b];
    f[b] = 0;
    cout << sum << "\n";
  }
}
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

counter = Counter(A)
ans = 0
for num, count in counter.items():
    ans += num * count

for i in range(Q):
    if B[i] in counter:
        ans += counter[B[i]] * C[i] - counter[B[i]] * B[i]
        counter[C[i]] = counter[C[i]] + counter.pop([B[i]])
    print(ans)


Fixed submission:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

counter = Counter(A)
ans = 0
for num, count in counter.items():
    ans += num * count

for i in range(Q):
    if B[i] in counter:
        ans += counter[B[i]] * C[i] - counter[B[i]] * B[i]
        counter[C[i]] = counter[C[i]] + counter.pop(B[i])
    print(ans)
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

mx = max(a)
table = [0] * (mx + 1)
for i in a:
    table[i] += 1

q = int(input())
total = sum(a)
for i in range(q):
    (
        b,
        c,
    ) = map(int, input().split())
    total -= table[c] * c + table[b] * b
    table[c] += table[b]
    table[b] = 0
    total += table[c] * c
    print(total)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))

mx = 10**5
table = [0] * (mx + 1)
for i in a:
    table[i] += 1

q = int(input())
total = sum(a)
for i in range(q):
    (
        b,
        c,
    ) = map(int, input().split())
    total -= table[c] * c + table[b] * b
    table[c] += table[b]
    table[b] = 0
    total += table[c] * c
    print(total)
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict

N = int(input())
_A = [*map(int, input().split())]
Q = int(input())
val = [[*map(int, line.split())] for line in open(0)]

A = defaultdict(int)
for _a in _A:
    A[_a] += 1

ans = sum(_A)
for v in val:
    if v[0] in A.keys():
        num = A[v[0]]
        A[v[0]] = 0
        A[v[1]] += num
        ans += (v[1] - v[0]) * num
    print(ans)


Fixed submission:
from collections import defaultdict

N, _A, Q, *val = [[*map(int, line.split())] for line in open(0)]
N = N[0]
Q = Q[0]

A = defaultdict(int)
for _a in _A:
    A[_a] += 1

ans = sum(_A)
for v in val:
    if v[0] in A.keys():
        num = A[v[0]]
        A[v[0]] = 0
        A[v[1]] += num
        ans += (v[1] - v[0]) * num
    print(ans)
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
using namespace std;

#define MAX 10005

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
using namespace std;

#define MAX 100005

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

  return 0;
}
Problem p02630 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    for i in range(N):
        if A[i] == B:
            A[i] = C
    print(sum(A))


Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
q = int(input())

cnt = Counter(a)

s = sum(a)

for i in range(q):
    b, c = map(int, input().split())
    s = s - b * cnt[b]
    s = s + c * cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(s)
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = list(map(int, input().split(" ")))
Q = int(input())
BC = [list(map(int, list(input().split(" ")))) for i in range(Q)]

count = [0] * 10**5 + 5
for i in range(N):
    count[A[i]] += 1

s = sum(A)
for i in range(Q):
    s = s - BC[i][0] * count[BC[i][0]] + BC[i][1] * count[BC[i][0]]
    count[BC[i][1]] += count[BC[i][0]]
    count[BC[i][0]] = 0
    print(s)


Fixed submission:
N = int(input())
A = list(map(int, input().split(" ")))
Q = int(input())
BC = [list(map(int, list(input().split(" ")))) for i in range(Q)]

count = [0] * (10**5 + 5)
for i in range(N):
    count[A[i]] += 1

s = sum(A)
for i in range(Q):
    s = s - BC[i][0] * count[BC[i][0]] + BC[i][1] * count[BC[i][0]]
    count[BC[i][1]] += count[BC[i][0]]
    count[BC[i][0]] = 0
    print(s)
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long a[7001];
int main() {
  long long n;
  cin >> n;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    long long o;
    cin >> o;
    a[o]++;
    s += o;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    s += (y - x) * a[x];
    a[y] += a[x];
    a[x] = 0;
    cout << s << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[100001];
int main() {
  long long n;
  cin >> n;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    long long o;
    cin >> o;
    a[o]++;
    s += o;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    s += (y - x) * a[x];
    a[y] += a[x];
    a[x] = 0;
    cout << s << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
using namespace std;

int N, Q, p[100010], b[100010], c[10010];
ll sum;
int main() {
  scanf("%d", &N);
  rep(i, 0, N) {
    int tmp;
    scanf("%d", &tmp);
    p[tmp]++;
    sum += tmp;
  }
  scanf("%d", &Q);
  rep(i, 0, Q) scanf("%d%d", &b[i], &c[i]);

  rep(i, 0, Q) {
    int _b = b[i], _c = c[i];
    sum -= p[_b] * _b;
    sum += p[_b] * _c;
    p[_c] += p[_b];
    p[_b] = 0;
    printf("%lld\n", sum);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
using namespace std;

int N, Q, p[100010], b[100010], c[100010];
ll sum;
int main() {
  scanf("%d", &N);
  rep(i, 0, N) {
    int tmp;
    scanf("%d", &tmp);
    p[tmp]++;
    sum += tmp;
  }
  scanf("%d", &Q);
  rep(i, 0, Q) scanf("%d%d", &b[i], &c[i]);

  rep(i, 0, Q) {
    int _b = b[i], _c = c[i];
    sum -= p[_b] * _b;
    sum += p[_b] * _c;
    p[_c] += p[_b];
    p[_b] = 0;
    printf("%lld\n", sum);
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  long long int sum = 0;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int search, val;
    cin >> search >> val;
    while (m[search]--) {
      sum = sum - search;
      sum = sum + val;
      m[val]++;
    }
    cout << sum << "\n";
  }
}

Fixed submission:
#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  long long int sum = 0;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int search, val;
    cin >> search >> val;
    sum = sum - (search * m[search]);
    sum = sum + (val * m[search]);
    m[val] = m[val] + m[search];
    m[search] = 0;
    cout << sum << "\n";
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;
int main() {

  ll N, Q;
  ll sum = 0;

  cin >> N;

  vector<ll> A(N);
  vector<ll> cnt(N + 1);

  for (ll i = 0; i < N; ++i) {
    cin >> A[i];
    sum += A[i];
    ++cnt[A[i]];
  }

  cin >> Q;

  vector<ll> B(Q);
  vector<ll> C(Q);
  vector<ll> S(Q, 0);

  for (ll i = 0; i < Q; ++i) {

    cin >> B[i] >> C[i];

    sum += (C[i] - B[i]) * cnt[B[i]];
    cnt[C[i]] += cnt[B[i]];
    cnt[B[i]] = 0;

    S[i] = sum;
  }

  for (ll i = 0; i < Q; ++i) {

    cout << S[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;
int main() {

  ll N, Q;
  ll sum = 0;

  cin >> N;

  vector<ll> A(N);
  vector<ll> cnt(100001, 0);

  for (ll i = 0; i < N; ++i) {
    cin >> A[i];
    sum += A[i];
    ++cnt[A[i]];
  }

  cin >> Q;

  vector<ll> B(Q);
  vector<ll> C(Q);
  vector<ll> S(Q, 0);

  for (ll i = 0; i < Q; ++i) {

    cin >> B[i] >> C[i];

    sum += (C[i] - B[i]) * cnt[B[i]];
    cnt[C[i]] += cnt[B[i]];
    cnt[B[i]] = 0;

    S[i] = sum;
  }

  for (ll i = 0; i < Q; ++i) {

    cout << S[i] << endl;
  }

  return 0;
}
Problem p02630 (C++)

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

using namespace std;

int main() {
  int N;
  long long s = 0;
  cin >> N;
  vector<int> A(N);
  vector<int> ap(10 * 10 * 10 * 10 * 10);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    s += A.at(i);
    ap.at(A.at(i))++;
  }

  int Q;
  cin >> Q;
  vector<int> b(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    cin >> b.at(i) >> c.at(i);
  }

  for (int i = 0; i < Q; i++) {
    s += (long long)ap.at(b.at(i)) * ((long long)c.at(i) - (long long)b.at(i));
    ap.at(c.at(i)) += ap.at(b.at(i));
    ap.at(b.at(i)) = 0;
    cout << s << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main() {
  int N;
  long long s = 0;
  cin >> N;
  vector<int> A(N);
  vector<int> ap(10 * 10 * 10 * 10 * 10 + 1);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    s += A.at(i);
    ap.at(A.at(i))++;
  }

  int Q;
  cin >> Q;
  vector<int> b(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    cin >> b.at(i) >> c.at(i);
  }

  for (int i = 0; i < Q; i++) {
    s += (long long)ap.at(b.at(i)) * ((long long)c.at(i) - (long long)b.at(i));
    ap.at(c.at(i)) += ap.at(b.at(i));
    ap.at(b.at(i)) = 0;
    cout << s << endl;
  }
}
Problem p02630 (C++)

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

typedef unsigned long long int lli;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli n;
  cin >> n;

  vector<lli> v(n + 1);
  vector<lli> frequency(n + 1);

  lli sum = 0;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    frequency[v[i]]++;
    sum += v[i];
  }

  lli q;
  cin >> q;

  while (q--) {
    lli b, c;
    cin >> b >> c;

    sum += -frequency[b] * b + frequency[b] * c;

    cout << sum << endl;

    frequency[c] += frequency[b];
    frequency[b] = 0;
  }
}


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

typedef unsigned long long int lli;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli n;
  cin >> n;

  vector<lli> v(n + 1);
  vector<lli> frequency(1000001);

  lli sum = 0;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    frequency[v[i]]++;
    sum += v[i];
  }

  lli q;
  cin >> q;

  while (q--) {
    lli b, c;
    cin >> b >> c;

    sum += -frequency[b] * b + frequency[b] * c;

    cout << sum << endl;

    frequency[c] += frequency[b];
    frequency[b] = 0;
  }
}
Problem p02630 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = [int(_) for _ in input().split()]
Q = int(input())
b_to_c = [[int(_) for _ in input().split()] for i in range(Q)]

sum = 0
memo = [0] * (10**5)
for i in A:
    memo[i] += 1
    sum += i

for bc in b_to_c:
    b = bc[0]
    c = bc[1]
    sum -= b * memo[b]
    sum += c * memo[b]
    memo[c] += memo[b]
    memo[b] = 0
    print(sum)


Fixed submission:
N = int(input())
A = [int(_) for _ in input().split()]
Q = int(input())
b_to_c = [[int(_) for _ in input().split()] for i in range(Q)]

sum = 0
memo = [0] * (10**5 + 1)
for i in A:
    memo[i] += 1
    sum += i

for bc in b_to_c:
    b = bc[0]
    c = bc[1]
    sum -= b * memo[b]
    sum += c * memo[b]
    memo[c] += memo[b]
    memo[b] = 0
    print(sum)
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define vint vector<int>

int main() {
  int n, q;
  cin >> n;
  vint a(n);
  REP(i, n) cin >> a[i];
  cin >> q;
  vector<vector<int>> b(q, vector<int>(2));
  REP(i, q) { cin >> b[i][0] >> b[i][1]; }

  sort(a.begin(), a.end());
  vector<long long> h(a[a.size() - 1], 0);

  long long s = 0;
  REP(i, n) {
    h[a[i] - 1]++;
    s += a[i];
  }

  REP(i, q) {
    s += (b[i][1] - b[i][0]) * h[b[i][0] - 1];
    h[b[i][1] - 1] += h[b[i][0] - 1];
    h[b[i][0] - 1] = 0;
    cout << s << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define vint vector<int>

int main() {
  int n, q;
  cin >> n;
  vint a(n);
  REP(i, n) cin >> a[i];
  cin >> q;
  vector<vector<int>> b(q, vector<int>(2));
  REP(i, q) { cin >> b[i][0] >> b[i][1]; }

  sort(a.begin(), a.end());
  vector<long long> h((int)(1e5), 0);

  long long s = 0;
  REP(i, n) {
    h[a[i] - 1]++;
    s += a[i];
  }

  REP(i, q) {
    s += (b[i][1] - b[i][0]) * h[b[i][0] - 1];
    h[b[i][1] - 1] += h[b[i][0] - 1];
    h[b[i][0] - 1] = 0;
    cout << s << endl;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<int> vec(10001, 0);
int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<int> A(N);
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    sum += A[i];
    vec[A[i]]++;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    if (C[i] >= B[i]) {
      sum += vec[B[i]] * (C[i] - B[i]);
    } else {
      sum -= vec[B[i]] * (B[i] - C[i]);
    }
    if (B[i] != C[i]) {
      vec[C[i]] += vec[B[i]];
      vec[B[i]] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<int> vec(100001, 0);
int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<int> A(N);
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    sum += A[i];
    vec[A[i]]++;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    if (C[i] >= B[i]) {
      sum += vec[B[i]] * (C[i] - B[i]);
    } else {
      sum -= vec[B[i]] * (B[i] - C[i]);
    }
    if (B[i] != C[i]) {
      vec[C[i]] += vec[B[i]];
      vec[B[i]] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}
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);

  int64_t s = 0;

  vector<vector<int>> v(100000, vector<int>(0));

  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    s += a.at(i);
    v.at(a.at(i)).push_back(i);
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      int z = v.at(b).size();
      for (int j = 0; j < z; j++) {
        v.at(c).push_back(v.at(b).at(j));
      }
      for (int j = 0; j < z; j++) {
        v.at(b).pop_back();
      }

      s += (c - b) * z;
      cout << s << endl;
    }

    else
      cout << s << endl;
  }
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);

  int64_t s = 0;

  vector<vector<int>> v(100001, vector<int>(0));

  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    s += a.at(i);
    v.at(a.at(i)).push_back(i);
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      int z = v.at(b).size();
      for (int j = 0; j < z; j++) {
        v.at(c).push_back(v.at(b).at(j));
      }
      for (int j = 0; j < z; j++) {
        v.at(b).pop_back();
      }

      s += (c - b) * z;
      cout << s << endl;
    }

    else
      cout << s << 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++)
#define srt(i) sort(i.begin(), i.end())
#define rvt(i) sort(i.begin(), i.end(), greater<int>())

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

  long sum = 0;
  vector<int> a(100000);
  rep(i, n) {
    int tmp;
    cin >> tmp;
    a.at(tmp)++;
    sum += tmp;
  }

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

    int cnt = a.at(b);
    sum = sum + cnt * (c - b);
    cout << sum << endl;

    a.at(c) += a.at(b);
    a.at(b) = 0;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define srt(i) sort(i.begin(), i.end())
#define rvt(i) sort(i.begin(), i.end(), greater<int>())

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

  long sum = 0;
  vector<int> a(100001);
  rep(i, n) {
    int tmp;
    cin >> tmp;
    a.at(tmp)++;
    sum += tmp;
  }

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

    int cnt = a.at(b);
    sum = sum + cnt * (c - b);
    cout << sum << endl;

    a.at(c) += a.at(b);
    a.at(b) = 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 < (int)(n); i++)
using ll = long long;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) { cin >> B.at(i) >> C.at(i); }
  int MAX = 100000;
  vector<long> S(Q);
  vector<int> count(MAX, 0);
  rep(i, N) { count.at(A.at(i))++; }

  long sum = 0;
  rep(i, N) { sum += A.at(i); }

  rep(i, Q) {
    int num = count.at(B.at(i));
    int diff = C.at(i) - B.at(i);
    count.at(B.at(i)) = 0;
    count.at(C.at(i)) += num;
    sum += diff * num;
    S.at(i) = sum;
  }

  rep(i, Q) { cout << S.at(i) << endl; }
}

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() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) { cin >> B.at(i) >> C.at(i); }
  int MAX = 100001;
  vector<ll> S(Q);
  vector<int> count(MAX, 0);
  rep(i, N) { count.at(A.at(i))++; }

  long sum = 0;
  rep(i, N) { sum += A.at(i); }

  rep(i, Q) {
    int num = count.at(B.at(i));
    int diff = C.at(i) - B.at(i);
    count.at(B.at(i)) = 0;
    count.at(C.at(i)) += num;
    sum += diff * num;
    S.at(i) = sum;
  }

  rep(i, Q) { cout << S.at(i) << endl; }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 1, 0);
  long long ans = 0;
  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    cnt[a[i]]++;
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  rep(i, q) cin >> b[i] >> c[i];
  rep(i, q) {
    int d = c[i] - b[i];
    ans += d * cnt[b[i]];
    cout << ans << endl;
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(100001, 0);
  long long ans = 0;
  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    cnt[a[i]]++;
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  rep(i, q) cin >> b[i] >> c[i];
  rep(i, q) {
    int d = c[i] - b[i];
    ans += d * cnt[b[i]];
    cout << ans << endl;
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using Graph = vector<vector<int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using Graph = vector<vector<int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
#include <vector>

using namespace std;

vector<string> split(const string &s, char delim) {
  vector<string> elems;
  string item;
  for (char ch : s) {
    if (ch == delim) {
      if (!item.empty()) {
        elems.push_back(item);
      }
      item.clear();
    } else {
      item += ch;
    }
  }
  if (!item.empty()) {
    elems.push_back(item);
  }
  return elems;
}

string to_str_with_zero(int i, int w) {
  ostringstream sout;
  sout << std::setfill('0') << std::setw(w) << i;
  string s = sout.str();
  return s;
}

int letter_to_int(char c) { return tolower(c) - 'a'; }

bool array_equal(vector<int> a1, vector<int> a2) {
  if (a1.size() != a2.size()) {
    return false;
  }
  for (int i = 0; i < a1.size(); i++) {
    if (a1.at(i) != a2.at(i)) {
      return false;
    }
  }
  return true;
}

int gcd(int a, int b) {
  if (a == b) {
    return a;
  } else if (a > b) {
    return gcd(a - b, b);
  } else {
    return gcd(a, b - a);
  }
}

int main() {
  std::cout << std::setprecision(9);
  int n;
  cin >> n;

  vector<int> a_count(100000);
  long sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a_count.at(a)++;
    sum += a;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += a_count.at(b) * (c - b);
    a_count.at(c) += a_count.at(b);
    a_count.at(b) = 0;
    cout << sum << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
#include <vector>

using namespace std;

vector<string> split(const string &s, char delim) {
  vector<string> elems;
  string item;
  for (char ch : s) {
    if (ch == delim) {
      if (!item.empty()) {
        elems.push_back(item);
      }
      item.clear();
    } else {
      item += ch;
    }
  }
  if (!item.empty()) {
    elems.push_back(item);
  }
  return elems;
}

string to_str_with_zero(int i, int w) {
  ostringstream sout;
  sout << std::setfill('0') << std::setw(w) << i;
  string s = sout.str();
  return s;
}

int letter_to_int(char c) { return tolower(c) - 'a'; }

bool array_equal(vector<int> a1, vector<int> a2) {
  if (a1.size() != a2.size()) {
    return false;
  }
  for (int i = 0; i < a1.size(); i++) {
    if (a1.at(i) != a2.at(i)) {
      return false;
    }
  }
  return true;
}

int gcd(int a, int b) {
  if (a == b) {
    return a;
  } else if (a > b) {
    return gcd(a - b, b);
  } else {
    return gcd(a, b - a);
  }
}

int main() {
  std::cout << std::setprecision(9);
  int n;
  cin >> n;

  vector<int> a_count(100001);
  long sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a_count.at(a)++;
    sum += a;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += a_count.at(b) * (c - b);
    a_count.at(c) += a_count.at(b);
    a_count.at(b) = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N;
  cin >> N;
  ll A;
  vector<ll> vec;
  vec.reserve(10001);
  for (auto i = 0; i < 10001; i++) {
    vec.push_back(0);
  }
  ll ret = 0;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    vec[A]++;
    ret += A;
  }
  ll Q, B, C;
  cin >> Q;
  for (auto i = 0; i < Q; i++) {
    cin >> B >> C;
    ret += (C - B) * vec[B];
    vec[C] += vec[B];
    vec[B] = 0;
    cout << ret << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N;
  cin >> N;
  ll A;
  vector<ll> vec;
  ll limit = pow(10, 6);
  vec.reserve(limit);
  for (auto i = 0; i < limit; i++) {
    vec.push_back(0);
  }
  ll ret = 0;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    vec[A]++;
    ret += A;
  }
  ll Q, B, C;
  cin >> Q;
  for (auto i = 0; i < Q; i++) {
    cin >> B >> C;
    ret += (C - B) * vec[B];
    vec[C] += vec[B];
    vec[B] = 0;
    cout << ret << endl;
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
/* author   : mpily :-()
title : Competing.cpp
time :Sun Jun 14 13:11:23 2020

*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

void solve(int cnum) {
  int64_t n;
  cin >> n;
  vector<int64_t> arr(n);
  vector<int64_t> freq(n);
  int64_t sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    sum += arr[i];
    freq[arr[i]]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int64_t a, b;
    cin >> a >> b;
    int64_t num = freq[a];
    int64_t torem = a * num;
    freq[a] = 0;
    sum -= torem;
    torem = b * num;
    sum += torem;
    freq[b] += num;
    cout << sum << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout.tie(nullptr);
  int t;
  int count = 1;
  for (t = 1; t--; solve(count))
    ;
  return 0;
}

Fixed submission:
/* author   : mpily :-()
title : Competing.cpp
time :Sun Jun 14 13:11:23 2020

*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

void solve(int cnum) {
  int64_t n;
  cin >> n;
  vector<int64_t> arr(n);
  vector<int64_t> freq(100005);
  int64_t sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    sum += arr[i];
    freq[arr[i]]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int64_t a, b;
    cin >> a >> b;
    int64_t num = freq[a];
    int64_t torem = a * num;
    freq[a] = 0;
    sum -= torem;
    torem = b * num;
    sum += torem;
    freq[b] += num;
    cout << sum << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout.tie(nullptr);
  int t;
  int count = 1;
  for (t = 1; t--; solve(count))
    ;
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
using namespace std;
// typedef long long LL;
#define For(a, b, c) for (int a = b; a <= c; a++)
#define For_(a, b, c) for (int a = b; a >= c; a--)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define debug(x) cout << #x << " " << x << " "
int read() {
  int r = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    r = (r << 1) + (r << 3) + c - '0';
    c = getchar();
  }
  return r * f;
}
#define MAXN 10005
int cnt[MAXN], n, q;
long long ans;
int main() {
  n = read();
  For(i, 1, n) {
    int x = read();
    ans += x;
    cnt[x]++;
  }
  q = read();
  For(i, 1, q) {
    int b = read(), c = read();
    ans -= 1ll * cnt[b] * b;
    ans += 1ll * cnt[b] * c;
    printf("%lld\n", ans);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
using namespace std;
// typedef long long LL;
#define For(a, b, c) for (int a = b; a <= c; a++)
#define For_(a, b, c) for (int a = b; a >= c; a--)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define debug(x) cout << #x << " " << x << " "
int read() {
  int r = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    r = (r << 1) + (r << 3) + c - '0';
    c = getchar();
  }
  return r * f;
}
#define MAXN 100005
int cnt[MAXN], n, q;
long long ans;
int main() {
  n = read();
  For(i, 1, n) {
    int x = read();
    ans += x;
    cnt[x]++;
  }
  q = read();
  For(i, 1, q) {
    int b = read(), c = read();
    ans -= 1ll * cnt[b] * b;
    ans += 1ll * cnt[b] * c;
    printf("%lld\n", ans);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  return 0;
}