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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;

int dp[301][90001][2];

ll mod_pow(ll a, ll b) {
  a %= MOD;
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = res * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

ll add(ll x, ll y) { return (x + y) % MOD; }

ll sub(ll x, ll y) { return (x + MOD - y) % MOD; }

ll mult(ll x, ll y) { return x * y % MOD; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int d;
  cin >> d;
  vector<int> l(d);
  rep(i, d) { cin >> l[i]; }
  int s;
  cin >> s;
  dp[0][s][0] = 1;
  rep(i, d) {
    rep(j, s + 1) {
      if (j >= l[i]) {
        dp[i + 1][j - l[i]][1] = add(dp[i + 1][j - l[i]][1], dp[i][j][0]);
        dp[i + 1][j - l[i]][0] = add(dp[i + 1][j - l[i]][0], dp[i][j][1]);
      }
      dp[i + 1][j][0] = add(dp[i + 1][j][0], dp[i][j][0]);
      dp[i + 1][j][1] = add(dp[i + 1][j][1], dp[i][j][1]);
    }
  }
  ll ans = 0;
  for (int i = 1; i <= s; i++) {
    ans = add(ans, mult(mod_pow(i, d), sub(dp[d][i][0], dp[d][i][1])));
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;

ll dp[301][90001][2];

ll mod_pow(ll a, ll b) {
  a %= MOD;
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = res * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

ll add(ll x, ll y) { return (x + y) % MOD; }

ll sub(ll x, ll y) { return (x + MOD - y) % MOD; }

ll mult(ll x, ll y) { return x * y % MOD; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int d;
  cin >> d;
  vector<int> l(d);
  rep(i, d) { cin >> l[i]; }
  int s;
  cin >> s;
  dp[0][s][0] = 1;
  rep(i, d) {
    rep(j, s + 1) {
      if (j >= l[i]) {
        dp[i + 1][j - l[i]][1] = add(dp[i + 1][j - l[i]][1], dp[i][j][0]);
        dp[i + 1][j - l[i]][0] = add(dp[i + 1][j - l[i]][0], dp[i][j][1]);
      }
      dp[i + 1][j][0] = add(dp[i + 1][j][0], dp[i][j][0]);
      dp[i + 1][j][1] = add(dp[i + 1][j][1], dp[i][j][1]);
    }
  }
  ll ans = 0;
  for (int i = 1; i <= s; i++) {
    ans = add(ans, mult(mod_pow(i, d), sub(dp[d][i][0], dp[d][i][1])));
  }
  cout << ans << endl;
  return 0;
}