Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
    AABRA KA DABRA.. !!
*/
#include "bits/stdc++.h"
using namespace std;
typedef long double ld;
#define int long long
#define RAGE                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, n) for (i = 0; i < n; i++)
#define repv(i, k, n) for (int i = k; i <= n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define endl '\\n'
#define deb(x) cout << #x << " = " << x << endl
#define gcd __gcd
#define inf LLONG_MAX
#define minf LLONG_MIN
const int mod = (int)(1e9 + 7);

int power(int x, int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % mod;
  if (n % 2 == 0) {
    int y = power(x, n / 2) % mod;
    return (y * y) % mod;
  }
  if (n & 1) {
    int y = power(x, n - 1);
    return (x % mod * y % mod) % mod;
  }
  return 0;
}
bool is_power_2(int n) { return log2(n) == floor(log2(n)); }
bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}

int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int maxn = 100005;

/*
    ╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩╦═╩═╦═╩═╦═╩═╦═╩═╦═╩╦═╩═╦═╩═╦═╩═╦═╩═╦═╩╦═╩═╦═╩═╦
*/

void solve() {
  int n, i, j, k, m;
  cin >> n;
  int ar[n];
  map<int, int> mp;
  int sum = 0;
  rep(i, n) {
    cin >> ar[i];
    sum += ar[i];
    mp[ar[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    m = mp[a];
    mp[a] = 0;
    mp[b] += m;
    sum -= a * m;
    sum += b * m;
    cout << sum << endl;
  }
}
signed main() {
  RAGE;
  int t = 1;
  //  cin>>t;
  while (t--)
    solve();

  return 0;
}

Error: TLE

Fixed submission:

/*
    AABRA KA DABRA.. !!
*/
#include "bits/stdc++.h"
using namespace std;
typedef long double ld;
#define int long long
#define RAGE                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, n) for (i = 0; i < n; i++)
#define repv(i, k, n) for (int i = k; i <= n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define endl '\\n'
#define deb(x) cout << #x << " = " << x << endl
#define gcd __gcd
#define inf LLONG_MAX
#define minf LLONG_MIN
const int mod = (int)(1e9 + 7);

int power(int x, int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % mod;
  if (n % 2 == 0) {
    int y = power(x, n / 2) % mod;
    return (y * y) % mod;
  }
  if (n & 1) {
    int y = power(x, n - 1);
    return (x % mod * y % mod) % mod;
  }
  return 0;
}
bool is_power_2(int n) { return log2(n) == floor(log2(n)); }
bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}

int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int maxn = 100005;

/*
    ╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩╦═╩═╦═╩═╦═╩═╦═╩═╦═╩╦═╩═╦═╩═╦═╩═╦═╩═╦═╩╦═╩═╦═╩═╦
*/

void solve() {
  int n, i, j, k, m;
  cin >> n;
  int ar[n];
  map<int, vector<int>> mp;
  int sum = 0;
  rep(i, n) {
    cin >> ar[i];
    sum += ar[i];
    mp[ar[i]].pb(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    m = sz(mp[a]);
    while (sz(mp[a])) {
      mp[b].pb(mp[a].back());
      mp[a].pop_back();
    }

    sum -= a * m;
    sum += b * m;
    cout << sum << endl;
  }
}
signed main() {
  RAGE;

  int t = 1;
  //  cin>>t;
  while (t--)
    solve();
  return 0;
}