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

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

long long int n, q;

void solve() {
  cin >> n;
  long long int sum = 0;
  vector<long long int> freq(100069, 0);
  for (long long int i = 0, inp; i < n; ++i) {
    cin >> inp;
    freq[inp]++;
    sum += inp;
  }
  cin >> q;
  while (q--) {
    long long int b, c;
    cin >> b >> c;
    sum -= freq[b] * b;
    sum += freq[b] * c;
    freq[c] += freq[b];
    freq[b] = 0;
    cout << sum << endl;
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  // int t; cin >> t;
  // while(t--)
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/*
         ,--"""",--.__,---[],-------._
       ,"   __,'            \\         \\--""""""==;-
     ," _,-"  "/---.___     \\       ___\\   ,-'',"
    /,-'      / ;. ,.--'-.__\\  _,-"" ,| `,'   /
   /``""""-._/,-|:\\       []\\,' ```-/:;-. `. /
             `  ;:::      ||       /:,;  `-.\\
                =.,'__,---||-.____',.=
                =(:\\_     ||__    ):)=
               ,"::::`----||::`--':::"._
             ,':::::::::::||::::::::::::'.
    .__     ;:::.-.:::::__||___:::::.-.:::\\     __,
       """-;:::( O )::::>_|| _<::::( O )::::-"""
   =======;:::::`-`:::::::||':::::::`-`:::::\\=======
    ,--"";:::_____________||______________::::""----.          , ,
         ; ::`._(    |    |||     |   )_,'::::\\_,,,,,,,,,,____/,'_,
       ,;    :::`--._|____[]|_____|_.-'::::::::::::::::::::::::);_
      ;/ /      :::::::::,||,:::::::::::::::::::::::::::::::::::/
     /; ``''''----------/,'/,__,,,,,____:::::::::::::::::::::,"
     ;/                :);/|_;| ,--.. . ```-.:::::::::::::_,"
    /;                :::):__,'//""\\\\. ,--.. \\:::,:::::_,"
   ;/              :::::/ . . . . . . //""\\\\. \\::":__,"
   ;/          :::::::,' . . . . . . . . . . .:`::\\
   ';      :::::::__,'. ,--.. . .,--. . . . . .:`::`
   ';   __,..--'''-. . //""\\\\. .//""\\\\ . ,--.. :`:::`
   ;    /  \\\\ .//""\\\\ . . . . . . . . . //""\\\\. :`::`
   ;   /       . . . . . . . . . . . . . . . . .:`::`
   ;   (          . . . . . . . . . . . . . . . ;:::`
   ,:  ;,            . . . . . . . . . . . . . ;':::`
   ,:  ;,             . . . . . . . . . . . . .;`:::
   ,:   ;,             . . . . . . . . . . . . ;`::;`
    ,:  ;             . . . . . . . . . . . . ;':::;`
     :   ;             . . . . . . . . . . . ,':::;
      :   '.          . . . . . . . .. . . .,':::;`
       :    `.       . . . . . . . . . . . ;::::;`
        '.    `-.   . . . . . . . . . . ,-'::::;
          `:_    ``--..___________..--'':::::;'`
             `._::,.:,.:,:_ctr_:,:,.::,.:_;'`
________________`"\\/"\\/\\/'""""`\\/"\\/""\\/"____________________________
*/
#include <bits/stdc++.h>
using namespace std;

#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)

using ll = long long int;
using pii = pair<int, int>;
using pid = pair<int, double>;
using pLL = pair<ll, ll>;
using pll = pair<long int, long int>;
using pis = pair<int, string>;
using vL = vector<ll>;
using vi = vector<int>;
using vs = vector<string>;

#define ff first
#define ss second
#define pb push_back
#define loope(i, n) for (int i = 1; i <= n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define kaj_sesh return 0
#define ferot_ja return
const double pi = acos(-1);

// debugger
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// debugger

bool comp(const pid &a, const pid &b) {
  return (a.ss > b.ss) or (a.ss == b.ss && a.ff < b.ff);
}

void solve() {
  int n, q;
  cin >> n;
  vi a(n);
  for (auto &it : a)
    cin >> it;
  cin >> q;
  while (q--) {
    int b, c, sum = 0;
    cin >> b >> c;
    loop(i, n) {
      if (a[i] == b)
        a[i] = c;
      sum += a[i];
    }
    cout << sum << endl;
  }
}

signed main() {
  FASTIO;
#ifdef LOCAL
  freopen("in", "r", stdin);
  freopen("deeptoh", "w", stdout);
#endif

  /*int ts;
    cin >> ts;
    while(ts--) */
  solve();
  kaj_sesh;
}