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

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

#define ll long long
#define mtv(kit) cout << #kit << " - " << kit << "\\n";
#define ff first
#define ss second
#define pb push_back
#define rep(i, a, b) for (i = a; i < b; i++)
#define pii pair<ll, ll>
#define nl "\\n"
#define ump unordered_map<ll, ll>
const ll N = 1e5 + 5;
ump freq;
void doit() {
  freq.clear();
  ll n, m, i, j, k, c, ans = 0, x;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    freq[x]++;
    ans += x;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll c1, c2, c3;
    cin >> c1 >> c2;
    k = freq[c1];
    freq[c1] = 0;
    c1 = k * c1;
    ans -= c1;
    ans += c2 * k;
    freq[c2] += k;
    cout << ans << nl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  for (ll i = 1; i <= t; i++) {
    doit();
  }
}


Error: -11

Fixed submission:

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

#define ll long long
#define mtv(kit) cout << #kit << " - " << kit << "\\n";
#define ff first
#define ss second
#define pb push_back
#define rep(i, a, b) for (i = a; i < b; i++)
#define pii pair<ll, ll>
#define nl "\\n"
#define ump unordered_map<ll, ll>
const ll N = 1e5 + 5;
ll freq[N];
void doit() {
  ll n, m, i, j, k, c, ans = 0, x;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    freq[x]++;
    ans += x;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll c1, c2;
    cin >> c1 >> c2;
    k = freq[c1];
    c1 = freq[c1] * c1;
    freq[c1] = 0;
    ans -= c1;
    ans += c2 * k;
    freq[c2] += k;
    cout << ans << nl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  for (ll i = 1; i <= t; i++) {
    doit();
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int N;
  cin >> N;
  long count[100001] = {0};
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    count[a]++;
  }
  long long tot = 0;
  for (int j = 1; j <= 1e5; j++) {
    tot += count[j] * j;
  }
  int Q;
  cin >> Q;
  int b, c;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    count[c] += count[b];
    tot = tot - b * count[b] + c * count[b];
    count[b] = 0;
    cout << tot << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  int count[100001] = {0};
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    count[a]++;
  }
  int Q;
  cin >> Q;
  int b, c, tmp;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    count[c] += count[b];
    count[b] = 0;
    long long tot = 0;
    for (int j = 0; j < 100001; j++) {
      tot += count[j] * j;
    }
    cout << tot << endl;
  }

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\\n"
#define space " "
#define ll long long int
#define vl vector<ll>
#define vvl vector<vll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define mod 1000000007
#define pi 3.1415926535897932384626433
#define inf 1000000000000000001;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define pb push_back
#define f first
#define s second

void solve() {
  int n;
  cin >> n;
  int a[n];
  std::map<int, int> m;
  ll s = 0;
  for (int i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
    s += a[i];
    m[a[i]]++;
  }
  int q;
  cin >> q;
  int b[q], c[q];
  for (int i = 0; i < q; ++i) {
    /* code */
    int d = 0;
    cin >> b[i] >> c[i];
    if (c[i] > b[i]) {
      int p = c[i] - b[i];
      int e = m[b[i]];
      m[b[i]] = 0;
      m[c[i]] += e;
      int f = e * p;
      s += f;
    } else {
      int p = b[i] - c[i];
      int e = m[b[i]];
      m[b[i]] = 0;
      m[c[i]] += e;
      int f = e * p;
      s -= f;
    }
    cout << s << endl;
  }
}

