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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<double, int> P;
typedef pair<P, ll> T;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-15
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
#define MOD 1000000007ll // prime
// How to devide :
// ModInt a(6ll);
// ModInt b(2ll);
// a *= b.exp(MOD-2ll);  -> a/=b;  result: a = 3
struct ModInt {
  ll val;
  ModInt() : val(0ll) {}
  ModInt(ll v) : val(((v % MOD) + MOD) % MOD) {}
  ModInt exp(ll y) const {
    if (!y)
      return ModInt(1ll);
    ModInt a = exp(y / 2ll);
    a *= a;
    if (y & 1)
      a *= (*this);
    return a;
  }
  bool operator==(const ModInt &x) const { return val == x.val; }
  inline bool operator!=(const ModInt &x) const { return !(*this == x); }
  bool operator<(const ModInt &x) const { return val < x.val; }
  bool operator>(const ModInt &x) const { return val > x.val; }
  inline bool operator>=(const ModInt &x) const { return !(*this < x); }
  inline bool operator<=(const ModInt &x) const { return !(*this > x); }
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val += MOD - x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    (val *= x.val) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
};
istream &operator>>(istream &i, ModInt &x) {
  i >> x.val;
  return i;
}
ostream &operator<<(ostream &o, const ModInt &x) {
  o << x.val;
  return o;
}
int d;
int l[305];
int s;
ModInt dp[2][2][100000];
int main() {
  scanf("%d", &d);
  for (int i = 0; i < d; i++)
    scanf("%d", &l[i]);
  scanf("%d", &s);
  dp[0][0][0] = ModInt(1);
  for (int i = 0; i < d; i++) {
    for (int j = 0; j <= 300 * (i + 1); j++) {
      dp[(i + 1) % 2][0][j] = dp[i % 2][0][j];
      dp[(i + 1) % 2][1][j] = dp[i % 2][1][j];
      if (j >= l[i])
        dp[(i + 1) % 2][1][j] += dp[i % 2][0][j - l[i]];
      if (j >= l[i])
        dp[(i + 1) % 2][0][j] += dp[i % 2][1][j - l[i]];
    }
  }
  ModInt ans;
  for (int i = 0; i <= s; i++) {
    ModInt a = ModInt(s - i);
    ans += a.exp((ll)d) * dp[d % 2][0][i];
    ans -= a.exp((ll)d) * dp[d % 2][1][i];
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<double, int> P;
typedef pair<P, ll> T;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-15
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
#define MOD 1000000007ll // prime
// How to devide :
// ModInt a(6ll);
// ModInt b(2ll);
// a *= b.exp(MOD-2ll);  -> a/=b;  result: a = 3
struct ModInt {
  ll val;
  ModInt() : val(0ll) {}
  ModInt(ll v) : val(((v % MOD) + MOD) % MOD) {}
  ModInt exp(ll y) const {
    if (!y)
      return ModInt(1ll);
    ModInt a = exp(y / 2ll);
    a *= a;
    if (y & 1)
      a *= (*this);
    return a;
  }
  bool operator==(const ModInt &x) const { return val == x.val; }
  inline bool operator!=(const ModInt &x) const { return !(*this == x); }
  bool operator<(const ModInt &x) const { return val < x.val; }
  bool operator>(const ModInt &x) const { return val > x.val; }
  inline bool operator>=(const ModInt &x) const { return !(*this < x); }
  inline bool operator<=(const ModInt &x) const { return !(*this > x); }
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val += MOD - x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    (val *= x.val) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
};
istream &operator>>(istream &i, ModInt &x) {
  i >> x.val;
  return i;
}
ostream &operator<<(ostream &o, const ModInt &x) {
  o << x.val;
  return o;
}
int d;
int l[305];
int s;
ModInt dp[305][2][100000];
int main() {
  scanf("%d", &d);
  for (int i = 0; i < d; i++)
    scanf("%d", &l[i]);
  scanf("%d", &s);
  dp[0][0][0] = ModInt(1);
  for (int i = 0; i < d; i++) {
    for (int j = 0; j <= 300 * (i + 1); j++) {
      dp[i + 1][0][j] = dp[i][0][j];
      dp[i + 1][1][j] = dp[i][1][j];
      if (j >= l[i])
        dp[i + 1][1][j] += dp[i][0][j - l[i]];
      if (j >= l[i])
        dp[i + 1][0][j] += dp[i][1][j - l[i]];
    }
  }
  ModInt ans;
  for (int i = 0; i <= s; i++) {
    ModInt a = ModInt(s - i);
    ans += a.exp((ll)d) * dp[d][0][i];
    ans -= a.exp((ll)d) * dp[d][1][i];
  }
  cout << ans << endl;
  return 0;
}