Problem p02662 (C++) — buggy submission, verdict: Runtime Error

// 5/14
// 過去問ガリガリ
#include <bits/stdc++.h>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int N, S;
  cin >> N >> S;

  vector<int> A(N);
  rep(i, N) cin >> A[i];

  // ll dp[3010][3010];  // dp[i+1][j] = i番目まででj個作れるかどうか

  vector<vector<mint>> dp(N + 1, vector<mint>(S + 1, 0));
  // memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  rep(i, N) {
    for (int j = 0; j <= S; j++) {
      dp[i + 1][j] += dp[i][j] * 2; // A[i]を使わない場合
      if (j >= A[i])
        dp[i + 1][j] += dp[i][j - A[i]]; // A[i]を使う場合
      // if (j + A[i] <= S) dp[i + 1][j + A[i]] += dp[i][j];
    }
  }

  // mint ans = 0;

  // rep(i, N) {
  //     ans += dp[i + 1][S] * mint(2).pow(N - i - 1);
  // }

  // cout << ans << endl;

  cout << dp[N][S] << endl;
}


Error: 0

Fixed submission:

// 5/14
// 過去問ガリガリ
#include <bits/stdc++.h>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int N, S;
  cin >> N >> S;

  vector<int> A(N);
  rep(i, N) cin >> A[i];

  // ll dp[3010][3010];  // dp[i+1][j] = i番目まででj個作れるかどうか

  vector<vector<mint>> dp(N + 1, vector<mint>(N + 1, 0));
  // memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  rep(i, N) {
    for (int j = 0; j <= S; j++) {
      dp[i + 1][j] += dp[i][j] * 2; // A[i]を使わない場合
      // if (j >= A[i]) dp[i + 1][j] += dp[i][j - A[i]];  // A[i]を使う場合
      if (j + A[i] <= S)
        dp[i + 1][j + A[i]] += dp[i][j];
    }
  }

  // mint ans = 0;

  // rep(i, N) {
  //     ans += dp[i + 1][S] * mint(2).pow(N - i - 1);
  // }

  // cout << ans << endl;

  cout << dp[N][S] << endl;
}