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

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for (int i = (x), i##_len = (int)(n); i < i##_len; ++i)
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 100;
const long long LLINF = 2e18 + 129;
const long long MOD = 1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

vector<lint> a;

lint cost(int i, int k) {
  if (k == 0 || k == 4) {
    return a[i];
  } else if (k == 1 || k == 3) {
    if (a[i] == 0) {
      return 2LL;
    } else if (a[i] % 2 == 0) {
      return 0LL;
    } else {
      return 1LL;
    }
  } else {
    if (a[i] % 2 == 0) {
      return 1LL;
    } else {
      return 0LL;
    }
  }
}

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

  lint l;
  cin >> l;

  a.resize(l);
  rep(i, l) { cin >> a[i]; }

  lint dp[100010][5];
  fill(dp[0], dp[l + 1], LLINF);
  dp[0][0] = 0;

  rep(i, l) {
    rep(j, 5) {
      REP(k, j, 5) { dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + cost(i, k)); }
    }
  }

  lint ans = LLINF;
  rep(i, 5) { ans = min(ans, dp[l][i]); }

  cout << ans << endl;

  return 0;
}

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for (int i = (x), i##_len = (int)(n); i < i##_len; ++i)
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 100;
const long long LLINF = 2e18 + 129;
const long long MOD = 1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

vector<lint> a;

lint cost(int i, int k) {
  if (k == 0 || k == 4) {
    return a[i];
  } else if (k == 1 || k == 3) {
    if (a[i] == 0) {
      return 2LL;
    } else if (a[i] % 2 == 0) {
      return 0LL;
    } else {
      return 1LL;
    }
  } else {
    if (a[i] % 2 == 0) {
      return 1LL;
    } else {
      return 0LL;
    }
  }
}

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

  lint l;
  cin >> l;

  a.resize(l);
  rep(i, l) { cin >> a[i]; }

  lint dp[200010][5];
  fill(dp[0], dp[l + 1], LLINF);
  dp[0][0] = 0;

  rep(i, l) {
    rep(j, 5) {
      REP(k, j, 5) { dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + cost(i, k)); }
    }
  }

  lint ans = LLINF;
  rep(i, 5) { ans = min(ans, dp[l][i]); }

  cout << ans << endl;

  return 0;
}
Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll INF = (9e18);
const int maxn = (1e5) + 10;
int n, a[maxn];
ll dp[maxn][5], ans = INF;
int op(int i, int j) {
  if (j == 1 || j == 3) {
    if (a[i] == 0)
      return 2;
    return a[i] & 1;
  }
  if (j == 2)
    return a[i] & 1 ^ 1;
  return a[i];
}
int main() {
  // freopen("1.txt","r",stdin);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 4; j++) {
      dp[i][j] = INF;
      for (int k = 0; k <= j; k++)
        dp[i][j] = min(dp[i][j], dp[i - 1][k]);
      dp[i][j] += op(i, j);
      if (i == n)
        ans = min(ans, dp[i][j]);
    }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll INF = (9e18);
const int maxn = (2e5) + 10;
int n, a[maxn];
ll dp[maxn][5], ans = INF;
int op(int i, int j) {
  if (j == 1 || j == 3) {
    if (a[i] == 0)
      return 2;
    return a[i] & 1;
  }
  if (j == 2)
    return a[i] & 1 ^ 1;
  return a[i];
}
int main() {
  // freopen("1.txt","r",stdin);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 4; j++) {
      dp[i][j] = INF;
      for (int k = 0; k <= j; k++)
        dp[i][j] = min(dp[i][j], dp[i - 1][k]);
      dp[i][j] += op(i, j);
      if (i == n)
        ans = min(ans, dp[i][j]);
    }
  printf("%lld\n", ans);
  return 0;
}
Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using P = pair<int, int>;
using node = pair<string, ll>;
const ll inf = numeric_limits<ll>::max() / 3;
const ll mod = 1e9 + 7;

ll a[100100];
ll acc[100100];
ll to_left[100100];
ll to_right[100100];
ll dp[100100];

int main(void) {
  int l;
  cin >> l;
  acc[0] = 0LL;
  for (int i = 1; i <= l; ++i) {
    cin >> a[i];
    acc[i] = acc[i - 1] + a[i];
  }

  to_left[0] = 0LL;
  for (int i = 1; i <= l; ++i) {
    if (a[i] == 0) {
      to_left[i] = min(2 + to_left[i - 1], acc[i]);
    } else {
      to_left[i] = min(a[i] % 2 + to_left[i - 1], acc[i]);
    }
  }

  to_right[l] = 0LL;
  for (int i = l - 1; i >= 0; --i) {
    if (a[i + 1] == 0) {
      to_right[i] = min(2 + to_right[i + 1], acc[l] - acc[i]);
    } else {
      to_right[i] = min(a[i + 1] % 2 + to_right[i + 1], acc[l] - acc[i]);
    }
  }

  dp[0] = 0LL;
  for (int i = 1; i <= l; ++i) {
    dp[i] = min((a[i] + 1) % 2 + dp[i - 1], to_left[i]);
  }
  ll res = inf;
  for (int i = 0; i <= l; ++i) {
    res = min(res, dp[i] + to_right[i]);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using P = pair<int, int>;
using node = pair<string, ll>;
const ll inf = numeric_limits<ll>::max() / 3;
const ll mod = 1e9 + 7;

ll a[200200];
ll acc[200200];
ll to_left[200200];
ll to_right[200200];
ll dp[200200];

int main(void) {
  int l;
  cin >> l;
  acc[0] = 0LL;
  for (int i = 1; i <= l; ++i) {
    cin >> a[i];
    acc[i] = acc[i - 1] + a[i];
  }

  to_left[0] = 0LL;
  for (int i = 1; i <= l; ++i) {
    if (a[i] == 0) {
      to_left[i] = min(2 + to_left[i - 1], acc[i]);
    } else {
      to_left[i] = min(a[i] % 2 + to_left[i - 1], acc[i]);
    }
  }

  to_right[l] = 0LL;
  for (int i = l - 1; i >= 0; --i) {
    if (a[i + 1] == 0) {
      to_right[i] = min(2 + to_right[i + 1], acc[l] - acc[i]);
    } else {
      to_right[i] = min(a[i + 1] % 2 + to_right[i + 1], acc[l] - acc[i]);
    }
  }

  dp[0] = 0LL;
  for (int i = 1; i <= l; ++i) {
    dp[i] = min((a[i] + 1) % 2 + dp[i - 1], to_left[i]);
  }
  ll res = inf;
  for (int i = 0; i <= l; ++i) {
    res = min(res, dp[i] + to_right[i]);
  }
  cout << res << endl;
  return 0;
}
Problem p03132 (C++)

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

const int inf = 1e15 + 10, N = 1e5 + 10;
int a[N], n;
long long dp[N][6], ans = inf;

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

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

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 5; j++)
      dp[i][j] = inf;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 5; j++) {
      int w = 0;
      if (a[i] == 0 && j != 0 && j != 4)
        w = 1 + (j % 2);
      else if (j == 2 && a[i] % 2 == 0)
        w = 1;
      else if ((j == 1 || j == 3) && a[i] % 2 != 0)
        w = 1;
      else if ((j == 0 || j == 4))
        w = a[i];

      for (int k = 0; k <= j; k++)
        dp[i][j] = min(dp[i - 1][k] + w, dp[i][j]);
    }
  }

  for (int i = 0; i < 5; i++)
    ans = min(ans, dp[n - 1][i]);

  cout << ans;
}

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

