alexjercan/bugnet huggingface dataset:
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

vector<ll> x(100001, 0);
void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  ll sum = 0;

  rep(i, N) {
    x[A[i]]++;
    sum += A[i];
  }
  rep(i, Q) {
    ll y = C[i] - B[i];
    sum += y * x[B[i]];
    std::cout << sum << std::endl;
    x[C[i]] += x[B[i]];
    x[B[i]] = 0;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, std::move(A), Q, std::move(B), std::move(C));
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  ll sum = 0;
  vector<ll> x(N + 1, 0);
  rep(i, N) {
    x[A[i]]++;
    sum += A[i];
  }
  rep(i, Q) {
    ll y = C[i] - B[i];
    sum += y * x[B[i]];
    std::cout << sum << std::endl;
    x[C[i]] += x[B[i]];
    x[B[i]] = 0;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, std::move(A), Q, std::move(B), std::move(C));
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int n;
  scanf("%d", &n);

  ll sum = 0;
  int num[100001];
  memset(num, 0, sizeof(num));

  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
    sum += p[i];
    num[p[i]]++;
  }

  int q;
  scanf("%d", &q);

  int b, c;
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &b, &c);
    sum += num[b] * (c - b);
    num[c] += num[b];
    num[b] = 0;
    printf("%lld\\n", sum);
  }
}


Error: 0

Fixed submission:

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

