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

#include <bits/stdc++.h>
using namespace std;

#define en "\\n"
#define INF (int)9e18
#define HELL 998244353LL
#define int long long
#define double long double
#define uint unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fs first
#define sc second
#define size(a) (int)a.size()
#define deb(x) cerr << #x << " => " << x << en
#define debp(a)                                                                \\
  cerr << #a << " => "                                                         \\
       << "(" << a.fs << ", " << a.sc << ") " << en;
#define deba(x)                                                                \\
  cerr << #x << en;                                                            \\
  for (auto a : x)                                                             \\
    cerr << a << " ";                                                          \\
  cerr << en;
#define debpa(x)                                                               \\
  cerr << #x << en;                                                            \\
  for (auto a : x)                                                             \\
    cerr << "(" << a.fs << ", " << a.sc << ") ";                               \\
  cerr << en;
#define debm(x)                                                                \\
  cerr << #x << en;                                                            \\
  for (auto a : x) {                                                           \\
    for (auto b : a)                                                           \\
      cerr << b << " ";                                                        \\
    cerr << en;                                                                \\
  }
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int>(m, val))
#define fastio                                                                 \\
  ios_base ::sync_with_stdio(false);                                           \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pout cout << fixed << setprecision(10)
int fastpow(int a, int b, int m = HELL) {
  int res = 1;
  a %= m;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
#define inv(a) fastpow(a, HELL - 2)
#define mul(a, b) ((a % HELL) * (b % HELL)) % HELL

int n, s;
const int maxN = 3e3 + 5;
int arr[maxN];
int dp[maxN][maxN];

int res(int pos, int sum) {
  if (sum > s) {
    return 0;
  }
  if (pos == n) {
    return sum == s;
  }
  int &ans = dp[pos][sum];
  if (ans == -1) {
    ans = 0;
    int x = res(pos + 1, sum);
    if (x)
      ans = mul(2, x);
    ans = (ans + res(pos + 1, sum + arr[pos])) % HELL;
  }
  return ans;
}

int32_t main() {
  fastio;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  memset(dp, -1, sizeof dp);
  cout << res(0, 0) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define en "\\n"
#define INF (int)9e18
#define HELL 998244353LL
#define int long long
#define double long double
#define uint unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fs first
#define sc second
#define size(a) (int)a.size()
#define deb(x) cerr << #x << " => " << x << en
#define debp(a)                                                                \\
  cerr << #a << " => "                                                         \\
       << "(" << a.fs << ", " << a.sc << ") " << en;
#define deba(x)                                                                \\
  cerr << #x << en;                                                            \\
  for (auto a : x)                                                             \\
    cerr << a << " ";                                                          \\
  cerr << en;
#define debpa(x)                                                               \\
  cerr << #x << en;                                                            \\
  for (auto a : x)                                                             \\
    cerr << "(" << a.fs << ", " << a.sc << ") ";                               \\
  cerr << en;
#define debm(x)                                                                \\
  cerr << #x << en;                                                            \\
  for (auto a : x) {                                                           \\
    for (auto b : a)                                                           \\
      cerr << b << " ";                                                        \\
    cerr << en;                                                                \\
  }
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int>(m, val))
#define fastio                                                                 \\
  ios_base ::sync_with_stdio(false);                                           \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pout cout << fixed << setprecision(10)
int fastpow(int a, int b, int m = HELL) {
  int res = 1;
  a %= m;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
#define inv(a) fastpow(a, HELL - 2)
#define mul(a, b) ((a % HELL) * (b % HELL)) % HELL

int n, s;
const int maxN = 3e3 + 5;
int arr[maxN];
int dp[maxN][maxN];

int res(int pos, int sum) {
  if (sum > s) {
    return 0;
  }
  if (pos == n) {
    return sum == s;
  }
  int ans = dp[pos][sum];
  if (ans == -1) {
    ans = 0;
    int x = res(pos + 1, sum);
    if (x)
      ans = mul(2, x);
    ans = (ans + res(pos + 1, sum + arr[pos])) % HELL;
  }
  return ans;
}

int32_t main() {
  fastio;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  memset(dp, -1, sizeof dp);
  cout << res(0, 0) << endl;
  return 0;
}