Problem p03782 (C++)

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

#define len(x) (int((x).size()))
#define append push_back
#define pp make_pair
#define ff(a, b) for (int a = 0; a < int(b); ++a)
#define kk(n) ff(k, n)
#define xx(n) ff(x, n)
#define yy(n) ff(y, n)
#define ii(n) ff(i, n)
#define fff(a, b, c) for (int a = int(b); a < int(c); ++a)
#define kkk(a, b) fff(k, a, b)
#define xxx(a, b) fff(x, a, b)
#define yyy(a, b) fff(y, a, b)
#define iii(a, b) fff(i, a, b)
#define bb begin()
#define ee end()
#define uu first
#define vv second
#define all(x) (x).bb, (x).ee
#define ite(v) decltype((v).bb)
#define fe(i, v) for (ite(v) i = (v).bb; i != (v).ee; ++i)
#define err(...)                                                               \
  {                                                                            \
    fprintf(stderr, __VA_ARGS__);                                              \
    fflush(stderr);                                                            \
  }
#define zz(array, byte) memset(array, byte, sizeof(array));

using LL = long long;
using DD = long double;
using pii = pair<int, int>;

const LL INFLL = 0x7f7f7f7f7f7f7f7fLL;
const int INFint = 0x7f7f7f7f; // Works with memset(..).

int N, K;
vector<int> aa;
int rr = 0;

void backtrack(int level, int base, vector<int> &previous) {
  int skip = (1 << level);
  int otherbase = base + (1 << (level - 1));
  if (otherbase >= len(aa)) {
    // One left.
    int remaining = aa[base];
    // printf("Remainging %d\n", remaining);
    iii(K - remaining, K) {
      if (previous[i]) {
        // printf("Got on %d\n", i);
        ++rr;
        return;
      }
    }
    return;
  }
  ii(2) {
    if (1 == i)
      swap(base, otherbase);
    vector<int> here(all(previous));
    for (int k = base; k < len(aa); k += skip) {
      int a = aa[k];
      for (int x = K - a; x >= 0; --x) {
        if (here[x])
          here[x + a] = 1;
      }
    }
    backtrack(level + 1, otherbase, here);
  }
}

int main() {
  // ios_base::sync_with_stdio(false);     cin.tie(NULL);
  // cout << '\n';     //Avoid flushing with endl.
  cout.precision(16);

  cin >> N >> K;

  ii(N) {
    int a;
    cin >> a;
    if (a >= K)
      continue;
    aa.append(a);
  }

  N = len(aa);

  vector<int> have(K + 1, 0);
  have[0] = 1;
  backtrack(1, 0, have);

  cout << len(aa) - rr << '\n';

  return 0;
}


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

#define len(x) (int((x).size()))
#define append push_back
#define pp make_pair
#define ff(a, b) for (int a = 0; a < int(b); ++a)
#define kk(n) ff(k, n)
#define xx(n) ff(x, n)
#define yy(n) ff(y, n)
#define ii(n) ff(i, n)
#define fff(a, b, c) for (int a = int(b); a < int(c); ++a)
#define kkk(a, b) fff(k, a, b)
#define xxx(a, b) fff(x, a, b)
#define yyy(a, b) fff(y, a, b)
#define iii(a, b) fff(i, a, b)
#define bb begin()
#define ee end()
#define uu first
#define vv second
#define all(x) (x).bb, (x).ee
#define ite(v) decltype((v).bb)
#define fe(i, v) for (ite(v) i = (v).bb; i != (v).ee; ++i)
#define err(...)                                                               \
  {                                                                            \
    fprintf(stderr, __VA_ARGS__);                                              \
    fflush(stderr);                                                            \
  }
#define zz(array, byte) memset(array, byte, sizeof(array));

using LL = long long;
using DD = long double;
using pii = pair<int, int>;

const LL INFLL = 0x7f7f7f7f7f7f7f7fLL;
const int INFint = 0x7f7f7f7f; // Works with memset(..).

int N, K;
vector<int> aa;
int rr = 0;

void backtrack(int level, int base, vector<int> &previous) {
  int skip = (1 << level);
  int otherbase = base + (1 << (level - 1));
  if (otherbase >= len(aa)) {
    // One left.
    int remaining = aa[base];
    // printf("Remainging %d\n", remaining);
    iii(K - remaining, K) {
      if (previous[i]) {
        // printf("Got on %d\n", i);
        ++rr;
        return;
      }
    }
    return;
  }
  ii(2) {
    if (1 == i)
      swap(base, otherbase);
    vector<int> here(all(previous));
    for (int k = base; k < len(aa); k += skip) {
      int a = aa[k];
      for (int x = K - a; x >= 0; --x) {
        if (here[x])
          here[x + a] = 1;
      }
    }
    backtrack(level + 1, otherbase, here);
  }
}

int main() {
  // ios_base::sync_with_stdio(false);     cin.tie(NULL);
  // cout << '\n';     //Avoid flushing with endl.
  cout.precision(16);

  cin >> N >> K;

  ii(N) {
    int a;
    cin >> a;
    if (a >= K)
      continue;
    aa.append(a);
  }

  N = len(aa);

  if (N == 0) {
    cout << "0\n";
    return 0;
  }

  vector<int> have(K + 1, 0);
  have[0] = 1;
  backtrack(1, 0, have);

  cout << len(aa) - rr << '\n';

  return 0;
}