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

/*
Aldew
PROB: Save Konoha
CLASSIFICATION: Priority Queue
DIFFICULTY:
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <string>
#include <vector>
#define check(x) cout << (#x) << ": " << x << " ";
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

// void io()
//{
//     #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
//     #endif
// }

vector<int> factors(int x) {
  vector<int> result;
  int i = 1;
  while (i * i <= x) {
    if (x % i == 0) {
      result.push_back(i);
      if (x / i != i) {
        result.push_back(x / i);
      }
    }
    i++;
  }
  return result;
}

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

  ll it;
  ll s = 0;

  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> it;
    s += it;
    m[it]++;
  }

  ll q;
  cin >> q;

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

    // cout << "here" << m[b] << endl;
    s -= (b * m[b]);
    s += (c * m[b]);

    m[c] += m[b];
    m[b] = 0;

    cout << s << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

/*
Aldew
PROB: Save Konoha
CLASSIFICATION: Priority Queue
DIFFICULTY:
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <string>
#include <vector>
#define check(x) cout << (#x) << ": " << x << " ";
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

// void io()
//{
//     #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
//     #endif
// }

vector<int> factors(int x) {
  vector<int> result;
  int i = 1;
  while (i * i <= x) {
    if (x % i == 0) {
      result.push_back(i);
      if (x / i != i) {
        result.push_back(x / i);
      }
    }
    i++;
  }
  return result;
}

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

  int a[n];
  int it;
  ll s = 0;
  for (int i = 0; i < n; i++) {
    cin >> it;
    s += it;
    a[i] = it;
  }

  ll q;
  cin >> q;

  sort(a, a + n);
  int b, c;
  while (q--) {
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        s -= b;
        s += c;
        a[i] = c;
      } else if (a[i] > b)
        break;
    }
    sort(a, a + n);
    cout << s << endl;
  }

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

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

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

Error: 0

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int arr[100001] = {0};
  long long int sum = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    v.push_back(x);
    arr[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    sum = sum - (long long int)a * arr[a];
    sum = sum + (long long int)b * arr[a];
    arr[b] = arr[b] + arr[a];
    arr[a] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    replace(v.begin(), v.end(), a, b);
    cout << accumulate(v.begin(), v.end(), 0) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

void aryan_p() {
  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
  return;
}

int32_t main() {

  int n;
  cin >> n;
  map<int, int> m;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    m[a]++;
    sum += a;
  }

  w(q) {
    int b, c;
    cin >> b >> c;

    int x = m[b];
    sum -= (b * m[b]);
    m[c] += x;
    sum += (c * x);
    m[b] = 0;
    cout << sum << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

void aryan_p() {
  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
  return;
}

int32_t main() {

  int n;
  cin >> n;
  multiset<int> a;

  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    a.insert(k);
  }

  w(q) {
    int b, c;
    cin >> b >> c;

    int val = a.count(b);
    a.erase(b);
    while (val--) {
      a.insert(c);
    }

    int sum = 0;
    sum = accumulate(a.begin(), a.end(), sum);
    cout << sum << "\\n";
  }

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

#include <bits/stdc++.h>

#define ll long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

int main() {
  fastio ll n;
  cin >> n;
  unordered_map<ll, ll> m;
  ll sum = 0;
  for (ll i = 0, tmp; i < n; i++)
    cin >> tmp, m[tmp]++, sum += tmp;
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    m[b] += m[a];
    sum -= m[a] * a;
    sum += b * m[a];
    m[a] = 0;
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

int main() {
  fastio ll n;
  cin >> n;
  unordered_map<ll, ll> m;
  for (ll i = 0, tmp; i < n; i++)
    cin >> tmp, m[tmp]++;
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    m[b] += m[a];
    m[a] = 0;
    ll ans = 0;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      ans += itr->first * itr->second;
    }
    cout << ans << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
  // freopen("input.txt", "r", stdin);
  long n, q, b, c, temp, sum = 0;
  vector<long> a(100001);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp]++;
    sum += temp;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    sum -= a[c] * c;
    sum -= a[b] * b;
    a[c] += a[b];
    a[b] = 0;
    sum += a[c] * c;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
long sum(vector<long> a) {
  long sum = 0;
  for (int i = 0; i < a.size(); i++) {
    sum += a[i] * i;
  }
  return sum;
}
int main() {
  // freopen("input.txt", "r", stdin);
  long n, q, b, c, temp;
  vector<long> a(100001);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    cout << sum(a) << 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() {
    ll n;
    cin>>n;
    vector<ll> vec(n);
    rep(i,n){
        cin>>vec.at(i);
    }
    ll q;
    cin>>q;
    ll b,c;
    rep(i,q){
        cin>>b>>c;
        ll sum=0;
        rep(k,n){
            if(vec.at(k)==b){
                sum+=c;
                vec.at(k)=c;
            }else{
                sum+=vec.at(k);
            }
        }
        cout<<sum<<endl;


    }

}*/
// 8乗9乗が限界らしい
// アイデアとして、
int main() {
  ll n;
  cin >> n;
  map<ll, ll> map;
  ll msum = 0;
  rep(i, n) {
    int a;
    cin >> a;
    map[a] += 1;
    msum += a;
  }
  ll q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    msum = msum - b * map[b] + c * map[b];
    cout << msum << endl;
    map[c] = map[c] + map[b];
    map[b] = 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() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  rep(i, n) { cin >> vec.at(i); }
  ll q;
  cin >> q;
  ll b, c;
  rep(i, q) {
    cin >> b >> c;
    ll sum = 0;
    rep(k, n) {
      if (vec.at(k) == b) {
        sum += c;
        vec.at(k) = c;
      } else {
        sum += vec.at(k);
      }
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t N, A;
  cin >> N;
  map<int64_t, int64_t> data;
  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A;
    sum += A;
    data[A]++;
  }

  int64_t Q, B, C;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    sum += (C - B) * data[B];
    data[C] += data[B];
    data[B] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N, Q;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  cin >> Q;
  int B, C;
  for (int i = 0; i < Q; i++) {
    long long sum = 0;
    cin >> B >> C;
    for (int j = 0; j < N; j++) {

      if (A[j] == B) {
        A[j] = C;
      }

      sum += A[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////
int cnt[100010];
int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  cin >> q;
  vector<long long> ans(q + 1);
  ans[0] = sum;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////
int cnt[100010];
int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  cin >> q;
  vector<int> b(n), c(n);
  vector<long long> ans(q + 1);
  ans[0] = sum;
  for (int i = 1; i <= q; i++) {
    cin >> b[i] >> c[i];
    if (b[i] == c[i] || cnt[b[i]] == 0) {
      ans[i] = ans[i - 1];
      continue;
    }
    sum = sum - (b[i] * cnt[b[i]]) + (c[i] * cnt[b[i]]);
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;
    ans[i] = sum;
  }

  for (int i = 1; i <= q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
#define rep(n, i) for (ll i = 0; i < n; ++i)

void solve(void) {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  rep(n, i) cin >> a[i];
  ll q;
  cin >> q;
  vector<ll> b(q, 0);
  vector<ll> c(q, 0);
  rep(q, i) cin >> b[i] >> c[i];

  map<ll, ll> m;
  rep(n, i)++ m[a[i]];

  ll res = 0;
  rep(n, i) res += a[i];

  rep(q, i) {
    res += m[b[i]] * (c[i] - b[i]);
    cout << res << endl;

    ll t = m[b[i]];
    m[b[i]] -= t;
    m[c[i]] += t;
  }
}

int main(void) {
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
#define rep(n, i) for (ll i = 0; i < n; ++i)

void solve(void) {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  rep(n, i) cin >> a[i];
  ll q;
  cin >> q;
  vector<ll> b(n, 0);
  vector<ll> c(n, 0);
  rep(q, i) cin >> b[i] >> c[i];

  map<ll, ll> m;
  rep(n, i)++ m[a[i]];

  ll res = 0;
  rep(n, i) res += a[i];

  rep(q, i) {
    res += m[b[i]] * (c[i] - b[i]);
    cout << res << endl;

    ll t = m[b[i]];
    m[b[i]] = 0;
    m[c[i]] += t;
  }
}

int main(void) {
  solve();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = unsigned long long;
using P = pair<int, int>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  rep1(i, n) { x[i] = 0; }

int main() {
  int n, q;
  cin >> n;
  ll zarray(a, 100000);

  rep1(i, n) {
    int x;
    cin >> x;
    a[x]++;
  }

  ll sum = 0;
  rep1(j, 100000) { sum += j * a[j]; }
  // cerr << sum << endl;

  cin >> q;
  rep0(i, q) {
    ll b;
    ll c;
    cin >> b >> c;
    ll sub = b * a[b] + c * a[c];
    a[c] += a[b];
    a[b] = 0;
    sum += c * a[c] - sub;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

using ll = long long;
using P = pair<int, int>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  rep1(i, n) { x[i] = 0; }

int main() {
  int n, q;
  cin >> n;
  int a[n];
  rep0(i, n) { cin >> a[i]; }

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

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

int main() {
  int N;
  cin >> N;
  ll ans = 0;
  map<ll, ll> A;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[a]++;
    ans += a;
  }

  int Q;
  cin >> Q;
  while (Q--) {
    ll b, c;
    cin >> b >> c;
    ll Ab = A[b];
    ll Ac = A[c];

    A[c] += Ab;
    A[b] = 0;

    ans -= Ab * b;
    ans += Ab * c;

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

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

  int Q, B, C, ans;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ans = 0;
    cin >> B >> C;
    for (int j = 0; j < N; j++) {
      if (A.at(j) == B) {
        A.at(j) = C;
      }
      ans += A.at(j);
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> num(100010, 0);
  ll t = 0;
  rep(i, n) {
    ll a;
    cin >> a;
    num[a]++;
    t += a;
  }
  int q, b, c;
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    t += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    cout << t << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q, b, c;
  cin >> q;
  rep(i, q) {
    cin >> b >> c;
    replace(a.begin(), a.end(), b, c);
    printf("%d\\n", accumulate(a.begin(), a.end(), 0));
  }

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, a, Q, B, C;
  long long ans = 0;
  cin >> N;
  vector<int> A_count(100000, 0);
  for (int i = 0; i < N; i++) {
    cin >> a;
    A_count[a - 1]++;
    ans += a;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    A_count[C - 1] += A_count[B - 1];
    ans += (C - B) * A_count[B - 1];

    A_count[B - 1] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, Q, B, C;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = a;
  }
  cin >> Q;
  int count_B;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    count_B = count(A.begin(), A.end(), B);
    if (count_B != 0) {
      replace(A.begin(), A.end(), B, C);
    }
    cout << accumulate(A.begin(), A.end(), 0) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int64_t sum = 0;
  unordered_map<int, int> a;
  for (int i = 0; i < n; ++i) {
    int A;
    cin >> A;
    ++a[A];
    sum += A;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum -= 1L * c * a[c];
    a[c] += a[b];
    sum += 1L * c * a[c];
    sum -= 1L * b * a[b];
    a.erase(b);
    cout << sum << "\\n"s;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  cin >> n;
  unordered_map<int, int> a;
  for (int i = 0; i < n; ++i) {
    int A;
    cin >> A;
    ++a[A];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    a.erase(b);
    int64_t ans = 0;
    for (auto &&j : a) {
      ans += 1L * j.first * j.second;
    }
    cout << ans << "\\n"s;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vll = vector<ll>;
int main() {
  ll n, q, a, b, c, i, k, s = 0;
  cin >> n;
  map<ll, ll> m;
  for (i = 0; i < n; i++) {
    cin >> a;
    if (m.count(a))
      m.at(a)++;
    else
      m[a] = 1;
    s += a;
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    if (m.count(b)) {
      k = m.at(b);
      m.erase(b);
      s += (c - b) * k;
      if (m.count(c))
        m.at(c) += k;
      else
        m[c] = k;
    }
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vll = vector<ll>;
int main() {
  ll n, q, a, b, c, i, j, k, s = 0;
  cin >> n;
  multiset<ll> m;
  for (i = 0; i < n; i++) {
    cin >> a;
    m.insert(a);
    s += a;
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    auto it = m.lower_bound(b);
    auto it2 = m.upper_bound(b);
    if (it != m.end()) {
      k = distance(it, it2);
      m.erase(b);
      s += (c - b) * k;
      for (j = 0; j < k; j++)
        m.insert(c);
    }
    cout << s << endl;
  }
}