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

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

typedef long long ll;
#define fori(n) for (int i = 0; i < n; i++)
#define pb push_back

ll power(ll base, ll e) {
  ll answer = 1;
  // ll e=1000000005;
  while (e) {
    if (e % 2 == 1) {
      answer *= base;
    }
    base *= base;
    // base%=mod;
    e /= 2;
    // answer%=mod;
  }
  return answer;
}

void mergearr(ll arr[], ll l, ll m, ll r) {
  ll i, j, k;
  ll n1 = m - l + 1;
  ll n2 = r - m;
  ll L[n1], R[n2];
  for (i = 0; i < n1; i++)
    L[i] = arr[l + i];
  for (i = 0; i < n2; i++)
    R[i] = arr[m + 1 + i];

  i = 0;
  j = 0;
  k = l;
  while (i < n1 && j < n2) {
    if (L[i] <= R[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = R[j];
      j++;
    }
    k++;
  }
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }
  while (j < n2) {
    arr[k] = R[j];
    j++;
    k++;
  }
}
void mergesort(ll arr[], ll l, ll r) {
  if (l < r) {
    ll m = l + (r - l) / 2;
    mergesort(arr, l, m);
    mergesort(arr, m + 1, r);
    mergearr(arr, l, m, r);
  }
}

void sieveprimes(ll n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (ll i = 2; i * i <= n; i++) {
    if (prime[i] == true) {
      for (ll j = i * i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }

  /*for (int p=2; p<=n; p++)
     if (prime[p])
        cout << p << " ";*/
}

int atoi(string str) {
  int ans = 0; // Your code here
  for (int i = 0; i < str.length(); i++) {
    if (i == 0 && str[i] == '-') {
      continue;
    }
    if (str[i] < 48 || str[i] > 57)
      return -1;
    int temp = (str[i] - '0') * power(10, str.length() - 1 - i);

    ans += temp;
  }
  if (str[0] == '-')
    return 0 - ans;
  return ans;
}

string parenthesis(string s) {
  stack<char> st;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '{' || s[i] == '(' || s[i] == '[')
      st.push(s[i]);
    else {

      switch (s[i]) {
      case ']':
        if (st.top() == '[')
          st.pop();
        break;
      case '}':
        if (st.top() == '{')
          st.pop();
        break;
      case ')':
        if (st.top() == '(')
          st.pop();
        break;
      }
    }
  }
  if (st.empty())
    return "balanced";
  else
    return "not balanced";
}
int main() {

  ll n;
  cin >> n;
  map<ll, ll> a;
  ll sum = 0;
  fori(n) {
    ll temp;

    cin >> temp;
    sum += temp;
    a[temp] = a[temp] + 1;
  }
  map<ll, ll>::iterator it1, it2;
  ll q;
  cin >> q;
  ll fin, rep;
  for (int i = 0; i < q; i++) {
    cin >> fin >> rep;
    if (a.find(fin) == a.end()) {

      cout << sum << endl;
      continue;
    } else {
      it1 = a.find(fin);
      it2 = a.find(rep);

      // cout<<it->first<<" "<<it->second<<endl;

      sum -= (it1->first) * (it1->second);
      sum += (it1->second) * rep;
      // it->second=0;

      a[rep] = a[rep] + it1->second;
      it1->second = 0;
    }

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define fori(n) for (int i = 0; i < n; i++)
#define pb push_back

ll power(ll base, ll e) {
  ll answer = 1;
  // ll e=1000000005;
  while (e) {
    if (e % 2 == 1) {
      answer *= base;
    }
    base *= base;
    // base%=mod;
    e /= 2;
    // answer%=mod;
  }
  return answer;
}

void mergearr(ll arr[], ll l, ll m, ll r) {
  ll i, j, k;
  ll n1 = m - l + 1;
  ll n2 = r - m;
  ll L[n1], R[n2];
  for (i = 0; i < n1; i++)
    L[i] = arr[l + i];
  for (i = 0; i < n2; i++)
    R[i] = arr[m + 1 + i];

  i = 0;
  j = 0;
  k = l;
  while (i < n1 && j < n2) {
    if (L[i] <= R[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = R[j];
      j++;
    }
    k++;
  }
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }
  while (j < n2) {
    arr[k] = R[j];
    j++;
    k++;
  }
}
void mergesort(ll arr[], ll l, ll r) {
  if (l < r) {
    ll m = l + (r - l) / 2;
    mergesort(arr, l, m);
    mergesort(arr, m + 1, r);
    mergearr(arr, l, m, r);
  }
}

void sieveprimes(ll n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (ll i = 2; i * i <= n; i++) {
    if (prime[i] == true) {
      for (ll j = i * i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }

  for (int p = 2; p <= n; p++)
    if (prime[p])
      cout << p << " ";
  /*for(ll i=2;i<=n;i++)
  {
      if(prime[i])    ans.insert(i*i);
  }*/
}

int main() {

  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  fori(n) {

    cin >> a[i];
    sum += a[i];
  }

  ll q;
  cin >> q;
  ll fin, rep;
  for (int i = 0; i < q; i++) {
    cin >> fin >> rep;
    for (int i = 0; i < n; i++) {
      if (a[i] == fin) {
        sum -= a[i];
        a[i] = rep;
        sum += a[i];
      }
    }

    cout << sum << endl;
  }
}