const int inf = 1e15 + 10, N = 3e5 + 10;
int a[N], n;
long long dp[N][6], ans = inf;

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

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

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 5; j++)
      dp[i][j] = inf;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 5; j++) {
      int w = 0;
      if (a[i] == 0 && j != 0 && j != 4)
        w = 1 + (j % 2);
      else if (j == 2 && a[i] % 2 == 0)
        w = 1;
      else if ((j == 1 || j == 3) && a[i] % 2 != 0)
        w = 1;
      else if ((j == 0 || j == 4))
        w = a[i];

      for (int k = 0; k <= j; k++)
        dp[i][j] = min(dp[i - 1][k] + w, dp[i][j]);
    }
  }

  for (int i = 0; i < 5; i++)
    ans = min(ans, dp[n - 1][i]);

  cout << ans;
}
Problem p03132 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
ll a[200010];
ll sum[200010];
ll dp[200010][2][3];
ll n;
ll func(ll here, ll ch, ll ch1) {
  if (ch1 >= 3)
    return sum[here];
  if (here == n + 1)
    return 0;
  ll &ret = dp[here][ch][ch1];
  ret = 9999999999999999;
  if (ch1 < 3) {
    ret = min(ret, func(here, ch ^ 1, ch1 + 1));
  }
  if (a[here] == 0) {
    if (ch == 0)
      ret = min(ret, func(here + 1, ch, ch1) + 2);
    else
      ret = min(ret, func(here + 1, ch, ch1) + 1);
  } else {
    if (ch == a[here] % 2)
      ret = min(ret, func(here + 1, ch, ch1));
    else
      ret = min(ret, func(here + 1, ch, ch1) + 1);
  }
  return ret;
}
int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = n; i >= 1; i--)
    sum[i] = sum[i + 1] + a[i];
  ll dap = 9999999999999999;
  ll sum1 = 0;
  for (int i = 1; i <= n; i++) {
    dap = min(func(i, 0, 0) + sum1, dap);
    sum1 += a[i];
  }
  printf("%lld", dap);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
ll a[200010];
ll sum[200010];
ll dp[200010][2][3];
ll n;
ll func(ll here, ll ch, ll ch1) {
  if (ch1 >= 3)
    return sum[here];
  if (here == n + 1)
    return 0;
  ll &ret = dp[here][ch][ch1];
  if (ret != -1)
    return ret;
  ret = 9999999999999999;
  if (ch1 < 3) {
    ret = min(ret, func(here, ch ^ 1, ch1 + 1));
  }
  if (a[here] == 0) {
    if (ch == 0)
      ret = min(ret, func(here + 1, ch, ch1) + 2);
    else
      ret = min(ret, func(here + 1, ch, ch1) + 1);
  } else {
    if (ch == a[here] % 2)
      ret = min(ret, func(here + 1, ch, ch1));
    else
      ret = min(ret, func(here + 1, ch, ch1) + 1);
  }
  return ret;
}
int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = n; i >= 1; i--)
    sum[i] = sum[i + 1] + a[i];
  ll dap = 9999999999999999;
  ll sum1 = 0;
  for (int i = 1; i <= n; i++) {
    dap = min(func(i, 0, 0) + sum1, dap);
    sum1 += a[i];
  }
  printf("%lld", dap);
}
Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int L;
ll A[100005], dp[100005][5];

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cin >> L;
  for (int i = 1; i <= L; i++) {
    cin >> A[i];
  }

  for (int i = 1; i <= L; i++) {
    ll mn = dp[i - 1][0];
    dp[i][0] = mn + A[i];

    mn = min(mn, dp[i - 1][1]);
    dp[i][1] = mn + (A[i] == 0 ? 2 : A[i] % 2);

    mn = min(mn, dp[i - 1][2]);
    dp[i][2] = mn - (A[i] % 2 - 1);

    mn = min(mn, dp[i - 1][3]);
    dp[i][3] = mn + (A[i] == 0 ? 2 : A[i] % 2);

    mn = min(mn, dp[i - 1][4]);
    dp[i][4] = mn + A[i];
  }

  ll ret = dp[L][0];
  for (int i = 1; i < 5; i++) {
    ret = min(ret, dp[L][i]);
  }

  cout << ret << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int L;
