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

#include <bits/stdc++.h>
#define vi vector<int>
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define sortrev(x) sort(all(x), greater<int>())
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define int long long
#define ld long double
#define endl "\\n";
#define mp make_pair
#define pii <int, int>
#define lb lower_bound
#define ub upper_bound
#define len(a) int((a).size())
#define prec(n) fixed << setprecision(n)
#define cnt(n) __builtin_popcountll(n)
#define pi pair<int, int>
const int mod = 1000000007;
using namespace std;
// const int N = 1e5 + 5;
// int n;
// int a[N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t;
  t = 1;
  // cin >>t;
  while (t--) {

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define sortrev(x) sort(all(x), greater<int>())
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define int long long
#define ld long double
#define endl "\\n";
#define lb lower_bound
#define ub upper_bound
const int mod = 1000000007;
using namespace std;
// const int N = 1e5 + 5;
// int n;
// int a[N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t;
  t = 1;
  // cin >>t;
  while (t--) {
    int n;
    cin >> n;
    vi v;

    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      v.pb(a);
    }

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

      replace(v.begin(), v.end(), b, c);
      // auto it = std::find (v.begin(), v.end(), b);
      // if(it != v.end()){
      //    // no element found

      // }
      // v[*it] = c;
      // for(auto x: v)
      // cout << x <<" ";
      cout << accumulate(v.begin(), v.end(), 0) << endl;
    }
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, arr[100005];
  long long sum = 0;
  map<int, int> mps;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    mps[arr[i]]++;
    sum += arr[i];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    if (!mps[b]) {
      printf("%lld\\n", sum);
      continue;
    }
    int x = mps[b];
    sum -= (long long)x * b;
    sum += (long long)x * c;
    mps[c] += x;
    mps[b] = 0;
    printf("%lld\\n", sum);
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n, arr[10005];
  long long sum = 0;
  map<int, int> mps;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    mps[arr[i]]++;
    sum += arr[i];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    if (!mps[b]) {
      printf("%lld\\n", sum);
      continue;
    }
    int x = mps[b];
    sum -= (x * b);
    sum += (x * c);
    mps[c] += x;
    mps[b] = 0;
    printf("%lld\\n", sum);
  }

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

#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>

#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;

int MAP[100001] = {-1};

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> freq;
  int tmp;
  uint64_t sum = 0;
  rep(i, N) {
    cin >> tmp;
    freq[tmp]++;
    sum += tmp;
  }

  int Q;
  cin >> Q;
  int B, C;
  rep(i, Q) {
    cin >> B >> C;
    sum -= B * freq[B];
    sum += C * freq[B];

    freq[C] += freq[B];
    freq[B] = 0;
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>

#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;

int MAP[100001] = {-1};

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> freq;
  int tmp;
  rep(i, N) {
    cin >> tmp;
    freq[tmp]++;
  }

  int Q;
  cin >> Q;

  int B, C;
  rep(i, Q) {
    cin >> B >> C;
    freq[C] += freq[B];
    freq[B] = 0;
    uint64_t ret = 0;
    for (auto itr : freq) {
      ret += itr.first * itr.second;
    }
    cout << ret << endl;
  }

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

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

int main() {
  int N;
  cin >> N;
  map<ll, ll> A;
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    A[a]++;
  }
  int Q;
  cin >> Q;
  vector<ll> S(Q);
  ll b, c;
  ll changed = 0;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    if (A.count(b)) {
      A[c] += A[b];
      changed = (c - b) * A[b];
      A.erase(b);
    } else {
      changed = 0;
    }
    if (i == 0) {
      ll s = 0;
      for (pair<ll, ll> kv : A) {
        s += kv.first * kv.second;
      }
      S.at(i) = s;
    } else {
      S.at(i) = S.at(i - 1) + changed;
    }
  }

  for (int i = 0; i < Q; i++) {
    cout << S.at(i) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  map<ll, ll> A;
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    A[a]++;
  }
  int Q;
  cin >> Q;
  vector<ll> S(Q);
  ll b, c;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    if (A.count(b)) {
      A[c] += A[b];
    }
    A.erase(b);
    ll s = 0;
    for (pair<ll, ll> kv : A) {
      s += kv.first * kv.second;
    }
    S.at(i) = s;
  }

  for (int i = 0; i < Q; i++) {
    cout << S.at(i) << endl;
  }

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

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

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  long long ans = accumulate(A.begin(), A.end(), 0LL);
  map<long long, long long> mp;
  for (auto a : A)
    mp[a] += 1;
  for (int i = 0; i < Q; ++i) {
    ans = ans - B[i] * mp[B[i]] + C[i] * mp[B[i]];
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    cout << ans << endl;
  }
  return;
}

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: TLE

