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

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <math.h>
using namespace __gnu_pbds;
using namespace std::chrono;
using namespace std;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// declaration: ordered_set os;  methods->  1. *os.find_by_order(x)   2.
// os.order_of_key(k) ->return int

#define fr(x, i, n) for (int x = i; x < n; ++x)
#define fb(x, i, n) for (int x = i; x >= n; --x)
#define pr pair<int, int>
#define ll long long int
#define ld long double
const ll mod = 998244353;
ll f(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return r;
}
void solve() {
  int n, s;
  cin >> n >> s;
  int a[n];
  fr(i, 0, n) cin >> a[i];
  ll dp[n][s + 1]; // no of non empty subsets
  // dp[i][j]...subset containing elements only upto 0..i and sum=j
  memset(dp, 0, sizeof(dp));
  if (a[0] <= s)
    dp[0][a[0]] = 1;
  fr(i, 0, n) {
    dp[i][0] =
        f(2, i + 1); // -1 is not added because whenever it is in use...its
                     // empty subset also counted since it is only counted when
                     // some a[i] needs to be attached to this
  }
  fr(i, 1, n) {
    fr(j, 1, s + 1) {
      dp[i][j] = (dp[i - 1][j] + dp[i - 1][j]) %
                 mod; // to all those subsets....either a[i] is added or not
                      // added...giving two different subsets
      if (j - a[i] >= 0)
        dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i]]) % mod;
    }
  }
  /*    fr(i,0,n){
          fr(j,0,s+1)
          cout<<dp[i][j]<<" ";
          cout<<endl;
      }*/
  cout << dp[n - 1][s];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  //    build();
  int t = 1;
  //    cin>>t;
  while (t--)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std::chrono;
using namespace std;
#define ordered_set                                                            \\
  tree<pr, null_type, less<pr>, rb_tree_tag, tree_order_statistics_node_update>
// declaration: ordered_set os;  methods->  1. *os.find_by_order(x)   2.
// os.order_of_key(k) ->return int

#define fr(x, i, n) for (int x = i; x < n; ++x)
#define fb(x, i, n) for (int x = i; x >= n; --x)
#define pr pair<ll, int>
#define ll long long int
#define ld long double
const ll mod = 998244353;

// const ll mod=1e9+7;
ll f(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n % 2 == 1)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n /= 2;
  }
  return r;
}
ll p2[3001];
void power2() {
  p2[0] = 1;
  fr(i, 1, 3001) { p2[i] = (p2[i - 1] * 2LL) % mod; }
}
void solve() {
  power2();
  int n, s;
  cin >> n >> s;
  int a[n];
  fr(i, 0, n) cin >> a[i];
  map<int, ll> mp[3001];
  mp[a[0]][1]++;
  mp[0][0]++;
  fr(i, 1, n) {
    fb(j, s, 1) {
      if (j - a[i] >= 0)
        for (auto p : mp[j - a[i]]) {
          mp[j][p.first + 1] = (mp[j][p.first + 1] + p.second) % mod;
        }
    }
  }
  ll ans = 0;
  for (auto p : mp[s]) {
    ll len = p.first;
    ll cnt = p.second;
    //    cout<<"len="<<len<<" count="<<cnt<<endl;
    ans = (ans + (cnt * p2[n - len] % mod)) % mod;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  //    build();
  int t = 1;
  //    cin>>t;
  while (t--)
    solve();
}