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


/*
FIND YOUR PURPOSE BECOME UNSTOPPABLE---->>> ??????
            ,.
          ,_> `.   ,';
      ,-`'      `'   '`'._
    ,,-) ---._   |   .---''`-),.
  ,'      `.  \\  ;  /   _,'     `,
,--' ____       \\   '  ,'    ___  `-,
_>   /--. `-.              .-'.--\\   \\__
'-,  (    `.  `.,`~ \\~'-. ,' ,'    )    _\\
_<    \\     \\ ,'  ') )   `. /     /    <,.
,-'   _,  \\    ,'    ( /      `.    /        `-,
`-.,-'     `.,'       `         `.,'  `\\    ,-'
,'       _  /   ,,,      ,,,     \\     `-. `-._
/-,     ,'  ;   ' _ \\    / _ `     ; `.     `(`-\\
/-,        ;    (o)      (o)      ;          `'`,
,~-'  ,-'    \\     '        `      /     \\      <_
/-. ,'        \\                   /       \\     ,-'
'`,     ,'   `-/             \\-' `.      `-. <
/_    /      /   (_     _)   \\    \\          `,
`-._;  ,' |  .::.`-.-' :..  |       `-.    _\\
_/       \\  `:: ,^. :.:' / `.        \\,-'
'`.   ,-'  /`-..-'-.-`-..-'\\            `-.
>_ /     ;  (\\/( ' )\\/)  ;     `-.    _<
,-'      `.  \\`-^^^-'/  ,'        \\ _<
`-,  ,'   `. `"""""' ,'   `-.   <`'
  ')        `._.,,_.'        \\ ,-'
    '._        '`'`'   \\       <
      >   ,'       ,   `-.   <`'
        `,/          \\      ,-`
        `,   ,' |   /     /
          '; /   ;        (
          _)|   `       (
          `')         .-'
            <_   \\   /
              \\   /\\(
                `;/  `
*/
#include <bits/stdc++.h>
using namespace std;
#define speed                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define ll long long int
#define INF 100000000

// GCD of 2 numbers START
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// Check Prime STARTS
int IsPrime(ll n) {
  if ((n % 2 == 0 && n != 2) || n == 1)
    return 0;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

// SIEVE
int sieve(ll N) {
  bool isPrime[N + 1];
  for (ll i = 0; i <= N; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (ll i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) { // Mark all the multiples of i as composite
                              // numbers
      for (int j = i * i; j <= N; j += i)
        isPrime[j] = false;
    }
  }
  // Count
  /*ll c=0;
  for (int p=2; p<=N; p++)
  {
      if (isPrime[p])
      c++;
  }
  return  c;*/
  // print
  /*for (int p=2; p<=N; p++)
  {
      if (isPrime[p])

    cout << p << " ";
  }
  return 0;
  */
}
/*
int main(){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
                cin>>a[i];
        }
        int sum=0,max=INT_MIN;
        for(int i=0;i<n;i++){
                sum=0;
                for(int j=i;j<n;j++){
                        sum+=a[j];
                        if(sum>max)
                        max=sum;
                }
        }
        cout<<max<<endl;
}
*/

int maxsubarraysum(int a[], int size) {
  int max_so_far = INT_MIN, max_ending_here = 0;

  for (int i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_so_far < max_ending_here)
      max_so_far = max_ending_here;

    if (max_ending_here < 0)
      max_ending_here = 0;
  }
  return max_so_far;
}

int maxSubArraySum(int a[], int size) {
  int max_so_far = 0, max_ending_here = 0;
  for (int i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_ending_here < 0)
      max_ending_here = 0;

    /* Do not compare for all elements. Compare only
       when  max_ending_here > 0 */
    else if (max_so_far < max_ending_here)
      max_so_far = max_ending_here;
  }
  return max_so_far;
}
int max_sum_subarray(int arr[], int n) {
  int meh = 0, msf = INT_MIN;
  for (int i = 0; i < n; i++) {
    meh = meh + arr[i];
    if (msf < meh)
      msf = meh;
    if (meh < 0)
      meh = 0;
  }
  return msf;
}

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  map<ll, ll> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    m[a[i]]++;
  }
  ll q;
  cin >> q;
  while (q--) {

    ll b, c;
    cin >> b >> c;
    sum += (m[b] * c - m[b] * b);
    m[c] += m[b];
    m[b] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define speed                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define ll long long int
#define INF 100000000

int main() {
  speed;
  ll n;
  cin >> n;
  ll a[n];
  ll sum1 = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum1 += a[i];
  }
  ll ans = sum1;
  ll q;
  cin >> q;
  while (q--) {

    ll b, c;
    cin >> b >> c;

    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        ans += (c - b);
        a[i] = c;
      }
    }
    cout << ans << endl;
    // ans=0;
  }
}