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

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#ifdef __debug_leywart
#include <debug/to_string.h>
#else
#define db1(x) ;
#define db2(x, begin, end) ;
#define db3(x) ;
#define db4(x, begin, end) ;
#define dbmat1(x, begin_v, end_v, begin_h, end_h) ;
#define dbmat2(x, begin_v, end_v, begin_h, end_h) ;
#define dbin1(x) ;
#define dbin2(x, begin, end) ;
#define dbtree(x, size) ;
#endif
using namespace std;
#define F first
#define S second
#define int long long

/// My functions
void sread(string &s) {
  cin >> s;
  s = '_' + s;
}
void sprint(string &s) { cout << s.substr(1, s.size()); }

// Constants
const int mxn = 3005, inf = 1e9 + 7, mod = 998244353;
const long long infLL = 1e18 + 5;
const double EPS = 1e-9, PI = acos(-1);

// Structs

// Variables
int n, m, Q, k, a, b, c, S, sum, ans, MAXans, MINans, sc, pos, acc[mxn],
    racc[mxn], ar[mxn], dp[mxn];
vector<int> v, g[mxn];
bitset<mxn> flag;
bool sol;
string s;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> S;
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    for (int j = S; j >= 0; j--) {

      if (j + a <= S) {

        dp[j + a] += dp[j];
        dp[j + a] %= mod;
      }

      dp[j] += dp[j];
      dp[j] %= mod;
    }
  }

  cout << dp[S];

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#ifdef __debug_leywart
#include <debug/to_string.h>
#else
#define db1(x) ;
#define db2(x, begin, end) ;
#define db3(x) ;
#define db4(x, begin, end) ;
#define dbmat1(x, begin_v, end_v, begin_h, end_h) ;
#define dbmat2(x, begin_v, end_v, begin_h, end_h) ;
#define dbin1(x) ;
#define dbin2(x, begin, end) ;
#define dbtree(x, size) ;
#endif
using namespace std;
#define F first
#define S second
#define int long long

/// My functions
void sread(string &s) {
  cin >> s;
  s = '_' + s;
}
void sprint(string &s) { cout << s.substr(1, s.size()); }

// Constants
const int mxn = 3005, inf = 1e9 + 7, mod = 998244353;
const long long infLL = 1e18 + 5;
const double EPS = 1e-9, PI = acos(-1);

long long Qpow(long long base, long long exp) {

  if (exp == 0)
    return 1;

  long long temp = Qpow(base, exp / 2);

  return (((temp * temp) % mod) * (exp & 1 ? base : 1) % mod);
}

// Structs

// Variables
int n, m, Q, a, b, c, sum, ans, MAXans, MINans, sc, pos, acc[mxn], racc[mxn],
    ar[mxn];
vector<int> v, g[mxn];
bitset<mxn> flag;
bool sol;
string s;
map<int, int> M[mxn];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }

  M[0][0] = 1;
  flag[0] = true;
  for (int i = 1; i <= n; i++) {
    for (int j = m; j - ar[i] >= 0; j--) {

      if (flag[j - ar[i]]) {

        flag[j] = true;
        for (auto k : M[j - ar[i]]) {

          M[j][k.F + 1] += k.S;
          M[j][k.F + 1] %= mod;
        }
      }
    }
  }

  ans = 0;
  for (auto i : M[m]) {

    ans += (i.S * (Qpow(2, n - i.F))) % mod;
    ans %= mod;
  }

  cout << ans;

  return 0;
}