alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
int main() {
  ll A;
  cin >> A;
  ll b = 0;
  vector<ll> vec(100000 + 1, 0);
  for (ll i = 0; i < A; i++) {
    ll a;
    cin >> a;
    vec.at(a) += 1;
    b += a;
  }

  ll K;
  cin >> K;
  for (ll i = 0; i < K; i++) {
    ll c, d;
    cin >> c >> d;
    b += (d - c) * vec.at(c);
    vec.at(d) += vec.at(c);
    vec.at(c) = 0;
    cout << b << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
int main() {
  ll A;
  cin >> A;
  ll b = 0;
  vector<ll> vec(A);
  for (ll i = 0; i < A; i++) {
    cin >> vec.at(i);
    b += vec.at(i);
  }
  ll B;
  cin >> B;
  for (ll i = 0; i < B; i++) {
    ll c, d;
    cin >> c >> d;
    b = b - vec.at(c - 1) + d;
    cout << b << endl;
    vec.at(c - 1) = d;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  vector<int> count(100001);
  vector<int> a(100001), b(100001), c(100001);
  int n;
  long long int sum = 0LL;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sum += a.at(i);
    count.at(a.at(i))++;
  }

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

Error: TLE

Fixed submission:

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

int main() {
  vector<int> count(100001);
  vector<int> a(100001), b(100001), c(100001);
  int n;
  int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sum += a.at(i);
    count.at(a.at(i))++;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b.at(i) >> c.at(i);
    for (int j = 0; j < 100001; j++) {
      if (j == b.at(i)) {
        sum += (count.at(j) * (c.at(i) - b.at(i)));
        count.at(c.at(i)) += count.at(b.at(i));
        count.at(b.at(i)) = 0;
        cout << sum << endl;
        break;
      }
    }
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  //  freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  map<ll, ll> m;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    sum += x;
    m[x]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll x = b - a;
    ll count = m[a];
    if (m[a] != 0) {
      m[b] += m[a];
      m[a] = 0;
      sum += (x * count);
    }
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  //	freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll n;
  cin >> n;
  map<ll, ll> m;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    m[x]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll sum = 0;
    if (m[b] != 0) {
      m[c] += m[b];
      m[b] = 0;
    }
    for (auto x : m) {
      sum += (x.first) * (x.second);
    }
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int aa = 100001;
int main() {

  int N, Q, B, C, A;
  cin >> N;
  int ct[aa];
  for (int i = 0; i < aa; i++) {
    ct[i] = 0;
  }
  long long int total = 0;
  for (int i = 0; i < N; i++) {
    cin >> A;
    total += A;
    ct[A]++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    int temp = C - B;
    total += temp * ct[B];
    cout << total << endl;
    ct[C] += ct[B];
    ct[B] = 0;
  }
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  int N, Q, B, C, A;
  cin >> N;
  int ct[N + 1];
  for (int i = 0; i < N + 1; i++) {
    ct[i] = 0;
  }
  long long int total = 0;
  for (int i = 0; i < N; i++) {
    cin >> A;
    total += A;
    ct[A]++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    int temp = C - B;
    total += temp * ct[B];
    cout << total << endl;
    ct[C] += ct[B];
    ct[B] = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  uint64_t N, Q;
  cin >> N;
  unordered_map<uint64_t, int> hmap;
  uint64_t tot = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if (hmap.find(a) != hmap.end())
      hmap[a] += 1;
    else
      hmap[a] = 1;
    tot += a;
  }
  cin >> Q;
  vector<uint64_t> ans(Q);
  for (int i = 0; i < Q; ++i) {
    int b, c;
    cin >> b >> c;
    ans[i] = i > 0 ? ans[i - 1] : tot;
    auto it = hmap.find(b);
    if (it != hmap.end()) {
      int &nb = it->second;
      hmap[c] += nb;
      ans[i] += (c - b) * nb;
      nb = 0;
    }
  }
  for (int i = 0; i < Q; ++i)
    cout << ans[i] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  uint64_t N, Q;
  cin >> N;
  unordered_map<uint64_t, int> hmap;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if (hmap.find(a) != hmap.end())
      hmap[a] += 1;
    else
      hmap[a] = 1;
  }
  cin >> Q;
  vector<uint64_t> ans(Q);
  for (int i = 0; i < Q; ++i) {
    int b, c;
    cin >> b >> c;
    hmap[c] += hmap[b];
    hmap.erase(hmap.find(b));
    ans[i] = 0;
    for (auto &p : hmap) {
      ans[i] += p.first * p.second;
    }
  }
  for (int i = 0; i < Q; ++i)
    cout << ans[i] << "\\n";
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

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

Error: 0

Fixed submission:

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

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

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

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

  rep(i, 0, Q) {
    int B = q[i].first;
    int C = q[i].second;
    num[C] += num[B];
    acum = acum + (num[B] * (C - B));
    num[B] = 0;
    cout << acum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>
#define ll long long
#define PI 3.14159265358979323846
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep1(x, n) for (ll x = 1; x < (ll)n + 1; x++)
using namespace std;
const ll INF = 100000;
int main() {
  ll N, Q, sum = 0;
  cin >> N;
  vector<ll> A(N);
  vector<ll> W(INF, 0);
  rep(i, N) {
    cin >> A[i];
    W[A[i]]++;
  }
  rep(i, N) sum += A[i];
  cin >> Q;
  vector<ll> ans(Q);
  rep(i, Q) {
    ll B, C;
    cin >> B >> C;
    W[C] += W[B];
    sum += (C - B) * W[B];
    W[B] = 0;
    ans[i] = sum;
  }
  rep(i, Q) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>
#define ll long long
#define PI 3.14159265358979323846
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep1(x, n) for (ll x = 1; x < (ll)n + 1; x++)
using namespace std;

int main() {
  ll N, Q;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    sort(A.begin(), A.end());
    ll ans = 0;
    ll j = 0;
    if (A[N / 2] < B[i])
      j = N / 2;
    for (j; j < N; j++) {
      if (A[j] == B[i])
        A[j] = C[i];
    }
    rep(j, N) ans += A[j];
    cout << ans << endl;
  }

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

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

int main() {
  int n;
  cin >> n;
  int a;
  int t[100010];
  rep(i, 100010) { t[i] = 0; }
  ll s = 0;
  rep(i, n) {
    cin >> a;
    t[a]++;
    s += a;
  }

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

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a;
  vector<int> t(n, 0);
  ll s = 0;
  rep(i, n) {
    cin >> a;
    t[a]++;
    s += a;
  }

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

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long int lli;

int main() {
  lli N, Q;
  map<lli, lli> val;
  vector<lli> A, B, C;
  cin >> N;
  for (lli i = 0; i < N; i++) {
    lli a;
    cin >> a;
    A.push_back(a);
    val[a] = 0;
  }
  cin >> Q;
  for (lli i = 0; i < N; i++) {
    val[A[i]]++;
  }
  for (lli i = 0; i < Q; i++) {
    lli b, c;
    cin >> b;
    cin >> c;
    B.push_back(b);
    C.push_back(c);
  }
  lli S = 0;
  for (lli i = 0; i < Q; i++) {
    if (i == 0) {
      val[C[i]] += val[B[i]];
      val[B[i]] = 0;
      for (auto itr = val.begin(); itr != val.end(); ++itr) {
        S += (itr->first) * (itr->second);
      }
    } else {
      lli dif;
      dif = (C[i] - B[i]) * val[B[i]];
      S += dif;
      val[C[i]] += val[B[i]];
      val[B[i]] = 0;
    }

    cout << S << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long int lli;

int main() {
  lli N, Q;
  map<lli, lli> val;
  vector<lli> A, B, C;
  cin >> N;
  for (lli i = 0; i < N; i++) {
    lli a;
    cin >> a;
    A.push_back(a);
    val[a] = 0;
  }
  cin >> Q;
  for (lli i = 0; i < N; i++) {
    val[A[i]]++;
  }
  for (lli i = 0; i < Q; i++) {
    lli b, c;
    cin >> b;
    cin >> c;
    B.push_back(b);
    C.push_back(c);
  }
  for (lli i = 0; i < Q; i++) {
    val[C[i]] += val[B[i]];
    val[B[i]] = 0;
    lli S = 0;
    for (auto itr = val.begin(); itr != val.end(); ++itr) {
      S += (itr->first) * (itr->second);
    }
    cout << S << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF MOD
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF MOD
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int main() {
  int n;
  cin >> n;
  Int a[n];
  map<Int, Int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  int q;
  cin >> q;
  Int ans[q];
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int p = mp[b];
    mp[b] = 0;
    mp[c] += p;
    Int su = 0;
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
      su += itr->first * itr->second;
    }
    ans[i] = su;
  }
  rep(i, q) cout << ans[i] << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, Q, b, c, d;
  long long int s = 0;
  int A[100010] = {};

  cin >> N;

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

  cin >> Q;

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

    s = s - A[b] * (b - c);
    A[c] = A[b] + A[c];
    A[b] = 0;
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, Q, b, c;
  long long int s = 0;

  cin >> N;
  int A[N];

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

  cin >> Q;

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

    for (int j = 0; j < N; j++) {
      // cout << A[j] << endl;;
      if (b == A[j]) {
        s = s - A[j] + c;
        A[j] = c;
      }
    }
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

/* 繰り返し自乗法. */
int modPow(long long a, long long n, long long p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合.
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

signed main() {
  int N, Q;
  ll sum = 0;
  cin >> N;
  vector<int> L(100010);
  rep(i, N) {
    int a;
    cin >> a;
    L[a]++;
    sum += a;
  }
  cin >> Q;
  ll S[Q];
  rep(i, Q) {
    int b, c;
    cin >> b >> c;
    sum += (L[b] * (c - b));
    L[c] += L[b];
    L[b] = 0;
    S[i] = sum;
  }
  rep(i, Q) cout << S[i] << endl;
}

Error: TLE

Fixed submission:

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

/* 繰り返し自乗法. */
int modPow(long long a, long long n, long long p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合.
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

signed main() {
  int N;
  cin >> N;
  int S[N];
  rep(i, N) cin >> S[i];

  int Q;
  cin >> Q;
  int a[Q];
  int b[Q];
  rep(i, Q) { cin >> a[i] >> b[i]; }
  //        rep (j, N) cout << S[j] << ',';
  //        cout << endl;
  rep(i, Q) {
    /*置き換え.*/
    rep(j, N) {
      if ((a[i]) == S[j])
        S[j] = b[i];
    }
    //        rep (j, N) cout << S[j] << ',';
    //      cout << endl;

    ll sum = 0;
    rep(j, N) { sum += (S[j]); }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int solve() {
  int n;
  cin >> n;
  vector<ll> v(100001, 0);
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    int ii;
    cin >> ii;
    v[ii]++;
    sum += ii;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int ll, mm;
    cin >> ll >> mm;
    sum -= v[ll] * ll;
    sum += v[ll] * mm;
    v[mm] = v[mm] + v[ll];
    v[ll] = 0;

    cout << sum << "\\n";
  }

  return 0;
}

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int solve() {
  int n;
  cin >> n;
  vector<int> v(100001, 0);
  for (int i = 1; i <= n; i++) {
    int ii;
    cin >> ii;
    v[ii]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int ll, mm;
    cin >> ll >> mm;
    v[mm] = v[mm] + v[ll];
    v[ll] = 0;

    int sum = 0;
    for (int j = 0; j <= 100001; j++) {
      sum += v[j] * j;
    }
    cout << sum << "\\n";
  }

  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define psb push_back
#define ppb pop_back

const int I_INF = 1001001001;
const ll LL_INF = 1001001001001001001;
const ld EPS = 0.001;

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

  map<ll, ll> a;
  rep(i, N) {
    ll n = 0;
    cin >> n;
    a[n]++;
  }

  cin >> Q;

  ll sum = 0;
  for (const auto [k, v] : a)
    sum += k * v;

  vl ans;
  rep(i, Q) {
    ll b, c;
    cin >> b >> c;

    if (!a.count(b)) {
      ans.psb(sum);
      continue;
    }

    ll v = a[b];
    sum -= b * v;
    a.erase(b);
    a[c] += v;
    sum += c * v;

    ans.psb(sum);
  }

  for (const auto n : ans)
    cout << n << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define psb push_back
#define ppb pop_back

const int I_INF = 1001001001;
const ll LL_INF = 1001001001001001001;
const ld EPS = 0.001;

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

  map<ll, ll> a;
  rep(i, N) {
    ll n = 0;
    cin >> n;
    a[n]++;
  }

  cin >> Q;
  vl ans;
  rep(i, Q) {
    ll b, c;
    cin >> b >> c;

    if (a.count(b)) {
      ll v = a[b];
      a.erase(b);
      a[c] += v;
    }

    ll s = 0;
    for (const auto [key, value] : a)
      s += key * value;
    ans.psb(s);
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define xrep(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
using ll = long long;
const ll INF = 100100100;

int main() {
  ll N, Q;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) {
    cin >> B[i];
    cin >> C[i];
  }
  ll sum = 0;
  vector<ll> cntA(10000000, 0);
  for (auto a : A) {
    sum += a;
    cntA[a]++;
  }

  rep(i, Q) {
    ll inc = (C[i] - B[i]) * cntA[B[i]];
    cntA[C[i]] += cntA[B[i]];
    cntA[B[i]] = 0;
    sum += inc;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  int Q;
  cin >> Q;
  int B, C;
  vector<int> ansVec;
  sort(A.begin(), A.end());
  rep(i, Q) {
    cin >> B >> C;
    int cnt = 0;
    rep(j, N) {
      if (A[j] == B) {
        A[j] = C;
        cnt++;
        continue;
      } else if (cnt > 0)
        break;
    }
    int ans = 0;
    for (int a : A) {
      ans += a;
    }
    ansVec.push_back(ans);
  }

  for (int an : ansVec)
    cout << an << endl;

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

#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using numr = std::numeric_limits<T>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll LLINF = 1e16;
const int MOD = 1000000007;
const int MOD2 = 998244353;

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> num(100100);
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += A[i];
    num[A[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using numr = std::numeric_limits<T>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll LLINF = 1e16;
const int MOD = 1000000007;
const int MOD2 = 998244353;

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int n, q;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (std::find(A.begin(), A.end(), b) != A.end()) {
      std::replace(A.begin(), A.end(), b, c);
    }
    ll sum = std::accumulate(A.begin(), A.end(), 0);
    cout << sum << endl;
  }
}