Problem p01750 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
  return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
  vll r(v.size());
  rep(i, v.size()) r[i] = v[i];
  return r;
}
using P = pair<ll, ll>;

template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
  o << "(" << v.first << ", " << v.second << ")";
  return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
  using s = int[];
  (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
    -> basic_ostream<Ch, Tr> & {
  os << "(";
  print_tuple(os, t, gen_seq<sizeof...(Args)>());
  return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) o << v[i][j] << " ";
    cout << endl;
  }
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << '[';
  rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
  o << "]";
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const unordered_map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it;
  o << "]";
  return o;
}
void printbits(ll mask, ll n) {
  rep(i, n) { cout << !!(mask & (1ll << i)); }
  cout << endl;
}
#define ldout fixed << setprecision(40)

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9 + 7;

class Mod {
public:
  int num;
  int mod;
  Mod() : Mod(0) {}
  Mod(long long int n) : Mod(n, 1000000007) {}
  Mod(long long int n, int m) {
    mod = m;
    num = (n % mod + mod) % mod;
  }
  Mod(const string &s) {
    long long int tmp = 0;
    for (auto &c : s)
      tmp = (c - '0' + tmp * 10) % mod;
    num = tmp;
  }
  Mod(int n) : Mod(static_cast<long long int>(n)) {}
  operator int() { return num; }
  void setmod(const int mod) { this->mod = mod; }
};
istream &operator>>(istream &is, Mod &x) {
  long long int n;
  is >> n;
  x = n;
  return is;
}
ostream &operator<<(ostream &o, const Mod &x) {
  o << x.num;
  return o;
}
Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % a.mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) {
  return Mod((a.mod + a.num - b.num) % a.mod);
}
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) {
  return Mod(((long long)a.num * b.num) % a.mod);
}
Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; }
Mod operator*(const Mod a, const int b) { return Mod(b) * a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod &a, const long long int &b) { return a = a * b; }
Mod factorial(const long long n) {
  if (n < 0)
    return 0;
  Mod ret = 1;
  for (int i = 1; i <= n; i++) {
    ret *= i;
  }
  return ret;
}
Mod operator^(const Mod a, const long long n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod modpowsum(const Mod a, const long long b) {
  if (b == 0)
    return 0;
  if (b % 2 == 1)
    return modpowsum(a, b - 1) * a + Mod(1);
  Mod result = modpowsum(a, b / 2);
  return result * (a ^ (b / 2)) + result;
}

/*************************************/
// ??\??????mod????´???°??§???????????????????????????
/*************************************/
Mod inv(const Mod a) { return a ^ (a.mod - 2); }
Mod operator/(const Mod a, const Mod b) {
  assert(b.num != 0);
  return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
  assert(b.num != 0);
  return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
  assert(b.num != 0);
  return a = a * inv(b);
}

// n!??¨1/n!???????????????????????????
// nCr?????????????¨????????????????????????????
//
// assert??§n????¶?????????????????????????????????¨????????????????????¨???
//
// O(n log mo)
vector<Mod> fact, rfact;
void constructFactorial(const long long n) {
  fact.resize(n);
  rfact.resize(n);
  fact[0] = rfact[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i;
    rfact[i] = Mod(1) / fact[i];
  }
}

// O(1)
Mod nCr(const long long n, const long long r) {
  //    assert(n < (long long)fact.size());
  if (n < 0 || r < 0)
    return 0;
  return fact[n] * rfact[r] * rfact[n - r];
}

// O(k log mo)
Mod nCrWithoutConstruction(const long long n, const long long k) {
  if (n < 0)
    return 0;
  if (k < 0)
    return 0;
  Mod ret = 1;
  for (int i = 0; i < k; i++) {
    ret *= n - (Mod)i;
    ret /= Mod(i + 1);
  }
  return ret;
}
// n*m????????¢????????????????????????????????´????????°
// O(1)
Mod nBm(const long long n, const long long m) {
  if (n < 0 || m < 0)
    return 0;
  return nCr(n + m, n);
}

ll d, r;
vll l;
static ll dp[301][100000][2] = {};
Mod f(ll i, ll s, bool b) {
  if (dp[i][s][b] >= 0)
    return dp[i][s][b];
  if (s < 0)
    return 0;
  if (i == d)
    return dp[i][s][b] = (Mod(s) ^ d) * (b ? -1 : 1);
  return dp[i][s][b] = f(i + 1, s, b) + f(i + 1, s - l[i], !b);
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> d;
  l.resize(d);
  rep(i, l.size()) cin >> l[i];
  cin >> r;
  rep(i, 301) rep(j, 100000) rep(h, 2) dp[i][j][h] = -1;
  cout << f(0, r, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template <class T1, class T2> bool chmin(T1 &a, T2 b) {
  return b < a && (a = b, true);
}
template <class T1, class T2> bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
  vll r(v.size());
  rep(i, v.size()) r[i] = v[i];
  return r;
}
using P = pair<ll, ll>;

template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
  o << "(" << v.first << ", " << v.second << ")";
  return o;
}
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
  using s = int[];
  (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
    -> basic_ostream<Ch, Tr> & {
  os << "(";
  print_tuple(os, t, gen_seq<sizeof...(Args)>());
  return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) o << v[i][j] << " ";
    cout << endl;
  }
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << '[';
  rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : "");
  o << "]";
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it << (next(it) != m.end() ? ", " : "");
  o << "]";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const unordered_map<T, U> &m) {
  o << '[';
  for (auto it = m.begin(); it != m.end(); it++)
    o << *it;
  o << "]";
  return o;
}
void printbits(ll mask, ll n) {
  rep(i, n) { cout << !!(mask & (1ll << i)); }
  cout << endl;
}
#define ldout fixed << setprecision(40)

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9 + 7;

