alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long n = 0;
  cin >> n;
  vector<long long> vect(100001, 0);
  long long sum = 0;
  long long temp = 0;
  for (long long i = 0; i < n; i++) {
    cin >> temp;
    sum = sum + temp;
    vect[temp]++;
  }
  long long q = 0;
  cin >> q;
  long long t1 = 0;
  long long t2 = 0;
  for (long long i = 0; i < q; i++) {
    cin >> t1 >> t2;
    if (vect[t1] != 0) {
      vect[t2] = vect[t2] + vect[t1];
      sum = sum - (t1 * vect[t1]) + (t2 * vect[t1]);
      cout << sum << endl;
      vect[t1] = 0;
    } else {
      cout << sum << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<int> vect(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> vect[i];
    sum = sum + vect[i];
  }
  int q = 0;
  cin >> q;
  int t1 = 0;
  int t2 = 0;
  for (int i = 0; i < q; i++) {
    cin >> t1 >> t2;
    for (int j = 0; j < vect.size(); j++) {
      if (vect[j] == t1) {
        vect[j] = t2;
        sum = sum - t1 + t2;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define mod 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(var, start, end) for (int var = start; var < end; var++)
#define repr(var, start, end) for (int var = start; var >= end; var--)
#define repi(var, start, end) for (int var = start; var <= end; var++)
#define debg(var) cerr << #var << " : " << var << " "
#define pb push_back
#define mp make_pair

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

void solve() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  map<int, int> m;
  rep(i, 0, n) { m[a[i]]++; }
  int q;
  cin >> q;
  ull sum = accumulate(a, a + n, (ull)0);
  while (q--) {
    ull b, c;
    cin >> b >> c;
    ull k = m[b];
    sum -= (b * k);
    m.erase(b);
    m[c] += k;
    sum += (c * k);
    cout << sum << '\\n';
  }
}

int main() {
  fastio int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define mod 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(var, start, end) for (int var = start; var < end; var++)
#define repr(var, start, end) for (int var = start; var >= end; var--)
#define repi(var, start, end) for (int var = start; var <= end; var++)
#define debg(var) cerr << #var << " : " << var << " "
#define pb push_back
#define mp make_pair

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

void solve() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  map<int, int> m;
  rep(i, 0, n) { m[a[i]]++; }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int k = m[b];
    m[b] = 0;
    m[c] += k;
    ull sum = 0;
    for (auto i = m.begin(); i != m.end(); ++i) {
      sum += (ull)i->first * i->second;
    }
    cout << sum << '\\n';
  }
}

int main() {
  fastio int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

i64 table[100001];

int main() {
  i64 n, q, sum{};
  cin >> n;
  for (i64 i = 0; i < n; i++) {
    i64 a;
    cin >> a;
    sum += a;
    table[a]++;
  }
  cin >> q;
  i64 ans[q];
  for (i64 i = 0; i < q; i++) {
    i64 b, c;
    cin >> b >> c;
    sum += (c - b) * table[b];
    table[c] += table[b];
    table[b] = 0;
    ans[i] = sum;
  }
  for (i64 i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

i64 table[10001];

int main() {
  i64 n, q, sum{};
  cin >> n;
  for (i64 i = 0; i < n; i++) {
    i64 a;
    cin >> a;
    sum += a;
    table[a]++;
  }
  cin >> q;
  i64 ans[q];
  for (i64 i = 0; i < q; i++) {
    i64 b, c;
    cin >> b >> c;
    sum += (c - b) * table[b];
    table[c] += table[b];
    table[b] = 0;
    ans[i] = sum;
  }
  for (i64 i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

/*   ꧁༒☬महाकाल☬༒꧂ ꧁༒ঔमहाकालঔ༒꧂

                   ঔৣ۝महाकालᴳᵒᵈ۝ঔৣ

 */
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T, typename cmp=less<T>> using oset =tree<T, null_type,
// cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define mod 1000000007
#define inf 100000000000000000
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define _it iterator
#define all(_x) _x.begin(), _x.end()
#define f first
#define s second
#define pb push_back
void mahakal() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
const ll maxN = 1e5 + 5;
ll a[maxN], n, q, A, B;
map<ll, ll> mp;

void solve() {
  cin >> n;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    cin >> A >> B;
    if (mp.count(A) == 0) {
      cout << sum << endl;
      continue;
    }
    ll ps = mp[A];
    sum -= (ps * A);
    sum += (ps * B);
    mp.erase(A);
    if (mp.count(B))
      mp[B] += ps;
    else
      mp[B] = ps;
    cout << sum << endl;
  }
}
int main() {
  mahakal(), fast;
  ll t = 1;
  // cin>>t ;
  while (t--)
    solve();

  return 0;
}

Error: 0

Fixed submission:

/*   ꧁༒☬महाकाल☬༒꧂ ꧁༒ঔमहाकालঔ༒꧂

                   ঔৣ۝महाकालᴳᵒᵈ۝ঔৣ

 */
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T, typename cmp=less<T>> using oset =tree<T, null_type,
// cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define mod 1000000007
#define inf 100000000000000000
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define _it iterator
#define all(_x) _x.begin(), _x.end()
#define f first
#define s second
#define pb push_back
void mahakal() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
const ll maxN = 1e5 + 5;
ll a[20], n, q, A, B;
map<ll, ll> mp;

void solve() {
  cin >> n;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  cin >> q;
  for (ll i = 0; i < q; i++) {
    cin >> A >> B;
    ll ps = mp[A];
    sum -= (ps * A);
    sum += (ps * B);
    mp.erase(A);
    if (mp.count(B))
      mp[B] += ps;
    else
      mp[B] = ps;
    cout << sum << endl;
  }
}
int main() {
  // mahakal(),fast;
  ll t = 1;
  // cin>>t ;
  while (t--)
    solve();

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

int main(void) {
  long long N, Q;
  cin >> N;

  long long sum = 0;
  map<long long, long long> data;
  for (int i = 0; i < N; i++) {
    long long val;
    cin >> val;
    auto it = data.find(val);
    if (it == data.end()) {
      data.insert({val, 1});
    } else {
      it->second += 1;
    }
    sum += val;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long B, C;
    cin >> B;
    cin >> C;

    auto itB = data.find(B);
    auto itC = data.find(C);
    if (itB != data.end()) {
      long long d = (C - B) * itB->second;
      if (itC == data.end()) {
        data.insert({C, itB->second});
      } else {
        itC->second += itB->second;
      }
      data.erase(itB);
      sum += d;
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

typedef struct data_info {
  long long val;
  long long count;
} data_info;

int main(void) {
  long long N, Q;
  cin >> N;

  long long sum = 0;
  vector<data_info> data;
  for (int i = 0; i < N; i++) {
    long long val;
    cin >> val;
    vector<data_info>::iterator it =
        std::find_if(data.begin(), data.end(), [val](const data_info &other) {
          return other.val == val;
        });
    if (it == data.end()) {
      data.emplace_back(data_info{val, 1});
    } else {
      it->count += 1;
    }
    sum += val;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long B, C;
    cin >> B;
    cin >> C;

    vector<data_info>::iterator itB =
        std::find_if(data.begin(), data.end(),
                     [B](const data_info &other) { return other.val == B; });
    vector<data_info>::iterator itC =
        std::find_if(data.begin(), data.end(),
                     [C](const data_info &other) { return other.val == C; });
    if (itB != data.end()) {
      long long d = (C - B) * itB->count;
      if (itC == data.end()) {
        itB->val = C;
      } else {
        itC->count += itB->count;
        data.erase(itB);
      }
      sum += d;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  long long sum = 0;
  vector<long long> a(100001, 0);
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a[b]++;
    sum += b;
  }

  int q;
  cin >> q;
  vector<long long> ans;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum -= a[b] * b;
    sum += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    ans.push_back(sum);
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  long long sum = 0;
  vector<int> a(10001, 0);
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    a[b]++;
    sum += b;
  }

  int q;
  cin >> q;
  vector<long long> ans;

  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum -= a[b] * b;
    sum += a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    ans.push_back(sum);
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int N, Q;
  cin >> N;
  int a;
  unordered_map<long long, long long> nums;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a;
    nums[a] += 1;
    sum += a;
  }
  cin >> Q;
  long long b, c;
  for (long long i = 0; i < Q; i++) {
    cin >> b >> c;
    if (nums.find(b) != nums.end()) {
      if (b > c) {
        sum -= (b - c) * nums[b];
      } else {
        sum += (c - b) * nums[b];
      }
      nums[c] += nums[b];
      nums[b] = 0;
    }
    cout << sum << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int N, Q;
  cin >> N;
  int a;
  vector<int> nums;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a;
    nums.push_back(a);
    sum += a;
  }
  cin >> Q;
  int b, c;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    for (int j = 0; j < N; j++) {
      if (nums[j] == b) {
        nums[j] = c;
        if (b > c) {
          sum -= (b - c);
        } else {
          sum += (c - b);
        }
      }
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <math.h>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(100010);
  ll ans = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    ans += x;
    a[x]++;
  }
  int q;
  cin >> q;
  rep(i, q) {
    ll b, c;
    cin >> b >> c;
    ans = ans - b * a[b] + c * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

// #include <math.h>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    ll ans = 0;
    rep(j, n) {
      if (a[j] == b)
        a[j] = c;
      ans += a[j];
    }

    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

// Replacing
#include <algorithm>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 29;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> digit(100001, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    digit[A[i]]++;
  }
  unsigned long long sum = 0;
  for (int j = 0; j < N; j++) {
    sum += A[j];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;

    int diff = C - B;
    sum = sum + digit[B] * diff;
    digit[C] = digit[C] + digit[B];
    digit[B] = 0;

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// Replacing
#include <algorithm>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 29;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> digit(9);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    digit[A[i]]++;
  }
  int sum = 0;
  for (int j = 0; j < N; j++) {
    sum += A[j];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;

    int diff = C - B;
    sum += digit[B] * diff;
    digit[C] += digit[B];
    digit[B] = 0;

    cout << sum << endl;
  }

  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INT(str) stoi((str), 0)
#define INTB(str) stoi((str), 0, 2) // 2進数の文字列を int に
// cout << fixed << setprecision(15);

int main() {
  ll r = 0;
  int N;
  cin >> N;
  vector<ll> A(1e5 + 1);
  REP(i, N) {
    int a;
    cin >> a;
    r += a;
    A.at(a)++;
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    ll B, C;
    cin >> B >> C;
    r += A.at(B) * (C - B);
    A.at(C) += A.at(B);
    A.at(B) = 0;
    cout << r << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INT(str) stoi((str), 0)
#define INTB(str) stoi((str), 0, 2) // 2進数の文字列を int に
// cout << fixed << setprecision(15);

int main() {
  ll r = 0;
  int N;
  cin >> N;
  vi A(1e5);
  REP(i, N) {
    int a;
    cin >> a;
    r += a;
    A.at(a)++;
  }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int B, C;
    cin >> B >> C;
    r += A.at(B) * (C - B);
    A.at(C) += A.at(B);
    A.at(B) = 0;
    cout << r << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main(void) {
  long N;
  cin >> N;
  long table[100000 + 20] = {0};
  vector<long> list;
  long sum = 0;
  for (long i = 0; i < N; i++) {
    long tmp = 0;
    cin >> tmp;
    sum += tmp;
    list.push_back(tmp);
    table[tmp] += 1;
  }
  long Q;
  cin >> Q;
  for (long i = 0; i < Q; i++) {
    long B, C;
    cin >> B >> C;

    long num = table[B];
    table[B] = 0;
    table[C] += num;

    sum -= num * B;
    sum += num * C;

    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main(void) {
  long N;
  cin >> N;
  vector<long> table;
  for (long i = 0; i < N; i++) {
    long tmp = 0;
    cin >> tmp;
    table.push_back(tmp);
  }
  long Q;
  cin >> Q;
  for (long i = 0; i < Q; i++) {
    long B, C;
    cin >> B >> C;
    long sum = 0;
    for (long i = 0; i < table.size(); i++) {
      if (table[i] == B) {
        table[i] = C;
      }
      sum += table[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {

  long long N;
  cin >> N;
  long long i;
  long long A[N];
  long long Anum[100001] = {};
  long long sum = 0;
  for (i = 0; i <= N - 1; i++) {

    cin >> A[i];
    Anum[A[i]]++;
  }
  long long Q;
  cin >> Q;
  long long B[Q];
  long long C[Q];
  for (i = 0; i <= Q - 1; i++) {

    cin >> B[i] >> C[i];
  }
  for (i = 0; i <= 100000; i++) {

    sum += Anum[i] * i;
  }
  for (i = 0; i <= Q - 1; i++) {

    sum += C[i] * Anum[B[i]];
    sum -= B[i] * Anum[B[i]];
    Anum[C[i]] += Anum[B[i]];
    Anum[B[i]] -= Anum[B[i]];
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  int N;
  cin >> N;
  int i;
  int A[N];
  int Anum[100001] = {};
  long long sum = 0;
  for (i = 0; i <= N - 1; i++) {

    cin >> A[i];
    Anum[A[i]]++;
  }
  int Q;
  cin >> Q;
  int B[Q];
  int C[Q];
  for (i = 0; i <= Q - 1; i++) {

    cin >> B[i] >> C[i];
  }
  for (i = 0; i <= Q - 1; i++) {
    sum = 0;
    Anum[C[i]] += Anum[B[i]];
    Anum[B[i]] = 0;
    for (int j = 0; j <= 100000; j++) {

      sum += Anum[j] * j;
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
const ll INF = 9223372036854775807;

int main() {
  int n;
  cin >> n;
  vector<int> num(100005, 0);
  rep(i, n) {
    int s;
    cin >> s;
    num[s]++;
  }
  ll ans = 0;
  rep(i, 100005) { ans += num[i] * i; }
  int Q;
  cin >> Q;
  rep(i, Q) {
    int a, b;
    cin >> a >> b;
    ans += (b - a) * num[a];
    num[b] += num[a];
    num[a] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
const ll INF = 9223372036854775807;

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  rep(i, n) cin >> vec[i];
  // sort(vec.begin(),vec.end());
  ll Ans = 0;
  rep(i, n) Ans += vec[i];
  int Q;
  cin >> Q;
  rep(i, Q) {
    int a = 0;
    int b = 0;
    cin >> a >> b;
    int c = 0;
    rep(i, n) {
      if (vec[i] == a) {
        c++;
        vec[i] = b;
      }
    }
    Ans += (b - a) * c;
    cout << Ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  long long int n;
  cin >> n;
  long long int sum = 0;
  unordered_map<long long int, long long int> m;
  for (long long int i = 0; i < n; i++) {
    long long int p;
    cin >> p;
    sum += p;
    m[p]++;
  }
  long long int q;
  cin >> q;
  while (q--) {
    long long int a, b;
    cin >> a >> b;
    m[b] += m[a];
    sum = sum + (b - a) * m[a];
    m.erase(a);
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  long long int n;
  cin >> n;
  unordered_map<long long int, long long int> m;
  for (long long int i = 0; i < n; i++) {
    long long int p;
    cin >> p;
    m[p]++;
  }
  long long int q;
  cin >> q;
  while (q--) {
    long long int a, b;
    cin >> a >> b;
    m[b] += m[a];
    m.erase(a);
    long long int sum = 0;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      sum += (itr->first) * (itr->second);
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);

  long long n, sum = 0;
  cin >> n;
  long long count[100005] = {0};
  for (int i = 1; i <= n; i++) {
    int l;
    cin >> l;
    count[l]++;
    sum += l;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum -= b * count[b];
    sum += c * count[b];
    count[c] += count[b];
    count[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ctype.h>
#include <iomanip>
#include <math.h>
#include <queue>
#include <string>
using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        a[i] = c;
        sum += a[i];
      } else {
        sum += a[i];
      }
    }
    cout << sum << endl;
  }
}