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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  ll ans[2010] = {0};
  int x, y, z, n, fir, i, j, k;
  ll a[1010], b[1010], c[1010];

  scanf("%d%d%d%d", &x, &y, &z, &n);
  for (i = 0; i < x; i++)
    scanf("%lld", &a[i]);
  for (i = 0; i < y; i++)
    scanf("%lld", &b[i]);
  for (i = 0; i < z; i++)
    scanf("%lld", &c[i]);
  sort(a, a + x);
  sort(b, b + y);
  sort(c, c + z);
  fir = 0;
  for (i = x - 1; i >= 0; i--) {
    if (x - i - 1 >= n)
      break;
    for (j = y - 1; j >= 0; j--) {
      if (y - j - 1 >= n)
        break;

      for (k = z - 1; k >= 0; k--) {
        if (z - k - 1 >= n)
          break;
        ans[fir] = a[i] + b[j] + c[k];
        if (ans[fir] <= ans[n])
          break;
        else
          fir++;
        if (fir == n) {
          sort(ans, ans + 2 * n);
          fir = 0;
        }
      }
    }
  }
  sort(ans, ans + 2 * n);
  for (i = 2 * n - 1; i >= n; i--)
    printf("%lld\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  ll ans[6010] = {0};
  int x, y, z, n, fir, i, j, k;
  ll a[1010], b[1010], c[1010];

  scanf("%d%d%d%d", &x, &y, &z, &n);
  for (i = 0; i < x; i++)
    scanf("%lld", &a[i]);
  for (i = 0; i < y; i++)
    scanf("%lld", &b[i]);
  for (i = 0; i < z; i++)
    scanf("%lld", &c[i]);
  sort(a, a + x);
  sort(b, b + y);
  sort(c, c + z);
  fir = 0;
  for (i = x - 1; i >= 0; i--) {
    if (x - i - 1 >= n)
      break;
    for (j = y - 1; j >= 0; j--) {
      if (y - j - 1 >= n)
        break;

      for (k = z - 1; k >= 0; k--) {
        if (z - k - 1 >= n)
          break;
        ans[fir] = a[i] + b[j] + c[k];
        if (ans[fir] <= ans[n])
          break;
        else
          fir++;
        if (fir == n) {
          sort(ans, ans + 2 * n);
          fir = 0;
        }
      }
    }
  }
  sort(ans, ans + 2 * n);
  for (i = 2 * n - 1; i >= n; i--)
    printf("%lld\n", ans[i]);
  return 0;
}
Problem p03078 (C++)

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

#define int long long

const int N = 3e6 + 9;

long long ar[N], br[N], cr[N], dr[N], er[N], x, y, z, k, pos = 0, val = 0;

using namespace std;

signed main()

{
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    cin >> ar[i];
  for (int i = 0; i < y; i++)
    cin >> br[i];
  for (int i = 0; i < z; i++)
    cin >> cr[i];
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      dr[pos++] = ar[i] + br[j];
    }
  }
  sort(dr, dr + pos);
  reverse(dr, dr + pos);
  sort(cr, cr + z);
  reverse(cr, cr + z);
  for (int i = 0; i < pos; i++) {
    for (int j = 0; j < z; j++) {
      er[val++] = dr[i] + cr[j];
    }
  }
  sort(er, er + val);
  reverse(er, er + val);
  for (int i = 0; i < k; i++)
    cout << er[i] << "\n";
}

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

#define int long long

const int N = 3e6 + 9;

long long ar[N], br[N], cr[N], dr[N], er[N], x, y, z, k, pos = 0, val = 0;

using namespace std;

signed main()

{
  cin >> x >> y >> z >> k;
  for (int i = 0; i < x; i++)
    cin >> ar[i];
  for (int i = 0; i < y; i++)
    cin >> br[i];
  for (int i = 0; i < z; i++)
    cin >> cr[i];
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      dr[pos++] = ar[i] + br[j];
    }
  }
  sort(dr, dr + pos);
  reverse(dr, dr + pos);
  sort(cr, cr + z);
  reverse(cr, cr + z);
  for (int i = 0; i < z; i++) {
    for (int j = 0; j < k; j++) {
      er[val++] = dr[j] + cr[i];
    }
  }
  sort(er, er + val);
  reverse(er, er + val);
  for (int i = 0; i < k; i++)
    cout << er[i] << "\n";
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):

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

using ll = long long;

vector<ll> AB;
vector<ll> ABC;

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

  ll X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  ll A[X], B[Y], C[Z];
  for (int i = 0; i < X; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < Y; ++i) {
    cin >> B[i];
  }
  for (int i = 0; i < Z; ++i) {
    cin >> C[i];
  }

  for (int i = 0; i < X; ++i) {
    for (int j = 0; j < Y; ++j) {
      AB.push_back(A[i] + B[j]);
    }
  }
  sort(AB.rbegin(), AB.rend());
  for (int i = 0; i < X * Y; ++i) {
    for (int j = 0; j < Z; ++j) {
      ABC.push_back(AB[i] + C[j]);
    }
  }

  sort(ABC.rbegin(), ABC.rend());

  for (int i = 0; i < K; ++i) {
    cout << ABC[i] << '\n';
  }

  return 0;
}


Fixed submission:

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

using ll = long long;

