Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PREP(i, s, x) for (ll i = (s); i < (x); i++)
#define MREP(i, s, x) for (ll i = (s); i >= (x); i--)
#define REP(i, x) PREP(i, 0, x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

// variable -------------------------------------------

constexpr ll INF = (1ll << 60);
constexpr ll MOD = 998244353ll;

// structure ------------------------------------------

// function -------------------------------------------

ll llabs(ll &a) { return (a < 0) ? (-a) : (a); }

// べき乗を計算　指数を計算
ll llpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

// aの逆元　MODをとる
ll llinv(ll a) {
  ll b = MOD, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0)
    u += MOD;
  return u;
}

// main -----------------------------------------------

int main() {
  ll n, s;
  cin >> n >> s;
  vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }

  // dp[i+1][j][k] :=
  // A_iまでのなかからj個選んだとき、総和がkとなるような選び方の個数
  // vector<vector<vector<ll>>> dp(n+1, vector<vector<ll>>(n+1, vector<ll>(s+1,
  // 0)));

  // 上記のdpからjを削除
  vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, 0));
  // 初期化 : 0個選んだ時の総和は0で、組み合わせ的には1通りです
  dp[0][0] = llpow(2, n);

  ll ninogyakugen = llinv(2);

  REP(i, n) {

    REP(k, s + 1) {
      dp[i + 1][k] = dp[i][k];
      if (k - a[i] < 0)
        continue;
      dp[i + 1][k] += (dp[i][k - a[i]] * ninogyakugen) % MOD;
      dp[i + 1][k] %= MOD;
    }
  }
  // ll ans = 0;
  // REP(j, n){
  //     ans += (dp[n][j+1][s] * llpow(2, n-(j+1))) % MOD;
  //     ans %= MOD;
  // }
  cout << dp[n][s] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PREP(i, s, x) for (ll i = (s); i < (x); i++)
#define MREP(i, s, x) for (ll i = (s); i >= (x); i--)
#define REP(i, x) PREP(i, 0, x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

// variable -------------------------------------------

constexpr ll INF = (1ll << 60);
constexpr ll MOD = 998244353ll;

// structure ------------------------------------------

// function -------------------------------------------

ll llabs(ll &a) { return (a < 0) ? (-a) : (a); }

// べき乗を計算　指数を計算
ll llpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

// main -----------------------------------------------

int main() {
  ll n, s;
  cin >> n >> s;
  vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }

  // dp[i+1][j][k] :=
  // A_iまでのなかからj個選んだとき、総和がkとなるような選び方の個数
  vector<vector<vector<ll>>> dp(
      n + 1, vector<vector<ll>>(n + 1, vector<ll>(s + 1, 0)));
  // 初期化 : 0個選んだ時の総和は0で、組み合わせ的には1通りです
  REP(i, n + 1) { dp[i][0][0] = 1; }

  REP(i, n) {
    REP(j, n) {
      REP(k, s + 1) {
        dp[i + 1][j + 1][k] = dp[i][j + 1][k];
        if (k - a[i] < 0)
          continue;
        dp[i + 1][j + 1][k] += dp[i][j][k - a[i]];
        dp[i + 1][j + 1][k] %= MOD;
      }
    }
  }
  ll ans = 0;
  REP(j, n) {
    ans += (dp[n][j + 1][s] * llpow(2, n - (j + 1))) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}