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

#include <bits/stdc++.h>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long int
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define loop(i, L, R) for (int i = L; i < R; ++i)
#define looprev(i, L, R) for (int i = L; i > R; --i)
#define MAX(x, y) (x > y ? x : y)
#define MIN(x, y) (x < y ? x : y)
#define wh(t) while (t--)
#define pii pair<int, int>
#define ppi pair<int, pii>
#define pll pair<ll, ll>
#define ppl pair<ll, pll>
#define vii vector<int>
#define vvi vector<vii>
#define vl vector<ll>
#define vvl vector<vl>
#define maxHi priority_queue<int>
#define maxHl priority_queue<ll>
#define minHi priority_queue<int, vector<int>, greater<int>>
#define minHl priority_queue<ll, vector<ll>, greater<ll>>
#define mod 1000000007
#define inf 1000000000000000001
#define PI 3.1415926535897932384626
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define IOS ios_base::sync_with_stdio(false)
#define endl "\\n"
using namespace std;

/*  AH SH*T,  HERE WE GO AGAIN.  */

void solve() {
  ll i, j, n, m, q, a, b;

  sl(n);

  std::vector<ll> v(n);

  unordered_map<ll, ll> umap;

  ll sum = 0;

  loop(i, 0, n) {

    sl(v[i]);

    umap[v[i]]++;

    sum += v[i];
  }

  sl(q);

  while (q--) {

    sl(a);

    sl(b);

    sum = sum - a * umap[a] + b * umap[a];

    umap[b] += umap[a];

    umap[a] = 0;

    cout << sum << endl;
  }
}

int32_t main() {

  IOS;
  cin.tie(NULL);

  int T = 1;

  wh(T) { solve(); }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long int
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define loop(i, L, R) for (int i = L; i < R; ++i)
#define looprev(i, L, R) for (int i = L; i > R; --i)
#define MAX(x, y) (x > y ? x : y)
#define MIN(x, y) (x < y ? x : y)
#define wh(t) while (t--)
#define pii pair<int, int>
#define ppi pair<int, pii>
#define pll pair<ll, ll>
#define ppl pair<ll, pll>
#define vii vector<int>
#define vvi vector<vii>
#define vl vector<ll>
#define vvl vector<vl>
#define maxHi priority_queue<int>
#define maxHl priority_queue<ll>
#define minHi priority_queue<int, vector<int>, greater<int>>
#define minHl priority_queue<ll, vector<ll>, greater<ll>>
#define mod 1000000007
#define inf 1000000000000000001
#define PI 3.1415926535897932384626
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define IOS ios_base::sync_with_stdio(false)
#define endl "\\n"
using namespace std;

/*  AH SH*T,  HERE WE GO AGAIN.  */

void solve() {
  ll i, j, n, m, q, a, b;

  sl(n);

  std::vector<ll> v(n);

  ll sum = 0;

  loop(i, 0, n) {

    sl(v[i]);

    sum += v[i];
  }

  sl(q);

  while (q--) {

    sl(a);

    sl(b);

    for (ll i = 0; i < n; ++i) {

      if (v[i] == a) {
        v[i] = b;

        sum = sum - a + b;
      }
    }

    cout << sum << endl;
  }
}

int32_t main() {

  IOS;
  cin.tie(NULL);

  int T = 1;

  wh(T) { solve(); }
}