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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(100005, 0);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[t]++;
    sum += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    sum -= a[b] * b;
    sum += a[b] * c;
    a[b] = 0;
    cout << sum << '\\n';
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n + 1, 0);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[t]++;
    sum += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    sum -= a[b] * b;
    sum += a[b] * c;
    a[b] = 0;
    cout << sum << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  ll n;
  cin >> n;
  ll count[(ll)(1e5 + 1)] = {0};
  ll sum = 0;
  rep0(i, n - 1) {
    ll tmp;
    cin >> tmp;
    sum += tmp;
    count[tmp]++;
  }
  ll q;
  cin >> q;

  rep1(i, q) {
    ll b, c;
    cin >> b >> c;
    if (count[b] > 0) {
      sum += count[b] * (c - b);
      count[c] += count[b];
      count[b] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  ll n;
  cin >> n;
  ll a[n];
  rep0(i, n - 1) cin >> a[i];
  ll q;
  cin >> q;
  sort(a, a + n);
  ll sum = 0;
  rep0(i, n - 1) sum += a[i];

  rep1(i, q) {
    ll b, c;
    cin >> b >> c;
    auto itr_bottom = lower_bound(a, a + n, b) - a;
    auto itr_up = lower_bound(a, a + n, b + 0.5) - a;
    sum += (itr_up - itr_bottom) * (c - b);
    for (auto j = itr_bottom; j < itr_up; j++) {
      a[j] = c;
    }
    sort(a, a + n);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// c++ テンプレ
#include <bits/stdc++.h>
using namespace std;

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
llint a[100005];
llint sum = 0;
void calc(int b, int c) {
  llint x = a[b];
  sum -= (b * x);
  a[b] = 0;
  a[c] += x;
  sum += (c * x);
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    llint x;
    cin >> x;
    a[x]++;
  }
  for (llint i = 1; i < 100005; i++) {
    sum += (i * a[i]);
  }
  int q;
  cin >> q;
  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    calc(b, c);
    cout << sum << endl;
  }
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

// c++ テンプレ
#include <bits/stdc++.h>
using namespace std;

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<int> a;

llint calc(int b, int c) {
  llint ret = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == b)
      a[i] = c;
    ret += a[i];
  }
  return ret;
}
void solve() {
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    cout << calc(b, c) << endl;
  }
}

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll seive[(int)2e5];
int main() {
  int t;
  cin >> t;
  ll sum = 0;
  for (int i = 0; i < t; i++) {
    ll k;
    cin >> k;
    sum += k;
    seive[k]++;
  }
  int k;
  cin >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    sum += ((ll)seive[a] * (b - a));
    seive[b] += seive[a];
    seive[a] = 0;
    cout << sum << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int seive[10000];
int main() {
  int t;
  cin >> t;
  ll sum = 0;
  for (int i = 0; i < t; i++) {
    ll k;
    cin >> k;
    sum += k;
    seive[k]++;
  }
  int k;
  cin >> k;
  while (k--) {
    ll a, b;
    cin >> a >> b;
    sum += ((ll)seive[a] * (b - a));
    seive[b] += seive[a];
    seive[a] = 0;
    cout << sum << '\\n';
  }
}
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++)

