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

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

#define ll long long

#define forn(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define pb push_back
#define sz(a) (int)a.size()

#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fastio;
  int n;
  cin >> n;
  map<int, int> m;
  ll s = 0;
  int a[n];
  forn(i, n) {
    cin >> a[i];
    m[a[i]]++;
    s += a[i];
  }
  int t;
  cin >> t;
  while (t--) {
    int a, b;
    cin >> a >> b;
    ll x = m[a];
    s -= x * a;
    s += x * b;
    m[a] -= x;
    m[b] += x;
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:


/*****************************************
****       Solution by SlavicG        ****
*****************************************/

#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define forn(i, n) for (int i = 0; i < n; ++i)
#define MX 10000000001
#define pow2(a) ((a) * (a))
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define FAST                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int longestIncreasingSubsequenceLenghtDP(int a[], int n) {
  int dp[n], ans = 0;
  for (int k = 0; k < n; k++) {
    dp[k] = 1;
    for (int i = 0; i < k; i++) {
      if (a[i] <= a[k]) {
        dp[k] = max(dp[k], dp[i] + 1);
        ans = max(dp[k], ans);
      }
    }
  }
  return ans;
}
int result[1000] = {0};
int factDP(int n) {
  if (n >= 0) {
    result[0] = 1;
    for (int i = 1; i <= n; ++i) {
      result[i] = i * result[i - 1];
    }
    return result[n];
  }
}
bool checkPrime(int n) {
  if (n < 2)
    return false;
  for (int x = 2; x * x <= n; x++) {
    if (n % x == 0)
      return false;
  }
  return true;
}
vector<int> primeFactors(int n) {
  vector<int> f;
  for (int x = 2; x * x <= n; x++) {
    while (n % x == 0) {
      f.push_back(x);
      n /= x;
    }
  }
  if (n > 1)
    f.push_back(n);
  return f;
}
int binary_Search_Classic(int n, int arr[], int target) {
  int l, r, mid;
  l = 0;
  r = n - 1;
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (arr[mid] == target) {
      return mid;
    }
    if (arr[mid] < target) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return -1;
}
int binary_Search_First_Greater(int n, int arr[], int target) {
  int l, r, ans, mid;
  l = 0;
  r = n - 1;
  ans = -1;
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (arr[mid] > target) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  return ans;
}
int countDistinctCharsString(string s) {
  unordered_map<char, int> m;
  for (int i = 0; i < s.length(); i++) {
    m[s[i]]++;
  }
  return m.size();
}
int main() {
  int n;
  cin >> n;
  int a[n];
  forn(i, n) cin >> a[i];
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int s = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
      }
      s += a[i];
    }
    cout << s << endl;
  }
  return 0;
}