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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const long long MAX = 1000000000000001;
long long ruijyo(int a, int n) {
  long long res = 1;
  while (n) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n = n >> 1;
  }
  return res;
}

int main() {
  string alph = "abcdefghijklmnopqrstuvwxyz";
  vector<long long> namesum;
  namesum.push_back(0);
  int i = 0;
  while (namesum.at(i) <= MAX) {
    namesum.push_back(namesum.at(i) * 26 + 26);
    i++;
  }

  long long N;
  cin >> N;
  for (int k = 0; k < namesum.size(); k++) {
    if (namesum.at(k) < N && N <= namesum.at(k + 1)) {
      N = N - namesum.at(k);
      for (int i = k; i >= 0; i--) {
        cout << alph.at((N - 1) / ruijyo(26, i));
        N = N - ((N - 1) / ruijyo(26, i)) * ruijyo(26, i);
      }
      break;
    }
  }
  cout << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const long long MAX = 1000000000000001;
long long ruijyo(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n = n >> 1;
  }
  return res;
}

int main() {
  string alph = "abcdefghijklmnopqrstuvwxyz";
  vector<long long> namesum;
  namesum.push_back(0);
  int i = 0;
  while (namesum.at(i) <= MAX) {
    namesum.push_back(namesum.at(i) * 26 + 26);
    i++;
  }

  long long N;
  cin >> N;
  for (int k = 0; k < namesum.size(); k++) {
    if (namesum.at(k) < N && N <= namesum.at(k + 1)) {
      N = N - namesum.at(k);
      for (int i = k; i >= 0; i--) {
        cout << alph.at((N - 1) / ruijyo(26, i));
        N = N - ((N - 1) / ruijyo(26, i)) * ruijyo(26, i);
      }
      break;
    }
  }
  cout << endl;
}
Problem p02629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
const int INF = 1e15;
using Graph = vector<vector<int>>;

