alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Name:- OMHARI
  Institution:- UNIVERSIT OF CALCUTTA(INFORMATION TECHNOLOGY)
  Email:- omharicu@gmail.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define ll long long
#define ul unsigned long
#define db double
#define pb push_back
#define mp make_pair
#define el "\\n"
#define vll vector<ll>
#define vi vector<int>
#define all(v) (v.begin(), v.end())
#define rall(v) (v.rbegin(), v.rend())
const ll mod = 1e9 + 7;

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  /***************-Code starts from here-***************/
  ll n, sum = 0, q;
  cin >> n;
  vll v(n), ans(100001, 0);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    ans[v[i]]++;
    sum += v[i];
  }
  cin >> q;
  vector<pair<ll, ll>> qu(q);

  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    qu[i] = mp(x, y);
  }

  for (ll i = 0; i < q; i++) {
    ll temp = ans[qu[i].first];
    ans[qu[i].first] = 0;
    ans[qu[i].second] += temp;
    sum = sum + temp * qu[i].second - temp * qu[i].first;
    cout << sum << el;
  }
  return 0;
}


Error: TLE

Fixed submission:

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Name:- OMHARI
  Institution:- UNIVERSIT OF CALCUTTA(INFORMATION TECHNOLOGY)
  Email:- omharicu@gmail.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define ll long long
#define ul unsigned long
#define db double
#define pb push_back
#define mp make_pair
#define el "\\n"
#define vll vector<ll>
#define vi vector<int>
#define all(v) (v.begin(), v.end())
#define rall(v) (v.rbegin(), v.rend())
const ll mod = 1e9 + 7;

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  /***************-Code starts from here-***************/
  ll n, sum = 0, q;
  cin >> n;
  vll v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  sort all(v);
  cin >> q;
  vector<pair<ll, ll>> qu(q);

  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    qu[i] = mp(x, y);
  }

  for (ll i = 0; i < q; i++) {
    for (ll j = 0; j < n; j++) {
      if (v[j] == qu[i].first) {
        sum = sum + qu[i].second - qu[i].first;
        v[j] = qu[i].second;
      }
    }
    cout << sum << el;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define INF 1e9

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
}

