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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MAX = 100010;

int main() {
  int n;
  cin >> n;
  vector<int> cnt(MAX);
  ll sum = 0;
  int temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    cnt[temp]++;
    sum += temp;
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void replaceArray(int a[], int n, int bi, int ci) {
  for (int i = 0; i < n; i++) {
    if (a[i] == bi)
      a[i] = ci;
  }
}

long long sumArray(int a[], int n) {
  long long temp = 0;
  for (int i = 0; i < n; i++) {
    temp += a[i];
  }
  return temp;
}

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

  for (int i = 0; i < q; i++) {
    replaceArray(a, n, b[i], c[i]);
    cout << sumArray(a, n) << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define ll long long
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  long long q;
  cin >> q;
  vector<ll> b(100001, 0), c(100001, 0);
  rep(i, 0, q) cin >> b[i] >> c[i];
  vector<ll> s(100000, 0);
  rep(i, 0, n) { s[a[i]]++; }
  rep(i, 0, q) {
    sum -= s[b[i]] * b[i];
    sum += s[b[i]] * c[i];
    s[c[i]] += s[b[i]];
    s[b[i]] = 0ll;
    cout << sum << "\\n";
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define ll long long
using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  long long q;
  cin >> q;
  vector<ll> b(q), c(q);
  rep(i, 0, q) cin >> b[i] >> c[i];
  vector<ll> s(10001, 0);
  rep(i, 0, n) { s[a[i]]++; }
  rep(i, 0, q) {
    sum -= s[b[i]] * b[i];
    sum += s[b[i]] * c[i];
    s[c[i]] += s[b[i]];
    s[b[i]] = 0ll;
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define PI 3.1415926535897932
#define MOD 1000000007;
using namespace std;
long long GCD(long long A, long long B) {
  if (B == 0) {
    return A;
  } else {
    return GCD(B, A % B);
  }
}
long long LCM(long long A, long long B) {
  long long g = GCD(A, B);
  return A / g * B;
}
long long num_order(long long X) {
  long long ans = 0;
  while (true) {
    if (X == 0) {
      break;
    } else {
      X /= 10;
      ans++;
    }
  }
  return ans;
}
int main() {

  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());

  int Q;
  cin >> Q;

  vector<vector<int>> vec(Q, vector<int>(2));
  for (int i = 0; i < Q; i++) {
    cin >> vec[i][0] >> vec[i][1];
  }

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += A[i];
  }

  vector<int> app(100000, 0);
  for (int i = 0; i < N; i++) {
    app[A[i] - 1]++;
  }

  for (int i = 0; i < Q; i++) {
    sum = sum + (vec[i][1] - vec[i][0]) * app[vec[i][0] - 1];
    cout << sum << endl;
    app[vec[i][1] - 1] += app[vec[i][0] - 1];
    app[vec[i][0] - 1] = 0;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define PI 3.1415926535897932
#define MOD 1000000007;
using namespace std;
long long GCD(long long A, long long B) {
  if (B == 0) {
    return A;
  } else {
    return GCD(B, A % B);
  }
}
long long LCM(long long A, long long B) {
  long long g = GCD(A, B);
  return A / g * B;
}
long long num_order(long long X) {
  long long ans = 0;
  while (true) {
    if (X == 0) {
      break;
    } else {
      X /= 10;
      ans++;
    }
  }
  return ans;
}
int main() {

  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());

  int Q;
  cin >> Q;

  vector<vector<int>> vec(Q, vector<int>(2));
  for (int i = 0; i < Q; i++) {
    cin >> vec[i][0] >> vec[i][1];
  }

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += A[i];
  }

  set<int> S;
  for (int i = 0; i < N; i++) {
    S.insert(A[i]);
  }

  int x = S.size();

  vector<int> app(x);
  for (int i = 0; i < x; i++) {
    app[i] = count(A.begin(), A.end(), *begin(S));
    S.erase(*begin(S));
  }

  for (int i = 0; i < Q; i++) {
    sum = sum + (vec[i][1] - vec[i][0]) * app[vec[i][0] - 1];
    cout << sum << endl;
    app[vec[i][1] - 1] += app[vec[i][0] - 1];
    app[vec[i][0] - 1] = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pi 3.14159265358979
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pi 3.14159265358979
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

int main() {
  ll n, x, a[100005], q, b, c, ans = 0;
  cin >> n;
  rep(i, 100005) { a[i] = 0; }
  rep(i, n) {
    cin >> x;
    a[x]++;
  }
  cin >> q;
  rep(i, q) {
    ans = 0;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    rep(i, 100005) { ans += i * a[i]; }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, Q;
  cin >> N;
  vector<int> bucket(100000, 0);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    bucket.at(x - 1)++;
    sum += x;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * bucket.at(b - 1);
    bucket.at(c - 1) += bucket.at(b - 1);
    bucket.at(b - 1) = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, Q;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    int sum = 0;
    for (int j = 0; j < N; j++) {
      if (A.at(j) == b)
        A.at(j) = c;
      sum += A.at(j);
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

int main() {
  int N;
  cin >> N;
  long long ans = 0;
  vector<long long> A(N);

  for (int n = 0; n < N; ++n) {
    cin >> A[n];
    ans += A[n];
  }
  int Q;
  cin >> Q;
  vector<long long> B(Q);
  vector<long long> C(Q);
  for (int n = 0; n < Q; ++n) {
    cin >> B[n] >> C[n];
  }
  vector<long long> X(100001);
  for (int n = 0; n < N; ++n) {
    X[A[n]]++;
  }
  for (int n = 0; n < Q; ++n) {
    ans = ans + C[n] * X[B[n]] - B[n] * X[B[n]];
    X[C[n]] += X[B[n]];
    X[B[n]] = 0;
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int main() {
  int N;
  cin >> N;
  long long ans = 0;
  vector<long long> A(N);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
    ans += A[n];
  }
  int Q;
  cin >> Q;
  vector<long long> B(Q);
  vector<long long> C(Q);
  for (int n = 0; n < Q; ++n) {
    cin >> B[n] >> C[n];
  }
  vector<long long> X(10001);
  for (int n = 0; n < N; ++n) {
    X[A[n]]++;
  }
  for (int n = 0; n < Q; ++n) {
    ans = ans + C[n] * X[B[n]] - B[n] * X[B[n]];
    X[C[n]] += X[B[n]];
    X[B[n]] = 0;
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vv = vector<vector<ll>>;
template <class _t> inline bool chmax(_t &a, _t b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class _t> inline bool chmin(_t &a, _t b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 2147483647;
const long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
  multiset<int> st;
  vector<int> table(100010, 0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    st.insert(a);
    table[a]++;
  }
  int Q;
  cin >> Q;
  ll ans = 0;
  for (auto itr = st.begin(); itr != st.end(); itr++) {
    ans += *itr;
  }

  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    int k = table[a];
    table[a] = 0;
    table[b] += k;
    ans += k * (b - a);
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vv = vector<vector<ll>>;
template <class _t> inline bool chmax(_t &a, _t b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class _t> inline bool chmin(_t &a, _t b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 2147483647;
const long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
  multiset<int> st;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    st.insert(a);
  }
  int Q;
  cin >> Q;
  ll ans = 0;
  for (auto itr = st.begin(); itr != st.end(); itr++) {
    ans += *itr;
  }
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    auto itr = st.find(a);
    int k = st.count(a);
    st.erase(a);
    for (int j = 0; j < k; j++) {
      st.insert(b);
    }
    ans += k * (b - a);
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define PI acos(-1)
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll mod = 1e9 + 7;

int main() {
  fast_io

      int n,
      q;
  cin >> n;
  vector<int> A(n);
  map<int, int> mp;
  rep(i, n) {
    cin >> A[i];
    mp[A[i]]++;
  }
  cin >> q;
  vector<int> B(q), C(q);
  rep(i, q) cin >> B[i] >> C[i];
  ll ans = 0;
  rep(i, n) { ans += A[i]; }

  rep(i, q) {
    if (mp[B[i]] == 0) {
      cout << ans << endl;
      continue;
    }
    ans += (C[i] - B[i]) * mp[B[i]];
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define PI acos(-1)
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll mod = 1e9 + 7;

int main() {
  fast_io

      int n,
      q;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];
  cin >> q;
  vector<int> B(q), C(q);
  rep(i, q) cin >> B[i] >> C[i];
  ll ans = 0;
  rep(i, n) { ans += A[i]; }

  rep(i, q) {
    rep(j, n) {
      if (A[j] == B[i]) {
        A[j] = C[i];
        ans += C[i] - B[i];
      }
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  ll N, Q, b, c;
  cin >> N;
  int anums[100005] = {};
  int as[100005] = {};
  ll sum = 0;
  for (ll i = 0; i < N; i++) {
    cin >> as[i];
    anums[as[i]]++;
    sum += as[i];
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    sum += anums[b] * (c - b);
    anums[c] += anums[b];
    anums[b] = 0;
    cout << sum << '\\n';
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  ll N, Q, b, c;
  cin >> N;
  int anums[10005] = {};
  int as[10005] = {};
  ll sum = 0;
  for (ll i = 0; i < N; i++) {
    cin >> as[i];
    anums[as[i]]++;
    sum += as[i];
  }
  cin >> Q;
  while (Q--) {
    cin >> b >> c;
    sum += anums[b] * (c - b);
    anums[c] += anums[b];
    anums[b] = 0;
    cout << sum << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Q 1000000007

// ll T,N;
// vector<ll> a(N);
// const int M = 2;
// void dfs(vector<int> &A) {
//     // 終端条件 --- 10 重ループまで回したら処理して打ち切り
//     if (A.size() == n) {
//         return;
//     }

//     for (int v = 0; v < M; ++v) {
//         A.push_back(v);
//         dfs(A);
//         A.pop_back(); // これが結構ポイント
//     }
// }

bool IsPrime(ll num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (ll i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {
  ll n;
  cin >> n;
  // vector<ll> a(n);
  ll cnt[100010] = {0};
  rep(i, n) {
    ll a;
    cin >> a;
    cnt[a]++;
  }
  ll q;
  cin >> q;
  vector<ll> b(q), c(q);
  ll sum = 0;
  rep(i, 100010) { sum += i * cnt[i]; }

  // ll sum = std::accumulate(a.begin(), a.end(), 0LL);
  rep(i, q) {
    cin >> b[i] >> c[i];

    ll diff = c[i] - b[i];
    sum += diff * cnt[b[i]];
    cnt[c[i]] += cnt[b[i]];
    cnt[b[i]] = 0;

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Q 1000000007

// ll T,N;
// vector<ll> a(N);
// const int M = 2;
// void dfs(vector<int> &A) {
//     // 終端条件 --- 10 重ループまで回したら処理して打ち切り
//     if (A.size() == n) {
//         return;
//     }

//     for (int v = 0; v < M; ++v) {
//         A.push_back(v);
//         dfs(A);
//         A.pop_back(); // これが結構ポイント
//     }
// }

bool IsPrime(ll num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (ll i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> m;
  rep(i, n) {
    cin >> a[i];
    m[a[i]]++;
  }
  ll q;
  cin >> q;
  vector<ll> b(q), c(q);
  ll sum = 0;
  for (const auto &[key, value] : m) {
    // std::cout << key << " => " << value << "\\n";
    sum += key * value;
  }

  // ll sum = std::accumulate(a.begin(), a.end(), 0LL);
  rep(i, q) {
    cin >> b[i] >> c[i];

    ll diff = c[i] - b[i];
    sum += diff * m[b[i]];
    m[c[i]] += m[b[i]];
    m[b[i]] = 0;
    pair<ll, ll> item;
    for (auto it = m.begin(); it != m.end(); it++) {
      item = *it;
      // cout<<item.first<<" : "<<item.second<<endl;
    }
    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define arep(x, n) for (int x : n)
using namespace std;
using ll = long long;

const int N = 100000;

int main() {
  ll n;
  cin >> n;
  ll tmp;
  ll total = 0;
  vector<ll> a(N + 1, 0);
  rep(i, n) {
    cin >> tmp;
    a[tmp]++;
    total += tmp;
  }
  ll q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  rep(i, q) cin >> b[i] >> c[i];

  vector<ll> ans(q, 0);
  rep(i, q) {
    total -= b[i] * a[b[i]];
    total += c[i] * a[b[i]];
    a[c[i]] += a[b[i]];
    a[b[i]] = 0;
    ans[i] = total;
  }

  for (ll x : ans)
    cout << x << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define arep(x, n) for (int x : n)
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;
  vector<int> b(q);
  vector<int> c(q);
  rep(i, q) cin >> b[i] >> c[i];

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

  for (ll x : ans)
    cout << x << endl;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;

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

  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    if (m.count(b)) {
      sum += (c - b) * m.at(b);
      m[c] += m.at(b);
      m.erase(b);
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vi vec(n);
  rep(i, n) cin >> vec.at(i);
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    rep(j, n) {
      if (vec.at(j) == b)
        vec.at(j) = c;
    }
    ll sum = 0;
    rep(k, n) sum += vec.at(k);
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

typedef long long ll;
const ll mod = 1000000007;
#define MAX 50

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> m;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll c = 0;
    ll x, y;
    cin >> x >> y;
    // cout<<m[x]<<" "<<m[y];
    sum -= (m[x] * x);

    sum += (m[x] * y);
    m[y] += m[x];
    m[x] = 0;

    /*for(int i=0;i<n;i++)
    {
        if(a[i]==x)
        a[i]=y;
        c+=a[i];

    }*/
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

typedef long long ll;
const ll mod = 1000000007;
#define MAX 50

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> m;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll c = 0;
    ll x, y;
    cin >> x >> y;
    for (int i = 0; i < n; i++) {
      if (a[i] == x)
        a[i] = y;
      c += a[i];
    }
    cout << c << endl;
  }

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

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

int main(void) {
  ll n, q, a;
  cin >> n;
  vector<ll> x(100001, 0);
  rep(i, n) {
    cin >> a;
    x[a]++;
  }
  cin >> q;
  vector<ll> b(q + 1), c(q + 1), s(q + 1, 0);
  rep(i, 100000) s[0] += i * x[i];
  ll A, B, C;
  rep(i, q) {
    cin >> b[i] >> c[i];
    B = b[i];
    C = c[i];
    A = x[B];
    x[B] = 0;
    x[C] += A;
    s[i] = s[i - 1] + A * (C - B);
  }
  rep(i, q) cout << s[i] << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n, q;
  cin >> n;
  vector<int> a(n + 1);
  rep(i, n) cin >> a[i];
  cin >> q;
  vector<int> b(q + 1), c(q + 1), s(q + 1, 0);
  rep(i, q) {
    cin >> b[i] >> c[i];
    rep(j, n) {
      if (a[j] == b[i])
        a[j] = c[i];
      s[i] += a[j];
    }
    cout << s[i] << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

// Use valgrind once if the submission doesn't go through. It can detect
// uninitialized variables and see where they could be an issue.
#include <bits/stdc++.h>
using namespace std;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  vector<long long> a, b, c, count(100000, 0);
  long long sum = 0;
  int n, q;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  cin >> q;
  b.resize(q);
  c.resize(q);
  for (int i = 0; i < n; i++) {
    count[a[i] - 1]++;
  }
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
    sum += (c[i] - b[i]) * count[b[i] - 1];
    count[c[i] - 1] += count[b[i] - 1];
    count[b[i] - 1] = 0;
    cout << sum << '\\n';
  }
}


Error: 0

Fixed submission:

// Use valgrind once if the submission doesn't go through. It can detect
// uninitialized variables and see where they could be an issue.
#include <bits/stdc++.h>
using namespace std;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  vector<long long> a, b, c, count;
  long long sum = 0;
  int n, q;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  cin >> q;
  b.resize(q);
  c.resize(q);
  count.resize(*max_element(a.begin(), a.end()), 0);
  for (int i = 0; i < n; i++) {
    count[a[i] - 1]++;
  }
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
    sum += (c[i] - b[i]) * count[b[i] - 1];
    count[c[i] - 1] += count[b[i] - 1];
    count[b[i] - 1] = 0;
    cout << sum << '\\n';
  }
}