ll A[200005], dp[200005][5];

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cin >> L;
  for (int i = 1; i <= L; i++) {
    cin >> A[i];
  }

  for (int i = 1; i <= L; i++) {
    ll mn = dp[i - 1][0];
    dp[i][0] = mn + A[i];

    mn = min(mn, dp[i - 1][1]);
    dp[i][1] = mn + (A[i] == 0 ? 2 : A[i] % 2);

    mn = min(mn, dp[i - 1][2]);
    dp[i][2] = mn - (A[i] % 2 - 1);

    mn = min(mn, dp[i - 1][3]);
    dp[i][3] = mn + (A[i] == 0 ? 2 : A[i] % 2);

    mn = min(mn, dp[i - 1][4]);
    dp[i][4] = mn + A[i];
  }

  ll ret = dp[L][0];
  for (int i = 1; i < 5; i++) {
    ret = min(ret, dp[L][i]);
  }

  cout << ret << "\n";
  return 0;
}
Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(c) (c).begin(), (c).end()
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
using namespace std;
#define int long long

vector<int> calc1(int L, vector<int> &A) {
  vector<int> dp(L + 1);
  int sum = 0;
  for (int i = 1; i <= L; ++i) {
    sum += A[i];
    if (A[i] == 0) {
      dp[i] = min(sum, dp[i - 1] + 2);
    } else if (A[i] % 2 == 0) {
      dp[i] = min(sum, dp[i - 1]);
    } else {
      dp[i] = min(sum, dp[i - 1] + 1);
    }
  }
  return dp;
}
vector<int> calc2(int L, vector<int> &A, vector<int> &B) {
  vector<int> dp(L + 1);
  for (int i = 1; i <= L + 1; ++i) {
    int c;
    if (A[i] % 2 == 0) {
      c = 1;
    } else {
      c = 0;
    }
    dp[i] = min(B[i], dp[i - 1] + c);
  }
  return dp;
}

signed main() {
  int L;
  cin >> L;
  vector<int> A(L + 2);
  for (int i = 1; i <= L; ++i) {
    cin >> A[i];
  }
  vector<int> dp1 = calc1(L, A);
  vector<int> dp2 = calc2(L, A, dp1);

  reverse(ALL(A));
  vector<int> rdp1 = calc1(L, A);
  vector<int> rdp2 = calc2(L, A, rdp1);
  reverse(ALL(rdp1));
  reverse(ALL(rdp2));
  int res = (int)(1e17);
  /*
  for(int i = 0; i <= L; ++i) {
          cout << dp1[i] << "," << dp2[i] << "," << rdp1[i] << "," << rdp2[i] <<
  endl;
  }
  */
  for (int i = 1; i <= L; ++i) {
    res = min(res, dp1[i] + rdp2[i]);
    res = min(res, rdp1[i] + dp2[i]);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(c) (c).begin(), (c).end()
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
using namespace std;
#define int long long

vector<int> calc1(int L, vector<int> &A) {
  vector<int> dp(L + 1);
  int sum = 0;
  for (int i = 1; i <= L; ++i) {
    sum += A[i];
    if (A[i] == 0) {
      dp[i] = min(sum, dp[i - 1] + 2);
    } else if (A[i] % 2 == 0) {
      dp[i] = min(sum, dp[i - 1]);
    } else {
      dp[i] = min(sum, dp[i - 1] + 1);
    }
  }
  return dp;
}
vector<int> calc2(int L, vector<int> &A, vector<int> &B) {
  vector<int> dp(L + 1);
  for (int i = 1; i <= L; ++i) {
    int c;
    if (A[i] % 2 == 0) {
      c = 1;
    } else {
      c = 0;
    }
    dp[i] = min(B[i], dp[i - 1] + c);
  }
  return dp;
}

signed main() {
  int L;
  cin >> L;
  vector<int> A(L + 2);
  for (int i = 1; i <= L; ++i) {
    cin >> A[i];
  }
  vector<int> dp1 = calc1(L, A);
  vector<int> dp2 = calc2(L, A, dp1);

  reverse(ALL(A));
  vector<int> rdp1 = calc1(L, A);
  vector<int> rdp2 = calc2(L, A, rdp1);
  reverse(ALL(rdp1));
  reverse(ALL(rdp2));
  int res = (int)(1e17);
  /*
  for(int i = 0; i <= L; ++i) {
          cout << dp1[i] << "," << dp2[i] << "," << rdp1[i] << "," << rdp2[i] <<
  endl;
  }
  */
  for (int i = 1; i <= L; ++i) {
    res = min(res, dp1[i] + rdp2[i]);
    res = min(res, rdp1[i] + dp2[i]);
  }
  cout << res << endl;
  return 0;
}
Problem p03132 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

#define null ""
#define endl '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fr, to) for (int i = fr; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const num inf = 0x7f7f7f7f7f7f;

int n;
num a[100005], dp[100005][5], ans = inf;

int main() {
  memset(dp, inf, sizeof(dp));
  cin >> n;
  cont(i, n) { cin >> a[i]; }
  dp[0][0] = 0;
  cont(i, n) {
    circ(j, 0, 4) {
      circ(k, 0, j) {
        if (j == 0 || j == 4) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + a[i]);
        } else if (j == 1 || j == 3) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + (a[i] > 0 ? a[i] % 2 : 2));
        } else {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + (a[i] + 1) % 2);
        }
      }
    }
  }
  ans = min(ans, dp[n][0]);
  ans = min(ans, dp[n][1]);
  ans = min(ans, dp[n][2]);
  ans = min(ans, dp[n][3]);
  ans = min(ans, dp[n][4]);
  cout << ans << endl;
}


Fixed submission:

#include <bits/stdc++.h>

#define null ""
#define endl '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fr, to) for (int i = fr; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const num inf = 0x7f7f7f7f7f7f;

int n;
num a[200005], dp[200005][5], ans = inf;

