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

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

int v[100000];

int main() {

  int n, q, b, c, i, aux;
  ll s = 0;
  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> aux;
    v[aux]++;
    s += aux;
  }

  cin >> q;

  while (q--) {

    cin >> b >> c;

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

    cout << s << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n, v[100000], q, b, c, s[100000], i, j;
  cin >> n;

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

  cin >> q;

  for (i = 0; i < q; i++) {
    cin >> b >> c;
    s[i] = 0;
    for (j = 0; j < n; j++) {
      if (v[j] == b)
        v[j] = c;
      s[i] += v[j];
    }
  }

  for (i = 0; i < q; i++) {
    cout << s[i] << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

const int M = 1000005;

int main() {
  int n, sum = 0;
  cin >> n;
  vector<ll> cnt(M);
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll tot = 0;
  rep(i, M) 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;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

const int M = 1000005;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  vector<pair<int, int>> b(q);
  rep(i, q) { cin >> b[i].first >> b[i].second; }
  vector<int> sum(q);
  rep(j, q) {
    rep(i, n) {
      if (a[i] == b[j].first) {
        a[i] = b[j].second;
      }
      sum[j] += a[i];
    }
    cout << sum[j] << endl;
    ;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<int> count(100001, 0);
  vector<int> v(n);
  long long sum = 0;
  for (auto i : v) {
    cin >> i;
    sum += i;
    count[i]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    count[c] += count[b];
    sum += (c - b) * count[b];
    count[b] = 0;
    cout << sum << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<int> count(10001, 0);
  vector<int> v(n);
  long long sum = 0;
  for (auto i : v) {
    cin >> i;
    sum += i;
    count[i]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    count[c] += count[b];
    sum += (c - b) * count[b];
    count[b] = 0;
    cout << sum << endl;
  }

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

#include <iostream>
#include <vector>
#define rep(i, a, n) for (long int i = a; i < n; i++)
using namespace std;
int main() {
  long int n, m = 0;
  cin >> n;
  long int a[n];
  vector<int> v(100000, 0);

  rep(i, 0, n) {
    cin >> a[i];
    m += a[i];
    v[a[i]]++;
  }

  long int q;
  cin >> q;

  rep(i, 0, q) {
    long int b, c;
    cin >> b >> c;
    long int d = c - b;
    cout << m + d * v[b] << "\\n";
    m += d * v[b];
    v[c] += v[b];
    v[b] = 0;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#define rep(i, a, n) for (long int i = a; i < n; i++)
using namespace std;
int main() {
  long int n;
  cin >> n;
  int a[n];

  rep(i, 0, n) cin >> a[i];

  long int q, m = 0;
  cin >> q;

  rep(i, 0, q) {
    long int b, c;
    cin >> b >> c;
    rep(j, 0, n) {
      if (a[j] == b) {
        a[j] = c;
      }
      m += a[j];
    }
    cout << m << "\\n";
    m = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PI 2 * acos(0)
#define ll long long
#define pii pair<int, int>
#define mx 100005
using namespace std;
ll int a[mx], f[mx];
ll int sum;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll int i, j, n, q, b, c;
  cin >> n;
  sum = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    // cout<<a[i]<<endl;
    f[a[i]]++;
    sum += a[i];
  }
  // cout<<"Sum =" <<sum<<endl;
  cin >> q;
  for (i = 1; i <= q; i++) {
    cin >> b >> c;
    sum += f[b] * (c - b);
    f[c] += f[b];
    f[b] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PI 2 * acos(0)
#define ll long long
#define pii pair<int, int>
#define mx 100005
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll int j, i, q, b, c, n;
  cin >> n;
  vector<ll int> a;
  for (i = 0; i < n; i++) {
    cin >> b;
    a.push_back(b);
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b >> c;
    for (j = 0; j < a.size(); j++) {
      if (a[j] == b)
        a[j] = c;
    }
    cout << accumulate(a.begin(), a.end(), 0) << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

typedef unsigned long ULONG;

using namespace std;
int main() {
  int n, q;
  vector<ULONG> b, c;
  cin >> n;

  map<ULONG, ULONG> a;
  for (int i = 0; i < n; i++) {
    ULONG tmp_a;
    cin >> tmp_a;
    auto itr = a.find(tmp_a);
    if (itr == a.end()) {
      a.insert(make_pair(tmp_a, 1UL));
    } else {
      itr->second++;
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    ULONG tmp_b, tmp_c;
    cin >> tmp_b >> tmp_c;
    b.push_back(tmp_b);
    c.push_back(tmp_c);
  }

  ULONG sum = 0UL;
  for (auto itr = a.begin(); itr != a.end(); ++itr) {
    sum += (itr->first * itr->second);
  }

  for (int i = 0; i < q; i++) {
    auto itr = a.find(b[i]);
    if (itr != a.end()) {
      ULONG tmp_cnt = itr->second;
      a.erase(b[i]);
      sum -= b[i] * tmp_cnt;

      auto tmp_itr = a.find(c[i]);
      if (tmp_itr == a.end()) {
        a.insert(make_pair(c[i], itr->second));
      } else {
        tmp_itr->second += tmp_cnt;
      }
      sum += c[i] * tmp_cnt;
    }
    cout << sum << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;
int main() {
  int n, q;
  vector<int> a, b, c;
  cin >> n;

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    int tmp_b, tmp_c;
    cin >> tmp_b >> tmp_c;
    b.push_back(tmp_b);
    c.push_back(tmp_c);
  }

  for (int i = 0; i < q; i++) {
    if (find(a.begin(), a.end(), b[i]) != a.end()) {
      for (int j = 0; j < n; j++) {
        if (a[j] == b[i]) {
          a[j] = c[i];
        }
      }
    }
    cout << accumulate(a.begin(), a.end(), 0) << 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 < (n); i++)
typedef long long ll;

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

  int q;
  cin >> q;
  rep(i, q) {
    int b, c;
    cin >> b >> c;
    if (mp.find(b) == mp.end())
      cout << sum << endl;
    else {
      sum = sum - b * mp[b] + c * mp[b];
      mp[c] += mp[b];
      mp.erase(b);
      cout << sum << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI acos(-1)
typedef long long ll;
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> co(1000000, 0);
  ll ans = 0;

  rep(i, n) {
    cin >> a[i];

    ans += a[i];
    co[a[i] - 1]++;
  }

  ll q;
  cin >> q;

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

    ll x = co[b - 1];
    ans += (c - b) * x;
    co[b - 1] = 0;

    co[c - 1] += x;
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI acos(-1)
typedef long long ll;
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> co(n, 0);
  ll ans = 0;

  rep(i, n) {
    cin >> a[i];
    ans += a[i];
    co[a[i] - 1]++;
  }

  ll q;
  cin >> q;

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

    ll x = co[b - 1];
    ans += (c - b) * x;
    co[b - 1] = 0;

    co[c - 1] += x;
    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;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
using P = pair<int, int>;
#define rep(i, n)                                                              \\
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

const int MX = 100005;

int main() {
  ll 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 -= b * cnt[b];
    tot -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    tot += c * cnt[c];
    cout << tot << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！

#define rep(i, n)                                                              \\
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！
const double pi = 3.14159265359;

int main() {
  ll N;
  cin >> N;

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

  ll Q;
  cin >> Q;

  vector<ll> 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];
      }
    }
    ll sum = 0;
    for (int i = 0; i < N; i++) {
      sum += A[i];
    }
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

using namespace std;

int main() {
  long long n;
  cin >> n;
  map<long long, long long> nums;
  for (long long i = 0; i < n; i++) {
    long long input;
    cin >> input;
    nums[input] += 1;
  }

  long long q;
  cin >> q;
  long long ans = 0;
  for (map<long long, long long>::iterator itr = nums.begin();
       itr != nums.end(); itr++) {
    ans += itr->first * itr->second;
  }
  for (long long i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    ans += c * nums[b] - b * nums[b];
    nums[c] += nums[b];
    nums.erase(nums.find(b));
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>

using namespace std;

int main() {
  long long n;
  cin >> n;
  map<long long, long long> nums;
  for (long long i = 0; i < n; i++) {
    long long input;
    cin >> input;
    nums[input] += 1;
  }

  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    nums[c] += nums[b];
    nums.erase(nums.find(b));
    long long ans = 0;
    for (map<long long, long long>::iterator itr = nums.begin();
         itr != nums.end(); itr++) {
      ans += itr->first * itr->second;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, i;
  cin >> n;
  ll a[n];
  ll sum = 0;
  vector<ll> v;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll q;
  cin >> q;
  ll b, c;
  unordered_map<ll, ll> hm;
  for (ll j = 0; j < n; j++) {
    hm[a[j]]++;
  }

  for (i = 0; i < q; i++) {
    cin >> b >> c;
    sum = sum + hm[b] * (c - b);
    hm[c] += hm[b];
    hm[b] = 0;
    cout << sum << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

/****************************************

        LostMartian

****************************************/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define vll vector<ll>
#define vvl vector<vector<ll>>
#define pi pair<ll, ll>
#define pb push_back
#define mk make_pair
#define sz size()
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define d(x) cout << "Value of " #x " = " << x << "\\n";
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define LOCAL                                                                  \\
#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin);                       \\
  freopen("output.txt", "w", stdout);                                          \\
#endif
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
int main() {
  clock_t z = clock();
  FAST
      // LOCAL
      ll n;
  cin >> n;
  ll a[n];
  vll vi;
  rep(i, 0, n) {
    ll x;
    cin >> x;
    vi.pb(x);
  }
  ll q;
  cin >> q;
  while (q--) {
    ll sum = 0;
    ll a1, b1;
    cin >> a1 >> b1;
    replace(all(vi), a1, b1);
    cout << accumulate(all(vi), sum) << "\\n";
    // cout<<sum;
  }

  debug("Total Time: %.3f\\n", (double)(clock() - z) / CLOCKS_PER_SEC);
  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++)

int main() {
  long long N, Q;
  long long buf;
  long long inp1, inp2;
  long long sum = 0;

  cin >> N;
  vector<long long> ans(N);
  vector<long long> num(100001);
  for (int i = 0; i < N; i++) {
    cin >> buf;
    num.at(buf)++;
    sum += buf;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> inp1 >> inp2;
    sum = sum + (inp2 - inp1) * num.at(inp1);
    num.at(inp2) += num.at(inp1);
    num.at(inp1) = 0;
    // for(int i=0;i<5;i++){
    //    cout << num.at(i) << " ";
    // }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

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

int main() {
  long long N, Q;
  long long buf;
  long long inp1, inp2;
  long long sum = 0;

  cin >> N;
  vector<long long> ans(N);
  vector<long long> num(100000);
  for (int i = 0; i < N; i++) {
    cin >> buf;
    num.at(buf)++;
    sum += buf;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> inp1 >> inp2;
    sum = sum - inp1 * num.at(inp1) + inp2 * num.at(inp1);
    num.at(inp2) += num.at(inp1);
    num.at(inp1) = 0;
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * Project: AtCoder Beginners Contest 171 D - Replacing
 * Author: toms74209200 <https://github.com/toms74209200>
 *
 * Created on 2020/06/21
 * Copyright (c) 2020 toms74209200
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
#include <algorithm>
#include <cstdio>
#include <numeric>
using namespace std;

int n;
int a[100000];
int q;
int b[100000];
int c[100000];

int bin[100001]{};
long long sum;

void solve() {
  long long e;
  for (int i = 0; i < q; i++) {
    e = c[i] * bin[b[i]] - b[i] * bin[b[i]];
    sum += e;
    bin[c[i]] += bin[b[i]];
    bin[b[i]] = 0;
    printf("%lld\\n", sum);
  }
}

int main() {

  sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    bin[a[i]]++;
    sum += a[i];
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &b[i]);
    scanf("%d", &c[i]);
  }

  solve();
}

Error: TLE

Fixed submission:

/*
 * Project: AtCoder Beginners Contest 171 D - Replacing
 * Author: toms74209200 <https://github.com/toms74209200>
 *
 * Created on 2020/06/21
 * Copyright (c) 2020 toms74209200
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
#include <algorithm>
#include <cstdio>
#include <numeric>
using namespace std;

int n;
int a[100000];
int q;
int b[100000];
int c[100000];

void solve() {

  sort(a, a + n);
  int sum = accumulate(a, a + n, 0);
  for (int i = 0; i < q; i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] == b[i]) {
        cnt++;
        a[j] = c[i];
      }
    }
    sum += (c[i] - b[i]) * cnt;
    printf("%d\\n", sum);
  }
}

int main() {

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &b[i]);
    scanf("%d", &c[i]);
  }

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

#include <iostream>

using namespace std;

int n, q;
long long sum, frecv[100005], b, c, nr;

int main() {
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> nr;
    sum += nr;
    ++frecv[nr];
  }

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

    sum -= b * frecv[b];
    sum += c * frecv[b];
    frecv[c] += frecv[b];
    frecv[b] = 0;

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int n, q, nr[100005], b, c;

int main() {
  cin >> n;

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

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

    long long sum = 0;

    for (int i = 0; i < n; ++i) {
      if (nr[i] == b)
        nr[i] = c;
      sum += nr[i];
    }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll n, a, sum, num[N], q, b;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a, sum += a, num[a]++;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    sum += (b - a) * num[a];
    num[b] += num[a];
    num[a] = 0;
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

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

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

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  map<long long, long long> m;
  for (long long a : A) {
    if (m.count(a))
      m[a] += 1;
    else
      m[a] = 1;
  }

  long long prev = -1;
  for (long long q = 0; q < Q; q++) {
    long long minus = B[q] * m[B[q]];
    long long plus = C[q] * m[B[q]];
    m[C[q]] += m[B[q]];
    m[B[q]] = 0;
    if (prev != -1) {
      cout << prev - minus + plus << endl;
      prev = prev - minus + plus;
    } else {
      long long sum = 0;
      for (auto p : m)
        sum += p.first * p.second;
      cout << sum << endl;
      prev = sum;
    }
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, std::move(A), Q, std::move(B), std::move(C));
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(long long N, std::vector<long long> A, long long Q,
           std::vector<long long> B, std::vector<long long> C) {
  map<long long, long long> m;
  for (long long a : A) {
    if (m.count(a))
      m[a] += 1;
    else
      m[a] = 1;
  }

  for (long long q = 0; q < Q; q++) {
    m[C[q]] += m[B[q]];
    m[B[q]] = 0;
    long long sum = 0;
    for (auto p : m)
      sum += p.first * p.second;
    cout << sum << endl;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> B(Q);
  std::vector<long long> C(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, std::move(A), Q, std::move(B), std::move(C));
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <iomanip> : 表示桁数, ex) cout << fixed << setprecision(15) << ...

#define pi 3.14159265358979323846
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using ld = long double;
using Byte = unsigned char;
using namespace std;

// string ans = "Yes";

void SetAns() {
  //	const ll max = 1000000000000001;
  ll N;
  vector<int> A(200000, 0);

  cin >> N;

  int tmp;
  rep(i, N) {
    cin >> tmp;
    A[tmp]++;
  }

  // 喰えり処理
  int Q;
  int B, C;
  cin >> Q;
  ll nowAns = 0;
  rep(i, 100000) nowAns += (ll)A[i + 1] * (ll)(i + 1);

  rep(_i, Q) {
    cin >> B >> C;
    nowAns -= (ll)B * (ll)A[B];
    nowAns += (ll)C * (ll)A[B];

    A[C] += A[B];

    A[B] = 0;

    cout << nowAns << endl;
  }

  // こたえ
  return;
}

int main() {
  SetAns();
  // cout << SetAns() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <iomanip> : 表示桁数, ex) cout << fixed << setprecision(15) << ...

#define pi 3.14159265358979323846
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using ld = long double;
using Byte = unsigned char;
using namespace std;

// string ans = "Yes";

void SetAns() {
  //	const ll max = 1000000000000001;
  ll N;
  vector<int> A(200000, 0);

  cin >> N;

  int tmp;
  rep(i, N) {
    cin >> tmp;
    A[tmp]++;
  }

  // 喰えり処理
  int Q;
  int B, C;
  cin >> Q;
  rep(_i, Q) {
    cin >> B >> C;
    A[C] += A[B];
    A[B] = 0;

    ll nowAns = 0;
    rep(i, 100000) nowAns += (ll)A[i + 1] * (ll)(i + 1);
    cout << nowAns << endl;
  }

  // こたえ
  return;
}

int main() {
  SetAns();
  // cout << SetAns() << endl;
  return 0;
}