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

#include <bits/stdc++.h>

using namespace std;

using ll_t = long long;
using occ_t = unordered_map<int, int>;

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

  ll_t sum = 0;
  occ_t occ;
  while (n--) {
    int i;
    cin >> i;
    sum += i;
    occ[i] += 1;
  }

  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;

    sum += (b - a) * occ[a];
    occ[b] += occ[a];
    occ[a] = 0;
    cout << sum << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll_t = long long;
using ints_t = vector<int>;

int main() {
  int n;
  cin >> n;
  ints_t ints;
  while (n--) {
    int i;
    cin >> i;
    ints.push_back(i);
  }

  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;

    for (auto &i : ints) {
      if (i == a) {
        i = b;
      }
    }
    ll_t sum = accumulate(ints.begin(), ints.end(), 0);
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL)
using namespace std;
#define int long long
#define MOD 1000000007
typedef vector<int> vi;
typedef pair<int, int> pi;
#define inf 1e18
#define For(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define ps(x, y) fixed << setprecision(y) << x
#define pb push_back
#define mp make_pair

vi a(100005);

int32_t main() {
  fast;
  int n, val, tot = 0;
  cin >> n;
  For(i, 0, n) {
    cin >> val;
    tot += val;
    a[val]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    tot -= (a[b] * b);
    tot += (a[b] * c);
    a[c] += a[b];
    a[b] = 0;
    cout << tot << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define ll long long

int main() {
  ll tot = 0, n, q, b, c, i;
  cin >> n;
  ll a[n];
  vector<ll> res;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    for (i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
      tot += a[i];
    }
    res.push_back(tot);
    tot = 0;
  }
  for (i = 0; i < res.size(); i++) {
    cout << res[i] << "\\n";
  }

  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;
  map<ll, ll> mp;
  cin >> n;
  ll temp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    mp[temp]++;
    sum += temp;
  }
  ll q;
  cin >> q;
  ll t1, t2;
  for (int i = 0; i < q; i++) {
    cin >> t1 >> t2;
    ll num = mp[t1];
    sum -= mp[t1] * t1;
    sum -= mp[t2] * t2;
    mp[t1] = 0;
    mp[t2] += num;
    sum += mp[t2] * t2;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ll n;
  map<ll, ll> mp;
  set<ll> st;
  cin >> n;
  ll temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    mp[temp]++;
    st.insert(temp);
  }
  ll q;
  cin >> q;
  ll t1, t2;
  for (int i = 0; i < q; i++) {
    cin >> t1 >> t2;
    int num = mp[t1];
    mp[t1] = 0;
    mp[t2] += num;
    st.erase(t1);
    st.insert(t2);
    ll sum = 0;
    for (auto i = st.begin(); i != st.end(); i++) {
      int number = *i;
      sum += number * mp[number];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long n, q, ans = 0;
  ;
  cin >> n;
  vector<long long> cnt(100002);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
    ans += a;
  }

  cin >> q;
  vector<long long> ver(q);
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    cnt[c] += cnt[b];
    ans += cnt[b] * (c - b);
    cnt[b] = 0;
    ver[i] = ans;
  }

  rep(i, q) { cout << ver[i] << endl; }
  return 0;
}

Error: WA

