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

#include <bits/stdc++.h> // {{{

#define ARG5(a, b, c, d, NAME, ...) NAME
#define REP(...) ARG5(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define REP1(a) REP2(i, a)
#define REP2(i, a) REP3(i, 0, a)
#define REP3(i, a, b) REP4(i, a, b, 1)
#define REP4(i, a, b, s) for (int i = (a); i < (int)(b); i += (s))
#define REPR(...) ARG5(__VA_ARGS__, REPR4, REPR3, REPR2, REPR1)(__VA_ARGS__)
#define REPR1(a) REPR2(i, a)
#define REPR2(i, a) REPR3(i, 0, a)
#define REPR3(i, a, b) REPR4(i, a, b, 1)
#define REPR4(i, a, b, s) for (int i = (b)-1; i >= (int)(a); i -= (s))
#define ALL(c) (c).begin(), (c).end()
#define DUMP(x) (std::cerr << #x << ':' << ' ' << x << '\\n')
#define TMPL_T template <typename T>
#define TMPL_TU template <typename T, typename U>
#define mut auto
#define let const auto

using Int = long long;
// clang-format off
namespace extio {
std::string delimiter=" ",pdelimiter=" ";
std::string bracket_b="",bracket_e="";
void chdelim(const std::string&s){delimiter=s;}
void chpdelim(const std::string&s){pdelimiter=s;}
void chbracket(const std::string&b,const std::string&e){bracket_b=b,bracket_e=e;}
TMPL_T  void pcont(std::ostream&os,const T&x){int c=0;for(const auto&a:x){if(c++)os<<delimiter;os<<a;}}
TMPL_TU void ppair(std::ostream&os,const std::pair<T,U>&p){os<<bracket_b<<p.first<<pdelimiter<<p.second<<bracket_e;}
}
namespace std {
TMPL_T ostream& operator<<(ostream&os,const vector<T>&x){extio::pcont(os,x);return os;}
TMPL_T ostream& operator<<(ostream&os,const set<T>&x){extio::pcont(os,x);return os;}
TMPL_T ostream& operator<<(ostream&os,const multiset<T>&x){extio::pcont(os,x);return os;}
TMPL_T ostream& operator<<(ostream&os,const deque<T>&x){extio::pcont(os,x);return os;}
TMPL_TU ostream& operator<<(ostream&os,const map<T,U>&x){extio::pcont(os,x);return os;}
TMPL_TU ostream& operator<<(ostream&os,const pair<T,U>&x){extio::ppair(os,x);return os;}
}
TMPL_TU inline bool chmax(T&x,U a){return x<a&&(x=a,1);}
TMPL_TU inline bool chmin(T&x,U a){return a<x&&(x=a,1);}

inline int in(){int x;std::cin>>x;return x;}
TMPL_T void read_seq(int N,std::vector<T>&v){v.reserve(N+2);REP(N){T x;std::cin>>x;v.emplace_back(x);}}
TMPL_T void read_seq(int N,T*v){REP(N)std::cin>>v[i];}

struct Initializer_{
  Initializer_(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
    std::cout<<std::setprecision(10);
    std::cerr<<std::setprecision(10);
  }
} precalc;
// clang-format on
// }}}

using namespace std;

using dp_type = std::array<std::array<int, 10001>, 200>;

