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

#include <bits/stdc++.h>
using namespace std;
#define author "Sihare0riginals"
#define ss string
#define lli long long int
#define ld long double
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define I(a, i) memset(a, i, sizeof(a));
#define pb push_back
#define mp make_pair
#define L(i, start, end) for (lli i = start; i <= end; i++)
#define R(i, start, end) for (lli i = start; i >= end; i--)
#define ip(n)                                                                  \\
  cin >> n;                                                                    \\
  lli ip[n + 1];                                                               \\
  L(i, 1, n) cin >> ip[i];
const lli LINF = 1e18, mod = 1e9 + 7;
void Bl0ck_M0mb0();

signed main() {
  Bl0ck_M0mb0();
  lli t = 1;
  // cin>>t;
  while (t--) {
    lli n;
    cin >> n;
    lli A[n + 1], sum = 0;
    map<lli, lli> ip;
    L(i, 1, n) {
      cin >> A[i];
      sum += A[i];
      ip[A[i]]++;
    }
    lli q;
    cin >> q;
    L(i, 1, q) {
      lli bi, ci;
      cin >> bi >> ci;
      lli sz = ip[bi];
      ip[ci] += ip[bi];
      ip[bi] = 0;
      sum -= sz * bi;
      sum += sz * ci;
      cout << sum << endl;
    }
  }
  return 0;
}

void Bl0ck_M0mb0() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
}
/*
#define dbg1D(i,start,end,arr) for(lli i=start;i<=end;i++) cout<<i<<"th :
"<<arr[i]<<endl; #define dbg2D(i,j,s1,e1,s2,e2,arr) for(lli
i=s1;i<=e1;i++){cout<<i<<"th :";for(lli j=s2;j<=e2;j++)cout<<arr[i][j]<<"
";cout<<endl;} #define M map<lli,lli> \\n #define sz(s) s.size()-1 #define P
pair<lli,lli> #define V vector<lli> lli dx[4]={-1, 0, 0, +1}; lli dy[4]={0, -1,
+1, 0}; lli power(lli a,lli b,lli m){ if(b==0) return 1; if(b==1) return a%m;
        lli t=power(a,b/2,m); t=(t*t)%m; if(b&1) t=(t*a)%m; return t;}
lli modInverse(lli a, lli m) { return power(a, m-2, m); } */

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define author "Sihare0riginals"
#define ss string
#define lli long long int
#define ld long double
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define I(a, i) memset(a, i, sizeof(a));
#define pb push_back
#define mp make_pair
#define L(i, start, end) for (lli i = start; i <= end; i++)
#define R(i, start, end) for (lli i = start; i >= end; i--)
#define ip(n)                                                                  \\
  cin >> n;                                                                    \\
  lli ip[n + 1];                                                               \\
  L(i, 1, n) cin >> ip[i];
const lli LINF = 1e18, mod = 1e9 + 7;
void Bl0ck_M0mb0();

signed main() {
  Bl0ck_M0mb0();
  lli t = 1;
  // cin>>t;
  while (t--) {
    lli n;
    cin >> n;
    lli A[n + 1], sum = 0;
    map<lli, list<lli>> ip;
    L(i, 1, n) {
      cin >> A[i];
      sum += A[i];
      ip[A[i]].pb(i);
    }
    lli q;
    cin >> q;
    L(i, 1, q) {
      lli bi, ci;
      cin >> bi >> ci;
      lli sz = ip[bi].size();
      ip[ci].merge(ip[bi]);
      ip[bi].clear();
      sum -= sz * bi;
      sum += sz * ci;
      cout << sum << endl;
    }
  }
  return 0;
}

void Bl0ck_M0mb0() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
}
/*
#define dbg1D(i,start,end,arr) for(lli i=start;i<=end;i++) cout<<i<<"th :
"<<arr[i]<<endl; #define dbg2D(i,j,s1,e1,s2,e2,arr) for(lli
i=s1;i<=e1;i++){cout<<i<<"th :";for(lli j=s2;j<=e2;j++)cout<<arr[i][j]<<"
";cout<<endl;} #define M map<lli,lli> \\n #define sz(s) s.size()-1 #define P
pair<lli,lli> #define V vector<lli> lli dx[4]={-1, 0, 0, +1}; lli dy[4]={0, -1,
+1, 0}; lli power(lli a,lli b,lli m){ if(b==0) return 1; if(b==1) return a%m;
        lli t=power(a,b/2,m); t=(t*t)%m; if(b&1) t=(t*a)%m; return t;}
lli modInverse(lli a, lli m) { return power(a, m-2, m); } */