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

//   __      __       .__           .____________                   __________
//   __ ________   __
//  /  \\    /  \\ ____ |_________  __| _\\______   \\__ __  ____  _____\\______ \\__
//  ___/  |\\_____  \\ |  | ______  ___.__. \\   \\/\\/   _/ __ \\|  \\_  __ \\/ __ | |
//  |  _|  |  \\/ ___\\/  ___/|    |  _|  |  \\   __/   |   \\|  |/ \\__  \\<   |  |
//   \\        /\\  ___/|  ||  | \\/ /_/ | |    |   |  |  / /_/  \\___ \\ |    |   |
//   |  /|  |/    |    |    < / __ \\\\___  |
//    \\__/\\  /  \\___  |__||__|  \\____ | |______  |____/\\___  /____  >|______
//    |____/ |__|\\_______  |__|_ (____  / ____|
//         \\/       \\/               \\/        \\/     /_____/     \\/        \\/
//         \\/     \\/    \\/\\/

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define PI 3.14159265358979323846

using namespace std;

void solve() {
  ll n, s;
  cin >> n >> s;
  ll a[n + 1];
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  ll dp[n + 1][s + 1];
  ll inf = 998244353;
  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= s; j++)
      dp[i][j] = 0;
  dp[0][0] = 1;
  for (ll i = 1; i <= n; i++)
    for (ll j = 0; j <= s; j++) {
      dp[i][j] = (dp[i][j] + 2 * dp[i - 1][j]) % inf;
      if (j >= a[i])
        dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i]]) % inf;
    }
  /*for(ll i = 0; i <= s; i++)
  {
      for(ll j = 0; j <= n; j++)
          cout << dp[j][i] << " ";
      cout << endl;
  }*/
  cout << dp[n][s] << endl;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

//   __      __       .__           .____________                   __________
//   __ ________   __
//  /  \\    /  \\ ____ |_________  __| _\\______   \\__ __  ____  _____\\______ \\__
//  ___/  |\\_____  \\ |  | ______  ___.__. \\   \\/\\/   _/ __ \\|  \\_  __ \\/ __ | |
//  |  _|  |  \\/ ___\\/  ___/|    |  _|  |  \\   __/   |   \\|  |/ \\__  \\<   |  |
//   \\        /\\  ___/|  ||  | \\/ /_/ | |    |   |  |  / /_/  \\___ \\ |    |   |
//   |  /|  |/    |    |    < / __ \\\\___  |
//    \\__/\\  /  \\___  |__||__|  \\____ | |______  |____/\\___  /____  >|______
//    |____/ |__|\\_______  |__|_ (____  / ____|
//         \\/       \\/               \\/        \\/     /_____/     \\/        \\/
//         \\/     \\/    \\/\\/

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define PI 3.14159265358979323846

using namespace std;

bool **dp;
vector<ll> ans;

ll pow3(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}

inline void display(const vector<int> &v) { ans.pb(v.size()); }

void printSubsetsRec(int arr[], int i, int sum, vector<int> &p) {
  // If we reached end and sum is non-zero. We print
  // p[] only if arr[0] is equal to sun OR dp[0][sum]
  // is true.
  if (i == 0 && sum != 0 && dp[0][sum]) {
    p.push_back(arr[i]);
    display(p);
    return;
  }

  // If sum becomes 0
  if (i == 0 && sum == 0) {
    display(p);
    return;
  }

  // If given sum can be achieved after ignoring
  // current element.
  if (dp[i - 1][sum]) {
    // Create a new vector to store path
    vector<int> b = p;
    printSubsetsRec(arr, i - 1, sum, b);
  }

  // If given sum can be achieved after considering
  // current element.
  if (sum >= arr[i] && dp[i - 1][sum - arr[i]]) {
    p.push_back(arr[i]);
    printSubsetsRec(arr, i - 1, sum - arr[i], p);
  }
}

// Prints all subsets of arr[0..n-1] with sum 0.
void printAllSubsets(int arr[], int n, int sum) {
  if (n == 0 || sum < 0)
    return;

  // Sum 0 can always be achieved with 0 elements
  dp = new bool *[n];
  for (int i = 0; i < n; ++i) {
    dp[i] = new bool[sum + 1];
    dp[i][0] = true;
  }

  // Sum arr[0] can be achieved with single element
  if (arr[0] <= sum)
    dp[0][arr[0]] = true;

  // Fill rest of the entries in dp[][]
  for (int i = 1; i < n; ++i)
    for (int j = 0; j < sum + 1; ++j)
      dp[i][j] =
          (arr[i] <= j) ? dp[i - 1][j] || dp[i - 1][j - arr[i]] : dp[i - 1][j];
  if (dp[n - 1][sum] == false) {
    // printf("There are no subsets with sum %d\\n", sum);
    return;
  }

  // Now recursively traverse dp[][] to find all
  // paths from dp[n-1][sum]
  vector<int> p;
  printSubsetsRec(arr, n - 1, sum, p);
}

void solve() {
  ll n, s;
  cin >> n >> s;
  int a[n];
  ll inf = 998244353;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  printAllSubsets(a, n, s);
  ll out = 0;
  // for(auto i : ans)
  // cout << i << " ";
  // cout << endl;
  for (auto i : ans) {
    ll temp = pow3(2, n - i, inf);
    out = (out + temp) % inf;
  }
  cout << out << endl;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}