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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author champon
 */

//<editor-fold desc="definitions">
#include <bits/stdc++.h>
#define se cout << "test ok" << endl;
#define sp << " " <<
#define enl "\\n"
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vector<int>>;
using viii = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using vlll = vector<vector<vector<ll>>>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T1, class T2> using P = pair<T1, T2>;
ll mod = ll(1e9) + 7;
ll inf = ll(1e18);
void bl(bool b, ostream &out = cout) { out << (b ? "Yes" : "No") << endl; }
void bl2(bool b, ostream &out = cout) { out << (b ? "YES" : "NO") << endl; }
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

#define TO_STRING(varName) #varName
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

namespace dbg {

using ll = long long;

class Debug {
  void prefix() {
    cout << "\\033[7m\\033[35m"
         << " DEBUG "
         << "\\033[0m" << endl;
  }

  void out_helper() { cout << endl; }

  void out_with_name_helper() { cout << endl; }

  template <typename T1, typename... T2> void out_helper(T1 v, T2... args);

  template <typename T1, typename... T2>
  void out_with_name_helper(string s, T1 v, T2... args);

  // value
  template <typename T> void print(T val);

  // pair
  template <typename A, typename B> void print(pair<A, B> p);

  // tuple
  template <typename A, typename B, typename C> void print(tuple<A, B, C> t);

  // vector
  template <typename T> void print(vector<T> &vec);

  // set
  template <typename T> void print(set<T> &st);

  // map
  template <typename A, typename B> void print(map<A, B> mp);

public:
  Debug() {}

  template <typename... T> void out(T... args) {
    prefix();
    out_helper(args...);
  }

  template <typename... T> void out_with_name(T... args) {
    prefix();
    out_with_name_helper(args...);
  }

  template <typename T> void outg(vector<vector<T>> &g) {
    prefix();
    for (auto &v1 : g) {
      for (auto &v2 : v1) {
        cout << v2 << " ";
      }
      cout << endl;
    }
  }
};

template <typename T1, typename... T2>
void Debug::out_helper(T1 v, T2... args) {
  print(v);
  cout << endl;
  out_helper(args...);
}

template <typename T1, typename... T2>
void Debug::out_with_name_helper(string s, T1 v, T2... args) {
  cout << s << ": ";
  print(v);
  cout << endl;
  out_with_name_helper(args...);
}

template <typename T> void Debug::print(T val) { cout << val; }

template <typename A, typename B> void Debug::print(pair<A, B> p) {
  cout << "(";
  print(p.first);
  cout << ", ";
  print(p.second);
  cout << ")";
}

template <typename A, typename B, typename C>
void Debug::print(tuple<A, B, C> t) {
  cout << "(";
  print(get<0>(t));
  cout << ", ";
  print(get<1>(t));
  cout << ", ";
  print(get<2>(t));
  cout << ")";
}

template <typename T> void Debug::print(vector<T> &vec) {
  cout << "[";
  bool fir = true;
  for (auto &e : vec) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "]";
}

template <typename T> void Debug::print(set<T> &st) {
  cout << "{";
  bool fir = true;
  for (auto &e : st) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "}";
}

template <typename A, typename B> void Debug::print(map<A, B> mp) {
  cout << "{";
  bool fir = true;
  for (auto &p : mp) {
    if (!fir)
      cout << " ";
    print(p), fir = false;
  }
  cout << "}";
}

} // namespace dbg

#endif // INCLUDE_DEBUG_HPP

dbg::Debug debug;
//</editor-fold>

#ifndef INCLUDE_MINT_HPP
#define INCLUDE_MINT_HPP

#include <ostream>

namespace mint {

using ll = long long;

class Mint {
public:
  ll v;
  ll mod;

  Mint(ll v = 0, ll mod = ll(1e9) + 7) : mod(mod), v((v % mod + mod) % mod) {}

  friend std::ostream &operator<<(std::ostream &os, const Mint &val) {
    os << val.v;
    return os;
  }

