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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  vector<ll> a(100005, 0);
  ll sum = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    sum += x;
    a[x]++;
  }
  ll q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(q);
  rep(i, q) { cin >> b[i] >> c[i]; }
  rep(i, q) {
    a[c[i]] += a[b[i]];
    sum += (c[i] - b[i]) * a[b[i]];
    a[b[i]] = 0;
    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;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  ll q;
  cin >> q;
  vector<ll> b(q);
  vector<ll> c(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] - b[i];
      }
    }
    cout << sum << endl;
  }
}
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++)
using LL = long long;
using P = pair<int, int>;

int main() {
  LL N;
  cin >> N;
  vector<LL> A(100050);
  LL ans = 0;
  rep(i, N) {
    int tmp;
    cin >> tmp;
    ++A[tmp];
    ans += tmp;
  }
  int Q;
  cin >> Q;
  rep(i, Q) {
    int B, C;
    cin >> B >> C;
    A[C] += A[B];
    ans += (C - B) * A[B];
    A[B] = 0;
    cout << ans << endl;
  }
}


Error: 0

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 P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<LL> A(10005);
  LL ans = 0;
  rep(i, N) {
    LL tmp;
    cin >> tmp;
    ++A[tmp];
    ans += tmp;
  }
  LL Q;
  cin >> Q;
  rep(i, Q) {
    LL B, C;
    cin >> B >> C;
    A[C] += A[B];
    ans += (C - B) * A[B];
    A[B] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <map>
#include <math.h>
using namespace std;

int main() {

  long long int a, b, i, q, res, ans = 0, j, n;
  cin >> n;
  map<int, int> ar;
  for (i = 0; i < n; i++) {
    cin >> res;
    ar[res]++;
    ans += res;
  }
  cin >> q;
  while (q--) {
    cin >> a >> b;
    ans += (b - a) * ar[a];
    res = ar[a];
    ar[b] += res;
    ar[a] = 0;
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define st string
#define con continue
#define br break
#define rt return 0
#define fr first
#define sec second
using namespace std;
int main() {
  // ll tc; cin>>tc; while(tc--){
  ll n, m, i, x, j, mx = 0, t, k, b, a, ans = 0, c = 0;
  st s, str;
  char strn;
  cin >> n; // a=n; n-=1;
  ll ara[n + 1], bra[n + 1], taj[n + 1];
  // memset(ara,0,sizeof(ara));
  for (i = 1; i <= n; i++)
    cin >> ara[i];
  cin >> k;
  while (k--) {
    ans = 0;
    cin >> a >> b;
    for (i = 1; i <= n; i++) {
      if (ara[i] == a)
        ara[i] = b;
      ans += ara[i];
    }

    cout << ans << endl;
  }

  // cout<<a<<endl;

  rt;
}
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 P = pair<int, int>;

const LL MX = 100005;

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


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 P = pair<int, int>;

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

#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

#define ll long long int

using namespace std;

int main(void) {

  ll n, q, tmp, b, c, res;
  deque<ll> a(100001, 0);

  cin >> n;

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

  cin >> q;

  res = 0;

  for (ll j = 0; j < (ll)a.size(); j++) {
    if (a[j]) {
      res += j * a[j];
    }
  }

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

    res -= b * a[b];
    res += c * a[b];

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

    cout << res << endl;
    ;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

#define ll long long int

using namespace std;

int main(void) {

  ll n, q, tmp, b, c;
  deque<ll> a;

  cin >> n;

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

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

  cin >> q;

  for (ll i = 0; i < q; i++) {
    cin >> b >> c;
    for (ll j = 0; j < n; j++) {
      if (b < a[j]) {
        break;
      }
      if (b == a[j]) {
        a[j] = c;
      }
    }
    cout << accumulate(a.begin(), a.end(), 0) << endl;

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

  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 output = 0;
  vector<int> num(100000, 0);
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    num.at(l - 1)++;
    output += l;
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int bef = 0;
    int aft = 0;
    cin >> bef >> aft;
    output += num.at(bef - 1) * (aft - bef);
    num.at(aft - 1) += num.at(bef - 1);
    num.at(bef - 1) = 0;

    cout << output << endl;
  }

  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> num(n);
  for (int i = 0; i < n; i++) {
    cin >> num.at(i);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int before, after;
    cin >> before >> after;
    long output = 0;
    for (int j = 0; j < n; j++) {

      if (num.at(j) == before) {
        num.at(j) = after;
      }
      output += num.at(j);
    }

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

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long long int n, a, k, p, q, g = 0, s = 0, i, b[100005];
  for (i = 0; i < 100001; i++)
    b[i] = 0;
  cin >> n;
  // cout<<n*100000;
  for (i = 0; i < n; i++) {
    cin >> a;
    s += a;
    b[a]++;
  }
  cin >> k;
  while (k--) {
    g = 0;
    cin >> p >> q;
    /*     for(i=0;i<n;i++)
         {
             if (a[i]==p){g++;
             a[i]=q;}
         }*/
    g = b[p];
    b[p] = 0;
    b[q] += g;
    s = s + (g * (q - p));
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long long int n, a[100005], k, p, q, g = 0, s = 0, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  cin >> k;
  while (k--) {
    g = 0;
    cin >> p >> q;
    for (i = 0; i < n; i++) {
      if (a[i] == p) {
        g++;
        a[i] = q;
      }
    }
    s = s + (g * (q - p));
    cout << s << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

//======================================
// author @Amit Singh : global_const
//======================================
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define ll long long
int a[100000];
// int n;
// ll sum=0;
// void change()
// {
//   int l,m;
//   scanf("%d%d",&l,&m);
//   for(int j=0;j<n;++j)
//   {if(a[j]==l)
//   	a[j]=m;
//   }
// }
// void solve()
// {
//   for(int j=0;j<n;++j)
//   sum+=a[j];
//   printf("%lld\\n",sum);
//   sum=0;
// }
signed main() {
  int n;
  cin >> n;
  unordered_map<int, int> pq;
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    ++pq[a[i]];
  }
  int k;
  scanf("%d", &k);
  for (int i = 0; i < k; ++i) {
    int l, m;
    cin >> l >> m;
    sum += (m - l) * pq[l];
    pq[m] = pq[m] + pq[l];
    pq[l] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

//======================================
// author @Amit Singh : global_const
//======================================
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <ctime>
#include <iostream>
#include <numeric>
#include <vector>
#define endl "\\n"
#define ll long long
int a[100000];
int n;
ll sum = 0;
void change() {
  int l, m;
  scanf("%d%d", &l, &m);
  for (int j = 0; j < n; ++j) {
    if (a[j] == l)
      a[j] = m;
  }
}
void solve() {
  for (int j = 0; j < n; ++j)
    sum += a[j];
  printf("%lld\\n", sum);
  sum = 0;
}
signed main() {

  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  int k;
  scanf("%d", &k);
  for (int i = 0; i < k; ++i) {
    change();
    solve();
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define LOCAL
#define ff first
#define se second
#define all(a) begin(a), end(a)
#define mod 1e9 + 7
#define rep(i, n) for (int i = 0; i <)
typedef vector<int> VI;
typedef long long ll;

ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

#ifdef LOCAL

void solve() {
  ll n;
  cin >> n;
  unordered_map<ll, ll> h;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    h[t]++;
    sum += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= h[b] * b;
    sum += h[b] * c;
    h[c] += h[b];
    h[b] = 0;
    cout << sum << endl;
  }
}

int main() {
  solve();
  return 0;
}

#endif


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define LOCAL
#define ff first
#define se second
#define all(a) begin(a), end(a)
#define mod 1e9 + 7
#define rep(i, n) for (int i = 0; i <)
typedef vector<int> VI;
typedef long long ll;

ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

#ifdef LOCAL

void solve() {
  ll n;
  cin >> n;
  unordered_map<ll, ll> h;

  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    h[t]++;
  }
  int q;
  cin >> q;
  while (q--) {
    ll sum = 0;
    int b, c;
    cin >> b >> c;
    int t = h[b];
    h[b] = 0;
    h[c] += t;
    for (auto t : h) {
      sum += t.first * t.second;
    }
    cout << sum << endl;
  }
}

int main() {
  solve();
  return 0;
}

#endif
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  vector<int> num(100000);
  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
    num.at(vec.at(i) - 1)++;
    sum += vec.at(i);
  }

  int Q;
  cin >> Q;
  vector<int> b(Q);
  vector<int> c(Q);
  for (int i = 0; i < Q; i++)
    cin >> b.at(i) >> c.at(i);

  for (int i = 0; i < Q; i++) {
    sum += (c.at(i) - b.at(i)) * num.at(b.at(i) - 1);
    cout << sum << endl;
    num.at(c.at(i) - 1) += num.at(b.at(i) - 1);
    num.at(b.at(i) - 1) = 0;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++)
    cin >> vec.at(i);

  int Q;
  cin >> Q;
  vector<int> b(Q);
  vector<int> c(Q);
  for (int i = 0; i < Q; i++)
    cin >> b.at(i) >> c.at(i);

  int64_t sum = 0;
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (vec.at(j) == b.at(i))
        vec.at(j) = c.at(i);
      sum += vec.at(j);
    }
    cout << sum << endl;
    sum = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  map<ll, ll> m;
  for (int i = 1; i <= n; i++) {
    ll a;
    cin >> a;
    m[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    ll x, y;
    cin >> x >> y;
    sum += (y - x) * m[x];
    m[y] += m[x];
    m[x] = 0;
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int q;
  cin >> q;
  int b[q], c[q];
  for (int i = 0; i < q; i++) {
    cin >> b[i] >> c[i];
  }
  int j = 0;
  while (q--) {
    for (int i = 0; i < n; i++) {
      if (b[j] == a[i]) {
        sum += c[j];
        sum -= a[i];
        a[i] = c[j];
      }
    }
    cout << sum << endl;
    j++;
  }
}
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 ll = long long;
using namespace std;

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

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  int b[q], c[q];
  rep(j, q) cin >> b[j] >> c[j];
  int con = 0;
  ll ans = 0;
  while (con < q) {
    rep(k, n) {
      if (b[con] == a[k]) {
        a[k] = c[con];
      }
    }
    rep(k, n) ans += a[k];
    cout << ans << endl;
    con += 1;
    ans = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\\n';
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll mininf = -922337203685477;
const ll nax = 1e5 + 5;
ll n, sums = 0, q;
unordered_map<ll, ll> frek;

int main() {
  IOS cin >> n;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    frek[a]++;
    sums += a;
  }
  cin >> q;
  while (q--) {
    ll b, c;
    cin >> b >> c;
    ll dif = c - b;
    sums += frek[b] * dif;
    frek[c] += frek[b];
    frek[b] = 0;
    cout << sums << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\\n';
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll mininf = -922337203685477;
const ll nax = 1e5 + 5;
int n, q;

int main() {
  IOS cin >> n;
  int arr[n + 5];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
      if (arr[i] == a) {
        arr[i] = b;
      }
    }
    ll sums = 0;
    for (int i = 0; i < n; i++) {
      sums += arr[i];
    }
    cout << sums << '\\n';
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define LL long long

LL n, bil, a, b, tot = 0, freq[100100], q;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> bil;
    freq[bil]++;
    tot += bil;
  }
  cin >> q;
  while (q--) {
    cin >> a >> b;
    tot += (b - a) * freq[a];
    freq[b] += freq[a];
    freq[a] = 0;
    cout << tot << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
int num[10000000];

int main() {
  int n;
  scanf("%d", &n);
  int max = INT_MIN;
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    num[temp]++;
    max = std::max(max, temp);
  }
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) {
    int b, c;
    scanf("%d %d", &b, &c);
    num[c] += num[b];
    num[b] = 0;
    max = std::max(max, c);
    unsigned long long count = 0;
    for (int i = 1; i <= max; i++) {
      count += i * num[i];
    }
    printf("%llu\\n", count);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  cin >> n;
  ll ar[n];
  ll sum = 0;

  map<ll, ll> m;

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

  ll q;
  cin >> q;
  while (q--) {

    ll b, c;
    cin >> b >> c;
    m[c] += m[b];
    sum -= m[b] * b;
    sum += m[b] * c;
    m[b] = 0;

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int ar[n];

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

  int q;
  cin >> q;
  while (q--) {

    int b, c;
    cin >> b >> c;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (ar[i] == b)
        ar[i] = c;
      sum += ar[i];
    }

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

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

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

// 解説読めば余裕なのでやり直し
//---main---------------------------------------------
int main() {
  // main
  int N;
  cin >> N;

  vector<int> count(100010);
  ll Asum = 0;
  int A;
  rep(i, N) {
    cin >> A;
    count[A]++;
    Asum += A;
  }

  int Q, B, C, Bnum;
  cin >> Q;
  rep(qi, Q) {
    cin >> B >> C;
    Bnum = count[B];
    count[B] = 0;
    count[C] += Bnum;
    Asum += (C - B) * Bnum;
    cout << Asum << 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>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

//---main---------------------------------------------
int main() {
  // main
  int N;
  cin >> N;

  vector<int> A(N);
  rep(i, N) cin >> A[i];
  // sort(A,A+N);

  int Q, Bi, Ci;
  cin >> Q;
  ll asum = 0;
  rep(i, N) asum += A[i];

  int l, r;
  rep(qi, Q) {
    cin >> Bi >> Ci;
    sort(A.begin(), A.end());

    l = lower_bound(A.begin(), A.end(), Bi) - A.begin();
    r = upper_bound(A.begin(), A.end(), Bi) - A.begin();

    for (int i = l; i < r; ++i)
      A[i] = Ci;
    asum += (Ci - Bi) * (r - l);

    cout << asum << endl;
  }

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

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

int main() {
  int n;
  cin >> n;
  long long sum = 0;
  map<int, int> m;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long b1, b2;
    cin >> b1 >> b2;
    sum -= m[b1] * b1;
    sum += m[b1] * b2;
    m[b2] += m[b1];
    m[b1] = 0;
    cout << sum << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

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

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

  int q;
  cin >> q;
  for (int i = 0; i < q - 1; i++) {
    int index, element;
    cin >> index >> element;
    sum -= a[index - 1];
    a[index - 1] = element;
    sum += element;
    cout << sum << endl;
  }

  int index1, index2;
  cin >> index1 >> index2;
  int z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == index1)
      z++;
  }

  sum -= index1 * z;
  sum += index2 * z;
  cout << sum << endl;

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

#include <cmath>
#include <iostream>
#include <string>

#define MAX 100001
#define ll long long

using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[n + 1];
  int count[MAX];
  for (int i = 0; i < MAX; i++)
    count[i] = 0;
  long sum = 0;
  arr[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> arr[i];
    sum += arr[i];
    count[arr[i]]++;
  }
  int q;
  cin >> q;
  while (q-- > 0) {
    int a, b;
    cin >> a >> b;

    long diff = b - a;
    sum = sum + (diff * count[a]);
    count[b] = count[a] + count[b];
    count[a] = 0;

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

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>

#define ll long long

using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[n + 1];
  long sum = 0;
  arr[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  int q;
  cin >> q;
  while (q-- > 0) {
    int a, b;
    cin >> a >> b;
    for (int i = 1; i <= n; i++) {
      if (arr[i] == a) {
        long diff = b - arr[i];
        sum += diff;
        arr[i] = b;
      }
    }
    cout << sum << endl;
  }
  return 0;
}