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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, j, n) for (int i = j; i < n; i++)
#define rrep(i, j, n) for (int i = j - 1; i >= n; i--)
#define prec(n) fixed << setprecision(n)
#define print_array(v)                                                         \\
  rep(__k, v.size()) {                                                         \\
    cout << v[__k];                                                            \\
    if (__k != v.size() - 1)                                                   \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define fi first
#define se second
#define endl "\\n"
using namespace std;

// constexpr int inf = 2147483647;
// constexpr int64_t inf64 = 9223372036854775807;
// constexpr int mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  int64_t res = 0;
  map<int64_t, int> mp;
  rep(i, n) {
    int64_t k;
    cin >> k;
    res += k;
    mp[k]++;
  }

  int q;
  cin >> q;
  // cout << res << endl;
  rep(i, q) {
    int64_t b, c;
    cin >> b >> c;
    /*for (auto p : mp) {
      cout << i+1 << " ";
      cout << p.fi << ":" << p.se << endl;
    }*/
    res = res - b * mp[b] + c * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
    cout << res << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, j, n) for (int i = j; i < n; i++)
#define rrep(i, j, n) for (int i = j - 1; i >= n; i--)
#define prec(n) fixed << setprecision(n)
#define print_array(v)                                                         \\
  rep(__k, v.size()) {                                                         \\
    cout << v[__k];                                                            \\
    if (__k != v.size() - 1)                                                   \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define fi first
#define se second
#define endl "\\n"
using namespace std;

// constexpr int inf = 2147483647;
// constexpr int64_t inf64 = 9223372036854775807;
// constexpr int mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int64_t> a(100005);
  rep(i, n) {
    int k;
    cin >> k;
    a[k]++;
  }

  int q;
  cin >> q;
  rep(i, q) {
    int64_t b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    int64_t res = 0;
    rep(j, 100005) res += a[j] * j;
    cout << res << endl;
  }
  return 0;
}