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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int p[1024][1 << 17], v[1 << 19], w[1 << 19], q, a, l, i;
int f(int a, int l) {
  if (l < 0)
    return -1e9;
  if (!a)
    return 0;
  if (a > 1023)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~p[a][l])
    return p[a][l];
  return p[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
};
int main() {
  memset(p, -1, 1 << 31);
  cin >> l;
  for (; i++ < l;)
    cin >> v[i] >> w[i];
  cin >> q;
  while (q--) {
    cin >> a >> l;
    cout << f(a, l) << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int p[1024][1 << 17], v[1 << 19], w[1 << 19], q, a, l, i;
int f(int a, int l) {
  if (l < 0)
    return -1e9;
  if (!a)
    return 0;
  if (a > 1023)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~p[a][l])
    return p[a][l];
  return p[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
};
int main() {
  memset(p, -1, 1 << 29);
  cin >> l;
  for (; i++ < l;)
    cin >> v[i] >> w[i];
  cin >> q;
  while (q--) {
    cin >> a >> l;
    cout << f(a, l) << " ";
  }
}
Problem p02648 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

n, *inp = map(int, sys.stdin.buffer.read().split())
vvv = [0] + inp[0 : n * 2 : 2]
www = [0] + inp[1 : n * 2 : 2]

weight_limit = 10**5
precalc_limit = min(1 << 9, n + 1)
precalc = [[0] * (weight_limit + 1)]

for u in range(1, precalc_limit):
    v = vvv[u]
    w = www[u]
    dp = precalc[u >> 1][:]
    for x in range(weight_limit, w - 1, -1):
        nv = dp[x - w] + v
        if dp[x] < nv:
            dp[x] = nv
    precalc.append(dp)

buf = []
mp = iter(inp[n * 2 + 1 :])
for u, l in zip(mp, mp):
    if u < precalc_limit:
        buf.append(precalc[u][l])
        continue
    dp_w = [0]
    dp_v = [0]
    while u >= precalc_limit:
        v = vvv[u]
        w = www[u]
        for i in range(len(dp_w)):
            nw = dp_w[i] + w
            if nw > l:
                continue
            nv = dp_v[i] + v
            dp_w.append(nw)
            dp_v.append(nv)
        u >>= 1
    ans = 0
    for w, v in zip(dp_w, dp_v):
        nv = v + precalc[u][l - w]
        if ans < nv:
            ans = nv
    buf.append(ans)

print("\n".join(map(str, buf)))


Fixed submission:
import sys

n, *inp = map(int, sys.stdin.buffer.read().split())
vvv = [0] + inp[0 : n * 2 : 2]
www = [0] + inp[1 : n * 2 : 2]

weight_limit = 10**5
precalc_limit = min(1 << 10, n + 1)
precalc = [[0] * (weight_limit + 1)]

for u in range(1, precalc_limit):
    v = vvv[u]
    w = www[u]
    dp = precalc[u >> 1][:]
    for x in range(weight_limit, w - 1, -1):
        nv = dp[x - w] + v
        if dp[x] < nv:
            dp[x] = nv
    precalc.append(dp)

buf = []
mp = iter(inp[n * 2 + 1 :])
for u, l in zip(mp, mp):
    if u < precalc_limit:
        buf.append(precalc[u][l])
        continue
    dp_w = [0]
    dp_v = [0]
    while u >= precalc_limit:
        v = vvv[u]
        w = www[u]
        for i in range(len(dp_w)):
            nw = dp_w[i] + w
            if nw > l:
                continue
            nv = dp_v[i] + v
            dp_w.append(nw)
            dp_v.append(nv)
        u >>= 1
    ans = 0
    for w, v in zip(dp_w, dp_v):
        nv = v + precalc[u][l - w]
        if ans < nv:
            ans = nv
    buf.append(ans)

print("\n".join(map(str, buf)))
Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int n, q;
int v[(1 << 18) + 1], w[(1 << 18) + 1];
int dp[550][100001];
int inf = -1;
int vq, wq;

int dfs(int i, int weight) {
  // cout << i << " " << weight << " " << dp[i][weight] << endl;
  int res = 0;
  if (i < 550 && weight <= 100000) {
    if (dp[i][weight] != inf)
      return dp[i][weight];
  }
  if (i == 0) {
    res = 0;
  } else if (weight < w[i]) {
    res = dfs(i / 2, weight);
  } else {
    res = max(dfs(i / 2, weight), dfs(i / 2, weight - w[i]) + v[i]);
  }
  if (i < 550 && weight <= 100000)
    dp[i][weight] = res;
  return res;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < 550; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = inf;
    }
  }

  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> vq >> wq;
    cout << dfs(vq, wq) << endl;
  }
  return 0;
}

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

using namespace std;

int n, q;
int v[(1 << 18) + 1], w[(1 << 18) + 1];
int dp[550][100001];
int inf = -1;
int vq, wq;

