Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (i = 0; i < n; i++)
#define FORAB(i, a, b) for (i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))

typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<PII> VP;

#define AIN(A, B, C) assert(IN(A, B, C))

// typedef int LL;
typedef long long int LL;
// typedef __int64 LL;

int n, k;
LL a[5003];
LL pref[5003][5003], suff[5003][5003];

int main() {
  scanf("%d %d", &n, &k);
  LL sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] = MIN(k, a[i]);
    sum += a[i];
  }

  if (sum < k) {
    printf("%d\n", n);
    return 0;
  }

  pref[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= k; j++)
      pref[i][j] = pref[i - 1][j];
    for (int j = k - a[i]; j >= 0; j--)
      pref[i][j + a[i]] |= pref[i][j];
  }

  suff[n + 1][0] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j <= k; j++)
      suff[i][j] = suff[i + 1][j];
    for (int j = k - a[i]; j >= 0; j--)
      suff[i][j + a[i]] |= suff[i][j];
  }

  for (int i = 0; i <= n + 1; i++) {
    // for (int j = 1; j <= k + 1; j++) pref[i][j] += pref[i][j - 1];
    for (int j = 1; j <= k + 1; j++)
      suff[i][j] += suff[i][j - 1];
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    // pref[i - 1][...], suff[i + 1][...]
    if (a[i] >= k)
      continue;
    int flag = 0;
    for (int j = 0; j < k; j++) {
      if (pref[i - 1][j] == 0)
        continue;
      int t = suff[i + 1][k - 1 - j] -
              (k - a[i] - j > 0 ? suff[i + 1][k - a[i] - 1 - j] : 0);
      if (t > 0) {
        flag = 1;
      }
    }
    if (!flag)
      cnt++;
  }

  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (i = 0; i < n; i++)
#define FORAB(i, a, b) for (i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))

typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<PII> VP;

#define AIN(A, B, C) assert(IN(A, B, C))

// typedef int LL;
typedef long long int LL;
// typedef __int64 LL;

int n, k;
LL a[5003];
int pref[5003][5003], suff[5003][5003];

int main() {
  scanf("%d %d", &n, &k);
  LL sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] = MIN(k, a[i]);
    sum += a[i];
  }

  if (sum < k) {
    printf("%d\n", n);
    return 0;
  }

  pref[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= k; j++)
      pref[i][j] = pref[i - 1][j];
    for (int j = k - a[i]; j >= 0; j--)
      pref[i][j + a[i]] |= pref[i][j];
  }

  suff[n + 1][0] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j <= k; j++)
      suff[i][j] = suff[i + 1][j];
    for (int j = k - a[i]; j >= 0; j--)
      suff[i][j + a[i]] |= suff[i][j];
  }

  for (int i = 0; i <= n + 1; i++) {
    // for (int j = 1; j <= k + 1; j++) pref[i][j] += pref[i][j - 1];
    for (int j = 1; j <= k + 1; j++)
      suff[i][j] += suff[i][j - 1];
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    // pref[i - 1][...], suff[i + 1][...]
    if (a[i] >= k)
      continue;
    int flag = 0;
    for (int j = 0; j < k; j++) {
      if (pref[i - 1][j] == 0)
        continue;
      int t = suff[i + 1][k - 1 - j] -
              (k - a[i] - j > 0 ? suff[i + 1][k - a[i] - 1 - j] : 0);
      if (t > 0) {
        flag = 1;
      }
    }
    if (!flag)
      cnt++;
  }

  printf("%d\n", cnt);
  return 0;
}