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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
  int N;
  cin >> N;
  map<ll, ll> hist;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    hist[A]++;
  }
  int Q;
  cin >> Q;
  ll S = 0;
  for (auto h : hist) {
    S += h.first * h.second;
  }
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    S -= B * hist[B];
    S += C * hist[B];
    hist[C] += hist[B];
    hist[B] = 0;
    cout << S << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
  int N;
  cin >> N;
  map<ll, ll> hist;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    hist[A]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    hist[C] += hist[B];
    hist[B] = 0;
    ll S = 0;
    for (auto h : hist) {
      S += h.first * h.second;
    }
    cout << S << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, q, a, b, s, x;
vector<ll> v(100005);
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    ++v[x];
    s += x;
  }
  cin >> q;
  while (q--) {
    cin >> a >> b;
    s -= (v[a] * a);
    s += (v[a] * b);
    v[b] += v[a], v[a] = 0;
    cout << s << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, q, a, b, s;
vector<ll> v(100005);
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> v[i];
  cin >> q;
  while (q--) {
    s = 0;
    cin >> a >> b;
    for (int i = 1; i <= n; ++i) {
      if (v[i] == a)
        v[i] = b;
      s += v[i];
    }
    cout << s << "\\n";
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N;
  cin >> N;
  ll A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int Q;
  cin >> Q;
  ll B[Q], C[Q];
  map<ll, ll> mp;
  for (int i = 0; i < Q; i++)
    cin >> B[i] >> C[i];
  for (int i = 0; i < N; i++) {
    mp[A[i]]++;
  }
  ll res = 0;
  for (auto x : mp) {
    res += x.first * x.second;
  }
  for (int i = 0; i < Q; i++) {
    res += C[i] * mp[B[i]];
    res -= B[i] * mp[B[i]];
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N;
  cin >> N;
  ll A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int Q;
  cin >> Q;
  ll B[Q], C[Q];
  map<ll, ll> mp;
  for (int i = 0; i < Q; i++)
    cin >> B[i] >> C[i];
  for (int i = 0; i < N; i++) {
    mp[A[i]]++;
  }

  for (int i = 0; i < Q; i++) {
    mp[C[i]] += mp[B[i]];
    mp[B[i]] = 0;
    ll res = 0;
    for (auto x : mp) {
      res += x.first * x.second;
    }
    cout << res << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long sum = 0;
  vector<int> A(N);
  vector<long long> count(110000, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    sum += A[i];
    count[A[i]]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B;
    int C;
    cin >> B >> C;
    sum += (C - B) * count[B];
    count[C] += count[B];
    count[B] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int S = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int Q;
  cin >> Q;
  vector<vector<int>> B(Q, vector<int>(2));
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> B.at(i).at(j);
    }
  }
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (A.at(j) == B.at(i).at(0)) {
        A.at(j) = B.at(i).at(1);
      }
      S += A.at(j);
    }
    cout << S << endl;
    S = 0;
  }
}
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;
typedef long long ll;

int main() {

  ll n;
  cin >> n;

  vector<ll> a(n);
  vector<ll> d(100001);
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    d[a[i]]++;
  }

  ll q;
  cin >> q;

  ll b, c;
  REP(i, q) {
    cin >> b >> c;
    sum = sum - b * d[b] + c * d[b];
    d[c] += d[b];
    d[b] = 0;

    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

  vector<ll> a(n);
  vector<ll> d(10001);
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    d[a[i]]++;
  }

  ll q;
  cin >> q;

  ll b, c;
  REP(i, q) {
    cin >> b >> c;
    sum = sum - b * d[b] + c * d[b];
    d[c] += d[b];
    d[b] = 0;

    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  map<int, int> m;
  long long int n;
  cin >> n;
  long long int a[n];
  long long int sum = 0;
  // long long int neww=0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  sort(a, a + n);
  int q;
  cin >> q;
  long long int b[q], c[q];
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  for (int i = 0; i < q; i++) {
    if (m[b[i]] != 0) {
      m[c[i]] = m[c[i]] + m[b[i]];
      sum = sum - m[b[i]] * b[i];
      sum = sum + m[b[i]] * c[i];
      m[b[i]] = 0;
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

int main() {
  int a;
  cin >> a;
  int b[100001];
  long long int c, d, e, f;
  long long int sum = 0;
  for (int i = 0; i < 100001; i++) {
    b[i] = 0;
  }
  for (int i = 0; i < a; i++) {
    cin >> c;
    b[c]++;
    sum += c;
  }
  cin >> d;
  for (int i = 0; i < d; i++) {
    cin >> e >> f;
    sum += (f - e) * b[e];
    cout << sum << endl;
    b[f] += b[e];
    b[e] = 0;
  }
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int a;
  int b[200000];
  cin >> a;
  int c, d;
  int ans = 0;
  int e;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
    ans += b[i];
  }
  cin >> e;
  for (int i = 0; i < e; i++) {
    int count = 0;
    cin >> c >> d;
    for (int k = 0; k < a; k++) {
      if (b[k] == c) {
        b[k] = d;
        count++;
      }
    }
    ans += count * (d - c);
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch;
  long long n, q, u, v;
  map<long long, long long> m;
  scanf("%lld", &n);
  long long a[n + 1];
  long long sum = 0;

  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]), m[a[i]]++, sum += a[i];
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld %lld", &u, &v);
    sum -= m[u] * u;
    sum += m[u] * v;
    m[v] += m[u];
    m[u] = 0;
    // printf("%lld %lld\\n",m[u],m[v]);
    printf("%lld\\n", sum);
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch;
  long long n, q, u, v;
  long long a[n + 1];
  map<long long, long long> m;
  scanf("%lld", &n);
  long long sum = 0;

  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]), m[a[i]]++, sum += a[i];
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld %lld", &u, &v);
    sum -= m[u] * u;
    sum += m[u] * v;
    m[v] += m[u];
    m[u] = 0;
    // printf("%lld %lld\\n",m[u],m[v]);
    printf("%lld\\n", sum);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define endl '\\n'
