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

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

template <long long MOD> class modint {
  long long x;

public:
  modint(long long x = 0) : x((x % MOD + MOD) % MOD) {}

  modint operator-() { return modint(-x); }

  modint operator+(const modint &other) { return modint(*this) += other; }

  modint &operator+=(const modint &other) {
    x = x + other.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  modint operator-(const modint &other) { return modint(*this) -= other; }

  modint &operator-=(const modint &other) {
    x = x - other.x + MOD;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  modint operator*(const modint &other) { return modint(*this) *= other; }

  modint &operator*=(const modint &other) {
    x = (x * other.x) % MOD;
    return *this;
  }

  modint operator/(const modint &other) { return modint(*this) /= other; }

  modint &operator/=(const modint &other) {
    this *= other ^ (MOD - 2);
    return *this;
  }

  modint operator^(long long b) {
    if (b == 0)
      return 1;
    modint ret = *this ^ (b / 2);
    ret = ret * ret;
    if (b % 2 == 1)
      ret = *this * ret;
    return ret;
  }

  long long value() { return x; }

  friend istream &operator>>(istream &in, modint &a) {
    long long b;
    in >> b;
    a = modint(b);
    return in;
  }

  friend ostream &operator<<(ostream &out, modint a) {
    out << a.x;
    return out;
  }
};

const ll MOD = 998244353;
using mint = modint<MOD>;
const int MAX_N = 3000;
int n;
ll s;
ll a[MAX_N];
mint dp[MAX_N + 1][MAX_N + 1];

int main() {
  fastio();

  cin >> n >> s;
  forn(i, n) cin >> a[i];

  dp[0][0] = 1;
  forn1(i, n) {
    forn(j, s + 1) {
      dp[i][j] += dp[i - 1][j] * 2;
      if (j + a[i - 1] <= s) {
        dp[i][j + a[i - 1]] += dp[i - 1][j];
      }
    }
  }

  cout << dp[n][s] << endl;

  return 0;
}


Error: TLE

Fixed submission:

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

template <long long mod> long long mod_pow(long long a, long long b) {
  if (b == 0)
    return 1;
  long long ret = mod_pow<mod>(a, b / 2);
  ret = ret * ret % mod;
  if (b % 2 == 1)
    ret = a * ret % mod;
  return ret;
}

const ll MOD = 998244353;
const int MAX_N = 3000;
int n;
ll s;
ll a[MAX_N];

int main() {
  fastio();

  cin >> n >> s;
  forn(i, n) cin >> a[i];

  ll ans = 0;

  map<pll, ll> cnt;
  cnt[mp(0, 0)] = 1;
  forn(i, n) {
    map<pll, ll> temp;

    for (auto [p, c] : cnt) {
      temp[p] = (temp[p] + c) % MOD;

      auto [x, y] = p;
      if (x + a[i] <= s) {
        temp[mp(x + a[i], y + 1)] += c;
      }
    }

    cnt = temp;
  }

  for (auto [x, c] : cnt) {
    if (x.fst == s) {
      // cout << c << " " << x.snd << endl;
      ans = (ans + c * mod_pow<MOD>(2, n - x.snd) % MOD) % MOD;
    }
  }

  cout << ans << endl;

  return 0;
}