Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long u64;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<u64> a;
  int flg[n];
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    u64 ai;
    cin >> ai;
    if (ai >= k) {
      continue;
    }
    a.push_back(ai);
    flg[cnt] = 0;
    cnt++;
  }
  n = a.size();
  sort(a.begin(), a.end());
  int tgt = n / 2, bottom = 0, top = n - 1;
  while (true) {
    if ((tgt < n - 1 && flg[tgt] == -1 && flg[tgt + 1] == 1) ||
        (tgt == n - 1 && flg[tgt] == -1) || (tgt == 0 && flg[tgt] == 1)) {
      break;
    }
    if (flg[tgt] != 0) {
      if (tgt == n - 1 || flg[tgt + 1] != 0) {
        tgt--;
        continue;
      } else {
        tgt++;
        continue;
      }
    }
    if (flg[tgt] == 0) {
      bool m[k];
      for (int i = 0; i < k; ++i) {
        m[i] = false;
      }
      m[0] = true;
      for (int i = 0; i < n; ++i) {
        if (i == tgt) {
          continue;
        }
        for (int j = k - a[i] - 1; j >= 0; --j) {
          if (m[j]) {
            m[j + a[i]] = true;
          }
        }
      }
      bool ok = false;
      for (int i = k - a[tgt]; i < k; ++i) {
        if (m[i]) {
          ok = true;
          break;
        }
      }
      if (ok) {
        flg[tgt] = 1;
        tgt = (bottom + tgt) / 2;
        top = tgt;
      } else {
        flg[tgt] = -1;
        tgt = (top + tgt) / 2;
        bottom = tgt;
      }
    }
  }
  if (flg[0] == 1) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < n - 1; ++i) {
      if (flg[i] == -1 && flg[i + 1] == 1) {
        cout << i + 1 << endl;
        return 0;
      }
    }
    cout << n << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long u64;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<u64> a;
  int flg[n];
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    u64 ai;
    cin >> ai;
    if (ai >= k) {
      continue;
    }
    a.push_back(ai);
    flg[cnt] = 0;
    cnt++;
  }
  n = a.size();
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  int tgt = n / 2, bottom = 0, top = n - 1;
  while (true) {
    if ((tgt < n - 1 && flg[tgt] == -1 && flg[tgt + 1] == 1) ||
        (tgt == n - 1 && flg[tgt] == -1) || (tgt == 0 && flg[tgt] == 1)) {
      break;
    }
    if (flg[tgt] != 0) {
      if (tgt == n - 1 || flg[tgt + 1] != 0) {
        tgt--;
        continue;
      } else {
        tgt++;
        continue;
      }
    }
    if (flg[tgt] == 0) {
      bool m[k];
      for (int i = 0; i < k; ++i) {
        m[i] = false;
      }
      m[0] = true;
      for (int i = 0; i < n; ++i) {
        if (i == tgt) {
          continue;
        }
        for (int j = k - a[i] - 1; j >= 0; --j) {
          if (m[j]) {
            m[j + a[i]] = true;
          }
        }
      }
      bool ok = false;
      for (int i = k - a[tgt]; i < k; ++i) {
        if (m[i]) {
          ok = true;
          break;
        }
      }
      if (ok) {
        flg[tgt] = 1;
        tgt = (bottom + tgt) / 2;
        top = tgt;
      } else {
        flg[tgt] = -1;
        tgt = (top + tgt) / 2;
        bottom = tgt;
      }
    }
  }
  if (flg[0] == 1) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < n - 1; ++i) {
      if (flg[i] == -1 && flg[i + 1] == 1) {
        cout << i + 1 << endl;
        return 0;
      }
    }
    cout << n << endl;
  }
  return 0;
}