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

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


Error: 0

Fixed submission:

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

#include <bits/stdc++.h>

#define endl "\\n"
#define INF INT_MAX
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;

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

  ll q, n, m, sum = 0, b, c;
  cin >> n;
  map<ll, ll> data;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    sum += m;
    data[m]++;
  }
  cin >> q;

  while (q--) {
    cin >> b >> c;
    ll count = data[b];
    if (count > 0) {
      data[b] = 0;
      data[c] += count;
      sum -= b * count;
      sum += c * count;
    }
    cout << sum << endl;
  }

  return 0;
} // AntiD2ta


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define endl "\\n"
#define INF INT_MAX
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;

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

  ll q, n, m, sum = 0, b, c;
  cin >> n;
  multiset<ll> s;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    sum += m;
    s.insert(m);
  }
  cin >> q;

  while (q--) {
    cin >> b >> c;
    ll count = s.count(b);
    if (count > 0) {
      s.erase(b);
      for (int i = 0; i < count; ++i) {
        s.insert(c);
      }
      sum -= b * count;
      sum += c * count;
    }
    cout << sum << endl;
  }

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

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

void solve() {
  int n;
  cin >> n;
  long long a[100001] = {0};
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    sum += x;
    a[x]++;
  }
  int q;
  cin >> q;
  while (q--) {
    long long dif = 0;
    long long x, y;
    cin >> x >> y;
    long long raz = y - x;
    a[y] += a[x];
    dif = raz * a[x];
    a[x] = 0;
    sum += dif;
    cout << sum << endl;
  }
}

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

  int t = 1;
  //    cin>>t;
  for (; t; t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void solve() {
  int n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    long long dif = 0;
    long long x, y;
    cin >> x >> y;
    long long raz = y - x;
    for (int i = 0; i < n; i++) {
      if (a[i] == x) {
        a[i] = y;
        dif += raz;
      }
    }
    sum += dif;
    cout << sum << endl;
  }
}

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

  int t = 1;
  //    cin>>t;
  for (; t; t--) {
    solve();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> f(100003, 0);
  long long int res = 0;
  for (int i = 0; i <= n - 1; i++) {
    cin >> a[i];
    f[a[i]]++;
    res += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    res += f[b] * (c - b);
    f[c] += f[b];
    f[b] = 0;
    cout << res << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> f(n, 0);
  long long int res = 0;
  for (int i = 0; i <= n - 1; i++) {
    cin >> a[i];
    f[a[i]]++;
    res += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    res += f[b] * (c - b);
    f[c] += f[b];
    cout << res << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/* ~ Programmer >> Tushar Mahajan */
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long int
#define MOD 1000000007
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

int main() {
  ios;
  ll n, q;
  ll sum = 0;
  cin >> n;
  ll a[n];
  map<ll, ll> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  while (q--) {
    ll b1, c1;
    cin >> b1 >> c1;
    ll value = m[b1];
    sum = sum - (b1 * value);
    sum = sum + (c1 * value);
    m[b1] = 0;
    m[c1] += value;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

/* ~ Programmer >> Tushar Mahajan */
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long int
#define MOD 1000000007
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

int main() {
  ios;
  int n, q, sum;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  while (q--) {
    sum = 0;
    int b1, c1;
    cin >> b1 >> c1;
    for (int i = 0; i < n; i++) {
      if (a[i] == b1) {
        a[i] = c1;
      }
      sum += a[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, b, c, t, s = 0, i, l;
  cin >> n;
  ll a[10001];
  map<ll, ll> m;
  for (i = 1; i <= n; i++) {
    cin >> l;
    m[l]++;
    s += l;
  }
  cin >> t;
  while (t--) {
    cin >> b >> c;
    s += (m[b] * (c - b));
    m[c] += m[b];
    m.erase(m.find(b));
    cout << s << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ll n, b, c, t, s = 0, i, l;
  cin >> n;
  ll a[10001] = {0};
  for (i = 1; i <= n; i++) {
    cin >> l;
    a[l]++;
    s += l;
  }
  cin >> t;
  while (t--) {
    cin >> b >> c;
    s += (a[b] * (c - b));
    l = a[b];
    a[b] = 0;
    a[c] += l;
    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 all(v) v.begin(), v.end()
//
int main() {
  int n, q;
  cin >> n;
  vector<int> v;
  rep(i, n) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  cin >> q;
  vector<pair<int, int>> vv;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    vv.push_back(pair<int, int>(a, b));
  }
  long long ans = 0;
  map<int, int> p;
  for (auto e : v) {
    ans = ans + e;
    p[e]++;
  }
  rep(i, q) {
    int t = p[vv[i].first];
    ans = ans + t * (vv[i].second - vv[i].first);
    cout << ans << endl;
    p[vv[i].second] += p[vv[i].first];
    p[vv[i].first] = 0;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
//
int main() {
  int n, q;
  cin >> n;
  vector<int> v;
  rep(i, n) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  cin >> q;
  vector<pair<int, int>> vv;
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    vv.push_back(pair<int, int>(a, b));
  }
  long long ans = 0;
  for (auto e : v)
    ans = ans + e;
  rep(i, q) {
    int t = count(all(v), vv[i].first);
    ans = ans + t * (vv[i].second - vv[i].first);
    cout << ans << endl;
    if (t == 0)
      continue;
    replace(all(v), vv[i].first, vv[i].second);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
typedef long long ll;

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


Error: 0

Fixed submission:

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

using namespace std;
typedef long long ll;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define int long long

void fastio() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  fastio();

  int n;
  cin >> n;
  map<int, int> cnt;
  int sum = 0;
  while (n--) {
    int a;
    cin >> a;
    sum += a;
    cnt[a]++;
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define int long long

void fastio() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  fastio();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    replace(a, a + n, x, y);
    int s = 0;
    for (int i = 0; i < n; ++i) {
      s = s + a[i];
    }
    cout << s << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int s[100004], m[100004];
int main() {
  long long t, ans = 0;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> s[i];
    m[s[i]]++;
    ans += s[i];
  }
  long long r;
  cin >> r;
  for (int i = 0; i < r; i++) {
    long long a, b;
    cin >> a >> b;
    long long q = b - a;
    ans += m[a] * q;
    m[b] += m[a];
    m[a] = 0;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int s[100004];
int main() {
  int t, ans = 0;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> s[i];
    ans += s[i];
  }
  int r;
  cin >> r;
  for (int i = 0; i < r; i++) {
    int a, b;
    cin >> a >> b;
    int q = b - a;
    for (int j = 0; j < t; j++)
      if (s[j] == a) {
        ans += q;
        s[j] = b;
      }
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int q;
  cin >> q;
  for (int j = 0; j < q; j++) {
    int b, c;
    cin >> b >> c;
    if (ac[b] > 0) {
      v += (long long)(ac[b] * (c - b));
      ac[c] += ac[b];
      ac[b] = 0;
    }
    cout << v << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  for (int j = 0; j < q; j++) {
    int b, c, v = 0;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
      v += a[i];
    }
    cout << v << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cstring>
#include <limits>
using namespace std;
#define ll long long int
#define lli long long int
#define fw(i, s, e) for (ll i = s; i < e; ++i)
#define fe(i, s, e) for (ll i = s; i <= e; ++i)
#define fb(i, e, s) for (ll i = e; i >= s; --i)
#define L(T)                                                                   \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define mem(a, i) memset(a, i, sizeof(a))
#define ld long double
#define endl "\\n"
#define in(P)                                                                  \\
  ll P;                                                                        \\
  cin >> P;
ll h[100034];
int main() {
  FASTIO
  // L(T)
  {
    in(n);

    ll a[n + 90];
    ll s = 0;
    fw(i, 0, n) {
      cin >> a[i];
      h[a[i]]++;
      s += a[i];
    }
    in(q);
    fw(i, 0, q) {
      in(b);
      in(c);
      ll temp = c * h[b] - b * h[b];
      h[c] = h[c] + h[b];
      h[b] = 0;
      s += temp;
      cout << s << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstring>
#include <limits>
using namespace std;
#define ll long long int
#define lli long long int
#define fw(i, s, e) for (ll i = s; i < e; ++i)
#define fe(i, s, e) for (ll i = s; i <= e; ++i)
#define fb(i, e, s) for (ll i = e; i >= s; --i)
#define L(T)                                                                   \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define mem(a, i) memset(a, i, sizeof(a))
#define ld long double
#define endl "\\n"
#define in(P)                                                                  \\
  ll P;                                                                        \\
  cin >> P;
int main() {
  FASTIO
  ll h[1034];
  // L(T)
  {
    in(n);

    ll a[n + 90];
    ll s = 0;
    fw(i, 0, n) {
      cin >> a[i];
      h[a[i]]++;
      s += a[i];
    }
    in(q);
    fw(i, 0, q) {
      in(b);
      in(c);
      ll temp = c * h[b] - b * h[b];
      h[c] = h[c] + h[b];
      h[b] = 0;
      s += temp;
      cout << s << endl;
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  // 初期化
  int N;
  cin >> N;

  // 1～10^5のAを数える
  vector<int> count_A(100000, 0);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    count_A.at(a - 1)++;
  }

  int Q;
  cin >> Q;
  // 答え
  int64_t sum = 0;

  // 初期配列の合計値
  for (int i = 0; i < 100000; i++) {
    sum += count_A.at(i) * (i + 1);
  }

  // Q回の操作
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    // 合計値の変化を計算
    sum += (c - b) * count_A.at(b - 1);
    cout << sum << endl;

    count_A.at(c - 1) += count_A.at(b - 1);
    count_A.at(b - 1) = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int64_t sum_out(vector<int> &count) {
  int64_t sum = 0;
  for (int i = 0; i < 100000; i++) {
    sum += count.at(i) * (i + 1);
  }
  return sum;
}

int main() {
  int N;
  cin >> N;
  vector<int> count(100000, 0);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    count.at(a - 1)++;
  }
  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    count.at(c - 1) += count.at(b - 1);
    count.at(b - 1) = 0;
    cout << sum_out(count) << endl;
  }

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

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

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

  ll sum = 0;
  ll A[100010] = {};
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    sum += a;
    A[a]++;
  }

  int q;
  cin >> q;

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

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

    A[c] += A[b];
    A[b] = 0;
  }
  return 0;
}


Error: TLE

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;

  int A[100010] = {};
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A[a] = A[a] + 1;
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    int s = 0;
    A[c] = A[c] + A[b];
    A[b] = 0;
    for (int j = 1; j <= 100000; j++) {
      s += j * A[j];
    }
    cout << s << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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


Error: WA

Fixed submission:

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

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

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

  // for(int i=0;i<q;++i) cout<<ans[i]<<endl;
  for (int i = 0; i < q; ++i)
    printf("%lld \\n", ans[i]);
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  // input
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> numa(100000);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    numa[a[i] - 1]++;
  }
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  // input end

  for (int i = 0; i < q; i++) {
    int numB = numa[b[i] - 1];
    numa[b[i] - 1] = 0;
    numa[c[i] - 1] += numB;
    sum = sum + numB * (c[i] - b[i]);
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  // input
  int n;
  cin >> n;
  vector<int> a(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  vector<int> b(q), c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  // input end

  for (int i = 0; i < q; i++) {
    stable_sort(a.begin(), a.end(), less<int>());
    for (int j = 0; j < n; j++) {
      if (a[j] > b[i]) {
        break;
      }

      if (a[j] == b[i]) {
        sum = sum + c[i] - a[j];
        a[j] = c[i];
      }
    }
    cout << sum << "\\n";
  }

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

#include <bits/stdc++.h>

typedef unsigned long long int ll;

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  unordered_map<int, int> mp;
  for (auto &ele : arr) {
    mp[ele]++;
  }
  int q;
  cin >> q;
  // vector<int> a(q), b(q);
  vector<ll> ans;
  int a, b;
  ll sum = accumulate(arr.begin(), arr.end(), 0ll);
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (mp.count(a)) {
      sum += (mp[a] * (b - a));
      mp[b] += mp[a];
      mp.erase(a);
    }
    ans.push_back(sum);
  }
  for (auto &ele : ans) {
    cout << ele << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int q;
  cin >> q;
  vector<int> a(q), b(q);
  vector<int> ans;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i];
    int cur = 0;
    for (int j = 0; j < n; j++) {
      if (arr[j] == a[i])
        arr[j] = b[i];
      cur += arr[j];
    }
    ans.push_back(cur);
  }
  for (auto &ele : ans) {
    cout << ele << endl;
  }
  return 0;
}