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

// Bismillahir-Rahmanir-Rahim
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int llu;
#define fst                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define f1(i, n) for (i = 1; i <= n; i++)
#define f(i, n) for (i = 0; i < n; i++)
#define Max 100009
using namespace std;
int main() {
  ll q, n, t, i, s = 0, r, p = 0, j, k, l, f;
  cin >> n;
  ll a[n], c[Max] = {0};
  f(i, n) {
    cin >> a[i];
    s += a[i];
    c[a[i]]++;
  }
  r = s;
  cin >> q;
  f(i, q) {
    cin >> l >> f;
    s = s + c[l] * f - c[l] * l;
    c[f] += c[l];
    c[l] = 0;
    cout << s << endl;
  }
}


Error: TLE

Fixed submission:

// Bismillahir-Rahmanir-Rahim
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int llu;
#define fst                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define f1(i, n) for (i = 1; i <= n; i++)
#define f(i, n) for (i = 0; i < n; i++)
#define Max 100009
using namespace std;
int main() {
  ll q, n, t, i, s, r, p = 0, j, k, l, f;
  cin >> n;
  ll a[n];
  f(i, n) cin >> a[i];
  cin >> q;
  f(i, q) {
    s = 0;
    cin >> l >> f;
    f(j, n) {
      if (a[j] == l) {
        a[j] = f;
        s += f;
      } else
        s += a[j];
    }
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, i;
  cin >> n;
  ll a[n];
  ll sum = 0;
  vector<ll> v;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll q;
  cin >> q;
  ll b, c;
  unordered_map<ll, ll> hm;
  for (ll j = 0; j < n; j++) {
    hm[a[j]]++;
  }

  for (i = 0; i < q; i++) {
    cin >> b >> c;
    sum = sum + hm[b] * (c - b);
    hm[c] += hm[b];
    hm[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, i;
  cin >> n;
  ll a[n];
  ll sum = 0;
  vector<ll> v;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll q;
  cin >> q;
  ll b, c;
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    unordered_map<ll, ll> hm;
    for (ll j = 0; j < n; j++) {
      hm[a[j]]++;
    }

    sum = sum + hm[b] * (c - b);
    replace(a, a + n, b, c);
    cout << sum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vstr = vector<string>;

// 　定数, 変数 *********************************************
const ll MOD = 1e9 + 7;
const ll INF = 1ll << 60;

int N, Q;
// *********************************************************
int main() {

  cin >> N;

  map<ll, ll> mp;
  rep(i, N) {
    ll a;
    cin >> a;
    ++mp[a];
  }

  map<ll, ll> mpseki;
  ll sum = 0;
  for (auto str = mp.begin(); str != mp.end(); ++str) {
    mpseki[str->first] = str->first * str->second;
    sum += mpseki[str->first];
  }

  cin >> Q;
  vll B, C;
  rep(i, Q) {
    int b, c;
    cin >> b >> c;
    B.emplace_back(b);
    C.emplace_back(c);
  }

  rep(i, Q) {

    sum += C.at(i) * mp[B.at(i)];
    sum -= B.at(i) * mp[B.at(i)];
    mp[C.at(i)] += mp[B.at(i)];

    // mpseki[C.at(i)] = C.at(i) * mp[C.at(i)];

    mp[B.at(i)] = 0;

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vstr = vector<string>;

// 　定数, 変数 *********************************************
const ll MOD = 1e9 + 7;
const ll INF = 1ll << 60;

int N, Q;
// *********************************************************
int main() {

  cin >> N;

  map<ll, ll> mp;
  rep(i, N) {
    ll a;
    cin >> a;
    ++mp[a];
  }

  map<ll, ll> mpseki;
  for (auto str = mp.begin(); str != mp.end(); ++str) {
    mpseki[str->first] = str->first * str->second;
  }

  cin >> Q;
  vll B, C;
  rep(i, Q) {
    int b, c;
    cin >> b >> c;
    B.emplace_back(b);
    C.emplace_back(c);
  }

  rep(i, Q) {

    mp[C.at(i)] += mp[B.at(i)];
    mp[B.at(i)] = 0;

    mpseki[C.at(i)] = C.at(i) * mp[C.at(i)];
    mpseki[B.at(i)] = 0;
    ll sum = 0;
    for (auto str = mpseki.begin(); str != mpseki.end(); ++str) {
      sum += str->second;
    }
    cout << sum << endl;
  }

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

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
#define PI 3.14159265358979
#define MOD 1000000007 // = 10^9 + 7

using namespace std;
using ll = long long;

void solve() {
  int n, input;
  cin >> n;
  vector<int> a(100001, 0);
  for (int i = 0; i < n; ++i) {
    cin >> input;
    a[input]++;
  }

  ll ans = 0;
  for (int i = 0; i <= 100001; ++i) {
    ans += a[i] * i;
  }

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

    a[c] += a[b];
    a[b] = 0;
  }
}

int main() {
  fastio;
  solve();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
#define PI 3.14159265358979
#define MOD 1000000007 // = 10^9 + 7

using namespace std;
using ll = long long;

void solve() {
  int n, input;
  cin >> n;
  vector<int> a(10001, 0);
  for (int i = 0; i < n; ++i) {
    cin >> input;
    a[input]++;
  }

  ll ans = 0;
  for (int i = 0; i <= 10001; ++i) {
    ans += a[i] * i;
  }

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

    a[c] += a[b];
    a[b] = 0;
  }
}

int main() {
  fastio;
  solve();

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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
// const ll INF = 1LL << 30;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

int main() {
  ll n;
  cin >> n;
  Vl a(n);
  set<ll> now;
  map<ll, ll> cnt;
  rep(i, 0, n) {
    cin >> a[i];
    now.insert(a[i]);
    cnt[a[i]]++;
  }
  ll q;
  cin >> q;

  ll ans = 0;
  for (auto it : now) {
    ans += cnt[it] * it;
  }

  Vl rec;

  rep(i, 0, q) {
    ll b, c;
    cin >> b >> c;
    ans += (c - b) * cnt[b];

    if (cnt[c] != 0)
      cnt[c] += cnt[b];
    else
      cnt[c] = cnt[b];
    cnt[b] = 0;

    rec.push_back(ans);
  }

  rep(i, 0, sz(rec)) co(rec[i]);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
// const ll INF = 1LL << 30;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

int main() {
  ll n;
  cin >> n;
  Vl a(n);
  set<ll> now;
  map<ll, ll> cnt;
  rep(i, 0, n) {
    cin >> a[i];
    now.insert(a[i]);
    cnt[a[i]]++;
  }
  ll q;
  cin >> q;

  rep(i, 0, q) {
    ll b, c;
    cin >> b >> c;
    if (cnt[c] != 0)
      cnt[c] += cnt[b];
    else
      cnt[c] = cnt[b];
    cnt[b] = 0;
    now.erase(b);
    now.insert(c);
    ll rec = 0;
    for (auto it : now) {
      // cout << it << " ";
      rec += cnt[it] * it;
    }
    // coel;
    co(rec);
  }

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

#include <bits/stdc++.h>

using namespace std;
#define DBG(x)                                                                 \\
  (void)(cout << "L" << __LINE__ << ": " << #x << " = " << (x) << '\\n')
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
typedef long long ll;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const long double PI = (acos(-1));
const long double EPS = 0.0000000001;
const int INF = 0x3f3f3f3f;
const int maxn = 1100000;
ll a[maxn];
ll cnt[maxn];
void run_case() {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += 1ll * a[i];
    cnt[a[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - cnt[b] * b + cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << "\\n";
  }
  // cout << sum << "\\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(12);
  run_case();
  cout.flush();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    mp[x]++;
  }
  ll quary;
  cin >> quary;
  for (int i = 0; i < quary; i++) {
    ll x, y;
    cin >> x >> y;
    mp[y] = mp[y] + mp[x];
    mp.erase(x);
    ll ans = 0;
    for (auto it = mp.begin(); it != mp.end(); ++it) {
      ans = ans + ((it->first) * (it->second));
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll jum = 0, n;
  map<ll, ll> m;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    jum += x;
    m[x]++;
  }
  ll q, a, b;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    cout << jum - a * m[a] + b * m[a] << endl;
    jum = jum - a * m[a] + b * m[a];
    m[b] += m[a];
    m[a] = 0;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, i, r, s = 0, b, c, j;
  cin >> n;
  long long int a[n + 10];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> b >> c;
    for (j = 0; j < n; j++) {
      if (a[j] == b) {
        s = ((s - b) + c);
        a[j] = c;
      }
    }
    cout << s << 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;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;

  ll p[100005] = {0};
  for (ll i = 0; i < n; i++)
    p[a[i]]++;
  ll sum = 0;
  for (ll i = 0; i < 100005; i++)
    sum += i * p[i];

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


Error: TLE

Fixed submission:

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

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

  int p[100005] = {0};
  for (int i = 0; i < n; i++)
    p[a[i]]++;
  ll sum = 0;
  for (int i = 0; i < 100005; i++)
    sum += i * p[i];

  for (int i = 1; i <= q; i++) {
    int b, c;
    cin >> b >> c;
    // ll ans = sum - p[b]*b + p[b]*c;
    p[c] = p[b] + p[c];
    p[b] = 0;
    ll sum = 0;
    for (int i = 0; i < 100005; i++)
      sum += i * p[i];
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define ll long long
using namespace std;

ll nums[100005];

int main() {
  ll sum = 0;
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; ++i) {
    ll temp;
    cin >> temp;
    nums[temp]++;
    sum += temp;
  }

  ll q;
  cin >> q;
  for (ll i = 1; i <= q; ++i) {
    ll b, c;
    cin >> b >> c;
    ll temp = nums[b];
    nums[b] = 0;
    nums[c] += temp;

    sum -= temp * b;
    sum += temp * c;
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <iterator>
#include <set>
#define ll long long
using namespace std;

multiset<int> nums;

void replace(int b, int c) {
  multiset<int>::iterator it = nums.find(b);
  while (it != nums.end()) {
    nums.erase(it);
    nums.emplace(c);
    it = nums.find(b);
  }
}

ll calcSum() {
  ll sum = 0;
  for (auto it : nums)
    sum += it;
  return sum;
}

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

  for (int i = 1; i <= n; ++i) {
    int temp;
    cin >> temp;
    nums.emplace(temp);
  }

  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int b, c;
    cin >> b >> c;

    replace(b, c);
    cout << calcSum() << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 200001
#define ll long long int
#define ldb long double
#define endl "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL));

using namespace std;

typedef pair<ll, ll> pp;
typedef vector<ll> vi;

void solve(ll test) {

  ll n;
  cin >> n;
  map<ll, ll> mp;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    sum += x;
    mp[x]++;
  }

  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    sum = sum + (y - x) * mp[x];
    mp[y] += mp[x];
    mp[x] = 0;
    cout << sum << endl;
  }
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
    // cout << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 200001
#define ll long long int
#define ldb long double
#define endl "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL));

using namespace std;

typedef pair<ll, ll> pp;
typedef vector<ll> vi;

void solve(ll test) {

  ll n;
  cin >> n;
  multiset<ll> s;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    sum += x;
    s.insert(x);
  }

  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    sum = sum + (y - x) * s.count(x);
    for (ll i = 0; i < s.count(x); i++)
      s.insert(y);
    s.erase(x);
    cout << sum << endl;
  }
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
    // cout << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// keep coding
typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\\n"
#define f0(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)
#define sd(val) scanf("%d", &val)
#define ss(val) scanf("%s", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\\n", val)
#define all(v) v.begin(), v.end()
#define pb push_back
#define MP make_pair
#define FF first
#define SS second
#define int long long
#define MOD 1000000007
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

signed main() {
  // OJ;
  FIO;
  int n;
  cin >> n;
  map<int, int> mp;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    mp[x]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    int k = mp[x];
    mp[x] = 0;
    mp[y] += k;
    sum += k * (y - x);
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

// keep coding
typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\\n"
#define f0(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)
#define sd(val) scanf("%d", &val)
#define ss(val) scanf("%s", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\\n", val)
#define all(v) v.begin(), v.end()
#define pb push_back
#define MP make_pair
#define FF first
#define SS second
#define int long long
#define MOD 1000000007
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

signed main() {
  // OJ;
  FIO;
  int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (arr[i] == b) {
        arr[i] *= c;
        c = arr[i] / b;
        arr[i] /= b;
      }
      sum += arr[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1000000000000000LL
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vll cnt(SIZE);
  ll tot = 0;
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  rep(i, SIZE) tot += i * cnt[i];
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    tot -= b * cnt[b];
    tot -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    tot += b * cnt[b];
    tot += c * cnt[c];
    cout << tot << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1000000000000000LL
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vll cnt(n + 1);
  ll tot = 0;
  rep1(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  rep1(i, n) tot += i * cnt[i];
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int b, c;
    cin >> b >> c;
    cnt[c] += cnt[b];
    tot -= cnt[b];
    tot -= cnt[c];
    cnt[b] = 0;
    tot += b * cnt[b];
    tot += c * cnt[c];
    cout << tot << endl;
  }
  return 0;
}