Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed << setprecision(x)
#define pii pair<int, int>
#define vint vector<int>
#define pb push_back
using namespace std;
void Freopen() {
  freopen(title ".in", "r", stdin);
  freopen(title ".out", "w", stdout);
}
int read() {
  int g = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    g = g * 10 + ch - '0';
    ch = getchar();
  }
  return g * f;
}
const int N = 20;
int n, m, hav[N][2 << N], s[2 << N], ne[2 << N][2], ans;
signed main() {
  n = read(), m = read();
  for (int i = 0; i <= n; i++)
    for (int x = 0; x < 1 << i; x++)
      scanf("%1d", &hav[i][1 << i ^ x]);
  s[0] = -1;
  for (int i = 1; i < 2 << n; i++)
    s[i] = s[i >> 1] + 1;
  for (int i = 2; i < 2 << n; i++) {
    if (i >> (s[i] - 1) & 1)
      ne[i][1] = i ^ 1 << s[i], ne[i][0] = ne[ne[i][1]][0];
    else
      ne[i][0] = i ^ 3 << (s[i] - 1), ne[i][1] = ne[ne[i][0]][1];
  }
  for (int i = n; i >= 1; i--)
    for (int sta = 1 << i; sta < 2 << n; sta++) {
      if (!hav[i][sta])
        continue;
      int re = sta >> i, to = (sta & ((1 << i) - 1)) ^ 1 << i;
      if (ne[to][0])
        hav[s[ne[to][0]]][(re << 1 ^ 1) << s[ne[to][0]] ^ ne[to][0]] +=
            hav[i][sta];
      if (ne[to][1])
        hav[s[ne[to][1]]][(re << 1 ^ 0) << s[ne[to][1]] ^ ne[to][1]] +=
            hav[i][sta];
      hav[0][re] += hav[i][sta];
    }
  ans = 1;
  for (int sta = 2; sta < 2 << n; sta++)
    if (hav[0][sta] >= m && s[sta] > s[ans])
      ans = sta;
  int j = n;
  while (~ans >> j & 1)
    j--;
  while (j--)
    cout << (ans >> j & 1);
  return signed();
}

Fixed submission:
#include <bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed << setprecision(x)
#define pii pair<int, int>
#define vint vector<int>
#define pb push_back
using namespace std;
void Freopen() {
  freopen(title ".in", "r", stdin);
  freopen(title ".out", "w", stdout);
}
int read() {
  int g = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    g = g * 10 + ch - '0';
    ch = getchar();
  }
  return g * f;
}
const int N = 20;
int n, m, hav[N + 5][2 << N], s[2 << N], ne[2 << N][2], ans;
signed main() {
  n = read(), m = read();
  for (int i = 0; i <= n; i++)
    for (int x = 0; x < 1 << i; x++)
      scanf("%1d", &hav[i][1 << i ^ x]);
  s[0] = -1;
  for (int i = 1; i < 2 << n; i++)
    s[i] = s[i >> 1] + 1;
  for (int i = 2; i < 2 << n; i++) {
    if (i >> (s[i] - 1) & 1)
      ne[i][1] = i ^ 1 << s[i], ne[i][0] = ne[ne[i][1]][0];
    else
      ne[i][0] = i ^ 3 << (s[i] - 1), ne[i][1] = ne[ne[i][0]][1];
  }
  for (int i = n; i >= 1; i--)
    for (int sta = 1 << i; sta < 2 << n; sta++) {
      if (!hav[i][sta])
        continue;
      int re = sta >> i, to = (sta & ((1 << i) - 1)) ^ 1 << i;
      if (ne[to][0])
        hav[s[ne[to][0]]][(re << 1 ^ 1) << s[ne[to][0]] ^ ne[to][0]] +=
            hav[i][sta];
      if (ne[to][1])
        hav[s[ne[to][1]]][(re << 1 ^ 0) << s[ne[to][1]] ^ ne[to][1]] +=
            hav[i][sta];
      hav[0][re] += hav[i][sta];
    }
  ans = 1;
  for (int sta = 2; sta < 2 << n; sta++)
    if (hav[0][sta] >= m && s[sta] > s[ans])
      ans = sta;
  int j = n;
  while (~ans >> j & 1)
    j--;
  while (j--)
    cout << (ans >> j & 1);
  return signed();
}