int dfs(int i, int weight) {
  // cout << i << " " << weight << " " << dp[i][weight] << endl;
  int res = 0;
  if (i < 550 && weight <= 100000) {
    if (dp[i][weight] != inf)
      return dp[i][weight];
  }
  if (i == 0) {
    res = 0;
  } else if (weight < w[i]) {
    res = dfs(i / 2, weight);
  } else {
    res = max(dfs(i / 2, weight), dfs(i / 2, weight - w[i]) + v[i]);
  }
  if (i < 550 && weight <= 100000)
    dp[i][weight] = res;
  return res;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < 550; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = inf;
    }
  }

  for (int j = 0; j < 512; j++) {
    for (int k = 0; k <= 100000; k++) {
      dfs(j, k);
    }
  }

  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> vq >> wq;
    cout << dfs(vq, wq) << endl;
  }
  return 0;
}
Problem p02649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  if ((s | t) != t) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int aa;
    cin >> aa;
    if ((aa & s) == s && (aa | t) == t) {
      a.push_back(aa);
    }
  }
  n = a.size();
  k = min(n, k);
  vector<P> cond;
  for (int i = 0; i < 18; ++i) {
    if (((s >> i) & 1) == 0) {
      cond.push_back({i, 0});
    }
    if (((t >> i) & 1) == 1) {
      cond.push_back({i, 1});
    }
  }
  vector<unordered_map<long long, long long>> dp(k + 1);
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    long long mask = 0;
    for (int j = 0; j < (int)cond.size(); ++j) {
      if (((a[i] >> cond[j].first) & 1) == cond[j].second) {
        mask ^= 1LL << j;
      }
    }
    for (int j = k - 1; j >= 0; --j) {
      for (auto &l : dp[j]) {
        dp[j + 1][l.first | mask] += l.second;
      }
    }
  }
  long long ans = 0;
  for (auto &i : dp) {
    ans += i[(1LL << (int)cond.size()) - 1];
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  if ((s | t) != t) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int aa;
    cin >> aa;
    if ((aa & s) == s && (aa | t) == t) {
      a.push_back(aa);
    }
  }
  n = a.size();
  k = min(n, k);
  vector<P> cond;
  for (int i = 0; i < 18; ++i) {
    if (((s >> i) & 1) == 0) {
      cond.push_back({i, 0});
    }
    if (((t >> i) & 1) == 1) {
      cond.push_back({i, 1});
    }
  }
  vector<unordered_map<long long, long long>> dp(k + 1);
  for (int i = 0; i <= k; ++i) {
    dp[i].reserve(1 << 18);
  }
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    long long mask = 0;
    for (int j = 0; j < (int)cond.size(); ++j) {
      if (((a[i] >> cond[j].first) & 1) == cond[j].second) {
        mask ^= 1LL << j;
      }
    }
    for (int j = k - 1; j >= 0; --j) {
      for (auto &l : dp[j]) {
        dp[j + 1][l.first | mask] += l.second;
      }
    }
  }
  long long ans = 0;
  for (auto &i : dp) {
    ans += i[(1LL << (int)cond.size()) - 1];
  }

  cout << ans << '\n';

  return 0;
}
Problem p02649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define MP make_pair
#define F first
#define S second
typedef long long ll;
using namespace std;
int main() {
  ll n, k, s, t, a[50];
  cin >> n >> k >> s >> t;
  REP(i, n) cin >> a[i];
  vector<map<pair<ll, ll>, ll>> dp(k);
  REP(i, n) {
    for (ll j = k - 2; j + 1; j--) {
      for (auto l = dp[j].begin(); l != dp[j].end(); l++) {
        dp[j + 1][MP((l->F.F) & a[i], (l->F.S) | a[i])] += (l->S);
      }
    }
    dp[0][MP(a[i], a[i])] = 1;
  }
  n = 0;
  REP(i, k) n += (dp[i][MP(s, t)]);
  cout << n;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define MP make_pair
#define F first
#define S second
typedef long long ll;
using namespace std;
int main() {
  ll n, k, s, t, a[50];
  cin >> n >> k >> s >> t;
  REP(i, n) cin >> a[i];
  map<pair<ll, ll>, ll> dp[50];
  REP(i, n) {
    for (ll j = k - 2; j + 1; j--) {
      for (auto l = dp[j].begin(); l != dp[j].end(); l++) {
        dp[j + 1][MP((l->F.F) & a[i], (l->F.S) | a[i])] += (l->S);
      }
    }
    dp[0][MP(a[i], a[i])] = 1;
  }
  n = 0;
  REP(i, k) n += (dp[i][MP(s, t)]);
  cout << n;
}
Problem p02649 (C++)

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

using i64 = int64_t;
template <class T> using Vec = std::vector<T>;
template <class K, class V> using UM = std::unordered_map<K, V>;

using namespace std;

namespace std {
template <> struct hash<pair<int, int>> {
  size_t operator()(pair<int, int> p) const noexcept {
    return (size_t(p.first) << 18) | (size_t(p.second));
  }
};
} // namespace std

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

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  Vec<int> as(n);
  for (auto &a : as)
    cin >> a;

  if ((s & t) != s || (s | t) != t) {
    cout << 0;
    return 0;
  }

  auto invalid = [&](int a) { return s != (s & a) || t != (t | a); };
  as.erase(remove_if(as.begin(), as.end(), invalid), as.end());

  n = as.size();

  Vec<UM<pair<int, int>, i64>> by_andor(k + 19);
  by_andor[0][{t, s}] = 1;

  for (auto &lvl : by_andor)
    lvl.reserve(1 << 14);
  int aand, oor;

  for (int i = 0; i < as.size(); ++i) {
    int a = as[i];

    for (int j = min(i, k - 1); j >= 0; --j) {
      auto &next = by_andor[j + 1];

      for (auto &[key, value] : by_andor[j]) {
        tie(aand, oor) = key;
        next[{aand & a, oor | a}] += value;
      }
    }
  }

  i64 ans = 0;
  for (int i = 0; i <= k; ++i)
    ans += by_andor[i][{s, t}];
  cout << ans << '\n';

  return 0;
}


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

using i64 = int64_t;
template <class T> using Vec = std::vector<T>;
template <class K, class V> using UM = std::unordered_map<K, V>;

using namespace std;

