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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using Graph = vector<vector<int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  vector<ll> m(100001, 0);
  ll n;
  cin >> n;
  ll ans = 0;
  REP(i, n) {
    ll a;
    cin >> a;
    m[a] += 1;
    ans += a;
  }
  ll q;
  cin >> q;
  REP(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using Graph = vector<vector<int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  vector<ll> m(10001, 0);
  ll n;
  cin >> n;
  ll ans = 0;
  REP(i, n) {
    ll a;
    cin >> a;
    m[a] += 1;
    ans += a;
  }
  ll q;
  cin >> q;
  REP(i, q) {
    ll b, c;
    cin >> b >> c;
    ans += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << ans << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N, Q;
  ll A_sum[100001] = {};
  vector<ll> S;
  cin >> N;
  ll total = 0;

  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    A_sum[A] += A;
    total += A;
  }
  cin >> Q;
  for (ll i = 0; i < Q; i++) {
    ll B, C;
    cin >> B >> C;
    total += (A_sum[B] / B) * C - A_sum[B];
    A_sum[C] += (A_sum[B] / B) * C;
    A_sum[B] = 0;
    S.push_back(total);
  }
  for (ll i = 0; i < Q; i++) {
    cout << S[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, Q;
  int A_count[100001] = {};
  vector<int> S;
  cin >> N;

  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    A_count[A]++;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    int sum = 0;
    cin >> B >> C;
    A_count[C] += A_count[B];
    A_count[B] = 0;
    for (int j = 1; j < 100001; j++) {
      sum += j * A_count[j];
    }
    S.push_back(sum);
  }
  for (int i = 0; i < Q; i++) {
    cout << S[i] << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  ;
  vector<int> A(N);
  map<int, int> count;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    count[A.at(i)]++;
  }

  int Q;
  cin >> Q;

  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }

  long long S = 0;
  for (int i = 0; i < N; i++)
    S += A.at(i);

  for (int i = 0; i < Q; i++) {
    S += ((long long)C.at(i) - (long long)B.at(i)) * (long long)count[B.at(i)];
    cout << S << endl;
    count[C.at(i)] += count[B.at(i)];
    count[B.at(i)] = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int swi(vector<int> *a, int b, int c, int n) {
  int count = 0;

  for (int i = 0; i < n; i++) {
    if (a->at(i) == b) {
      a->at(i) = c;
      count++;
    }
  }

  return count * (c - b);
}

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

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

  int Q;
  cin >> Q;

  vector<int> B(Q), C(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i) >> C.at(i);
  }

  long long S = 0;
  for (int i = 0; i < N; i++) {
    S += A.at(i);
  }

  for (int i = 0; i < Q; i++) {
    S += swi(&A, B.at(i), C.at(i), N);
    cout << S << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, sum = 0;
  unordered_map<int, int> temp;
  cin >> n;
  long long int a[2000005];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
    temp[a[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    long long int b, c;
    cin >> b >> c;
    if (temp.find((int)b) != temp.end()) {
      sum = sum - b * temp[b];
      sum = sum + c * temp[b];
      temp[c] = temp[c] + temp[b];
      temp[b] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int n;

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

  while (q--) {
    long long int b, c, sum = 0;
    cin >> b >> c;
    for (int i = 0; i < n; i++) {
      if (a[i] == b) {
        replace(a, a + n, a[i], c);
      }
    }

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

    cout << sum << endl;
  }

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

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

using namespace std;

vector<string> split(const string &s, char delim) {
  vector<string> elems;
  string item;
  for (char ch : s) {
    if (ch == delim) {
      if (!item.empty()) {
        elems.push_back(item);
      }
      item.clear();
    } else {
      item += ch;
    }
  }
  if (!item.empty()) {
    elems.push_back(item);
  }
  return elems;
}

string to_str_with_zero(int i, int w) {
  ostringstream sout;
  sout << std::setfill('0') << std::setw(w) << i;
  string s = sout.str();
  return s;
}

int letter_to_int(char c) { return tolower(c) - 'a'; }

bool array_equal(vector<int> a1, vector<int> a2) {
  if (a1.size() != a2.size()) {
    return false;
  }
  for (int i = 0; i < a1.size(); i++) {
    if (a1.at(i) != a2.at(i)) {
      return false;
    }
  }
  return true;
}

int gcd(int a, int b) {
  if (a == b) {
    return a;
  } else if (a > b) {
    return gcd(a - b, b);
  } else {
    return gcd(a, b - a);
  }
}

int main() {
  std::cout << std::setprecision(9);
  int n;
  cin >> n;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += a_count.at(b) * (c - b);
    a_count.at(c) += a_count.at(b);
    a_count.at(b) = 0;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

using namespace std;

vector<string> split(const string &s, char delim) {
  vector<string> elems;
  string item;
  for (char ch : s) {
    if (ch == delim) {
      if (!item.empty()) {
        elems.push_back(item);
      }
      item.clear();
    } else {
      item += ch;
    }
  }
  if (!item.empty()) {
    elems.push_back(item);
  }
  return elems;
}

string to_str_with_zero(int i, int w) {
  ostringstream sout;
  sout << std::setfill('0') << std::setw(w) << i;
  string s = sout.str();
  return s;
}

int letter_to_int(char c) { return tolower(c) - 'a'; }

bool array_equal(vector<int> a1, vector<int> a2) {
  if (a1.size() != a2.size()) {
    return false;
  }
  for (int i = 0; i < a1.size(); i++) {
    if (a1.at(i) != a2.at(i)) {
      return false;
    }
  }
  return true;
}

int gcd(int a, int b) {
  if (a == b) {
    return a;
  } else if (a > b) {
    return gcd(a - b, b);
  } else {
    return gcd(a, b - a);
  }
}

int main() {
  std::cout << std::setprecision(9);
  int n;
  cin >> n;

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

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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < (n); ++i)
#define repeq(i, a, n) for (int i = a, i <= (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
/*
int main(){
    int n,q;
    cin >> n;
    vector<int> a(n);
    rep(i,0,n){
        cin >> a.at(i);
    }

    cin >> q;
    vector<int> b(q);
    vector<int> c(q);
    vector<ll> s(q);
    int ans=0;

    rep(i,0,q){
        cin >> b.at(i) >> c.at(i);
    }

    int count=0;
    rep(i,0,q){
        ans=0;
        count=0;
        rep(j,0,n){
            if(a.at(j)==b.at(i)){

                a.at(j)=c.at(i);
            }
            ans+=a.at(j);
        }
        cout << ans << endl;
    }
    return 0;
}
*/
const int MX = 100005;

int main() {
  int n;
  // Nの読み込み
  cin >> n;
  // それぞれの値が何個ずつ入っているかの配列
  vector<ll> cnt(MX);
  // aの入力を受けたらaの個数を+1していく
  rep(i, 0, n) {
    int a;
    cin >> a;
    cnt[a]++;
  }

  ll total = 0;
  rep(i, 0, MX) { total += i * cnt[i]; }

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

  return 0;
}


Error: TLE

Fixed submission:

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

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

  cin >> q;
  vector<int> b(q);
  vector<int> c(q);
  vector<ll> s(q);
  int ans = 0;

  rep(i, 0, q) { cin >> b.at(i) >> c.at(i); }

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

#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {
  long N, Q;
  vector<long> index(100001, 0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    index[tmp]++;
  }
  cin >> Q;
  vector<vector<long>> B(Q, vector<long>(2, 0));
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i).at(0) >> B.at(i).at(1);
  }
  long sum = 0;
  for (int i = 0; i < 100001; i++) {
    sum += i * index[i];
  }

  for (int i = 0; i < Q; i++) {
    long tmp1, tmp2;
    tmp1 = B.at(i).at(0);
    tmp2 = B.at(i).at(1);
    sum += (tmp2 - tmp1) * index[tmp1];
    index[tmp2] += index[tmp1];
    index[tmp1] = 0;
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {
  long N, Q;
  vector<long> A;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
  }
  cin >> Q;
  vector<vector<long>> B(Q, vector<long>(2, 0));
  for (int i = 0; i < Q; i++) {
    cin >> B.at(i).at(0) >> B.at(i).at(1);
  }

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (B.at(i).at(0) == A[j])
        A[j] = B.at(i).at(1);
    }
    cout << accumulate(A.begin(), A.end(), 0) << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define ll long long

int num[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q, t, i;
  ll sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> t;
    num[t]++;
    sum += t;
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    num[c] += num[b];
    sum += (c - b) * num[b];
    num[b] = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define ll long long

int num[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q, i;
  ll sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> num[i];
    sum += num[i];
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int rel = c - b, ctr = 0;
    for (i = 0; i < n; i++) {
      if (num[i] == b) {
        num[i] = c;
        ctr++;
      }
    }
    sum += rel * ctr;
    cout << sum << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long ll;

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define AA(i) cout << i << endl;
#define AS(i) cout << i << " ";

const int INF = 1001001001;

const double PI = 3.14159;

using namespace std;

// vector<ll> memo(1000007, INF);

int main() {

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

  int q;
  cin >> q;
  int b, c;

  rep(i, q) {
    cin >> b >> c;
    sum += (c - b) * map[b];
    map[c] += map[b];
    map[b] = 0;

    AA(sum);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define AA(i) cout << i << endl;
#define AS(i) cout << i << " ";

const int INF = 1001001001;

const double PI = 3.14159;

using namespace std;

// vector<ll> memo(1000007, INF);

int main() {

  int n;
  cin >> n;
  vector<int> map(10e5 + 1, 0);
  int a;
  rep(i, n) {
    cin >> a;
    map[a]++;
  }

  int q;
  cin >> q;
  int b, c;

  rep(i, q) {
    cin >> b >> c;
    map[c] += map[b];
    map[b] = 0;

    ll sum = 0;
    rep(j, map.size()) { sum += j * map[j]; }
    AA(sum);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N;
  cin >> N;
  ll A;
  vector<ll> vec;
  ll limit = pow(10, 6);
  vec.reserve(limit);
  for (auto i = 0; i < limit; i++) {
    vec.push_back(0);
  }
  ll ret = 0;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    vec[A]++;
    ret += A;
  }
  ll Q, B, C;
  cin >> Q;
  for (auto i = 0; i < Q; i++) {
    cin >> B >> C;
    ret += (C - B) * vec[B];
    vec[C] += vec[B];
    vec[B] = 0;
    cout << ret << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N;
  cin >> N;
  ll A;
  vector<ll> vec;
  vec.reserve(10001);
  for (auto i = 0; i < 10001; i++) {
    vec.push_back(0);
  }
  ll ret = 0;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    vec[A]++;
    ret += A;
  }
  ll Q, B, C;
  cin >> Q;
  for (auto i = 0; i < Q; i++) {
    cin >> B >> C;
    ret += (C - B) * vec[B];
    vec[C] += vec[B];
    vec[B] = 0;
    cout << ret << endl;
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sct(tc) scanf("%lld", &tc)
#define scl(n) scanf("%lld", &n)
#define scll(n, m) scanf("%lld%lld", &n, &m)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
ll ar[100000] = {0};
int main() {
  ll n, i, b, c, q, j, x, sum = 0;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &x);
    ar[x]++;
    sum += x;
  }
  scanf("%lld", &q);
  for (i = 0; i < q; i++) {
    scanf("%lld %lld", &b, &c);
    {
      sum = sum - ar[b] * b + c * ar[b];
      ar[c] += ar[b];
      ar[b] = 0;
    }
    printf("%lld\\n", sum);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sct(tc) scanf("%lld", &tc)
#define scl(n) scanf("%lld", &n)
#define scll(n, m) scanf("%lld%lld", &n, &m)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
ll ar[100000];
int main() {
  ll n, i, sum, b, c, q, j;
  scanf("%lld", &n);
  for (i = 0; i < n; i++)
    scanf("%lld", &ar[i]);
  scanf("%lld", &q);
  for (i = 0; i < q; i++) {
    sum = 0;
    scanf("%lld %lld", &b, &c);
    for (j = 0; j < n; j++) {
      if (ar[j] == b) {
        ar[j] = c;
        sum += c;
      } else
        sum += ar[j];
    }
    printf("%lld\\n", sum);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
using namespace std;
// typedef long long LL;
#define For(a, b, c) for (int a = b; a <= c; a++)
#define For_(a, b, c) for (int a = b; a >= c; a--)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define debug(x) cout << #x << " " << x << " "
int read() {
  int r = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    r = (r << 1) + (r << 3) + c - '0';
    c = getchar();
  }
  return r * f;
}
#define MAXN 100005
int cnt[MAXN], n, q;
long long ans;
int main() {
  n = read();
  For(i, 1, n) {
    int x = read();
    ans += x;
    cnt[x]++;
  }
  q = read();
  For(i, 1, q) {
    int b = read(), c = read();
    ans -= 1ll * cnt[b] * b;
    ans += 1ll * cnt[b] * c;
    printf("%lld\\n", ans);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
using namespace std;
// typedef long long LL;
#define For(a, b, c) for (int a = b; a <= c; a++)
#define For_(a, b, c) for (int a = b; a >= c; a--)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define debug(x) cout << #x << " " << x << " "
int read() {
  int r = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    r = (r << 1) + (r << 3) + c - '0';
    c = getchar();
  }
  return r * f;
}
#define MAXN 10005
int cnt[MAXN], n, q;
long long ans;
int main() {
  n = read();
  For(i, 1, n) {
    int x = read();
    ans += x;
    cnt[x]++;
  }
  q = read();
  For(i, 1, q) {
    int b = read(), c = read();
    ans -= 1ll * cnt[b] * b;
    ans += 1ll * cnt[b] * c;
    printf("%lld\\n", ans);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  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++)
using ll = long long;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) { cin >> B.at(i) >> C.at(i); }
  int MAX = 100001;
  vector<ll> S(Q);
  vector<int> count(MAX, 0);
  rep(i, N) { count.at(A.at(i))++; }

  long sum = 0;
  rep(i, N) { sum += A.at(i); }

  rep(i, Q) {
    int num = count.at(B.at(i));
    int diff = C.at(i) - B.at(i);
    count.at(B.at(i)) = 0;
    count.at(C.at(i)) += num;
    sum += diff * num;
    S.at(i) = sum;
  }

  rep(i, Q) { cout << S.at(i) << 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;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  ll Q;
  cin >> Q;
  vector<ll> B(Q), C(Q);
  rep(i, Q) { cin >> B.at(i) >> C.at(i); }
  int MAX = 100000;
  vector<long> S(Q);
  vector<int> count(MAX, 0);
  rep(i, N) { count.at(A.at(i))++; }

  long sum = 0;
  rep(i, N) { sum += A.at(i); }

  rep(i, Q) {
    int num = count.at(B.at(i));
    int diff = C.at(i) - B.at(i);
    count.at(B.at(i)) = 0;
    count.at(C.at(i)) += num;
    sum += diff * num;
    S.at(i) = sum;
  }

  rep(i, Q) { cout << S.at(i) << endl; }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N;
  cin >> N;
  ll A[N];
  map<ll, ll> map;
  ll sum = 0;
  rep(i, N) {
    cin >> A[i];
    map[A[i] - 1]++;
  }

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

  ll Q;
  cin >> Q;
  ll B[Q], C[Q];
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    sum -= map[B[i] - 1] * (B[i] - 1);
    sum += (map[B[i] - 1]) * (C[i] - 1);
    map[C[i] - 1] += map[B[i] - 1];
    map.erase(B[i] - 1);
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  ll A[N];
  map<ll, ll> map;
  rep(i, N) {
    cin >> A[i];
    map[A[i] - 1]++;
  }
  ll Q;
  cin >> Q;
  ll B[Q], C[Q];
  rep(i, Q) cin >> B[i] >> C[i];
  rep(i, Q) {
    map[C[i] - 1] += map[B[i] - 1];
    map.erase(B[i] - 1);
    ll sum = 0;
    for (auto m : map) {
      sum += m.second * (m.first + 1);
    }
    cout << sum << endl;
  }
}