const int MOD = 1e9 + 7;
int N, W;
int w[256];
dp_type cur, nxt;
void dumpDP(const dp_type &dp) {
  return;
  REP(i, N) {
    cout << "(" << i << "," << w[i] << "):";
    REP(j, W + 1) cout << dp[i][j] << " ";
    cout << endl;
  }
}
signed main() {
  N = in(), W = in();
  REP(i, N) w[i] = in();
  sort(w, w + N);
  for (auto &x : cur) {
    x.fill(0);
  }
  cur[N - 1][0] = 1;
  REP(k, N) {
    dumpDP(cur);
    for (auto &x : nxt) {
      x.fill(0);
    }
    REP(i, N) REPR(j, 0, W - w[k] + 1) {
      int &r = nxt[i][j + w[k]];
      r = (r + cur[i][j]) % MOD;
    }
    REP(i, N)
    REP(j, W + 1) nxt[min(i, k)][j] = (nxt[min(i, k)][j] + cur[i][j]) % MOD;
    swap(cur, nxt);
  }
  int ans = 0;
  REP(i, N) REP(j, W - w[i] + 1, W + 1) ans = (ans + cur[i][j]) % MOD;
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h> // {{{

#define ARG5(a, b, c, d, NAME, ...) NAME
#define REP(...) ARG5(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define REP1(a) REP2(i, a)
#define REP2(i, a) REP3(i, 0, a)
#define REP3(i, a, b) REP4(i, a, b, 1)
#define REP4(i, a, b, s) for (int i = (a); i < (int)(b); i += (s))
#define REPR(...) ARG5(__VA_ARGS__, REPR4, REPR3, REPR2, REPR1)(__VA_ARGS__)
#define REPR1(a) REPR2(i, a)
#define REPR2(i, a) REPR3(i, 0, a)
#define REPR3(i, a, b) REPR4(i, a, b, 1)
#define REPR4(i, a, b, s) for (int i = (b)-1; i >= (int)(a); i -= (s))
#define ALL(c) (c).begin(), (c).end()
#define DUMP(x) (std::cerr << #x << ':' << ' ' << x << '\\n')
#define TMPL_T template <typename T>
#define TMPL_TU template <typename T, typename U>
#define mut auto
#define let const auto

using Int = long long;
// clang-format off
namespace extio {
std::string delimiter=" ",pdelimiter=" ";
std::string bracket_b="",bracket_e="";
void chdelim(const std::string&s){delimiter=s;}
void chpdelim(const std::string&s){pdelimiter=s;}
void chbracket(const std::string&b,const std::string&e){bracket_b=b,bracket_e=e;}
TMPL_T  void pcont(std::ostream&os,const T&x){int c=0;for(const auto&a:x){if(c++)os<<delimiter;os<<a;}}
TMPL_TU void ppair(std::ostream&os,const std::pair<T,U>&p){os<<bracket_b<<p.first<<pdelimiter<<p.second<<bracket_e;}
}
namespace std {
TMPL_T ostream& operator<<(ostream&os,const vector<T>&x){extio::pcont(os,x);return os;}
TMPL_T ostream& operator<<(ostream&os,const set<T>&x){extio::pcont(os,x);return os;}
TMPL_T ostream& operator<<(ostream&os,const multiset<T>&x){extio::pcont(os,x);return os;}
TMPL_T ostream& operator<<(ostream&os,const deque<T>&x){extio::pcont(os,x);return os;}
TMPL_TU ostream& operator<<(ostream&os,const map<T,U>&x){extio::pcont(os,x);return os;}
TMPL_TU ostream& operator<<(ostream&os,const pair<T,U>&x){extio::ppair(os,x);return os;}
}
TMPL_TU inline bool chmax(T&x,U a){return x<a&&(x=a,1);}
TMPL_TU inline bool chmin(T&x,U a){return a<x&&(x=a,1);}

inline int in(){int x;std::cin>>x;return x;}
TMPL_T void read_seq(int N,std::vector<T>&v){v.reserve(N+2);REP(N){T x;std::cin>>x;v.emplace_back(x);}}
TMPL_T void read_seq(int N,T*v){REP(N)std::cin>>v[i];}

struct Initializer_{
  Initializer_(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
    std::cout<<std::setprecision(10);
    std::cerr<<std::setprecision(10);
  }
} precalc;
// clang-format on
// }}}

using namespace std;

const int MOD = 1e9 + 7;
int N, W;
int w[256];

using arg_type = tuple<int, int, int>;
map<arg_type, int> memo;
int dfs(int p, int mp, int weight) {
  if (weight < 0)
    return 0;
  if (p == N)
    return weight < w[mp];
  auto arg = arg_type(p, mp, weight);
  if (memo.count(arg))
    return memo[arg];
  return memo[arg] =
             (dfs(p + 1, mp, weight - w[p]) + dfs(p + 1, min(p, mp), weight)) %
             MOD;
}

signed main() {
  N = in(), W = in();
  REP(i, N) w[i] = in();
  sort(w, w + N);
  cout << dfs(0, N - 1, W) << endl;
  return 0;
}