int main() {
  memset(dp, inf, sizeof(dp));
  cin >> n;
  cont(i, n) { cin >> a[i]; }
  dp[0][0] = 0;
  cont(i, n) {
    circ(j, 0, 4) {
      circ(k, 0, j) {
        if (j == 0 || j == 4) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + a[i]);
        } else if (j == 1 || j == 3) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + (a[i] > 0 ? a[i] % 2 : 2));
        } else {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + (a[i] + 1) % 2);
        }
      }
    }
  }
  ans = min(ans, dp[n][0]);
  ans = min(ans, dp[n][1]);
  ans = min(ans, dp[n][2]);
  ans = min(ans, dp[n][3]);
  ans = min(ans, dp[n][4]);
  cout << ans << endl;
}
Problem p03132 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define llong long long
using namespace std;

const int N = 1e5;
llong a[N + 3];
int b[N + 3];
llong dp[N + 3][5];
int n;

int eval(int x, int y, int id) {
  if (x == 0 || x == 4) {
    return a[id];
  } else if (x == 1 || x == 3) {
    return 2 - y;
  } else if (x == 2) {
    return y == 1 ? 0 : 1;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0)
      b[i] = 0;
    else if (a[i] & 1)
      b[i] = 1;
    else
      b[i] = 2;
  }
  memset(dp, 42, sizeof(dp));
  for (int i = 0; i <= 4; i++)
    dp[0][i] = 0ll;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 4; j++) {
      for (int k = 0; k <= j; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + eval(j, b[i], i));
      }
    }
  }
  llong ans =
      min(min(min(dp[n][0], dp[n][1]), dp[n][2]), min(dp[n][3], dp[n][4]));
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define llong long long
using namespace std;

const int N = 2e5;
llong a[N + 3];
int b[N + 3];
llong dp[N + 3][5];
int n;

int eval(int x, int y, int id) {
  if (x == 0 || x == 4) {
    return a[id];
  } else if (x == 1 || x == 3) {
    return 2 - y;
  } else if (x == 2) {
    return y == 1 ? 0 : 1;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0)
      b[i] = 0;
    else if (a[i] & 1)
      b[i] = 1;
    else
      b[i] = 2;
  }
  memset(dp, 42, sizeof(dp));
  for (int i = 0; i <= 4; i++)
    dp[0][i] = 0ll;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 4; j++) {
      for (int k = 0; k <= j; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + eval(j, b[i], i));
      }
    }
  }
  llong ans =
      min(min(min(dp[n][0], dp[n][1]), dp[n][2]), min(dp[n][3], dp[n][4]));
  printf("%lld\n", ans);
  return 0;
}
Problem p03133 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;

const int MOD = 998244353;

bool p[302][302];

int _pow(int x) {
  int v = 1, w = 2;

  while (x > 0) {
    if (x & 1)
      v = 1LL * v * w % MOD;
    w = 1LL * w * w % MOD;
    x >>= 1;
  }

  return v;
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      int x;
      scanf("%d", &x);
      p[i][j] = x == 1;
    }
  }

  int f = 0;

  for (int i = 1; i <= M; i++) {
    int z = 0;

    for (int j = f + 1; j <= N; j++) {
      if (p[j][i]) {
        z = j;
        break;
      }
    }

    if (z == 0)
      continue;

    f++;
    for (int j = 1; j <= M; j++)
      swap(p[f][j], p[z][j]);

    for (int j = 1; j <= N; j++) {
      if (j == f)
        continue;

      if (p[j][i]) {
        for (int k = 1; i <= M; k++)
          p[j][k] ^= p[f][k];
      }
    }
  }

  int a = _pow(N), b = _pow(N - f), c = _pow(M - 1);
  int ans = 1LL * (a - b < 0 ? a - b + MOD : a - b) * c % MOD;

  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;

const int MOD = 998244353;

bool p[302][302];

int _pow(int x) {
  int v = 1, w = 2;

  while (x > 0) {
    if (x & 1)
      v = 1LL * v * w % MOD;
    w = 1LL * w * w % MOD;
    x >>= 1;
  }

  return v;
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      int x;
      scanf("%d", &x);
      p[i][j] = x == 1;
    }
  }

  int f = 0;

  for (int i = 1; i <= M; i++) {
    int z = 0;

    for (int j = f + 1; j <= N; j++) {
      if (p[j][i]) {
        z = j;
        break;
      }
    }

    if (z == 0)
      continue;

    f++;
    for (int j = 1; j <= M; j++)
      swap(p[f][j], p[z][j]);

    for (int j = 1; j <= N; j++) {
      if (j == f)
        continue;

      if (p[j][i]) {
        for (int k = 1; k <= M; k++)
          p[j][k] ^= p[f][k];
      }
    }
  }

  int a = _pow(N), b = _pow(N - f), c = _pow(M - 1);
  int ans = 1LL * (a - b < 0 ? a - b + MOD : a - b) * c % MOD;

  printf("%d\n", ans);
  return 0;
}
Problem p03133 (C++)

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

#define MOD 998244353

using namespace std;