  Mint operator-() const { return Mint(-v); }
  Mint operator+(const Mint rhs) const {
    Mint res(*this);
    return res += rhs;
  }
  Mint operator-(const Mint rhs) const {
    Mint res(*this);
    return res -= rhs;
  }
  Mint operator*(const Mint rhs) const {
    Mint res(*this);
    return res *= rhs;
  }
  Mint &operator+=(const Mint rhs) {
    if ((v += rhs.v) >= mod)
      v -= mod;
    return *this;
  }
  Mint &operator-=(const Mint rhs) {
    if ((v += mod - rhs.v) >= mod)
      v -= mod;
    return *this;
  }
  Mint &operator*=(const Mint rhs) {
    (v *= rhs.v) %= mod;
    return *this;
  }
  Mint pow(ll t) const {
    if (!t)
      return 1;
    Mint b = pow(t >> 1);
    b *= b;
    if (t & 1)
      b *= *this;
    return b;
  }
  Mint inv() const { return pow(mod - 2); }
  Mint &operator/=(const Mint rhs) { return (*this) *= rhs.inv(); }
  Mint operator/(const Mint rhs) const {
    Mint res(*this);
    return res /= rhs;
  }
};

} // namespace mint

#endif // INCLUDE_MINT_HPP

using mint::Mint;

class FKnapsackForAllSubsets {
public:
  void solve(std::istream &in, std::ostream &out) {
    int n, s;
    in >> n >> s;
    vi a(n);
    for (int i = 0; i < n; i++)
      in >> a[i];

    mod = 998244353;
    VV<Mint> dp(n + 1, V<Mint>(s + 1, Mint(0, mod)));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= s; j++) {
        if (j + a[i] <= s) {
          dp[i + 1][j + a[i]] += dp[i][j];
        }
        dp[i + 1][j] += dp[i][j] * 2;
      }
    }

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  FKnapsackForAllSubsets solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author champon
 */

//<editor-fold desc="definitions">
#include <bits/stdc++.h>
#define se cout << "test ok" << endl;
#define sp << " " <<
#define enl "\\n"
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using viii = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using vlll = vector<vector<vector<ll>>>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
ll mod = ll(1e9) + 7;
ll inf = ll(1e18);
void bl(bool b, ostream &out = cout) { out << (b ? "Yes" : "No") << endl; }
void bl2(bool b, ostream &out = cout) { out << (b ? "YES" : "NO") << endl; }
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

#define TO_STRING(varName) #varName
#include <iostream>
#include <map>
#include <vector>
using namespace std;

namespace dbg {

using ll = long long;

class Debug {
  void prefix() {
    cout << "\\033[7m\\033[35m"
         << " DEBUG "
         << "\\033[0m" << endl;
  }

  void out_helper() { cout << endl; }

  void out_with_name_helper() { cout << endl; }

  template <typename T1, typename... T2> void out_helper(T1 v, T2... args);

  template <typename T1, typename... T2>
  void out_with_name_helper(string s, T1 v, T2... args);

  // value
  template <typename T> void print(T val);

  // pair
  template <typename A, typename B> void print(pair<A, B> p);

  // tuple
  template <typename A, typename B, typename C> void print(tuple<A, B, C> t);

  // vector
  template <typename T> void print(vector<T> &vec);

  // set
  template <typename T> void print(set<T> &st);

  // map
  template <typename A, typename B> void print(map<A, B> mp);

public:
  Debug() {}

  template <typename... T> void out(T... args) {
    prefix();
    out_helper(args...);
  }

  template <typename... T> void out_with_name(T... args) {
    prefix();
    out_with_name_helper(args...);
  }

  template <typename T> void outg(vector<vector<T>> &g) {
    prefix();
    for (auto &v1 : g) {
      for (auto &v2 : v1) {
        cout << v2 << " ";
      }
      cout << endl;
    }
  }
};

template <typename T1, typename... T2>
void Debug::out_helper(T1 v, T2... args) {
  print(v);
  cout << endl;
  out_helper(args...);
}

template <typename T1, typename... T2>
void Debug::out_with_name_helper(string s, T1 v, T2... args) {
  cout << s << ": ";
  print(v);
  cout << endl;
  out_with_name_helper(args...);
}

template <typename T> void Debug::print(T val) { cout << val; }

template <typename A, typename B> void Debug::print(pair<A, B> p) {
  cout << "(";
  print(p.first);
  cout << ", ";
  print(p.second);
  cout << ")";
}

template <typename A, typename B, typename C>
void Debug::print(tuple<A, B, C> t) {
  cout << "(";
  print(get<0>(t));
  cout << ", ";
  print(get<1>(t));
  cout << ", ";
  print(get<2>(t));
  cout << ")";
}

template <typename T> void Debug::print(vector<T> &vec) {
  cout << "[";
  bool fir = true;
  for (auto &e : vec) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "]";
}

template <typename T> void Debug::print(set<T> &st) {
  cout << "{";
  bool fir = true;
  for (auto &e : st) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "}";
}

template <typename A, typename B> void Debug::print(map<A, B> mp) {
  cout << "{";
  bool fir = true;
  for (auto &p : mp) {
    if (!fir)
      cout << " ";
    print(p), fir = false;
  }
  cout << "}";
}

} // namespace dbg

