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

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 05/31/20 18:19:58
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T, T MOD> class modint {
private:
  T val_;

public:
  constexpr modint() : val_(0) {}
  constexpr modint(T v) : val_(v % MOD) {}
  constexpr modint &operator=(T v) {
    val_ = v % MOD;
    return *this;
  }

  constexpr T val() const { return val_; }
  constexpr modint operator+(modint x) const { return (val() + x.val()) % MOD; }
  constexpr modint operator+=(modint x) { return *this = *this + x; }
  constexpr modint operator++() { return *this += 1; }
  constexpr modint operator++(int) {
    modint c = *this;
    ++(*this);
    return c;
  }
  constexpr modint operator-(modint x) const {
    return (val() + MOD - x.val()) % MOD;
  }
  constexpr modint operator-=(modint x) { return *this = *this - x; }
  constexpr modint operator--() { return *this -= 1; }
  constexpr modint operator--(int) {
    modint c = *this;
    --(*this);
    return c;
  }
  constexpr modint operator*(modint x) const { return (val() * x.val()) % MOD; }
  constexpr modint operator*=(modint x) { return *this = *this * x; }
  friend constexpr modint pow(modint x, T p) {
    modint t = 1;
    modint u = x;
    while (p) {
      if (p & 1)
        t *= u;
      u *= u;
      p >>= 1;
    }
    return t;
  }
  constexpr modint operator/(modint x) const {
    return *this * pow(modint{x}, MOD - 2);
  }
  constexpr modint operator/=(modint x) { return *this = *this / x; }

  constexpr bool operator==(modint x) const { return val() == x.val(); }
  constexpr bool operator!=(modint x) const { return val() != x.val(); }
  constexpr bool operator<(modint x) const { return val() < x.val(); };
  constexpr bool operator<=(modint x) const { return val() <= x.val(); };
  constexpr bool operator>(modint x) const { return val() > x.val(); };
  constexpr bool operator>=(modint x) const { return val() >= x.val(); };

  friend std::ostream &operator<<(std::ostream &os, const modint &x) {
    return os << x.val();
  }
  friend std::istream &operator>>(std::istream &is, modint &x) {
    return is >> x.val_;
  }
};

using mint = modint<ll, 998244353>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, s;
  cin >> n >> s;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  vector<mint> dp(s + 1);
  dp[0] = pow(mint(2), n);
  for (int i = 0; i < n; i++) {
    for (int t = s - A[i]; t >= 0; t--) {
      dp[t + A[i]] += dp[t] / 2;
    }
  }
  cout << dp[s] << endl;

  return 0;
}


Error: TLE

Fixed submission:

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 05/31/20 18:19:58
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T, T MOD> class modint {
private:
  T val_;

public:
  constexpr modint() : val_(0) {}
  constexpr modint(T v) : val_(v % MOD) {}
  constexpr modint &operator=(T v) {
    val_ = v % MOD;
    return *this;
  }

  constexpr T val() const { return val_; }
  constexpr modint operator+(modint x) const { return (val() + x.val()) % MOD; }
  constexpr modint operator+=(modint x) { return *this = *this + x; }
  constexpr modint operator++() { return *this += 1; }
  constexpr modint operator++(int) {
    modint c = *this;
    ++(*this);
    return c;
  }
  constexpr modint operator-(modint x) const {
    return (val() + MOD - x.val()) % MOD;
  }
  constexpr modint operator-=(modint x) { return *this = *this - x; }
  constexpr modint operator--() { return *this -= 1; }
  constexpr modint operator--(int) {
    modint c = *this;
    --(*this);
    return c;
  }
  constexpr modint operator*(modint x) const { return (val() * x.val()) % MOD; }
  constexpr modint operator*=(modint x) { return *this = *this * x; }
  friend constexpr modint pow(modint x, T p) {
    modint t = 1;
    modint u = x;
    while (p) {
      if (p & 1)
        t *= u;
      u *= u;
      p >>= 1;
    }
    return t;
  }
  constexpr modint operator/(modint x) const {
    return *this * pow(modint{x}, MOD - 2);
  }
  constexpr modint operator/=(modint x) { return *this = *this / x; }

  constexpr bool operator==(modint x) const { return val() == x.val(); }
  constexpr bool operator!=(modint x) const { return val() != x.val(); }
  constexpr bool operator<(modint x) const { return val() < x.val(); };
  constexpr bool operator<=(modint x) const { return val() <= x.val(); };
  constexpr bool operator>(modint x) const { return val() > x.val(); };
  constexpr bool operator>=(modint x) const { return val() >= x.val(); };

  friend std::ostream &operator<<(std::ostream &os, const modint &x) {
    return os << x.val();
  }
  friend std::istream &operator>>(std::istream &is, modint &x) {
    return is >> x.val_;
  }
};

using mint = modint<ll, 998244353>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, s;
  cin >> n >> s;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  vector<unordered_map<int, mint>> dp(n + 1);
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int c = i; c >= 0; c--) {
      for (auto t : dp[c]) {
        if (t.first + A[i] > s)
          continue;
        dp[c + 1][t.first + A[i]] += t.second;
      }
    }
  }
  //  for (int c = 0; c <= n; c++) {
  //    for (int t = 0; t <= s; t++) {
  //      cerr<<dp[c][t]<<" ";
  //    }
  //    cerr<<endl;
  //  }
  mint ans = 0;
  for (int c = 1; c <= n; c++) {
    ans += dp[c][s] * pow(mint(2), n - c);
  }
  cout << ans << endl;

  return 0;
}