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;
using ll = long long;
using P = pair<int, int>;
using G = vector<vector<int>>;
vector<int> coun(1e5 + 1, 0);
vector<ll> ans;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll total = 0;
  rep(i, n) {
    cin >> a[i];
    total += a[i];
    coun[a[i]]++;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    if (coun[b] == 0) {
      ans.push_back(total);
      continue;
    }
    total += (c - b) * coun[b];
    coun[c] += coun[b];
    coun[b] = 0;
    ans.push_back(total);
  }

  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  ll total = 0;
  rep(i, n) {
    cin >> a[i];
    total += a[i];
  }
  sort(a.begin(), a.end());
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int left = lower_bound(a.begin(), a.end(), b) - a.begin();
    int right = lower_bound(a.begin(), a.end(), b + 1) - a.begin();
    if (a[left] != b) {
      ans.push_back(total);
      continue;
    }
    if (a[right] == b)
      right++;
    int sa = c - b;
    total += sa * (right - left);
    ans.push_back(total);

    for (int j = left; j < right; j++) {
      a[j] = c;
    }

    sort(a.begin(), a.end());
  }

  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;

int main() {
  int N;
  cin >> N;
  vi a(1e5 + 10, 0);
  int maxa = 0;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    a[temp]++;
    maxa = max(maxa, temp);
  }
  long long ans = 0;
  for (int i = 1; i <= maxa; i++) {
    ans += (i)*a[i];
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    ans += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;

int main() {
  int N;
  cin >> N;
  vi a(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    int temp;
    cin >> temp;
    a[temp]++;
  }
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += (i)*a[i];
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    ans += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define ll long long int
using namespace std;
int main() {
  map<ll, ll> m;

  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    m[x]++;
  }
  int b;
  cin >> b;
  for (int i = 0; i < b; i++) {
    int x, y;
    cin >> x >> y;

    sum = sum - m[x] * x + m[x] * y;
    m[y] += m[x];
    m[x] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define ll long long
using namespace std;
int main() {
  multiset<int> m;
  int n;
  cin >> n;
  int a;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    sum += a;
    m.insert(a);
  }

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

    cin >> x >> y;
    ll cnt = m.count(x);
    m.erase(x);
    for (int i = 0; i < cnt; i++) {
      m.insert(y);
    }
    sum += cnt * (y - x);
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\\n'
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pii pair<int, int>
using namespace std;

inline void solve() {
  vector<int> v[100001];
  int n, c, b, q, sum = 0;
  cin >> n;
  vi a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    v[a[i]].pb(i);
    sum += a[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    for (int i = 0; i < sz(v[b]); i++) {
      sum -= a[v[b][i]];
      a[v[b][i]] = c;
      v[c].pb(v[b][i]);
      sum += a[v[b][i]];
    }
    v[b].clear();
    cout << sum << endl;
  }
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\\n'
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pii pair<int, int>
using namespace std;

inline void solve() {
  vector<int> v[100001];
  int n, c, b, q, sum = 0;
  cin >> n;
  vi a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    v[a[i]].pb(i);
    sum += a[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    for (auto &it : v[b]) {
      sum -= b;
      a[it] = c;
      v[c].pb(it);
      sum += c;
    }
    v[b].clear();
    sum -= a[b];
    a[b] = c;
    sum += a[b];
    cout << sum << endl;
  }
}

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

void solve() {}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(100001);
  rep(i, n) {
    int m;
    cin >> m;
    a[m]++;
  }
  int q;
  cin >> q;
  ll sum = 0;
  for (int i = 1; i <= 100001; i++) {
    if (a[i] != 0) {
      sum += i * a[i];
    }
  }
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int y = a[b];
    a[b] = 0;
    a[c] += y;
    sum += (c - b) * y;

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

void solve() {}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(100001);
  rep(i, n) {
    int m;
    cin >> m;
    a[m]++;
  }
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    int y = a[b];
    a[b] = 0;
    a[c] += y;
    ll sum = 0;
    for (int i = 1; i < 100001; i++) {
      if (a[i] != 0) {
        sum += i * a[i];
      }
    }
    cout << sum << endl;
  }

  return 0;
}
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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
  ll N, Q, sum = 0, a = 0, A[100005], B[100005], C[100005];
  cin >> N;
  memset(A, 0, sizeof(100005));
  REP(i, N) {
    cin >> a;
    A[a]++;
    sum += a;
  }
  cin >> Q;
  REP(i, Q) { cin >> B[i] >> C[i]; }
  REP(i, Q) {
    if (A[B[i]] > 0) {
      sum += (C[i] - B[i]) * A[B[i]];
      A[C[i]] += A[B[i]];
      A[B[i]] = 0;
    }
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
  ll N, Q, sum = 0, A[100000], B[100000], C[100000];
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  cin >> Q;
  REP(i, Q) { cin >> B[i] >> C[i]; }
  REP(i, Q) {
    REP(j, N) {
      if (A[j] == B[i]) {
        A[j] = C[i];
        sum += C[i];
        continue;
      } else {
        sum += A[j];
      }
    }
    cout << sum << endl;
    sum = 0;
  }

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

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

#define ll long long

ll a[100001], ct[100001];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, sum = 0;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    ct[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    ll t = ct[b];
    ct[c] += t;
    ct[b] = 0;
    sum -= b * t;
    sum += c * t;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

#define ll long long

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

  ll n;
  cin >> n;
  ll a[100001];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll b, c;
    ll sum = 0;
    cin >> b >> c;
    for (ll i = 0; i < n; i++) {
      if (a[i] == b)
        a[i] = c;
      sum += a[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

typedef unsigned long long int lli;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli n;
  cin >> n;

  vector<lli> v(n + 1);
  vector<lli> frequency(1000001);

  lli sum = 0;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    frequency[v[i]]++;
    sum += v[i];
  }

  lli q;
  cin >> q;

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

    sum += -frequency[b] * b + frequency[b] * c;

    cout << sum << endl;

    frequency[c] += frequency[b];
    frequency[b] = 0;
  }
}


Error: 0

Fixed submission:

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

typedef unsigned long long int lli;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli n;
  cin >> n;

  vector<lli> v(n + 1);
  vector<lli> frequency(n + 1);

  lli sum = 0;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    frequency[v[i]]++;
    sum += v[i];
  }

  lli q;
  cin >> q;

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

    sum += -frequency[b] * b + frequency[b] * c;

    cout << sum << endl;

    frequency[c] += frequency[b];
    frequency[b] = 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;
const int INF = 1001001001;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  vector<int> d(100007, 0);
  rep(i, n) {
    int a;
    cin >> a;
    d.at(a)++;
  }
  cin >> q;
  vector<pair<int, int>> bc;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    pair<int, int> p(b, c);
    bc.push_back(p);
  }
  ll ans = 0;
  rep(i, 100007) ans += i * d.at(i);
  rep(i, q) {
    int b = bc.at(i).first;
    int c = bc.at(i).second;
    int t = d.at(b);
    d.at(b) = 0;
    d.at(c) += t;
    ans += c * t - b * t;
    cout << ans << endl;
  }
}

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;
const int INF = 1001001001;