int pow(int x, int pow) {
  int res = 1, t = x;
  while (pow) {
    if (pow & 1) {
      res = 1LL * res * t % MOD;
    }
    t = 1LL * t * t % MOD;
    pow >>= 1;
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  int **mat = new int *[n];
  for (int i = 0; i < n; i++) {
    mat[i] = new int[m];
    for (int j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
  }
  int cur = 0;
  for (int c = 0; c < m; c++) {
    for (int r = cur; r < n; r++) {
      if (mat[r][c]) {
        swap(mat[cur], mat[r]);
        break;
      }
    }
    if (!mat[cur][c]) {
      continue;
    }
    for (int r = 0; r < n; r++) {
      if (r != cur && mat[r][c]) {
        for (int k = c; k < m; k++) {
          mat[r][k] ^= mat[cur][k];
        }
      }
    }
    cur++;
  }

  int tot = pow(2, n + m - 1);
  tot -= pow(2, n + m - cur - 1);
  if (tot < 0) {
    tot += MOD;
  }
  cout << tot << endl;

  for (int i = 0; i < n; i++) {
    delete mat[i];
  }
  delete mat;
  return 0;
}

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

#define MOD 998244353

using namespace std;

int pow(int x, int pow) {
  int res = 1, t = x;
  while (pow) {
    if (pow & 1) {
      res = 1LL * res * t % MOD;
    }
    t = 1LL * t * t % MOD;
    pow >>= 1;
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  int **mat = new int *[n];
  for (int i = 0; i < n; i++) {
    mat[i] = new int[m];
    for (int j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
  }
  int cur = 0;
  for (int c = 0; cur < n && c < m; c++) {
    for (int r = cur; r < n; r++) {
      if (mat[r][c]) {
        swap(mat[cur], mat[r]);
        break;
      }
    }
    if (!mat[cur][c]) {
      continue;
    }
    for (int r = 0; r < n; r++) {
      if (r != cur && mat[r][c]) {
        for (int k = c; k < m; k++) {
          mat[r][k] ^= mat[cur][k];
        }
      }
    }
    cur++;
  }

  int tot = pow(2, n + m - 1);
  tot -= pow(2, n + m - cur - 1);
  if (tot < 0) {
    tot += MOD;
  }
  cout << tot << endl;

  for (int i = 0; i < n; i++) {
    delete mat[i];
  }
  delete mat;
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>

const int MOD = 998244353;

const int ms = 2020;

int memo[ms][ms + ms];
std::string str;

int dp(int round, int red, int blue) {
  if (round < (int)str.size()) {
    if (str[round] == '0')
      red += 2;
    else if (str[round] == '1') {
      red++;
      blue++;
    } else
      blue += 2;
  }
  int &ans = memo[round][red];
  if (ans != -1)
    return ans;
  ans = 0;
  if (red + blue == 0)
    return ans = 1;
  if (red) {
    ans += dp(round + 1, red - 1, blue);
  }
  if (blue) {
    ans += dp(round + 1, red, blue - 1);
  }
  if (ans >= MOD)
    ans -= MOD;
  return ans;
}

int main() {
  memset(memo, -1, sizeof memo);
  std::cin >> str;
  std::cout << dp(0, 0, 0) << '\n';
}

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

const int MOD = 998244353;

const int ms = 2020;

int memo[ms + ms][ms + ms];
std::string str;

int dp(int round, int red, int blue) {
  if (round < (int)str.size()) {
    if (str[round] == '0')
      red += 2;
    else if (str[round] == '1') {
      red++;
      blue++;
    } else
      blue += 2;
  }
  int &ans = memo[round][red];
  if (ans != -1)
    return ans;
  ans = 0;
  if (red + blue == 0)
    return ans = 1;
  if (red) {
    ans += dp(round + 1, red - 1, blue);
  }
  if (blue) {
    ans += dp(round + 1, red, blue - 1);
  }
  if (ans >= MOD)
    ans -= MOD;
  return ans;
}

int main() {
  memset(memo, -1, sizeof memo);
  std::cin >> str;
  std::cout << dp(0, 0, 0) << '\n';
}
Problem p03134 (C++)

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

using namespace std;

typedef long long ll;

int readint() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int cys = 998244353;
int n;
char s[2005];
int sum[2005], d[2005][4005];

int mod(int x) { return x >= cys ? x - cys : x; }

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + s[i] - '0';
  d[0][0] = 1;
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = sum[min(n, i)]; j >= 0; j--) {
      if (i - j <= 2 * min(n, i) - sum[min(n, i)])
        d[i][j] = d[i - 1][j];
      if (j >= 1)
        d[i][j] = mod(d[i][j] + d[i - 1][j - 1]);
    }
  }
  cout << d[2 * n][sum[n]] << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

int readint() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int cys = 998244353;
int n;
char s[2005];
int sum[2005], d[4005][4005];

int mod(int x) { return x >= cys ? x - cys : x; }

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + s[i] - '0';
  d[0][0] = 1;
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = sum[min(n, i)]; j >= 0; j--) {
      if (i - j <= 2 * min(n, i) - sum[min(n, i)])
        d[i][j] = d[i - 1][j];
      if (j >= 1)
        d[i][j] = mod(d[i][j] + d[i - 1][j - 1]);
    }
  }
  cout << d[2 * n][sum[n]] << endl;
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 998244353;
ll dp[2010][2010];
ll cntr[4010];
ll cntb[4010];

int main() {
  string S;
  cin >> S;
  dp[0][0] = 1;
  for (ll i = 0; i < S.size(); i++) {
    char c = S[i];
    cntr[i + 1] = cntr[i] + 2 * (c == '0') + (c == '1');
    cntb[i + 1] = cntb[i] + 2 * (c == '2') + (c == '1');
  }
  for (ll i = S.size(); i < 4010; i++) {
    cntr[i + 1] = cntr[i];
    cntb[i + 1] = cntb[i];
  }
  for (ll i = 1; i <= 2 * S.size(); i++) {
    for (ll r = 0; r <= min(i, cntr[i]); r++) {
      ll b = i - r;
      if (b > cntb[i])
        continue;
      if (r > 0)
        dp[r][b] += dp[r - 1][b];
      if (b > 0)
        dp[r][b] += dp[r][b - 1];
      dp[r][b] %= MOD;
    }
  }
  cout << dp[cntr[4010 - 1]][cntb[4010 - 1]] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 998244353;
ll dp[4010][4010];
ll cntr[4010];
ll cntb[4010];

int main() {
  string S;
  cin >> S;
  dp[0][0] = 1;
  for (ll i = 0; i < S.size(); i++) {
    char c = S[i];
    cntr[i + 1] = cntr[i] + 2 * (c == '0') + (c == '1');
    cntb[i + 1] = cntb[i] + 2 * (c == '2') + (c == '1');
  }
  for (ll i = S.size(); i < 4010; i++) {
    cntr[i + 1] = cntr[i];
    cntb[i + 1] = cntb[i];
  }
  for (ll i = 1; i <= 2 * S.size(); i++) {
    for (ll r = 0; r <= min(i, cntr[i]); r++) {
      ll b = i - r;
      if (b > cntb[i])
        continue;
      if (r > 0)
        dp[r][b] += dp[r - 1][b];
      if (b > 0)
        dp[r][b] += dp[r][b - 1];
      dp[r][b] %= MOD;
    }
  }
  cout << dp[cntr[4010 - 1]][cntb[4010 - 1]] << endl;
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2010;
const int mod = 998244353;
int sum1, sum2;
char s[MAXN];
int f[MAXN][2], dp[MAXN][MAXN];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    sum1 += (s[i] - '0');
  sum2 = 2 * n - sum1;
  for (int i = 1; i <= n; i++)
    f[i][0] += f[i - 1][0] + (s[i] - '0'),
        f[i][1] += f[i - 1][1] + 2 - (s[i] - '0');
  dp[0][0] = 1;
  for (int i = 0; i <= sum1; i++) {
    for (int j = 0; j <= sum2; j++)
      if (i | j)
        if (i <= f[min(i + j, n)][0] && j <= f[min(i + j, n)][1]) {
          if (i)
            dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
          if (j)
            dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
        }
  }
  cout << dp[sum1][sum2] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 4010;
const int mod = 998244353;
int sum1, sum2;
char s[MAXN];
int f[MAXN][2], dp[MAXN][MAXN];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    sum1 += (s[i] - '0');
  sum2 = 2 * n - sum1;
  for (int i = 1; i <= n; i++)
    f[i][0] += f[i - 1][0] + (s[i] - '0'),
        f[i][1] += f[i - 1][1] + 2 - (s[i] - '0');
  dp[0][0] = 1;
  for (int i = 0; i <= sum1; i++) {
    for (int j = 0; j <= sum2; j++)
      if (i | j)
        if (i <= f[min(i + j, n)][0] && j <= f[min(i + j, n)][1]) {
          if (i)
            dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
          if (j)
            dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
        }
  }
  cout << dp[sum1][sum2] << endl;
}
Problem p03134 (C++)

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

int dp[2005][2005];
int tot[2005][2];
const int mod = 998244353;

signed main() {

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

#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  string a;
  cin >> a;
  int n = a.length();
  dp[0][0] = 1;
  a = "#" + a;
  for (int i = 1; i <= n; i++) {
    tot[i][0] = tot[i - 1][0];
    tot[i][1] = tot[i - 1][1];
    if (a[i] == '0')
      tot[i][0] += 2;
    else if (a[i] == '1') {
      tot[i][0]++;
      tot[i][1]++;
    } else {
      tot[i][1] += 2;
    }
  }

  for (int i = 0; i <= tot[n][0]; i++) {
    for (int j = 0; j <= tot[n][1]; j++) {
      if (i + j == 0)
        continue;
      if (i > 0) {
        int z = i + j;
        z = min(z, n);
        if (tot[z][0] >= i) {
          dp[i][j] += dp[i - 1][j];
        }
      }
      if (j > 0) {
        int z = i + j;
        z = min(z, n);
        if (tot[z][1] >= j) {
          dp[i][j] += dp[i][j - 1];
        }
      }
      dp[i][j] %= mod;
    }
  }
  cout << dp[tot[n][0]][tot[n][1]];
}

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

int dp[4005][4005];
int tot[2005][2];
const int mod = 998244353;

signed main() {

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

#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  string a;
  cin >> a;
  int n = a.length();
  dp[0][0] = 1;
  a = "#" + a;
  for (int i = 1; i <= n; i++) {
    tot[i][0] = tot[i - 1][0];
    tot[i][1] = tot[i - 1][1];
    if (a[i] == '0')
      tot[i][0] += 2;
    else if (a[i] == '1') {
      tot[i][0]++;
      tot[i][1]++;
    } else {
      tot[i][1] += 2;
    }
  }

  for (int i = 0; i <= tot[n][0]; i++) {
    for (int j = 0; j <= tot[n][1]; j++) {
      if (i + j == 0)
        continue;
      if (i > 0) {
        int z = i + j;
        z = min(z, n);
        if (tot[z][0] >= i) {
          dp[i][j] += dp[i - 1][j];
        }
      }
      if (j > 0) {
        int z = i + j;
        z = min(z, n);
        if (tot[z][1] >= j) {
          dp[i][j] += dp[i][j - 1];
        }
      }
      dp[i][j] %= mod;
    }
  }
  cout << dp[tot[n][0]][tot[n][1]];
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 2000
#define MOD 998244353
char s[MAXN + 5];
int n, num1[MAXN + 5], num2[MAXN + 5];
int dp[MAXN + 5][MAXN + 5];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    num2[i] = s[i] - '0';
    num1[i] = 2 - num2[i] + num1[i - 1];
    num2[i] += num2[i - 1];
  }
  dp[0][0] = 1;
  for (int i = 1; i <= n * 2; i++) {
    int cnt1 = num1[min(n, i)], cnt2 = num2[min(n, i)];
    for (int j = 0; j <= i; j++)
      if (cnt1 >= j && cnt2 >= i - j) {
        dp[i][j] = dp[i - 1][j];
        if (j > 0)
          dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
      }
  }
  printf("%d\n", dp[n * 2][num1[n]]);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 4000
#define MOD 998244353
char s[MAXN + 5];
int n, num1[MAXN + 5], num2[MAXN + 5];
int dp[MAXN + 5][MAXN + 5];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    num2[i] = s[i] - '0';
    num1[i] = 2 - num2[i] + num1[i - 1];
    num2[i] += num2[i - 1];
  }
  dp[0][0] = 1;
  for (int i = 1; i <= n * 2; i++) {
    int cnt1 = num1[min(n, i)], cnt2 = num2[min(n, i)];
    for (int j = 0; j <= i; j++)
      if (cnt1 >= j && cnt2 >= i - j) {
        dp[i][j] = dp[i - 1][j];
        if (j > 0)
          dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
      }
  }
  printf("%d\n", dp[n * 2][num1[n]]);
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

ll dp[2020][2020];
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> red, blue;
  rep(i, n) {
    if (s[i] == '0') {
      red.push_back(i);
      red.push_back(i);
    }
    if (s[i] == '1') {
      red.push_back(i);
      blue.push_back(i);
    }
    if (s[i] == '2') {
      blue.push_back(i);
      blue.push_back(i);
    }
  }
  dp[0][0] = 1;
  int r = red.size(), b = blue.size();
  rep(i, r + 1) rep(j, b + 1) {
    dp[i][j] %= mod;
    int m = i + j;
    if (j < b && blue[j] <= m)
      dp[i][j + 1] += dp[i][j];
    if (i < r && red[i] <= m)
      dp[i + 1][j] += dp[i][j];
  }
  cout << dp[r][b] % mod << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;

ll dp[4020][4020];
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> red, blue;
  rep(i, n) {
    if (s[i] == '0') {
      red.push_back(i);
      red.push_back(i);
    }
    if (s[i] == '1') {
      red.push_back(i);
      blue.push_back(i);
    }
    if (s[i] == '2') {
      blue.push_back(i);
      blue.push_back(i);
    }
  }
  dp[0][0] = 1;
  int r = red.size(), b = blue.size();
  rep(i, r + 1) rep(j, b + 1) {
    dp[i][j] %= mod;
    int m = i + j;
    if (j < b && blue[j] <= m)
      dp[i][j + 1] += dp[i][j];
    if (i < r && red[i] <= m)
      dp[i + 1][j] += dp[i][j];
  }
  cout << dp[r][b] % mod << endl;
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <vector>

using ll = long long int;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = 998244353ll;

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll memo[2222][2222];
bool done[2222][2222];

int m, r[4444], b[4444];

ll solve(int i, int j) {
  if (done[i][j]) {
    return memo[i][j];
  }
  done[i][j] = true;
  if (i > r[i + j] || j > b[i + j]) {
    return memo[i][j] = 0ll;
  }
  if (i + j == m) {
    return memo[i][j] = 1ll;
  }
  memo[i][j] = solve(i + 1, j) + solve(i, j + 1);
  memo[i][j] %= MOD;
  return memo[i][j];
}

int main(void) {
  int n, i;
  std::string s;
  std::cin >> s;
  n = s.l_ength();
  m = n * 2;
  for (i = 0; i < n; ++i) {
    r[i + 1] = s[i] - '0';
    b[i + 1] = '2' - s[i];
  }
  for (i = 1; i < m; ++i) {
    r[i + 1] += r[i];
    b[i + 1] += b[i];
  }
  std::cout << solve(0, 0) << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <vector>

using ll = long long int;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = 998244353ll;

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll memo[4444][4444];
bool done[4444][4444];

int m, r[4444], b[4444];

ll solve(int i, int j) {
  if (done[i][j]) {
    return memo[i][j];
  }
  done[i][j] = true;
  if (i > r[i + j] || j > b[i + j]) {
    return memo[i][j] = 0ll;
  }
  if (i + j == m) {
    return memo[i][j] = 1ll;
  }
  memo[i][j] = solve(i + 1, j) + solve(i, j + 1);
  memo[i][j] %= MOD;
  return memo[i][j];
}

int main(void) {
  int n, i;
  std::string s;
  std::cin >> s;
  n = s.l_ength();
  m = n * 2;
  for (i = 0; i < n; ++i) {
    r[i + 1] = s[i] - '0';
    b[i + 1] = '2' - s[i];
  }
  for (i = 1; i < m; ++i) {
    r[i + 1] += r[i];
    b[i + 1] += b[i];
  }
  std::cout << solve(0, 0) << std::endl;
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long intl;
int MOD = 998244353;
using namespace std;
int main() {
  vector<vector<int>> dp(2001, vector<int>(2001, 0));
  vector<vector<int>> visited(2001, vector<int>(2001, 0));
  string s;
  cin >> s;
  int n = s.size();
  vector<pair<int, int>> v;
  v.push_back({0, 0});
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == '0') {
      v.push_back({v[i - 1].first + 2, v[i - 1].second});
    } else if (s[i - 1] == '1') {
      v.push_back({v[i - 1].first + 1, v[i - 1].second + 1});
    } else {
      v.push_back({v[i - 1].first, v[i - 1].second + 2});
    }
  }
  dp[0][0] = 1;
  queue<pair<int, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    int a = q.front().first;
    int b = q.front().second;
    q.pop();
    if (visited[a][b]) {
      continue;
    }
    visited[a][b] = 1;

    int tot = a + b + 1;
    if (tot > n) {
      tot = n;
    }

    if (v[tot].second > b) {
      dp[a][b + 1] += dp[a][b];
      dp[a][b + 1] = dp[a][b + 1] % MOD;
      q.push({a, b + 1});
    }
    if (v[tot].first > a) {
      dp[a + 1][b] += dp[a][b];
      dp[a + 1][b] = dp[a + 1][b] % MOD;
      q.push({a + 1, b});
    }
  }
  cout << dp[v[n].first][v[n].second] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long intl;
int MOD = 998244353;
using namespace std;
int main() {
  vector<vector<int>> dp(4001, vector<int>(4001, 0));
  vector<vector<int>> visited(4001, vector<int>(4001, 0));
  string s;
  cin >> s;
  int n = s.size();
  vector<pair<int, int>> v;
  v.push_back({0, 0});
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == '0') {
      v.push_back({v[i - 1].first + 2, v[i - 1].second});
    } else if (s[i - 1] == '1') {
      v.push_back({v[i - 1].first + 1, v[i - 1].second + 1});
    } else {
      v.push_back({v[i - 1].first, v[i - 1].second + 2});
    }
  }
  dp[0][0] = 1;
  queue<pair<int, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    int a = q.front().first;
    int b = q.front().second;
    q.pop();
    if (visited[a][b]) {
      continue;
    }
    visited[a][b] = 1;

    int tot = a + b + 1;
    if (tot > n) {
      tot = n;
    }

    if (v[tot].second > b) {
      dp[a][b + 1] += dp[a][b];
      dp[a][b + 1] = dp[a][b + 1] % MOD;
      q.push({a, b + 1});
    }
    if (v[tot].first > a) {
      dp[a + 1][b] += dp[a][b];
      dp[a + 1][b] = dp[a + 1][b] % MOD;
      q.push({a + 1, b});
    }
  }
  cout << dp[v[n].first][v[n].second] << endl;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
