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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
using namespace std;

#define MAX 100005

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
using namespace std;

#define MAX 10005

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  unordered_map<int, int> mp;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int no;
    cin >> no;
    sum += no;
    mp[no]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum = sum + (c - b) * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
    cout << sum << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  cin >> n;
  vector<int> v(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    while (find(v.begin(), v.end(), b) != v.end()) {
      auto it = find(v.begin(), v.end(), b);
      v[it - v.begin()] = c;
      long long diff = c - b;
      sum += diff;
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<int> a_num(100000, 0);
  ll sum = 0;
  rep(i, n) {
    a_num[a[i] - 1]++;
    sum += a[i];
  }
  // sort(a.begin(), a.end());
  // ll sum = accumulate(a.begin(), a.end(), 0);

  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += a_num[b - 1] * (c - b);
    cout << sum << endl;

    a_num[c - 1] += a_num[b - 1];
    a_num[b - 1] = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<int> a_num(10000, 0);
  rep(i, n) { a_num[a[i] - 1]++; }
  // sort(a.begin(), a.end());
  ll sum = accumulate(a.begin(), a.end(), 0);
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += a_num[b - 1] * (c - b);
    cout << sum << endl;

    a_num[c - 1] += a_num[b - 1];
    a_num[b - 1] = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

int main() {
  ll N, Q, a[100010] = {0}, b[100010] = {0}, c[100010] = {0}, s[100010] = {0},
           cnt[100010] = {0};
  cin >> N;
  rep(i, N) {
    cin >> a[i];
    cnt[a[i] - 1]++;
  }
  cin >> Q;
  rep(i, Q) { cin >> b[i] >> c[i]; }
  rep(i, N) { s[0] += a[i]; }
  rep(i, Q) {
    s[i + 1] = s[i] + (c[i] - b[i]) * cnt[b[i] - 1];
    cnt[c[i] - 1] += cnt[b[i] - 1];
    cnt[b[i] - 1] = 0;
  }
  for (int i = 1; i <= Q; i++) {
    p(s[i]);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

int main() {
  ll N, Q, a[10001], b[10001], c[10001], s[10001];
  cin >> N;
  rep(i, N) cin >> a[i];
  cin >> Q;
  rep(i, Q) { cin >> b[i] >> c[i]; }
  rep(j, Q) {
    rep(i, N) {
      if (a[i] == b[j])
        a[i] = c[j];
      s[j] += a[i];
    }
  }
  rep(i, Q) p(s[i]);
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n;
  cin >> n;
  vector<int64_t> a(100001, 0);
  int64_t sum = 0;
  for (int i = 0; i < n;
       i++) { // 1~10000の各数を数列がそれぞれいくつ含むかを調べるための配列
    int x;
    cin >> x;
    a.at(x)++;
    sum += x; // 数列の和
  }

  int q;
  cin >> q;
  vector<int64_t> answer(q, 0);
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a.at(b); // 数列の和を更新
    answer.at(i) = sum;
    a.at(c) += a.at(b);
    a.at(b) = 0;
  }

  for (int64_t ans : answer) {
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {

  int n;
  cin >> n;
  vector<int64_t> a(10001, 0);
  int64_t sum = 0;
  for (int i = 0; i < n;
       i++) { // 1~10000の各数を数列がそれぞれいくつ含むかを調べるための配列
    int x;
    cin >> x;
    a.at(x)++;
    sum += x; // 数列の和
  }

  int q;
  cin >> q;
  vector<int64_t> answer(q, 0);
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a.at(b); // 数列の和を更新
    answer.at(i) = sum;
    a.at(c) += a.at(b);
    a.at(b) = 0;
  }

  for (int64_t ans : answer) {
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define vite                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define tst                                                                    \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define ll long long int
#define vt vector
#define pb push_back
ll a[100001] = {0};
int main() {
  // clock_t start, end;
  // start = clock();
  vite;
  ll n, t = 0;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    t += x;
    a[x]++;
  }
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    a[y] += a[x];
    t += y * a[x] - x * a[x];
    a[x] = 0;
    cout << t << "\\n";
  }
  // end = clock();
  // double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  // cout<<fixed << time_taken << setprecision(5);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define vite                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define tst                                                                    \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define ll long long int
#define vt vector
#define pb push_back
int main() {
  // clock_t start, end;
  // start = clock();
  vite;
  ll n, q;
  cin >> n;
  ll a[n], t = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    t += a[i];
  }
  sort(a, a + n);
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    for (ll j = 0; j < n; j++) {
      if (a[j] == x) {
        t = t + y - x;
        a[j] = y;
      }
      if (a[j - 1] == x && a[j] != x)
        break;
    }
    sort(a, a + n);
    cout << t << "\\n";
  }
  // end = clock();
  // double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  // cout<<fixed << time_taken << setprecision(5);
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define init(a, i)                                                             \\
  for (int k = 0; k < (i); k++)                                                \\
  (a)[k] = 0
#define in(a, i)                                                               \\
  for (int k = 0; k < (i); k++)                                                \\
  cin >> (a)[k]
int main() {
  int n;
  cin >> n;
  int a[n];
  in(a, n);
  int q;
  cin >> q;
  int b[q], c[q];
  rep(i, q) cin >> b[i] >> c[i];
  ll num[100001];
  init(num, 100001);
  rep(i, n) num[a[i]]++;
  ll sum = 0;
  rep(i, 100001) sum += i * num[i];
  rep(i, q) {
    num[c[i]] += num[b[i]];
    sum += (c[i] - b[i]) * num[b[i]];
    num[b[i]] = 0;
    cout << sum << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define init(a, i)                                                             \\
  for (int k = 0; k < (i); k++)                                                \\
  (a)[k] = 0
#define in(a, i)                                                               \\
  for (int k = 0; k < (i); k++)                                                \\
  cin >> (a)[k]
int main() {
  int n;
  cin >> n;
  int a[n];
  in(a, n);
  int q;
  cin >> q;
  int b[q], c[q];
  rep(i, q) cin >> b[i] >> c[i];
  ll num[100001];
  init(num, 100001);
  rep(i, n) num[a[i]]++;
  ll sum = 0;
  rep(i, q) {
    sum = 0;
    num[c[i]] += num[b[i]];
    num[b[i]] = 0;
    rep(i, 100001) sum += i * num[i];
    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;
  vector<int> a(100001, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x]++;
    sum += x;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

#include "bits/stdc++.h"

#define rep(i, N) for (int i = 0; i < N; i++)

typedef long long ll;

const int mod = 1e9 + 7;

using namespace std;

int main(void) {
  vector<ll> m(1000000, 0);
  ll s = 0;

  long long N, Q;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) {
    cin >> A[i];
    m[A[i]]++;
    s += A[i];
  }

  cin >> Q;

  vector<long long> B(Q), C(Q);
  rep(i, Q) {
    cin >> B[i];
    cin >> C[i];
  }

  rep(i, Q) {
    long long temp = m[B[i]];
    m[C[i]] += temp;
    m[B[i]] = 0;

    s -= B[i] * temp;
    s += C[i] * temp;
    cout << s << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#define rep(i, N) for (int i = 0; i < N; i++)

typedef long long ll;

const int mod = 1e9 + 7;

using namespace std;

int main(void) {

  map<long long, long long> m;

  long long N, Q;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) {
    cin >> A[i];
    m[A[i]]++;
  }

  cin >> Q;

  vector<long long> B(Q), C(Q);
  rep(i, Q) {
    cin >> B[i];
    cin >> C[i];
  }

  rep(i, Q) {
    long long temp = m[B[i]];
    m[C[i]] += temp;
    m[B[i]] = 0;

    long long sum = 0;
    for (auto x : m)
      sum += x.first * x.second;
    cout << sum << 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 < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define All(vec) vec.begin(), vec.end()
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())
#define PI acos(-1)

int main() {
  int N;
  cin >> N;
  vector<int> count(100001);
  Rep(i, N) {
    int A;
    cin >> A;
    count[A]++;
  }
  int64_t ans = 0;
  Rep(i, count.size()) { ans += (int64_t)count[i] * i; }
  int Q;
  cin >> Q;
  Rep(i, Q) {
    int B, C;
    cin >> B >> C;
    ans -= (int64_t)count[B] * B;
    ans += (int64_t)count[B] * C;
    count[C] += count[B];
    count[B] = 0;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define All(vec) vec.begin(), vec.end()
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())
#define PI acos(-1)

int main() {
  int N;
  cin >> N;
  vector<int> count(100001);
  Rep(i, N) {
    int A;
    cin >> A;
    count[A]++;
  }
  int Q;
  cin >> Q;
  Rep(i, Q) {
    int B, C;
    cin >> B >> C;
    count[C] += count[B];
    count[B] = 0;
    int64_t ans = 0;
    Rep(i, count.size()) { ans += count[i] * i; }
    cout << ans << 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 rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1000);
  rep(i, n) cin >> a[i];
  ll q;
  cin >> q;
  ll sum = 0;
  rep(i, n) sum += a[i];
  vector<ll> cnt(100007, 0);
  rep(i, n) { cnt[a[i]]++; }
  rep(i, q) {
    // rep(i,n) cout << cnt[i] << " ";
    // cout << endl;
    ll b, c;
    cin >> b >> c;
    ll d = c - b;
    // bの数を数える
    sum += cnt[b] * d;
    cout << sum << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
}

/*
Ctrl+@ でコンソールを開け、以下で実行。
g++ d.cpp
./a.out

サンプルのダウンロードは oj d https://atcoder.jp/contests/abc171/tasks/abc171_d
テストは oj t
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1000);
  rep(i, n) cin >> a[i];
  ll q;
  cin >> q;
  ll sum = 0;
  rep(i, n) sum += a[i];
  vector<ll> cnt(n + 1000, 0);
  rep(i, n) { cnt[a[i]]++; }
  rep(i, q) {
    // rep(i,n) cout << cnt[i] << " ";
    // cout << endl;
    ll b, c;
    cin >> b >> c;
    ll d = c - b;
    // bの数を数える
    sum += cnt[b] * d;
    cout << sum << endl;
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
}

/*
Ctrl+@ でコンソールを開け、以下で実行。
g++ d.cpp
./a.out

サンプルのダウンロードは oj d https://atcoder.jp/contests/abc171/tasks/abc171_d
テストは oj t
*/
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 < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long int ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  ll t = 0;
  cin >> n;
  vector<int> a(100000, 0);
  rep(i, n) {
    int x;
    cin >> x;
    a[x]++;
    t += x;
  }
  cin >> q;
  while (q--) {
    int b, e;
    cin >> b >> e;
    t += a[b] * (e - b);
    a[e] += a[b];
    a[b] = 0;
    cout << t << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  ll t = 0;
  cin >> n;
  multiset<int> a;
  rep(i, n) {
    int x;
    cin >> x;
    a.insert(x);
    t += x;
  }
  cin >> q;
  while (q--) {
    int b, e, d;
    cin >> b >> e;
    d = a.erase(b);
    rep(i, d) { a.insert(e); }
    t += d * (e - b);
    cout << t << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define SortAsc(c) std::sort(c.begin(), c.end())
#define SortDesc(c) std::sort(c.rbegin(), c.rend())
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);

  unordered_map<ll, int> m;

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

  ll q;
  cin >> q;

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

  ll res = 0;
  for (int j = 0; j < n; j++) {
    res += a[j];
  }

  for (int i = 0; i < q; i++) {

    if (m.count(b[i])) {
      ll d = c[i] - b[i];
      int count = m[b[i]];

      m[c[i]] += count;

      m[b[i]] = 0;

      res += d * count;
    }

    cout << res << endl;
  }
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define SortAsc(c) std::sort(c.begin(), c.end())
#define SortDesc(c) std::sort(c.rbegin(), c.rend())
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);

  unordered_map<ll, vector<int>> m;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]].push_back(i);
  }

  ll q;
  cin >> q;

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

  ll res = 0;
  for (int j = 0; j < n; j++) {
    res += a[j];
  }

  for (int i = 0; i < q; i++) {

    if (m.count(b[i])) {
      ll d = c[i] - b[i];
      int count = 0;
      for (int j : m[b[i]]) {
        a[j] = c[i];
        m[c[i]].push_back(j);
        count++;
      }

      m.erase(b[i]);

      res += d * count;
    }

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
int main() {
  int n;
  cin >> n;
  int num[n];
  long long int sum = 0;
  int h[100001];
  for (int i = 0; i < 100001; i++) {
    h[i] = 0;
  }
  for (int i = 0; i < n; i++) {

    cin >> num[i];
    sum = sum + num[i];
    h[num[i]]++;
  }
  long long int q;
  cin >> q;
  // vector<int>arr[q];
  /*for(int i=0;i<q;i++)
  {
          int b;
          int c;
          cin>>b>>c;
          arr[i][0]=b;
          arr[i][1]=c;
  }*/

  while (q--) {
    long long int b;
    long long int c;
    cin >> b >> c;
    if (h[b]) {
      sum = sum + (c - b) * h[b];
      h[c] = h[c] + h[b];
      h[b] = 0;
    }

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  int num[n];
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  int q;
  cin >> q;
  vector<int> arr[q];
  /*for(int i=0;i<q;i++)
  {
          int b;
          int c;
          cin>>b>>c;
          arr[i][0]=b;
          arr[i][1]=c;
  }*/
  while (q--) {
    int b;
    int c;
    cin >> b >> c;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (num[i] == b) {
        sum = sum + c;
        num[i] = c;
      } else
        sum = sum + num[i];
    }
    cout << sum << endl;
  }
}