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

/*  TAHMID RAHMAN
    DAMIAN FOREVER
     MATH LOVER
    NEVER GIVE UP
*/
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long
#define pb push_back
#define fi first
#define se second
#define in insert
#define mp make_pair
#define GCD(a, b) __gcd(a, b);
#define endl "\\n"
#define FRU freopen("out.txt", "w", stdout)
#define FRO freopen("in.txt", "r", stdin)
#define INFLL 9223372036854775807
#define debug 0
#define MAXN 100001
// Don't hesitate to ask me if you don't understand my code.......Happy
// coding,Tahmid...;
ll a[MAXN] = {0};
int main() {
  fastio;
  ll n, i, t, sum = 0, k, co, f;
  cin >> n;
  vector<ll> v(n);
  for (i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
    a[v[i]]++;
  }
  cin >> t;
  vector<pair<ll, ll>> p(t);
  for (i = 0; i < t; i++) {
    ll x, y;
    cin >> x >> y;
    p[i] = mp(x, y);
  }
  //  cout<<sum<<endl;
  sort(v.begin(), v.end());
  for (i = 0; i < t; i++) {
    co = a[p[i].fi];
    // ll z,c;
    // z=lower_bound(v.begin(),v.end(),p[i].fi)-v.begin();
    // c=upper_bound(v.begin(),v.end(),p[i].fi)-v.begin();
    // cout<<c-z<<endl;
    a[p[i].se] += co;
    a[p[i].fi] = 0;
    sum -= co * p[i].fi;
    sum += co * p[i].se;
    cout << sum << endl;
    // cout<<a[2];
  }

  if (debug) {
    for (auto z : p) {
      cout << z.fi << " " << z.se << endl;
    }
  }
}


Error: TLE

Fixed submission:

/*  TAHMID RAHMAN
    DAMIAN FOREVER
     MATH LOVER
    NEVER GIVE UP
*/
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long
#define pb push_back
#define fi first
#define se second
#define in insert
#define mp make_pair
#define GCD(a, b) __gcd(a, b);
#define endl "\\n"
#define FRU freopen("out.txt", "w", stdout)
#define FRO freopen("in.txt", "r", stdin)
#define INFLL 9223372036854775807
#define debug 0
#define MAXN 100001
// Don't hesitate to ask me if you don't understand my code.......Happy
// coding,Tahmid...;
int main() {
  fastio;
  ll n, i, t, sum = 0, k, co, f;
  cin >> n;
  //  ll a[n+2]={0};
  vector<ll> v(n);
  for (i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
    // a[v[i]]++;
  }
  cin >> t;
  vector<pair<ll, ll>> p(t);
  for (i = 0; i < t; i++) {
    ll x, y;
    cin >> x >> y;
    p[i] = mp(x, y);
  }
  //  cout<<sum<<endl;
  sort(v.begin(), v.end());
  for (i = 0; i < t; i++) {
    // ll z,c;
    // z=lower_bound(v.begin(),v.end(),p[i].fi)-v.begin();
    // c=upper_bound(v.begin(),v.end(),p[i].fi)-v.begin();
    // cout<<c-z<<endl;
    co = count(v.begin(), v.end(), p[i].fi);
    f = 0;
    for (k = 0; k < n; k++) {
      if (v[k] == p[i].fi) {
        v[k] = p[i].se;
        f = 1;
      } else if (f) {
        break;
      }
    }
    sum -= co * p[i].fi;
    sum += co * p[i].se;

    cout << sum << endl;
  }

  if (debug) {
    for (auto z : p) {
      cout << z.fi << " " << z.se << endl;
    }
  }
}