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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll n, s, i, j, k = 1, m = 998244353;
  cin >> n >> s;
  ll dp[n + 1][s + 1];
  ll ar[n + 1];
  for (i = 1; i <= n; i++)
    cin >> ar[i];
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= s; j++) {
      if (i == 0)
        dp[i][j] = 0;
      if (j == 0) {
        dp[i][j] = k % m;
        k = ((k % m) * (2 % m)) % m;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= s; j++) {
      if (ar[i] <= j)
        dp[i][j] = ((dp[i - 1][j - ar[i]] % m) + (2 * (dp[i - 1][j]) % m) % m);
      else
        dp[i][j] = (2 * (dp[i - 1][j] % m) % m);
    }
  }
  cout << dp[n][s] % m << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> // This will work only for g++ compiler.
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)  // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forab(i, l, r)                                                         \\
  for (int i = (int)(l); i <= (int)(r);                                        \\
       ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i)   // reverse 1 based
#define pb push_back
#define fi first
#define se second
#define __builtin_popcount(x) ones(x)
#define __builtin_popcountl(x) onesl(x)
#define __builtin_popcountll(x)                                                \\
  onesll(x) // to be used with algorithms that processes a container Eg:
            // find(all(c
#define all(x) (x).begin(), (x).end()  // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal
#define tr(c, i)                                                               \\
  for (__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define sz(a) int((a).size())
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define InputOuput                                                             \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef unsigned long long ull;
ull mod = 998244353;
vi arr;
int n, s;
ull ans;
ull m(ull num) { return (num + mod) % mod; }
vector<ull> po;

void countSub(int index, int sum, int till) {
  // cout << "index "<< index << " sum "<< sum << " til "<<till<<'\\n';
  if (index == n) {
    if (sum == 0) {
      // cout << till << " " << n << '\\n';
      ans += m(po[n - till]);
      ans = m(ans);
    }
    return;
  }

  countSub(index + 1, sum, till);
  countSub(index + 1, sum - arr[index], till + 1);
  return;
}
int main() {
  Fast;
  // InputOuput;
  cin >> n >> s;
  arr = vi(n, 0);
  arr = vi(n, 0);
  for0(i, n) { cin >> arr[i]; }
  po = vector<ull>(n, 0);
  for0(i, n) po[i] = (i == 0) ? 1 : m(2 * m(po[i - 1]));
  ans = 0;
  countSub(0, s, 0);
  cout << ans << '\\n';
  return 0;
}