namespace std {
template <> struct hash<pair<int, int>> {
  size_t operator()(pair<int, int> p) const noexcept {
    return (size_t(p.first) << 18) | (size_t(p.second));
  }
};
} // namespace std

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

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  Vec<int> as(n);
  for (auto &a : as)
    cin >> a;

  if ((s & t) != s || (s | t) != t) {
    cout << 0;
    return 0;
  }

  auto invalid = [&](int a) { return s != (s & a) || t != (t | a); };
  as.erase(remove_if(as.begin(), as.end(), invalid), as.end());

  n = as.size();

  Vec<UM<pair<int, int>, i64>> by_andor(k + 19);
  by_andor[0][{t, s}] = 1;

  for (auto &lvl : by_andor)
    lvl.reserve(1 << 18);
  int aand, oor;

  for (int i = 0; i < as.size(); ++i) {
    int a = as[i];

    for (int j = min(i, k - 1); j >= 0; --j) {
      auto &next = by_andor[j + 1];

      for (auto &[key, value] : by_andor[j]) {
        tie(aand, oor) = key;
        next[{aand & a, oor | a}] += value;
      }
    }
  }

  i64 ans = 0;
  for (int i = 0; i <= k; ++i)
    ans += by_andor[i][{s, t}];
  cout << ans << '\n';

  return 0;
}
Problem p02649 (C++)

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

using namespace std;