class Mod {
public:
  int num;
  int mod;
  Mod() : Mod(0) {}
  Mod(long long int n) : Mod(n, 1000000007) {}
  Mod(long long int n, int m) {
    mod = m;
    num = (n % mod + mod) % mod;
  }
  Mod(const string &s) {
    long long int tmp = 0;
    for (auto &c : s)
      tmp = (c - '0' + tmp * 10) % mod;
    num = tmp;
  }
  Mod(int n) : Mod(static_cast<long long int>(n)) {}
  operator int() { return num; }
  void setmod(const int mod) { this->mod = mod; }
};
istream &operator>>(istream &is, Mod &x) {
  long long int n;
  is >> n;
  x = n;
  return is;
}
ostream &operator<<(ostream &o, const Mod &x) {
  o << x.num;
  return o;
}
Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % a.mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) {
  return Mod((a.mod + a.num - b.num) % a.mod);
}
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) {
  return Mod(((long long)a.num * b.num) % a.mod);
}
Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; }
Mod operator*(const Mod a, const int b) { return Mod(b) * a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod &a, const long long int &b) { return a = a * b; }
Mod factorial(const long long n) {
  if (n < 0)
    return 0;
  Mod ret = 1;
  for (int i = 1; i <= n; i++) {
    ret *= i;
  }
  return ret;
}
Mod operator^(const Mod a, const long long n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod modpowsum(const Mod a, const long long b) {
  if (b == 0)
    return 0;
  if (b % 2 == 1)
    return modpowsum(a, b - 1) * a + Mod(1);
  Mod result = modpowsum(a, b / 2);
  return result * (a ^ (b / 2)) + result;
}

/*************************************/
// ??\??????mod????´???°??§???????????????????????????
/*************************************/
Mod inv(const Mod a) { return a ^ (a.mod - 2); }
Mod operator/(const Mod a, const Mod b) {
  assert(b.num != 0);
  return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
  assert(b.num != 0);
  return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
  assert(b.num != 0);
  return a = a * inv(b);
}

// n!??¨1/n!???????????????????????????
// nCr?????????????¨????????????????????????????
//
// assert??§n????¶?????????????????????????????????¨????????????????????¨???
//
// O(n log mo)
vector<Mod> fact, rfact;
void constructFactorial(const long long n) {
  fact.resize(n);
  rfact.resize(n);
  fact[0] = rfact[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i;
    rfact[i] = Mod(1) / fact[i];
  }
}

// O(1)
Mod nCr(const long long n, const long long r) {
  //    assert(n < (long long)fact.size());
  if (n < 0 || r < 0)
    return 0;
  return fact[n] * rfact[r] * rfact[n - r];
}

// O(k log mo)
Mod nCrWithoutConstruction(const long long n, const long long k) {
  if (n < 0)
    return 0;
  if (k < 0)
    return 0;
  Mod ret = 1;
  for (int i = 0; i < k; i++) {
    ret *= n - (Mod)i;
    ret /= Mod(i + 1);
  }
  return ret;
}
// n*m????????¢????????????????????????????????´????????°
// O(1)
Mod nBm(const long long n, const long long m) {
  if (n < 0 || m < 0)
    return 0;
  return nCr(n + m, n);
}

ll d, r;
vll l;
static int dp[301][100000][2] = {};
Mod f(ll i, ll s, bool b) {
  if (dp[i][s][b] >= 0)
    return dp[i][s][b];
  if (s < 0)
    return 0;
  if (i == d)
    return dp[i][s][b] = (Mod(s) ^ d) * (b ? -1 : 1);
  return dp[i][s][b] = f(i + 1, s, b) + f(i + 1, s - l[i], !b);
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> d;
  l.resize(d);
  rep(i, l.size()) cin >> l[i];
  cin >> r;
  rep(i, 301) rep(j, 100000) rep(h, 2) dp[i][j][h] = -1;
  cout << f(0, r, 0) << endl;
}