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

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

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  string sep = " ";
  if (v.size())
    os << v[0];
  for (int i = 1; i < v.size(); i++)
    os << sep << v[i];
  return os;
}

#ifdef DBG
void debug_() { cout << endl; }
template <typename T, typename... Args> void debug_(T &&x, Args &&...xs) {
  cout << x << " ";
  debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif

// Extended Euclid's greatest common divisor algorithm
// Find (x, y, g)
// where $a*x + b*y = g$ and g is the greatest common divisor of (a, b)
long long ext_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long nx, ny;
  long long g = ext_gcd(b, a % b, nx, ny);
  x = ny;
  y = nx - a / b * ny;
  return g;
}

struct mint {
  typedef long long ll;
  ll x, m;
  mint(ll x = 0, ll m = 998244353) : x((m + x % m) % m), m(m) {}
  mint operator-() const { return mint(m - x, m); }
  mint &operator+=(const mint o) {
    x = (x + o.x) % m;
    return *this;
  }
  mint &operator+=(const ll o) { return (*this) += mint(o, m); }
  mint &operator-=(const mint o) { return (*this) += (-o); }
  mint &operator-=(const ll o) { return (*this) -= mint(o, m); }
  mint &operator*=(const mint o) {
    x = x * o.x % m;
    return *this;
  }
  mint &operator*=(const ll o) { return (*this) *= mint(o, m); }
  mint &operator/=(const mint o) { return (*this) *= o.inv(); }
  mint &operator/=(const ll o) { return (*this) /= mint(o, m); }
  friend mint operator+(mint l, const mint r) {
    l += r;
    return l;
  }
  friend mint operator+(mint l, const ll r) {
    l += r;
    return l;
  }
  friend mint operator+(const ll l, mint r) {
    r += l;
    return r;
  }
  friend mint operator-(mint l, const mint r) {
    l -= r;
    return l;
  }
  friend mint operator-(mint l, const ll r) {
    l -= r;
    return l;
  }
  friend mint operator-(const ll l, mint r) {
    r -= l;
    return r;
  }
  friend mint operator*(mint l, const mint r) {
    l *= r;
    return l;
  }
  friend mint operator*(mint l, const ll r) {
    l *= r;
    return l;
  }
  friend mint operator*(const ll l, mint r) {
    r *= l;
    return r;
  }
  friend mint operator/(mint l, const mint r) {
    l /= r;
    return l;
  }
  friend mint operator/(mint l, const ll r) {
    l /= r;
    return l;
  }
  friend mint operator/(const ll l, const mint r) { return mint(l, r.m) / r; }
  bool operator==(const mint o) const { return x == o.x; }
  bool operator!=(const mint o) const { return x != o.x; }
  friend bool operator==(const ll l, const mint r) { return mint(l, r.m) == r; }
  friend bool operator!=(const ll l, const mint r) { return mint(l, r.m) != r; }
  friend std::ostream &operator<<(std::ostream &os, const mint x) {
    return os << x.x;
  }

  mint pow(ll k) const {
    if (k == 0)
      return mint(1, m);
    if (k % 2)
      return pow(k - 1) * x;
    mint z = pow(k / 2);
    return z * z;
  }

  mint inv() const {
    ll y, z;
    ext_gcd(x, m, y, z);
    return mint(y, m);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cout << setprecision(20) << fixed;

  int n, s;
  cin >> n >> s;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<mint> dp(s + 1);
  dp[0] = mint(2).pow(n);
  for (int i = 0; i < n; i++) {
    vector<mint> ndp(dp);

    for (int j = 0; j < s; j++) {
      if (j + a[i] <= s) {
        ndp[j + a[i]] += dp[j] / 2;
      }
    }
    dp = ndp;
  }

  cout << dp[s] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  string sep = " ";
  if (v.size())
    os << v[0];
  for (int i = 1; i < v.size(); i++)
    os << sep << v[i];
  return os;
}

#ifdef DBG
void debug_() { cout << endl; }
template <typename T, typename... Args> void debug_(T &&x, Args &&...xs) {
  cout << x << " ";
  debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif

// Extended Euclid's greatest common divisor algorithm
// Find (x, y, g)
// where $a*x + b*y = g$ and g is the greatest common divisor of (a, b)
long long ext_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long nx, ny;
  long long g = ext_gcd(b, a % b, nx, ny);
  x = ny;
  y = nx - a / b * ny;
  return g;
}

struct mint {
  typedef long long ll;
  ll x, m;
  mint(ll x = 0, ll m = 998244353) : x((m + x % m) % m), m(m) {}
  mint operator-() const { return mint(m - x, m); }
  mint &operator+=(const mint o) {
    x = (x + o.x) % m;
    return *this;
  }
  mint &operator+=(const ll o) { return (*this) += mint(o, m); }
  mint &operator-=(const mint o) { return (*this) += (-o); }
  mint &operator-=(const ll o) { return (*this) -= mint(o, m); }
  mint &operator*=(const mint o) {
    x = x * o.x % m;
    return *this;
  }
  mint &operator*=(const ll o) { return (*this) *= mint(o, m); }
  mint &operator/=(const mint o) { return (*this) *= o.inv(); }
  mint &operator/=(const ll o) { return (*this) /= mint(o, m); }
  friend mint operator+(mint l, const mint r) {
    l += r;
    return l;
  }
  friend mint operator+(mint l, const ll r) {
    l += r;
    return l;
  }
  friend mint operator+(const ll l, mint r) {
    r += l;
    return r;
  }
  friend mint operator-(mint l, const mint r) {
    l -= r;
    return l;
  }
  friend mint operator-(mint l, const ll r) {
    l -= r;
    return l;
  }
  friend mint operator-(const ll l, mint r) {
    r -= l;
    return r;
  }
  friend mint operator*(mint l, const mint r) {
    l *= r;
    return l;
  }
  friend mint operator*(mint l, const ll r) {
    l *= r;
    return l;
  }
  friend mint operator*(const ll l, mint r) {
    r *= l;
    return r;
  }
  friend mint operator/(mint l, const mint r) {
    l /= r;
    return l;
  }
  friend mint operator/(mint l, const ll r) {
    l /= r;
    return l;
  }
  friend mint operator/(const ll l, const mint r) { return mint(l, r.m) / r; }
  bool operator==(const mint o) const { return x == o.x; }
  bool operator!=(const mint o) const { return x != o.x; }
  friend bool operator==(const ll l, const mint r) { return mint(l, r.m) == r; }
  friend bool operator!=(const ll l, const mint r) { return mint(l, r.m) != r; }
  friend std::ostream &operator<<(std::ostream &os, const mint x) {
    return os << x.x;
  }

  mint pow(ll k) const {
    if (k == 0)
      return mint(1, m);
    if (k % 2)
      return pow(k - 1) * x;
    mint z = pow(k / 2);
    return z * z;
  }

  mint inv() const {
    ll y, z;
    ext_gcd(x, m, y, z);
    return mint(y, m);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cout << setprecision(20) << fixed;

  int n, s;
  cin >> n >> s;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<map<int, mint>> dp(s + 1);
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    vector<map<int, mint>> ndp(dp);

    for (int j = 0; j < s; j++) {
      if (j + a[i] <= s) {
        for (auto p : dp[j]) {
          ndp[j + a[i]][p.first + 1] += p.second;
        }
      }
    }
    dp = ndp;
  }

  mint ans(0);
  for (int i = 1; i <= n; i++) {
    ans += dp[s][i] * mint(2).pow(n - i);
  }

  cout << ans << endl;

  return 0;
}