vector<ll> AB;
vector<ll> ABC;

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

  ll X, Y, Z, K;
  cin >> X >> Y >> Z >> K;

  ll A[X], B[Y], C[Z];
  for (int i = 0; i < X; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < Y; ++i) {
    cin >> B[i];
  }
  for (int i = 0; i < Z; ++i) {
    cin >> C[i];
  }

  for (int i = 0; i < X; ++i) {
    for (int j = 0; j < Y; ++j) {
      AB.push_back(A[i] + B[j]);
    }
  }
  sort(AB.rbegin(), AB.rend());
  int ab_length = min(K, X * Y);
  for (int i = 0; i < ab_length; ++i) {
    for (int j = 0; j < Z; ++j) {
      ABC.push_back(AB[i] + C[j]);
    }
  }

  sort(ABC.rbegin(), ABC.rend());

  for (int i = 0; i < K; ++i) {
    cout << ABC[i] << '\n';
  }

  return 0;
}
Problem p03078 (C++)

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

const long long INF = 1000000000;

typedef long long ll;

#define writeln(n) cout << n << "\n"
#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef pair<int, int> P;
typedef pair<string, int> Psi;

vector<ll> A(1010), B(1010), C(1010);

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

  int x, y, z, k;
  cin >> x >> y >> z >> k;

  rep(i, x) { cin >> A[i]; }
  rep(i, y) { cin >> B[i]; }
  rep(i, z) { cin >> C[i]; }

  sort(A.begin(), A.end(), greater<ll>());
  sort(B.begin(), B.end(), greater<ll>());
  sort(C.begin(), C.end(), greater<ll>());

  vector<ll> ans;

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      for (int l = 0; l < z; l++) {
        if ((i + 1) + (j + 1) + (l + 1) > k)
          break;
        ans.push_back(A[i] + B[j] + C[l]);
      }
    }
  }
  sort(ans.begin(), ans.end(), greater<ll>());

  rep(i, k) { writeln(ans[i]); }
}


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

const long long INF = 1000000000;

typedef long long ll;

#define writeln(n) cout << n << "\n"
#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef pair<int, int> P;
typedef pair<string, int> Psi;

vector<ll> A(1010), B(1010), C(1010);

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

  int x, y, z, k;
  cin >> x >> y >> z >> k;

  rep(i, x) { cin >> A[i]; }
  rep(i, y) { cin >> B[i]; }
  rep(i, z) { cin >> C[i]; }

  sort(A.begin(), A.end(), greater<ll>());
  sort(B.begin(), B.end(), greater<ll>());
  sort(C.begin(), C.end(), greater<ll>());

  vector<ll> ans;

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      for (int l = 0; l < z; l++) {
        if ((i + 1) * (j + 1) * (l + 1) > k)
          break;
        ans.push_back(A[i] + B[j] + C[l]);
      }
    }
  }
  sort(ans.begin(), ans.end(), greater<ll>());

  rep(i, k) { writeln(ans[i]); }
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
/*
 *  Author: windystreet
 *  Date  : 2019-04-13 11:07:49
 *  Motto : Think twice, code once.
 */
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define eps 1e-5
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x) & (-x)
#define bug printf("!!!!!\n");
#define mem(x, y) memset(x, y, sizeof(x))

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

const int maxn = 1e5 + 7;
const int INF = 1 << 30;
const int mod = 1e9 + 7;

long long A[maxn];
long long B[maxn];
long long C[maxn];
bool cmp(long long a, long long b) { return a > b; }
vector<long long> ans;
void solve() {
  long long x, y, z, k;
  cin >> x >> y >> z >> k;
  for (int i = 1; i <= x; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= y; i++)
    cin >> B[i];
  for (int i = 1; i <= z; i++)
    cin >> C[i];
  sort(A + 1, A + 1 + x, cmp);
  sort(B + 1, B + 1 + y, cmp);
  sort(C + 1, C + 1 + z, cmp);

  for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= y; j++) {
      for (int c = 1; c <= z; c++) {
        ans.pb(A[i] + B[j] + C[c]);
      }
    }
  }
  sort(ans.begin(), ans.end(), cmp);
  for (int i = 0; i < k; i++) {
    printf("%lld\n", ans[i]);
  }
  return;
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("F:/in.txt","r",stdin);
#endif
  //    freopen("out.txt","w",stdout);
  //    ios::sync_with_stdio(false);
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    //    printf("Case %d: ",cas++);
    solve();
  }
  return 0;
}

Fixed submission:
/*
 *  Author: windystreet
 *  Date  : 2019-04-13 11:07:49
 *  Motto : Think twice, code once.
 */
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define eps 1e-5
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x) & (-x)
#define bug printf("!!!!!\n");
#define mem(x, y) memset(x, y, sizeof(x))

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

const int maxn = 1e5 + 7;
const int INF = 1 << 30;
const int mod = 1e9 + 7;

long long A[maxn];
long long B[maxn];
long long C[maxn];
bool cmp(long long a, long long b) { return a > b; }
vector<long long> ans;
void solve() {
  long long x, y, z, k;
  cin >> x >> y >> z >> k;
  for (int i = 1; i <= x; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= y; i++)
    cin >> B[i];
  for (int i = 1; i <= z; i++)
    cin >> C[i];
  sort(A + 1, A + 1 + x, cmp);
  sort(B + 1, B + 1 + y, cmp);
  sort(C + 1, C + 1 + z, cmp);

  for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= y; j++) {
      for (int c = 1; c <= z; c++) {
        if (i * j * c > k)
          break;
        ans.pb(A[i] + B[j] + C[c]);
      }
    }
  }
  sort(ans.begin(), ans.end(), cmp);
  for (int i = 0; i < k; i++) {
    printf("%lld\n", ans[i]);
  }
  return;
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("F:/in.txt","r",stdin);
#endif
  //    freopen("out.txt","w",stdout);
  //    ios::sync_with_stdio(false);
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    //    printf("Case %d: ",cas++);
    solve();
  }
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define MOD 1000000007
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;

