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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using Vec = vector<int>;

int main() {

  ll n;
  cin >> n;
  Vec vec(100, 0);
  int ct = 0;

  while (n != 0) {
    int x = n % 26;
    if (x == 0) {
      x = 26;
      vec[ct] = x;
      n = (n / 26) - 1;
      ct++;
    } else {
      vec[ct] = x;
      n = n / 26;
      ct++;
    }
  }
  string s = "";

  for (int i = 0; i < ct; i++) {
    char q;

    q = vec[i] + 96;
    s += q;
  }
  reverse(s.begin(), s.end());

  cout << s << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using Vec = vector<int>;

int main() {

  ll n;
  cin >> n;
  ll max = 1000000000000001;
  Vec vec(1000, 0);
  int ct = 0;
  int p = 100;
  while (p > 1) {
    if (n == 0) {
      break;
    }
    vec[ct] = n % 26;
    ll w = n;
    n = n / 26;
    ct++;
    if (w == n) {
      break;
    }
  }
  string s = "";

  for (int i = 0; i < ct; i++) {
    char q;

    q = vec[i] + 96;
    s += q;
  }
  reverse(s.begin(), s.end());

  for (int i = 0; i < ct; i++) {
    if (s[i] == '`' || s[i] == '_') {
      s[i] += 26;
      if (i + 1 == ct) {
        s.erase(s.end());
      } else {
        s[i + 1]--;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  string s;
  char arr[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                  's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  int x = 0, y = 0, count = 0;
  while (true) {
    if (N < 26) {

      break;
    } else {
      if (N % 26 != 0) {
        N--;
        count++;
      } else if (N % 26 == 0 && count == 0) {
        s += arr[25];
        N -= 1;
        N = N / 26;
      } else {
        s += arr[count - 1];
        N = N / 26;
        count = 0;
      }
    }
  }
  if (N == 26) {
    s += arr[0];
  } else if (N >= 1 && N < 26) {
    s += arr[N - 1];
  }

  reverse(s.begin(), s.end());
  cout << s;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

int main() {
  long long N;
  cin >> N;
  string s;
  char arr[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                  's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  int count = 0;
  while (true) {

    if (N < 26 && N >= 1) {

      s += arr[N - 1];
      break;
    } else {
      if (N % 26 != 0) {
        N--;
        count++;
      } else if (N % 26 == 0 && count == 0) {
        s += 'z';
        N -= 1;
        N /= 26;

      } else {
        s += arr[count - 1];
        N = N / 26;
        count = 0;
      }
    }
  }
  reverse(s.begin(), s.end());
  cout << s;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
string a = "abcdefghijklmnopqrstuvwxyz";
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ll x, y, c = 0;
  ll p[1000];
  scanf("%lld", &x);
  int b = 26;
  while (x != 0) {
    x--;
    y = x % 26;
    x = x / 26;
    c++;
    p[c] = y;
  }
  for (int i = c; i >= 1; i--) {
    cout << a[p[i]];
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
string a = "abcdefghijklmnopqrstuvwxyz";
int main() {
  ll x, y, c = 0;
  ll p[1000];
  scanf("%lld", &x);
  int b = 26;
  while (x != 0) {
    x--;
    y = x % 26;
    x = x / 26;
    c++;
    p[c] = y;
  }
  for (int i = c; i >= 1; i--) {
    printf("%s", a[p[i]]);
  }
  return 0;
}
Problem p02629 (C++) — buggy submission, verdict: Runtime Error

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

string f(ll n) {
  if (n == 0)
    return "";
  // 扱いやすいようにデクリメントして
  n--;
  return f(n / 26) + string(1, 'a' + n % 26);
}

int main() {
  ll n;
  cin >> n;
  cout << f(n) << endl;
  return 0;
}

Error: -11

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  // sortの引数には開始位置と終了位置
  sort(p.begin(), p.end());
  int ans = 0;
  // 前からk個取って出力
  rep(i, k) ans += p[i];
  cout << ans << endl;
  return 0;
}
Problem p02629 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  string al = "abcdefghijklmnopqrstuvwxyz";
  string ans = "";
  long long N, rem;
  cin >> N;

  while (N != 0) {

    rem = (N - 1) % 26;
    ans += al.at(rem);
    N = (N - rem + 1) / 26;
  }

  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {

  string al = "abcdefghijklmnopqrstuvwxyz";
  long long N, n, i;
  cin >> N;

  long long keta = 0;
  while (N - pow(26, keta) > 0) {
    keta++;
  }
  keta--;

  while (keta != -1) {

    int Si = 0;
    while (N - Si * pow(26, keta) >= 0) {
      Si++;
    }
    cout << al.at(Si - 2);
    N = N - (Si - 1) * pow(26, keta);
    keta--;
  }
  return 0;
}
Problem p02629 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int k, i;
long long n;
string s;
int main() {
  cin >> n;
  while (n != 0) {
    n -= 1;
    k = n % 26;
    n /= 26;
    s += char(k + 97);
  }
  reverse(s.begin(), s.end());
  if (n != 1)
    cout << s;
  else
    cout << "a";
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int k, i;
long long n;
string s;
int main() {
  cin >> n;
  n -= 1;
  while (n != 0) {
    k = n % 26;
    n /= 26;
    s += char(k + 97);
  }
  reverse(s.begin(), s.end());
  for (i = 0; i < s.size() - 1; i++)
    s[i] = char(s[i] - 1);
  cout << s;
}
Problem p02629 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  vector<char> alpha;
  alpha.push_back('z');
  for (char i = 'a'; i <= 'z'; i++) {
    alpha.push_back(i);
  }

  string ans;
  if (n > 26LL) {
    n -= 1;
  } else {
    cout << alpha.at(n % 26) << endl;
    ;
    return 0;
  }

  while (n >= 26LL) {
    ll tmp = (n + 1) % 26LL;
    if (tmp != 0)
      ans.push_back(alpha.at(tmp));
    else
      ans.push_back(alpha.at(26));
    n -= 26;
    n /= 26;
  }

  dump(n);
  ans += alpha.at(n + 1);
  reverse(ans.begin(), ans.end());

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  vector<char> alpha;
  alpha.push_back('z');
  for (char i = 'a'; i <= 'z'; i++) {
    alpha.push_back(i);
  }

  string ans;
  if (n > 26LL) {
    n -= 1;
  } else {
    cout << alpha.at(n % 26) << endl;
    ;
    return 0;
  }

  while (n > 26LL) {
    ll tmp = (n + 1) % 26LL;
    if (tmp != 0)
      ans.push_back(alpha.at(tmp));
    else
      ans.push_back(alpha.at(26));
    n -= 26;
    n /= 26;
  }

  dump(n);
  ans += alpha.at(n + 1);
  reverse(ans.begin(), ans.end());

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll ans;
  cin >> ans;
  string str = "";
  while (ans > 0) {
    int m = ans % 26;
    if (m == 0)
      m = 26;
    str += (char)(m + 'a' - 1);
    ans = (ans - m) / 26;
  }
  reverse(str.begin(), str.end());
  cout << str << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
ll roots[15];
int ans[10];
void getit(ll n) {
  int i;
  for (i = 1; i <= 10; i++)
    if (pow(26, i) > n)
      break;
  i--;
  while (n > 0) {
    ll p = pow(26, i), k;
    int j;
    for (j = 1; j < 26; j++) {
      k = p * j;
      if (n < k)
        break;
    }
    ans[i] = j - 1;
    n -= k - p;
    i--;
  }
}
int main() {
  ll n;
  cin >> n;
  ll k = 1, q = 0;
  while (roots[q - 1] <= n) {
    k *= 26;
    roots[q++] = k + roots[q - 1];
  }
  getit(n);
  for (int i = q - 1; i >= 0; i--) {
    printf("%c", 'a' + ans[i] - 1);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 100005;
#define ll long long
int cmp(int a, int b) { return a > b; }
int gcd(int x, int y) {
  while (y ^= x ^= y ^= x %= y)
    ;
  return x;
}
int a[100005];
int t[100005];
int x, y;
ll s = 0;
int main() {
  int n, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    t[x]++;
    s += x;
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    if (t[x] > 0) {
      s += (y - x) * t[x];
      t[y] += t[x];
      t[x] = 0;
    }
    cout << s << endl;
  }

  //	cin >> k;
  //	for(int i=0;i<k;i++){
  //		cin >> x >> y;
  //		if(t[x]>0) {
  //			t[x]=0;
  //			t[y]+=t[x];
  //			s=s+(y-x)*t[x];
  //		}
  //		cout << s <<endl;
  //	}
  return 0;
}

// 1000000000000001


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 100005;
#define ll long long
int cmp(int a, int b) { return a > b; }
int gcd(int x, int y) {
  while (y ^= x ^= y ^= x %= y)
    ;
  return x;
}
int a[100005];
int x, y;
int main() {
  int n, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    int s = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] == x) {
        a[j] = y;
      }
      s += a[j];
    }
    cout << s << endl;
  }
  return 0;
}

// 1000000000000001
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const unsigned int m = 1000000007;

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;
  // int arr[n];
  unordered_map<int, int> m;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int ele;
    cin >> ele;
    m[ele]++;
    sum += ele;
  }

  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;

    int count = m[a];

    m[a] = 0;
    m[b] += count;
    ll new_sum = sum + (b - a) * count;
    cout << new_sum << endl;
    sum = new_sum;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const unsigned int m = 1000000007;

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;
  int arr[n];
  int sum = 0;
  for (int i = 0; i < n; i++)
    cin >> arr[i], sum += arr[i];

  // cout << sum << endl;

  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;

    int count = 0;
    for (int i = 0; i < n; i++)
      if (arr[i] == a) {
        arr[i] = b;
        count++;
      }
    int new_sum = sum + (b - a) * count;
    cout << new_sum << endl;
    sum = new_sum;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll q;
  cin >> q;
  unordered_map<int, int> m;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll sum = 0;
    for (ll i = 0; i < n; i++) {
      if (arr[i] == a) {
        arr[i] = b;
      }
      sum += arr[i];
    }

    //	for(ll i =0;i<n;i++)sum+=arr[i];
    cout << sum << endl;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define f first
#define s second
#define ins insert
#define pb push_back
#define endl '\\n'
#define ll long long
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define sz(c) (int)(c).size()
#define trav(x, c) for (auto &x : c)
#define all(c) (c).begin(), (c).end()
#define rll(c) (c).rbegin(), (c).rend()
#define db(x) cout << #x << ": " << x << endl;
#define _db(c)                                                                 \\
  trav(x, c) cout << x << " ";                                                 \\
  cout << endl;
#define godspeed                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

int a[100005];
int main() {
  godspeed;

  ll n, sum = 0;
  cin >> n;
  vl v(n);
  for (ll i = 0; i < n; ++i) {
    cin >> v[i];
    sum += v[i];
    a[v[i]]++;
  }
  sort(all(v));

  ll k;
  cin >> k;
  ll lsum = sum;
  while (k--) {
    ll x, y;
    cin >> x >> y;
    lsum += (y - x) * a[x];
    cout << lsum << endl;
    a[y] += a[x];
    a[x] = 0;
  }
}

Error: TLE

Fixed submission:

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

#define f first
#define s second
#define ins insert
#define pb push_back
#define endl '\\n'
#define ll long long
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define sz(c) (int)(c).size()
#define trav(x, c) for (auto &x : c)
#define all(c) (c).begin(), (c).end()
#define rll(c) (c).rbegin(), (c).rend()
#define db(x) cout << #x << ": " << x << endl;
#define _db(c)                                                                 \\
  trav(x, c) cout << x << " ";                                                 \\
  cout << endl;
#define godspeed                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

int main() {
  godspeed;

  ll n, sum = 0;
  cin >> n;
  vl v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    sum += v[i];
  }
  sort(all(v));

  int k;
  cin >> k;
  int lsum = sum;
  while (k--) {
    int x, y;
    cin >> x >> y;
    int mycount = count(all(v), x);
    lsum += mycount * (y - x);
    cout << lsum << endl;
    replace(all(v), x, y);
  }
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(100005);
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    ll k;
    cin >> k;

    sum += k;
    a[k]++;
  }
  ll q;
  cin >> q;

  while (q--) {
    ll b, c;
    cin >> b >> c;
    sum -= a[b] * b;
    sum += a[b] * c;

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

int 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 += a[i];
    }

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

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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  long long int n, sum = 0;
  scanf("%lld", &n);
  long long int num[100005] = {0};
  long long int book[100005] = {0};
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &num[i]);
    book[num[i]]++;
    sum += num[i];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    if (book[b] == 0) {
      printf("%lld\\n", sum);
      continue;
    } else {
      book[c] += book[b];
      sum = sum + (c - b) * book[b];
      book[b] = 0;
      printf("%lld\\n", sum);
      continue;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int num[100005] = {0};
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b, c;
    scanf("%d %d", &b, &c);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (num[i] == b)
        num[i] = c;
      sum += num[i];
    }
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define brep(index, num) for (int index = num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = num; index > 0; index--)
#define prin(a) cout << a << "\\n"
#define kaigyo cout << "\\n"
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SORT(V) sort(ALL(V))
#define REV(V) reverse(ALL(V))
#define RSORT(V)                                                               \\
  SORT(V);                                                                     \\
  REV(V)

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<string> vst;
typedef vector<char> vc;
typedef vector<long double> vd;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
typedef vector<vector<int>> vint2;
ll mod = 1e9 + 7;

const double pi = acos(-1.0);
const ll inf = 1LL << 60;
ll gcd(ll p, ll q) {
  if (q == 0)
    return p;
  return gcd(q, p % q);
}
ll lcm(ll p, ll q) { return (p * q) / gcd(p, q); }
ll kaizyo(ll k) {
  ll sum = 1;
  rep1(i, k) {
    sum *= i;
    sum %= mod;
  }
  return sum;
}
ll ncr(ll n, ll r) { return kaizyo(n) / (kaizyo(r) * kaizyo(n - r)); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(20);
  int n;
  cin >> n;
  vint a(n);
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  vint b(q), 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];
    }
    ll p = accumulate(ALL(a), 0LL);
    prin(p);
  }

  /*
 ll moto=accumulate(ALL(a),0LL);

 rep(i,q){
   int p=count(ALL(a),b[i]);
 prin(moto+p*(c[i]-b[i]));
 }*/
}
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 (int i = 0; i < (n); i++)

