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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll, ll>
#define ppp pair<ll, pp>
#define trace(...)                                                             \\
  cout << "Line:" << __LINE__ << " ";                                          \\
  __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (ll i = a; i < b; i += 1)
#define sz(v) (ll)(v.size())
#define hell (ll)998244353
#define slld(x) scanf("%lld", &x)
ll t = 1, tt;
const ll nax = 3005, N = nax;
ll dp[3005][3005];
ll fact[N], invFact[N];
ll add(ll x, ll y) {
  x += y;
  return x % hell;
}
ll mult(ll x, ll y) { return (1LL * x * y) % hell; }
ll powMod(ll base, ll expo) {
  ll ans = 1;
  while (expo) {
    if (expo & 1)
      ans = mult(ans, base);
    base = mult(base, base);
    expo >>= 1;
  }
  return ans;
}
ll C(ll x, ll y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return mult(fact[x], mult(invFact[y], invFact[x - y]));
}
ll get(ll x) { return powMod(powMod(2, x), hell - 2); }
void solve() {
  ll n, s;
  cin >> n >> s;
  vector<ll> v(n), cnt(nax, 0);
  rep(i, 1, n + 1) {
    cin >> v[i - 1];
    cnt[v[i - 1]]++;
  }
  dp[0][0] = powMod(2, n);
  rep(i, 1, n + 1) {
    rep(j, 0, nax) {
      dp[i][j] = add(dp[i - 1][j], dp[i][j]);
      if (j + v[i - 1] < nax)
        dp[i][j + v[i - 1]] =
            add(dp[i][j + v[i - 1]], mult(dp[i - 1][j], get(1)));
    }
  }
  cout << dp[n][s];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fact[0] = 1;
  rep(i, 1, N) fact[i] = mult(fact[i - 1], i);
  invFact[N - 1] = powMod(fact[N - 1], hell - 2);
  for (ll i = N - 2; i >= 0; i--)
    invFact[i] = mult(invFact[i + 1], i + 1);
  // cin>>t;
  tt = t;
  while (t--) {
    solve();
    if (t)
      cout << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll, ll>
#define ppp pair<ll, pp>
#define trace(...)                                                             \\
  cout << "Line:" << __LINE__ << " ";                                          \\
  __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (ll i = a; i < b; i += 1)
#define sz(v) (ll)(v.size())
#define hell (ll)998244353
#define slld(x) scanf("%lld", &x)
ll t = 1, tt;
const ll nax = 3005, N = nax;
ll dp[3005][3005];
ll fact[N], invFact[N];
ll add(ll x, ll y) {
  x += y;
  return x % hell;
}
ll mult(ll x, ll y) { return (1LL * x * y) % hell; }
ll powMod(ll base, ll expo) {
  ll ans = 1;
  while (expo) {
    if (expo & 1)
      ans = mult(ans, base);
    base = mult(base, base);
    expo >>= 1;
  }
  return ans;
}
ll C(ll x, ll y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return mult(fact[x], mult(invFact[y], invFact[x - y]));
}
ll get(ll x) { return powMod(powMod(2, x), hell - 2); }
void solve() {
  ll n, s;
  cin >> n >> s;
  vector<ll> v(n), cnt(nax, 0);
  rep(i, 1, n + 1) {
    cin >> v[i - 1];
    cnt[v[i - 1]]++;
  }
  dp[0][0] = powMod(2, n);
  //~ vector<ll> val(nax,0);
  //~ val[0]=1;
  rep(i, 1, nax) {
    //~ for(ll curSum=0;curSum<nax;curSum++){
    for (ll j = 0; j <= cnt[i]; j++) {
      for (ll curSum = 0; curSum < nax; curSum++) {
        if (curSum + j * i < nax) {
          dp[i][curSum + j * i] =
              add(dp[i][curSum + j * i],
                  mult(dp[i - 1][curSum], mult(C(cnt[i], j), get(j))));
          //~ val[curSum+i*j]=add(val[curSum+i*j],val[curSum]);
        }
      }
    }
  }
  cout << dp[nax - 1][s];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fact[0] = 1;
  rep(i, 1, N) fact[i] = mult(fact[i - 1], i);
  invFact[N - 1] = powMod(fact[N - 1], hell - 2);
  for (ll i = N - 2; i >= 0; i--)
    invFact[i] = mult(invFact[i + 1], i + 1);
  // cin>>t;
  tt = t;
  while (t--) {
    solve();
    if (t)
      cout << "\\n";
  }
  return 0;
}