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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int64_t power(int64_t x, int64_t y, int64_t mod) {
  int64_t result = 1;
  while (y > 0) {
    if (y & 1)
      result = result * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return result;
}

void solve() {
  int d, s, mod = 1e9 + 7, sum = 0;
  cin >> d;
  vector<int> l(d);
  rep(i, 0, d) {
    cin >> l[i];
    sum += l[i];
  }
  cin >> s;

  auto f = [&](int x) { return power(x, d, mod); };

  static int dp[300][2][90001];
  rep(i, 0, 300) rep(j, 0, 2) rep(k, 0, 90001) dp[i][j][k] = -1;
  function<int(int, int, int)> rec = [&](int i, int c, int k) {
    if (dp[i][c][k] != -1)
      return dp[i][c][k];
    dp[i][c][k] = 0;
    if (i == d - 1) {
      if (c == 1) {
        dp[i][c][k] = (-f(k) + mod) % mod;
        if (k - l[i] >= 0)
          dp[i][c][k] = (dp[i][c][k] + f(k - l[i]) + mod) % mod;
      } else {
        dp[i][c][k] = (f(k) + mod) % mod;
        if (k - l[i] >= 0)
          dp[i][c][k] = (dp[i][c][k] - f(k - l[i]) + mod) % mod;
      }
      return dp[i][c][k];
    }
    dp[i][c][k] = rec(i + 1, c, k);
    if (k - l[i] >= 0)
      dp[i][c][k] = (dp[i][c][k] + rec(i + 1, (c + 1) % 2, k - l[i])) % mod;
    return dp[i][c][k];
  };
  cout << rec(0, 0, s) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

Error: MLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int64_t power(int64_t x, int64_t y, int64_t mod) {
  int64_t result = 1;
  while (y > 0) {
    if (y & 1)
      result = result * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return result;
}

void solve() {
  int64_t d, s, mod = 1e9 + 7, sum = 0;
  cin >> d;
  vector<int64_t> l(d);
  rep(i, 0, d) {
    cin >> l[i];
    sum += l[i];
  }
  cin >> s;

  auto f = [&](int64_t x) { return power(x, d, mod); };
  /*
  int64_t ans=0;
  rep(i,0,1<<d){
      bitset<300> bs(i);
      int64_t sum=0;
      rep(j,0,d){
          if(!bs[j]) continue;
          sum+=l[j];
      }
      if(sum>=s) continue;
      if(__builtin_popcount(i)%2==1) ans=(ans-f(s-sum)+mod)%mod;
      else ans=(ans+f(s-sum))%mod;
  }
  */
  static int64_t dp[300][2][90001];
  rep(i, 0, 300) rep(j, 0, 2) rep(k, 0, 90001) dp[i][j][k] = -1;
  function<int64_t(int i, int c, int64_t k)> rec = [&](int i, int c,
                                                       int64_t k) {
    if (dp[i][c][k] != -1)
      return dp[i][c][k];
    dp[i][c][k] = 0;
    if (i == d - 1) {
      if (c == 1) {
        dp[i][c][k] = (-f(k) + mod) % mod;
        if (k - l[i] >= 0)
          dp[i][c][k] = (dp[i][c][k] + f(k - l[i]) + mod) % mod;
      } else {
        dp[i][c][k] = (f(k) + mod) % mod;
        if (k - l[i] >= 0)
          dp[i][c][k] = (dp[i][c][k] - f(k - l[i]) + mod) % mod;
      }
      return dp[i][c][k];
    }
    dp[i][c][k] = rec(i + 1, c, k);
    if (k - l[i] >= 0)
      dp[i][c][k] = (dp[i][c][k] + rec(i + 1, (c + 1) % 2, k - l[i])) % mod;
    return dp[i][c][k];
  };
  cout << rec(0, 0, s) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}