signed main() {

  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\\n"
#define space " "
#define ll long long int
#define vl vector<ll>
#define vvl vector<vll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define mod 1000000007
#define pi 3.1415926535897932384626433
#define inf 1000000000000000001;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define pb push_back
#define f first
#define s second

void solve() {
  int n;
  cin >> n;
  int a[n];
  std::map<int, int> m;
  for (int i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
    m[a[i]]++;
  }
  int q;
  cin >> q;
  int b[q], c[q];
  for (int i = 0; i < q; ++i) {
    /* code */
    cin >> b[i] >> c[i];
    ll s = 0;
    for (int j = 0; j < n; ++j) {
      /* code */
      if (a[j] == b[i])
        a[j] = c[i];
      s += a[j];
    }
    cout << s << endl;
  }
}

signed main() {

  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <string.h>
#define ll long long
using namespace std;
struct value {
  ll x;
  ll y;
};
bool compare(value a, value b) {
  // for descending order replace with a.roll >b.roll
  if (a.y < b.y)
    return 1;
  else
    return 0;
}
int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  vector<ll> v(n);
  map<ll, ll> m;
  for (auto &i : v) {
    cin >> i;
    sum = sum + i;
    m[i]++;
  }
  ll q;
  ll b, c;
  cin >> q;
  while (q--) {
    cin >> b >> c;
    if (m.find(b) != m.end()) {
      if (c >= b)
        sum += m[b] * abs(b - c);
      else
        sum -= m[b] * abs(b - c);
      m[c] += m[b];
      m[b] = 0;
    }
    cout << sum << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <string.h>
#define ll long long
using namespace std;
struct value {
  ll x;
  ll y;
};
bool compare(value a, value b) {
  // for descending order replace with a.roll >b.roll
  if (a.y < b.y)
    return 1;
  else
    return 0;
}
int main() {
  ll n;
  cin >> n;
  vector<ll> arr;
  ll q, temp;

  ll sum;
  for (ll i = 0; i < n; i++) {
    cin >> temp;
    arr.push_back(temp);
  }
  cin >> q;
  ll b, c;
  for (ll i = 0; i < q; i++) {
    sum = 0;
    cin >> b >> c;
    for (ll i = 0; i < n; i++) {
      if (arr[i] == b)
        arr[i] = c;
    }
    for (ll i = 0; i < n; i++) {
      sum = sum + arr[i];
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    int pos1 = lower_bound(a, a + n, x) - a;
    int pos2 = upper_bound(a, a + n, x) - a;
    // cout<<pos2-pos1<<endl;
    cout << sum - (pos2 - pos1) * x + (pos2 - pos1) * y << endl;
    sum = sum - (pos2 - pos1) * x + (pos2 - pos1) * y;
    fill(a + pos1, a + pos2, y);
    // for(int i=0;i<n;i++) cout<<a[i]<<" ";
    sort(a, a + n);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fr first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const long long N = 1e5 + 7;
string s, s1;
ll n, m, k, cnt;
int main() {
  cin >> n;
  ll arr[n];
  map<ll, ll> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i], cnt += arr[i];
    mp[arr[i]]++;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    ll a, b, ans = 0;
    cin >> a >> b;
    ans = mp[a] * b - mp[a] * a;
    cnt += ans;
    mp[b] += mp[a];
    mp[a] = 0;
    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fr first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const long long N = 1e5 + 7;
string s, s1;
ll n, m, k, cnt;
int main() {
  cin >> n;
  ll arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cin >> m;
  for (int i = 0; i < m; i++) {
    ll a, b, cnt = 0;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
      if (arr[i] == a)
        arr[i] = b;
      cnt += arr[i];
    }
    cout << cnt << endl;
    cnt = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

// AUTHOR : Kishan Srivastav
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define ios                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  ios
#ifndef ONLINE_JUDGE
      in;
  out;
#endif
  ll n, m[100001], a, b;
  cin >> n;
  memset(m, 0, sizeof(100001));

  ll arr[100001];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    m[arr[i]]++;
    sum += arr[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    ll cntr = m[a];
    sum -= (cntr * a);
    sum += (cntr * b);

    m[a] -= cntr;
    m[b] += cntr;

    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

// AUTHOR : Kishan Srivastav
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define ios                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  ios
#ifndef ONLINE_JUDGE
      in;
  out;
#endif
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    ll sum = 0;
    cin >> a >> b;
    replace(arr, arr + n, a, b);
    for (int i = 0; i < n; ++i) {
      sum += arr[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define fr first
#define sec second
#define rep(i, n) for (i = 0; i < n; i++)
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vb vector<bool>
#define pb push_back
#define M 1000000007
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define open freopen("input.txt", "r", stdin);
#define close freopen("output.txt", "w", stdout);
#define M_PI 3.14159265358979323846
#define pp pair<int, int>
#define nn 100003

int main() {
  ios;
  ll n, i, q, b, c, sum = 0;
  map<ll, ll> mp;
  cin >> n;
  ll a[n];
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  cin >> q;
  rep(i, q) {
    ll currsum;
    cin >> b >> c;
    if (mp.find(b) != mp.end()) {
      ll x = mp[b];
      currsum = sum - x * b + x * c;
      sum = currsum;
      mp.erase(b);
      if (mp.find(c) != mp.end())
        mp[c] += x;
      else
        mp[c] = x;
    } else
      currsum = sum;
    cout << currsum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define fr first
#define sec second
#define rep(i, n) for (i = 0; i < n; i++)
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vb vector<bool>
#define pb push_back
#define M 1000000007
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define open freopen("input.txt", "r", stdin);
#define close freopen("output.txt", "w", stdout);
#define M_PI 3.14159265358979323846
#define pp pair<int, int>
#define nn 100003

int main() {
  ios;
  ll n, i, q, b, c, sum = 0;
  map<ll, ll> mp;
  cin >> n;
  ll a[n];
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  cin >> q;
  rep(i, q) {
    ll currsum;
    cin >> b >> c;
    if (mp.find(b) != mp.end()) {
      ll x = mp[b];
      currsum = sum - x * b + x * c;
      sum = currsum;
      replace(a, a + n, b, c);
      mp.erase(b);
      if (mp.find(c) != mp.end())
        mp[c] += x;
      else
        mp[c] = x;
    } else
      currsum = sum;
    cout << currsum << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  map<long long, int> m;

  int n, q;
  cin >> n;

  int a[n];

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

  long long sum = 0;

  for (auto it : m) {
    sum += (it.first * it.second);
  }

  cin >> q;

  while (q--) {
    int a, b;

    cin >> a >> b;

    sum += m[a] * (b - a);

    m[b] += m[a];
    m[a] = 0;

    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  map<long long, int> m;

  int n, q;
  cin >> n;

  int a[n];

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

  while (q--) {
    int a, b;

    cin >> a >> b;

    long long sum = 0;

    int x = m[a];
    m[a] = 0;
    m[b] += x;

    for (auto it : m) {
      sum += (it.first * it.second);
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, q;
  cin >> n;
  vector<int> A(n);
  vector<int> num(100001, 0);
  rep(i, n) {
    cin >> A[i];
    num[A[i]]++;
  }
  cin >> q;
  vector<int> B(q);
  vector<int> C(q);
  rep(i, q) { cin >> B[i] >> C[i]; }

  ll sum = 0;
  rep(i, n) { sum += A[i]; }

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

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int 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<int> sum(q);
  rep(j, q) {
    rep(i, n) {
      if (A[i] == B[j]) {
        A[i] = C[j];
      }
      sum[j] += A[i];
    }
    cout << sum[j] << endl;
  }

  rep(i, n) {}
  return 0;
}
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++)
#define srt(i) sort(i.begin(), i.end())
#define rvt(i) sort(i.begin(), i.end(), greater<int>())

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

  long sum = 0;
  vector<int> a(100001);
  rep(i, n) {
    int tmp;
    cin >> tmp;
    a.at(tmp)++;
    sum += tmp;
  }

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

    int cnt = a.at(b);
    sum = sum + cnt * (c - b);
    cout << sum << endl;

    a.at(c) += a.at(b);
    a.at(b) = 0;
  }
}


Error: 0

Fixed submission:

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

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

  long sum = 0;
  vector<int> a(100000);
  rep(i, n) {
    int tmp;
    cin >> tmp;
    a.at(tmp)++;
    sum += tmp;
  }

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

    int cnt = a.at(b);
    sum = sum + cnt * (c - b);
    cout << sum << endl;

    a.at(c) += a.at(b);
    a.at(b) = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N;
  cin >> N;
  map<long long, long long> M;
  long long A, sum = 0;
  for (long long i = 0; i < N; i++) {
    cin >> A;
    sum += A;
    M[A]++;
  }
  long long Q;
  cin >> Q;
  long long B, C;
  for (long long i = 0; i < Q; i++) {
    cin >> B >> C;
    if (M[B] > 0) {
      sum += (C - B) * M[B];
      M[C] += M[B];
      M[B] = 0;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  map<long long int, long long int> s;
  long long int sum{0};

  for (int i{0}; i < n; i++) {

    long long int a;
    cin >> a;
    if (s.count(a)) {
      s[a]++;
    } else {
      s[a] = 1;
    }
    sum = sum + a;
  }

  long long int q;
  cin >> q;
  while (q--) {
    long long int b, c;
    cin >> b >> c;

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

    s[c] = s[c] + s[b];

    //	cout<<s[b]<<endl;
    s[b] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr;

  for (int i{0}; i < n; i++) {
    int a;
    cin >> a;
    arr.push_back(a);
  }

  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    long long int sum = 0;
    for (int i{0}; i < arr.size(); i++) {
      if (arr.at(i) == b) {
        arr.at(i) = c;
      }
    }
    for (int i{0}; i < arr.size(); i++) {
      sum = sum + arr.at(i);
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MX = 100005;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX);
  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 < (int)(n); i++)
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), c(q);
  rep(i, q) cin >> b[i] >> c[i];
  int i, j;
  for (i = 0; i < q; i++) {
    ll ans = 0;
    for (j = 0; j < n; j++) {
      if (a[j] == b[i]) {
        a[j] = c[i];
        ans += a[j];
      } else {
        ans += a[j];
      }
    }
    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 < (int)(n); i++)
const int INF = 1 << 29;
using namespace std;
using ll = long long;
ll s, f[1000005], a, b, n, q;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    f[a]++;
    s += a;
  }
  cin >> q;
  while (q--) {
    cin >> a >> b;
    f[b] += f[a];
    s = s - f[a] * a + f[a] * b;
    f[a] = 0;
    cout << s << endl;
  }
}


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const double pi = 3.14159265358979323846;
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
ll a[100005];
int main() {
  ull n, q, sum = 0, x, y, z, i;

  cin >> n;

  for (i = 1; i <= n; i++) {
    cin >> z;
    a[z]++;
    sum += z;
  }

  cin >> q;
  while (q--) {
    cin >> x >> y;

    if (a[x]) {
      sum -= x * a[x];

      sum += a[x] * y;
      a[y] += a[x];
      a[x] = 0;
    }

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

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const double pi = 3.14159265358979323846;
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int main() {
  ull n, q, sum = 0, x, y;

  cin >> n;

  ull a[n + 1];

  for (int i = 1; i <= n; i++)
    cin >> a[i], sum += a[i];

  cin >> q;
  while (q--) {
    cin >> x >> y;
    sum -= a[x];
    sum += y;
    a[x] = y;

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

  return 0;
}