const int MX = 100005;

int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX); //{0,0,...,0}が初期値
  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 += b * cnt[b];
    tot += c * cnt[c];
    cout << tot << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>   //"M_PI"→円周率(小数点以下5桁)
#include <iomanip> //fixed<<setprecision(15)<<
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define LL long long
#define E "\\n"
#define PI 3.141592653589793

int main() {
  int N, Q, cnt1, cnt2, A_begin, plus;
  cin >> N;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  cin >> Q;
  vector<vector<int>> BC(Q, vector<int>(2));
  rep(i, Q) {
    rep(j, 2) { cin >> BC.at(i).at(j); }
  }
  // ここまでimput
  rep(i, N) { A_begin += A[i]; }
  rep(i, Q) {
    cnt1 = count(A.begin(), A.end(), BC.at(i).at(0));
    replace(A.begin(), A.end(), BC.at(i).at(0), BC.at(i).at(1));
    plus = (BC.at(i).at(1) - BC.at(i).at(0)) * cnt1;
    A_begin += plus;
    cout << A_begin << E;
    cnt1 = 0;
    cnt2 = 0;
  }
}
Problem p02630 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int n;
  cin >> n;
  ll tot = 0;
  vector<ll> cnt(100005);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    tot += a;
  }

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

Error: 0

Fixed submission:

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

int main(void) {
  int n;
  cin >> n;
  ll tot = 0;
  vector<ll> cnt(n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    tot += a;
  }

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