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

#if 1 // use_templates

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, up) for (ll i = 0; i < up; ++i)
#define repp(i, up) for (ll i = 0; i <= up; ++i)
#define rrep(i, lo, up) for (ll i = lo; i < up; ++i)
#define rrepp(i, lo, up) for (ll i = lo; i <= up; ++i)
#define eep(i, lo, up) for ((i) = (lo); (i) < (up); ++(i))
#define eepp(i, lo, up) for ((i) = (lo); (i) <= (up); ++(i))

#define rdi(i)                                                                 \\
  ll i;                                                                        \\
  scanf("%lld", &i)
#define rds(s)                                                                 \\
  string s;                                                                    \\
  cin >> s
#define rdc(c)                                                                 \\
  char c;                                                                      \\
  cin >> c

#define INF 10000000000
#define MOD 998244353

ll gcd(ll a, ll b);
template <class T> auto fast_pow(T a, ll n);

template <class T, T n> struct mod_t {
  static_assert(n > 0, "invalid divisor");

  T value;

  constexpr mod_t() : value() {}
  constexpr mod_t(T x) : value(normalize(x)) {}
  constexpr mod_t(const mod_t &x) : value(normalize(x.value)) {}

  explicit constexpr operator T() const { return value; }
  constexpr auto operator+(mod_t<T, n> right) const {
    return mod_t<T, n>{(value + right.value) % n};
  }
  constexpr auto &operator+=(mod_t<T, n> right) {
    return *this = *this + right;
  }
  constexpr auto &operator++() { return *this = *this + mod_t<T, n>(1); }
  constexpr auto operator++(int) {
    decltype(*this) tmp(*this);
    *this = *this + mod_t<T, n>(1);
    return tmp;
  }
  constexpr auto operator-(mod_t<T, n> right) const {
    return mod_t<T, n>{(value - right.value + n) % n};
  }
  constexpr auto &operator-=(mod_t<T, n> right) {
    return *this = *this - right;
  }
  constexpr auto &operator--() { return *this = *this - mod_t<T, n>(1); }
  constexpr auto operator--(int) {
    decltype(*this) tmp(*this);
    *this = *this - mod_t<T, n>(1);
    return tmp;
  }
  constexpr auto operator*(mod_t<T, n> right) const {
    return mod_t<T, n>{(value * right.value) % n};
  }
  constexpr auto &operator*=(mod_t<T, n> right) {
    return *this = *this * right;
  }
  constexpr auto operator/(mod_t<T, n> right) const {
    return *this * right.pow(n - 2);
  }
  constexpr auto &operator/=(mod_t<T, n> right) {
    return *this = *this / right;
  }
  constexpr auto pow(ll nn) const { return fast_pow(*this, nn); }
  constexpr auto inv() const { return pow(n - 2); }

  static constexpr auto normalize(T x) {
    if (x < 0) {
      return (n + x % n) % n;
    } else {
      return x % n;
    }
  }
};
template <class T, T n> auto &operator>>(istream &in, mod_t<T, n> &out) {
  T tmp;
  in >> tmp;
  out.value = mod_t<T, n>::normalize(tmp);
  return in;
}
template <class T, T n> auto &operator<<(ostream &out, mod_t<T, n> val) {
  out << val.value;
  return out;
}
typedef mod_t<ll, MOD> mod;

template <class T, T n> class combi_t {
  using value_t = mod_t<T, n>;

  vector<value_t> factn;
  vector<value_t> ifactn;

  ll maxn;

public:
  combi_t() : factn(0), ifactn(0), maxn(0) {}

  combi_t(ll maxn) : factn(maxn + 1), ifactn(maxn + 1) { init(maxn); }

  void init(ll maxn) {
    factn.resize(maxn + 1);
    ifactn.resize(maxn + 1);

    this->maxn = maxn;
    factn[0] = 1;
    ifactn[0] = 1;
    for (ll i = 1; i <= maxn; ++i) {
      factn[i] = i * factn[i - 1];
      ifactn[i] = value_t(i).inv() * ifactn[i - 1];
    }
  }

  constexpr auto combi(ll a, ll b) const {
    if (b <= 0 || a - b <= 0) {
      return value_t(1);
    } else {
      return factn[a] * ifactn[b] * ifactn[a - b];
    }
  }

  constexpr auto perm(ll a, ll b) const { return factn[a] * ifactn[a - b]; }

  constexpr auto operator()(ll a, ll b) const { return combi(a, b); }
};
#define USE_COMBI(maxn) combi_t<ll, MOD> combi(maxn);