int main(void) {
  ll n;
  scanf(" %lld", &n);

  ll sum = 0;
  int num[100001];
  for (int i = 0; i < 10001; i++)
    num[i] = 0;

  vector<char> p(n);
  for (ll i = 0; i < n; i++) {
    scanf(" %lld", &p[i]);
    sum += p[i];
    num[p[i]]++;
  }

  ll q;
  scanf(" %lld", &q);

  ll b, c;
  for (ll i = 0; i < q; i++) {
    scanf("%lld %lld", &b, &c);
    sum += num[b] * (c - b);
    num[c] += num[b];
    num[b] = 0;
    printf("%lld\\n", sum);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>

using namespace std;
using ll = long long;
ll gcd(ll a, ll b);
int main() {
  cin.tie(0);
  ll n, a, q, b, c;
  vector<ll> sum(100010);
  vector<ll> list(100010);
  sum[0] = 0;
  cin >> n;

  for (ll i = 1; i <= n; i++) {
    cin >> a;
    sum[0] += a;
    list[a]++;
  }
  cin >> q;

  for (ll i = 1; i <= q; i++) {
    cin >> b >> c;
    sum[i] = sum[i - 1];

    sum[i] += list[b] * (c - b);

    list[c] += list[b];
    list[b] = 0;
  }
  for (ll i = 1; i <= q; i++)
    cout << sum[i] << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>

using namespace std;
using ll = long long;
ll gcd(ll a, ll b);
int main() {
  cin.tie(0);
  ll n, a, q, b, c;
  vector<ll> sum, list(100010);
  sum[0] = 0;
  cin >> n;

  for (ll i = 1; i <= n; i++) {
    cin >> a;
    sum[0] += a;
    list[a]++;
  }
  cin >> q;
  sum[1] = sum[0];
  for (ll i = 1; i <= q; i++) {
    cin >> b >> c;

    sum[i] += list[b] * (c - b);

    list[c] += list[b];
    list[b] = 0;
  }
  for (ll i = 1; i <= q; i++)
    cout << sum[i] << endl;

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

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

int main(void) {
  int N, Q, P;
  long long sum = 0;
  cin >> N;
  vector<long long> X(100010);
  for (int i = 0; i < N; i++) {
    cin >> P;
    X.at(P - 1)++;
    sum += P;
  }
  cin >> Q;
  int B, C;

  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    sum += (C - B) * X.at(B - 1);
    cout << sum << endl;
    X.at(C - 1) += X.at(B - 1);
    X.at(B - 1) = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, Q, P;
  long long sum = 0;
  cin >> N;
  vector<long long> X(99999);
  for (int i = 0; i < N; i++) {
    cin >> P;
    X.at(P - 1)++;
    sum += P;
  }
  cin >> Q;
  int B, C;

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

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

typedef long long ll;

// see: https://drken1215.hatenablog.com/entry/2020/06/21/224900
int main() {
  int N;
  cin >> N;

  vector<int> counts(1000000, 0);

  ll sum = 0;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    // 数をメモっとく
    counts[tmp]++;
    sum += tmp;
  }

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    ll b, c;
    cin >> b >> c;

    sum += (c - b) * counts[b]; // 移動させる分、sum を差分更新
    counts[c] += counts[b];     // カウントを移動
    counts[b] = 0;              // 移動させたカウント削除
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int main() {
  int N;
  cin >> N;

  vector<int> counts(1000000, 0);

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    // 数をメモっとく
    counts[tmp]++;
    ans += tmp;
  }

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;

    for (int j = 0; j < N; j++) {
      if (counts[b] != 0) {
        int oldCounts = counts[b];
        counts[c] += oldCounts; // カウントを移動
        counts[b] = 0;          // カウント削除
        // ans を更新
        ans -= oldCounts * b;
        ans += oldCounts * c;
      }
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  map<int64_t, int64_t> m;
  for (int64_t i = 0; i < N; ++i) {
    cin >> A[i];
    m[A[i]] = m[A[i]] + 1;
  }
  int64_t sum = 0;
  for (auto p : m) {
    int64_t val = p.first;
    int64_t num = p.second;
    sum = sum + val * num;
  }
  int64_t Q;
  cin >> Q;
  vector<int64_t> B(Q), C(Q);
  for (int64_t i = 0; i < Q; ++i) {
    cin >> B[i] >> C[i];
    int64_t num = m[B[i]];
    m[B[i]] = 0;
    m[C[i]] = m[C[i]] + num;
    sum = sum - B[i] * num + C[i] * num;
    cout << sum << endl;
  }

  /*int64_t sum = 0;
  for ( auto p : m ){
      int64_t val = p.first;
      int64_t num = p.second;
      sum = sum + val * num;
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  map<int64_t, int64_t> m;
  for (int64_t i = 0; i < N; ++i) {
    cin >> A[i];
    m[A[i]] = m[A[i]] + 1;
  }
  int64_t Q;
  cin >> Q;
  vector<int64_t> B(Q), C(Q);
  for (int64_t i = 0; i < Q; ++i) {
    cin >> B[i] >> C[i];
    int64_t num = m[B[i]];
    m[B[i]] = 0;
    m[C[i]] = m[C[i]] + num;
    int64_t sum = 0;
    for (auto p : m) {
      int64_t val = p.first;
      int64_t num = p.second;
      sum = sum + val * num;
    }

    cout << sum << endl;
  }

  /*int64_t sum = 0;
  for ( auto p : m ){
      int64_t val = p.first;
      int64_t num = p.second;
      sum = sum + val * num;
  }

  cout << sum << endl;*/
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> num(100000);
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    sum += a;
    num[a]++;
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> num(n + 1);
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    sum += a;
    num[a]++;
  }

  int q = 0;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    std::cin >> b >> c;
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    std::cout << sum << std::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 all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MX = 100005;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX, 0);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll tot = 0;
  rep(i, MX) tot += i * cnt[i];

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  cin >> q;
  vector<P> b(q);
  rep(i, q) { cin >> b[i].first >> b[i].second; }

  rep(i, q) {
    ll ans = 0;
    rep(j, n) {
      if (a[j] == b[i].first) {
        a[j] = b[i].second;
      }
      ans += a[j];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <fstream>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

long long total[100005], N, Q;
long long sum;

int main() {
  cin >> N;

  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    sum += a;
    total[a]++;
  }
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    long long B, C;
    cin >> B >> C;
    sum -= B * total[B];
    sum += C * total[B];
    total[C] += total[B];
    total[B] = 0;
    cout << sum << "\\n";
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <fstream>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int N, Q;
long long sum;

int main() {
  cin >> N;
  vector<int> total(10005, 0);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    sum += a;
    total[a]++;
  }
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    sum -= B * total[B];
    sum += C * total[B];
    total[C] += total[B];
    total[B] = 0;
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int a, b, c, d[100001] = {0}, q;
  ll x = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    x += a;
    d[a]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    x += 1ll * (c - b) * d[b];
    cout << x << endl;
    d[c] += d[b];
    d[b] = 0;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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], m = 0;
  for (int i = 0; i < q; i++)
    cin >> b[i] >> c[i];
  for (int i = 0; i < q; i++) {
    m = 0;
    for (int j = 0; j < n; j++) {
      if (b[i] == a[j])
        a[j] = c[i];
      m += a[j];
    }
    cout << m << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf{int(1e9)};
int main(void) {
  int N;
  cin >> N;

  vector<ll> num(100001); // 値がiの要素が出てきた回数
  rep(i, N) {
    int A;
    cin >> A;
    num[A]++;
  }
  ll total = 0;
  rep(i, 100001) total += i * num[i];
  int Q;
  cin >> Q;

  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    total += num[B] * (C - B);
    // B_i全てをC_iに置き換える
    num[C] += num[B];
    num[B] = 0;
    cout << total << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf{int(1e9)};
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  int Q;
  cin >> Q;
  vector<int> B(N);
  vector<int> C(N);
  rep(i, Q) cin >> B[i] >> C[i];

  vector<int> num(100001); // 値がiの要素が出てきた回数
  rep(i, N) { num[A[i]]++; }
  ll ans = accumulate(A.begin(), A.end(), 0);
  rep(i, Q) {
    ans += (C[i] - B[i]) * num[B[i]];
    // B_i全てをC_iに置き換える
    num[C[i]] += num[B[i]];
    num[B[i]] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n, i, j, k, q, p, b;
  cin >> n;
  long long sum = 0;
  long long freq[100005];
  memset(freq, 0, sizeof(freq));
  long long a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    freq[a[i]]++;
  }
  cin >> q;
  while (q--) {
    cin >> p >> b;
    if (freq[p] > 0) {
      sum -= (p * freq[p]);

      freq[b] += freq[p];
      sum += (b * freq[p]);
      freq[p] = 0;
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n, i, j, k, q, p, b;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> q;
  while (q--) {
    cin >> p >> b;
    long long sum = 0;
    for (i = 0; i < n; i++) {
      if (a[i] == p)
        a[i] = b;
      sum += a[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using namespace std;

ll print_map_total(map<ll, ll> a) {
  ll total = 0;
  for (auto i = a.begin(); i != a.end(); ++i) {
    total += (i->first) * (i->second);
  }

  return total;
}

int main() {
  int n;
  cin >> n;
  map<ll, ll> a;

  int temp;
  rep(i, n) {
    cin >> temp;
    a[temp]++;
  }

  int q;
  cin >> q;
  vector<ll> b(q), c(q);
  rep(i, q) {
    cin >> b[i];
    cin >> c[i];
  }

  ll total = print_map_total(a);

  int henka = 0;
  rep(i, q) {
    // 消える数の個数を取得
    henka = a[b[i]];
    // 消す数の個数をCに追加
    a[c[i]] += henka;
    // 合計からb*(bの要素数)を引く
    total -= b[i] * henka;
    // 合計にC*(Cの要素数)を足す
    total += c[i] * henka;
    // bを削除
    a.erase(b[i]);
    cout << total << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using namespace std;

void print_map_total(map<ll, ll> a) {
  ll total = 0;
  for (auto i = a.begin(); i != a.end(); ++i) {
    total += (i->first) * (i->second);
  }
  cout << total << endl;
}

int main() {
  int n;
  cin >> n;
  map<ll, ll> a;

  int temp;
  rep(i, n) {
    cin >> temp;
    a[temp]++;
  }

  int q;
  cin >> q;
  vector<ll> b(q), c(q);
  rep(i, q) {
    cin >> b[i];
    cin >> c[i];
  }

  int henka = 0;
  rep(i, q) {
    henka = a[b[i]];
    a[b[i]] = 0;
    a[c[i]] += henka;
    print_map_total(a);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define unmap unordered_map<int, int>
#define unset unordered_set<int>
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define ll long long
#define ull unsigned ll
#define ff first
#define ss second // typedef second ss;
#define foton(i, n) for (int i = 0; i < n; i++)
#define faton(i, a, n) for (int i = a; i <= n; i++)
#define mod 1000000007
#define endl "\\n"
using namespace std;
void solve() {
  int n;
  cin >> n;
  int ar[n];
  ll sum = 0;
  unmap mp;
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    sum += ar[i];
    mp[ar[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    if (!mp[a]) {
      cout << sum << endl;
      continue;
    }
    int x = mp[a];
    sum -= 1ll * x * a;
    sum += 1ll * x * b;
    mp[a] = 0;
    mp[b] += x;
    cout << sum << endl;
  }
}

int main() {
  fastio;
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define unmap unordered_map<int, int>
#define unset unordered_set<int>
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define ll long long
#define ull unsigned ll
#define ff first
#define ss second // typedef second ss;
#define foton(i, n) for (int i = 0; i < n; i++)
#define faton(i, a, n) for (int i = a; i <= n; i++)
#define mod 1000000007
#define endl "\\n"
using namespace std;
void power(int a, int n) {
  ull ans = 1;
  while (n > 0) {
    if (n % 2) {
      ans *= a;
      n--;
      cout << ans << endl;
    } else {
      a *= a;
      n /= 2;
    }
  }
  cout << ans << endl;
}
void solve() {
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    sort(ar, ar + n);
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (ar[i] == a)
        ar[i] = b;
      sum += ar[i];
    }
    cout << sum << endl;
  }
}

int main() {
  fastio;
  solve();
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define ll long long

int main() {
  ll n;
  std::cin >> n;
  ll a[n], store[1000000];
  for (ll i = 0; i < 1000000; i++)
    store[i] = 0;
  ll t = 0;
  for (ll i = 1; i <= n; i++) {
    std::cin >> a[i];
    store[a[i]]++;
    t += a[i];
  }
  ll q;
  std::cin >> q;
  while (q--) {
    ll b, c;
    std::cin >> b >> c;
    t -= (store[b] * b);
    t += (store[b] * c);
    store[c] += store[b]; // updating c frequency
    store[b] = 0;         // updating b frequency
    std::cout << t << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define ll long long

int main() {
  ll n;
  std::cin >> n;
  ll a[n];
  for (int i = 1; i <= n; i++)
    std::cin >> a[i];
  ll q;
  std::cin >> q;
  while (q--) {
    ll b, c;
    std::cin >> b >> c;
    ll t = 0;
    for (ll i = 1; i <= n; i++) {
      if (a[i] == b) {
        a[i] = c;
      }
      t += a[i];
    }
    std::cout << t << std::endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[100010];
int cnt[100010];

int main() {
  long long n, q, b, c, s = 0;

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;

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

  cin >> q;
  while (q--) { // q operations
    cin >> b >> c;
    long long tmp = cnt[b] * (b - c);
    s = s - tmp;
    cnt[c] += cnt[b];
    cnt[b] = 0;

    cout << s << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int a[100010];
int binarySearch(int n, int number[], int find) {
  int low = 0;
  int upper = n - 1;
  while (low <= upper) {
    int mid = (low + upper) / 2;
    if (number[mid] < find)
      low = mid + 1;
    else if (number[mid] > find)
      upper = mid - 1;
    else
      return mid;
  }
  return -1;
}

int main() {
  long long n, q, b, c;

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;

  for (long long i = 0; i < n; i++)
    cin >> a[i];

  cin >> q;
  while (q--) { // q operations
    cin >> b >> c;
    long long s = 0;
    for (long long i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
      s += a[i];
    }
    cout << s << endl;
  }

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

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
long long int a[100000];
int main() {
  long long int n, i, x, y, t, s, d;
  scanf("%lld", &n);
  s = 0;
  for (i = 0; i < n; i++) {
    scanf("%lld", &d);
    a[d]++;
    s += d;
  }
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld", &x, &y);
    a[y] += a[x];
    s += (y - x) * a[x];
    a[x] = 0;
    printf("%lld\\n", s);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, a[100000], i, x, y, t, s;
  // cin>>n;
  scanf("%lld", &n);
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  // cin>>a[i];
  // cin>>t;
  scanf("%lld", &t);
  while (t--) {
    // cin>>x>>y;
    scanf("%lld%lld", &x, &y);
    s = 0;
    for (i = 0; i < n; i++) {
      if (a[i] == x)
        a[i] = y;

      s += a[i];
    }
    // cout<<s<<endl;
    printf("%lld\\n", s);
  }
}