#endif // INCLUDE_DEBUG_HPP

dbg::Debug debug;
//</editor-fold>

#ifndef DEFINE_MATH_HPP
#define DEFINE_MATH_HPP

namespace math {

using ll = long long;

class Math {
  ll mod;

public:
  Math(ll mod = 1e9 + 7) : mod(mod) {}
  ll keta(ll num);
  ll gcd(ll a, ll b);
  ll lcm(ll a, ll b);
  ll powm(ll num1, ll num2);
  bool isPrime(ll n);
};

ll Math::gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll Math::lcm(ll a, ll b) { return a * (b / gcd(a, b)); }

ll Math::keta(ll num) {
  ll k = 0;
  while (num > 0) {
    num /= 10;
    k++;
  }
  return k;
}

ll Math::powm(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res;
}

bool Math::isPrime(ll n) {
  bool flg = true;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flg = false;
      break;
    }
  }
  return flg;
}

} // namespace math

#endif // DEFINE_MATH_HPP

math::Math mt;

#ifndef INCLUDE_MINT_HPP
#define INCLUDE_MINT_HPP

#include <ostream>

namespace mint {

using ll = long long;

class Mint {
public:
  ll v;
  ll mod;

  Mint(ll v = 0, ll mod = ll(1e9) + 7) : mod(mod), v((v % mod + mod) % mod) {}

  friend std::ostream &operator<<(std::ostream &os, const Mint &val) {
    os << val.v;
    return os;
  }

  Mint operator-() const { return Mint(-v); }
  Mint operator+(const Mint rhs) const {
    Mint res(*this);
    return res += rhs;
  }
  Mint operator-(const Mint rhs) const {
    Mint res(*this);
    return res -= rhs;
  }
  Mint operator*(const Mint rhs) const {
    Mint res(*this);
    return res *= rhs;
  }
  Mint &operator+=(const Mint rhs) {
    if ((v += rhs.v) >= mod)
      v -= mod;
    return *this;
  }
  Mint &operator-=(const Mint rhs) {
    if ((v += mod - rhs.v) >= mod)
      v -= mod;
    return *this;
  }
  Mint &operator*=(const Mint rhs) {
    (v *= rhs.v) %= mod;
    return *this;
  }
  Mint pow(ll t) const {
    if (!t)
      return 1;
    Mint b = pow(t >> 1);
    b *= b;
    if (t & 1)
      b *= *this;
    return b;
  }
  Mint inv() const { return pow(mod - 2); }
  Mint &operator/=(const Mint rhs) { return (*this) *= rhs.inv(); }
  Mint operator/(const Mint rhs) const {
    Mint res(*this);
    return res /= rhs;
  }
};

} // namespace mint

#endif // INCLUDE_MINT_HPP

using mint::Mint;

class FKnapsackForAllSubsets {
public:
  int n, s;
  vll dp;
  vi a;
  vi visited;
  map<vector<int>, int> mp;
  Mint ans;

  void dfs(int d, vector<int> &st) {
    if (!d) {
      sort(st.begin(), st.end());
      if (mp.count(st))
        return;
      mp[st] = 1;
      ans += mt.powm(2LL, (n - st.size()));
      return;
    }
    for (auto &e : dp[d]) {
      if (visited[e])
        continue;
      st.push_back(e);
      visited[e] = 1;
      dfs(d - a[e], st);
      visited[e] = 0;
      st.erase(remove(st.begin(), st.end(), e), st.end());
    }
  }

  void solve(std::istream &in, std::ostream &out) {
    mod = 998244353;
    ans = Mint(0, mod);
    mt = math::Math(mod);
    in >> n >> s;
    a = vi(n);
    for (int i = 0; i < n; i++)
      in >> a[i];

    dp = vll(s + 1, vl());
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= s; j++) {
        if (j + a[i] <= s) {
          dp[j + a[i]].push_back(i);
        }
      }
    }

    vector<int> st;
    visited = vi(n);
    dfs(s, st);

    out << ans << endl;
  }
};

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  FKnapsackForAllSubsets solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}