Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define LL long long
#define INF (2147483647)
#define sqr(x) ((x) * (x))

using namespace std;

inline void read(int &data) {
  data = 0;
  int w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = data * 10 + ch - '0', ch = getchar();
  data = data * w;
}

inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void read(LL &data) {
  data = 0;
  int w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = data * 10 + ch - '0', ch = getchar();
  data = data * w;
}

inline void write(LL x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline bool cmp(const int a, const int b) { return a < b; }

int n, k;
bool f[5005];
int a[5005];

bool check(int p) {
  memset(f, false, sizeof(f));
  f[0] = true;
  rep(i, 1, n) {
    if (i == p)
      continue;
    per(j, k, a[i]) if (f[j - a[i]]) f[j] = true;
  }
  rep(i, k - a[p], k - 1) if (f[i]) return true;
  return false;
}

int main() {
  read(n), read(k);
  rep(i, 1, n) read(a[i]);
  sort(a + 1, a + n + 1, cmp);

  int l = 1, r = n, ans = n + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }

  write(ans - 1);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define LL long long
#define INF (2147483647)
#define sqr(x) ((x) * (x))

using namespace std;

inline void read(int &data) {
  data = 0;
  int w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = data * 10 + ch - '0', ch = getchar();
  data = data * w;
}

inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void read(LL &data) {
  data = 0;
  int w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = data * 10 + ch - '0', ch = getchar();
  data = data * w;
}

inline void write(LL x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline bool cmp(const int a, const int b) { return a < b; }

int n, k;
bool f[5005];
int a[5005];

bool check(int p) {
  memset(f, false, sizeof(f));
  f[0] = true;
  rep(i, 1, n) {
    if (i == p)
      continue;
    per(j, k, a[i]) if (f[j - a[i]]) f[j] = true;
  }
  rep(i, max(k - a[p], 0), k - 1) if (f[i]) return true;
  return false;
}

int main() {
  read(n), read(k);
  rep(i, 1, n) read(a[i]);
  sort(a + 1, a + n + 1, cmp);

  int l = 1, r = n, ans = n + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }

  write(ans - 1);
}