template <class T> auto fast_pow(T a, ll n) {
  if (n <= 0) {
    return T(1);
  } else if (n == 1) {
    return T(a);
  } else if (n % 2 == 0) {
    auto work = fast_pow(a, n / 2);
    return work * work;
  } else {
    return fast_pow(a, n - 1) * a;
  }
}

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll r;
  while (r = a % b) {
    a = b;
    b = r;
  }
  return b;
}

template <class T> void read_vec(vector<T> &v, ll n) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    cin >> v[i];
  }
}
template <> void read_vec<ll>(vector<ll> &v, ll n) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    scanf("%lld", &v[i]);
  }
}
template <class T> auto read_vec(ll n) {
  vector<T> result;
  read_vec(result, n);
  return result;
}
void read_idxs(vector<ll> &v, ll n) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    scanf("%lld", &v[i]);
    --v[i];
  }
}
auto read_idxs(ll n) {
  vector<ll> result(n);
  for (ll i = 0; i < n; ++i) {
    scanf("%lld", &result[i]);
    --result[i];
  }
  return result;
}
template <class T> void read_mat(vector<vector<T>> &v, ll n, ll m) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    read_vec(v[i], m);
  }
}
template <class T> auto read_mat(ll n, ll m) {
  vector<T> result(n);
  for (ll i = 0; i < n; ++i) {
    read_vec(result[i], m);
  }
  return result;
}

#endif // end of use_templates

int main() {
  ll n, s;
  cin >> n >> s;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<mod> dp(s + 1, 0);
  mod inv2 = mod(1) / 2;
  dp[0] = mod(2).pow(n);
  rep(i, n) {
    for (ll j = s; j >= 0; --j) {
      if (j >= a[i]) {
        dp[j] += dp[j - a[i]] * inv2;
      }
    }
  }
  cout << dp[s] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#if 1 // use_templates

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, up) for (ll i = 0; i < up; ++i)
#define repp(i, up) for (ll i = 0; i <= up; ++i)
#define rrep(i, lo, up) for (ll i = lo; i < up; ++i)
#define rrepp(i, lo, up) for (ll i = lo; i <= up; ++i)
#define eep(i, lo, up) for ((i) = (lo); (i) < (up); ++(i))
#define eepp(i, lo, up) for ((i) = (lo); (i) <= (up); ++(i))

#define rdi(i)                                                                 \\
  ll i;                                                                        \\
  scanf("%lld", &i)
#define rds(s)                                                                 \\
  string s;                                                                    \\
  cin >> s
#define rdc(c)                                                                 \\
  char c;                                                                      \\
  cin >> c

#define INF 10000000000
#define MOD 998244353

ll gcd(ll a, ll b);
template <class T> auto fast_pow(T a, ll n);

template <class T, T n> struct mod_t {
  static_assert(n > 0, "invalid divisor");

  T value;

  constexpr mod_t() : value() {}
  constexpr mod_t(T x) : value(normalize(x)) {}
  constexpr mod_t(const mod_t &x) : value(normalize(x.value)) {}

  explicit constexpr operator T() const { return value; }
  constexpr auto operator+(mod_t<T, n> right) const {
    return mod_t<T, n>{(value + right.value) % n};
  }
  constexpr auto &operator+=(mod_t<T, n> right) {
    return *this = *this + right;
  }
  constexpr auto &operator++() { return *this = *this + mod_t<T, n>(1); }
  constexpr auto operator++(int) {
    decltype(*this) tmp(*this);
    *this = *this + mod_t<T, n>(1);
    return tmp;
  }
  constexpr auto operator-(mod_t<T, n> right) const {
    return mod_t<T, n>{(value - right.value + n) % n};
  }
  constexpr auto &operator-=(mod_t<T, n> right) {
    return *this = *this - right;
  }
  constexpr auto &operator--() { return *this = *this - mod_t<T, n>(1); }
  constexpr auto operator--(int) {
    decltype(*this) tmp(*this);
    *this = *this - mod_t<T, n>(1);
    return tmp;
  }
  constexpr auto operator*(mod_t<T, n> right) const {
    return mod_t<T, n>{(value * right.value) % n};
  }
  constexpr auto &operator*=(mod_t<T, n> right) {
    return *this = *this * right;
  }
  constexpr auto operator/(mod_t<T, n> right) const {
    return *this * right.pow(n - 2);
  }
  constexpr auto &operator/=(mod_t<T, n> right) {
    return *this = *this / right;
  }
  constexpr auto pow(ll nn) const { return fast_pow(*this, nn); }
  constexpr auto inv() const { return pow(n - 2); }

  static constexpr auto normalize(T x) {
    if (x < 0) {
      return (n + x % n) % n;
    } else {
      return x % n;
    }
  }
};
template <class T, T n> auto &operator>>(istream &in, mod_t<T, n> &out) {
  T tmp;
  in >> tmp;
  out.value = mod_t<T, n>::normalize(tmp);
  return in;
}
template <class T, T n> auto &operator<<(ostream &out, mod_t<T, n> val) {
  out << val.value;
  return out;
}
typedef mod_t<ll, MOD> mod;

