Problem p01450 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int N, W;
int memo[200][10001];
vi a;
int t, z;
int dp(int now, int w) {
  if (now == a.size() && w == 0)
    return 1;
  if (now == a.size())
    return 0;
  if (w < 0)
    return 0;
  if (memo[now][w] != -1)
    return memo[now][w];
  int ans = 0;
  ans += dp(now + 1, w - a[now]);
  if (a[now] > z)
    ans += dp(now + 1, w);
  ans %= M;
  return memo[now][w] = ans;
}

int main() {
  cin >> N >> W;
  FOR(i, N) {
    INT(x);
    a.push_back(x);
  }
  sort(ALL(a));
  int ans = 0;
  for (t = 0; t <= W; t++) {
    z = W - t;
    memset(memo, -1, sizeof memo);
    ans = (ans + dp(0, t)) % M;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int N, W;
int memo[250][11000];
vi a;
int t, z;
int dp(int now, int w) {
  if (now == a.size() && w == 0)
    return 1;
  if (now == a.size())
    return 0;
  if (w < 0)
    return 0;
  if (memo[now][w] != -1)
    return memo[now][w];
  int ans = 0;
  ans += dp(now + 1, w - a[now]);
  if (a[now] > z)
    ans += dp(now + 1, w);
  ans %= M;
  return memo[now][w] = ans;
}

int main() {
  cin >> N >> W;
  FOR(i, N) {
    INT(x);
    a.push_back(x);
  }
  sort(ALL(a));
  int ans = 0;
  for (t = 0; t <= W; t++) {
    z = W - t;
    memset(memo, -1, sizeof memo);
    ans = (ans + dp(0, t)) % M;
  }
  cout << ans << endl;
}