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

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();

#define MAXX 100005

#define PI 3.14159265358979323846264338327950

#define ll long long int
ll mod = 1000000007;
ll a[MAXX], b[MAXX];
char str[MAXX];

int main() {
  FAST;
  ll n, k, ans = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> k;
    ans += k;
    a[k]++;
  }
  ll t;
  cin >> t;
  while (t--) {
    ll b, c;
    cin >> b >> c;
    ans -= a[b] * b;
    ans += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
    ;
  }
}

Error: TLE

Fixed submission:

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();

#define MAXX 100005

#define PI 3.14159265358979323846264338327950

#define ll long long int
ll mod = 1000000007;
ll a[MAXX], b[MAXX];
char str[MAXX];

int main() {
  FAST;
  ll n, k;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> k;
    a[k]++;
  }
  ll t;
  cin >> t;
  while (t--) {
    ll ans = 0;
    ll b, c;
    cin >> b >> c;
    for (ll i = 1; i <= 100000; i++) {
      if (i == b) {
        a[c] += a[i];
        a[i] = 0;
      }
    }
    for (ll i = 1; i <= 100000; i++) {
      ans += a[i] * i;
    }
    cout << ans << endl;
    ;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for_each(begin(A), end(A), [](auto &x) { cin >> x; });
  ll Q;
  cin >> Q;
  vector<ll> B(Q);
  vector<ll> C(Q);
  for (ll i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }
  map<ll, ll> m;
  for (ll i = 0; i < N; i++) {
    m[A[i]]++;
  }
  ll basesum = accumulate(cbegin(A), cend(A), 0LL);

  for (ll i = 0; i < Q; i++) {
    ll count = 0;
    try {
      count = m.at(B[i]);
    } catch (...) {
    }
    ll sum = basesum;
    if (count > 0) {
      m.at(B[i]) -= count;
      try {
        m.at(C[i]) += count;
      } catch (...) {
        m[C[i]] = count;
      }
      sum -= count * B[i];
      sum += count * C[i];
    }
    cout << sum << endl;
    basesum = sum;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for_each(begin(A), end(A), [](auto &x) { cin >> x; });
  ll Q;
  cin >> Q;
  vector<ll> B(Q);
  vector<ll> C(Q);
  for (ll i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }
  for (ll i = 0; i < Q; i++) {
    replace(begin(A), end(A), B[i], C[i]);
    ll sum = accumulate(cbegin(A), cend(A), 0LL);
    cout << sum << endl;
  }

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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>

#define ll long long int
#define pb push_back
#define llu unsigned long long int
#define fo(i, n) for (int i = 0; i < n; i++)
#define eb emplace_back
#define M 1000000007
#define vi vector<int>
#define vlli vector<lli>
#define pi pair<int, int>
#define mp make_pair
#define mapi map<int, int>

using namespace std;
int min(int x, int y) {
  if (x < y)
    return x;
  return y;
}
int max(int x, int y) {
  if (x > y)
    return x;
  return y;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  ll sum = 0;
  cin >> n;
  vector<ll> a(100000, 0);
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    sum += temp;
    a[temp - 1]++;
  }
  int q;
  cin >> q;
  while (q--) {
    ll a1, a2;
    cin >> a1 >> a2;
    sum = sum - a[a1 - 1] * a1 + a[a1 - 1] * a2;
    a[a2 - 1] += a[a1 - 1];
    a[a1 - 1] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>

#define ll long long int
#define pb push_back
#define llu unsigned long long int
#define fo(i, n) for (int i = 0; i < n; i++)
#define eb emplace_back
#define M 1000000007
#define vi vector<int>
#define vlli vector<lli>
#define pi pair<int, int>
#define mp make_pair
#define mapi map<int, int>

using namespace std;
int min(int x, int y) {
  if (x < y)
    return x;
  return y;
}
int max(int x, int y) {
  if (x > y)
    return x;
  return y;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  ll sum = 0;
  cin >> n;
  vector<int> v(n);
  int a[10000] = {0};
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    sum += temp;
    v.push_back(temp);
    a[temp - 1]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int a1, a2, temp;
    cin >> a1 >> a2;
    sum = sum - a[a1 - 1] * a1;
    sum = sum + a[a1 - 1] * a2;
    temp = a[a1 - 1];
    a[a2 - 1] += temp;
    a[a1 - 1] = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, t, x, y, k, i, j, p, q;

  cin >> n;

  ll a[n + 5];

  map<ll, ll> mp;

  ll sum = 0;

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

    sum += a[i];

    mp[a[i]]++;
  }

  cin >> t;

  while (t--) {
    cin >> x >> y;

    sum += (mp[x] * y - mp[x] * x);

    mp[y] = mp[y] + mp[x];

    mp[x] = 0;

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, sum = 0;
  cin >> a;
  int ar[a + 5];
  for (int i = 0; i < a; i++) {
    cin >> ar[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int c, d;
    cin >> c >> d;
    for (int j = 0; j < a; j++) {
      if (ar[j] == c) {
        ar[j] = d;
      }
      sum += ar[j];
    }

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

#include <bits/stdc++.h>
#define FOR(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, N) FOR(i, 0, N)
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vl;
const ll MOD = round(1e9 + 7);

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) cin >> A[i];
  int Q;
  cin >> Q;
  vl B(Q), C(Q);
  rep(i, Q) cin >> B[i] >> C[i];

  map<int, ll> mp;
  ll sum = 0;
  rep(i, N) mp[A[i]]++;
  rep(i, N) sum += A[i];

  queue<ll> ans;
  rep(i, Q) {
    if (mp[B[i]] > 0) {
      sum += (C[i] * mp[B[i]] - B[i] * mp[B[i]]);
      mp[C[i]] += mp[B[i]];
      mp[B[i]] = 0LL;
    }
    ans.push(sum);
  }

  while (!ans.empty()) {
    cout << ans.front() << "\\n";
    ans.pop();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, N) FOR(i, 0, N)
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vl;
const ll MOD = round(1e9 + 7);

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) cin >> A[i];
  int Q;
  cin >> Q;
  vl B(N), C(N);
  rep(i, Q) cin >> B[i] >> C[i];

  map<int, ll> mp;
  ll sum = 0;
  rep(i, N) mp[A[i]]++;
  rep(i, N) sum += A[i];

  queue<ll> ans;
  rep(i, Q) {
    if (mp[B[i]] > 0) {
      sum += (C[i] * mp[B[i]] - B[i] * mp[B[i]]);
      mp[C[i]] += mp[B[i]];
      mp[B[i]] = 0;
    }
    ans.push(sum);
  }

  while (!ans.empty()) {
    cout << ans.front() << "\\n";
    ans.pop();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <unordered_map>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  unordered_map<ll, ll> mp;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    mp[A]++;
    ans += A;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ll n = 0;
    auto ite = mp.find(B);
    if (ite != mp.end()) {
      ans += (C - B) * ite->second;
      mp[C] += ite->second;
      ite->second = 0;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_map>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  unordered_map<ll, ll> mp;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    mp[A]++;
    ans += A;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ll n = 0;
    auto ite = mp.begin();
    for (ite; ite != mp.end(); ite++) {
      if (ite->first == B)
        break;
    }
    if (ite != mp.end()) {
      ans += (C - B) * ite->second;
      mp[C] += ite->second;
      ite->second = 0;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> A(1000000);
  ll sum = 0;
  rep(i, N) {
    int tmp;
    cin >> tmp;
    A.at(tmp)++;
    sum += tmp;
  }

  int Q;
  cin >> Q;
  vector<vector<int>> bc(Q, vector<int>(2));
  rep(i, Q) { cin >> bc.at(i).at(0) >> bc.at(i).at(1); }

  rep(i, Q) {
    sum += (bc.at(i).at(1) - bc.at(i).at(0)) * A.at(bc.at(i).at(0));
    cout << sum << endl;
    A.at(bc.at(i).at(1)) += A.at(bc.at(i).at(0));
    A.at(bc.at(i).at(0)) = 0;
  }
}

Error: 0

Fixed submission:

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

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

  vector<int> A(100000);
  ll sum = 0;
  rep(i, N) {
    int tmp;
    cin >> tmp;
    A.at(tmp)++;
    sum += tmp;
  }

  int Q;
  cin >> Q;
  vector<vector<int>> bc(Q, vector<int>(2));
  rep(i, Q) { cin >> bc.at(i).at(0) >> bc.at(i).at(1); }

  rep(i, Q) {
    sum += (bc.at(i).at(1) - bc.at(i).at(0)) * A.at(bc.at(i).at(0));
    cout << sum << endl;
    A.at(bc.at(i).at(1)) += A.at(bc.at(i).at(0));
    A.at(bc.at(i).at(0)) = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve() {
  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  map<ll, ll> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    m[a[i]]++;
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    ll b, c;
    cin >> b >> c;
    sum += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << sum << endl;
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

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

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

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

  vector<int> s(100000, 0);
  int64_t sum = 0;

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

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

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

    s[c[i]] += s[b[i]];
    sum += (c[i] - b[i]) * s[b[i]];
    s[b[i]] = 0;

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
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;
  vector<int> b(q);
  vector<int> c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }

  int64_t sum;

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

    for (int j = 0; j < n; j++) {
      if (a[j] == b[i]) {
        a[j] = c[i];
      }

      sum += a[j];
    }

    cout << sum << endl;
  }

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

#include <iostream>
using namespace std;

int main() {
  long long n;
  cin >> n;
  long long i, p, q, r, sum = 0;
  long long arr[100001] = {0};
  for (i = 0; i < n; i++) {
    cin >> p;
    arr[p]++;
    sum += p;
  }
  cin >> q;
  while (q--) {
    cin >> p >> r;
    sum = sum - (arr[p] * p);
    sum = sum + (arr[p] * r);
    arr[r] = arr[r] + arr[p];
    arr[p] = 0;
    cout << sum << "\\n";
  }

  // your code goes here
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long n;
  cin >> n;
  long long i, p, q, r, sum = 0;
  long long arr[n];
  for (i = 0; i < n; i++)
    cin >> arr[i];
  cin >> q;
  while (q--) {
    cin >> p >> r;
    for (i = 0; i < n; i++) {
      if (arr[i] == p)
        arr[i] = r;
      sum += arr[i];
    }
    cout << sum << "\\n";
    sum = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

vector<ll> cnt(100010, 0);

int main() {
  ll N;
  ll ans = 0;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) {
    cin >> A[i];
    cnt[A[i]]++;
    ans += A[i];
  }
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  REP(i, Q) cin >> B[i] >> C[i];

  REP(i, Q) {
    ll tmp = cnt[B[i]];
    cnt[B[i]] = 0;
    cnt[C[i]] += tmp;
    ans += tmp * (C[i] - B[i]);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

vector<ll> cnt(100010, 0);

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll Q;
  cin >> Q;
  vector<ll> B(N), C(N);
  REP(i, Q) cin >> B[i] >> C[i];
  ll ans = 0;
  REP(i, N) {
    cnt[A[i]]++;
    ans += A[i];
  }
  REP(i, Q) {
    if (C[i] == B[i]) {
      cout << ans << endl;
      continue;
    }
    cnt[C[i]] += cnt[B[i]];
    ans += cnt[B[i]] * (C[i] - B[i]);
    cnt[B[i]] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int n;
  cin >> n;
  vector<int> cnt(1e5 + 1);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt.at(x)++;
    sum += x;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    cnt.at(c) += cnt.at(b);
    sum += (c - b) * cnt.at(b);
    cnt.at(b) = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) ((int)(v).size())
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double PI = acos(-1);

int main(void) {
  int n;
  cin >> n;
  vector<int> a(1e5);
  rep(i, n) {
    int x;
    cin >> x;
    a.at(x)++;
  }
  int q;
  cin >> q;
  rep(z, q) {
    int qb, qc;
    cin >> qb >> qc;
    a.at(qc) += a.at(qb);
    a.at(qb) = 0;
    int ans = 0;
    rep(i, n) { ans += a.at(i) * (i + 1); }
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF INT_MAX
#define ll long long
#define ull unsigned long long
#define rep(i, n) for (ull i = 0; i < n; ++i)
#define FOR(i, s, e) for (ull i = s; i < e; ++i)
#define MOD 1000000007
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ull N;
  cin >> N;
  vector<ull> P(100010, 0);
  rep(i, N) {
    ull A;
    cin >> A;
    P[A]++;
  }
  ull sum = 0;
  rep(i, P.size()) { sum += i * P[i]; }
  ull Q;
  cin >> Q;
  vector<ull> S(Q);
  rep(i, Q) {
    ull B, C;
    cin >> B >> C;
    sum += C * P[B];
    sum -= B * P[B];
    P[C] += P[B];
    P[B] = 0;
    S[i] = sum;
  }
  for (auto s : S)
    cout << s << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF INT_MAX
#define ll long long
#define ull unsigned long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define FOR(i, s, e) for (ll i = s; i < e; ++i)
#define MOD 1000000007
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> P(100010, 0);
  rep(i, N) {
    int A;
    cin >> A;
    P[A]++;
  }
  int Q;
  cin >> Q;
  vector<ll> S(Q);
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    P[C] += P[B];
    P[B] = 0;
    rep(j, P.size()) { S[i] += j * P[j]; }
  }
  for (auto s : S)
    cout << s << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

void solve() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  rep(i, n) mp[a[i]]++;
  int q;
  cin >> q;
  ll sum = 0;
  for (auto m : mp) {
    sum += ((ll)m.first * m.second);
  }
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int cnt = mp[b];
    sum += ((ll)mp[b] * (c - b));
    mp[c] += cnt;
    mp[b] = 0;
    cout << sum << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

void solve() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  rep(i, n) mp[a[i]]++;
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int cnt = mp[b];
    mp[c] += cnt;
    mp[b] = 0;
    ll ans = 0;
    for (auto m : mp) {
      ans += ((ll)m.first * m.second);
    }
    cout << ans << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}