int main() {
  init();
  int n;
  cin >> n;
  int a;
  vector<unsigned long long> A(100000);
  ll ans;
  rep(i, n) {
    cin >> a;
    A[a]++;
    ans += a;
  }
  int q;
  cin >> q;
  vector<unsigned long long> data(q);
  unsigned long long b, c;
  rep(i, q) {
    cin >> b >> c;
    if (A[b] != 0) {
      ans += (c - b) * A[b];
      A[c] += A[b];
      A[b] = 0;
    }
    data[i] = ans;
  }

  rep(i, q) { cout << data[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define INF 1e9

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
}

int main() {
  init();
  int n;
  cin >> n;
  int a;
  vector<int> A(100001, 0);
  rep(i, n) {
    cin >> a;
    A[a]++;
  }
  int q, b, c;
  cin >> q;
  vector<int> ans(q, 0);
  rep(i, q) {
    cin >> b >> c;
    if (A[b] != 0) {
      A[c] += A[b];
      A[b] = 0;
    }
    rep(j, 100001) { ans[i] += j * A[j]; }
  }
  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;
  cin >> n;
  int a[n];
  vector<long long> b(100001, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  int x, y;
  for (int i = 0; i < q; i++) {

    cin >> x >> y;
    sum = sum - (b[x] * x) + (b[x] * y);
    b[y] += b[x];
    b[x] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int q;
  cin >> q;
  int x, y;
  for (int i = 0; i < q; i++) {
    long long sum = 0;
    cin >> x >> y;
    for (int j = 0; j < n; j++) {
      if (a[j] == x)
        a[j] = y;
      sum += a[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
  int n, q;
  long long ans = 0;
  std::cin >> n;
  std::array<int, 100001> dat{};
  for (int i = 0; i < n; i++) {
    int input;
    std::cin >> input;
    ans += static_cast<long long>(input);
    dat[input]++;
  }
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    long long from, to;
    std::cin >> from >> to;

    ans += (static_cast<long long>(to - from) *
            static_cast<long long>(dat.at(from)));
    dat.at(to) += dat.at(from);
    dat.at(from) = 0;
    std::cout << ans << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
  int n, q;
  std::cin >> n;
  std::vector<std::pair<int, int>> dat;
  for (int i = 0; i < n; i++) {
    int input;
    std::cin >> input;
    auto it = std::find_if(dat.begin(), dat.end(),
                           [input](auto &&e) { return input == e.first; });
    if (it != dat.end()) {
      it->second++;
    } else {
      dat.emplace_back(input, 1);
    }
  }

  std::cin >> q;
  long long ans = 0;
  for (int i = 0; i < q; i++) {
    long long from, to;
    std::cin >> from >> to;
    int from_i = -1, to_i = -1;

    for (int i = 0; i < dat.size(); i++) {
      auto &p = dat.at(i);
      long long add;
      if (p.first == to) {
        to_i = i;
      }
      if (p.first == from) {
        from_i = i;
        p.first = to;
      }
      add = static_cast<long long>(p.first) * static_cast<long long>(p.second);
      ans += add;
    }
    if (to_i != -1 && from_i != -1) {
      dat.at(to_i).second += dat.at(from_i).second;
      dat.erase(dat.begin() + from_i);
    }

    std::cout << ans << "\\n";
    ans = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: 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[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;
}


Error: 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[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;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
const int MOOD = 998244353;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

ll coun[100001];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    coun[x]++;
  }
  int q;
  cin >> q;
  ll ans = 0;
  for (int j = 1; j <= 100000; j++) {
    ans += coun[j] * j;
  }
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ans -= b * coun[b];
    ans += c * coun[b];
    coun[c] += coun[b];
    coun[b] = 0;
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
const int MOOD = 998244353;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int coun[100001];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    coun[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    coun[c] += coun[b];
    coun[b] = 0;
    ll ans = 0;
    for (int j = 1; j <= 100000; j++) {
      ans += coun[j] * j;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, ll> m;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (!m.count(A.at(i))) {
      m[A.at(i)] = 1;
    } else {
      m[A.at(i)]++;
    }
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }
  ll num = 0;
  for (pair<int, ll> p : m) {
    num += p.first * p.second;
  }
  for (int i = 0; i < Q; i++) {
    if (m.count(B.at(i))) {
      num += (C.at(i) - B.at(i)) * m[B.at(i)];
      if (!m.count(C.at(i))) {
        m[C.at(i)] = 0;
      }
      m[C.at(i)] += m[B.at(i)];
      m.erase(B.at(i));
    }
    cout << num << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, ll> m;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (!m.count(A.at(i))) {
      m[A.at(i)] = 1;
    } else {
      m[A.at(i)]++;
    }
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }
  long long num;
  for (int i = 0; i < Q; i++) {
    if (m.count(B.at(i))) {
      if (!m.count(C.at(i))) {
        m[C.at(i)] = 0;
      }
      m[C.at(i)] += m[B.at(i)];
      m.erase(B.at(i));
    }
    num = 0;
    for (pair<int, ll> p : m) {
      num += p.first * p.second;
    }
    cout << num << endl;
  }
}
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)
typedef long long int ll;
typedef pair<int, int> P;
const int INF = 1e9 + 7;

int main() {
  ll n, sum = 0;
  ;
  cin >> n;
  map<ll, ll> a;
  rep(i, n) {
    ll t;
    cin >> t;
    sum += t;
    if (a.count(t)) {
      ++a[t];
    } else {
      a[t] = 1;
    }
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    if (a.count(b)) {
      if (a.count(c)) {
        sum += c * a[b];
        sum -= b * a[b];
        a[c] += a[b];
        a.erase(b);
      } else {
        sum += c * a[b];
        sum -= b * a[b];
        a[c] = a[b];
        a.erase(b);
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  map<ll, ll> a;
  rep(i, n) {
    ll t;
    cin >> t;
    if (a.count(t)) {
      ++a[t];
    } else {
      a[t] = 1;
    }
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    if (a.count(b)) {
      if (a.count(c)) {
        a[c] += a[b];
        a.erase(b);
      } else {
        a[c] = a[b];
        a.erase(b);
      }
    }
    ll sum = 0;
    for (auto t : a) {
      auto k = t.first;
      auto v = t.second;
      sum += k * v;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[100005];
long long sum1;
int k;
int A, B;
int main() {
  int x;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[x]++;
    sum1 += x;
  }
  cin >> k;
  for (int i = 1; i <= k; i++) {
    cin >> A >> B;
    a[B] += a[A];
    sum1 += (B - A) * a[A];
    a[A] = 0;
    cout << sum1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long a, b, c, d, e, f, h, i;
  cin >> a;
  int s[a];
  for (i = 0; i < a; i++) {
    cin >> s[i];
  }
  cin >> d;
  while (d--) {
    cin >> e >> f;
    for (i = 0; i < a; i++) {
      if (e == s[i])
        s[i] = f;
    }
    int sum = 0;
    for (i = 0; i < a; i++) {
      sum = sum + s[i];
    }

    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, i, r, s = 0, b, c, j;
  scanf("%lld", &n);
  long long int a[n + 10];
  long long int ar[100001] = {0};
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
    ar[a[i]]++;
  }
  cin >> m;
  long long int f[m + 10];
  for (i = 0; i < m; i++) {
    cin >> b >> c;
    s += (c - b) * ar[b];
    ar[c] += ar[b];
    ar[b] = 0;
    cout << s << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, i, r, s = 0, b, c, j;
  scanf("%lld", &n);
  long long int a[n + 10];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  cin >> m;
  long long int f[m + 10];
  for (i = 0; i < m; i++) {
    cin >> b >> c;
    for (j = 0; j < n; j++) {
      if (a[j] == b) {
        s = ((s - b) + c);
        a[j] = c;
      }
    }
    f[i] = s;
  }
  for (i = 0; i < m; i++)
    cout << f[i] << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define maxn 100005
#define ll long long
using namespace std;
ll n, q, a[maxn], sum;
int tm[maxn];
int main(void) {
  while (~scanf("%lld", &n)) {
    sum = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%lld", &a[i]);
      sum += a[i]; // 求和
      tm[a[i]]++;  // a[i]出现的次数自增
    }
    scanf("%lld", &q);
    ll b, c;
    for (int i = 0; i < q; ++i) {
      scanf("%lld %lld", &b, &c);
      sum += (c - b) * tm[b];
      tm[c] += tm[b]; // 更新tm[c]
      tm[b] = 0;      // 更新tm[b]
      printf("%lld\\n", sum);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int a[100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    cin >> k;
    while (k--) {
      int p, q;
      cin >> p >> q;
      long long sum = 0;
      for (int i = 0; i < n; ++i) {
        if (a[i] == p)
          a[i] = q;
        sum += a[i];
      }
      cout << sum << endl;
    }
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
using namespace std;

int num[100050];

int main() {
  long long sum = 0;
  int N, Q, B, C, a;
  scanf("%d", &N);
  for (int i = 0; i < 100050; ++i)
    num[i] = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%d", &a);
    sum += (long long)a;
    ++num[a];
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d%d", &B, &C);
    sum += (long long)(C - B) * num[B];
    num[C] += num[B];
    num[B] = 0;
    printf("%lld\\n", sum);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;
const int N = 10010;
int a[N];
int main() {
  int n;
  cin >> n;
  int x;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    sum += x;
    a[x]++;
  }

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

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int SIZEN = 500010;
int N;
map<long long, int> mp;
int main() {
  scanf("%d", &N);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    long long x;
    scanf("%lld", &x);
    mp[x]++;
    ans += x;
  }
  int M;
  scanf("%d", &M);
  for (int i = 1; i <= M; i++) {
    long long x, y;
    scanf("%lld%lld", &x, &y);
    ans -= x * mp[x];
    ans += y * mp[x];
    mp[y] += mp[x];
    mp[x] = 0;
    printf("%lld\\n", ans);
  }

  return 0;
}
/*
4
1 1 1 1
3
1 2
2 1
3 5
*/

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a[10010];
int cont[10010] = {};
int main() {
  int n, q, x, y;
  scanf("%d", &n);
  long total = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cont[a[i]]++;
    total += a[i];
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &x, &y);
    total += (cont[x]) * (y - x);
    cont[y] += cont[x];
    cont[x] = 0;
    cout << total << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: 0

Fixed submission:

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

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

#include <iostream>

#define endl '\\n'
#define pi acos(-1)
#define euler_e 2.71828
#define pii pair<int, int>
#define pb push_back
#define all(a) a.begin(), a.end()
#define ll long long
#define ull unsigned long long
#define io                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
void mycode();
const ll int mod = 998244353;
ll int ceil(ll int a, ll int b) { return (a + b - 1) / b; }
ll int min(ll int a, ll int b) {
  if (a > b)
    return b;
  else
    return a;
}
bool bit_check(ll int a, int i) {
  if ((a & (1LL << i)))
    return 1;
  return 0;
}
ll int bit_toggle(ll int a, int i) { return (a ^ (1LL << i)); }
ll int bit_sum(ll int a, int i) { return a + (1LL << i); }
ll int bit_sub(ll int a, int i) { return a - (1LL << i); }
ll int mod_power(ll int x, ll int y) { // x^y%p
  ll int p = mod;
  ll int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll int power_of(ll int a, int b) {
  if (a == 0)
    return -1;
  return 1 + power_of(a / b, b);
}
ll int power(ll int a, ll int b) {
  if (a == 1)
    return 1;
  ll int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
//__int128
int main() {
  boost;
  // io;
  // auto start = chrono::high_resolution_clock::now();
  mycode();
  // auto end = chrono::high_resolution_clock::now();cerr << "\\nExecution time:
  // "<< chrono::duration_cast<chrono::milliseconds>(end - start).count() << "
  // ms" << endl;
  return 0;
}
#include <map>
void mycode() {
  int t, k;
  cin >> t;
  ll int sum = 0;
  map<int, int> mp;
  for (int i = 0; i < t; i++) {
    cin >> k;
    mp[k]++;
    sum += k;
  }
  int q, a, b;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    sum += -mp[a] * a + mp[a] * b;
    cout << sum << endl;
    mp[b] += mp[a];
    mp.erase(a);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
map<int, int> mp;
const int maxn = 10e5 + 3;
int a[maxn];
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int sum = 0;
    int B, C;
    scanf("%d%d", &B, &C);
    for (int j = 0; j < N; j++) {
      if (a[j] == B) {
        a[j] = C;
        sum += a[j];
      } else
        sum += a[j];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll res = 0;
  map<ll, ll> memo;
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
    memo[a[i]]++;
    res += a[i];
  }

  ll q;
  cin >> q;
  vector<ll> b(q), c(q);
  for (ll i = 0; i < q; ++i) {
    cin >> b[i] >> c[i];
    res -= b[i] * memo[b[i]];
    res -= c[i] * memo[c[i]];

    memo[c[i]] += memo[b[i]];
    memo[b[i]] = 0;
    res += c[i] * memo[c[i]];

    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
stack<char> sta;

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