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

#include <iostream>
#include <map>
#include <vector>

int main(void) {
  int n, q, b, c;
  long long int sum = 0;
  std::cin >> n;
  std::vector<int> A(n);
  std::map<int, int> index_map;

  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
    if (index_map.find(A[i]) == index_map.end())
      index_map[A[i]] = 1;
    else
      index_map[A[i]]++;
    sum += A[i];
  }

  std::cin >> q;

  for (int i = 0; i < q; i++) {
    std::cin >> b >> c;
    sum = sum + ((-b) + c) * index_map[b];
    index_map[c] += index_map[b];
    index_map[b] = 0;
    std::cout << sum << std::endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

int main(void) {
  int n, q, b, c;
  long long int sum = 0;
  std::cin >> n;
  std::vector<int> A(n);
  std::map<int, std::vector<int>> index_map;

  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
    index_map[A[i]].push_back(i);
    sum += A[i];
  }

  std::cin >> q;

  for (int i = 0; i < q; i++) {
    std::cin >> b >> c;
    sum = sum + ((-b) + c) * index_map[b].size();
    for (int j = 0; j < index_map[b].size(); j++) {
      index_map[c].push_back(index_map[b][j]);
    }
    index_map[b].clear();
    std::cout << sum << std::endl;
  }

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

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

ll arraySum(ll arr[], ll n) {
  ll initial_sum = 0;
  return accumulate(arr, arr + n, initial_sum);
}

int main() {
  ll n, q, b, c;
  cin >> n;
  ll arr[n];
  ll sum = 0;
  unordered_map<ll, ll> m;
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    m[arr[i]]++;
    sum += arr[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    if (m[b]) {
      ll x = m[b];
      sum -= x * b;
      sum += c * x;
      m[b] = 0;
      m[c] += x;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

ll arraySum(ll arr[], ll n) {
  ll initial_sum = 0;
  return accumulate(arr, arr + n, initial_sum);
}

int main() {
  ll n, q, b, c;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    for (ll j = 0; j < n; j++) {
      if (arr[j] == b) {
        arr[j] = c;
      }
    }
    cout << arraySum(arr, n) << "\\n";
  }

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

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

  return 0;
}


Error: -11

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define LIMIT 1000000007 // 10^9+7
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) (v).begin(), (v).end()

int main() {
  int N;
  cin >> N;
  ll ans = 0;
  map<ll, ll> A;
  rep(i, N) {
    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;
#define LIMIT 1000000007 // 10^9+7
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) (v).begin(), (v).end()

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

  int Q;
  cin >> Q;
  while (Q--) {
    ans = 0;

    int b, c;
    cin >> b >> c;
    int Ab = A[b];
    int Ac = A[c];

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

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

    for (auto p : A) {
      ans += p.first * p.second;
    }

    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

using ull = unsigned long long;

int main() {
  int n, q, a, b, c;
  ull s = 0;
  cin >> n;
  map<int, int> m;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    auto it = m.find(a);
    if (it == m.end()) {
      m[a] = 1;
    } else {
      m[a]++;
    }
    s += a;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    s += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << s << endl;
  }
  return 0;
}


Error: 0

Fixed submission:


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

using namespace std;

using ull = unsigned long long;

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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
int n, q, a, b, c;
unsigned long long s = 0, sum = 0;

int main() {
  vector<unsigned long long> v(100005);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    v[a]++;
  }
  for (int i = 0; i <= 100004; ++i)
    sum += i * v[i];
  s = sum;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> b >> c;
    v[c] += v[b];
    s += v[b] * c;
    s -= v[b] * b;
    v[b] = 0;
    cout << s << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
int n, q, a, b, c;
unsigned long long s = 0;

int main() {
  int v[1000000];
  cin >> n;
  int m = 1000000, x = -1;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    v[a]++;
    if (a < m)
      m = a;
    if (a > x)
      x = a;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> b >> c;
    m = min(min(b, c), m);
    x = max(max(b, c), x);
    v[c] += v[b];
    v[b] = 0;
    for (int i = m; i <= x; ++i)
      s += v[i] * i;
    cout << s << "\\n";
    s = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

  long long sum = 0;
  map<int, int> M;
  // vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    // A[i] = a;
    if (M.find(a) == M.end()) {
      M[a] = 1;
    } else {
      M[a]++;
    }
    sum += a;
  }

  int Q;
  cin >> Q;

  string result;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;

    long long r = sum;
    if (M.find(B) != M.end()) {
      int n = M[B];
      if (M.find(C) != M.end()) {
        M[C] += M[B];
        M[B] = 0;
      } else {
        M[C] = M[B];
        M[B] = 0;
      }
      r += (C * n) - (B * n);
    }

    // for (auto& it : M) {
    //   r += it.first * it.second;
    // }
    result += to_string(r) + "\\n";
    sum = r;
  }
  cout << result;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

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

  map<int, int> M;
  // vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    // A[i] = a;
    if (M.find(a) == M.end()) {
      M[a] = 1;
    } else {
      M[a]++;
    }
  }

  int Q;
  cin >> Q;

  string result;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;

    if (M.find(B) != M.end()) {
      if (M.find(C) != M.end()) {
        M[C] += M[B];
        M[B] = 0;
      } else {
        M[C] = M[B];
        M[B] = 0;
      }
    }

    long long r = 0;
    for (auto &it : M) {
      r += it.first * it.second;
    }
    result += to_string(r) + "\\n";
  }
  cout << result;

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

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

