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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, s, f) for (i = s; i < f; i++)
#define print(v)                                                               \\
  for (auto &z : v)                                                            \\
    cout << z << ' ';                                                          \\
  cout << '\\n'
#define db cout << "db: "
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B, v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int, int>>
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define clk1 clock_t start_time = clock()
#define clk2 cout << (clock() - start_time) / (double)CLOCKS_PER_SEC
#define clean(arr) memset(arr, 0, sizeof(arr))
// #define mod 1000000007
#define mod 998244353
#define space 100005
//<<fixed << setprecision(9)

ll EE(ll a, ll b, ll *x, ll *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  ll x1, y1;
  ll gcd = EE(b % a, a, &x1, &y1);

  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

ll inverse(ll a, ll m) {
  ll x, y;
  EE(a, m, &x, &y);
  if (x < 0)
    x += m;
  return x;
}

int main() {
  int n, i, j, t;
  boost;

  ll p = inverse(2, mod);

  int s;
  cin >> n >> s;
  int arr[n];
  rep(i, 0, n) cin >> arr[i];

  int two[n + 1];
  two[0] = 1;
  rep(i, 1, n + 1) { two[i] = (two[i - 1] * 2) % mod; }

  ll dp[n][s + 1];
  clean(dp);
  dp[0][0] = two[n];
  if (arr[0] <= s) {
    dp[0][arr[0]] = two[n - 1];
  }
  rep(i, 1, n) {
    rep(j, 0, s + 1) {
      dp[i][j] = dp[i - 1][j];

      if (arr[i] <= j) {
        dp[i][j] += dp[i - 1][j - arr[i]] * p;
        dp[i][j] %= mod;
      }
    }
  }

  cout << dp[n - 1][s];

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, s, f) for (i = s; i < f; i++)
#define print(v)                                                               \\
  for (auto &z : v)                                                            \\
    cout << z << ' ';                                                          \\
  cout << '\\n'
#define db cout << "db: "
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B, v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int, int>>
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define clk1 clock_t start_time = clock()
#define clk2 cout << (clock() - start_time) / (double)CLOCKS_PER_SEC
#define clean(arr) memset(arr, 0, sizeof(arr))
// #define mod 1000000007
#define mod 998244353
#define space 100005
//<<fixed << setprecision(9)

int main() {
  int n, i, j, t;
  boost;

  int s;
  cin >> n >> s;
  int arr[n];
  rep(i, 0, n) cin >> arr[i];

  int two[n + 1];
  two[0] = 1;
  rep(i, 1, n + 1) { two[i] = (two[i - 1] * 2) % mod; }

  bool dp[n][s + 1];
  clean(dp);
  vi len[s + 1];
  len[0].pb(0);
  dp[0][0] = 1;

  if (arr[0] <= s) {
    dp[0][arr[0]] = 1;
    len[arr[0]].pb(1);
  }

  rep(i, 1, n) {
    vector<vi> temp(s + 1, vi());
    rep(j, 0, s + 1) {
      dp[i][j] = dp[i - 1][j];
      if (j >= arr[i]) {
        dp[i][j] = dp[i][j] | dp[i - 1][j - arr[i]];
        if (dp[i - 1][j - arr[i]]) {
          for (auto &z : len[j - arr[i]]) {
            temp[j].pb(1 + z);
          }
        }
      }
    }
    rep(j, 0, s + 1) {
      for (auto &z : temp[j])
        len[j].pb(z);
    }
    temp.clear();
  }
  // rep(i,0,n)
  // {
  //     rep(j,0,s+1) cout<<dp[i][j]<<' ';
  //     cout<<'\\n';
  // }
  ll ans = 0;
  for (auto &z : len[s]) {
    ans += two[n - z];
    ans %= mod;
  }
  // print(len[s]);
  cout << ans;

  return 0;
}