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

/*********************************************************************************\\
*  _________  _      _  ________  _       _       __       ________ *
* |___   ___|| |    | ||  ______|| |     | |     /  \\     |  ____  | *
*     | |    | |    | || |______ | |_____| |    / /\\ \\    | |____| | *
*     | |    | |    | ||______  ||  _____  |   / /__\\ \\   |  __  __| *
*     | |    | |____| | ______| || |     | |  / ______ \\  | |
\\ \\                *
*     |_|    |________||________||_|     |_| /_/      \\_\\ |_|
\\_\\               *
* *
*   Department of Computer Science & Engineering *
*   Student ID : 18CSE035 *
*   Bangabnadhu Sheikh Mujibur Rahman Science & Technology University,Gopalgonj.
*
* *
\\*********************************************************************************/

// Now Write to Code ___________________________

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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;

inline int Int() {
  int x;
  cin >> x;
  return x;
}
inline ll Long() {
  ll x;
  cin >> x;
  return x;
}
inline float Float() {
  float x;
  cin >> x;
  return x;
}
inline double Double() {
  double x;
  cin >> x;
  return x;
}
inline void Yes() { cout << "Yes" << endl; }
inline void No() { cout << "No" << endl; }
inline void YES() { cout << "YES" << endl; }
inline void NO() { cout << "NO" << endl; }

const int N = (int)2e5 + 5;
const int maxN = (int)1e6 + 6;
const ll Mod = (ll)1e9 + 7;
const int inf = (int)2e9;
const ll Inf = (ll)1e18;

#define T                                                                      \\
  int t, q;                                                                    \\
  cin >> t;                                                                    \\
  for (q = 1; q <= t; q++)
#define debug(x) cerr << #x << " = " << x << '\\n';
#define rep(i, b, e)                                                           \\
  for (__typeof(e) i = (b); i != (e + 1) - 2 * (b > e); i = i + 1 - 2 * (b > e))
#define Int Int()
#define Long Long()
#define Float Float()
#define Double Double()
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mem(a) memset(a, 0, sizeof a)
#define memn(a) memset(a, -1, sizeof a)
#define biday return 0
#define PI                                                                     \\
  3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342

ll X[100001];

int main() {
  ll N, Q, A, i, sum = 0;

  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> A;
    X[A]++;
    sum += A;
  }

  cin >> Q;
  ll B[Q], C[Q];
  for (i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
    sum -= (B[i] * X[B[i]]);
    sum += (C[i] * X[B[i]]);
    X[C[i]] += X[B[i]];
    X[B[i]] = 0;
    cout << sum << endl;
  }

  biday;
}

//...............BYE BYE................

Error: TLE

Fixed submission:

/*********************************************************************************\\
*  _________  _      _  ________  _       _       __       ________ *
* |___   ___|| |    | ||  ______|| |     | |     /  \\     |  ____  | *
*     | |    | |    | || |______ | |_____| |    / /\\ \\    | |____| | *
*     | |    | |    | ||______  ||  _____  |   / /__\\ \\   |  __  __| *
*     | |    | |____| | ______| || |     | |  / ______ \\  | |
\\ \\                *
*     |_|    |________||________||_|     |_| /_/      \\_\\ |_|
\\_\\               *
* *
*   Department of Computer Science & Engineering *
*   Student ID : 18CSE035 *
*   Bangabnadhu Sheikh Mujibur Rahman Science & Technology University,Gopalgonj.
*
* *
\\*********************************************************************************/

// Now Write to Code ___________________________

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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;

inline int Int() {
  int x;
  cin >> x;
  return x;
}
inline ll Long() {
  ll x;
  cin >> x;
  return x;
}
inline float Float() {
  float x;
  cin >> x;
  return x;
}
inline double Double() {
  double x;
  cin >> x;
  return x;
}
inline void Yes() { cout << "Yes" << endl; }
inline void No() { cout << "No" << endl; }
inline void YES() { cout << "YES" << endl; }
inline void NO() { cout << "NO" << endl; }

const int N = (int)2e5 + 5;
const int maxN = (int)1e6 + 6;
const ll Mod = (ll)1e9 + 7;
const int inf = (int)2e9;
const ll Inf = (ll)1e18;

#define T                                                                      \\
  int t, q;                                                                    \\
  cin >> t;                                                                    \\
  for (q = 1; q <= t; q++)
#define debug(x) cerr << #x << " = " << x << '\\n';
#define rep(i, b, e)                                                           \\
  for (__typeof(e) i = (b); i != (e + 1) - 2 * (b > e); i = i + 1 - 2 * (b > e))
#define Int Int()
#define Long Long()
#define Float Float()
#define Double Double()
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mem(a) memset(a, 0, sizeof a)
#define memn(a) memset(a, -1, sizeof a)
#define biday return 0
#define PI                                                                     \\
  3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342

int main() {
  ll N, Q, i, j, sum;

  cin >> N;
  ll A[N];
  for (i = 0; i < N; i++)
    cin >> A[i];

  cin >> Q;
  ll B[Q], C[Q];
  for (i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
    sum = 0;
    for (j = 0; j < N; j++) {
      if (A[j] == B[i])
        A[j] = C[i];
      sum += A[j];
    }
    cout << sum << endl;
  }

  biday;
}

//...............BYE BYE................