signed main() {
  int N;
  cin >> N;
  string alfa = "abcdefghijklmnopqrstuvwxyz";
  string ans = "";
  while (N > 0) {
    ans += alfa.at(N % 26 - 1);
    N /= 26;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
const int INF = 1e15;
using Graph = vector<vector<int>>;

signed main() {
  int N;
  cin >> N;
  string alfa = "abcdefghijklmnopqrstuvwxyz";
  string ans = "";
  while (N > 0) {
    int A = N % 26;
    A--;
    if (A < 0)
      A = 25;
    ans += alfa.at(A);
    if (A == 25)
      N--;
    N /= 26;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
Problem p02629 (C++)

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

template <typename T> T ipow(T b, long long e) {
  if (e == 0) {
    return 1;
  } else if (e % 2 == 0) {
    return ipow(b * b, e / 2);
  } else {
    return ipow(b * b, e / 2) * b;
  }
}

// aを1番目、zを26番目としてx番目のcharを返す
char get_char(int x) { return 'a' - 1 + x; }

int main() {
  long long N;
  cin >> N;
  int i = 1;
  while (N > ipow<long long>(26, i)) {
    N -= ipow<long long>(26, i);
    i++;
  }
  string ans;
  // i文字の名前のうち、N番目を計算して出力
  for (int j = i; j > 0; j--) {
    int c = 1;
    while (N > ipow(26, j - 1)) {
      N -= ipow(26, j - 1);
      c++;
    }
    ans += get_char(c);
  }
  cout << ans << endl;
  return 0;
}


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

template <typename T> T ipow(T b, long long e) {
  if (e == 0) {
    return 1;
  } else if (e % 2 == 0) {
    return ipow(b * b, e / 2);
  } else {
    return ipow(b * b, e / 2) * b;
  }
}

// aを1番目、zを26番目としてx番目のcharを返す
char get_char(int x) { return 'a' - 1 + x; }

int main() {
  long long N;
  cin >> N;
  int i = 1;
  while (N > ipow<long long>(26, i)) {
    N -= ipow<long long>(26, i);
    i++;
  }
  string ans;
  // i文字の名前のうち、N番目を計算して出力
  for (int j = i; j > 0; j--) {
    int c = 1;
    while (N > ipow<long long>(26, j - 1)) {
      N -= ipow<long long>(26, j - 1);
      c++;
    }
    ans += get_char(c);
  }
  cout << ans << endl;
  return 0;
}
Problem p02629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\n"

int power(int a, int b, int m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int res = power(a, b / 2, m);
  res = (res * res) % m;
  if (b & 1)
    res = (res * a) % m;
  return res;
}
int modinv(int a, int m = mod) { return power(a, m - 2, m); }
int add(int a, int b, int m = mod) {
  int c = (a % m + b % m);
  if (c >= m)
    c -= m;
  return c;
}
int sub(int a, int b, int m = mod) {
  int c = (a % m - b % m);
  if (c < 0)
    c += m;
  return c;
}
int mul(int a, int b, int m = mod) { return (a * b) % m; }
//	x<<1 => x*2		x>>1 => x/2;	cout<<flush;
//	cout<<fixed<<setprecision(10)<<ans<<endl;

void solve() {
  int n, k;
  cin >> n >> k;
  int a[n], i, ans = 0;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (i = 0; i < k; i++)
    ans += a[i];
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\n"

int power(int a, int b, int m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int res = power(a, b / 2, m);
  res = (res * res) % m;
  if (b & 1)
    res = (res * a) % m;
  return res;
}
int modinv(int a, int m = mod) { return power(a, m - 2, m); }
int add(int a, int b, int m = mod) {
  int c = (a % m + b % m);
  if (c >= m)
    c -= m;
  return c;
}
int sub(int a, int b, int m = mod) {
  int c = (a % m - b % m);
  if (c < 0)
    c += m;
  return c;
}
int mul(int a, int b, int m = mod) { return (a * b) % m; }
//	x<<1 => x*2		x>>1 => x/2;	cout<<flush;
//	cout<<fixed<<setprecision(10)<<ans<<endl;

void solve() {
  int n;
  cin >> n;
  string s;
  while (n) {
    int x = (n - 1) % 26;
    char c = x + 'a';
    s += c;
    n = (n - 1) / 26;
  }
  reverse(s.begin(), s.end());
  cout << s << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
Problem p02629 (C++)

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

using namespace std;

vector<char> word;

void remainder(long long int n) {
  if (n != 0) {
    if (n / 26 < 1) {
      word.push_back('a' + n % 26 - 1);
    } else if (n / 26 >= 1) {
      if ((n % 26) == 0) {
        word.push_back('z');
        if (n > 26)
          remainder((n - 26) / 26);
        else
          return;
      } else {
        word.push_back('a' + n % 26 - 1);
        remainder(n / 26);
      }
    }
  } else {
    return;
  }
}

int main() {
  for (int k = 0;; k++) {
    long long int num;
    cin >> num;
    remainder(num);
    for (int i = word.size() - 1; i >= 0; i--)
      cout << word[i];
    cout << endl;
    word.clear();
  }

  return 0;
}

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

using namespace std;

vector<char> word;

void remainder(long long int n) {
  if (n != 0) {
    if (n / 26 < 1) {
      word.push_back('a' + n % 26 - 1);
    } else if (n / 26 >= 1) {
      if ((n % 26) == 0) {
        word.push_back('z');
        if (n > 26)
          remainder((n - 26) / 26);
        else
          return;
      } else {
        word.push_back('a' + n % 26 - 1);
        remainder(n / 26);
      }
    }
  } else {
    return;
  }
}

int main() {
  long long int num;
  cin >> num;
  remainder(num);
  for (int i = word.size() - 1; i >= 0; i--)
    cout << word[i];
  return 0;
}
Problem p02629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, n) for (int i = x; i < n; i++)
#define repr(i, x, n) for (int i = x; i > n; i--)
#define pii pair<int, int>
#define ll long long
const long long iinf = 1e18;
const int inf = 1e9;
const int MOD = (1e6 + 3);
int dir4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int knight[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
                    {1, 2}, {-1, 2}, {1, -2}, {-1, -2}};

using namespace std;

void solve() {
  long long n;
  cin >> n;
  string s = "zabcdefghijklmnopqrstuvwxy";
  string ans = "";
  long long x = 26;
  while (x <= n) {
    ans += s[n % x];
    x = x + x * 26;
    // n -= 26;
    // n /= 26LL;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}
// new line is printed in the main

int main() {
  int t = 1;
  // cin >> t; // comment out if no test case is required
  // int i = 1;
  while (t--) {
    // cout << "Case " << i++ << ": ";
    solve();
    // printf("\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, n) for (int i = x; i < n; i++)
#define repr(i, x, n) for (int i = x; i > n; i--)
#define pii pair<int, int>
#define ll long long
const long long iinf = 1e18;
const int inf = 1e9;
const int MOD = (1e6 + 3);
int dir4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int knight[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
                    {1, 2}, {-1, 2}, {1, -2}, {-1, -2}};

using namespace std;

void solve() {
  long long n;
  cin >> n;
  string s = "zabcdefghijklmnopqrstuvwxy";
  string ans = "";
  while (n) {
    int rem = n % 26;
    ans += s[rem];
    if (rem == 0)
      n--;
    n /= 26LL;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}
// new line is printed in the main

int main() {
  int t = 1;
  // cin >> t; // comment out if no test case is required
  // int i = 1;
  while (t--) {
    // cout << "Case " << i++ << ": ";
    solve();
    // printf("\n");
  }
  return 0;
}
Problem p02629 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int mod = 1e9 + 7;
const int nax = 1e7;
const ll inf = 1e10 + 17;
vector<int> prime(nax, -1);
ll pow1(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2)
      res = (res * 1LL * a);
    a = (a * 1LL * a);
    b /= 2;
  }
  return res;
}
ll mul(ll a, ll b) { return (a * 1LL * b) % mod; }
ll eval(int i) { ll ans = (26 * 1LL * (pow1(26, i) - 1)) / (25 * 1LL); }
int main() {
  int n = 12;
  ll N, i;
  cin >> N;
  for (i = 1; i <= 11; i++) {
    if (eval(i) >= N) {
      break;
    }
  }
  // cout<<i<<endl;
  int y = i - 1;
  N -= eval(y);

  int len = i;
  vector<char> ans(len + 1);
  set<char> unused;
  // cout<<N<<endl;
  for (int k = 0; k < 26; k++) {
    unused.insert((char)(k + 'a'));
  }
  for (int j = 1; j <= len; j++) {
    ll ev = pow1(26, len - j);

    ll x = (N + ev - 1) / ev;
    N -= (x - 1) * ev;
    // cout<<N<<endl;

    ans[j] = (char)(x - 1 + 'a');
    // unused.erase(ans[j]);
  }
  for (int i = 1; i <= len; i++) {
    cout << ans[i];
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int mod = 1e9 + 7;
const int nax = 1e7;
const ll inf = 1e10 + 17;
vector<int> prime(nax, -1);
ll pow1(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2)
      res = (res * 1LL * a);
    a = (a * 1LL * a);
    b /= 2;
  }
  return res;
}
ll mul(ll a, ll b) { return (a * 1LL * b) % mod; }
ll eval(int i) {
  ll ans = (26 * 1LL * (pow1(26, i) - 1)) / (25 * 1LL);
  return ans;
}
int main() {
  int n = 12;
  ll N, i;
  cin >> N;
  for (i = 1; i <= 11; i++) {
    if (eval(i) >= N) {
      break;
    }
  }
  // cout<<i<<endl;
  int y = i - 1;
  N -= eval(y);

  int len = i;
  vector<char> ans(len + 1);
  set<char> unused;
  // cout<<N<<endl;
  for (int k = 0; k < 26; k++) {
    unused.insert((char)(k + 'a'));
  }
  for (int j = 1; j <= len; j++) {
    ll ev = pow1(26, len - j);

    ll x = (N + ev - 1) / ev;
    N -= (x - 1) * ev;
    // cout<<N<<endl;

    ans[j] = (char)(x - 1 + 'a');
    // unused.erase(ans[j]);
  }
  for (int i = 1; i <= len; i++) {
    cout << ans[i];
  }
}
Problem p02629 (C++)

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

int main() {
  int64_t N;
  cin >> N;
  int64_t P = N / 26;
  vector<char> namae(P + 1);
  int64_t i = 0;
  // cout << N << " " << i << endl ;

  while (N != 0) {
    N--;
    int M = N % 26;
    namae.at(i) = 'a' + M;
    N /= 26;
    i++;

    // cout << N << " " << i << endl ;
  }

  for (int j = i - 1; j >= 0; j--) {
    cout << namae.at(j);
  }
}

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

int main() {
  int64_t N;
  cin >> N;
  int P = N / 26;
  vector<char> namae(100);
  int i = 0;
  // cout << N << " " << i << endl ;

  while (N != 0) {
    N--;
    int M = N % 26;
    namae.at(i) = 'a' + M;
    N /= 26;
    i++;

    // cout << N << " " << i << endl ;
  }

  for (int j = i - 1; j >= 0; j--) {
    cout << namae.at(j);
  }
}
Problem p02629 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
i64 N, Len;
string ans;
inline void pre() {
  i64 l = 1, Bas = 26, Ans = 26;
  while (Ans < N) {
    Ans += (Bas *= 26);
    l++;
  }
  ans = string(l, 'a');
  N -= Ans - Bas;
  Len = l;
}
inline void work() {
  pre();
  N--;
  int Bas = 1, Cur = 0;
  for (int i = 1; i < Len; i++)
    Bas *= 26;
  while (Cur < ans.size()) {
    while (N >= Bas)
      N -= Bas, ans[Cur]++;
    Cur++;
    Bas /= 26;
  }
  cout << ans;
}
int main() {
  cin >> N;
  work();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
i64 N, Len;
string ans;
inline void pre() {
  i64 l = 1, Bas = 26, Ans = 26;
  while (Ans < N) {
    Ans += (Bas *= 26);
    l++;
  }
  ans = string(l, 'a');
  N -= Ans - Bas;
  Len = l;
}
inline void work() {
  pre();
  N--;
  i64 Bas = 1, Cur = 0;
  for (int i = 1; i < Len; i++)
    Bas *= 26;
  while (Cur < ans.size()) {
    while (N >= Bas)
      N -= Bas, ans[Cur]++;
    Cur++;
    Bas /= 26;
  }
  cout << ans;
}
int main() {
  cin >> N;
  work();
  return 0;
}
Problem p02629 (C++)

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

using namespace std;

int main() {
  long N;
  cin >> N;
  N--;
  string a_to_z = "abcdefghijklmnopqrstuvwxyz";
  string name = "";
  int max = 0;
  long x = 1;
  long M = N;
  do {
    name = a_to_z.substr(N % 26, 1) + name;
    N = (N - 26) / 26;
    x *= 26;
    max += x;
  } while (M >= max);
  cout << name;
}

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

using namespace std;

int main() {
  long N;
  cin >> N;
  N--;
  string a_to_z = "abcdefghijklmnopqrstuvwxyz";
  string name = "";
  long max = 0;
  long x = 1;
  long M = N;
  do {
    name = a_to_z.substr(N % 26, 1) + name;
    N = (N - 26) / 26;
    x *= 26;
    max += x;
  } while (M >= max);
  cout << name;
}
Problem p02629 (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;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  vector<char> alpha;
  alpha.push_back('z');
  for (char i = 'a'; i <= 'z'; i++) {
    alpha.push_back(i);
  }

  string ans;
  if (n > 26LL) {
    n -= 1;
  } else {
    cout << alpha.at(n % 26) << endl;
    ;
    return 0;
  }

  while (n > 26LL) {
    ll tmp = (n + 1) % 26LL;
    if (tmp != 0)
      ans.push_back(alpha.at(tmp));
    else
      ans.push_back(alpha.at(26));
    n -= 26;
    n /= 26;
  }

  dump(n);
  ans += alpha.at(n + 1);
  reverse(ans.begin(), ans.end());

  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++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  vector<char> alpha;
  alpha.push_back('z');
  for (char i = 'a'; i <= 'z'; i++) {
    alpha.push_back(i);
  }

  string ans;
  if (n > 26LL) {
    n -= 1;
  } else {
    cout << alpha.at(n % 26) << endl;
    ;
    return 0;
  }

  while (n >= 26LL) {
    ll tmp = (n + 1) % 26LL;
    if (tmp != 0)
      ans.push_back(alpha.at(tmp));
    else
      ans.push_back(alpha.at(26));
    n -= 26;
    n /= 26;
  }

  dump(n);
  ans += alpha.at(n + 1);
  reverse(ans.begin(), ans.end());

  cout << ans << endl;
  return 0;
}
Problem p02629 (C++)

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

int main() {
  int a = 26, k = 1;
  long long N;
  cin >> N;
  while (N > a) {
    N -= a;
    a *= 26;
    k++;
  }
  N--;
  for (int i = 0; i < k; i++) {
    a /= 26;
    int b = N / a;
    cout << (char)('a' + b);
    N %= a;
  }
  cout << endl;
}

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

int main() {
  long long N, a = 26, k = 1;
  cin >> N;
  while (N > a) {
    N -= a;
    a *= 26;
    k++;
  }
  N--;
  for (int i = 0; i < k; i++) {
    a /= 26;
    int b = N / a;
    cout << (char)('a' + b);
    N %= a;
  }
  cout << 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 long double ld;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(n) begin(n), end(n)
#define IN(a, x, b) (a <= x && x < b)
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);
template <class T> inline T CHMAX(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T CHMIN(T &a, const T b) {
  return a = (a > b) ? b : a;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);

int main() {
  INIT;
  int n, q;
  cin >> n;
  vector<int> m(105, 0);
  ll total = 0;
  REP(i, n) {
    int a;
    cin >> a;
    m[a] += 1;
    total += a;
  }
  cin >> q;
  int b;
  int c;
  REP(i, q) {
    cin >> b >> c;
    int s = c - b;
    total = total + (m[b] * s);
    cout << total << "\n";
    m[c] += m[b];
    m[b] = 0;
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(n) begin(n), end(n)
#define IN(a, x, b) (a <= x && x < b)
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);
template <class T> inline T CHMAX(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T CHMIN(T &a, const T b) {
  return a = (a > b) ? b : a;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);

int main() {
  INIT;
  int n, q;
  cin >> n;
  vector<int> m(100005, 0);
  ll total = 0;
  REP(i, n) {
    int a;
    cin >> a;
    m[a] += 1;
    total += a;
  }
  cin >> q;
  int b;
  int c;
  REP(i, q) {
    cin >> b >> c;
    int s = c - b;
    total = total + (m[b] * s);
    cout << total << "\n";
    m[c] += m[b];
    m[b] = 0;
  }
  cout << 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> vec(n + 1);
  vec[0] = 0;
  vector<int> a(n + 1, 0);
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> vec[i];
    a[vec[i]]++;
    sum += vec[i];
  }

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    vector<int> totori(2);
    cin >> totori[0] >> totori[1];
    sum = sum + a[totori[0]] * (totori[1] - totori[0]);
    a[totori[1]] += a[totori[0]];
    a[totori[0]] = 0;
    cout << sum << endl;
  }
}

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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n + 1);
  vec[0] = 0;
  vector<int> a(100001, 0);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> vec[i];
    a[vec[i]]++;
    sum += vec[i];
  }

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    vector<int> totori(2);
    cin >> totori[0] >> totori[1];
    sum = sum + a[totori[0]] * (totori[1] - totori[0]);
    a[totori[1]] += a[totori[0]];
    a[totori[0]] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

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

typedef long long ll;
typedef pair<int, int> P;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

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


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

typedef long long ll;
typedef pair<int, int> P;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  int n;
  cin >> n;
  ll a[100005] = {}, s = 0;
  for (int i = 0; i < n; i++) {
    ll ai;
    cin >> ai;
    a[ai]++;
    s += ai;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    s -= b * a[b];
    s += c * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << s << 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++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MX = 10005;

int main() {
  int n, q, b, c;
  ll ans;
  cin >> n;
  vector<ll> k(MX);
  rep(i, n) {
    int a;
    cin >> a;
    k[a]++;
  }
  ans = 0;
  rep(i, MX) ans += i * k[i];
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    ans -= b * k[b];
    ans -= c * k[c];
    k[c] += k[b];
    k[b] = 0;
    ans += c * k[c];
    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>;
const int MX = 100005;

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

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>>
    vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

vector<ll> p(10000 + 10);

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

  ll N;
  string S;
  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); }

  REP(i, N) { p.at(A.at(i))++; }

  ll ans = 0;

  REP(i, N) { ans += A.at(i); }

  REP(i, Q) {
    ll tmp = p.at(B.at(i));
    p.at(C.at(i)) += tmp;

    ll tmp1 = (C.at(i) - B.at(i)) * tmp;

    ans += tmp1;

    cout << ans << endl;

    p.at(B.at(i)) = 0;
  }
}


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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>>
    vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

vector<ll> p(100000 + 10);

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

  ll N;
  string S;
  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); }

  REP(i, N) { p.at(A.at(i))++; }

  ll ans = 0;

  REP(i, N) { ans += A.at(i); }

  REP(i, Q) {
    ll tmp = p.at(B.at(i));
    p.at(C.at(i)) += tmp;

    ll tmp1 = (C.at(i) - B.at(i)) * tmp;

    ans += tmp1;

    cout << ans << endl;

    p.at(B.at(i)) = 0;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define N 100010
int has[N];
int long long n;
int main() {
  int n, q, b, c;
  long long sum, a;
  while (scanf("%d", &n) != EOF) {
    sum = 0;
    for (int i = 0; i <= 100000; i++)
      has[i] = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      has[a]++;
      sum += a;
    }
    scanf("%d", &q);
    while (q--) {
      scanf("%d %d", &b, &c);
      sum -= b * has[b];
      sum += c * has[b];
      has[c] += has[b];
      has[b] = 0;
      printf("%lld\n", sum);
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define N 100010
int has[N];
int long long n;
int main() {
  int n, q, a, b, c;
  long long sum;
  while (scanf("%d", &n) != EOF) {
    sum = 0;
    for (int i = 0; i <= 100000; i++)
      has[i] = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      has[a]++;
      sum += a;
    }
    scanf("%d", &q);
    while (q--) {
      scanf("%d %d", &b, &c);
      sum -= b * has[b];
      sum += c * has[b];
      has[c] += has[b];
      has[b] = 0;
      printf("%lld\n", sum);
    }
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Time Limit Exceeded):
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

typedef long long ll;
const ll mod = 1000000007;
#define MAX 50

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

  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> m;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll c = 0;
    ll x, y;
    cin >> x >> y;
    for (int i = 0; i < n; i++) {
      if (a[i] == x)
        a[i] = y;
      c += a[i];
    }
    cout << c << endl;
  }

  return 0;
}


Fixed submission:
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

typedef long long ll;
const ll mod = 1000000007;
#define MAX 50

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

  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> m;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll c = 0;
    ll x, y;
    cin >> x >> y;
    // cout<<m[x]<<" "<<m[y];
    sum -= (m[x] * x);

    sum += (m[x] * y);
    m[y] += m[x];
    m[x] = 0;

    /*for(int i=0;i<n;i++)
    {
        if(a[i]==x)
        a[i]=y;
        c+=a[i];

    }*/
    cout << sum << endl;
  }

  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];
  ll Q;
  cin >> Q;
  vector<ll> b(N);
  vector<ll> c(N);
  rep(i, Q) cin >> b[i] >> c[i];

  ll ans = 0;
  rep(i, N) ans += a[i];
  map<ll, ll> m;
  rep(i, N) m[a[i]]++;

  rep(i, Q) {
    if (m[b[i]] > 0) {
      // 変更あり
      ans += (c[i] - b[i]) * m[b[i]];
      m[c[i]] += m[b[i]];
      m[b[i]] = 0;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];
  ll Q;
  cin >> Q;
  vector<ll> b(Q);
  vector<ll> c(Q);
  rep(i, Q) cin >> b[i] >> c[i];

  ll ans = 0;
  rep(i, N) ans += a[i];
  map<ll, ll> m;
  rep(i, N) m[a[i]]++;

  rep(i, Q) {
    if (m[b[i]] > 0) {
      // 変更あり
      ans += (c[i] - b[i]) * m[b[i]];
      m[c[i]] += m[b[i]];
      m[b[i]] = 0;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n + 1, 0);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[t]++;
    sum += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    sum -= a[b] * b;
    sum += a[b] * c;
    a[b] = 0;
    cout << sum << '\n';
  }
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(100005, 0);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[t]++;
    sum += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    sum -= a[b] * b;
    sum += a[b] * c;
    a[b] = 0;
    cout << sum << '\n';
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;

int main() {
  ull n, sum = 0, q;
  cin >> n;
  vector<ull> a(100000);
  fill(a.begin(), a.end(), 0);
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    sum += y;
    a.at(y)++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ull count = a.at(b);
    a.at(b) = 0;
    sum += (c - b) * count;
    a.at(c) += count;
    cout << sum << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;

int main() {
  ull n, sum = 0, q;
  cin >> n;
  vector<ull> a(100001);
  fill(a.begin(), a.end(), 0);
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    sum += y;
    a.at(y)++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ull count = a.at(b);
    a.at(b) = 0;
    sum += (c - b) * count;
    a.at(c) += count;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int seive[10000];
int main() {
  int t;
  cin >> t;
  ll sum = 0;
  for (int i = 0; i < t; i++) {
    ll k;
    cin >> k;
    sum += k;
    seive[k]++;
  }
  int k;
  cin >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    sum += ((ll)seive[a] * (b - a));
    seive[b] += seive[a];
    seive[a] = 0;
    cout << sum << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll seive[(int)2e5];
int main() {
  int t;
  cin >> t;
  ll sum = 0;
  for (int i = 0; i < t; i++) {
    ll k;
    cin >> k;
    sum += k;
    seive[k]++;
  }
  int k;
  cin >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    sum += ((ll)seive[a] * (b - a));
    seive[b] += seive[a];
    seive[a] = 0;
    cout << sum << '\n';
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, q;
  cin >> n;
  ll ans = 0;
  vector<ll> cnt(11000, 0);
  for (ll i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    ans += tmp;
    cnt[tmp]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    ans += (y - x) * cnt[x];
    cnt[y] += cnt[x];
    cnt[x] = 0;
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, q;
  cin >> n;
  ll ans = 0;
  vector<ll> cnt(110000, 0);
  for (ll i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    ans += tmp;
    cnt[tmp]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    ans += (y - x) * cnt[x];
    cnt[y] += cnt[x];
    cnt[x] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> backet(N, 0);
  long long sum = 0LL;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    sum += A;
    backet.at(A - 1)++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    sum += backet.at(B - 1) * (C - B);
    backet.at(C - 1) += backet.at(B - 1);
    backet.at(B - 1) = 0;
    cout << sum << endl;
  }
}


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

int main() {
  int N;
  cin >> N;
  vector<int> backet(100000, 0);
  long long sum = 0LL;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    sum += A;
    backet.at(A - 1)++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    sum += backet.at(B - 1) * (C - B);
    backet.at(C - 1) += backet.at(B - 1);
    backet.at(B - 1) = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)

using namespace std;

int main() {
  int n;
  int i;
  long long sum = 0;
  cin >> n;
  vector<long long> cnt(n + 1);
  for (i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
    sum += tmp;
  }
  int q;
  cin >> q;
  for (i = 0; i < q; i++) {
    long long t, f;
    cin >> t >> f;
    sum -= t * cnt[t];
    sum += f * cnt[t];
    cnt[f] += cnt[t];
    cnt[t] = 0;
    cout << sum << "\n";
  }
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)

using namespace std;

int main() {
  int n;
  int i;
  long long sum = 0;
  cin >> n;
  vector<long long> cnt(100002);
  for (i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
    sum += tmp;
  }
  int q;
  cin >> q;
  for (i = 0; i < q; i++) {
    long long t, f;
    cin >> t >> f;
    sum -= t * cnt[t];
    sum += f * cnt[t];
    cnt[f] += cnt[t];
    cnt[t] = 0;
    cout << sum << "\n";
  }
}
Problem p02630 (C++)

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

using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  long long sum = 0;
  vector<int> a(10001, 0);
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a[b]++;
    sum += b;
  }

  int q;
  cin >> q;
  vector<long long> ans;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum -= a[b] * b;
    sum += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    ans.push_back(sum);
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


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

using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  long long sum = 0;
  vector<long long> a(100001, 0);
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a[b]++;
    sum += b;
  }

  int q;
  cin >> q;
  vector<long long> ans;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum -= a[b] * b;
    sum += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    ans.push_back(sum);
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02630 (C++)

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

using namespace std;
using i64 = long long;

i64 table[10001];

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

  return 0;
}

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

using namespace std;
using i64 = long long;

i64 table[100001];

int main() {
  i64 n, q, sum{};
  cin >> n;
  for (i64 i = 0; i < n; i++) {
    i64 a;
    cin >> a;
    sum += a;
    table[a]++;
  }
  cin >> q;
  i64 ans[q];
  for (i64 i = 0; i < q; i++) {
    i64 b, c;
    cin >> b >> c;
    sum += (c - b) * table[b];
    table[c] += table[b];
    table[b] = 0;
    ans[i] = sum;
  }
  for (i64 i = 0; i < q; i++) {
    cout << ans[i] << 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 all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;

int main() {
  const int MAX = 1000;
  vector<int> cnt(MAX, 0);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  sort(all(a));

  ll s = accumulate(all(a), 0ll);

  int q;
  cin >> q;
  vector<pair<int, int>> bc;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    bc.push_back({b, c});
  }

  rep(i, q) {
    int before = bc[i].first;
    int after = bc[i].second;

    int sa = after - before;

    int c = cnt[before];
    s += (c * sa);
    cout << s << endl;

    cnt[before] = 0;
    cnt[after] += c;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;

int main() {
  const int MAX = 100100;
  vector<int> cnt(MAX, 0);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  sort(all(a));

  ll s = accumulate(all(a), 0ll);

  int q;
  cin >> q;
  vector<pair<int, int>> bc;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    bc.push_back({b, c});
  }

  rep(i, q) {
    int before = bc[i].first;
    int after = bc[i].second;

    int sa = after - before;

    int c = cnt[before];
    s += (c * sa);
    cout << s << endl;

    cnt[before] = 0;
    cnt[after] += c;
  }
}
Problem p02630 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> A(10010, 0);
  int long long sum = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[a] += 1;
    sum += a;
  }

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(100010, 0);
  int long long sum = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[a] += 1;
    sum += a;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    sum += c * A[b] - b * A[b];
    A[c] += A[b];
    A[b] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> ctr(n + 1, 0);
    ll val = 0ll;
    for (int i = 1; i <= n; i++) {
      ll a;
      cin >> a;
      ctr[a]++;
      val += a;
    }
    ll q;
    cin >> q;
    while (q--) {
      ll b, c;
      cin >> b >> c;
      val += (c - b) * ctr[b];
      ctr[c] += ctr[b];
      ctr[b] = 0;
      cout << val << "\n";
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> ctr(100001, 0);
    ll val = 0ll;
    for (int i = 1; i <= n; i++) {
      ll a;
      cin >> a;
      ctr[a]++;
      val += a;
    }
    ll q;
    cin >> q;
    while (q--) {
      ll b, c;
      cin >> b >> c;
      val += (c - b) * ctr[b];
      ctr[c] += ctr[b];
      ctr[b] = 0;
      cout << val << "\n";
    }
  }
  return 0;
}
Problem p02630 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(var, start, stop, step)                                           \
  for (long long var = start; var < stop; var += step)
#define vi vector<int>
#define vl vector<long long>
#define fio                                                                    \
  cin.tie(nullptr);                                                            \
  ios::sync_with_stdio(false);                                                 \
  cout.tie(nullptr);

int main() {
  fio;
  ll n;
  cin >> n;
  vi v(n);
  ll sum = 0;
  map<ll, ll> m;
  loop(i, 0, n, 1) {
    cin >> v[i];
    sum += v[i];
    m[v[i]]++;
  }
  ll q;
  cin >> q;
  loop(i, 0, q, 1) {
    ll old, ne;
    cin >> old >> ne;
    replace(v.begin(), v.end(), old, ne);
    sum -= m[old] * old;
    sum += ne * m[old];
    cout << sum << endl;
    m[ne] += m[old];
    m.erase(old);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(var, start, stop, step)                                           \
  for (long long var = start; var < stop; var += step)
#define vi vector<int>
#define vl vector<long long>
#define fio                                                                    \
  cin.tie(nullptr);                                                            \
  ios::sync_with_stdio(false);                                                 \
  cout.tie(nullptr);

int main() {
  fio;
  ll n;
  cin >> n;
  vi v(n);
  ll sum = 0;
  map<ll, ll> m;
  loop(i, 0, n, 1) {
    cin >> v[i];
    sum += v[i];
    m[v[i]]++;
  }
  ll q;
  cin >> q;
  loop(i, 0, q, 1) {
    ll old, ne;
    cin >> old >> ne;
    sum -= m[old] * old;
    sum += ne * m[old];
    cout << sum << endl;
    m[ne] += m[old];
    m.erase(old);
  }
  return 0;
}