int main() {
  ll n, sum = 0;
  cin >> n;

  ll arr[1000001] = {0};

  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    arr[x]++;
    sum += x;
  }

  ll q;
  cin >> q;

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n, sum = 0;
  cin >> n;

  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }

  ll q;
  cin >> q;

  for (ll i = 0; i < q; i++) {
    ll b, c, total = 0;
    cin >> b >> c;
    for (ll i = 0; i < n; i++) {
      if (arr[i] == b) {
        total++;
        arr[i] = c;
      }
    }

    sum = sum + total * (c - b);
    cout << sum << "\\n";
  }

  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 ans = 0;
  map<int, int> map;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    map[a]++;
    ans += a;
  }
  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (map[b] != 0) {
      ans += (c - b) * map[b];
      map[c] += map[b];
      map[b] = 0;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N;
  cin >> N;

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

  long long Q;
  cin >> Q;

  long long B[Q];
  long long C[Q];
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }

  long long Ans[Q];
  for (int i = 0; i < Q; i++) {
    long long ans = 0;
    for (int j = 0; j < N; j++) {
      if (B[i] == A[j]) {
        A[j] = C[i];
      }
      ans += A[j];
    }
    Ans[i] = ans;
  }

  for (int i = 0; i < Q; i++) {
    cout << Ans[i] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
using VVI = vector<vector<int>>;
using VI = vector<int>;
const static string el = "\\n";

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

  unsigned long long total = 0;
  VI a(100001);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a[tmp]++;
    total += tmp;
  }

  int q;
  cin >> q;
  int replace_count = 0;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (a[b] != 0) {
      a[c] += a[b];
      replace_count = a[b];
      a[b] = 0;
    }

    // 総和の計算
    if (replace_count > 0) {
      int df = c - b; // 負数もあり得る
      total += df * replace_count;
      cout << total << el;
      replace_count = 0;
    } else {
      cout << total << el;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using VVI = vector<vector<int>>;
using VI = vector<int>;
const static string el = "\\n";

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

  unsigned long long total = 0;
  VI a(n);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a[i] = tmp;
    total += tmp;
  }

  sort(a.begin(), a.end());

  int q;
  cin >> q;
  int replace_count = 0;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    for (int k = 0; k < n; k++) {
      if (a[k] == b) {
        a[k] = c;
        replace_count++;
      } else if (a[k] < b) {
        continue;
      } else {
        break;
      }
    }

    // 総和の計算
    if (replace_count > 0) {

      int df = c - b; // 負数もあり得る
      total += df * replace_count;
      cout << total << el;
      replace_count = 0;

      // insertSort
      for (int pos = 1; pos < n; pos++) {
        for (int cmp = pos - 1; cmp >= 0; cmp--) {
          if (a[cmp] > a[cmp + 1]) {
            int tmp = a[cmp];
            a[cmp] = a[cmp + 1];
            a[cmp + 1] = tmp;
          } else {
            break;
          }
        }
      }
    } else {
      cout << total << el;
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

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

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

vector<ll> p(100000 + 10);

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

  ll N;
  string S;
  cin >> N;

  vector<ll> A(N);
  REP(i, N) { cin >> A.at(i); }
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  REP(i, Q) { cin >> B.at(i) >> C.at(i); }

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

  ll ans = 0;

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

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

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

    ans += tmp1;

    cout << ans << endl;

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


Error: 0

Fixed submission:

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

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

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

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

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

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

vector<ll> p(10000 + 10);

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

  ll N;
  string S;
  cin >> N;

  vector<ll> A(N);
  REP(i, N) { cin >> A.at(i); }
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  REP(i, Q) { cin >> B.at(i) >> C.at(i); }

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

  ll ans = 0;

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

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

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

    ans += tmp1;

    cout << ans << endl;

    p.at(B.at(i)) = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N, Q, A, B, C;
  long long S = 0;
  cin >> N;
  int oc[100001] = {0};
  for (int i = 0; i < N; i++) {
    cin >> A;
    oc[A]++;
    S += A;
  }
  cin >> Q;
  for (int j = 0; j < Q; j++) {
    cin >> B >> C;
    S += (C - B) * oc[B];
    oc[C] += oc[B];
    oc[B] = 0;
    cout << S << "\\n";
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N, Q, A, B, C;
  int S = 0;
  cin >> N;
  int oc[10001] = {0};
  for (int i = 0; i < N; i++) {
    cin >> A;
    oc[A]++;
    S += A;
  }
  cin >> Q;
  for (int j = 0; j < Q; j++) {
    cin >> B >> C;
    S += (C - B) * oc[B];
    oc[C] += oc[B];
    oc[B] = 0;
    cout << S << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
using namespace std;
using ll = long long;

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

  map<ll, ll> mp;
  ll ans = 0LL;

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

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    mp[c] += mp[b];
    ans = ans + (c - b) * mp[b];

    mp.erase(b);

    // cout << "size: " << mp.size() << endl;

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
using namespace std;
using ll = long long;

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

  map<ll, ll> mp;

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

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    mp[c] += mp[b];
    mp.erase(b);

    ll ans = 0LL;
    for (auto x : mp) {
      ans += x.first * x.second;
    }
    cout << ans << endl;
  }

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

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

int main() {
  ll n, q, i = 0, s = 0;
  cin >> n;

  vector<int> a(n), num(100000);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    num[a[i]]++;
    s += a[i];
  }

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, q, i = 0, j = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;

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

  vector<ll> sum(q);
  for (i = 0; i < q; i++) {
    for (j = 0; j < n; j++) {
      if (a[j] == b[i]) {
        a[j] = c[i];
        sum[i] += a[j];
      } else {
        sum[i] += a[j];
      }
    }
  }

  for (int i = 0; i < q; i++) {
    cout << sum[i] << 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)
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N, Q;
  cin >> N;
  vector<ll> A(100005);
  rep(i, N) {
    int j;
    cin >> j;
    A[j] += 1;
  }
  ll ans = 0;
  rep(i, 100005) ans += A[i] * i;
  cin >> Q;
  rep(i, Q) {
    int b, c;
    cin >> b >> c;
    ans += (c - b) * A[b];
    cout << ans << endl;
    A[c] += A[b];
    A[b] = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, Q;
  cin >> N;
  vector<int> A(100005);
  rep(i, N) {
    int j;
    cin >> j;
    A[j] += 1;
  }
  cin >> Q;
  vector<int> B(N);
  vector<int> C(N);
  rep(i, Q) { cin >> B[i] >> C[i]; }
  int ans = 0;
  rep(i, 100005) ans += A[i] * i;
  rep(i, Q) {
    ans += (C[i] - B[i]) * A[B[i]];
    cout << ans << endl;
    A[C[i]] += A[B[i]];
    A[B[i]] = 0;
  }
  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++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<int> cnt(100005, 0);
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  REP(qq, q) {
    ll b, c;
    cin >> b >> c;
    sum += (c - b) * cnt[b];
    cout << sum << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> a(n);
  map<ll, int> mp;
  REP(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  int q;
  cin >> q;
  REP(qq, q) {
    ll b, c;
    cin >> b >> c;
    mp[c] += mp[b];
    mp[b] = 0;

    ll sum = 0;
    for (auto p : mp) {
      sum += p.first * p.second;
    }
    cout << sum << endl;
  }

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

#include <iostream>
#include <numeric>
#include <unordered_map>

int main() {
  long n;
  std::cin >> n;

  std::unordered_map<long, long> a;

  for (long i = 0; i < n; ++i) {
    long aa;
    std::cin >> aa;
    ++a[aa];
  }

  long q;
  std::cin >> q;

  long b, c;
  {
    std::cin >> b >> c;

    const long count = a[b];
    a.erase(b);
    a[c] += count;
  }
  auto previous =
      std::accumulate(std::begin(a), std::end(a), std::make_pair(0l, 0l),
                      [](std::pair<long, long> f, std::pair<long, long> s) {
                        return std::make_pair(1l, (f.second * f.first) +
                                                      (s.second * s.first));
                      })
          .second;
  std::cout << previous << std::endl;

  for (long i = 1; i < q; ++i) {
    std::cin >> b >> c;

    const long count = a[b];
    a.erase(b);
    a[c] += count;

    previous -= (b * count);
    previous += (c * count);
    std::cout << previous << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <numeric>
#include <unordered_set>

int main() {
  long n;
  std::cin >> n;

  std::unordered_multiset<long> a;

  for (long i = 0; i < n; ++i) {
    long aa;
    std::cin >> aa;
    a.emplace(aa);
  }

  long q;
  std::cin >> q;

  long b, c;
  {
    std::cin >> b >> c;

    const long count = a.count(b);
    a.erase(b);
    for (long j = 0; j < count; ++j) {
      a.emplace(c);
    }
  }
  auto previous = std::accumulate(std::begin(a), std::end(a), 0l);
  std::cout << previous << std::endl;

  for (long i = 1; i < q; ++i) {
    std::cin >> b >> c;

    const long count = a.count(b);
    a.erase(b);
    for (long j = 0; j < count; ++j) {
      a.emplace(c);
    }
    previous -= (b * count);
    previous += (c * count);
    std::cout << previous << std::endl;
  }
}