Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
int a[5010];
bool can[5010];
int main(void) {
  can[0] = 1;
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  sort(&a[1], &a[n + 1]);
  int l = 0, r = n + 1;
  while (l + 1 < r) {
    int md = (l + r) >> 1;
    memset(can, 0, sizeof(can));
    can[0] = 1;
    bool f = 0;
    for (int i = 1; i <= n; i++) {
      if (i == md)
        continue;
      for (int j = k - 1; ~j; j--) {
        if (!can[j])
          continue;
        if (j + a[i] < k)
          can[j + a[i]] = 1;
      }
    }
    for (int i = k - 1; i >= k - a[md]; i--) {
      f |= can[i];
    }
    if (f) {
      r = md;
    } else {
      l = md;
    }
  }
  printf("%d", r - 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
int a[5010];
bool can[5010];
int main(void) {
  can[0] = 1;
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  sort(&a[1], &a[n + 1]);
  int l = 0, r = n + 1;
  while (l + 1 < r) {
    int md = (l + r) >> 1;
    memset(can, 0, sizeof(can));
    can[0] = 1;
    bool f = 0;
    for (int i = 1; i <= n; i++) {
      if (i == md)
        continue;
      for (int j = k - 1; ~j; j--) {
        if (!can[j])
          continue;
        if (j + a[i] < k)
          can[j + a[i]] = 1;
      }
    }
    for (int i = k - 1; i >= max(k - a[md], 0); i--) {
      f |= can[i];
    }
    if (f) {
      r = md;
    } else {
      l = md;
    }
  }
  printf("%d", r - 1);
  return 0;
}