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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1e18;
const int MOD = 998244353;
const int MAX = 200001;
int fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int powMOD(int n, int r) {
  if (r == 0)
    return 1;
  if (r % 2 == 0)
    return powMOD(n, r / 2) * powMOD(n, r / 2) % MOD;
  if (r % 2 == 1)
    return powMOD(n, r - 1) * n % MOD;
}

void add(int &x, int y) {
  x += y;
  x %= MOD;
}

signed main() {
  int n, s;
  cin >> n >> s;
  int a[n];
  REP(i, n) cin >> a[i];
  int dp[n + 1][s + 1];
  REP(i, n + 1) REP(j, s + 1) dp[i][j] = 0;
  dp[0][0] = 1;
  REP(i, n)
  REP(j, s + 1)
      add(dp[i + 1][j], (j - a[i] >= 0 ? dp[i][j - a[i]] : 0) + 2 * dp[i][j]);
  cout << dp[n][s] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1e18;
const int MOD = 998244353;
const int MAX = 200001;
int fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int powMOD(int n, int r) {
  if (r == 0)
    return 1;
  if (r % 2 == 0)
    return powMOD(n, r / 2) * powMOD(n, r / 2) % MOD;
  if (r % 2 == 1)
    return powMOD(n, r - 1) * n % MOD;
}

void add(int &x, int y) {
  x += y;
  x %= MOD;
  return;
}

int n, s, ans = 0;
vector<int> a, b;
void dfs(int i, int sum, int kosu) {
  if (i == n) {
    if (sum != s)
      return;
    add(ans, powMOD(2, n - kosu));
    return;
  }
  if (i == n - 1) {
    dfs(i + 1, sum + a[i], kosu + 1);
    dfs(i + 1, sum, kosu);
    return;
  }
  if (b[i + 1] + sum >= s)
    dfs(i + 1, sum, kosu);
  if (sum + a[i] <= s && sum + b[i + 1] + a[i] >= s)
    dfs(i + 1, sum + a[i], kosu + 1);
  return;
}

signed main() {
  cin >> n >> s;
  a.resize(n);
  b.resize(n);
  REP(i, n) cin >> a[i];
  sort(all(a));
  REP(i, n) {
    if (i == 0)
      b[i] = a[i];
    else
      b[i] = b[i - 1] + a[i];
  }
  reverse(all(a));
  reverse(all(b));
  dfs(0, 0, 0);
  cout << ans << endl;
}