int main() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;

  vector<map<pair<int, int>, long long>> dp(K + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    for (int k = K - 1; k >= 0; --k) {
      for (auto &p : dp[k]) {
        int prod, sum;
        tie(prod, sum) = p.first;
        dp[k + 1][{prod & a, sum | a}] += p.second;
      }
    }
  }

  long long ans = 0;
  for (int k = 1; k <= K; ++k) {
    ans += dp[k][{S, T}];
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;

  vector<map<pair<int, int>, long long>> dp(K + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if ((a & S) != S)
      continue;
    if ((a | T) != T)
      continue;
    for (int k = K - 1; k >= 0; --k) {
      for (auto &p : dp[k]) {
        int prod, sum;
        tie(prod, sum) = p.first;
        dp[k + 1][{prod & a, sum | a}] += p.second;
      }
    }
  }

  long long ans = 0;
  for (int k = 1; k <= K; ++k) {
    ans += dp[k][{S, T}];
  }
  cout << ans << endl;

  return 0;
}
Problem p02650 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int H, W, K;

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> H >> W >> K;
  ll ret = 0;
  FOR(i, 2) {
    for (int s = 0; s <= H - 2; s++) {
      int R = 2 * K + __gcd(s, W) - 2;
      ll pat = 0;
      for (x = 1; x < W; x++) {
        int R2 = R - s * x;
        if (R2 < 0)
          break;

        int Ymi = R2 / W;
        // if(Ymi) pat+=Ymi-1;
        // for(y=max(1,Ymi);y<=Ymi+2 && y+s<H;y++) {
        for (y = 1; y <= Ymi + 2 && y + s < H; y++) {
          ll v = 1LL * W * y - __gcd(x, y) - __gcd(W - x, y + s);
          if (v <= R2)
            pat++;
        }
      }
      ret += ((s == 0) ? 1 : 2) * pat;
    }
    swap(H, W);
  }
  cout << 2 * ret << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int H, W, K;

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> H >> W >> K;
  ll ret = 0;
  FOR(i, 2) {
    for (int s = 0; s <= H - 2; s++) {
      int R = 2 * K + __gcd(s, W) - 2;
      ll pat = 0;
      for (x = 1; x < W; x++) {
        int R2 = R - s * x;
        if (R2 < 0)
          break;

        int Ymi = min(H - s - 2, R2 / W - 1);
        if (Ymi > 0)
          pat += Ymi;
        for (y = max(1, Ymi + 1); y <= Ymi + 2 && y + s < H; y++) {
          ll v = 1LL * W * y - __gcd(x, y) - __gcd(W - x, y + s);
          if (v <= R2)
            pat++;
        }
      }
      ret += ((s == 0) ? 1 : 2) * pat;
    }
    swap(H, W);
  }
  cout << 2 * ret << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02650 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int gcd(Int x, Int y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

Int inside(Int x1, Int y1, Int x2, Int y2) {
  Int s = abs(x1 * y2 - x2 * y1);
  s += 2;
  s -= gcd(abs(x1), abs(y1));
  s -= gcd(abs(x2), abs(y2));
  s -= gcd(abs(x2 - x1), abs(y2 - y1));
  return s / 2;
}

Int cnt(Int w, Int h, Int k) {
  Int ans = 0;
  for (Int x = 1; x < w; x++) {
    for (Int s = 0; s < h - 1; s++) {
      if (inside(1, x, -s, w) > 2 * k)
        break;
      if (inside(1, x, -s, w) > k)
        continue;
      Int bottom = 1, top = h - s;
      while (top - bottom > 1) {
        Int mid = (top + bottom) / 2;
        if (inside(mid, x, -s, w) <= k)
          bottom = mid;
        else
          top = mid;
      }
      if (s == 0)
        ans += bottom;
      else
        ans += bottom * 2;
    }
  }
  return ans;
}

int main() {
  Int w, h, k;
  cin >> w >> h >> k;
  cout << 2 * cnt(w, h, k) + 2 * cnt(h, w, k) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int gcd(Int x, Int y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

Int inside(Int x1, Int y1, Int x2, Int y2) {
  Int s = abs(x1 * y2 - x2 * y1);
  s += 2;
  s -= gcd(abs(x1), abs(y1));
  s -= gcd(abs(x2), abs(y2));
  s -= gcd(abs(x2 - x1), abs(y2 - y1));
  return s / 2;
}

Int cnt(Int w, Int h, Int k) {
  Int ans = 0;
  for (Int x = 1; x < w; x++) {
    for (Int s = 0; s < h - 1; s++) {
      if (inside(1, x, -s, w) > 2 * k)
        break;
      if (inside(1, x, -s, w) > k)
        continue;
      Int R = 2 * k - x * s - 2 + gcd(w, s);
      Int bottom = max(1LL, R / w), top = min(h - s, R / w + 2);
      bottom = min(bottom, top - 1);
      while (top - bottom > 1) {
        Int mid = (top + bottom) / 2;
        if (inside(mid, x, -s, w) <= k)
          bottom = mid;
        else
          top = mid;
      }
      if (s == 0)
        ans += bottom;
      else
        ans += bottom * 2;
    }
  }
  return ans;
}

int main() {
  Int w, h, k;
  cin >> w >> h >> k;
  cout << 2 * cnt(w, h, k) + 2 * cnt(h, w, k) << endl;
  return 0;
}
Problem p02651 (Python)

Buggy submission (status: Runtime Error):
def solve(n, a_list, s):
    for i in range(n - 1, -1, -1):
        if s[i] == "0":
            r = 2 ** (a_list[i].bit_length() - 1)
            for j in range(i):
                if a_list[j] & r > 0:
                    a_list[j] = a_list[i] ^ a_list[j]
        else:
            if a_list[i] > 0:
                return 1
    return 0


def main():
    t = int(input())
    res_list = []
    for _ in range(t):
        n = int(input())
        a_list = list(map(int, input().split()))
        s = input()
        res = solve(n, a_list, s)
        res_list.append(res)
    for res in res_list:
        print(res)


def test():
    assert solve(2, [1, 2], "10") == 1
    assert solve(2, [1, 1], "10") == 0
    assert solve(6, [2, 3, 4, 5, 6, 7], "111000") == 0


if __name__ == "__main__":
    test()
    main()


Fixed submission:
def solve(n, a_list, s):
    for i in range(n - 1, -1, -1):
        if s[i] == "0":
            if a_list[i] == 0:
                continue
            r = 2 ** (a_list[i].bit_length() - 1)
            for j in range(i):
                if a_list[j] & r > 0:
                    a_list[j] = a_list[i] ^ a_list[j]
        else:
            if a_list[i] > 0:
                return 1
    return 0


def main():
    t = int(input())
    res_list = []
    for _ in range(t):
        n = int(input())
        a_list = list(map(int, input().split()))
        s = input()
        res = solve(n, a_list, s)
        res_list.append(res)
    for res in res_list:
        print(res)


def test():
    assert solve(2, [1, 2], "10") == 1
    assert solve(2, [1, 1], "10") == 0
    assert solve(6, [2, 3, 4, 5, 6, 7], "111000") == 0


if __name__ == "__main__":
    test()
    main()
Problem p02651 (C++)

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

using namespace std;

typedef long long LL;
#define N 120

const LL k = 60;
LL n, a[N], f[80];
char s[N];

void ins(LL x) {
  for (LL i = k; ~i; --i) {
    if (x >> i & 1) {
      if (!f[i]) {
        f[i] = x;
        return;
      } else
        x ^= f[i];
    }
  }
}

bool pd(LL x) {
  for (LL i = k; ~i; --i) {
    if (x >> i & 1) {
      if (!f[i])
        return 0;
      x ^= f[i];
    }
  }
  return 1;
}

void solve() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; ++i)
    scanf("%lld", a + i);
  scanf("%s", s + 1);
  memset(f, 0, sizeof f);
  LL flag = 0;
  for (LL i = n; i; --i) {
    if (s[i] == '0')
      ins(a[i]);
    else if (!pd(a[i])) {
      flag = 1;
      break;
    }
  }
  printf("%lld\n", flag);
}

int main() {
  LL T;
  scanf("%lld", &T);
  while (T--)
    solve();

  return 0;
}


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

using namespace std;

typedef long long LL;
#define N 300

const LL k = 60;
LL n, a[N], f[80];
char s[N];

void ins(LL x) {
  for (LL i = k; ~i; --i) {
    if (x >> i & 1) {
      if (!f[i]) {
        f[i] = x;
        return;
      } else
        x ^= f[i];
    }
  }
}

bool pd(LL x) {
  for (LL i = k; ~i; --i) {
    if (x >> i & 1) {
      if (!f[i])
        return 0;
      x ^= f[i];
    }
  }
  return 1;
}

void solve() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; ++i)
    scanf("%lld", a + i);
  scanf("%s", s + 1);
  memset(f, 0, sizeof f);
  LL flag = 0;
  for (LL i = n; i; --i) {
    if (s[i] == '0')
      ins(a[i]);
    else if (!pd(a[i])) {
      flag = 1;
      break;
    }
  }
  printf("%lld\n", flag);
}