template <class T, T n> class combi_t {
  using value_t = mod_t<T, n>;

  vector<value_t> factn;
  vector<value_t> ifactn;

  ll maxn;

public:
  combi_t() : factn(0), ifactn(0), maxn(0) {}

  combi_t(ll maxn) : factn(maxn + 1), ifactn(maxn + 1) { init(maxn); }

  void init(ll maxn) {
    factn.resize(maxn + 1);
    ifactn.resize(maxn + 1);

    this->maxn = maxn;
    factn[0] = 1;
    ifactn[0] = 1;
    for (ll i = 1; i <= maxn; ++i) {
      factn[i] = i * factn[i - 1];
      ifactn[i] = value_t(i).inv() * ifactn[i - 1];
    }
  }

  constexpr auto combi(ll a, ll b) const {
    if (b <= 0 || a - b <= 0) {
      return value_t(1);
    } else {
      return factn[a] * ifactn[b] * ifactn[a - b];
    }
  }

  constexpr auto perm(ll a, ll b) const { return factn[a] * ifactn[a - b]; }

  constexpr auto operator()(ll a, ll b) const { return combi(a, b); }
};
#define USE_COMBI(maxn) combi_t<ll, MOD> combi(maxn);

template <class T> auto fast_pow(T a, ll n) {
  if (n <= 0) {
    return T(1);
  } else if (n == 1) {
    return T(a);
  } else if (n % 2 == 0) {
    auto work = fast_pow(a, n / 2);
    return work * work;
  } else {
    return fast_pow(a, n - 1) * a;
  }
}

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll r;
  while (r = a % b) {
    a = b;
    b = r;
  }
  return b;
}

template <class T> void read_vec(vector<T> &v, ll n) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    cin >> v[i];
  }
}
template <> void read_vec<ll>(vector<ll> &v, ll n) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    scanf("%lld", &v[i]);
  }
}
template <class T> auto read_vec(ll n) {
  vector<T> result;
  read_vec(result, n);
  return result;
}
void read_idxs(vector<ll> &v, ll n) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    scanf("%lld", &v[i]);
    --v[i];
  }
}
auto read_idxs(ll n) {
  vector<ll> result(n);
  for (ll i = 0; i < n; ++i) {
    scanf("%lld", &result[i]);
    --result[i];
  }
  return result;
}
template <class T> void read_mat(vector<vector<T>> &v, ll n, ll m) {
  v.resize(n);
  for (ll i = 0; i < n; ++i) {
    read_vec(v[i], m);
  }
}
template <class T> auto read_mat(ll n, ll m) {
  vector<T> result(n);
  for (ll i = 0; i < n; ++i) {
    read_vec(result[i], m);
  }
  return result;
}

#endif // end of use_templates

int main() {
  ll n, s;
  cin >> n >> s;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<mod> dp(s + 1, 0);
  dp[0] = mod(2).pow(n);
  rep(i, n) {
    for (ll j = s; j >= 0; --j) {
      if (j >= a[i]) {
        dp[j] += dp[j - a[i]] / 2;
      }
    }
  }
  cout << dp[s] << endl;
  return 0;
}