#define vi vector<int>
#define rep(i, n) for (i = 0; i < n; i++)

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, i;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  map<ll, ll> mp1;
  rep(i, n) {
    cin >> a[i];
    mp1[a[i]]++;
    sum += a[i];
  }
  ll q;
  cin >> q;
  while (q--) {
    ll x, y;
    cin >> x >> y;
    sum -= mp1[x] * x;
    if (mp1.find(y) == mp1.end()) {
      mp1[y] = mp1[x];
      sum += mp1[y] * y;
      mp1.erase(x);
      cout << sum << endl;
      continue;
    } else {
      mp1[y] += mp1[x];
      sum += mp1[x] * y;
      mp1.erase(x);
      cout << sum << endl;
      continue;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define endl '\\n'
#define vi vector<int>
#define rep(i, n) for (i = 0; i < n; i++)

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll i, n, q, s = 0, j;
  cin >> n;

  ll a[n];

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

  cin >> q;

  ll b[q], c[q];

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

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

    replace(a, a + n, b[i], c[i]);

    for (j = 0; j < n; j++)
      s = s + a[j];

    cout << s << endl;
  }

  // for(i=0;i<n;i++)
  // 	cout<<a[i]<<endl;

  /*  for(i=1;i<=q;i++)
   {   cout<<b[i]<<" "<<c[i];
    cout<<endl;
  }
  */
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
ll a[100005];
ll x[100005] = {};
ll sum[100005];
int main() {
  int n;
  cin >> n;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += a[i];
    x[a[i]]++;
  }
  pair<ll, ll> p;
  int m;
  cin >> m;
  ll count = 0;
  for (int i = 0; i < m; i++) {
    cin >> p.first >> p.second;
    ans -= x[p.second] * p.second;
    x[p.second] += x[p.first];
    ans = ans + (x[p.second] * p.second - x[p.first] * p.first);
    sum[count] = ans;
    x[p.first] = 0;
    count++;
  }
  for (int i = 0; i < m; i++) {
    cout << sum[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
ll a[100005];
ll x[100005] = {};
ll sum[100005];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x[a[i]]++;
  }
  pair<ll, ll> p;
  int m;
  cin >> m;
  ll count = 0;
  for (int i = 0; i < m; i++) {

    cin >> p.first >> p.second;
    x[p.second] += x[p.first];
    x[p.first] = 0;
    for (int i = 1; i <= 100000; i++) {
      if (x[i] > 0) {
        sum[count] += i * x[i];
      }
    }
    count++;
  }
  for (int i = 0; i < m; i++) {
    cout << sum[i] << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/* 	* In the name of GOD
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define int long long

int32_t main() {
  int n;
  cin >> n;
  int a[n];
  int cnt[100012] = {};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  int sum = 0;
  for (int i = 0; i < 100012; i++) {
    sum += (cnt[i] * i);
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum += (cnt[b] * (c - b));
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

/* 	* In the name of GOD
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define int long long

int32_t main() {
  int n;
  cin >> n;
  int a[n];
  int cnt[100012] = {};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    int sum = 0;
    for (int i = 0; i < 100012; i++) {
      sum += (cnt[i] * i);
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long int
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> v(n + 1);
  vector<int> cnt(100010);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    ans += v[i];
    cnt[v[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ans += (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << ans << endl;
  }
}

int32_t main() { solve(); }

Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int n;
map<int, int> a;
int q;
// vector<int> b, c;

int main() {
  ll sum;
  int b, c;

  cin >> n;

  sum = 0;
  for (int i = 0; i < n; i++) {
    int ain;
    cin >> ain;
    auto it = a.find(ain);
    if (it != a.end()) {
      a[ain] = a[ain] + 1;
    } else {
      a[ain] = 1;
    }
    sum += ain;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int bnum, cnum;
    cin >> b >> c;
    auto it = a.find(b);
    if (it != a.end()) {
      bnum = a[b];
    } else {
      bnum = 0;
    }
    a[b] = 0;
    it = a.find(c);
    if (it != a.end()) {
      cnum = a[c];
    } else {
      cnum = 0;
    }
    a[c] = bnum + cnum;
    sum += (c - b) * bnum;
    printf("%lld\\n", sum);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int n;
multiset<int> a;
int q;
// vector<int> b, c;

int main() {
  ll sum;
  int b, c;

  cin >> n;

  for (int i = 0; i < n; i++) {
    int ain;
    cin >> ain;
    a.insert(ain);
  }
  sum = 0;
  for (auto it = a.begin(); it != a.end(); it++) {
    sum += *it;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    int bnum = a.count(b);
    a.erase(b);
    for (int j = 0; j < bnum; j++)
      a.insert(c);
    sum += (c - b) * bnum;
    printf("%lld\\n", sum);
  }

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

#include <cstdio>
#include <iostream>
using namespace std;
long long a[100005];
int main() {
  ios::sync_with_stdio(false);
  int n;
  scanf("%d", &n);
  long long sum(0);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[x] += x;
    sum += x;
  }
  int q;
  scanf("%d", &q);
  int b, c;
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &b, &c);
    long long tmp = a[b] / b * c;
    sum += tmp - a[b];
    a[c] += tmp;
    a[b] = 0;
    printf("%lld\\n", sum);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <time.h>
#include <vector>

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;
  while (Q--) {
    int B, C;
    cin >> B >> C;
    for (int i = 0; i < N; i++) {
      if (A[i] == B) {
        A[i] = C;
      }
    }
    long sum = 0;
    for (int i = 0; i < N; i++) {
      sum += 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 (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<int> a(n);
  map<ll, ll> mp;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }

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

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

Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll q;
  cin >> q;
  vector<int> b(q, 1), c(q, 1);
  rep(i, q) cin >> b[i] >> c[i];

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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> mp_a;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp_a[a[i]]++;
  }

  ll q;
  cin >> q;
  vector<ll> b(q), c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  ll ans = 0;
  for (auto itr = mp_a.begin(); itr != mp_a.end(); ++itr) {
    ans += itr->first * itr->second;
  }
  for (int i = 0; i < q; i++) {
    ans += mp_a[b[i]] * (c[i] - b[i]);
    mp_a[c[i]] += mp_a[b[i]];
    mp_a[b[i]] = 0;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> mp_a;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp_a[a[i]]++;
  }

  ll q;
  cin >> q;
  vector<ll> b(q), c(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  for (long long i = 0; i < q; i++) {
    mp_a[c[i]] += mp_a[b[i]];
    mp_a[b[i]] = 0;
    ll ans = 0;
    for (auto itr = mp_a.begin(); itr != mp_a.end(); ++itr) {
      ans += itr->first * itr->second;
    }
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int N;
  cin >> N;
  vector<int> A;
  rep(i, N) {
    int num;
    cin >> num;
    A.push_back(num);
  }

  int Q;
  cin >> Q;
  int B[Q], C[Q];
  rep(i, Q) { cin >> B[i] >> C[i]; }

  map<int, int> M;
  for (auto v : A) {
    M[v]++;
  }
  long long ans = accumulate(A.begin(), A.end(), 0LL);

  rep(i, Q) {
    M[C[i]] += M[B[i]];
    ans = ans + (C[i] - B[i]) * M[B[i]];
    M[B[i]] = 0;
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int N;
  cin >> N;
  vector<int> A;
  rep(i, N) {
    int num;
    cin >> num;
    A.push_back(num);
  }

  int Q;
  cin >> Q;
  int B[Q], C[Q];
  rep(i, Q) { cin >> B[i] >> C[i]; }

  long long ans = accumulate(A.begin(), A.end(), 0LL);
  rep(i, Q) {
    int tag_cnt = count(A.begin(), A.end(), B[i]);
    replace(A.begin(), A.end(), B[i], C[i]);
    ans = ans + (C[i] - B[i]) * tag_cnt;
    cout << ans << endl;
  }

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

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

const int MX = 100005;

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

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

Error: TLE

Fixed submission:

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

int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long q, b, c;
  long long ans = 0;
  cin >> q;
  vector<long long> ansarray(q);
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    ans = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] == b)
        a[j] = c;
      ans += a[j];
    }
    ansarray[i] = ans;
  }
  for (int i = 0; i < q; i++) {
    cout << ansarray[i] << endl;
  }
  return 0;
}
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 < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(100002);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll sum = 0;
  rep(i, 100002) sum += i * cnt[i];
  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    sum -= b * cnt[b];
    sum -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    sum += c * cnt[c];
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  vi b(q), c(q);
  rep(i, q) cin >> b[i] >> c[i];
  vi ans(q);
  rep(i, q) {
    rep(j, n) {
      if (b[i] == a[j]) {
        a[j] = c[i];
      }
      ans[i] += a[j];
    }
  }
  rep(i, q) cout << ans[i] << endl;
}