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

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
// 省略
#define ll long long
#define ld long double
#define Vi vector<int>
#define Vl vector<ll>
// for
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Repp(i, a, b) for (int i = a; i <= (int)(b); i++)
#define Repm(i, a, b) for (int i = a; i <= (int)(b); i— -)
// 入力
#define ini(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define inl(x)                                                                 \\
  ll x;                                                                        \\
  cin >> x;
#define ind(x)                                                                 \\
  double x;                                                                    \\
  cin >> x;
#define inc(x)                                                                 \\
  char x;                                                                      \\
  cin >> x;
#define ins(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
// 出力
#define co(x) cout << x;
#define ce cout << endl;
#define cs cout << “ “;
#define keta(x) cout << fixed << setprecision(x);
// 配列
#define Sort(x) sort(x.begin(), x.end()); // 小順
#define Rev(x) reverse(x.begin(), x.end());

int main() {
  ini(N) Vl As(100100, 0);
  rep(i, N) { ini(A) As[A]++; }

  ll ans = 0;
  rep(i, 100100) { ans += i * As[i]; }

  ini(Q) rep(i, Q) {
    inl(B) inl(C) ll keep;
    keep = As[B];
    As[B] = 0;
    ans -= keep * B;
    As[C] += keep;
    ans += keep * C;
    co(ans) ce
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
// 省略
#define ll long long
#define ld long double
#define Vi vector<int>
#define Vl vector<ll>
// for
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Repp(i, a, b) for (int i = a; i <= (int)(b); i++)
#define Repm(i, a, b) for (int i = a; i <= (int)(b); i— -)
// 入力
#define ini(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define inl(x)                                                                 \\
  ll x;                                                                        \\
  cin >> x;
#define ind(x)                                                                 \\
  double x;                                                                    \\
  cin >> x;
#define inc(x)                                                                 \\
  char x;                                                                      \\
  cin >> x;
#define ins(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
// 出力
#define co(x) cout << x;
#define ce cout << endl;
#define cs cout << “ “;
#define keta(x) cout << fixed << setprecision(x);
// 配列
#define Sort(x) sort(x.begin(), x.end()); // 小順
#define Rev(x) reverse(x.begin(), x.end());

int main() {
  inl(N) Vl As(100010, 0);
  repl(i, N) { inl(A) As[A]++; }
  inl(Q) repl(i, Q) {
    inl(B) inl(C) ll keep;
    keep = As[B];
    As[B] = 0;
    As[C] += keep;
    ll ans = 0;
    repl(i, 100010) { ans += i * As[i]; }
    co(ans) ce
  }
}