Problem p02662 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 998244353;

const int MAX_N = 3010;

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<MOD>;

int N, S;
vector<int> A;
int dp[MAX_N][MAX_N][3];

// Ai を見ていて、現在の値は val, i の選定ステートは state のときの
// 合計パターン数
// state:
//   0: T の選定漏れ(Ai は選べない)
//   1: T にはいるけど選ばれなかった
//   2: T にいるし、選ばれた

modint dfs(int i, int val, int state) {
  if (i >= N) {
    return (val == S) ? 1 : 0;
  }

  int pres = dp[i][val][state];
  if (~pres) {
    return modint(pres);
  }

  modint ret(0);

  if (state == 0) {
    ret += dfs(i + 1, val, 0);
    ret += dfs(i + 1, val, 1);
    ret += dfs(i + 1, val, 2);
  } else if (state == 1) {
    ret += dfs(i + 1, val, 0);
    ret += dfs(i + 1, val, 1);
    ret += dfs(i + 1, val, 2);
  } else {
    int next_val = val + A[i];
    if (next_val <= S) {
      ret += dfs(i + 1, next_val, 0);
      ret += dfs(i + 1, next_val, 1);
      ret += dfs(i + 1, next_val, 2);
    }
  }

  return ret;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> S;
  A.resize(N);
  REP(i, N) { cin >> A[i]; }

  MINUS(dp);

  modint ans(0);
  ans += dfs(0, 0, 0);
  ans += dfs(0, 0, 1);
  ans += dfs(0, 0, 2);

  ans /= modint(3);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 998244353;

const int MAX_N = 3010;

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<MOD>;

int N, S;
vector<int> A;
int dp[MAX_N][MAX_N][3];

// Ai を見ていて、現在の値は val, i の選定ステートは state のときの
// 合計パターン数
// state:
//   0: T の選定漏れ(Ai は選べない)
//   1: T にはいるけど選ばれなかった
//   2: T にいるし、選ばれた

modint dfs(int i, int val, int state) {
  if (i >= N) {
    return (val == S) ? 1 : 0;
  }

  int pres = dp[i][val][state];
  if (~pres) {
    return modint(pres);
  }

  modint ret(0);

  if (state == 0) {
    ret += dfs(i + 1, val, 0);
    ret += dfs(i + 1, val, 1);
    ret += dfs(i + 1, val, 2);
  } else if (state == 1) {
    ret += dfs(i + 1, val, 0);
    ret += dfs(i + 1, val, 1);
    ret += dfs(i + 1, val, 2);
  } else {
    int next_val = val + A[i];
    if (next_val <= S) {
      ret += dfs(i + 1, next_val, 0);
      ret += dfs(i + 1, next_val, 1);
      ret += dfs(i + 1, next_val, 2);
    }
  }

  dp[i][val][state] = ret.x;

  return ret;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> S;
  A.resize(N);
  REP(i, N) { cin >> A[i]; }

  MINUS(dp);

  modint ans(0);
  ans += dfs(0, 0, 0);
  ans += dfs(0, 0, 1);
  ans += dfs(0, 0, 2);

  ans /= modint(3);

  cout << ans << endl;

  return 0;
}