signed main() {
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  int a[114514], b[114514], c[114514];
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < y; i++)
    cin >> b[i];
  for (int i = 0; i < z; i++)
    cin >> c[i];
  vector<int> ab;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      ab.push_back(a[i] + b[j]);
    }
  }
  sort(ab.begin(), ab.end(), greater<int>());
  sort(c, c + z, greater<int>());
  vector<P> com;
  int cnt = 1;
  while (1) {
    int m = com.size();
    for (int i = m - 1; i >= m - cnt + 1; i--) {
      com.push_back(make_pair(com[i].first, com[i].second + 1));
    }
    com.push_back(make_pair(cnt, 1));
    cnt++;
    if (cnt == k + 1)
      break;
  }
  vector<int> ans;
  for (int i = 0; i < com.size(); i++) {
    if (com[i].first - 1 < ab.size() && com[i].second - 1 < z) {
      ans.push_back(ab[com[i].first - 1] + c[com[i].second - 1]);
    }
  }
  sort(ans.rbegin(), ans.rend());
  for (int i = 0; i < k; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define MOD 1000000007
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;

signed main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  int a[114514], b[114514], c[114514];
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < y; i++)
    cin >> b[i];
  for (int i = 0; i < z; i++)
    cin >> c[i];
  vector<int> ab;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      ab.push_back(a[i] + b[j]);
    }
  }
  sort(ab.begin(), ab.end(), greater<int>());
  sort(c, c + z, greater<int>());
  vector<P> com;
  int cnt = 1;
  while (1) {
    int m = com.size();
    for (int i = m - 1; i >= m - cnt + 1; i--) {
      com.push_back(make_pair(com[i].first, com[i].second + 1));
    }
    com.push_back(make_pair(cnt, 1));
    cnt++;
    if (cnt == k + 1)
      break;
  }
  vector<int> ans;
  for (int i = 0; i < com.size(); i++) {
    if (com[i].first - 1 < ab.size() && com[i].second - 1 < z) {
      ans.push_back(ab[com[i].first - 1] + c[com[i].second - 1]);
    }
  }
  sort(ans.rbegin(), ans.rend());
  for (int i = 0; i < k; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<long long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define MOD 1000000007

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vll A(1000, 0), B(1000, 0), C(1000, 0);
  rep(i, X) cin >> A[i];
  rep(j, Y) cin >> B[j];
  rep(k, Z) cin >> C[k];
  vll D(1e6 + 1, 0);
  rep(i, X) {
    rep(j, Y) { D[i * Y + j] = A[i] + B[j]; }
  }
  sort(all(D), greater<ll>());
  vll E(3e6 + 1, 0);
  rep(i, min(X * Y, K)) {
    rep(j, min(Z, K)) { E[i * K + j] = D[i] + C[j]; }
  }
  sort(all(E), greater<ll>());
  rep(i, K) cout << E[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<long long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define MOD 1000000007

int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vll A(1000, 0), B(1000, 0), C(1000, 0);
  rep(i, X) cin >> A[i];
  rep(j, Y) cin >> B[j];
  rep(k, Z) cin >> C[k];
  vll D(1e6 + 1, 0);
  rep(i, X) {
    rep(j, Y) { D[i * Y + j] = A[i] + B[j]; }
  }
  sort(all(D), greater<ll>());
  vll E(9e6 + 1, 0);
  int a = min(X * Y, K), b = min(Z, K);
  rep(i, a) {
    rep(j, b) { E[i * b + j] = D[i] + C[j]; }
  }
  sort(all(E), greater<ll>());
  rep(i, K) cout << E[i] << endl;
}
Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

int main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> a(x), b(y), c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];

  vector<ll> result;
  rep(i, x) rep(j, y) rep(k, z) result.push_back(a[i] + b[j] + c[k]);
  sort(result.rbegin(), result.rend());
  rep(i, k) cout << result[i] << endl;

  return 0;
}


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

using namespace std;
using ll = long long;

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

