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

#include <bits/stdc++.h>

#include <iostream>
#define pb push_back
#define sort_incr(v) sort(v.begin(), v.end());
#define sort_decr(v)                                                           \\
  {                                                                            \\
    sort(v.begin(), v.end());                                                  \\
    reverse(v.begin(), v.end());                                               \\
  }
#define take_input(v, n)                                                       \\
  for (ll i = 0, temp; i < n; i++) {                                           \\
    cin >> temp;                                                               \\
    v.pb(temp);                                                                \\
  }
#define fastio                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define ALL(v) v.begin(), v.end()

typedef long long ll;
using namespace std;

int main() {
  fastio;
  ll n, temp, su = 0;
  cin >> n;
  vector<ll> v;
  int co[100004] = {0};
  // take_input(v,n);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    co[temp]++;
    su = su + temp;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    su = su + (b - a) * co[a];
    co[b] += co[a];
    co[a] = 0;

    cout << su << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <iostream>
#define pb push_back
#define sort_incr(v) sort(v.begin(), v.end());
#define sort_decr(v)                                                           \\
  {                                                                            \\
    sort(v.begin(), v.end());                                                  \\
    reverse(v.begin(), v.end());                                               \\
  }
#define take_input(v, n)                                                       \\
  for (ll i = 0, temp; i < n; i++) {                                           \\
    cin >> temp;                                                               \\
    v.pb(temp);                                                                \\
  }
#define fastio                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define ALL(v) v.begin(), v.end()

typedef long long ll;
using namespace std;

////*********           REMOVE DUPLICATES FROM VECTOR       **********///////

vector<int> remove_duplicate_elements(vector<int> v) {
  vector<int>::iterator ip;
  ip = unique(v.begin(), v.end());
  v.resize(std::distance(v.begin(), ip));
  return v;
}

//// **************                CONVERT STRING TO UNIQUE CHARACTERS
///**************////
string make_unique_string(string s) {
  string s1 = "";
  for (int i = 0; i < s.length(); i++) {
    bool flag = true;
    for (int j = 0; j < s1.length(); j++) {
      if (s1[j] == s[i]) {
        flag = false;
        break;
      }
    }

    if (flag)
      s1 = s1 + s[i];
  }
  return s1;
}

//////// Three Element Vector
struct vec_thre {
  int first, second, third;
};

/////// Comparing on sorting a vector

bool comp(vec_thre a, vec_thre b) {
  if (a.first != b.first)
    return a.first < b.first;
  if (a.second != b.second)
    return a.second < b.second;
}

/////////Sorting vector on the basis of second Element of vector

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

// Driver function to sort the vector elements by  first element of pair in
// descending order,
// if first is same than second is consdidered
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {

  if (a.first != b.first)
    return (a.first > b.first);

  return (a.second > b.second);
}

int main() {
  int n;
  cin >> n;
  vector<int> v;
  take_input(v, n);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
      if (v[i] == a)
        v[i] = b;
    }

    // for(int i=0;i<n;i++)
    // cout<<v[i]<<" ";
    // cout<<"\\n";
    cout << accumulate(ALL(v), 0) << "\\n";
  }

  return 0;
}