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

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

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"); }

#define int long long
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define endl '\\n'
int powmod(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

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...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

int string_to_int(string s) {
  int ans = 0;
  int p = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    ans += (s[i] - '0') * p;
    p *= 10;
  }
  return ans;
}

void t_c_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
  #endif*/

  int n;
  cin >> n;
  int arr[n];
  int sum = 0;
  int mx = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > mx) {
      mx = arr[i];
    }
    sum += arr[i];
  }
  int pre[100001];
  memset(pre, 0, sizeof pre);
  for (int i = 0; i < n; i++) {
    pre[arr[i] - 1]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int cnt = 0;
    int a, b;
    cin >> a >> b;
    cnt = pre[a - 1];
    sum -= cnt * a;
    sum += cnt * b;
    pre[a - 1] = 0;
    pre[b - 1] += cnt;
    cout << sum << endl;
  }
}

int32_t main() {
  t_c_c();
  return 0;
}


Error: TLE

Fixed submission:

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

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"); }

#define int long long
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define endl '\\n'
int powmod(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

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...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

int string_to_int(string s) {
  int ans = 0;
  int p = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    ans += (s[i] - '0') * p;
    p *= 10;
  }
  return ans;
}

void t_c_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
  #endif*/

  int n;
  cin >> n;
  int arr[n];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int cnt = 0;
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
      if (arr[i] == a) {
        cnt++;
        arr[i] = b;
      }
    }
    sum -= cnt * a;
    sum += cnt * b;
    cout << sum << endl;
  }
}

int32_t main() {
  t_c_c();
  return 0;
}