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

#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define modulo 998244353
#define endl '\\n'
#define pb push_back
using namespace std;
vector<int> adj[100001];
ll fxp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll c = fxp(a, b / 2);
    return (c * c) % 1000000007;
  }
  return (a * fxp(a, b - 1)) % 1000000007;
}
int lcm(int c, int d) {
  int a = c;
  int b = d;
  int ans;
  if (a >= b) {
    while (b != 0) {
      int x = b;
      b = a % b;
      a = x;
    }
    ans = (c * d) / a;
  } else {
    while (a != 0) {
      int x = a;
      a = b % a;
      b = x;
    }
    ans = (c * d) / b;
  }
  return ans;
}
int root_find() {
  ll x;
  for (int i = 1; i < 100001; i++) {
    if (adj[i].size() == 1) {
      x = i;
      break;
    }
  }
  return x;
}
void dfs(int s, int y) {
  for (int i = 0; i < adj[s].size(); i++) {
    if (adj[s][i] == y)
      continue;
    dfs(adj[s][i], s);
  }
}
bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
ll smallestDivisor(ll n) {
  // if divisible by 2
  if (n % 2 == 0)
    return n / 2;

  // iterate from 3 to sqrt(n)
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return n / i;
  }

  return n;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  ll a[n];
  ll cnt[100001];
  for (int i = 0; i < 100001; i++)
    cnt[i] = 0;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    sum = sum + a[i];
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    // sum= sum + (c-b);
    // cout<<sum<<endl;
    /*while(binary_search(a,a+n,b))
    {
        ll k = upper_bound(a,a+n,b)-a;
        k--;
        a[k]=c;
        sum= sum + (c-b);
    }*/
    sum = sum + cnt[b] * (c - b);
    ll x = cnt[b];
    cnt[b] = 0;
    cnt[c] = cnt[c] + x;
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define modulo 998244353
#define endl '\\n'
#define pb push_back
using namespace std;
vector<int> adj[100001];
ll fxp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll c = fxp(a, b / 2);
    return (c * c) % 1000000007;
  }
  return (a * fxp(a, b - 1)) % 1000000007;
}
int lcm(int c, int d) {
  int a = c;
  int b = d;
  int ans;
  if (a >= b) {
    while (b != 0) {
      int x = b;
      b = a % b;
      a = x;
    }
    ans = (c * d) / a;
  } else {
    while (a != 0) {
      int x = a;
      a = b % a;
      b = x;
    }
    ans = (c * d) / b;
  }
  return ans;
}
int root_find() {
  ll x;
  for (int i = 1; i < 100001; i++) {
    if (adj[i].size() == 1) {
      x = i;
      break;
    }
  }
  return x;
}
void dfs(int s, int y) {
  for (int i = 0; i < adj[s].size(); i++) {
    if (adj[s][i] == y)
      continue;
    dfs(adj[s][i], s);
  }
}
bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
ll smallestDivisor(ll n) {
  // if divisible by 2
  if (n % 2 == 0)
    return n / 2;

  // iterate from 3 to sqrt(n)
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return n / i;
  }

  return n;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
  sort(a, a + n);
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    // sum= sum + (c-b);
    // cout<<sum<<endl;
    while (binary_search(a, a + n, b)) {
      ll k = lower_bound(a, a + n, b) - a;
      a[k] = c;
      sort(a, a + n);
      sum = sum + (c - b);
    }
    cout << sum << endl;
  }

  return 0;
}