Problem p03350 (C++)

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

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
const int MOD = 1000000007;
inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
inline int mul(int x, int y) { return 1LL * x * y % MOD; }
inline int qpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      ans = mul(ans, x);
  return ans;
}
}; // namespace modular

namespace Base {
template <typename Tp> inline Tp input() {
  Tp x = 0, y = 1;
  char c = getchar();
  while ((c < '0' || '9' < c) && c != EOF) {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  if (c == EOF)
    return 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x *= y;
}
template <typename Tp> inline void read(Tp &x) { x = input<Tp>(); }
template <typename Tp> inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; }
template <typename Tp> inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; }
}; // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 22

int N, K;
int f[1 << MAX_N][MAX_N], pw[MAX_N];
char X[MAX_N];

int main() {
  pw[0] = 1;
  Rep(i, 21) pw[i] = pw[i - 1] << 1;
  read(N), read(K);
  rep(i, 0, N) {
    int len = 1 << i;
    scanf("%s", X);
    lep(j, 0, len) f[pw[i] | j][i] = X[j] - '0';
  }
  int val;
  irep(i, N, 0) {
    lep(s, 0, pw[i]) {
      irep(j, i, 0) if ((val = f[pw[i] | s][j])) {
        int s1 = s >> j, s2 = s & (pw[j] - 1);
        if (j)
          f[pw[i - j] | s1][0] += val;
        if (s2) {
          int k = j - 1;
          while (!(s2 >> k & 1))
            k--;
          f[pw[i - (j - k - 1)] | (s1 << k + 1) | pw[k] | (s2 & (pw[k] - 1))]
           [k] += val;
        }
        if ((~s2) & (pw[j] - 1)) {
          int k = j - 1;
          while (s2 >> k & 1)
            k--;
          f[pw[i - (j - k - 1)] | (s1 << k + 1) | (s2 & (pw[k] - 1))][k] += val;
        }
      }
    }
  }
  irep(i, N, 0) {
    lep(s, 0, pw[i]) if (f[pw[i] | s][0] >= K) {
      irep(j, i - 1, 0) printf("%d", s >> j & 1);
      puts("");
      return 0;
    }
  }
  return 0;
}


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

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
const int MOD = 1000000007;
inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
inline int mul(int x, int y) { return 1LL * x * y % MOD; }
inline int qpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      ans = mul(ans, x);
  return ans;
}
}; // namespace modular

namespace Base {
template <typename Tp> inline Tp input() {
  Tp x = 0, y = 1;
  char c = getchar();
  while ((c < '0' || '9' < c) && c != EOF) {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  if (c == EOF)
    return 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x *= y;
}
template <typename Tp> inline void read(Tp &x) { x = input<Tp>(); }
template <typename Tp> inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; }
template <typename Tp> inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; }
}; // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 22

int N, K;
int f[1 << MAX_N][MAX_N], pw[MAX_N];
char X[1 << MAX_N];

int main() {
  pw[0] = 1;
  Rep(i, 21) pw[i] = pw[i - 1] << 1;
  read(N), read(K);
  rep(i, 0, N) {
    int len = 1 << i;
    scanf("%s", X);
    lep(j, 0, len) f[pw[i] | j][i] = X[j] - '0';
  }
  int val;
  irep(i, N, 0) {
    lep(s, 0, pw[i]) {
      irep(j, i, 0) if ((val = f[pw[i] | s][j])) {
        int s1 = s >> j, s2 = s & (pw[j] - 1);
        if (j)
          f[pw[i - j] | s1][0] += val;
        if (s2) {
          int k = j - 1;
          while (!(s2 >> k & 1))
            k--;
          f[pw[i - (j - k - 1)] | (s1 << k + 1) | pw[k] | (s2 & (pw[k] - 1))]
           [k] += val;
        }
        if ((~s2) & (pw[j] - 1)) {
          int k = j - 1;
          while (s2 >> k & 1)
            k--;
          f[pw[i - (j - k - 1)] | (s1 << k + 1) | (s2 & (pw[k] - 1))][k] += val;
        }
      }
    }
  }
  irep(i, N, 0) {
    lep(s, 0, pw[i]) if (f[pw[i] | s][0] >= K) {
      irep(j, i - 1, 0) printf("%d", s >> j & 1);
      puts("");
      return 0;
    }
  }
  return 0;
}