Fixed submission:

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

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  map<long long, long long> mp;
  for (auto a : A)
    mp[a] += 1;
  for (int i = 0; i < Q; ++i) {
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    long long ans = 0;
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
      ans += itr->first * itr->second;
    }
    cout << ans << endl;
  }
  return;
}

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: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cmath>
#include <math.h>
#include <stdio.h>
using namespace std;
#define vsort(vec) sort(vec.begin(), vec.end())
#define vreverse(vec) reverse(vec.begin(), vec.end())
using ll = long long;
using ull = unsigned long long;
const ll INF = 1001001001;

// 10億7。　　　100000007

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
// 最小公倍数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  cout << fixed << setprecision(10);
  // cin.tie(0); ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<ll> vec(N);
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> vec[i];
    sum += vec[i];
  }

  ll Q;
  cin >> Q;
  vector<ll> vecB(Q);
  vector<ll> vecC(Q);
  for (int i = 0; i < Q; i++) {
    cin >> vecB[i] >> vecC[i];
  }

  vector<int> vecN(100000);
  for (int i = 0; i < 100000; i++) {
    vecN[i] = 0;
  }
  vsort(vec);
  for (int i = 0; i < N; i++) {
    vecN[vec[i]]++;
  }

  for (int i = 0; i < Q; i++) {

    sum = sum - vecN[vecB[i]] * vecB[i] + vecN[vecB[i]] * vecC[i];
    cout << sum << endl;
    vecN[vecC[i]] += vecN[vecB[i]];
    vecN[vecB[i]] = 0;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <math.h>
#include <stdio.h>
using namespace std;
#define vsort(vec) sort(vec.begin(), vec.end())
#define vreverse(vec) reverse(vec.begin(), vec.end())
using ll = long long;
using ull = unsigned long long;
const ll INF = 1001001001;

// 10億7。　　　100000007

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
// 最小公倍数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  cout << fixed << setprecision(10);
  // cin.tie(0); ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<ll> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec[i];
  }

  ll Q;
  cin >> Q;
  vector<ll> vecB(Q);
  vector<ll> vecC(Q);
  for (int i = 0; i < Q; i++) {
    cin >> vecB[i] >> vecC[i];
  }

  for (int i = 0; i < Q; i++) {
    ll sum = 0;
    for (int j = 0; j < N; j++) {
      if (vec[j] == vecB[i]) {
        vec[j] = vecC[i];
      }
      sum += vec[j];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define RAP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

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

// 重複順列
vector<ll> buf;
void permutation(ll i, const ll size, const ll range_start,
                 const ll range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    for (ll itr : buf) {
      cout << itr;
    }
  } else {
    for (ll j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      permutation(i + 1, size, range_start, range_end);
    }
  }
}

int main(void) {
  ll n, a, q, b, c, ans = 0, changeB, changeC;
  unordered_map<ll, ll> mp;
  cin >> n;
  RAP(i, n) {
    cin >> a;
    mp[a]++;
  }
  cin >> q;
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    ans += itr->first * itr->second;
  }
  RAP(i, q) {
    cin >> b >> c;
    changeB = b * mp[b];
    changeC = c * mp[b];
    ans = ans - changeB + changeC;
    mp[c] += mp[b];
    mp[b] = 0;
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define RAP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

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

// 重複順列
vector<ll> buf;
void permutation(ll i, const ll size, const ll range_start,
                 const ll range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    for (ll itr : buf) {
      cout << itr;
    }
  } else {
    for (ll j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      permutation(i + 1, size, range_start, range_end);
    }
  }
}

int main(void) {
  ll n, a, q, b, c, ans = 0;
  unordered_map<ll, ll> mp;
  cin >> n;
  RAP(i, n) {
    cin >> a;
    mp[a]++;
  }
  cin >> q;
  RAP(i, q) {
    ans = 0;
    cin >> b >> c;
    mp[c] += mp[b];
    mp[b] = 0;
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
      ans += itr->first * itr->second;
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

int max1 = -1e9;

using namespace std;

int main(int argc, const char *argv[]) {

  std::ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  cin.tie(NULL);
  int n;
  cin >> n;
  long long soma = 0;
  vector<int> lista(10e5 + 10);
  for (int i = 0; i < n; i++) {
    int aux;
    cin >> aux;
    lista[aux] += 1;
    soma += aux;
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int aux, aux1;
    cin >> aux >> aux1;
    int tmp;
    int val1;
    int val2;
    val1 = aux * lista[aux];
    val2 = aux1 * lista[aux];
    int val = val2 - val1;
    soma += val;
    tmp = lista[aux];
    lista[aux] = 0;
    lista[aux1] += tmp;

    cout << soma << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

int max1 = -1e9;

using namespace std;

int main(int argc, const char *argv[]) {

  std::ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> lista(10e5 + 10);
  for (int i = 0; i < n; i++) {
    int aux;
    cin >> aux;
    lista[aux] += 1;
    max1 = max(max1, aux);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int aux, aux1;
    cin >> aux >> aux1;
    int tmp;
    tmp = lista[aux];
    lista[aux] = 0;
    lista[aux1] += tmp;
    long long soma = 0;
    for (int j = 0; j <= 10e5 + 5; j++) {
      int val = lista[j] * j;
      soma += val;
    }
    cout << soma << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  map<int, int> A;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    A[a] += 1;
  }
  long sum = 0;
  for (auto x : A) {
    sum += x.first * x.second;
  }

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

    sum += A[b] * (c - b);
    A[c] += A[b];
    A.erase(b);

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  map<int, int> A;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    A[a] += 1;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    A[c] += A[b];
    A.erase(b);
    long sum = 0;
    for (auto x : A) {
      sum += x.first * x.second;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

const int MX = 100005;

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

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using L = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a.at(i);
  }
  int q;
  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  for (int i = 0; i < q; ++i) {
    cin >> b.at(i);
    cin >> c.at(i);
  }
  for (int i = 0; i < q; ++i) {
    replace(a.begin(), a.end(), b[i], c[i]);
    int sum = 0;
    sum = accumulate(a.begin(), a.end(), 0);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i > ll(b); i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
#define INF 1000000000000
#define MOD 1000000007
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define Umap unordered_map
#define Uset unordered_set

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N, Q;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  cin >> Q;

  ll sum = 0;
  vector<ll> kosu(2 + 10e+5, 0);
  FOR(i, 0, N) {
    kosu[A[i]]++;
    sum += A[i];
  }

  REP(i, Q) {
    ll B, C;
    cin >> B >> C;
    sum += (C - B) * kosu[B];
    cout << sum << endl;
    kosu[C] += kosu[B];
    kosu[B] = 0;
  }

  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i > ll(b); i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
#define INF 1000000000000
#define MOD 1000000007
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define Umap unordered_map
#define Uset unordered_set

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N, Q;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  cin >> Q;
  vector<int> B(Q);
  vector<int> C(Q);
  REP(i, Q) cin >> B[i] >> C[i];

  vector<ll> kosu(N + 1, 0);
  FOR(i, 0, N) { kosu[A[i]]++; }
  ll sum = accumulate(A.begin(), A.end(), 0);

  REP(i, Q) {
    ll diff = C[i] - B[i];
    sum += diff * kosu[B[i]];
    cout << sum << endl;
    kosu[C[i]] += kosu[B[i]];
    kosu[B[i]] = 0;
  }

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

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

typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, ll> m;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (!m.count(A.at(i))) {
      m[A.at(i)] = 1;
    } else {
      m[A.at(i)]++;
    }
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }
  ll num = 0;
  for (pair<int, ll> p : m) {
    num += p.first * p.second;
  }
  for (int i = 0; i < Q; i++) {
    if (m.count(B.at(i))) {
      num += (C.at(i) - B.at(i)) * m[B.at(i)];
      if (!m.count(C.at(i))) {
        m[C.at(i)] = 0;
      }
      m[C.at(i)] += m[B.at(i)];
      m.erase(B.at(i));
    }
    cout << num << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, ll> m;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (!m.count(A.at(i))) {
      m[A.at(i)] = 1;
    } else {
      m[A.at(i)]++;
    }
  }
  int Q;
  cin >> Q;
  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }
  ll num;
  for (int i = 0; i < Q; i++) {
    if (m.count(B.at(i))) {
      if (!m.count(C.at(i))) {
        m[C.at(i)] = 0;
      }
      m[C.at(i)] += m[B.at(i)];
      m.erase(B.at(i));
    }
    num = 0;
    for (pair<int, ll> p : m) {
      num += p.first * p.second;
    }
    cout << num << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, Q, B, C, T;
  long long Sum = 0;
  cin >> N;
  vector<int> A(100001, 0);
  rep(i, N) {
    cin >> T;
    A.at(T)++;
    Sum += T;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    Sum += A.at(B) * (C - B);
    A.at(C) += A.at(B);
    A.at(B) = 0;
    cout << Sum << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int N, Q, B, C, T;
  long long Sum = 0;
  cin >> N;
  vector<int> A(100000, 0);
  rep(i, N) {
    cin >> T;
    A.at(T)++;
    Sum += T;
  }
  cin >> Q;
  rep(i, Q) {
    cin >> B >> C;
    Sum += A.at(B) * (C - B);
    A.at(C) += A.at(B);
    A.at(B) = 0;
    cout << Sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {

  ll n;
  cin >> n;
  map<ll, ll> mp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ll v;
    cin >> v;
    mp[v]++;
    sum += v;
  }
  ll q;
  cin >> q;
  vector<ll> ans(q, 0);
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    if (mp[b] != 0) {
      sum -= b * mp[b];
      sum += c * mp[b];
      mp[c] += mp[b];
      mp[b] = 0;
    }
    ans[i] = sum;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {

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