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

#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\\n'
using namespace std;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  int getmod() { return mod; }
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
const int MOD = 998244353; // if inv is needed, this shold be prime.
using modint = ModInt<MOD>;

modint dp[3005][6005];
int main() {
  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  sort(A.begin(), A.end());

  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int s = 0; s <= S; s++) {
      dp[i + 1][s] += dp[i][s] * 2;
      dp[i + 1][s + A[i]] += dp[i][s];
    }
  }
  cout << dp[N][S] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\\n'
using namespace std;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  int getmod() { return mod; }
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
const int MOD = 998244353; // if inv is needed, this shold be prime.
using modint = ModInt<MOD>;

bool **dp;
vector<int> A;
modint ans;
int N;

void display(const vector<int> &v) { ans += modint(2).pow(N - (int)v.size()); }
// A recursive function to print all subsets with the
// help of dp[][]. Vector p[] stores current subset.
void printSubsetsRec(vector<int> &arr, int i, int sum, vector<int> &p) {
  // If we reached end and sum is non-zero. We print
  // p[] only if arr[0] is equal to sun OR dp[0][sum]
  // is true.
  if (i == 0 && sum != 0 && dp[0][sum]) {
    p.push_back(arr[i]);
    display(p);
    return;
  }

  // If sum becomes 0
  if (i == 0 && sum == 0) {
    display(p);
    return;
  }

  // If given sum can be achieved after ignoring
  // current element.
  if (dp[i - 1][sum]) {
    // Create a new vector to store path
    vector<int> b = p;
    printSubsetsRec(arr, i - 1, sum, b);
  }

  // If given sum can be achieved after considering
  // current element.
  if (sum >= arr[i] && dp[i - 1][sum - arr[i]]) {
    p.push_back(arr[i]);
    printSubsetsRec(arr, i - 1, sum - arr[i], p);
  }
}

// Prints all subsets of arr[0..n-1] with sum 0.
void printAllSubsets(vector<int> &arr, int n, int sum) {
  if (n == 0 || sum < 0)
    return;

  // Sum 0 can always be achieved with 0 elements
  dp = new bool *[n];
  for (int i = 0; i < n; ++i) {
    dp[i] = new bool[sum + 1];
    dp[i][0] = true;
  }

  // Sum arr[0] can be achieved with single element
  if (arr[0] <= sum)
    dp[0][arr[0]] = true;

  // Fill rest of the entries in dp[][]
  for (int i = 1; i < n; ++i)
    for (int j = 0; j < sum + 1; ++j)
      dp[i][j] =
          (arr[i] <= j) ? dp[i - 1][j] || dp[i - 1][j - arr[i]] : dp[i - 1][j];
  if (dp[n - 1][sum] == false) {
    return;
  }

  // Now recursively traverse dp[][] to find all
  // paths from dp[n-1][sum]
  vector<int> p;
  printSubsetsRec(arr, n - 1, sum, p);
}
int main() {
  int S;
  cin >> N >> S;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  sort(A.begin(), A.end());

  ans = modint(0);
  printAllSubsets(A, N, S);
  cout << ans << endl;
}