alexjercan/bugnet huggingface dataset:
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;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  ll s = 0;
  unordered_map<ll, ll> m;
  rep(i, n) {
    ll a;
    cin >> a;
    s += a;
    m[a]++;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    s += m[b] * (c - b);
    m[c] += m[b];
    m[b] = 0;
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  int n, q;
  int b, c;
  int s = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    for (int j = 0; j < n; j++) {
      if (a[j] == b) {
        a[j] = c;
        s += (c - b);
      }
    }
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#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>;

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

  rep(i, q) {
    sum += num[b[i]] * (c[i] - b[i]);
    num[c[i]] += num[b[i]];
    num[b[i]] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: 0

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>;

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

  rep(i, q) {
    sum += num[b[i]] * (c[i] - b[i]);
    num[c[i]] += num[b[i]];
    num[b[i]] = 0;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  int a;
  long long s = 0;
  vector<int> an(100001, 0);
  for (int i = 0; i < n; i++) {
    cin >> a;
    s += a;
    an[a]++;
  }

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

    s += an[b] * (c - b);
    an[c] += an[b];
    an[b] = 0;

    cout << s << endl;
  }
}

Error: 0

Fixed submission:

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

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

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

  int q;
  cin >> q;

  vector<int> b(q);
  vector<int> c(q);
  for (int i = 0; i < q; i++)
    cin >> b[i] >> c[i];

  long long s = accumulate(a.begin(), a.end(), 0);

  vector<int> an(10000, 0);
  for (int i = 0; i < n; i++)
    an[a[i]]++;

  for (int i = 0; i < q; i++) {
    s += an[b[i]] * (c[i] - b[i]);
    an[c[i]] += an[b[i]];
    an[b[i]] = 0;
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main(void) {
  ll n;
  cin >> n;

  map<ll, ll> mp;
  ll sum = 0;
  rep(i, n) {
    ll a;
    cin >> a;

    mp[a]++;
    sum += a;
  }

  ll q;
  cin >> q;

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

  rep(i, q) {
    sum += (mp[b[i]] * (c[i] - b[i]));
    mp[c[i]] += mp[b[i]];
    mp[b[i]] = 0;

    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main(void) {
  ll n;
  cin >> n;

  map<ll, ll> mp;
  rep(i, n) {
    ll a;
    cin >> a;

    mp[a]++;
  }

  ll q;
  cin >> q;

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

  rep(i, q) {
    mp[c[i]] += mp[b[i]];
    mp[b[i]] = 0;

    ll sum = 0;
    for (auto &tmp : mp) {
      sum += tmp.first * tmp.second;
    }
    cout << sum << endl;
  }

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

#include <iostream>
using namespace std;
#define ll long long

const ll N = 1e5 + 3;
ll m[N] = {0};

int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);

  ll n, q, n1, n2, sum, val;
  cin >> n;
  // int a[n];
  sum = 0;
  // int m[10001] = {0};
  for (int i = 0; i < n; i++) {
    cin >> val;
    sum = sum + val;
    m[val]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n1 >> n2;

    sum += (n2 - n1) * m[n1];
    cout << sum << "\\n";
    m[n2] += m[n1]; // if  not present or zero scenario
    m[n1] = 0;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  int n, q, n1, n2, sum;
  cin >> n;
  int a[n];
  sum = 0;
  int m[10001] = {0};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
    m[a[i]]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n1 >> n2;
    sum = sum + (n2 - n1) * m[n1];
    cout << sum << '\\n';
    m[n2] = m[n2] + m[n1]; // if  not present or zero scenario
    m[n1] = 0;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define rep(i, p) for (long long int i = 0; i < p; i++)
#define reep(i, p) for (long long int i = 1; i <= p; i++)
#define ll long long

using namespace std;

int main() {

  ll int N;
  ll int maxim = 0;
  cin >> N;
  vector<ll int> A(N);
  rep(i, N) {
    cin >> A[i];
    if (maxim < A[i]) {
      maxim = A[i];
    } else {
    }
  }
  ll int Q;
  cin >> Q;
  vector<ll int> B(Q);
  vector<ll int> C(Q);
  rep(i, Q) {
    cin >> B[i] >> C[i];
    if (maxim < C[i]) {
      maxim = C[i];
    } else {
    }
  }
  map<ll int, ll int> counter;
  rep(i, N) { counter[A[i]] += 1; }

  for (long long int j = 1; j <= maxim; j++) {
    if (counter[j] < 1) {
      counter[j] = 0;
    } else {
    }
  }

  /*
  for( long long int j=1;j<=4;j++){
          cout << j << " " <<  counter[j]<< endl;
  }
          cout << endl;
  */

  ll int ans = 0;
  for (long long int j = 1; j <= maxim; j++) {
    //	cout << j << " " <<  counter[j]*j<< endl;
    ans += counter[j] * j;
  }
  ll int tasu, hiku;
  for (long long int i = 0; i < Q; i++) {
    ans = ans + counter[B[i]] * C[i] - counter[B[i]] * B[i];
    counter[C[i]] += counter[B[i]];
    counter[B[i]] = 0;
    //	cout << 2 << " " <<  counter[2]1<< endl;

    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define rep(i, p) for (long long int i = 0; i < p; i++)
#define reep(i, p) for (long long int i = 1; i <= p; i++)
#define ll long long

using namespace std;

int main() {

  ll int N;
  ll int maxim = 0;
  cin >> N;
  vector<ll int> A(N);
  rep(i, N) {
    cin >> A[i];
    if (maxim < A[i]) {
      maxim = A[i];
    } else {
    }
  }
  ll int Q;
  cin >> Q;
  vector<ll int> B(Q);
  vector<ll int> C(Q);
  rep(i, Q) {
    cin >> B[i] >> C[i];
    if (maxim < C[i]) {
      maxim = C[i];
    } else {
    }
  }
  map<ll int, ll int> counter;
  rep(i, N) { counter[A[i]] += 1; }

  for (long long int j = 1; j <= maxim; j++) {
    if (counter[j] < 1) {
      counter[j] = 0;
    } else {
    }
  }

  /*
  for( long long int j=1;j<=4;j++){
          cout << j << " " <<  counter[j]<< endl;
  }
          cout << endl;
  */

  ll int ans = 0;

  for (long long int i = 0; i < Q; i++) {
    counter[C[i]] += counter[B[i]];
    counter[B[i]] = 0;
    //	cout << 2 << " " <<  counter[2]1<< endl;

    for (long long int j = 1; j <= maxim; j++) {
      //	cout << j << " " <<  counter[j]*j<< endl;
      ans += counter[j] * j;
    }

    cout << ans << endl;
    ans = 0;
  }

  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>;

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

  int q;
  cin >> q;
  rep(i, q) {
    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 <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>;

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];

  rep(i, q) {
    rep(j, n) {
      if (b[i] == a[j]) {
        a[j] = c[i];
      }
    }
    int sum = accumulate(a.begin(), a.end(), 0);
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  // your code goes here
  ll n;
  cin >> n;
  ll arr[100000];
  map<ll, ll> mp;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]]++;
  }
  for (auto i : mp)
    sum += i.first * i.second;

  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    mp[b] += mp[a];
    sum += (b - a) * mp[a];
    mp[a] = 0;
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll sum(ll arr[], ll n) {
  ll s = 0;
  for (ll i = 0; i < n; i++)
    s += arr[i];
  return s;
}
int main() {
  // your code goes here
  ll n;
  cin >> n;
  ll arr[100000];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll old_val = a;
    ll new_val = b;
    replace(arr, arr + n, old_val, new_val);
    cout << sum(arr, n) << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n + 1];
  int b[100005];
  long long ans = 0;
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]]++;
    ans += a[i];
  }
  int q, be, af;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> be >> af;
    ans += (af - be) * b[be];
    b[af] += b[be];
    b[be] = 0;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n + 1];
  int b[10005];
  long long ans = 0;
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]]++;
    ans += a[i];
  }
  int q, be, af;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> be >> af;
    ans += (af - be) * b[be];
    b[af] += b[be];
    b[be] = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  ll n, q;
  cin >> n;

  ve a(n + 5);

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

  int d[100005];
  ll p = 0;

  for (int i = 1; i <= 100000; i++) {
    d[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    d[a[i]] += 1;
    p += a[i];
  }

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

    int b, c;
    cin >> b >> c;

    p += d[b] * (c - b);

    d[c] += d[b];
    d[b] = 0;

    cout << p << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  ll n, q;
  cin >> n;

  ve a(n + 5);

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

  int d[100005];

  for (int i = 1; i <= 100000; i++) {
    d[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    d[a[i]] += 1;
  }

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

    d[c] += d[b];
    d[b] = 0;

    for (int i = 1; i <= 100000; i++) {
      p += d[i] * i;
    }

    cout << p << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF LLONG_MAX / 10
#define rep(i, n) for (int i = 0; i < n; i++)
#define krep(i, k, n, m) for (int i = k; i < n; i += m)
#define erep(i, k, n) for (int i = k; i < n; i++)
#define mod 1000000007
// 最大公約数
int gcd(int s, int t) {
  if (t == 0)
    return s;
  return gcd(t, s % t);
}
signed main() {
  int n;
  cin >> n;
  int m[100001];
  int a[n];
  int wkey = 0;
  rep(i, 100001) { m[i] = 0; }
  rep(i, n) {
    cin >> a[i];
    m[a[i]]++;
    wkey += a[i];
  }
  int w;
  cin >> w;
  int b[w], c[w];
  rep(i, w) { cin >> b[i] >> c[i]; }
  int ans;
  rep(i, w) {
    wkey += (c[i] - b[i]) * m[b[i]];
    ans = m[b[i]];
    m[c[i]] += ans;
    m[b[i]] = 0;
    cout << wkey << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF LLONG_MAX / 10
#define rep(i, n) for (int i = 0; i < n; i++)
#define krep(i, k, n, m) for (int i = k; i < n; i += m)
#define erep(i, k, n) for (int i = k; i < n; i++)
#define mod 1000000007
// 最大公約数
int gcd(int s, int t) {
  if (t == 0)
    return s;
  return gcd(t, s % t);
}
signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) { cin >> a[i]; }
  int w;
  cin >> w;
  int b[w], c[w];
  rep(i, w) { cin >> b[i] >> c[i]; }

  int ans = 0;
  rep(i, w) {
    rep(j, n) {
      ans = 0;
      if (a[j] == b[i]) {
        a[j] = c[i];
      }
    }
    rep(j, n) { ans += a[j]; }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
#define DESC std::greater<int>() // sort 3rd arg

int main() {
  const ll MAX = 1e5 + 1;

  ll n;
  cin >> n;
  vector<ll> a(MAX);
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    a[tmp]++;
  }
  ll tot = 0;
  for (ll j = 1; j < MAX; j++) {
    tot += a[j] * j;
  }

  ll q;
  cin >> q;

  REP(i, q) {
    int b, c;
    cin >> b >> c;

    // 変わり得る部分を一度totから引く
    tot -= b * a[b];
    tot -= c * a[c];

    // 置き換えの実行
    a[c] += a[b];
    a[b] = 0;

    // 変更分合計を変える <-　ここがポイント
    tot += b * a[b];
    tot += c * a[c];

    cout << tot << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
#define DESC std::greater<int>() // sort 3rd arg

int main() {
  const ll MAX = 1e5 + 1;

  ll n;
  cin >> n;
  vector<ll> a(MAX);
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    a[tmp]++;
  }
  ll q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  REP(i, q) {
    cin >> b[i];
    cin >> c[i];
  }

  REP(i, q) {
    ll ans = 0;
    if (a[b[i]] > 0) {

      ll num = a[b[i]];
      a[b[i]] = 0;
      a[c[i]] += num;
    }
    for (ll j = 1; j < MAX; j++) {
      ans += a[j] * j;
    }

    cout << ans << endl;
  }

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

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

ifstream fin("date.in");
ull freq[100005];

int main() {
  ull n, v[100005], q, x, y;
  ull sum = 0;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    sum += v[i];
    freq[v[i]]++;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;

    ull rem = freq[x];
    freq[x] = 0;
    sum -= x * rem;
    freq[y] += rem;
    sum += y * rem;

    cout << sum << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MAX 1000000000000001
using namespace std;

ifstream fin("date.in");
int freq[1000005];

int main() {
  int n, v[1000], q, x, y;
  ull sum = 0;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    sum += v[i];
    freq[v[i]]++;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;

    int rem = freq[x];
    sum -= x * rem;
    freq[y] += rem;
    sum += y * rem;

    cout << sum << "\\n";
  }

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

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

/*
100000
*/

const int MAX = 100000;

int main() {
  int n;
  cin >> n;
  vector<long long> x(100010);
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    x[a]++;
  }

  int q;
  cin >> q;
  int b[q + 1], c[q + 1];

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

  unsigned long long ans = 0;

  for (int i = 1; i <= MAX; i++) {
    ans += x[i] * i;
  }

  for (int j = 0; j < q; j++) {

    ans -= x[b[j]] * b[j];

    x[c[j]] += x[b[j]];

    ans += x[b[j]] * c[j];

    x[b[j]] = 0;

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

/*
100000
*/

const int MAX = 100000;

int main() {
  int n;
  cin >> n;
  vector<long long> x(100010);
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    x[a]++;
  }

  int q;
  cin >> q;
  int b[q + 1], c[q + 1];

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

  for (int j = 0; j < q; j++) {
    unsigned long long ans = 0;
    for (int i = 1; i <= MAX; i++) {
      if (b[j] == i) {
        x[c[j]] += x[b[j]];
        x[b[j]] = 0;
      }
    }

    for (int i = 1; i <= MAX; i++) {
      ans += x[i] * i;
      // cout << x[i] << " ";
    }

    cout << ans << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, Q;
  cin >> N;
  vector<int> A(N + 1);
  ll ans = 0;
  map<int, int> cnt;
  REP(i, N) {
    cin >> A[i];
    ans += A[i];
    cnt[A[i]]++;
  }
  cin >> Q;
  vector<int> B(Q + 1);
  vector<int> C(Q + 1);
  REP(i, Q) { cin >> B[i] >> C[i]; }
  REP(i, Q) {
    if (cnt[B[i]] != 0) {
      ans += (C[i] - B[i]) * cnt[B[i]];
      cnt[C[i]] += cnt[B[i]];
      cnt[B[i]] = 0;
      cout << ans << endl;

    } else {
      cout << ans << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, Q;
  cin >> N;
  vector<int> A(N + 1);
  ll ans = 0;
  map<int, int> cnt;
  REP(i, N) {
    cin >> A[i];
    ans += A[i];
    cnt[A[i]]++;
  }
  cin >> Q;
  vector<int> B(N + 1);
  vector<int> C(N + 1);
  REP(i, Q) { cin >> B[i] >> C[i]; }
  REP(i, Q) {
    if (cnt[B[i]] != 0) {
      ans += (C[i] - B[i]) * cnt[B[i]];
      cnt[C[i]] += cnt[B[i]];
      cnt[B[i]] = 0;
      cout << ans << endl;

    } else {
      cout << ans << endl;
    }
  }
}
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 (ll i = 0; i < (n); i++)

int main() {
  ll n, q;
  ll tmp;
  cin >> n;
  ll sum = 0;
  map<ll, ll> list;
  map<ll, ll>::iterator iter;
  map<ll, ll>::iterator iter2;

  rep(i, n) {
    cin >> tmp;
    if (iter = list.find(tmp); iter != end(list)) { // ある場合
      list[tmp]++;
      // std::cout << iter->second << std::endl;
    } else { // ない場合
      list.insert(make_pair(tmp, 1));
    }

    sum += tmp;
  }

  // rep(j,4){
  //             cout<<list[j+1]<<endl;
  //         }

  cin >> q;
  ll x, y;
  rep(i, q) {
    cin >> x >> y;
    if (iter = list.find(x); iter != end(list)) { // ある場合
      tmp = list[x];
      list[x] = 0;
      sum += (y - x) * tmp;
      if (iter2 = list.find(y); iter2 != end(list)) { // ある場合
        list[y] += tmp;
      } else { // ない場合
        list.insert(make_pair(y, tmp));
      }
    }

    cout << sum << endl;

    // cout<<"======"<<endl;

    // rep(j,4){
    //     cout<<list[j+1]<<endl;
    // }

    // cout<<"======"<<endl;
  }
}


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 (ll i = 0; i < (n); i++)

int main() {
  ll n, q;
  vector<ll> a;
  ll tmp;
  cin >> n;
  rep(i, n) {
    cin >> tmp;
    a.push_back(tmp);
  }
  cin >> q;
  ll x, y;
  rep(i, q) {
    cin >> x >> y;
    ll sum = 0;
    rep(j, a.size()) {
      if (a[j] == x)
        a[j] = y;
      sum += a[j];
    }
    cout << sum << endl;
  }
}