typedef long long ll;
using namespace std;

const int N = 2010, mod = 998244353;
char s[N];
int n, ans, a[N], b[N], f[N][N];
void inc(int &x, int y) {
  x += y;
  x -= (x >= mod) ? mod : 0;
}

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  rep(i, 1, n) a[i] = a[i - 1] + 2 - (s[i] - '0'), b[i] = b[i - 1] + s[i] - '0';
  f[0][0] = 1;
  rep(i, 0, 2 * n) rep(j, 0, i) if (f[i][j]) {
    if (a[min(i + 1, n)] > j)
      inc(f[i + 1][j + 1], f[i][j]);
    if (b[min(i + 1, n)] > i - j)
      inc(f[i + 1][j], f[i][j]);
  }
  printf("%d\n", f[2 * n][a[n]]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
typedef long long ll;
using namespace std;

const int N = 4010, mod = 998244353;
char s[N];
int n, ans, a[N], b[N], f[N][N];
void inc(int &x, int y) {
  x += y;
  x -= (x >= mod) ? mod : 0;
}

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  rep(i, 1, n) a[i] = a[i - 1] + 2 - (s[i] - '0'), b[i] = b[i - 1] + s[i] - '0';
  f[0][0] = 1;
  rep(i, 0, 2 * n) rep(j, 0, i) if (f[i][j]) {
    if (a[min(i + 1, n)] > j)
      inc(f[i + 1][j + 1], f[i][j]);
    if (b[min(i + 1, n)] > i - j)
      inc(f[i + 1][j], f[i][j]);
  }
  printf("%d\n", f[2 * n][a[n]]);
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int mo = 998244353;

int f[2001][2001], s1[2001], s2[2001];
char s[2001];

int main() {
  scanf("%s", s);
  int n = strlen(s);
  for (int i = 0; i < n; i++) {
    s1[i + 1] = s1[i] + s[i] - '0';
    s2[i + 1] = 2 * (i + 1) - s1[i + 1];
  }
  f[0][0] = 1;
  for (int i = 0; i <= s1[n]; i++)
    for (int j = 0; j <= s2[n]; j++)
      if ((s1[min(i + j, n)] >= i) && (s2[min(i + j, n)] >= j)) {
        if (i)
          f[i][j] = (f[i][j] + f[i - 1][j]) % mo;
        if (j)
          f[i][j] = (f[i][j] + f[i][j - 1]) % mo;
      }
  cout << f[s1[n]][s2[n]] << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int mo = 998244353;

int f[4001][4001], s1[2001], s2[2001];
char s[2001];

int main() {
  scanf("%s", s);
  int n = strlen(s);
  for (int i = 0; i < n; i++) {
    s1[i + 1] = s1[i] + s[i] - '0';
    s2[i + 1] = 2 * (i + 1) - s1[i + 1];
  }
  f[0][0] = 1;
  for (int i = 0; i <= s1[n]; i++)
    for (int j = 0; j <= s2[n]; j++)
      if ((s1[min(i + j, n)] >= i) && (s2[min(i + j, n)] >= j)) {
        if (i)
          f[i][j] = (f[i][j] + f[i - 1][j]) % mo;
        if (j)
          f[i][j] = (f[i][j] + f[i][j - 1]) % mo;
      }
  cout << f[s1[n]][s2[n]] << endl;
  return 0;
}
Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

const int maxn = 2100;
const int mod = 998244353;
char z[maxn];
int n;
int prer[maxn];
int dp[maxn * 2][maxn * 2];
void work() {
  int i, j;
  scanf("%s", z + 1);
  n = strlen(z + 1);
  for (i = 1; i <= n; i++)
    prer[i] = prer[i - 1] + (2 - z[i] + '0');
  for (i = n + 1; i <= n + n; i++)
    prer[i] = prer[i - 1];
  dp[0][0] = 1;
  for (i = 0; i < n * 2; i++) {
    for (j = 0; j <= i; j++) {
      if (j < prer[i + 1]) {
        dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % mod;
      }
      if (i - j < i * 2 + 2 - prer[i + 1]) {
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod;
      }
      // printf ( "%d %d %d\n" , i , j , dp[i][j] );
    }
  }
  printf("%d\n", dp[n * 2][prer[n * 2]]);
}
int main() {
  work();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

const int maxn = 2100;
const int mod = 998244353;
char z[maxn];
int n;
int prer[maxn * 2];
int dp[maxn * 2][maxn * 2];
void work() {
  int i, j;
  scanf("%s", z + 1);
  n = strlen(z + 1);
  for (i = 1; i <= n; i++)
    prer[i] = prer[i - 1] + (2 - z[i] + '0');
  for (i = n + 1; i <= n + n; i++)
    prer[i] = prer[i - 1];
  dp[0][0] = 1;
  for (i = 0; i < n * 2; i++) {
    for (j = 0; j <= i; j++) {
      if (j < prer[i + 1]) {
        dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % mod;
      }
      if (i - j < i * 2 + 2 - prer[i + 1]) {
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod;
      }
      // printf ( "%d %d %d\n" , i , j , dp[i][j] );
    }
  }
  printf("%d\n", dp[n * 2][prer[n * 2]]);
}
int main() {
  work();
  return 0;
}