int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  cin >> q;
  vector<pair<int, int>> bc;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    pair<int, int> p(b, c);
    bc.push_back(p);
  }
  rep(i, q) {
    int b = bc.at(i).first;
    int c = bc.at(i).second;
    ll ans = 0;
    rep(j, n) {
      if (a.at(j) == b)
        a.at(j) = c;
      ans += a.at(j);
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define endl '\\n'

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<vi>
#define vp vector<pii>
#define vs vector<string>
#define all(v) v.begin(), v.end()

#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
// #define rep(i,l,r) for(int i=(l);i<(r);++i)

#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

#define pi 3.14159265358979323846264338327950L
#define MOD 1000000007

int32_t main() {

  int n;
  cin >> n;
  vi a(n);
  int sum = 0;
  vi rep((int)1e5 + 5);
  for (auto &it : a) {
    cin >> it;
    sum += it;
    rep[it]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= rep[b] * b;
    rep[c] += rep[b];

    sum += rep[b] * c;
    rep[b] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long
#define endl '\\n'

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<vi>
#define vp vector<pii>
#define vs vector<string>
#define all(v) v.begin(), v.end()

#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

#define pi 3.14159265358979323846264338327950L
#define MOD 1000000007
int a[104];

int32_t main() {

  int n;
  cin >> n;
  vi a(n);
  for (auto &it : a)
    cin >> it;

  int q;
  cin >> q;
  while (q--) {
    int ans = 0;
    sort(all(a));
    int k, b;
    cin >> k >> b;
    auto it1 = lb(all(a), k);
    auto it2 = ub(all(a), k);
    int pos1 = it1 - a.begin();
    int pos = it2 - a.begin();
    // cout<<*it1<<*it2<<endl;
    for (int i = pos1; i < pos; i++)
      a[i] = b;
    for (int i = 0; i < n; i++)
      ans += a[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define pb push_back

#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)

void solve() {
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  vector<ll> cnt(100005, 0);
  rep(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }

  ll t, b, c, d;
  cin >> t;
  rep(i, t) {
    cin >> b >> c;
    d = cnt[b];
    sum += d * (c - b);
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << "\\n";
  }
}

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

  int tt = 1;
  /// cin >> tt;
  while (tt--) {
    solve();
  }
}


Error: TLE

Fixed submission:

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

#define ll long long
#define pb push_back

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

  ll n, b, c;
  cin >> n;
  ll cnt[n];
  for (int i = 0; i < n; i++)
    cin >> cnt[i];
  ll q, sum;
  cin >> q;
  while (q--) {
    sum = 0;
    cin >> b >> c;
    for (ll i = 0; i < n; i++) {
      if (cnt[i] == b)
        cnt[i] = c;
      sum += cnt[i];
    }
    cout << sum << "\\n";
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

//  SMOKE SHISHA PLAY FIFA  //
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pll pair<ll, ll>
#define MP make_pair
#define ff first
#define ss second
#define PB push_back
#define mod 1000000007
#define lp(i, start, end) for (ll i = start; i <= end; i++)
#define deb1(a) cout << #a << " = " << (a) << endl;
#define deb2(a, b)                                                             \\
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << endl;
#define deb3(a, b, c)                                                          \\
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << ", " << #c       \\
       << " = " << (c) << endl;

ll modu(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % mod;
    b /= 2;
    a = (a * a) % mod;
  }
  return ans;
}

int main() {
  fastio ll n, sum = 0;
  cin >> n;
  ll dp[100005] = {0};
  ll a[n];
  lp(i, 0, n - 1) {
    cin >> a[i];
    sum += a[i];
    dp[a[i]]++;
  }
  ll q;
  cin >> q;
  lp(i, 0, q - 1) {
    ll x, y;
    cin >> x >> y;
    if (dp[x]) {
      sum += dp[x] * (y - x);
      dp[y] += dp[x];
      dp[x] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

//  SMOKE SHISHA PLAY FIFA  //
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pll pair<ll, ll>
#define MP make_pair
#define ff first
#define ss second
#define PB push_back
#define mod 1000000007
#define lp(i, start, end) for (ll i = start; i <= end; i++)
#define deb1(a) cout << #a << " = " << (a) << endl;
#define deb2(a, b)                                                             \\
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << endl;
#define deb3(a, b, c)                                                          \\
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << ", " << #c       \\
       << " = " << (c) << endl;

ll modu(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % mod;
    b /= 2;
    a = (a * a) % mod;
  }
  return ans;
}

int main() {
  fastio ll n, sum = 0;
  cin >> n;
  ll dp[50000] = {0};
  ll a[n];
  lp(i, 0, n - 1) {
    cin >> a[i];
    sum += a[i];
    dp[a[i]]++;
  }
  ll q;
  cin >> q;
  lp(i, 0, q - 1) {
    ll x, y;
    cin >> x >> y;
    if (dp[x]) {
      sum += dp[x] * (y - x);
      dp[y] += dp[x];
      dp[x] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n = 0;
  cin >> n;
  vector<ll> a(100009);
  rep(i, 0, n) {
    ll in = 0;
    cin >> in;
    a.at(in)++;
  }
  int q = 0;
  cin >> q;

  vector<pair<int, int>> shiji(q);

  rep(i, 0, q) { cin >> shiji.at(i).first >> shiji.at(i).second; }
  ll sum = 0;

  rep(i, 0, 100009) { sum += (ll)a.at(i) * i; }

  rep(i, 0, q) {
    sum += a.at(shiji.at(i).first) * shiji.at(i).second;
    sum -= a.at(shiji.at(i).first) * shiji.at(i).first;
    a.at(shiji.at(i).second) += a.at(shiji.at(i).first);
    a.at(shiji.at(i).first) = 0;

    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n = 0;
  cin >> n;
  vi a(100009);
  rep(i, 0, n) {
    int in = 0;
    cin >> in;
    a.at(in)++;
  }
  int q = 0;
  cin >> q;

  vector<pair<int, int>> shiji(q);

  rep(i, 0, q) { cin >> shiji.at(i).first >> shiji.at(i).second; }
  rep(j, 1, q + 1) {

    rep(i, 0, j) {
      a.at(shiji.at(i).second) += a.at(shiji.at(i).first);
      a.at(shiji.at(i).first) = 0;

      ll sum = 0;

      rep(i, 0, 100009) { sum += (ll)i * a.at(i); }
      if (i == j - 1) {
        cout << sum << endl;
      }
    }
    // cout<<sum<<endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main(void) {
  int ary[100001] = {};
  int num;
  cin >> num;
  unsigned long long sum = 0;

  //  cout << ary[100000];

  for (int i = 0; i < num; i++) {
    int tmp;
    cin >> tmp;
    ary[tmp]++;
    sum += tmp;
  }

  /*
  for(int i = 0; i < 10; i++) {
    cout << ary[i] << ' ';
  }
  cout << '\\n';

  cout << sum << '\\n';
  */
  int CNT;
  cin >> CNT;

  for (int i = 0; i < CNT; i++) {
    int b, c;
    cin >> b >> c;
    sum = sum - ((unsigned long long)b * ary[b]) +
          ((unsigned long long)c * ary[b]);
    ary[c] = ary[c] + ary[b];
    ary[b] = 0;
    cout << sum << '\\n';
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int num;
  cin >> num;
  vector<int> array;

  for (int i = 0; i < num; i++) {
    int tmp;
    cin >> tmp;
    array.push_back(tmp);
  }

  int CNT;
  cin >> CNT;

  for (int i = 0; i < CNT; i++) {
    int b, c;
    cin >> b >> c;
    replace(array.begin(), array.end(), b, c);
    int sum = 0;
    for (int j = 0; j < num; j++) {
      sum += array[j];
      //      cout << array[j];
    }
    cout << sum << '\\n';
    //    cout << '\\n';
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define M_PI 3.14159265358979323846
const int MOD = 1e9 + 7;
vector<ll> a(100005);
void solve() {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    sum += (ll)k;
    a[k]++;
  }
  int q;
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    sum -= (a[b] * b);
    sum += (a[b] * c);
    a[c] += a[b];
    a[b] = 0;
    cout << sum << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[100005], q, b, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    int sum = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] == b) {
        sum = sum + c;
        a[j] = c;
      } else {
        sum = sum + a[j];
      }
    }
    cout << sum << endl;
  }
  return 0;
}