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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  cin >> q;
  vector<pair<int, int>> bc(q);
  rep(i, q) cin >> bc[i].first >> bc[i].second;

  vector<int> qty(100001);
  rep(i, n) qty[a[i]]++;
  long long sum = 0;
  for (int j = 1; j < qty.size(); j++) {
    sum += qty[j] * j;
  }
  rep(i, q) {
    int before = bc[i].first;
    int after = bc[i].second;

    sum += (after - before) * qty[before];

    qty[after] += qty[before];
    qty[before] = 0;

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  cin >> q;
  vector<pair<int, int>> bc(q);
  rep(i, q) cin >> bc[i].first >> bc[i].second;

  vector<long long> qty(100001);
  vector<long long> index;
  rep(i, n) {
    qty[a[i]]++;
    index.push_back(a[i]);
  }
  sort(index.begin(), index.end());
  index.erase(unique(index.begin(), index.end()), index.end());

  rep(i, q) {
    int before = bc[i].first;
    int after = bc[i].second;
    qty[after] += qty[before];
    qty[before] = 0;

    vector<long long>::iterator itr = find(index.begin(), index.end(), after);
    if (itr == index.end())
      index.push_back(after);
    itr = find(index.begin(), index.end(), before);
    if (itr != index.end())
      index.erase(itr);

    long long sum = 0;
    for (long long j : index) {
      sum += qty[j] * j;
    }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#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(n) begin(n), end(n)
#define IN(a, x, b) (a <= x && x < b)
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);
template <class T> inline T CHMAX(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T CHMIN(T &a, const T b) {
  return a = (a > b) ? b : a;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);

int main() {
  INIT;
  int n, q;
  cin >> n;
  vector<int> m(100005, 0);
  ll total = 0;
  REP(i, n) {
    int a;
    cin >> a;
    m[a] += 1;
    total += a;
  }
  cin >> q;
  int b;
  int c;
  REP(i, q) {
    cin >> b >> c;
    int s = c - b;
    total = total + (m[b] * s);
    cout << total << "\\n";
    m[c] += m[b];
    m[b] = 0;
  }
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#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(n) begin(n), end(n)
#define IN(a, x, b) (a <= x && x < b)
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);
template <class T> inline T CHMAX(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T CHMIN(T &a, const T b) {
  return a = (a > b) ? b : a;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);

int main() {
  INIT;
  int n, q;
  cin >> n;
  vector<int> m(105, 0);
  ll total = 0;
  REP(i, n) {
    int a;
    cin >> a;
    m[a] += 1;
    total += a;
  }
  cin >> q;
  int b;
  int c;
  REP(i, q) {
    cin >> b >> c;
    int s = c - b;
    total = total + (m[b] * s);
    cout << total << "\\n";
    m[c] += m[b];
    m[b] = 0;
  }
  cout << endl;
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  int N;
  unordered_map<int, int> A;
  int t1;
  cin >> N;

  long initSum = 0;
  for (int i = 0; i != N; i++) {
    cin >> t1;
    A[t1]++;
    initSum += t1;
  }

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

  long out = initSum;
  for (int i = 0; i != Q; i++) {
    out = (out + A[B[i]] * (C[i] - B[i]));
    A[C[i]] += A[B[i]];
    A[B[i]] = 0;
    cout << out;
    /*for(auto itr = A.begin(); itr != A.end(); ++itr) {
    std::cout << "(" << itr->first           // キーを表示
                    << ", " << itr->second << ") ";
    }*/
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  multiset<int> A;
  int t1;
  cin >> N;

  long initSum = 0;
  for (int i = 0; i != N; i++) {
    cin >> t1;
    A.insert(t1);
    initSum += t1;
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i != Q; i++) {
    cin >> B[i];
    cin >> C[i];
  }

  long out = initSum;
  for (int i = 0; i != Q; i++) {
    auto beg = A.lower_bound(B[i]);
    auto end = upper_bound(beg, A.end(), B[i]);
    int n = distance(beg, end);
    out = (out + n * (C[i] - B[i]));
    A.erase(beg, end);
    vector<int> ins(n, C[i]);
    A.insert(ins.begin(), ins.end());
    cout << out;
    // cout << out << " " <<  n << " ";
    // for(auto it=A.begin(); it!=A.end(); it++) cout << *it << ",";
    cout << 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;
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  int q;
  cin >> q;
  vector<pair<int, int>> change(q);
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    change[i] = make_pair(b, c);
  }
  map<int, int> mp;
  rep(i, n) { mp[v[i]]++; }
  ll ans = 0;
  for (auto p : mp) {
    ans += (ll)p.second * (ll)p.first;
  }
  rep(i, q) {
    int b, c;
    b = change[i].first;
    c = change[i].second;
    int k = mp[b];
    mp[b] = 0;
    mp[c] += k;
    ans -= (ll)b * (ll)k;
    ans += (ll)c * (ll)k;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  int q;
  cin >> q;
  vector<pair<int, int>> change(q);
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    change[i] = make_pair(b, c);
  }
  map<int, int> mp;
  rep(i, n) { mp[v[i]]++; }
  rep(i, q) {
    int b, c;
    b = change[i].first;
    c = change[i].second;
    int k = mp[b];
    mp[b] = 0;
    mp[c] += k;
    ll ans = 0;
    for (auto p : mp) {
      ans += (ll)p.second * (ll)p.first;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  rep(i, N) { ans += A[i]; }

  rep(i, Q) {
    ans -= count[B[i]] * B[i];
    ans -= count[C[i]] * C[i];
    count[C[i]] += count[B[i]];
    ans += count[C[i]] * C[i];
    count[B[i]] = 0;
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

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

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

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

  rep(i, N) { ans += A[i]; }

  rep(i, Q) {
    ans -= count[B[i]] * B[i];
    ans -= count[C[i]] * C[i];
    count[C[i]] += count[B[i]];
    ans += count[C[i]] * C[i];
    count[B[i]] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, k = 0;
  cin >> n;
  vector<ll> a(100000);
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    k += x;
    a[x - 1]++;
  }

  ll q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    ll x = a[b - 1];
    a[b - 1] = 0;
    a[c - 1] += x;
    k += (c - b) * x;
    cout << k << endl;
  }
}

Error: TLE

Fixed submission:

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

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

  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    ll ans = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] == b) {
        a[j] = c;
        ans++;
      }
    }
    k += (c - b) * ans;
    cout << k << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, q;
  long long sum = 0;
  vector<int> x(pow(10, 5) + 1, 0); // カウント用

  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  cin >> q;
  vector<int> b(q), c(q);
  for (int i = 0; i < q; i++)
    cin >> b.at(i) >> c.at(i);

  for (int i = 0; i < n; i++)
    x.at(a.at(i)) += 1; // デフォルトカウント
  for (int i = 0; i < x.size(); i++)
    sum += x.at(i) * i; // デフォ合計

  for (int i = 0; i < q; i++) {
    sum += (c.at(i) - b.at(i)) * x.at(b.at(i));
    x.at(c.at(i)) += x.at(b.at(i)); // CiにBiを追加
    x.at(b.at(i)) = 0;              // Biを削除
    cout << sum << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)


Fixed submission:

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

int main() {
  int n, q;
  long long sum = 0;
  vector<int> x(pow(10, 5) + 1, 0); // カウント用
  vector<int> a(n), b(q), c(q);     // A,B,C

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a.at(i); // A
  cin >> q;
  for (int i = 0; i < q; i++)
    cin >> b.at(i) >> c.at(i); // B,C

  for (int i = 0; i < n; i++)
    x.at(a.at(i)) += 1; // デフォルトカウント
  for (int i = 0; i < x.size(); i++)
    sum += x.at(i) * i; // デフォ合計

  for (int i = 0; i < q; i++) {
    sum += (c.at(i) - b.at(i)) * x.at(b.at(i)); // sumの計算
    x.at(c.at(i)) += x.at(b.at(i));             // CiにBiを追加
    x.at(b.at(i)) = 0;                          // Biを削除
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, q, bi, ci, maxn = (int)1e6, tmp;
ll tot;
vector<int> freq(maxn, 0);

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    freq[tmp]++;
    tot += (ll)tmp;
  }

  cin >> q;
  while (q--) {
    cin >> bi >> ci;
    tmp = freq[bi];

    if (tmp) {
      freq[bi] = 0;
      freq[ci] += tmp;

      tot += (ll)ci * (ll)tmp - ((ll)bi * (ll)tmp);
    }
    cout << tot << endl;
  }
}

Error: TLE

Fixed submission:

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

int n, q, bi, ci;
ll tot;
vector<int> arr;
multiset<int> segments;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;

  arr.resize(n);

  for (int &x : arr) {
    cin >> x;
    segments.insert(x);
    tot += (ll)x;
  }

  cin >> q;
  while (q--) {
    cin >> bi >> ci;
    int qtd = segments.count(bi);

    if (qtd) {
      tot -= (ll)qtd * (ll)bi;
      tot += (ll)qtd * (ll)ci;

      segments.erase(bi);

      auto it = segments.insert(ci);

      for (int i = 1; i < qtd; i++)
        segments.insert(it, ci);
    }

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

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

int INF = 100001;

signed main() {
  int N;
  cin >> N;
  vector<int> ans(INF);
  rep(i, N) {
    int A;
    cin >> A;
    ans[A] += 1;
  }
  int sum = 0;
  rep(i, INF) sum += i * ans[i];
  int Q;
  cin >> Q;
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    sum -= B * ans[B];
    sum -= C * ans[C];
    ans[C] += ans[B];
    ans[B] = 0;
    sum += B * ans[B];
    sum += C * ans[C];
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  int Q;
  cin >> Q;
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    rep(j, N) {
      if (A[j] == B) {
        A[j] = C;
      }
    }
    int Asum = accumulate(A.begin(), A.end(), 0);
    cout << Asum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

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

  vector<ll> a(n);

  map<ll, ll> count;

  ll s = 0;
  for (ll &x : a) {
    ll tmp;
    cin >> tmp;
    s += tmp;
    x = tmp;

    count[tmp]++;
  }

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

    s -= b * count[b];
    s += c * count[b];
    count[c] += count[b];
    count.erase(b);

    cout << s << endl;
  }
}


Error: TLE

Fixed submission:

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

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

  vector<ll> a(n);

  map<ll, ll> count;

  for (ll &x : a) {
    ll tmp;
    cin >> tmp;
    x = tmp;

    count[tmp]++;
  }

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

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

    ll s = 0;
    for (auto x : count) {
      s += x.first * x.second;
    }
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N, Q;

  long long A[100010];
  long long B[100010];
  long long C[100010];

  cin >> N;

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

  cin >> Q;

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

  long long res = 0;

  for (long long j = 1; j < 100010; j++) {
    if (A[j] > 0) {
      res += A[j] * j;
    }
  }

  for (long long i = 0; i < Q; i++) {

    res -= B[i] * A[B[i]];
    res -= C[i] * A[C[i]];

    A[C[i]] += A[B[i]];
    A[B[i]] = 0;

    res += B[i] * A[B[i]];
    res += C[i] * A[C[i]];

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N, Q;

  long long A[100010];
  long long B[100010];
  long long C[100010];

  cin >> N;

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

  cin >> Q;

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

  for (long long i = 0; i < Q; i++) {
    long long res = 0;
    for (long long j = 1; j < 100010; j++) {

      if (j == B[i]) {
        long long tmp;
        tmp = A[j];
        for (long long k = 0; k < tmp; k++) {
          A[j]--;
          A[C[i]]++;
        }
      } else {
      }
    }
    for (long long j = 1; j < 100010; j++) {
      if (A[j] > 0) {
        res += A[j] * j;
      }
    }
    cout << res << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll n, q;
  cin >> n;
  ll sum = 0;
  vector<ll> A(100000, 0);
  vector<ll> S;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    A[k]++;
    sum += k;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    sum += A[b] * c - A[b] * b;
    S.emplace_back(sum);
    A[c] += A[b];
    A[b] = 0;
  }
  for (int i = 0; i < S.size(); i++) {
    cout << S[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll n, q;
  cin >> n;
  vector<ll> A;
  vector<ll> B;
  vector<ll> C;
  vector<ll> S;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    A.emplace_back(k);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    B.emplace_back(b);
    C.emplace_back(c);
    ll sum = 0;
    for (int j = 0; j < A.size(); j++) {
      if (A[j] == B[i]) {
        A[j] = C[i];
      }
      sum += A[j];
    }
    S.emplace_back(sum);
  }
  for (int i = 0; i < S.size(); i++) {
    cout << S[i] << 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;

int main() {
  int n, q;
  ll ans = 0;
  vector<int> a(200000), count(200000, 0);

  cin >> n;
  rep(i, n) {
    cin >> a.at(i);
    count.at(a.at(i))++;
    ans += a.at(i);
  }
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    ans += (c - b) * count.at(b);
    count.at(c) += count.at(b);
    count.at(b) = 0;
    cout << ans << endl;
  }
  return 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;

int main() {
  int n, q, ans;
  vector<int> a(200000), b(200000), c(200000);

  cin >> n;
  rep(i, n) cin >> a.at(i);
  cin >> q;
  rep(i, q) cin >> b.at(i) >> c.at(i);

  rep(i, q) {
    rep(j, n) {
      if (a.at(j) == b.at(i))
        a.at(j) = c.at(i);
    }
    cout << accumulate(a.begin(), a.end(), 0) << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// V - Subtree
// https://atcoder.jp/contests/dp/tasks/dp_v
// https://www.youtube.com/watch?v=r9hbdyVkz-I
#include <bits/stdc++.h>
using namespace std;

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

typedef long long ll;

#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 FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  int n, q;
  cin >> n;
  map<int, ll> m;
  int a;
  rep(i, n) {
    cin >> a;
    m[a]++;
  }
  ll ans = 0;
  for (auto itr = m.begin(); itr != m.end(); itr++)
    ans += (*itr).F * (*itr).S;
  cin >> q;
  int b, c;
  rep(i, q) {
    cin >> b >> c;
    ans -= b * m[b];
    ans += c * m[b];
    cout << ans << endl;
    ll temp = m[b];
    m[b] = 0;
    m[c] += temp;
  }
  return 0;
}

Error: TLE

Fixed submission:

// V - Subtree
// https://atcoder.jp/contests/dp/tasks/dp_v
// https://www.youtube.com/watch?v=r9hbdyVkz-I
#include <bits/stdc++.h>
using namespace std;

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

typedef long long ll;

#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 FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  int n, q;
  cin >> n;
  map<int, ll> m;
  int a;
  rep(i, n) {
    cin >> a;
    m[a]++;
  }
  cin >> q;
  int b, c;
  rep(i, q) {
    cin >> b >> c;
    ll temp = m[b];
    m[b] = 0;
    m[c] += temp;
    ll ans = 0;
    for (auto itr = m.begin(); itr != m.end(); itr++)
      ans += (*itr).F * (*itr).S;
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> ctr(100001, 0);
    ll val = 0ll;
    for (int i = 1; i <= n; i++) {
      ll a;
      cin >> a;
      ctr[a]++;
      val += a;
    }
    ll q;
    cin >> q;
    while (q--) {
      ll b, c;
      cin >> b >> c;
      val += (c - b) * ctr[b];
      ctr[c] += ctr[b];
      ctr[b] = 0;
      cout << val << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> ctr(n + 1, 0);
    ll val = 0ll;
    for (int i = 1; i <= n; i++) {
      ll a;
      cin >> a;
      ctr[a]++;
      val += a;
    }
    ll q;
    cin >> q;
    while (q--) {
      ll b, c;
      cin >> b >> c;
      val += (c - b) * ctr[b];
      ctr[c] += ctr[b];
      ctr[b] = 0;
      cout << val << "\\n";
    }
  }
  return 0;
}