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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
#define ll long long
#define vi vector<ll>
// -------------------Standard Traversal Moves---------------------
// vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2,  1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------

#define int long long
#define pb push_back
#define mii map<ll, ll>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#ifdef LOCAL
#define dbg(args...)                                                           \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#else
#define dbg(args...)
#endif
#define pii pair<ll, ll>
#define ppb pop_back
#define vii vector<pair<ll, ll>>
#define si set<ll>
// #define hell               1000000007
#define hell (ll)998244353
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define mp make_pair
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define inf 3223372036854775807
#define endl '\\n'
#define PI 3.14159265
#define N 3005
ll n, m, k, a[N];
int dp[N][N];
ll powe(ll x, ll y) {
  ll res = 1;

  x = x % hell;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % hell;

    y = y >> 1;
    x = (x * x) % hell;
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ll tests = 1;
  // cin >> tests;
  rep(test, 1, tests + 1) {
    cin >> n;
    cin >> m;
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n + 1) { rep(j, 0, m + 1) dp[i][j] = 0; }
    dp[0][0] = 1;
    rep(i, 1, n + 1) {
      rep(j, 0, m + 1)(dp[i][j] = 2 * dp[i - 1][j]) %= hell;
      rep(j, 0, m - a[i - 1] + 1) {
        dp[i][j + a[i - 1]] += dp[i - 1][j];
        dp[i][j + a[i - 1]] %= hell;
      }
    }
    // rep(i, 1, n + 1)
    // {
    // 	rep(j ,0, m + 1)
    // 	{
    // 		cout << dp[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    cout << dp[n][m];
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
#define ll long long
#define vi vector<ll>
// -------------------Standard Traversal Moves---------------------
// vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2,  1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------

#define int long long
#define pb push_back
#define mii map<ll, ll>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#ifdef LOCAL
#define dbg(args...)                                                           \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#else
#define dbg(args...)
#endif
#define pii pair<ll, ll>
#define ppb pop_back
#define vii vector<pair<ll, ll>>
#define si set<ll>
// #define hell               1000000007
#define hell (ll)998244353
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define mp make_pair
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define inf 3223372036854775807
#define endl '\\n'
#define PI 3.14159265
#define N 10000
ll n, m, k, a[N];
int v[N][N], cnt[N];
ll fac[N];
ll powe(ll x, ll y) {
  ll res = 1;

  x = x % hell;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % hell;

    y = y >> 1;
    x = (x * x) % hell;
  }
  return res;
}

ll modInverse(ll n) { return powe(n, hell - 2); }

void fact() {
  fac[0] = 1;
  rep(i, 1, N) fac[i] = fac[i - 1] * i % hell;
}
// fact();   //just put it before the while loop so that all the factorials are
// computed

ll ncrmodp(ll n, ll r) {
  if (r == 0)
    return 1;
  return (fac[n] * modInverse(fac[r]) % hell * modInverse(fac[n - r]) % hell) %
         hell;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ll tests = 1;
  // cin >> tests;
  rep(test, 1, tests + 1) {
    cin >> n;
    cin >> m;
    fact();
    rep(i, 0, n) cin >> a[i];
    v[0][0] = 1;
    cnt[0] = 1;
    rep(j, 0, n) for (int i = m - a[j]; i > -1; i--) {
      if (cnt[i] and i + a[j] <= m) {
        rep(l, 0, j + 1) {
          if (v[i][l])
            cnt[i + a[j]] = 1;
          v[i + a[j]][l + 1] += v[i][l];
          v[i + a[j]][l + 1] %= hell;
        }
      }
    }
    int ans = 0;
    rep(i, 1, n + 1) {
      ans += powe(2, n - i) * v[m][i];
      ans %= hell;
      // }
    }
    cout << ans;
  }
  return 0;
}