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

// #include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
using namespace std;
// using namespace __gnu_pbds;
// template <class c, class cmp = less<c> > using ordered_set = tree<c,
// null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 10005;
#define endl '\\n'
// #define int ll
#define ld long double
#define all(x) (x).begin(), (x).end()

const ll md = (ll)998244353;

inline ll add(ll a, ll b) {
  a += b;
  if (a >= md)
    a -= md;
  return a;
}

inline ll sub(ll a, ll b) {
  a -= b;
  if (a < 0)
    a += md;
  return a;
}

inline ll mul(ll a, ll b) { return (ll)((long long)a * b % md); }

inline ll power(ll a, long long b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline ll inv(ll a) {
  a %= md;
  if (a < 0)
    a += md;
  ll b = md, u = 0, v = 1;
  while (a) {
    ll t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  assert(b == 1);
  if (u < 0)
    u += md;
  return u;
}

int32_t main() {
  IOS

      int n,
      s;
  cin >> n >> s;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp[n + 1][s + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = power(2, n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i + 1][j] = add(dp[i][j], dp[i + 1][j]);
      if (j + a[i] <= s)
        dp[i + 1][j + a[i]] = add(mul(dp[i][j], inv(2)), dp[i + 1][j + a[i]]);
    }
  }

  cout << dp[n][s] << endl;
}


Error: TLE

Fixed submission:

// #include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
using namespace std;
// using namespace __gnu_pbds;
// template <class c, class cmp = less<c> > using ordered_set = tree<c,
// null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 10005;
#define endl '\\n'
#define int ll
#define ld long double
#define all(x) (x).begin(), (x).end()
const ll md = (ll)998244353;

inline ll add(ll a, ll b) {
  a += b;
  if (a >= md)
    a -= md;
  return a;
}

inline ll sub(ll a, ll b) {
  a -= b;
  if (a < 0)
    a += md;
  return a;
}

inline ll mul(ll a, ll b) { return (ll)((long long)a * b % md); }

inline ll power(ll a, long long b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline ll inv(ll a) {
  a %= md;
  if (a < 0)
    a += md;
  ll b = md, u = 0, v = 1;
  while (a) {
    ll t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  assert(b == 1);
  if (u < 0)
    u += md;
  return u;
}

vector<int> fact(maxn + 5);
void init(int n) {
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = mul(i, fact[i - 1]);
  }
}
int nck(int n, int k) {
  return mul(fact[n], mul(inv(fact[k]), inv(fact[n - k])));
}

int32_t main() {
  IOS int n, s;
  cin >> n >> s;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp[2][s + 1][n + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    int cur = i % 2;
    for (int j = 0; j <= s; j++) {
      for (int k = 0; k <= n; k++)
        dp[cur ^ 1][j][k] = 0;
    }
    for (int j = 0; j <= s; j++) {
      for (int k = 0; k <= i; k++) {
        dp[cur ^ 1][j][k] = add(dp[cur][j][k], dp[cur ^ 1][j][k]);
        if (j + a[i] <= s)
          dp[cur ^ 1][j + a[i]][k + 1] =
              add(dp[cur][j][k], dp[cur ^ 1][j + a[i]][k + 1]);
      }
    }
  }
  int ans = 0;
  for (int k = 1; k <= n; k++) {
    ans += (dp[n % 2][s][k] * power(2, n - k));
    ans %= md;
  }
  cout << ans << endl;
}