int main() {
  int x, y, z, k;
  cin >> x >> y >> z >> k;
  vector<ll> a(x), b(y), c(z);
  rep(i, x) cin >> a[i];
  rep(i, y) cin >> b[i];
  rep(i, z) cin >> c[i];

  vector<ll> e;
  rep(i, x) rep(j, y) e.push_back(a[i] + b[j]);
  sort(e.rbegin(), e.rend());

  vector<ll> f;
  rep(i, min(x * y, k)) rep(j, z) f.push_back(e[i] + c[j]);
  sort(f.rbegin(), f.rend());

  rep(i, k) cout << f[i] << endl;

  return 0;
}
Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
#define chmin(i, j) i = min(i, j);
#define chmax(i, j) i = max(i, j);
#define rep(i, n) for (int i = 0; i < n; i++)
ll X, Y, Z, K;
vector<ll> a, b, c;
vector<ll> ans;
bool solve(ll T, bool sp) {
  int cnt = 0;
  rep(i, X) {
    rep(j, Y) {
      rep(k, Z) {
        if (a[i] + b[j] + c[k] < T)
          break;
        if (sp)
          ans.emplace_back(a[i] + b[j] + c[k]);
        cnt++;
        if (!sp && cnt >= K)
          return true;
      }
    }
  }
  return false;
}
int main() {
  cin >> X >> Y >> Z >> K;
  a.resize(X);
  b.resize(Y);
  c.resize(Z);
  rep(i, X) cin >> a[i];
  rep(i, Y) cin >> b[i];
  rep(i, Z) cin >> c[i];
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  sort(c.rbegin(), c.rend());
  ll front = 0;
  ll back = 30000000000;
  while (front != back) {
    ll half = (back + front) / 2;
    if (back = front + 1) {
      back = front;
    } else if (solve(half, false))
      front = half;
    else
      back = half - 1;
  }
  solve(front, true);
  sort(ans.rbegin(), ans.rend());
  rep(i, K) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
#define chmin(i, j) i = min(i, j);
#define chmax(i, j) i = max(i, j);
#define rep(i, n) for (int i = 0; i < n; i++)
ll X, Y, Z, K;
vector<ll> a, b, c;
vector<ll> ans;
bool solve(ll T, bool sp) {
  int cnt = 0;
  rep(i, X) {
    rep(j, Y) {
      rep(k, Z) {
        if (a[i] + b[j] + c[k] < T)
          break;
        if (sp)
          ans.emplace_back(a[i] + b[j] + c[k]);
        cnt++;
        if (!sp && cnt >= K)
          return true;
      }
    }
  }
  return false;
}
int main() {
  cin >> X >> Y >> Z >> K;
  a.resize(X);
  b.resize(Y);
  c.resize(Z);
  rep(i, X) cin >> a[i];
  rep(i, Y) cin >> b[i];
  rep(i, Z) cin >> c[i];
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  sort(c.rbegin(), c.rend());
  ll front = 0;
  ll back = 30000000000;
  while (front != back) {
    ll half = (back + front) / 2;
    if (back == front + 1) {
      back = front;
    } else if (solve(half, false))
      front = half;
    else
      back = half - 1;
  }
  solve(front, true);
  sort(ans.rbegin(), ans.rend());
  rep(i, K) cout << ans[i] << endl;
  return 0;
}
Problem p03079 (Python)

Buggy submission (status: Runtime Error):
A = int(input())
B = int(input())
C = int(input())
if A == B and B == C:
    print("Yes")
else:
    print("No")


Fixed submission:
A, B, C = map(int, input().split())
if A == B and B == C:
    print("Yes")
else:
    print("No")
Problem p03079 (C++)

Buggy submission (status: Runtime Error):
/*author: hyperion_1724
  date:
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  // freopen ("OUTPUT.txt" , "w" , stdout);
#endif

  int A, B, C;
  cin >> A >> B >> C;
  if (A == B && B == C && A == C) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Fixed submission:
/*author: hyperion_1724
  date:
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int A, B, C;
  cin >> A >> B >> C;
  if (A == B && B == C && A == C) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p03079 (C++)

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  int a, b, c;
  cin >> a >> b >> c;
  if (a != b || a != c) {
    cout << "No";
  } else {
    cout << "Yes";
  }
}

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  int a, b, c;
  cin >> a >> b >> c;
  if (a != b || a != c) {
    cout << "No";
  } else {
    cout << "Yes";
  }
}
Problem p03079 (Python)

Buggy submission (status: Runtime Error):
abc = list(map(int, input.split()))
if len(set(abc)) == 1:
    print("Yes")
else:
    print("No")


Fixed submission:
abc = list(map(int, input().split()))
if len(set(abc)) == 1:
    print("Yes")
else:
    print("No")
Problem p03079 (Python)

Buggy submission (status: Runtime Error):
a, b, c = input()
if a == b == c:
    print("Yes")
else:
    print("No")


Fixed submission:
a, b, c = input().split()
if a == b == c:
    print("Yes")
else:
    print("No")
Problem p03080 (C++)

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

int main() {
  int n, r = 0, b = 0;
  cin >> n;
  char s[4];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'B')
      b += 1;
    else
      r += 1;
  }
  if (r > b)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

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

int main() {
  int n, r = 0, b = 0;
  cin >> n;
  char s[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'B')
      b += 1;
    else
      r += 1;
  }
  if (r > b)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03080 (C++)

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

int main() {
  int N, count = 0;
  cin >> N;
  string S;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    if (S[i] == 'R')
      count++;
    if (S[i] == 'B')
      count--;
  }

  if (count <= 0)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}

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

int main() {
  int N, count = 0;
  cin >> N;
  string S;
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S[i] == 'R')
      count++;
    if (S[i] == 'B')
      count--;
  }

  if (count <= 0)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}
Problem p03080 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int main() {

  int people, count = 0;
  string hat;

  cin >> people;
  cin >> hat;

  for (int i = 0; i < people; i++) {

    if (hat[i] == 'R') {

      count++;
    }
  }

  hat = (count > people / 2) ? "Yes" : "No";
  cout << hat;

  while (true) {
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int main() {

  int people, count = 0;
  string hat;

  cin >> people;
  cin >> hat;

  for (int i = 0; i < people; i++) {

    if (hat[i] == 'R') {

      count++;
    }
  }

  hat = (count > people / 2) ? "Yes" : "No";
  cout << hat;

  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, q;
char a[100005], b[100005];
string s;
int sol(int x, int u) {
  x--;
  for (int i = 1; i <= q; i++)
    if (a[i] == s[x]) {
      if (b[i] == 'L') {
        x--;
        if (x < 0 && !u)
          return 1;
      } else {
        x++;
        if (x == n && u)
          return 1;
      }
    }
  return 0;
}
int main() {
  cin >> n >> q;
  cin >> s;
  for (int j = 1; j <= q; j++)
    cin >> a[j] >> b[j];
  int rez = n + 1, rez1 = 0;
  for (int step = 1 << 17; step; step >>= 1)
    if (rez - step >= 0 && sol(rez - step, 1))
      rez -= step;
  for (int step = 1 << 17; step; step >>= 1)
    if (rez1 + step <= n && sol(rez1 + step, 0))
      rez1 += step;
  if (rez > rez1)
    cout << rez - rez1 - 1;
  else
    cout << 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, q;
char a[200005], b[200005];
string s;
int sol(int x, int u) {
  x--;
  for (int i = 1; i <= q; i++)
    if (a[i] == s[x]) {
      if (b[i] == 'L') {
        x--;
        if (x < 0 && !u)
          return 1;
      } else {
        x++;
        if (x == n && u)
          return 1;
      }
    }
  return 0;
}
int main() {
  cin >> n >> q;
  cin >> s;
  for (int j = 1; j <= q; j++)
    cin >> a[j] >> b[j];
  int rez = n + 1, rez1 = 0;
  for (int step = 1 << 17; step; step >>= 1)
    if (rez - step >= 0 && sol(rez - step, 1))
      rez -= step;
  for (int step = 1 << 17; step; step >>= 1)
    if (rez1 + step <= n && sol(rez1 + step, 0))
      rez1 += step;
  if (rez > rez1)
    cout << rez - rez1 - 1;
  else
    cout << 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline void init(T &x) {
  x = 0;
  char ch = getchar();
  bool t = 0;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      t = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch - 48);
  if (t)
    x = -x;
  return;
}
int n, Q;
const int N = 1e5 + 10;
char S[N];
char opt[N];
int LR[N];
inline char Gc() {
  char ch = getchar();
  while (!(ch >= 'A' && ch <= 'Z'))
    ch = getchar();
  return ch;
}
inline bool checkL(int p) {
  for (int i = 1; i <= Q; ++i) {
    if (opt[i] == S[p])
      p += LR[i];
    if (p <= 0)
      return 1;
    if (p > n)
      return 0;
  }
  return 0;
}
inline bool checkR(int p) {
  for (int i = 1; i <= Q; ++i) {
    if (opt[i] == S[p])
      p += LR[i];
    if (p > n)
      return 1;
    if (p <= 0)
      return 0;
  }
  return 0;
}
int main() {
  init(n), init(Q);
  scanf("%s", S + 1);
  for (int i = 1; i <= Q; ++i) {
    char ch = Gc();
    opt[i] = ch;
    ch = Gc();
    LR[i] = (ch == 'L') ? (-1) : 1;
  }
  int l = 1, r = n, pos = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (checkL(mid))
      pos = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  int ans = pos;
  l = pos + 1, r = n;
  pos = n + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (checkR(mid))
      pos = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  ans += (n - pos + 1);
  cout << (n - ans) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline void init(T &x) {
  x = 0;
  char ch = getchar();
  bool t = 0;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      t = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch - 48);
  if (t)
    x = -x;
  return;
}
int n, Q;
const int N = 2e5 + 10;
char S[N];
char opt[N];
int LR[N];
inline char Gc() {
  char ch = getchar();
  while (!(ch >= 'A' && ch <= 'Z'))
    ch = getchar();
  return ch;
}
inline bool checkL(int p) {
  for (int i = 1; i <= Q; ++i) {
    if (opt[i] == S[p])
      p += LR[i];
    if (p <= 0)
      return 1;
    if (p > n)
      return 0;
  }
  return 0;
}
inline bool checkR(int p) {
  for (int i = 1; i <= Q; ++i) {
    if (opt[i] == S[p])
      p += LR[i];
    if (p > n)
      return 1;
    if (p <= 0)
      return 0;
  }
  return 0;
}
int main() {
  init(n), init(Q);
  scanf("%s", S + 1);
  for (int i = 1; i <= Q; ++i) {
    char ch = Gc();
    opt[i] = ch;
    ch = Gc();
    LR[i] = (ch == 'L') ? (-1) : 1;
  }
  int l = 1, r = n, pos = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (checkL(mid))
      pos = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  int ans = pos;
  l = pos + 1, r = n;
  pos = n + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (checkR(mid))
      pos = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  ans += (n - pos + 1);
  cout << (n - ans) << endl;
  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using lli = long long int;
using namespace std;
int inf = 1000000007;

int N, Q;
string s;
char t, d;

bool solve(vector<tuple<char, char>> q, int x) {
  char now = s[x];
  for (int i = 0; i < Q; i++) {
    tie(t, d) = q[i];
    if (t == now) {
      if (d == 'R')
        x++;
      else
        x--;
    }
    if (x == -1 || x == N)
      return false;
    now = s[x];
  }
  return true;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  vector<tuple<char, char>> quary(Q);
  for (int i = 0; i < Q; i++) {
    cin >> t >> d;
    quary[i] = make_tuple(t, d);
  }
  // 左端と右端探す
  int ok = -1;
  int ng = N;
  while (ng - ok != 1) {
    int x = (ng + ok) / 2;
    if (solve(quary, x))
      ok = x;
    else
      ng = x;
  }
  int left = ok;
  ok = left;
  ng = -1;
  while (ok - ng != 1) {
    int x = (ng + ok) / 2;
    if (solve(quary, x))
      ok = x;
    else
      ng = x;
  }
  cout << max(0, left - ok + 1) << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using lli = long long int;
using namespace std;
int inf = 1000000007;

int N, Q;
string s;
char t, d;

bool solve(vector<tuple<char, char>> q, int x) {
  char now = s[x];
  for (int i = 0; i < Q; i++) {
    tie(t, d) = q[i];
    if (t == now) {
      if (d == 'R')
        x++;
      else
        x--;
    }
    if (x == -1 || x == N)
      return false;
    now = s[x];
  }
  return true;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  vector<tuple<char, char>> quary(Q);
  for (int i = 0; i < Q; i++) {
    cin >> t >> d;
    quary[i] = make_tuple(t, d);
  }
  // 左端と右端探す
  int ok = -1;
  int ng = N;
  while (ng - ok != 1) {
    int x = (ng + ok) / 2;
    if (solve(quary, x))
      ok = x;
    else
      ng = x;
  }
  int left = ok;
  ok = N - 1;
  ng = -1;
  while (ok - ng != 1) {
    int x = (ng + ok) / 2;
    if (solve(quary, x))
      ok = x;
    else
      ng = x;
  }
  cout << max(0, left - ok + 1) << endl;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N, Q;
string s;
char t[100010], d[100010];

bool judge(int mid, int x) {
  int now = mid - 1;
  for (int i = 0; i < Q; i++) {
    if (s[now] == t[i]) {
      if (d[i] == 'L')
        now = now - 1;
      else
        now = now + 1;
    }
    if (now == x)
      return true;
    else if (x == N && now == -1)
      return false;
    else if (x == -1 && now == N)
      return false;
  }
  return false;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }

  int ok = 0, ng = N + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    if (judge(mid, -1))
      ok = mid;
    else
      ng = mid;
  }
  int ans = ok;

  ok = N + 1;
  ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    if (judge(mid, N))
      ok = mid;
    else
      ng = mid;
  }

  cout << N - min(ans + N - ok + 1, N) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N, Q;
string s;
char t[200020], d[200020];

bool judge(int mid, int x) {
  int now = mid - 1;
  for (int i = 0; i < Q; i++) {
    if (s[now] == t[i]) {
      if (d[i] == 'L')
        now = now - 1;
      else
        now = now + 1;
    }
    if (now == x)
      return true;
    else if (x == N && now == -1)
      return false;
    else if (x == -1 && now == N)
      return false;
  }
  return false;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }

  int ok = 0, ng = N + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    if (judge(mid, -1))
      ok = mid;
    else
      ng = mid;
  }
  int ans = ok;

  ok = N + 1;
  ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    if (judge(mid, N))
      ok = mid;
    else
      ng = mid;
  }

  cout << N - min(ans + N - ok + 1, N) << endl;

  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
const ll MOD = ll(1e9 + 7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(2e4 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n, q;
string s, t[MAX_N], d[MAX_N];

bool check(int k, bool f) {
  int pos = k;
  char now = s[k];
  REP(i, q) {
    if (now == t[i][0]) {
      if (d[i] == "L") {
        now = s[pos - 1];
        pos--;
      } else {
        now = s[pos + 1];
        pos++;
      }
    }
  }
  if (f)
    return pos == 0;
  else
    return pos == n + 1;
}

int main() {
  cin >> n >> q >> s;
  s.insert(0, "#");
  s.push_back('#');
  REP(i, q) { cin >> t[i] >> d[i]; }
  int l, r;
  int ok = 0, ng = n + 1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid, true)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  l = ok;
  ok = n + 1, ng = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid, false)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  r = ok;
  cout << r - l - 1 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
const ll MOD = ll(1e9 + 7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(2e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n, q;
string s, t[MAX_N], d[MAX_N];

bool check(int k, bool f) {
  int pos = k;
  char now = s[k];
  REP(i, q) {
    if (now == t[i][0]) {
      if (d[i] == "L") {
        now = s[pos - 1];
        pos--;
      } else {
        now = s[pos + 1];
        pos++;
      }
    }
  }
  if (f)
    return pos == 0;
  else
    return pos == n + 1;
}

int main() {
  cin >> n >> q >> s;
  s.insert(0, "#");
  s.push_back('#');
  REP(i, q) { cin >> t[i] >> d[i]; }
  int l, r;
  int ok = 0, ng = n + 1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid, true)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  l = ok;
  ok = n + 1, ng = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid, false)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  r = ok;
  cout << r - l - 1 << endl;

  return 0;
}
Problem p03081 (C++)

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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end());
#define vdelete(v) (v).erase(unique(all(v)), (v).end());
#define pb(n) push_back(n);
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, q, now, ans;
string s;
bool memo1[200000], memo2[200000];
char t[200000], d[200000];

signed main() {
  cin >> n >> q >> s;
  rep(i, q) cin >> t[i] >> d[i];
  now = 0;
  repm(i, q) {
    if (t[i] == s[now] && d[i] == 'L')
      memo1[now] = true, now++;
    if (now)
      if (t[i] == s[now - 1] && d[i] == 'R')
        now--, memo1[now] = false;
    if (now == s.size())
      break;
  }
  now = s.size() - 1;
  repm(i, q) {
    if (t[i] == s[now] && d[i] == 'R')
      memo2[now] = true, now--;
    if (now != s.size() - 1)
      if (t[i] == s[now + 1] && d[i] == 'L')
        n++, memo2[now] = false;
    if (now < 0)
      break;
  }
  rep(i, n) if (memo1[i] | memo2[i]) ans++;
  cout << n - ans << endl;
}


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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end());
#define vdelete(v) (v).erase(unique(all(v)), (v).end());
#define pb(n) push_back(n);
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, q, now, ans;
string s;
bool memo1[200000], memo2[200000];
char t[200000], d[200000];

signed main() {
  cin >> n >> q >> s;
  rep(i, q) cin >> t[i] >> d[i];
  now = 0;
  repm(i, q) {
    if (t[i] == s[now] && d[i] == 'L')
      memo1[now] = true, now++;
    if (now)
      if (t[i] == s[now - 1] && d[i] == 'R')
        now--, memo1[now] = false;
    if (now == s.size())
      break;
  }
  now = s.size() - 1;
  repm(i, q) {
    if (t[i] == s[now] && d[i] == 'R')
      memo2[now] = true, now--;
    if (now != s.size() - 1)
      if (t[i] == s[now + 1] && d[i] == 'L')
        now++, memo2[now] = false;
    if (now < 0)
      break;
  }
  rep(i, n) if (memo1[i] | memo2[i]) ans++;
  cout << n - ans << endl;
}
Problem p03081 (C++)

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

const int N = 1e5 + 7;

int n, q;
char c[N];
char a[N], b[N];
int dl, dr;

int die(int x) {
  for (int i = 1; i <= q; i++) {
    if (a[i] != c[x])
      continue;
    if (b[i] == 'R')
      x++;
    else
      x--;
    if (x == 0)
      return -1;
    if (x == n + 1)
      return 1;
  }
  return 0;
}

int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i];
  int l = 0, r = n + 1, md;
  while (r - l > 1) {
    md = (r + l) / 2;
    if (die(md) == -1)
      l = md;
    else
      r = md;
  }
  dl = l;
  l = 0, r = n + 1, md;
  while (r - l > 1) {
    md = (r + l) / 2;
    if (die(md) == 1)
      r = md;
    else
      l = md;
  }
  dr = r;
  if (dr <= dl)
    dr = dl + 1;
  cout << n - dl - (n - (dr) + 1);
  return 0;
}


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

const int N = 2e5 + 7;

int n, q;
char c[N];
char a[N], b[N];
int dl, dr;

int die(int x) {
  for (int i = 1; i <= q; i++) {
    if (a[i] != c[x])
      continue;
    if (b[i] == 'R')
      x++;
    else
      x--;
    if (x == 0)
      return -1;
    if (x == n + 1)
      return 1;
  }
  return 0;
}

int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i];
  int l = 0, r = n + 1, md;
  while (r - l > 1) {
    md = (r + l) / 2;
    if (die(md) == -1)
      l = md;
    else
      r = md;
  }
  dl = l;
  l = 0, r = n + 1, md;
  while (r - l > 1) {
    md = (r + l) / 2;
    if (die(md) == 1)
      r = md;
    else
      l = md;
  }
  dr = r;
  if (dr <= dl)
    dr = dl + 1;
  cout << n - dl - (n - (dr) + 1);
  return 0;
}
Problem p03081 (C++)

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

using namespace std;

int N, Q;
string s;
char t[123456], d[123456];

bool left(int p) {
  for (int i = 0; i < Q; i++) {
    if (t[i] == s[p]) {
      p += ((d[i] == 'L') ? -1 : 1);
    }
    if (p < 0)
      return true;
  }

  return false;
}

bool right(int p) {
  for (int i = 0; i < Q; i++) {
    if (t[i] == s[p]) {
      p += ((d[i] == 'L') ? -1 : 1);
    }
    if (p >= N)
      return true;
  }

  return false;
}

int main(int argc, char *argv[]) {

  cin >> N >> Q;
  cin >> s;

  int ans = N;

  for (int i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }

  int ok = 0, ng = N;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (left(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  if (left(ok)) {
    ans -= (ok + 1);
  }

  ok = N - 1, ng = -1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (right(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  if (right(ok)) {
    ans -= N - (ok);
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

int N, Q;
string s;
char t[223456], d[223456];

bool left(int p) {
  for (int i = 0; i < Q; i++) {
    if (t[i] == s[p]) {
      p += ((d[i] == 'L') ? -1 : 1);
    }
    if (p < 0)
      return true;
  }

  return false;
}

bool right(int p) {
  for (int i = 0; i < Q; i++) {
    if (t[i] == s[p]) {
      p += ((d[i] == 'L') ? -1 : 1);
    }
    if (p >= N)
      return true;
  }

  return false;
}

int main(int argc, char *argv[]) {

  cin >> N >> Q;
  cin >> s;

  int ans = N;

  for (int i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }

  int ok = 0, ng = N;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (left(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  if (left(ok)) {
    ans -= (ok + 1);
  }

  ok = N - 1, ng = -1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (right(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  if (right(ok)) {
    ans -= N - (ok);
  }

  cout << ans << endl;

  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>

using namespace std;

const int N = 2e5 + 123;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const int M = 210;

ll n, q;
char a[N], s[N], d[N];

bool checkleft(ll x) {
  ll curpos = x;
  for (int i = 1; i <= q; i++) {
    if (curpos == 0 || curpos == n + 1)
      break;
    if (a[curpos] == s[i]) {
      if (d[i] == 'L')
        curpos--;
      else
        curpos++;
    }
  }
  return (curpos == 0);
}

bool checkright(ll x) {
  ll curpos = x;
  for (int i = 1; i <= q; i++) {
    if (curpos == 0 || curpos == n + 1)
      break;
    if (a[curpos] == s[i]) {
      if (d[i] == 'L')
        curpos--;
      else
        curpos++;
    }
  }
  return (curpos == n + 1);
}

int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  freopen("input.txt", "r", stdin);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++)
    cin >> s[i] >> d[i];
  ll L = 1, R = n, M, a1 = 0, a2 = n + 1;
  while (L <= R) {
    M = (L + R) / 2;
    if (checkleft(M)) {
      a1 = max(a1, M);
      L = M + 1;
    } else {
      R = M - 1;
    }
  }
  L = 1;
  R = n;
  while (L <= R) {
    M = (L + R) / 2;
    if (checkright(M)) {
      a2 = min(a2, M);
      R = M - 1;
    } else {
      L = M + 1;
    }
  }
  a2 = n - a2 + 1;
  cout << n - a1 - a2;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>

using namespace std;

const int N = 2e5 + 123;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const int M = 210;

ll n, q;
char a[N], s[N], d[N];

bool checkleft(ll x) {
  ll curpos = x;
  for (int i = 1; i <= q; i++) {
    if (curpos == 0 || curpos == n + 1)
      break;
    if (a[curpos] == s[i]) {
      if (d[i] == 'L')
        curpos--;
      else
        curpos++;
    }
  }
  return (curpos == 0);
}

bool checkright(ll x) {
  ll curpos = x;
  for (int i = 1; i <= q; i++) {
    if (curpos == 0 || curpos == n + 1)
      break;
    if (a[curpos] == s[i]) {
      if (d[i] == 'L')
        curpos--;
      else
        curpos++;
    }
  }
  return (curpos == n + 1);
}

int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  //	freopen("input.txt", "r", stdin);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++)
    cin >> s[i] >> d[i];
  ll L = 1, R = n, M, a1 = 0, a2 = n + 1;
  while (L <= R) {
    M = (L + R) / 2;
    if (checkleft(M)) {
      a1 = max(a1, M);
      L = M + 1;
    } else {
      R = M - 1;
    }
  }
  L = 1;
  R = n;
  while (L <= R) {
    M = (L + R) / 2;
    if (checkright(M)) {
      a2 = min(a2, M);
      R = M - 1;
    } else {
      L = M + 1;
    }
  }
  a2 = n - a2 + 1;
  cout << n - a1 - a2;
  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
/// supercalifragilisticexpialidocious.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define PB pop_back
#define pb push_back
#define mp make_pair
#define int long long
#define sz(s) (int)s.size()
#define seper(n) setprecision(n)
#define all(sum) sum.begin(), sum.end()
#define mem(a, b) memset(a, b, sizeof a)
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef map<string, int> msi;
typedef pair<int, string> pis;
const int MAXN = 1e5 + 10;
int n, q;
string s;
pair<char, char> c[MAXN];
int f(int i) {
  int ptr = i;
  for (int j = 0; j < q; j++)
    if (c[j].f == s[ptr])
      if (c[j].s == 'L')
        ptr--;
      else
        ptr++;
  if (ptr < 0)
    return 0;
  if (ptr >= n)
    return 1;
  return 2;
}
int32_t main() {
  cin >> n >> q >> s;
  for (int i = 0; i < q; i++)
    cin >> c[i].f >> c[i].s;
  int l = -1, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (f(mid) == 1)
      r = mid;
    else
      l = mid;
  }
  int right = l;
  l = -1, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (!f(mid))
      l = mid;
    else
      r = mid;
  }
  return cout << n - (n - 1 - right + 1) - (l + 1) + 1 << endl, 0;
}


Fixed submission:
/// supercalifragilisticexpialidocious.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define PB pop_back
#define pb push_back
#define mp make_pair
#define int long long
#define sz(s) (int)s.size()
#define seper(n) setprecision(n)
#define all(sum) sum.begin(), sum.end()
#define mem(a, b) memset(a, b, sizeof a)
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef map<string, int> msi;
typedef pair<int, string> pis;
const int MAXN = 2e5 + 10;
int n, q;
string s;
pair<char, char> c[MAXN];
int f(int i) {
  int ptr = i;
  for (int j = 0; j < q; j++)
    if (c[j].f == s[ptr])
      if (c[j].s == 'L')
        ptr--;
      else
        ptr++;
  if (ptr < 0)
    return 0;
  if (ptr >= n)
    return 1;
  return 2;
}
int32_t main() {
  cin >> n >> q >> s;
  for (int i = 0; i < q; i++)
    cin >> c[i].f >> c[i].s;
  int l = -1, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (f(mid) == 1)
      r = mid;
    else
      l = mid;
  }
  int right = l;
  l = -1, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (!f(mid))
      l = mid;
    else
      r = mid;
  }
  return cout << n - (n - 1 - right + 1) - (l + 1) + 1 << endl, 0;
}