prithivMLmods/Coder-Stat huggingface dataset:
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define SREP(s, i, m) for (unsigned int i = s; i < m; ++i)
#define REP(i, m) SREP(0, i, m)

using namespace std;
#ifdef _MSC_VER
using LONG = __int64;
#else
using LONG = long long int;
#endif

int main(void) {
  int W, H;
  vector<pair<int, int>> p; // cost, i
  vector<pair<int, int>> q; // cost, i
  cin >> W >> H;
  int w_need = W + 1;
  int h_need = H + 1;
  for (int i = 0; i < W; i++) {
    int cost;
    cin >> cost;
    p.push_back(make_pair(cost, i));
  }
  for (int i = 0; i < H; i++) {
    int cost;
    cin >> cost;
    q.push_back(make_pair(cost, i));
  }
  sort(p.begin(), p.end());
  sort(q.begin(), q.end());
  int qindex = 0, pindex = 0;
  LONG totalCost = 0;
  while (pindex < W || qindex < H) {
    if (pindex < p.size() && p[pindex].first <= q[qindex].first) {
      totalCost += (LONG)h_need * p[pindex].first;
      w_need--;
      w_need = max(w_need, 1);
      pindex++;
    } else {
      totalCost += (LONG)w_need * q[qindex].first;
      h_need--;
      h_need = max(h_need, 1);
      qindex++;
    }
  }
  cout << totalCost << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define SREP(s, i, m) for (unsigned int i = s; i < m; ++i)
#define REP(i, m) SREP(0, i, m)

using namespace std;
#ifdef _MSC_VER
using LONG = __int64;
#else
using LONG = long long int;
#endif

int main(void) {
  int W, H;
  vector<pair<int, int>> p; // cost, i
  vector<pair<int, int>> q; // cost, i
  cin >> W >> H;
  int w_need = W + 1;
  int h_need = H + 1;
  for (int i = 0; i < W; i++) {
    int cost;
    cin >> cost;
    p.push_back(make_pair(cost, i));
  }
  for (int i = 0; i < H; i++) {
    int cost;
    cin >> cost;
    q.push_back(make_pair(cost, i));
  }
  sort(p.begin(), p.end());
  sort(q.begin(), q.end());
  int qindex = 0, pindex = 0;
  LONG totalCost = 0;
  while (pindex < W || qindex < H) {
    if (qindex == q.size() ||
        pindex < p.size() && p[pindex].first <= q[qindex].first) {
      totalCost += (LONG)h_need * p[pindex].first;
      w_need--;
      w_need = max(w_need, 1);
      pindex++;
    } else {
      totalCost += (LONG)w_need * q[qindex].first;
      h_need--;
      h_need = max(h_need, 1);
      qindex++;
    }
  }
  cout << totalCost << endl;
  return 0;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
#define rep(p, q) for (int i = p; i < q; i++)

int main(void) {
  int width = 0, height = 0;
  vector<long int> cost_w;
  vector<long int> cost_h;
  int count_w = 0, count_h = 0;
  long int total_cost = 0;
  cin >> width >> height;
  rep(0, width) {
    long int cost;
    cin >> cost;
    cost_w.push_back(cost);
  }
  rep(0, height) {
    long int cost;
    cin >> cost;
    cost_h.push_back(cost);
  }
  sort(cost_w.begin(), cost_w.end());
  sort(cost_h.begin(), cost_h.end());

  cost_w.push_back((long int)20000000);
  cost_h.push_back((long int)20000000);

  while (width != 0 || height != 0) {
    if (cost_w[count_w] <= cost_h[count_h]) {
      total_cost += cost_w[count_w] * (height + 1);
      width--;
      count_w++;
    } else {
      total_cost += cost_h[count_h] * (width + 1);
      height--;
      count_h++;
    }
  }
  cout << total_cost << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
#define rep(p, q) for (int i = p; i < q; i++)

int main(void) {
  int width = 0, height = 0;
  vector<long int> cost_w;
  vector<long int> cost_h;
  int count_w = 0, count_h = 0;
  long int total_cost = 0;
  cin >> width >> height;
  rep(0, width) {
    long int cost;
    cin >> cost;
    cost_w.push_back(cost);
  }
  rep(0, height) {
    long int cost;
    cin >> cost;
    cost_h.push_back(cost);
  }
  sort(cost_w.begin(), cost_w.end());
  sort(cost_h.begin(), cost_h.end());

  cost_w.push_back((long int)200000000);
  cost_h.push_back((long int)200000000);

  while (width != 0 || height != 0) {
    if (cost_w[count_w] <= cost_h[count_h]) {
      total_cost += cost_w[count_w] * (height + 1);
      width--;
      count_w++;
    } else {
      total_cost += cost_h[count_h] * (width + 1);
      height--;
      count_h++;
    }
  }
  cout << total_cost << endl;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<lint, lint>;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint W, H;
  cin >> W >> H;
  vector<lint> p(W);
  REP(i, W) cin >> p[i];
  vector<lint> q(H);
  REP(i, H) cin >> q[i];
  vector<P> s;
  REP(i, W) s.push_back(make_pair(p[i], 0));
  REP(i, H) s.push_back(make_pair(q[i], 1));
  sort(ALL(s), [](auto const &a, auto const &b) { return a.first < b.first; });
  lint ans = 0;
  lint zeros = 0;
  lint ones = 0;
  REP(i, W * H) {
    if (s[i].second == 0) {
      ans += s[i].first * (H - ones + 1);
      zeros++;
    } else {
      ans += s[i].first * (W - zeros + 1);
      ones++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<lint, lint>;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint W, H;
  cin >> W >> H;
  vector<lint> p(W);
  REP(i, W) cin >> p[i];
  vector<lint> q(H);
  REP(i, H) cin >> q[i];
  vector<P> s;
  REP(i, W) s.push_back(make_pair(p[i], 0));
  REP(i, H) s.push_back(make_pair(q[i], 1));
  sort(ALL(s), [](auto const &a, auto const &b) { return a.first < b.first; });
  lint ans = 0;
  lint zeros = 0;
  lint ones = 0;
  REP(i, W + H) {
    if (s[i].second == 0) {
      ans += s[i].first * (H - ones + 1);
      zeros++;
    } else {
      ans += s[i].first * (W - zeros + 1);
      ones++;
    }
  }
  cout << ans << endl;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;

const int MAX_N = (int)10;
lint P[MAX_N], Q[MAX_N], SP[MAX_N];

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  lint W, H;
  cin >> W >> H;
  for (int i = 0; i < W; i++)
    cin >> P[i];
  for (int i = 0; i < H; i++)
    cin >> Q[i];

  sort(P, P + W);
  for (int i = 0; i < W; i++)
    SP[i + 1] = SP[i] + P[i];

  lint ans = SP[W];
  for (int i = 1; i <= H; i++) {
    int idx = distance(P, upper_bound(P, P + W, Q[i - 1]));
    ans += SP[idx] + (W - idx + 1) * Q[i - 1];
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;

const int MAX_N = (int)1e5 + 5;
lint P[MAX_N], Q[MAX_N], SP[MAX_N];

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  lint W, H;
  cin >> W >> H;
  for (int i = 0; i < W; i++)
    cin >> P[i];
  for (int i = 0; i < H; i++)
    cin >> Q[i];

  sort(P, P + W);
  for (int i = 0; i < W; i++)
    SP[i + 1] = SP[i] + P[i];

  lint ans = SP[W];
  for (int i = 1; i <= H; i++) {
    int idx = distance(P, upper_bound(P, P + W, Q[i - 1]));
    ans += SP[idx] + (W - idx + 1) * Q[i - 1];
  }

  cout << ans << endl;
  return 0;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

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

using namespace std;
using lint = long long;

int main() {
  int w, h;
  scanf("%d%d", &w, &h);
  vector<lint> p(w), q(h);
  rep(i, w) scanf("%lld", &p[i]);
  rep(i, h) scanf("%lld", &q[i]);

  sort(p.begin(), p.end());
  sort(q.begin(), q.end());

  lint ans = 0;
  int ip = 0, iq = 0;
  while (ip < w || iq < h) {
    if (ip == w || p[ip] >= q[iq]) {
      ans += (w - ip + 1) * q[iq++];
    } else {
      ans += (h - iq + 1) * p[ip++];
    }
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

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

using namespace std;
using lint = long long;

int main() {
  int w, h;
  scanf("%d%d", &w, &h);
  vector<lint> p(w), q(h);
  rep(i, w) scanf("%lld", &p[i]);
  rep(i, h) scanf("%lld", &q[i]);

  sort(p.begin(), p.end());
  sort(q.begin(), q.end());

  lint ans = 0;
  int ip = 0, iq = 0;
  while (ip < w || iq < h) {
    if (ip == w || (iq < h && p[ip] >= q[iq])) {
      ans += (w - ip + 1) * q[iq++];
    } else {
      ans += (h - iq + 1) * p[ip++];
    }
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p03973 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
long long MOD = 1000000007;
long long INF = 1000000000000000; // 10^15
typedef long long ll;
typedef unsigned long long ull;

int main(void) {

  int n;
  ll a[15000];
  cin >> n;
  rep(i, n) cin >> a[i];

  ll ans = a[0] - 1;
  ll k = 2;
  for (int i = 1; i < n; i++) {
    if (a[i] == k)
      k++;
    if (a[i] % k == 0)
      ans += a[i] / k - 1;
    else
      ans += a[i] / k;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
long long MOD = 1000000007;
long long INF = 1000000000000000; // 10^15
typedef long long ll;
typedef unsigned long long ull;

int main(void) {

  int n;
  ll a[150000];
  cin >> n;
  rep(i, n) cin >> a[i];

  ll ans = a[0] - 1;
  ll k = 2;
  for (int i = 1; i < n; i++) {
    if (a[i] == k)
      k++;
    if (a[i] % k == 0)
      ans += a[i] / k - 1;
    else
      ans += a[i] / k;
  }

  cout << ans << endl;

  return 0;
}
Problem p03973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vll vector<ll>
#define rep(c, a, b) for (ll c = a; c < b; c++)
#define re(c, b) for (ll c = 0; c < b; c++)
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll p, n;
  std::cin >> n;
  deque<ll> h;
  re(i, n) {
    std::cin >> p;
    h.push_back(p);
  }
  ll c = 0, now = 0;
  while (!h.empty()) {
    if (h.front() <= now + 1) {
      if (h.front() == now + 1)
        now++;
      h.pop_front();
    } else {
      if (h.front() <= 2 * (now + 1)) {
        h.front() = 1, c++;
      } else {
        h.front() -= now + 1, c++;
      }
    }
  }
  std::cout << c << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vll vector<ll>
#define rep(c, a, b) for (ll c = a; c < b; c++)
#define re(c, b) for (ll c = 0; c < b; c++)
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll p, n;
  std::cin >> n;
  deque<ll> h;
  re(i, n) {
    std::cin >> p;
    h.push_back(p);
  }
  ll c = 0, now = 0;
  while (!h.empty()) {
    if (h.front() <= now + 1) {
      if (h.front() == now + 1)
        now++;
      h.pop_front();
    } else {
      if (h.front() <= 2 * (now + 1)) {
        h.front() = 1, c++;
      } else {
        c += 2 * (h.front() / (2 * (now + 1))), h.front() %= (2 * (now + 1));
        if (h.front() <= (now + 1))
          c--, h.front() += now + 1;
        if (h.front() <= (now + 1))
          c--, h.front() += now + 1;
      }
    }
  }
  std::cout << c << '\n';
  return 0;
}
Problem p03973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

const int SIZE = 100000;
int N, A[SIZE];

int main(void) {
  cin >> N;
  REP(i, 0, N) cin >> A[i];

  int m = 1;
  long cnt = 0;

  REP(i, 0, N) {
    if (A[i] > m) {
      while (A[i] > m) {
        A[i] -= m;
        cnt++;
      }
      A[i] = max(m - 1, 1);
    }
    if (A[i] == m) {
      m++;
    }
  }

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

const int SIZE = 100000;
int N, A[SIZE];

int main(void) {
  cin >> N;
  REP(i, 0, N) cin >> A[i];

  int m = 1;
  long cnt = 0;

  REP(i, 0, N) {
    if (A[i] > m) {
      cnt += (A[i] - 1) / m;
      A[i] = max(m - 1, 1);
    }
    if (A[i] == m) {
      m++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03973 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 *    FileName:
 *      Source:
 *        Date:
 *      Author: Mew
 *	  Function:
 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int n;
long long a[100008];
int main() {
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  int h = 0, now = 1;
  while (h < n) {
    while (a[h] + 1 == a[now] && h < n) {
      h = now;
      now++;
      while (now <= n && a[now] <= a[h])
        now++;
      if (now > n)
        break;
    }
    if (h >= n || now > n)
      break;
    ans++;
    a[now] -= a[h] + 1;
    if (a[now] <= a[h] + 1)
      a[now] = 1;
    while (now <= n && a[now] <= a[h])
      now++;
    if (now > n)
      break;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*
 *    FileName:
 *      Source:
 *        Date:
 *      Author: Mew
 *	  Function:
 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int n;
long long a[100008];
int main() {
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  int h = 0, now = 1;
  while (h < n) {
    while (a[h] + 1 == a[now] && h < n) {
      h = now;
      now++;
      while (now <= n && a[now] <= a[h])
        now++;
      if (now > n)
        break;
    }
    if (h >= n || now > n)
      break;
    int t = (a[now] - 1) / (a[h] + 1);
    ans += t;
    a[now] = 1;
    while (now <= n && a[now] <= a[h])
      now++;
    if (now > n)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p03973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define len(val) static_cast<long long>(val.size())
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  ll A[N];
  rep(i, N) cin >> A[i];

  ll nmax = 0;
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    if (A[i] <= nmax)
      continue;
    if (A[i] > nmax + 1) {
      int tmp = ans;
      int a = A[i];
      while (A[i] > nmax + 1) {
        A[i] -= (nmax + 1);
        ans++;
      }
      A[i] = 1;
      if (nmax == 0) {
        nmax = 1;
      }
    } else {
      nmax++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define len(val) static_cast<long long>(val.size())
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  ll A[N];
  rep(i, N) cin >> A[i];

  ll nmax = 0;
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    if (A[i] <= nmax)
      continue;
    if (A[i] > nmax + 1) {
      int tmp = ans;
      int a = A[i];
      // while(A[i] > nmax+1){
      //   A[i] -= (nmax+1);
      //   ans++;
      // }
      // cout << (ans-tmp) << " " << floor((double)(a-1)/(double)(nmax+1)) <<
      // endl;
      ans += floor((double)(a - 1) / (double)(nmax + 1));
      A[i] = 1;
      if (nmax == 0) {
        nmax = 1;
      }
    } else {
      nmax++;
    }
  }
  cout << ans << endl;
}
Problem p03973 (C++)

Buggy submission (status: 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;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  ll result = 0;
  int M = 0;
  REP(i, N) {
    int q = (A[i] - 1) / (M + 1);
    REP(j, q) {
      A[i] -= M + 1;
      if (j == q - 1 && M > 0 && A[i] > M) {
        A[i] = M;
      }
    }
    result += q;
    M = max(M, A[i]);
  }
  cout << result << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

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

using namespace std;

using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  ll result = 0;
  int M = 0;
  REP(i, N) {
    int q = (A[i] - 1) / (M + 1);
    if (q) {
      A[i] -= q * (M + 1);
      if (M > 0 && A[i] > M) {
        A[i] = M;
      }
    }
    result += q;
    M = max(M, A[i]);
  }
  cout << result << endl;

  return 0;
}
Problem p03973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = a[0] - 1;
  ll p = ans;
  reps(i, 1, n) {
    if (a[i] < p)
      continue;
    if (a[i] == p) {
      p++;
    } else {
      ans += (a[i] - 1) / p;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = a[0] - 1;
  ll p = 2;
  reps(i, 1, n) {
    if (a[i] < p)
      continue;
    if (a[i] == p) {
      p++;
    } else {
      ans += (a[i] - 1) / p;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03974 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
// using std::stdin;
// using std::stdout;
// using std::freopen;

const int MXN = 100010;
const int MXL = 400060;
const int M = 26;

int trie[MXL][M], sum[MXL], len[MXL], equal[MXL], tot = 1;
bool end[MXL];
int n, q;
string str[MXN];

inline void ins(const string &s) {
  int p = 0;
  //++sum[p];
  for (string::size_type i = 0; i != s.size(); ++i) {
    if (trie[p][s[i] - 'a'] == 0)
      trie[p][s[i] - 'a'] = ++tot;
    p = trie[p][s[i] - 'a'];
    ++sum[p];
    len[p] = i + 1;
    equal[p] = p;
  }
  end[p] = true;
}

void clean(int p) {
  for (int i = 0; i != M; ++i)
    if (trie[p][i] != 0) {
      clean(trie[p][i]);
      if (sum[trie[p][i]] == sum[p])
        equal[p] = equal[trie[p][i]];
    }
}

inline int find(const string &s, const string &o) {
  int p = 0, res = 0;
  for (string::size_type i = 0; i < s.size();) {
    for (string::size_type j = 0; o[j] != s[i]; ++j)
      res += sum[trie[p][o[j] - 'a']];
    res += (int)end[p];
    p = equal[trie[p][s[i] - 'a']];
    i = len[p];
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i != n; ++i)
    cin >> str[i];
  for (int i = 0; i != n; ++i)
    ins(str[i]);
  // clean(0);

  cin >> q;
  while (q--) {
    int k;
    string order;
    cin >> k >> order;
    cout << find(str[k - 1], order) + 1 << endl;
  }
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
// using std::stdin;
// using std::stdout;
// using std::freopen;

const int MXN = 100010;
const int MXL = 400060;
const int M = 26;

int trie[MXL][M], sum[MXL], len[MXL], equal[MXL], tot = 1;
bool end[MXL];
int n, q;
string str[MXN];

inline void ins(const string &s) {
  int p = 0;
  //++sum[p];
  for (string::size_type i = 0; i != s.size(); ++i) {
    if (trie[p][s[i] - 'a'] == 0)
      trie[p][s[i] - 'a'] = ++tot;
    p = trie[p][s[i] - 'a'];
    ++sum[p];
    len[p] = i + 1;
    equal[p] = p;
  }
  end[p] = true;
}

void clean(int p) {
  for (int i = 0; i != M; ++i)
    if (trie[p][i] != 0) {
      clean(trie[p][i]);
      if (sum[trie[p][i]] == sum[p])
        equal[p] = equal[trie[p][i]];
    }
}

inline int find(const string &s, const string &o) {
  int p = 0, res = 0;
  for (string::size_type i = 0; i < s.size();) {
    for (string::size_type j = 0; o[j] != s[i]; ++j)
      res += sum[trie[p][o[j] - 'a']];
    res += (int)end[p];
    p = equal[trie[p][s[i] - 'a']];
    i = len[p];
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i != n; ++i)
    cin >> str[i];
  for (int i = 0; i != n; ++i)
    ins(str[i]);
  clean(0);

  cin >> q;
  while (q--) {
    int k;
    string order;
    cin >> k >> order;
    cout << find(str[k - 1], order) + 1 << endl;
  }
}
Problem p03974 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int maxn = 1e5 + 7;

int n, q, a[maxn], k, f[maxn][26][26], prf[maxn];
string s[maxn], order;
struct Trie {
  int id, cnt;
  Trie *Child[26];
};

Trie *GetTrie() {
  Trie *cac = new Trie;
  cac->id = cac->cnt = 0;
  for (int i = 0; i < 26; i++)
    cac->Child[i] = NULL;
  return cac;
}

Trie *cac = GetTrie();

void Insert(int x) {
  Trie *ahihi = cac;
  for (auto i : s[x]) {
    if (ahihi->Child[i - 'a'] == NULL)
      ahihi->Child[i - 'a'] = GetTrie();
    ahihi = ahihi->Child[i - 'a'];
    ahihi->cnt++;
  }
  ahihi->id = x;
}

void Solve(int x) {
  Trie *ahihi = cac;
  for (auto i : s[x]) {
    if (ahihi->id != 0)
      prf[x]++;
    i -= 'a';
    for (int j = 0; j < 26; j++) {
      if (j != i && ahihi->Child[j] != NULL) {
        f[x][j][i] += ahihi->Child[j]->cnt;
      }
    }
    ahihi = ahihi->Child[i];
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("test.inp", "r"))
    freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    Insert(i);
  }
  for (int i = 1; i <= n; i++)
    Solve(i);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> k >> order;
    int ans = 0;
    for (int x = 1; x < 26; x++) {
      for (int y = x - 1; y >= 0; y--) {
        ans += f[k][order[y] - 'a'][order[x] - 'a'];
      }
    }
    cout << ans + prf[k] + 1 << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 7;

int n, q, a[maxn], k, f[maxn][26][26], prf[maxn];
string s[maxn], order;
struct Trie {
  int id, cnt;
  Trie *Child[26];
};

Trie *GetTrie() {
  Trie *cac = new Trie;
  cac->id = cac->cnt = 0;
  for (int i = 0; i < 26; i++)
    cac->Child[i] = NULL;
  return cac;
}

Trie *cac = GetTrie();

void Insert(int x) {
  Trie *ahihi = cac;
  for (auto i : s[x]) {
    if (ahihi->Child[i - 'a'] == NULL)
      ahihi->Child[i - 'a'] = GetTrie();
    ahihi = ahihi->Child[i - 'a'];
    ahihi->cnt++;
  }
  ahihi->id = x;
}

void Solve(int x) {
  Trie *ahihi = cac;
  for (auto i : s[x]) {
    if (ahihi->id != 0)
      prf[x]++;
    i -= 'a';
    for (int j = 0; j < 26; j++) {
      if (j != i && ahihi->Child[j] != NULL) {
        f[x][j][i] += ahihi->Child[j]->cnt;
      }
    }
    ahihi = ahihi->Child[i];
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("test.inp", "r"))
    freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    Insert(i);
  }
  for (int i = 1; i <= n; i++)
    Solve(i);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> k >> order;
    int ans = 0;
    for (int x = 1; x < 26; x++) {
      for (int y = x - 1; y >= 0; y--) {
        ans += f[k][order[y] - 'a'][order[x] - 'a'];
      }
    }
    cout << ans + prf[k] + 1 << '\n';
  }
}
Problem p03974 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll, ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
int n, c[100005][30][30], q, r[30], ans[100005];
string s[100005];
void dfs(vector<int> x, int l) {
  int o = 0;
  vector<int> p[26];
  for (int i = 0; i < x.size(); i++) {
    if (l < s[x[i]].size())
      p[s[x[i]][l] - 'a'].PB(x[i]);
    else
      o++;
  }
  for (int i = 0; i < x.size(); i++) {
    if (s[x[i]].size() <= l)
      continue;
    ans[x[i]] += o;
    for (int j = 0; j < 26; j++) {
      c[x[i]][s[x[i]][l] - 'a'][j] += int(p[j].size());
    }
  }
  for (int i = 0; i < 26; i++)
    if (p[i].size())
      dfs(p[i], l + 1);
}
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  scanf("%d", &q);
  vector<int> x;
  for (int i = 0; i < n; i++)
    x.PB(i);
  dfs(x, 0);
  while (q--) {
    int k;
    scanf(" %d", &k);
    k--;
    int res = ans[k];
    for (int i = 0; i < 26; i++) {
      char t;
      scanf(" %c", &t);
      r[t - 'a'] = i;
    }
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        if (r[i] > r[j])
          res += c[k][i][j];
      }
    }
    printf("%d\n", res + 1);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll, ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
int n, c[100005][30][30], q, r[30], ans[100005];
string s[100005];
void dfs(vector<int> x, int l) {
  int o = 0;
  vector<int> p[26];
  for (int i = 0; i < x.size(); i++) {
    if (l < s[x[i]].size())
      p[s[x[i]][l] - 'a'].PB(x[i]);
    else
      o++;
  }
  for (int i = 0; i < x.size(); i++) {
    if (s[x[i]].size() <= l)
      continue;
    ans[x[i]] += o;
    for (int j = 0; j < 26; j++) {
      c[x[i]][s[x[i]][l] - 'a'][j] += int(p[j].size());
    }
  }
  for (int i = 0; i < 26; i++)
    if (p[i].size())
      dfs(p[i], l + 1);
}
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  scanf("%d", &q);
  if (n == 1) {
    while (q--)
      printf("1\n");
    return 0;
  }
  vector<int> x;
  for (int i = 0; i < n; i++)
    x.PB(i);
  dfs(x, 0);
  while (q--) {
    int k;
    scanf(" %d", &k);
    k--;
    int res = ans[k];
    for (int i = 0; i < 26; i++) {
      char t;
      scanf(" %c", &t);
      r[t - 'a'] = i;
    }
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        if (r[i] > r[j])
          res += c[k][i][j];
      }
    }
    printf("%d\n", res + 1);
  }
}
Problem p03974 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

int N, Q;
string s[100010];
int cnt[100010][26][26];
int sub[100010];

struct Trie {
  bool f;
  int num[26];
  Trie *nxt[26];

  Trie() {
    f = 0;
    rep(i, 26) num[i] = 0;
    fill(nxt, nxt + 0x100, (Trie *)0);
  }
};

Trie *insert(string &t, Trie *r) {
  rep(i, t.size()) {
    int c = t[i] - 'a';
    if (!r->nxt[c])
      r->nxt[c] = new Trie;
    ++(r->num[c]);
    r = r->nxt[c];
  }

  r->f = true;

  return r;
}

void pre(int id, string &t, Trie *r) {
  rep(i, t.size()) {
    sub[id] += r->f;

    int c = t[i] - 'a';

    rep(j, 26) cnt[id][j][c] += r->num[j];
    r = r->nxt[c];
  }
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N;
  Trie *r = new Trie();

  rep(i, N) {
    cin >> s[i];
    Trie *v = r;
    insert(s[i], v);
  }

  rep(i, N) {
    Trie *v = r;
    pre(i, s[i], v);
  }

  cin >> Q;
  rep(i, Q) {
    int k;
    string pr;
    cin >> k >> pr;
    --k;
    int ret = sub[k];

    rep(p, 26) {
      for (int q = p + 1; q < 26; ++q) {
        ret += cnt[k][pr[p] - 'a'][pr[q] - 'a'];
      }
    }

    cout << ret + 1 << "\n";
  }

  return 0;
}

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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

int N, Q;
string s[100010];
int cnt[100010][26][26];
int sub[100010];

struct Trie {
  bool f;
  int num[26];
  Trie *nxt[26];

  Trie() {
    f = 0;
    rep(i, 26) num[i] = 0;
    fill(nxt, nxt + 26, (Trie *)0);
  }
};

Trie *insert(string &t, Trie *r) {
  rep(i, t.size()) {
    int c = t[i] - 'a';
    if (!r->nxt[c])
      r->nxt[c] = new Trie;
    ++(r->num[c]);
    r = r->nxt[c];
  }

  r->f = true;

  return r;
}

void pre(int id, string &t, Trie *r) {
  rep(i, t.size()) {
    sub[id] += r->f;

    int c = t[i] - 'a';

    rep(j, 26) cnt[id][j][c] += r->num[j];
    r = r->nxt[c];
  }
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N;
  Trie *r = new Trie();

  rep(i, N) {
    cin >> s[i];
    Trie *v = r;
    insert(s[i], v);
  }

  rep(i, N) {
    Trie *v = r;
    pre(i, s[i], v);
  }

  cin >> Q;
  rep(i, Q) {
    int k;
    string pr;
    cin >> k >> pr;
    --k;
    int ret = sub[k];

    rep(p, 26) {
      for (int q = p + 1; q < 26; ++q) {
        ret += cnt[k][pr[p] - 'a'][pr[q] - 'a'];
      }
    }

    cout << ret + 1 << "\n";
  }

  return 0;
}
Problem p03974 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <map>
using namespace std;
const int NQmax = 100000;
int N, Q;
string S[NQmax];
int prefix[NQmax];
map<int, int> former[NQmax];

struct trie {
  int k; // index of this node
  int n; // number of children
  struct trie *c[26];
} root[400000];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  int n = 1;
  for (int i = 0; i < N; i++) {
    struct trie *t = root;
    for (int j = 0; j < S[i].size(); j++) {
      int c = S[i][j] - 'a';
      if (t->c[c] == NULL)
        t->c[c] = root + n;
      n++;
      t->c[c]->n++;
      t = t->c[c];
    }
    t->k = i + 1;
  }

  for (int i = 0; i < N; i++) {
    struct trie *t = root;
    for (int j = 0; j < S[i].size(); j++) {
      int c = S[i][j] - 'a';
      struct trie *s = t->c[c];
      if (t->k > 0)
        prefix[i]++;
      for (int k = 0; k < 26; k++) {
        if (k == c || t->c[k] == NULL)
          continue;
        former[i][k * 26 + c] += t->c[k]->n;
      }
      t = s;
    }
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int K;
    string P;
    cin >> K;
    cin >> P;
    int res = prefix[K - 1] + 1;
    for (char j = 0; j < 26; j++) {
      for (int k = j + 1; k < 26; k++) {
        res += former[K - 1][(P[j] - 'a') * 26 + P[k] - 'a'];
      }
    }
    cout << res << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
using namespace std;
const int NQmax = 100000;
int N, Q;
string S[NQmax];
int prefix[NQmax];
map<int, int> former[NQmax];

struct trie {
  int k; // index of this node
  int n; // number of children
  struct trie *c[26];
} root[400000];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  int n = 1;
  for (int i = 0; i < N; i++) {
    struct trie *t = root;
    for (int j = 0; j < S[i].size(); j++) {
      int c = S[i][j] - 'a';
      if (t->c[c] == NULL)
        t->c[c] = root + n;
      n++;
      t->c[c]->n++;
      t = t->c[c];
    }
    t->k = i + 1;
  }

  for (int i = 0; i < N; i++) {
    struct trie *t = root;
    for (int j = 0; j < S[i].size(); j++) {
      int c = S[i][j] - 'a';
      struct trie *s = t->c[c];
      if (t->k > 0)
        prefix[i]++;
      for (int k = 0; k < 26; k++) {
        if (k == c || t->c[k] == NULL)
          continue;
        former[i][k * 26 + c] += t->c[k]->n;
      }
      t = s;
    }
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int K;
    string P;
    cin >> K;
    cin >> P;
    int res = prefix[K - 1] + 1;
    for (char j = 0; j < 26; j++) {
      for (int k = j + 1; k < 26; k++) {
        int l = (P[j] - 'a') * 26 + P[k] - 'a';
        auto m = former[K - 1].find(l);
        if (m != former[K - 1].end())
          res += m->second;
      }
    }
    cout << res << endl;
  }

  return 0;
}
Problem p03974 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;

struct Trie {
  int num;
  bool flag;
  Trie *next[26];
  Trie() {
    num = 0;
    flag = false;
    fill(next, next + 26, (Trie *)0);
  }
};

void add(string s, Trie *t) {
  for (int i = 0; i < s.length(); i++) {
    int c = s[i] - 'a';
    if (!t->next[c])
      t->next[c] = new Trie;
    t = t->next[c];
    t->num++;
  }
  t->flag = true;
}

int cnt[26][26][100000], ad[100000];

void calc(string s, int ind, int c, Trie *t) {
  for (int i = 0; i < s.length(); i++) {
    int d = s[i] - 'a';
    if (t->next[c])
      cnt[c][d][ind] += t->next[c]->num;
    if (!c && t->flag)
      ad[ind]++;
    t = t->next[d];
  }
}

signed main() {
  ios::sync_with_stdio(false);
  int n, q;
  string s[100000];
  Trie *trie = new Trie;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    add(s[i], trie);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++)
      calc(s[i], i, j, trie);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, ans = 1;
    string pr;
    cin >> a >> pr;
    a--;
    for (int j = 0; j < 26; j++) {
      for (int k = j + 1; k < 26; k++)
        ans += cnt[pr[j] - 'a'][pr[k] - 'a'][a];
    }
    cout << ans + ad[a] << endl;
  }
  return 0;
}

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

struct Trie {
  int num;
  bool flag;
  Trie *next[26];
  Trie() {
    num = 0;
    flag = false;
    fill(next, next + 26, (Trie *)0);
  }
};

void add(string s, Trie *t) {
  for (int i = 0; i < s.length(); i++) {
    int c = s[i] - 'a';
    if (!t->next[c])
      t->next[c] = new Trie;
    t = t->next[c];
    t->num++;
  }
  t->flag = true;
}

int cnt[26][26][100000], ad[100000];

void calc(string s, int ind, int c, Trie *t) {
  for (int i = 0; i < s.length(); i++) {
    int d = s[i] - 'a';
    if (t->next[c])
      cnt[c][d][ind] += t->next[c]->num;
    if (!c && t->flag)
      ad[ind]++;
    t = t->next[d];
  }
}

signed main() {
  ios::sync_with_stdio(false);
  int n, q;
  string s[100000];
  Trie *trie = new Trie;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    add(s[i], trie);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++)
      calc(s[i], i, j, trie);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, ans = 1;
    string pr;
    cin >> a >> pr;
    a--;
    for (int j = 0; j < 26; j++) {
      for (int k = j + 1; k < 26; k++)
        ans += cnt[pr[j] - 'a'][pr[k] - 'a'][a];
    }
    cout << ans + ad[a] << endl;
  }
  return 0;
}
Problem p03975 (Python)

Buggy submission (status: Time Limit Exceeded):
n, a, b = map(int, input().split())

s_t = set()
for i in range(n):
    s_t.add(int(input()))

print(len(s_t.difference(set(range(a, b)))))


Fixed submission:
n, a, b = map(int, input().split())

s_t = set()
for i in range(n):
    s_t.add(int(input()))
answer = 0
for s in s_t:
    if a <= s < b:
        pass
    else:
        answer += 1
print(answer)
Problem p03976 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int cnt[26];
int N, K;
string P[1004];

int next(int &S) {
  int ret = -1;
  int maxi = 0;
  rep(i, 26) {
    if (S >> i & 1)
      continue;
    if (cnt[i] && maxi < cnt[i]) {
      maxi = cnt[i];
      ret = i;
    }
  }
  if (ret != -1)
    S |= 1 << ret;
  return ret;
}

int main() {
  cin >> N >> K;
  rep(i, N) cin >> P[i];
  rep(i, N) cnt[P[i][0] - 'A']++;
  int ans = 0;

  bool isfinish = true;
  while (isfinish) {
    int S = 0;
    rep(i, K) {
      int c = next(S);
      if (c == -1)
        isfinish = false;
      cnt[c]--;
    }
    ans++;
  }
  cout << ans - 1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int cnt[26];
int N, K;
string P[10004];

int next(int &S) {
  int ret = -1;
  int maxi = 0;
  rep(i, 26) {
    if (S >> i & 1)
      continue;
    if (cnt[i] && maxi < cnt[i]) {
      maxi = cnt[i];
      ret = i;
    }
  }
  if (ret != -1)
    S |= 1 << ret;
  return ret;
}

int main() {
  cin >> N >> K;
  rep(i, N) cin >> P[i];
  rep(i, N) cnt[P[i][0] - 'A']++;
  int ans = 0;

  bool isfinish = true;
  while (isfinish) {
    int S = 0;
    rep(i, K) {
      int c = next(S);
      if (c == -1)
        isfinish = false;
      cnt[c]--;
    }
    ans++;
  }
  cout << ans - 1 << endl;

  return 0;
}
Problem p03976 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
using namespace std;
int main() {
  int n, k, a[26] = {}, ans = 0, b;
  cin >> n >> k;
  b = n / k;
  string s[1000] = {};
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    a[s[i][0] - 'A']++;
  }
  sort(a, a + 26, greater<int>());
  while (a[k - 1] > 0 && b) {
    ans++, b--;
    for (int i = 0; i < k; i++) {
      a[i]--;
    }
    sort(a, a + 26, greater<int>());
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
using namespace std;
int main() {
  int n, k, a[26] = {}, ans = 0, b;
  cin >> n >> k;
  b = n / k;
  string s[10000] = {};
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    a[s[i][0] - 'A']++;
  }
  sort(a, a + 26, greater<int>());
  while (a[k - 1] > 0 && b) {
    ans++, b--;
    for (int i = 0; i < k; i++) {
      a[i]--;
    }
    sort(a, a + 26, greater<int>());
  }
  cout << ans << endl;
}
Problem p03981 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int to[maxn][26], lnk[maxn], len[maxn], last, sz;
void init() {
  memset(to, -1, sizeof(to));
  lnk[0] = -1;
  last = 0;
  sz = 1;
}
void extend(int c) {
  int cur = sz++;
  len[cur] = len[last] + 1;
  int p = last;
  while (p != -1 && to[p][c] == -1) {
    to[p][c] = cur;
    p = lnk[p];
  }
  if (p == -1)
    lnk[cur] = 0;
  else {
    int q = to[p][c];
    if (len[p] + 1 == len[q])
      lnk[cur] = q;
    else {
      int nq = sz++;
      memcpy(to[nq], to[q], sizeof(to[nq]));
      lnk[nq] = lnk[q];
      len[nq] = len[p] + 1;
      lnk[q] = nq;
      lnk[cur] = nq;
      while (p != -1 && to[p][c] == q) {
        to[p][c] = nq;
        p = lnk[p];
      }
    }
  }
  last = cur;
}
int main() {
  string s;
  cin >> s;
  int siz = s.size();
  init();
  for (int i = 0; i < siz; i++)
    extend(s[i] - 'a');
  vector<int> cnt(siz + 10, 0);
  for (int i = 1; i < sz; i++) {
    cnt[len[lnk[i]] + 1]++;
    cnt[len[i] + 1]--;
  }
  int ans = 0;
  for (int i = 1; i <= siz; i++) {
    cnt[i] += cnt[i - 1];
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
int to[maxn][26], lnk[maxn], len[maxn], last, sz;
void init() {
  memset(to, -1, sizeof(to));
  lnk[0] = -1;
  last = 0;
  sz = 1;
}
void extend(int c) {
  int cur = sz++;
  len[cur] = len[last] + 1;
  int p = last;
  while (p != -1 && to[p][c] == -1) {
    to[p][c] = cur;
    p = lnk[p];
  }
  if (p == -1)
    lnk[cur] = 0;
  else {
    int q = to[p][c];
    if (len[p] + 1 == len[q])
      lnk[cur] = q;
    else {
      int nq = sz++;
      memcpy(to[nq], to[q], sizeof(to[nq]));
      lnk[nq] = lnk[q];
      len[nq] = len[p] + 1;
      lnk[q] = nq;
      lnk[cur] = nq;
      while (p != -1 && to[p][c] == q) {
        to[p][c] = nq;
        p = lnk[p];
      }
    }
  }
  last = cur;
}
int main() {
  string s;
  cin >> s;
  int siz = s.size();
  init();
  for (int i = 0; i < siz; i++)
    extend(s[i] - 'a');
  vector<int> cnt(siz + 10, 0);
  for (int i = 1; i < sz; i++) {
    cnt[len[lnk[i]] + 1]++;
    cnt[len[i] + 1]--;
  }
  int ans = 0;
  for (int i = 1; i <= siz; i++) {
    cnt[i] += cnt[i - 1];
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define mod 1000000007
#define ll long long int
#define pb(x) push_back(x)
#define MP(x, y) make_pair(x, y)

using namespace std;

ll power(ll a, ll b, ll m) {
  ll sol = power(a, b / 2, m);
  sol = (sol * sol) % m;
  if (b % 2 == 1)
    sol = (sol * a) % m;
  return (sol);
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else {
    return gcd(b, a % b);
  }
}
string s;
stack<char> sol;
void solve() {
  cin >> s;
  sol.push(s[0]);
  int n = s.length();
  for (int i = 1; i < n; i++) {
    if (s[i] == 'T' && (!s.empty())) {
      if (sol.top() == 'S')
        sol.pop();
      else
        sol.push('T');
    } else {
      sol.push(s[i]);
    }
  }
  cout << sol.size();
}
int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define mod 1000000007
#define ll long long int
#define pb(x) push_back(x)
#define MP(x, y) make_pair(x, y)

using namespace std;

ll power(ll a, ll b, ll m) {
  ll sol = power(a, b / 2, m);
  sol = (sol * sol) % m;
  if (b % 2 == 1)
    sol = (sol * a) % m;
  return (sol);
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else {
    return gcd(b, a % b);
  }
}
string s;
stack<char> sol;
void solve() {
  cin >> s;
  sol.push(s[0]);
  int n = s.length();
  for (int i = 1; i < n; i++) {
    if (s[i] == 'T' && (!sol.empty())) {
      if (sol.top() == 'S')
        sol.pop();
      else
        sol.push('T');
    } else {
      sol.push(s[i]);
    }
  }
  cout << sol.size();
}
int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
const ll INF = 1000000000ll;
const ll MOD = 1000000007ll;
const double EPS = 1e-8;

int main(void) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);

  long long ans = 0;
  string x;
  cin >> x;
  stack<char> st;
  ans = x.size();

  st.push(x[0]);
  for (int i = 1; i < x.size(); i++) {
    if (x[i] == 'T' && st.top() == 'S') {
      st.pop();
      ans -= 2;
    } else {
      st.push(x[i]);
    }
  }

  if (st.empty()) {
    cout << '0' << endl;
  } else {
    cout << st.size() << endl;
  }

  return 0;
}


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

#define pb push_back

typedef long long ll;
const ll INF = 1000000000ll;
const ll MOD = 1000000007ll;
const double EPS = 1e-8;

int main(void) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);

  long long ans = 0;
  string x;
  cin >> x;
  stack<char> st;
  ans = x.size();

  st.push(x[0]);
  for (int i = 1; i < x.size(); i++) {
    if (x[i] == 'T' && (!st.empty() && st.top() == 'S')) {
      st.pop();
      ans -= 2;
    } else {
      st.push(x[i]);
    }
  }

  if (st.empty()) {
    cout << '0' << endl;
  } else {
    cout << st.size() << endl;
  }

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  string X;
  cin >> X;
  stack<char> s;
  s.push(X[0]);
  for (int i = 1; i < X.size(); i++) {
    if (X[i] == 'S')
      s.push(X[i]);
    else {
      if (s.top() == 'S') {
        s.pop();
      } else {
        s.push(X[i]);
      }
    }
  }
  cout << s.size() << endl;
}

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

int main() {
  string X;
  cin >> X;
  stack<char> s;
  s.push(X[0]);
  for (int i = 1; i < X.size(); i++) {
    if (X[i] == 'S')
      s.push(X[i]);
    else {
      if (s.size() > 0 && s.top() == 'S') {
        s.pop();
      } else {
        s.push(X[i]);
      }
    }
  }
  cout << s.size() << endl;
}