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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(var, start, stop, step)                                           \\
  for (long long var = start; var < stop; var += step)
#define vi vector<int>
#define vl vector<long long>
#define fio                                                                    \\
  cin.tie(nullptr);                                                            \\
  ios::sync_with_stdio(false);                                                 \\
  cout.tie(nullptr);

int main() {
  fio;
  ll n;
  cin >> n;
  vi v(n);
  ll sum = 0;
  map<ll, ll> m;
  loop(i, 0, n, 1) {
    cin >> v[i];
    sum += v[i];
    m[v[i]]++;
  }
  ll q;
  cin >> q;
  loop(i, 0, q, 1) {
    ll old, ne;
    cin >> old >> ne;
    sum -= m[old] * old;
    sum += ne * m[old];
    cout << sum << endl;
    m[ne] += m[old];
    m.erase(old);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(var, start, stop, step)                                           \\
  for (long long var = start; var < stop; var += step)
#define vi vector<int>
#define vl vector<long long>
#define fio                                                                    \\
  cin.tie(nullptr);                                                            \\
  ios::sync_with_stdio(false);                                                 \\
  cout.tie(nullptr);

int main() {
  fio;
  ll n;
  cin >> n;
  vi v(n);
  ll sum = 0;
  map<ll, ll> m;
  loop(i, 0, n, 1) {
    cin >> v[i];
    sum += v[i];
    m[v[i]]++;
  }
  ll q;
  cin >> q;
  loop(i, 0, q, 1) {
    ll old, ne;
    cin >> old >> ne;
    replace(v.begin(), v.end(), old, ne);
    sum -= m[old] * old;
    sum += ne * m[old];
    cout << sum << endl;
    m[ne] += m[old];
    m.erase(old);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  map<int, int> A;
  int n;
  int64_t sum = 0;

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

  int Q;
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (n = 0; n < Q; n++) {
    cin >> B.at(n);
    cin >> C.at(n);
  }

  for (n = 0; n < Q; n++) {
    int b = B.at(n);
    int c = C.at(n);
    int diff = c - b;

    if (A.count(b)) {
      if (A.count(c)) {
        A[c] += A[b];
        sum += diff * A[b];
        A.erase(b);
      } else {
        A[c] = A[b];
        sum += diff * A[b];
        A.erase(b);
      }
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

void print(vector<int> x) {
  for (int n = 0; n < x.size(); n++) {
    cout << x.at(n) << " ";
  }
  cout << endl;
}

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

  vector<int> A(N);
  int n;
  int64_t sum = 0;
  for (n = 0; n < N; n++) {
    cin >> A.at(n);
    sum += A.at(n);
  }

  int Q;
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  for (n = 0; n < Q; n++) {
    cin >> B.at(n);
    cin >> C.at(n);
  }

  for (n = 0; n < Q; n++) {
    int b = B.at(n);
    int c = C.at(n);
    int diff = c - b;
    for (int m = 0; m < A.size(); m++) {
      if (A.at(m) == b) {
        A.at(m) = c;
        sum += diff;
      }
    }
    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> vec(n + 1);
  vec[0] = 0;
  vector<int> a(100001, 0);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> vec[i];
    a[vec[i]]++;
    sum += vec[i];
  }

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    vector<int> totori(2);
    cin >> totori[0] >> totori[1];
    sum = sum + a[totori[0]] * (totori[1] - totori[0]);
    a[totori[1]] += a[totori[0]];
    a[totori[0]] = 0;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

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

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    vector<int> totori(2);
    cin >> totori[0] >> totori[1];
    sum = sum + a[totori[0]] * (totori[1] - totori[0]);
    a[totori[1]] += a[totori[0]];
    a[totori[0]] = 0;
    cout << sum << endl;
  }
}
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 vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  map<ll, ll> a;
  ll s = 0;
  rep(i, n) {
    int x;
    cin >> x;
    ++a[x];
  }
  for (auto p : a) {
    s += p.first * p.second;
  }
  int q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  rep(i, q) cin >> b[i] >> c[i];
  rep(i, q) {
    s = s - a[b[i]] * (b[i] - c[i]);
    cout << s << "\\n";
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
  }
  return 0;
}

Error: 0

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 vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n);
  ll s = 0;
  rep(i, n) {
    cin >> a[i];
    s += a[i];
  }
  int q;
  cin >> q;
  vector<ll> b(n);
  vector<ll> c(n);
  rep(i, q) cin >> b[i] >> c[i];
  ll dp[10010];
  rep(i, 10010) dp[i] = 0;
  rep(i, n)++ dp[a[i]];
  rep(i, q) {
    s = s - dp[b[i]] * (b[i] - c[i]);
    cout << s << "\\n";
    dp[c[i]] += dp[b[i]];
    dp[b[i]] = 0;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define int long long
#define mk(arr, n, type) type *arr = new type[n];
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
int32_t main() {
  int n;
  cin >> n;
  map<int, int> m;
  int *arr = new int[n];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    m[arr[i]]++;
  }
  int q;
  cin >> q;
  for (int z = 0; z < q; z++) {
    int b, c;
    cin >> b >> c;
    int temp = m[b];
    sum = sum + (c - b) * temp;
    m[b] = 0;
    m[c] = m[c] + temp;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define mk(arr, n, type) type *arr = new type[n];
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // map<int, int>m;
  int n;
  cin >> n;
  vector<int> v;
  int *arr = new int[n];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    // 		cin >> arr[i];
    sum += temp;
    v.push_back(temp);
  }
  int q;
  cin >> q;
  for (int z = 0; z < q; z++) {
    int b, c;
    cin >> b >> c;
    auto it = find(v.begin(), v.end(), b);
    while (it != v.end()) {
      sum = sum - *it + c;
      *it = c;
      it = find(v.begin(), v.end(), b);
    }

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

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

int a[100001];

int main() {
  int n, q;

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int q;
  cin >> q;
  while (q-- > 0) {
    ll b, c;
    cin >> b >> c;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] == b) {
        v[i] = c;
      }
      sum += v[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A;
  A.resize(N);
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ans += (long long)A[i];
  }
  int Q;
  cin >> Q;
  vector<int> B, C;
  B.resize(Q);
  C.resize(Q);
  for (int i = 0; i < Q; i++)
    cin >> B[i] >> C[i];

  map<int, int> m;
  for (int i = 0; i < A.size(); i++)
    m[A[i]]++;

  for (int i = 0; i < B.size(); i++) {
    auto itr = m.find(B[i]);
    // B[i]が存在すれば
    if (itr != m.end()) {
      int add = itr->second;
      m[C[i]] += add;
      m.erase(itr);

      ans += (long long)((C[i] - B[i]) * add);
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A;
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int Q;
  cin >> Q;
  vector<int> B, C;
  B.resize(Q);
  C.resize(Q);
  for (int i = 0; i < Q; i++)
    cin >> B[i] >> C[i];

  map<int, int> m;
  for (int i = 0; i < A.size(); i++)
    m[A[i]]++;

  for (int i = 0; i < B.size(); i++) {
    auto itr = m.find(B[i]);
    // B[i]が存在すれば
    if (itr != m.end()) {
      m[C[i]] += itr->second;
      m.erase(itr);
    }

    long long ans = 0;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      ans += ((long long)itr->first * (long long)itr->second);
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define ll long long
#define MAX 100000
#define MAX1 10000
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

int freq[MAX + 5];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  int n, i, x, y, q;
  ll sum = 0;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> x;
    freq[x]++;
    sum += x;
  }
  cin >> q;
  while (q-- > 00) {
    cin >> x >> y;
    sum -= 1LL * freq[x] * x;
    sum -= 1LL * freq[y] * y;
    freq[y] += freq[x];
    freq[x] = 0;
    sum += 1LL * freq[y] * y;
    cout << sum << "\\n";
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define ll long long
#define MAX 1000
#define MAX1 10000
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

int freq[MAX + 5];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  int n, i, x, y, q;
  ll sum = 0;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> x;
    freq[x]++;
    sum += x;
  }
  cin >> q;
  while (q-- > 00) {
    cin >> x >> y;
    sum -= 1LL * freq[x] * x;
    sum -= 1LL * freq[y] * y;
    freq[y] += freq[x];
    freq[x] = 0;
    sum += 1LL * freq[y];
    cout << sum << "\\n";
  }

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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<ll> A(n), C(100010);
  ll sum = 0;
  rep(i, n) {
    cin >> A[i];
    C[A[i]]++;
    sum += A[i];
  }
  int q;
  cin >> q;
  vector<ll> res;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    sum += (c - b) * C[b];
    res.push_back(sum);
    C[c] += C[b];
    C[b] = 0;
  }
  for (auto num : res)
    cout << num << ln;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<ll> A(n), C(n + 10);
  ll sum = 0;
  rep(i, n) {
    cin >> A[i];
    C[A[i]]++;
    sum += A[i];
  }
  int q;
  cin >> q;
  vector<ll> res;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * C[b];
    res.push_back(sum);
    C[c] += C[b];
    C[b] = 0;
  }
  for (auto num : res)
    cout << num << ln;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n, q, b, c, g;
  long long ans = 0;
  cin >> n;
  vector<long long> a(10000000);
  for (long long i = 1; i <= n; i++) {
    cin >> g;
    a[g]++;
  }
  cin >> q;
  for (long long j = 1; j <= a.size(); j++) {
    ans += a[j] * j;
  }

  for (long long i = 1; i <= q; i++) {
    cin >> b >> c;
    ans -= a[b] * b;
    ans -= a[c] * c;
    a[c] += a[b];
    a[b] = 0;
    ans += a[b] * b;
    ans += a[c] * c;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  long long n, q;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cin >> q;
  vector<long long> b(q + 1);
  vector<long long> c(q + 1);
  for (long long i = 1; i <= q; i++) {
    cin >> b[i] >> c[i];
  }

  long long ans = 0;
  for (long long i = 1; i <= q; i++) {
    for (long long j = 1; j <= n; j++) {
      if (a[j] == b[i])
        a[j] = c[i];
      ans += a[j];
    }

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

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long N, val, sum = 0;
  cin >> N;
  vector<long long> cnt(1e5 + 10, 0);

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

  long long Q, b, c;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> b;
    cin >> c;
    if (cnt[b] > 0) {
      sum += cnt[b] * (c - b);
      cnt[c] += cnt[b];
      cnt[b] = 0;
    }
    // vector<long long>::iterator it;
    // it = find (ival.begin(), ival.end(), b);

    // while(it != ival.end()){
    //*it = c;
    // it = find (ival.begin(), ival.end(), b);
    //}
    // cout<<accumulate(ival.begin(),ival.end(),0)<<'\\n';
    cout << sum << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <iostream>
// #include<vector>
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long N, val, sum = 0;
  cin >> N;
  // vector<long long> ival;
  multiset<long long> ival;
  for (int i = 0; i < N; i++) {
    cin >> val;
    // ival.push_back(val);
    ival.insert(val);
    sum += val;
  }

  long long Q, b, c;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> b;
    cin >> c;
    // vector<long long>::iterator it;
    // it = find (ival.begin(), ival.end(), b);
    if (ival.find(b) != ival.end()) {
      int count = ival.count(b);
      ival.erase(b);
      for (int i = 0; i < count; i++) {
        ival.insert(c);
      }
      sum = sum - count * b + count * c;
    }

    cout << sum << '\\n';

    // while(it != ival.end()){
    //   *it = c;
    //   it = find (ival.begin(), ival.end(), b);
    // }
    // cout<<accumulate(ival.begin(),ival.end(),0)<<'\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
int main() {
  fast;
  map<long long, long long> m;
  long long n;
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    sum += a;
    m[a]++;
  }
  long long q;
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    long long diff = m[b];
    m[b] = 0;
    m[c] += diff;
    sum -= diff * b;
    sum += diff * c;
    cout << sum << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
int main() {
  fast;
  multiset<long long> s;
  long long n;
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    sum += a;
    s.insert(a);
  }
  long long q;
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    int sz = (int)s.size();
    s.erase(b);
    int new_sz = (int)s.size();
    long long diff = sz - new_sz;
    sum -= diff * b;
    sum += diff * c;
    for (int i = 0; i < diff; i++)
      s.insert(c);
    cout << sum << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e9;
const double EPS = 1e-9;
const ll MOD = 1e9 + 7;

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  map<ll, ll> mp;
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    mp[A[i]]++;
    sum += A[i];
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
    mp[C[i]] += mp[B[i]];
    sum += C[i] * mp[B[i]];
    sum -= B[i] * mp[B[i]];
    mp[B[i]] = 0;
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e9;
const double EPS = 1e-9;
const ll MOD = 1e9 + 7;

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    mp[A[i]]++;
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    ll sum = 0;
    for (auto &e : mp)
      sum += e.first * e.second;
    cout << sum << endl;
  }

  return 0;
}