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>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 998244353
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll dp[3005][3005];

ll bin(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res % mod * (a % mod)) % mod;
    res %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return res % mod;
}

ll inv(ll a) { return bin(a, mod - 2); }

int main() {
  fastio int n, s;
  cin >> n >> s;
  vector<int> vec(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> vec[i];

  dp[0][0] = bin(2, n) % mod;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= s; ++j) {
      if (dp[i - 1][j] > 0) {
        dp[i][j] += dp[i - 1][j];
        if (j + vec[i] <= s) {
          dp[i][j + vec[i]] += (dp[i - 1][j] % mod) * inv(2);
          dp[i][j + vec[i]] %= mod;
        }
      }
    }
  }

  cout << dp[n][s] % mod << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 998244353
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

map<ll, ll> mp;
vector<ll> vec;
ll n, s;

void solve(int ind, ll curSum, int len) {
  if (ind > n)
    return;
  if (curSum < 0)
    return;
  if (ind == n) {
    if (curSum == 0) {
      mp[n - len]++;
    }
    return;
  }

  solve(ind + 1, curSum - vec[ind], len++);
  solve(ind + 1, curSum, len);
}

ll bin(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = res % mod * (a % mod) % mod;
    b >>= 1;
    a *= a;
    a %= mod;
  }
  return res % mod;
}

int main() {
  fastio cin >> n >> s;
  vec.assign(n, 0);
  for (auto &it : vec)
    cin >> it;
  sort(all(vec));

  solve(0, s, 0);

  ll ans = 0;
  for (auto &it : mp) {
    ll first = it.first;
    ll sec = it.second;
    ans = ans % mod + ((bin(2, (n - first)) % mod) * (sec % mod)) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}