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

#include <bits/extc++.h>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
using ll = long long;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) int((x).size())
#define REP(i, a, b) for (auto(i) = (a); (i) <= (b); (i)++)
#define PER(i, a, b) for (auto(i) = (a); (i) >= (b); (i)--)
#define ITR(i, x) for (auto(i) = (x).begin(); (i) != (x).end(); (i)++)

string to_string(string s) { return '\\"' + s + '\\"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.F) + ", " + to_string(p.S) + ")";
}
template <typename T> string to_string(T v) {
  int f = 0;
  string res = "{";
  for (const auto &x : v)
    res += (f++ ? ", " : "") + to_string(x);
  return res + "}";
}
void debug_out() { cerr << "]\\n"; }
template <typename T, typename... V> void debug_out(T a, V... v) {
  cerr << to_string(a);
  if (sizeof...(v))
    cerr << ", ";
  debug_out(v...);
}

#ifdef LOCAL // add -DLOCAL
#define debug(...)                                                             \\
  cerr << "[" << #__VA_ARGS__ << "] = [", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif // debug(var1, var2, ...);

const int mxN = 3e3;
const ll MOD = 998244353;
int a[mxN + 5];
ll memo[mxN + 5][mxN + 5];

ll modpow(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * modpow(b, e - 1) % MOD;
  return modpow((b * b) % MOD, e / 2);
}

ll dp(int i, int s) {
  // debug(i, s);
  if (s == 0)
    return modpow(2, i + 1);
  if (i == 0) {
    if (a[0] == s)
      return 1;
    else
      return 0;
  }
  if (memo[i][s] == -1) {
    memo[i][s] = (2 * dp(i - 1, s)) % MOD;
    if (a[i] <= s)
      memo[i][s] = (memo[i][s] + dp(i - 1, s - a[i])) % MOD;
  }
  return memo[i][s];
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(memo, -1, sizeof memo);

  int n, s;
  cin >> n >> s;
  REP(i, 0, n - 1) cin >> a[i];
  cout << dp(n - 1, s) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/extc++.h>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
using ll = long long;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) int((x).size())
#define REP(i, a, b) for (auto(i) = (a); (i) <= (b); (i)++)
#define PER(i, a, b) for (auto(i) = (a); (i) >= (b); (i)--)
#define ITR(i, x) for (auto(i) = (x).begin(); (i) != (x).end(); (i)++)

string to_string(string s) { return '\\"' + s + '\\"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.F) + ", " + to_string(p.S) + ")";
}
template <typename T> string to_string(T v) {
  int f = 0;
  string res = "{";
  for (const auto &x : v)
    res += (f++ ? ", " : "") + to_string(x);
  return res + "}";
}
void debug_out() { cerr << "]\\n"; }
template <typename T, typename... V> void debug_out(T a, V... v) {
  cerr << to_string(a);
  if (sizeof...(v))
    cerr << ", ";
  debug_out(v...);
}

#ifdef LOCAL // add -DLOCAL
#define debug(...)                                                             \\
  cerr << "[" << #__VA_ARGS__ << "] = [", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif // debug(var1, var2, ...);

const ll MOD = 998244353;
const int mxN = 3e3;
int n;
int a[mxN + 5];

ll ans = 0;

ll modpow(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * modpow(b, e - 1) % MOD;
  return modpow((b * b) % MOD, e / 2);
}

void backtrack(int i, int s, int num) {
  if (s < 0)
    return;
  if (i == n) {
    if (s == 0)
      ans = (ans + modpow(2, n - num)) % MOD;
  } else {
    backtrack(i + 1, s, num);
    backtrack(i + 1, s - a[i], num + 1);
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int s;
  cin >> n >> s;
  REP(i, 0, n - 1) cin >> a[i];
  backtrack(0, s, 0);
  cout << ans << '\\n';
  return 0;
}