Fixed submission:

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

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

  rep(i, Q) {
    rep(k, N) tmp[k] = A[k];
    rep(j, N) {
      if (tmp[j] == B[i])
        tmp[j] = C[i];
    }
    int sum = 0;
    rep(i, Q) { sum += tmp[i]; }
    cout << sum << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  long long n;
  long long q;
  vector<long long> s;
  long long a[100001];
  for (long long i = 0; i < 100001; i++)
    a[i] = 0;

  cin >> n;

  s.push_back(0);

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

  cin >> q;

  for (long long i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    long long cnt = a[b];
    s[i] += cnt * (c - b);
    a[c] += cnt;
    a[b] = 0;

    s.push_back(s[i]);
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  long long n;
  long long q;
  vector<long long> s;
  multiset<long long> a;

  cin >> n;

  s.push_back(0);

  for (long long i = 0; i < n; i++) {
    long long aa;
    cin >> aa;
    s[0] += aa;
    a.insert(aa);
  }

  cin >> q;

  for (long long i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    long long cnt = a.erase(b);
    s[i] += cnt * (c - b);
    for (long long i = 0; i < cnt; i++) {
      a.insert(c);
    }

    s.push_back(s[i]);
  }

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

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

  vector<int> num(100000);
  rep(i, n) num[a[i]]++;

  ll s = 0;
  rep(i, n) s += a[i];
  rep(i, q) {
    s += (rpc[i].second - rpc[i].first) * num[rpc[i].first];
    cout << s << endl;
    num[rpc[i].second] += num[rpc[i].first];
    num[rpc[i].first] = 0;
  }

  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;
using p = pair<int, int>;

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

  ll s = 0;
  rep(i, n) s += a[i];
  rep(i, q) {
    rep(j, n) {
      if (a[j] == rpc[i].first) {
        s += rpc[i].second - a[j];
        a[j] = rpc[i].second;
      }
    }
    cout << s << endl;
  }

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


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

int main() {
  int n, q;
  vector<int> a;
  vector<pair<int, int>> bc;
  long long sum = 0;
  unordered_map<int, int> mp;
  cin >> n;
  while (n--) {
    int x;
    cin >> x;
    a.push_back(x);
    sum += x;
    mp[x]++;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    bc.emplace_back(b, c);
  }

  for (auto &p : bc) {
    sum += mp[p.first] * p.second - mp[p.first] * p.first;
    mp[p.second] += mp[p.first];
    mp[p.first] = 0;
    cout << sum << '\\n';
  }

  return 0;
}

Error: TLE

Fixed submission:


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

int main() {
  int n, q;
  vector<int> a;
  vector<pair<int, int>> bc;

  cin >> n;
  while (n--) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    bc.emplace_back(b, c);
  }
  long long sum;
  for (auto &p : bc) {
    sum = 0;
    for (int &x : a) {
      if (x == p.first)
        x = p.second;
      sum += x;
    }
    cout << sum << '\\n';
  }

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

#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
long long n, q;
long long ans[100005];
long long b, c;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long temp;
    cin >> temp;
    ans[temp]++;
    sum += temp;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    sum = sum + (c - b) * ans[b];
    ans[c] += ans[b];
    ans[b] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
int n, q;
int ans[100005];
int b, c;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    ans[temp]++;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;

    ans[c] += ans[b];
    ans[b] = 0;
    long long sum = 0;
    for (int i = 1; i <= 100000; i++) {
      sum += (i * ans[i]);
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
const int MX = 100005;

int main() {
  int N;
  long long A;
  long long Q;
  int B, C;
  long long sum;
  long long num;

  cin >> N;
  vector<long long> x(MX);

  for (int i = 1; i <= N; i++) {
    cin >> A;
    x[A]++;
    sum += A;
  }

  cin >> Q;

  long long calc;

  while (Q--) {
    cin >> B >> C;
    sum -= B * x[B];
    sum += C * x[B];
    x[C] += x[B];
    x[B] = 0;
    cout << sum << endl;
    /**
    num = C - B;
    calc = num * x[B] + sum;
    sum = calc;
    x[C] = x[C] + x[B];
    x[B] = 0;
    cout << calc << endl;
    **/
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  int Q;
  int B, C;

  cin >> N;
  vector<int> A(N);

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

  cin >> Q;
  vector<int> sum(Q);

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

  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>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<long long>> a(100001, vector<long long>(1, 0));
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.at(x).at(0)++;
    ans += x;
  }
  int q;
  cin >> q;
  while (q > 0) {
    int b, c;
    cin >> b >> c;
    long long d = a.at(b).at(0);
    a.at(c).at(0) += a.at(b).at(0);
    a.at(b).at(0) = 0;
    ans -= b * d;
    ans += c * d;

    cout << ans << endl;
    q--;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(100000, vector<int>(1));
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.at(x).at(0)++;
    ans += x;
  }
  int q;
  cin >> q;
  while (q > 0) {
    int b, c;
    cin >> b >> c;
    int d = a.at(b).at(0);
    a.at(c).at(0) += a.at(b).at(0);
    a.at(b).at(0) = 0;
    ans -= b * d;
    ans += c * d;

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

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, ll> x;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    x[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  vector<P> bc(q);
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    bc[i].first = b;
    bc[i].second = c;
  }
  rep(i, q) {
    ll y = bc[i].second - bc[i].first;
    sum += y * x[bc[i].first];
    cout << sum << endl;
    x[bc[i].second] += x[bc[i].first];
    x[bc[i].first] = 0;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, ll> x;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    x[a[i]]++;
    sum += a[i];
  }
  int q;
  vector<P> bc(q);
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    bc[i].first = b;
    bc[i].second = c;
  }
  rep(i, q) {
    ll y = bc[i].second - bc[i].first;
    sum += y * x[bc[i].first];
    cout << sum << endl;
    x[bc[i].second] += x[bc[i].first];
    x[bc[i].first] = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<ll> vi(100001, 0);

  ll sum = 0;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    vi.at(a)++;
    sum += a;
  }

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    ll from, to;
    cin >> from >> to;

    ll dif = vi.at(from) * (to - from);
    sum = sum + dif;

    vi.at(to) += vi.at(from);
    vi.at(from) -= vi.at(from);

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

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

  vector<ll> vi(100000, 0);

  ll sum = 0;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    vi.at(a)++;
    sum += a;
  }

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    ll from, to;
    cin >> from >> to;

    ll dif = vi.at(from) * (to - from);
    sum = sum + dif;

    vi.at(to) += vi.at(from);
    vi.at(from) -= vi.at(from);

    cout << sum << endl;
  }

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

// by shubham_bitsg

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<string> vs;

// #define oset tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define forl(i, a, b) for (int i = a; i < b; i++)
#define rfor(i, b, a) for (int i = b; i >= a; i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define srt(x) sort(x.begin(), x.end())
#define sumup(x) accumulate(x.begin(), x.end(), 0)
#define PI 3.141592653589793238

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vi m1(n + 1, -1);
  ll sum = 0;
  unordered_map<int, int> m2;
  forl(i, 1, n + 1) {
    int t;
    cin >> t;
    sum += t;
    m2[t]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    if (m2[b] > 0) {
      sum += (c - b) * m2[b];
      m2[c] += m2[b];
      m2[b] = 0;
    }
    //        if(!m2[b].empty()) {
    //            int len = m2[b].size();
    //            for(int i=0; i<len; i++) {
    //                m1[m2[b][i]] = c;
    //            }
    //            m2[c].insert(m2[c].end(), m2[b].begin(), m2[b].end());
    //            m2.erase(b);
    //        }
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

// by shubham_bitsg

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<string> vs;

// #define oset tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define forl(i, a, b) for (int i = a; i < b; i++)
#define rfor(i, b, a) for (int i = b; i >= a; i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define srt(x) sort(x.begin(), x.end())
#define sumup(x) accumulate(x.begin(), x.end(), 0)
#define PI 3.141592653589793238

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vi m1(n + 1, -1);
  unordered_map<int, vi> m2;
  forl(i, 1, n + 1) {
    int t;
    cin >> t;
    m1[i] = t;
    m2[t].pb(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    if (!m2[b].empty()) {
      int len = m2[b].size();
      for (int i = 0; i < len; i++) {
        m1[m2[b][i]] = c;
      }
      m2[c].insert(m2[c].end(), m2[b].begin(), m2[b].end());
      m2.erase(b);
    }
    ll sum = sumup(m1);
    sum += 1;
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// AtCoder Beginner Contest 171
// D - Replacing
//

#include <algorithm>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#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 ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;

const double PI = 3.14159265358979323846;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

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

  ll sum = 0;
  map<int, int> am;
  REP(i, n) {
    int a;
    cin >> a;
    sum += a;
    am[a]++;
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  REP(i, q) cin >> b[i] >> c[i];

  vector<ll> s(q, 0);
  REP(i, q) {
    if (am[b[i]] > 0) {
      sum -= (b[i] - c[i]) * am[b[i]];
      am[c[i]] += am[b[i]];
      am[b[i]] = 0;
    }
    s[i] = sum;
  }

  REP(i, q) cout << s[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

//
// AtCoder Beginner Contest 171
// D - Replacing
//

#include <algorithm>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#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 ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;

const double PI = 3.14159265358979323846;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

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

  ll all = 0;
  vector<int> a(n);
  REP(i, n) {
    cin >> a[i];
    all += a[i];
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  REP(i, q) cin >> b[i] >> c[i];

  vector<ll> s(q, 0);
  REP(i, q) {
    /*
    int idx = lower_bound(ALL(a), b[i]) - a.begin();
    while(idx<n){
        if (a[idx] != b[i]) break;
        int atmp = a[idx];
        a[idx] = c[i];
        all = all - (atmp - c[i]);
        idx++;
    }
    sort(ALL(a));
    */
    REP(j, n) {
      if (a[j] == b[i]) {
        int atmp = a[j];
        a[j] = c[i];
        all = all - (atmp - c[i]);
      }
    }
    s[i] = all;
  }

  REP(i, q) cout << s[i] << endl;

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

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
// #define MOD  998244353
// #define MOD ll(1e9) + 7
#define ll long long
const double PI = atan(1.0) * 4;
// const int N = 1e5 + 5 ;
// const ll INF = (int)1e9 ;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// const ll INF = (ll)2e18 + 50;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
const int N = 1e5 + 50;
vector<ll> cnt(N);

ll power(ll a, ll e) {
  ll res = 1LL;
  while (e > 0) {
    if (e % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    e /= 2;
  }
  return res;
}
vector<int> complement(const vector<int> &vec) {
  int n = vec.size();
  vector<bool> occur(n + 1, true);
  vector<int> ans;
  for (int i = 0; i < (int)vec.size(); i++) {
    occur[vec[i]] = false;
  }
  for (int i = 1; i <= n; i++) {
    if (occur[i])
      ans.push_back(i);
  }
  return ans;
}

ll intlog(double base, double x) { return (ll)(log(x) / log(base)); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t ;
  // cin >> t;
  // while (t--) {
  int n;
  cin >> n;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    cnt[b]++;
    ans += b;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ans -= b * cnt[b];
    ans -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    ans += b * cnt[b];
    ans += c * cnt[c];
    cout << ans << "\\n";
  }

  // }

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define ll long long
const double PI = atan(1.0) * 4;
// const int N = 1e5 + 5 ;
// const ll INF = (int)1e9 ;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, -1, 0, 1};
const ll INF = (ll)2e18 + 50;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t ;
  // cin >> t ;
  // while (t--) {
  int n, q;
  cin >> n;
  multiset<int> a;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a.insert(b);
  }
  ll ans = 0;
  for (std::set<int>::iterator it = a.begin(); it != a.end(); ++it) {
    ans += *it;
  }
  // cout << ans << "\\n";
  cin >> q;
  // auto replace = [&](int i , int j ) {

  // 	int freq = a.count(i) ;
  // 	ans -= freq * i ;
  // 	ans += freq * j ;
  // 	while (freq--) {
  // 		a.erase(a.find(i)) ;
  // 		a.insert(j) ;
  // 	}
  // 	// while (freq--) {
  // 	// 	a.insert(j) ;
  // 	// }

  // } ;

  while (q--) {
    // ll ans = 0 ;
    int b, c;
    cin >> b >> c;
    int freq = a.count(b);
    ans -= freq * b;
    ans += freq * c;
    while (freq--) {
      a.erase(a.find(b));
      a.insert(c);
    }
    // replace(b, c) ;
    // for (std::set<int>::iterator it = a.begin(); it != a.end(); ++it) {
    // 	ans += *it;
    // }
    // cout << ans << "\\n";

    cout << ans << "\\n";
  }

  // }

  return 0;
}