int main() {
  int N;
  cin >> N;
  vector<int> vec(100000);
  int Q;
  long Ans;
  Ans = 0;
  rep(i, N) {
    cin >> Q;
    Ans += Q;
    vec.at(Q - 1)++;
  }
  int B, C;
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    Ans += (C - B) * vec.at(B - 1);
    vec.at(C - 1) += vec.at(B - 1);
    vec.at(B - 1) = 0;
    cout << Ans << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N, Ans;
  cin >> N;
  vector<int> A(N);
  Ans = 0;
  rep(i, N) {
    cin >> A.at(i);
    Ans += A.at(i);
  }
  sort(A.begin(), A.end());
  int Q, B, C;
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    rep(i, N) {
      if (A.at(i) > B) {
        break;
      } else if (A.at(i) == B) {
        A.at(i) = C;
        Ans += C - B;
      }
    }
    sort(A.begin(), A.end());
    cout << Ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<size_t> vUnique(vector<size_t> vec) {
  std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

int main() {
  size_t N, Q;
  size_t A[100000 + 1];
  vector<size_t> Alist;
  for (int i = 0; i < 100000 + 1; ++i) {
    A[i] = 0;
  }

  cin >> N;
  for (size_t i = 0; i < N; ++i) {
    size_t t;
    cin >> t;
    A[t]++;
    Alist.push_back(t);
  }
  Alist = vUnique(Alist);

  size_t sum = 0;
  for (size_t j = 0; j < Alist.size(); ++j) {
    sum += A[Alist[j]] * Alist[j];
  }

  cin >> Q;
  for (size_t i = 0; i < Q; ++i) {
    size_t b, c;
    cin >> b;
    cin >> c;
    A[c] += A[b];
    sum += A[b] * (c - b);
    A[b] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

vector<size_t> vUnique(vector<size_t> vec) {
  std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

int main() {
  size_t N, Q;
  size_t A[100000 + 1];
  vector<size_t> Alist;
  for (int i = 0; i < 100000 + 1; ++i) {
    A[i] = 0;
  }

  cin >> N;
  for (size_t i = 0; i < N; ++i) {
    size_t t;
    cin >> t;
    A[t]++;
    Alist.push_back(t);
  }
  Alist = vUnique(Alist);

  cin >> Q;
  for (size_t i = 0; i < Q; ++i) {
    size_t b, c;
    cin >> b;
    cin >> c;
    A[c] += A[b];
    A[b] = 0;
    auto result = find(Alist.begin(), Alist.end(), b);
    if (result != Alist.end()) {
      Alist.erase(result);
    }
    result = find(Alist.begin(), Alist.end(), c);
    if (result == Alist.end()) {
      Alist.push_back(c);
    }
    size_t sum = 0;
    for (size_t j = 0; j < Alist.size(); ++j) {
      sum += A[Alist[j]] * Alist[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  vector<int> A(100010, 0);
  int long long sum = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[a] += 1;
    sum += a;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    sum += c * A[b] - b * A[b];
    A[c] += A[b];
    A[b] = 0;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(10010, 0);
  int long long sum = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[a] += 1;
    sum += a;
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    sum += c * A[b] - b * A[b];
    A[c] += A[b];
    A[b] = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 1e5 + 10;
int cnt[MAX];

int main() {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    cnt[x - 1]++;
  }
  int c, u, v;
  cin >> c;
  while (c--) {
    cin >> u >> v;
    sum += (v * cnt[u - 1]) - (u * cnt[u - 1]);
    cnt[v - 1] += cnt[u - 1];
    cnt[u - 1] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

const int MAX = 1e5 + 10;
int cnt[MAX];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    cnt[x]++;
  }
  int c, u, v, sum = 0;
  cin >> c;
  while (c--) {
    cin >> u >> v;
    cnt[v - 1] += cnt[u - 1];
    cnt[u - 1] = 0;
    for (int i = 0; i < MAX; i++)
      sum += (cnt[i] * (i + 1));
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007
#define inf 1e18
#define ll long long
#define PI 3.1415926536

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, input;
  cin >> n;
  map<ll, ll> map;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> input;
    sum += input;
    map[input]++;
  }

  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - (map[b] * b) + (map[b] * c);
    cout << sum << endl;
    map[c] += map[b];
    map[b] = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007
#define inf 1e18
#define ll long long
#define PI 3.1415926536

int first(int arr[], int low, int high, int x, int n) {
  if (high >= low) {
    int mid = low + (high - low) / 2;
    if ((mid == 0 || x > arr[mid - 1]) && arr[mid] == x)
      return mid;
    else if (x > arr[mid])
      return first(arr, (mid + 1), high, x, n);
    else
      return first(arr, low, (mid - 1), x, n);
  }
  return -1;
}

/* if x is present in arr[] then returns the index of
LAST occurrence of x in arr[0..n-1], otherwise
returns -1 */
int last(int arr[], int low, int high, int x, int n) {
  if (high >= low) {
    int mid = low + (high - low) / 2;
    if ((mid == n - 1 || x < arr[mid + 1]) && arr[mid] == x)
      return mid;
    else if (x < arr[mid])
      return last(arr, low, (mid - 1), x, n);
    else
      return last(arr, (mid + 1), high, x, n);
  }
  return -1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  int sum = 0;
  int arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  int q;
  cin >> q;
  sort(arr, arr + n);
  int b, c;
  while (q--) {
    cin >> b >> c;
    sort(arr, arr + n);
    int firstOc = first(arr, 0, n - 1, b, n);
    if (firstOc != -1) {
      int lastOc = last(arr, 0, n - 1, b, n);
      int multiplier = lastOc - firstOc + 1;
      sum = sum - (multiplier * b) + (multiplier * c);
      for (int i = firstOc; i <= lastOc; i++) {
        arr[i] = c;
      }
    }

    cout << sum << endl;
  }

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

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

using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ordered_set                                                            \\
  tree<pll, null_type, less<pll>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define endl "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define int long long int

#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define ones(x) __builtin_popcount(x)
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 998244353

ll mod_pow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

ll mod_inverse(ll a) { return mod_pow(a, mod - 2); }

void solve() {
  int n, q;

  cin >> n;

  map<int, int> m;

  int sm = 0;

  for (int i = 0; i < n; ++i) {
    int x;

    cin >> x;

    m[x]++;

    sm += x;
  }

  cin >> q;

  int x, y;

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

    if (x == y) {
      continue;
    }

    sm = sm - m[x] * x + m[x] * y;

    int rem = m[x];

    m.erase(x);

    m[y] += rem;

    cout << sm << endl;
  }
}

signed main() {
  fast;

  int t = 1;

  // cin >> t;

  while (t--) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define bg begin()
#define end end()
#define sz size()
#define vi vector<int>
#define vl vector<long long int>
int main() {
  fast;
  int n, q, b, c, sum;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cin >> q;
  while (q--) {
    sum = 0;
    cin >> b >> c;
    for (int i = 0; i < n; i++)
      if (arr[i] == b)
        arr[i] = c;
    for (int i = 0; i < n; i++)
      sum += arr[i];
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define optimize                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define ld long double
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define vll vector<ll>
#define vd vector<double>
#define pb push_back
#define lp(i, a, b) for (int i = a; i <= b; i++)
#define rlp(i, a, b) for (int i = b; i >= a; i--)
#define a_sort(v) sort(v.begin(), v.end())
#define d_sort(v) sort(v.rbegin(), v.rend())
#define pii pair<int, int>
#define pi 3.14159265358979323846264338327950
using namespace std;
// #define debug

const int MOD = 1E9 + 7;
const int N = 1E5;

vi cnt(N + 5, 0);
int main() {
#ifdef debug
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // debug
  optimize

      int n;
  cin >> n;
  ll sum = 0;
  vll a(n);
  lp(i, 0, n - 1) {
    cin >> a[i];
    sum += a[i];
    ++cnt[a[i]];
  }
  int q;
  cin >> q;
  lp(qq, 1, q) {
    ll ai, bi;
    cin >> ai >> bi;

    sum = sum + (bi - ai) * cnt[ai];
    cnt[bi] += cnt[ai];
    cnt[ai] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define optimize                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define ld long double
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define vll vector<ll>
#define vd vector<double>
#define pb push_back
#define lp(i, a, b) for (int i = a; i <= b; i++)
#define rlp(i, a, b) for (int i = b; i >= a; i--)
#define a_sort(v) sort(v.begin(), v.end())
#define d_sort(v) sort(v.rbegin(), v.rend())
#define pii pair<int, int>
#define pi 3.14159265358979323846264338327950
using namespace std;
// #define debug

const int MOD = 1E9 + 7;
const int N = 1E5;

int main() {
#ifdef debug
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // debug
  optimize

      int n;
  cin >> n;
  ll sum = 0;
  vll a(n);
  lp(i, 0, n - 1) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  lp(qq, 1, q) {
    ll ai, bi;
    cin >> ai >> bi;
    a_sort(a);
    int lb = lower_bound(a.begin(), a.end(), ai) -
             a.begin(); // cout << "lb " << lb << endl ;
    int up = upper_bound(a.begin(), a.end(), ai) - a.begin() -
             1; // cout << "ub " << up << endl ;
    for (int i = lb; i <= up; i++)
      a[i] = bi;
    sum = sum - (up - lb + 1) * (ai - bi);
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (int)(n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define INF 2000000000

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;

// const double PI = 3.1415926535897932384626433832795;

void show(map<LL, LL> mp) {
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    if (itr->second != 0) {
      printf("%lld: %lld, ", itr->first, itr->second);
    }
  }
  printf("\\n");
}

int main() {
  int n;
  scanf("%d", &n);
  // cin >> n;
  map<LL, LL> mp;

  int t;
  REP(i, n) {
    scanf("%d", &t);
    // cin >> t;
    mp[t] += 1;
  }
  /*
  LL total = 0;
  REP(i, 10) {
    total += i * mp[i];
  }
  */
  LL total = 0;
  // show(mp);
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    total += itr->first * itr->second;
  }
  // cout << "total:" << total << endl;
  int q;
  scanf("%d", &q);
  int b, c;
  REP(i, q) {
    scanf("%d %d", &b, &c);
    mp[c] += mp[b];
    total -= b * mp[b];
    total += c * mp[b];
    mp.erase(b);
    // show(mp);
    // LL total = 0;
    // for(auto itr = mp.begin(); itr != mp.end(); itr++) {
    //   total += itr->first * itr->second;
    // }
    printf("%lld\\n", total);
    // cout << total << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (int)(n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define INF 2000000000

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;

// const double PI = 3.1415926535897932384626433832795;

void show(map<LL, LL> mp) {
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    if (itr->second != 0) {
      printf("%d: %d, ", itr->first, itr->second);
    }
  }
  printf("\\n");
}

int main() {
  int n;
  scanf("%d", &n);
  // cin >> n;
  map<LL, LL> mp;

  int t;
  REP(i, n) {
    scanf("%d", &t);
    // cin >> t;
    mp[t] += 1;
  }
  LL total = 0;
  REP(i, 10) { total += i * mp[i]; }
  int q;
  scanf("%d", &q);
  int b, c;
  REP(i, q) {
    scanf("%d %d", &b, &c);
    // cin >> b >> c;
    // cout << b << ' ' << c << endl;
    mp[c] += mp[b];
    mp.erase(b);
    LL total = 0;
    // show(mp);
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
      total += itr->first * itr->second;
    }
    printf("%lld\\n", total);
    // cout << total << endl;
  }
}
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(0);
  int n;
  map<int, int> m;
  cin >> n;
  long long t = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    m[a]++;
    t += a;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int cnt = m[b];
    m[b] = 0;
    m[c] += cnt;
    t += cnt * (c - b);
    cout << t << '\\n';
  }

  return 0;
}

//      *   *  *****  *   *  *   *   ****  ****    ****     **  ***
//     *   *  *      **  *  *   *  *      *   *  *        * *  *  *
//    *****  *****  * * *  *   *  *      ****   *       *  *    *
//   *   *      *  *  **  *   *  *      *  *   *       *****  *
//  *   *  *****  *   *   ***    ****  *   *    ****     *   ****

//     ****  *   *  *   *   ***   *   *  *****  *   *
//    *     *   *  *   *  *   *  *   *  *      *   *
//    ***  *****  *   *  *****  * * *  ****   *   *
//      *   *    *   *  *   *  * * *  *      *   *
//  ****   *     ***   *   *   * *   *****   ***


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  int arr[100000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int t = 0;
  for (int i = 0; i < n; i++) {
    t += arr[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] == b) {
        cnt++;
        arr[i] = c;
      }
    }
    t += cnt * (c - b);
    cout << t << '\\n';
  }

  return 0;
}

//      *   *  *****  *   *  *   *   ****  ****    ****     **  ***
//     *   *  *      **  *  *   *  *      *   *  *        * *  *  *
//    *****  *****  * * *  *   *  *      ****   *       *  *    *
//   *   *      *  *  **  *   *  *      *  *   *       *****  *
//  *   *  *****  *   *   ***    ****  *   *    ****     *   ****

//     ****  *   *  *   *   ***   *   *  *****  *   *
//    *     *   *  *   *  *   *  *   *  *      *   *
//    ***  *****  *   *  *****  * * *  ****   *   *
//      *   *    *   *  *   *  * * *  *      *   *
//  ****   *     ***   *   *   * *   *****   ***