int main() {
  LL T;
  scanf("%lld", &T);
  while (T--)
    solve();

  return 0;
}
Problem p02651 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline T read() {
  T x = 0, f = 0;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
#define int long long
namespace run {
const int N = 109;
namespace linebasis {
int a[69];
inline void insert(int x) {
  for (int i = 63; ~i; i--)
    if ((x >> i) & 1) {
      if (!a[i])
        return a[i] = x, void();
      else
        x ^= a[i];
    }
}
inline bool check(int x) {
  for (int i = 63; ~i; i--)
    if ((x >> i) & 1)
      x ^= a[i];
  return !x;
}
} // namespace linebasis
using namespace linebasis;

int n, A[N];
char s[N];
int main() {
  int T = read<int>();
  while (T--) {
    n = read<int>();
    for (int i = 1; i <= n; i++)
      A[i] = read<int>();
    cin >> s + 1;

    memset(a, 0, sizeof a);
    int peg = 0;
    for (int i = n; i >= 1; i--)
      if (s[i] == '1' && !check(A[i]))
        peg = 1;
      else
        insert(A[i]);

    if (!peg)
      puts("0");
    else
      puts("1");
  }
  return 0;
}
} // namespace run
#undef int
int main() {
#ifdef my
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
  return run::main();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline T read() {
  T x = 0, f = 0;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
#define int long long
namespace run {
const int N = 209;
namespace linebasis {
int a[69];
inline void insert(int x) {
  for (int i = 63; ~i; i--)
    if ((x >> i) & 1) {
      if (!a[i])
        return a[i] = x, void();
      else
        x ^= a[i];
    }
}
inline bool check(int x) {
  for (int i = 63; ~i; i--)
    if ((x >> i) & 1)
      x ^= a[i];
  return !x;
}
} // namespace linebasis
using namespace linebasis;

int n, A[N];
char s[N];
int main() {
  int T = read<int>();
  while (T--) {
    n = read<int>();
    for (int i = 1; i <= n; i++)
      A[i] = read<int>();
    cin >> s + 1;

    memset(a, 0, sizeof a);
    int peg = 0;
    for (int i = n; i >= 1; i--)
      if (s[i] == '1' && !check(A[i]))
        peg = 1;
      else
        insert(A[i]);

    if (!peg)
      puts("0");
    else
      puts("1");
  }
  return 0;
}
} // namespace run
#undef int
int main() {
#ifdef my
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
  return run::main();
}
Problem p02651 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 110, M = 64;

ll a[N];
int n;
char s[N];

ll b[N];

ll insert(ll x) {
  for (int i = 60; ~i; i--)
    if (1 & (x >> i)) {
      if (!b[i]) {
        b[i] = x;
        break;
      } else
        x ^= b[i];
    }
  return x;
}

void Main() {
  for (int i = 0; i < M; i++)
    b[i] = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  scanf("%s", s + 1);
  bool Ans = 0;
  for (int i = n; i; i--) {
    if (s[i] == '0') {
      insert(a[i]);
    } else {
      ll d = insert(a[i]);
      // cerr << d << "??" << endl;
      Ans |= d != 0;
    }
  }
  printf("%d\n", (int)Ans);
}

int main() {
  int T;
  cin >> T;
  while (T--)
    Main();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010, M = 64;

ll a[N];
int n;
char s[N];

ll b[N];

ll insert(ll x) {
  for (int i = 60; ~i; i--)
    if (1 & (x >> i)) {
      if (!b[i]) {
        b[i] = x;
        break;
      } else
        x ^= b[i];
    }
  return x;
}

void Main() {
  for (int i = 0; i < M; i++)
    b[i] = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  scanf("%s", s + 1);
  bool Ans = 0;
  for (int i = n; i; i--) {
    if (s[i] == '0') {
      insert(a[i]);
    } else {
      ll d = insert(a[i]);
      // cerr << d << "??" << endl;
      Ans |= d != 0;
    }
  }
  printf("%d\n", (int)Ans);
}

int main() {
  int T;
  cin >> T;
  while (T--)
    Main();
}
Problem p02651 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 71, B = 61;
int t, n, flg;
ll x, k, a[MAXN], b[MAXN];
char s[MAXN];
bool ins(ll x) {
  for (int i = B; i >= 0; i--) {
    if ((x & (1ll << i))) {
      if (b[i]) {
        x ^= b[i];
      } else {
        b[i] = x;
        break;
      }
    }
  }
  if (x) {
    return 1;
  } else {
    return 0;
  }
}
int main() {
  scanf("%d", &t);
  for (int ii = 1; ii <= t; ii++) {
    memset(b, 0, sizeof(b));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
    }
    scanf("%s", s + 1);
    int flg = 0;
    for (int i = n; i >= 1; i--) {
      if (s[i] == '0') {
        ins(a[i]);
      } else {
        if (ins(a[i])) {
          flg = 1;
          break;
        }
      }
    }
    printf("%d\n", flg);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 271, B = 61;
int t, n, flg;
ll x, k, a[MAXN], b[MAXN];
char s[MAXN];
bool ins(ll x) {
  for (int i = B; i >= 0; i--) {
    if ((x & (1ll << i))) {
      if (b[i]) {
        x ^= b[i];
      } else {
        b[i] = x;
        break;
      }
    }
  }
  if (x) {
    return 1;
  } else {
    return 0;
  }
}
int main() {
  scanf("%d", &t);
  for (int ii = 1; ii <= t; ii++) {
    memset(b, 0, sizeof(b));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
    }
    scanf("%s", s + 1);
    int flg = 0;
    for (int i = n; i >= 1; i--) {
      if (s[i] == '0') {
        ins(a[i]);
      } else {
        if (ins(a[i])) {
          flg = 1;
          break;
        }
      }
    }
    printf("%d\n", flg);
  }
  return 0;
}
Problem p02651 (C++)

Buggy submission (status: Runtime Error):
// A.

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;

bool possible(LL t, vector<LL> a) {
  LL x = 0;
  int st = 0;
  int sz = (int)a.size();
  sort(a.rbegin(), a.rend());
  for (LL b = 60; b >= 0; --b) {
    LL f = 1LL << b;
    if (!(a[st] & f))
      continue;
    for (int i = 0; i < sz; ++i) {
      if (i != st && (a[i] & f) != 0)
        a[i] ^= a[st];
    }
    ++st;
    if (st >= sz)
      break;
    sort(a.rbegin(), a.rbegin() + sz - st);
  }
  for (LL b = 60; b >= 0; --b) {
    LL f = 1LL << b;
    if ((f & t) != 0 && (f & x) == 0) {
      for (int i = 0; i < sz; ++i) {
        if (a[i] & f) {
          x ^= a[i];
          break;
        }
      }
    }
  }
  return t == x;
}

int main(int argc, char *argv[]) {
  LL t, n;
  cin >> t;
  for (LL tt = 0; tt < t; ++tt) {
    cin >> n;
    vector<LL> a(n);
    for (LL i = 0; i < n; ++i) {
      cin >> a[i];
    }
    string s;
    cin >> s;
    int ans = 0;
    vector<LL> x;
    for (LL i = n - 1; i >= 0; --i) {
      if (s[i] != '0') {
        if (!possible(a[i], x)) {
          ans = 1;
          break;
        }
      } else {
        x.emplace_back(a[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
// A.

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;

bool possible(LL t, vector<LL> a) {
  if (a.empty())
    return t == 0;
  LL x = 0;
  int st = 0;
  int sz = (int)a.size();
  sort(a.rbegin(), a.rend());
  for (LL b = 60; b >= 0; --b) {
    LL f = 1LL << b;
    if (!(a[st] & f))
      continue;
    for (int i = 0; i < sz; ++i) {
      if (i != st && (a[i] & f) != 0)
        a[i] ^= a[st];
    }
    ++st;
    if (st >= sz)
      break;
    sort(a.rbegin(), a.rbegin() + sz - st);
  }
  for (LL b = 60; b >= 0; --b) {
    LL f = 1LL << b;
    if ((f & t) != 0 && (f & x) == 0) {
      for (int i = 0; i < sz; ++i) {
        if (a[i] & f) {
          x ^= a[i];
          break;
        }
      }
    }
  }
  return t == x;
}

int main(int argc, char *argv[]) {
  LL t, n;
  cin >> t;
  for (LL tt = 0; tt < t; ++tt) {
    cin >> n;
    vector<LL> a(n);
    for (LL i = 0; i < n; ++i) {
      cin >> a[i];
    }
    string s;
    cin >> s;
    int ans = 0;
    vector<LL> x;
    for (LL i = n - 1; i >= 0; --i) {
      if (s[i] != '0') {
        if (!possible(a[i], x)) {
          ans = 1;
          break;
        }
      } else {
        x.emplace_back(a[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02651 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

void reduce(int arr[64][64], int lead[64], int *d) {
  for (int i = 0; i < *d - 1; i++) {
    if (arr[*d - 1][lead[i]] == 1) {
      for (int k = lead[i]; k < 64; k++) {
        arr[*d - 1][k] = arr[*d - 1][k] ^ arr[i][k];
      }
    }
  }
  int t = 0;
  for (int i = 0; i < 64; i++) {
    if (arr[*d - 1][i] != 0) {
      t = 1;
      lead[*d - 1] = i;
      break;
    }
  }
  if (t == 0) {
    *d = *d - 1;
    return;
  }

  for (int i = *d - 2; i >= 0; i--) {
    if (lead[i + 1] < lead[i]) {
      for (int j = 0; j < 64; j++) {
        t = arr[i][j];
        arr[i][j] = arr[i + 1][j];
        arr[i + 1][j] = t;
      }
      t = lead[i];
      lead[i] = lead[i + 1];
      lead[i + 1] = t;
    } else
      return;
  }
  return;
}

int solve() {
  int n;
  cin >> n;
  long *a = new long[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  string s;
  cin >> s;

  int arr[64][64];
  int lead[64];
  int d = 0;
  int t, temp;
  for (int i = s.length() - 1; i >= 0; i--) {
    t = d;
    temp = a[i];
    for (int j = 63; j >= 0; j--) {
      arr[d][j] = temp % 2;
      temp /= 2;
    }
    d++;
    reduce(arr, lead, &d);

    if (s[i] == '1' && t != d)
      return 1;
  }
  return 0;
}

int main(void) {

  long t;
  cin >> t;
  string ans;

  for (int i = 0; i < t; i++) {
    cout << solve() << "\n";
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

void reduce(int arr[64][64], int lead[64], int *d) {
  for (int i = 0; i < *d - 1; i++) {
    if (arr[*d - 1][lead[i]] == 1) {
      for (int k = lead[i]; k < 64; k++) {
        arr[*d - 1][k] = arr[*d - 1][k] ^ arr[i][k];
      }
    }
  }
  int t = 0;
  for (int i = 0; i < 64; i++) {
    if (arr[*d - 1][i] != 0) {
      t = 1;
      lead[*d - 1] = i;
      break;
    }
  }
  if (t == 0) {
    *d = *d - 1;
    return;
  }

  for (int i = *d - 2; i >= 0; i--) {
    if (lead[i + 1] < lead[i]) {
      for (int j = 0; j < 64; j++) {
        t = arr[i][j];
        arr[i][j] = arr[i + 1][j];
        arr[i + 1][j] = t;
      }
      t = lead[i];
      lead[i] = lead[i + 1];
      lead[i + 1] = t;
    } else
      return;
  }
  return;
}

int solve() {
  int n;
  cin >> n;
  long *a = new long[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  string s;
  cin >> s;

  int arr[64][64];
  int lead[64];
  int d = 0;
  int t;
  long temp;
  for (int i = s.length() - 1; i >= 0; i--) {
    t = d;
    temp = a[i];
    for (int j = 63; j >= 0; j--) {
      arr[d][j] = temp % 2;
      temp /= 2;
    }
    d++;
    reduce(arr, lead, &d);

    if (s[i] == '1' && t != d)
      return 1;
  }
  return 0;
}

int main(void) {

  long t;
  cin >> t;
  string ans;

  for (int i = 0; i < t; i++) {
    cout << solve() << "\n";
  }

  return 0;
}
Problem p02652 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

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

const int N = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;

int mini = 0, maxi = 0, n;
char s[N];
int desno[N];

int probaj(int x) {
  if (desno[0] < x)
    return n;
  int cur = 0, gran = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cur++;
    } else if (s[i] == '0') {
      cur--;
    } else {
      if (cur + desno[i + 1] > x)
        cur--;
      else
        cur++;
    }
    gran = max(cur, gran);
  }
  return gran - x;
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  desno[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1' || s[i] == '?') {
      desno[i] = min(desno[i + 1] + 1, 0);
    } else {
      desno[i] = desno[i + 1] - 1;
    }
  }
  if (n <= 1e4) {
    int bst = n, gdje = 0;
    for (int i = 0; 2 * i <= n; i++)
      bst = min(bst, probaj(-i));
    printf("%d\n", bst);
  } else {
    int bst = n, gdje = 0;
    int lo = 0, hi = n;
    while (lo + 100 > hi) {
      int mid1 = (2 * lo + hi) / 3;
      int mid2 = (lo + 2 * hi) / 3;
      int s1 = probaj(-mid1), s2 = probaj(-mid2);
      bst = min(bst, min(s1, s2));
      if (s1 == s2) {
        lo = mid1, hi = mid2;
      } else if (s1 < s2) {
        hi = mid2;
      } else {
        lo = mid1;
      }
    }
    for (int i = lo; i <= hi; i++)
      bst = min(bst, probaj(-i));
    printf("%d\n", bst);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

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

const int N = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;

int mini = 0, maxi = 0, n;
char s[N];
int desno[N];

int probaj(int x) {
  if (desno[0] < x)
    return n;
  int cur = 0, gran = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cur++;
    } else if (s[i] == '0') {
      cur--;
    } else {
      if (cur + desno[i + 1] > x)
        cur--;
      else
        cur++;
    }
    gran = max(cur, gran);
  }
  return gran - x;
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  desno[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1' || s[i] == '?') {
      desno[i] = min(desno[i + 1] + 1, 0);
    } else {
      desno[i] = desno[i + 1] - 1;
    }
  }
  int sol = n;
  for (int x = desno[0]; x >= desno[0] - 50; x--)
    sol = min(sol, probaj(x));
  printf("%d\n", sol);
  return 0;
}
Problem p02652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}
char buf[100010];
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cin >> buf;
  int a0 = 0, a1 = 1, b0 = 0, b1 = -1;
  int c0 = a0 - b0, c1 = a0 - b1, c2 = a1 - b0, c3 = a1 - b1;
  for (char *p = buf; *p; p++) {
    if (*p == '0') {
      --a0;
      --a1;
      --b0;
      --b1;
    } else if (*p == '1') {
      ++a0;
      ++a1;
      ++b0;
      ++b1;
    } else {
      --a0;
      --a1;
      ++b0;
      ++b1;
    }
    if (a0 < 0)
      a0 += 2;
    if (a1 < 0)
      a1 += 2;
    if (b0 > 0)
      b0 -= 2;
    if (b1 > 0)
      b1 -= 2;
    c0 = std::max(c0, a0 - b0);
    c1 = std::max(c1, a0 - b1);
    c2 = std::max(c2, a1 - b0);
    c3 = std::max(c3, a1 - b1);
  }
  fin(std::min(std::min(c0, c1), std::min(c2, c3)));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}
char buf[1000010];
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cin >> buf;
  int a0 = 0, a1 = 1, b0 = 0, b1 = -1;
  int c0 = a0 - b0, c1 = a0 - b1, c2 = a1 - b0, c3 = a1 - b1;
  for (char *p = buf; *p; p++) {
    if (*p == '0') {
      --a0;
      --a1;
      --b0;
      --b1;
    } else if (*p == '1') {
      ++a0;
      ++a1;
      ++b0;
      ++b1;
    } else {
      --a0;
      --a1;
      ++b0;
      ++b1;
    }
    if (a0 < 0)
      a0 += 2;
    if (a1 < 0)
      a1 += 2;
    if (b0 > 0)
      b0 -= 2;
    if (b1 > 0)
      b1 -= 2;
    c0 = std::max(c0, a0 - b0);
    c1 = std::max(c1, a0 - b1);
    c2 = std::max(c2, a1 - b0);
    c3 = std::max(c3, a1 - b1);
  }
  fin(std::min(std::min(c0, c1), std::min(c2, c3)));
  return 0;
}
Problem p02652 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 100010
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int mx, n, a[N], b[N];
char s[N];
void init() {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (s[i] == '1' ? 1 : -1);
    mx = max(mx, sum);
    a[i] = sum;
  }
  b[n] = a[n];
  for (int i = n - 1; i >= 0; --i) {
    b[i] = max(a[i], b[i + 1]);
  }
}
int calc(int lim) {
  int mn = 0, sum = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1')
      ++sum;
    else if (s[i] == '0')
      --sum;
    else {
      if (b[i] - a[i - 1] + sum + 2 <= lim)
        ++sum;
      else
        --sum;
    }
    mn = min(mn, sum);
  }
  return mn;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  init();
  printf("%d\n", min(mx - calc(mx), mx + 1 - calc(mx + 1)));
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 1000010
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int mx, n, a[N], b[N];
char s[N];
void init() {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (s[i] == '1' ? 1 : -1);
    mx = max(mx, sum);
    a[i] = sum;
  }
  b[n] = a[n];
  for (int i = n - 1; i >= 0; --i) {
    b[i] = max(a[i], b[i + 1]);
  }
}
int calc(int lim) {
  int mn = 0, sum = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1')
      ++sum;
    else if (s[i] == '0')
      --sum;
    else {
      if (b[i] - a[i - 1] + sum + 2 <= lim)
        ++sum;
      else
        --sum;
    }
    mn = min(mn, sum);
  }
  return mn;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  init();
  printf("%d\n", min(mx - calc(mx), mx + 1 - calc(mx + 1)));
  return 0;
}
Problem p02652 (C++)

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

const int N = 1e6 + 5;

int n;
char s[N];
int sum[N], pmx[N], pmi[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum[0] = 0;
  pmx[0] = 0;
  pmi[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + 1 - 2 * (s[i] == '0');
    pmx[i] = max(pmx[i - 1], sum[i]);
    pmi[i] = min(pmi[i - 1], sum[i]);
  }
  int suf_mi = sum[n], suf_mx = sum[n];
  int ans = pmx[n] - pmi[n];
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] < suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
      }
      if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  suf_mi = sum[n], suf_mx = sum[n];
  bool found = false;
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] + 1 == suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
        if (!found) {
          suf_mi -= 2;
          suf_mx -= 2;
          found = true;
        }
      } else if (pmx[i - 1] + 1 < suf_mx) {
        assert(found);
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


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

const int N = 1e6 + 5;

int n;
char s[N];
int sum[N], pmx[N], pmi[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum[0] = 0;
  pmx[0] = 0;
  pmi[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + 1 - 2 * (s[i] == '0');
    pmx[i] = max(pmx[i - 1], sum[i]);
    pmi[i] = min(pmi[i - 1], sum[i]);
  }
  int suf_mi = sum[n], suf_mx = sum[n];
  int ans = pmx[n] - pmi[n];
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] < suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
      }
      if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  suf_mi = sum[n], suf_mx = sum[n];
  bool found = false;
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] + 1 == suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
        if (!found) {
          suf_mi -= 2;
          suf_mx -= 2;
          found = true;
        }
      } else if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int dp0[5005][5005], dp1[5005][5005], dp2[5005][5005], sum1[5005][5005];
int main() {
  int n, a, b;
  scanf("%d%d%d", &n, &a, &b);
  if (a > b)
    swap(a, b);
  dp1[0][0] = 1;
  for (int j = 0; j <= n; j++)
    sum1[0][j] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= a - 1; j++)
      dp0[i][0] = (dp0[i][0] + sum1[i - j][n]) % mod;
    for (int j = 0; j <= b - a - 1; j++)
      dp2[i][j + a] = (dp2[i][j + a] + dp1[i - a][j]) % mod;

    for (int j = 0; j < b - 1; j++) {
      dp2[i][j + 1] = (dp2[i][j + 1] + dp2[i - 1][j]) % mod;
    }
    for (int j = 0; j < b - 1; j++) {
      dp1[i][j + 1] = (dp1[i][j + 1] + dp0[i - 1][j]) % mod;
      dp1[i][j + 1] = (dp1[i][j + 1] + dp1[i - 1][j]) % mod;
      dp1[i][j + 1] = (dp1[i][j + 1] + dp2[i - 1][j]) % mod;
    }

    for (int j = 0; j <= n; j++) {
      if (j == 0)
        sum1[i][j] = dp1[i][j];
      else
        sum1[i][j] = (sum1[i][j - 1] + dp1[i][j]) % mod;
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    ans = ans * 2 % mod;
  for (int j = 0; j <= n; j++) {
    ans = (ans - dp0[n][j] + mod) % mod;
    ans = (ans - dp1[n][j] + mod) % mod;
    ans = (ans - dp2[n][j] + mod) % mod;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int dp0[5005][5005], dp1[5005][5005], dp2[5005][5005], sum1[5005][5005];
int main() {
  int n, a, b;
  scanf("%d%d%d", &n, &a, &b);
  if (a > b)
    swap(a, b);
  dp1[0][0] = 1;
  for (int j = 0; j <= n; j++)
    sum1[0][j] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= a - 1; j++)
      if (i - j >= 0)
        dp0[i][0] = (dp0[i][0] + sum1[i - j][n]) % mod;
    if (i >= a)
      for (int j = 0; j <= b - a - 1; j++)
        dp2[i][j + a] = (dp2[i][j + a] + dp1[i - a][j]) % mod;

    for (int j = 0; j < b - 1; j++) {
      dp2[i][j + 1] = (dp2[i][j + 1] + dp2[i - 1][j]) % mod;
    }
    for (int j = 0; j < b - 1; j++) {
      dp1[i][j + 1] = (dp1[i][j + 1] + dp0[i - 1][j]) % mod;
      dp1[i][j + 1] = (dp1[i][j + 1] + dp1[i - 1][j]) % mod;
      dp1[i][j + 1] = (dp1[i][j + 1] + dp2[i - 1][j]) % mod;
    }

    for (int j = 0; j <= n; j++) {
      if (j == 0)
        sum1[i][j] = dp1[i][j];
      else
        sum1[i][j] = (sum1[i][j - 1] + dp1[i][j]) % mod;
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    ans = ans * 2 % mod;
  for (int j = 0; j <= n; j++) {
    ans = (ans - dp0[n][j] + mod) % mod;
    ans = (ans - dp1[n][j] + mod) % mod;
    ans = (ans - dp2[n][j] + mod) % mod;
  }
  printf("%d\n", ans);
}