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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 998244353
#define F first
#define S second

// #define COMMENT

/*

const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];

void inverse_modulao() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for(int i = 2;i < NN;i++) {
    fact[i] = fact[i-1]*i%mod;
    inv[i] = mod-mod/i*inv[mod%i]%mod;
    finv[i] = finv[i-1]*inv[i]%mod;
  }
}

ll nCr(int n, int r) {
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}

*/

// 3000 3000 3000

int N, S, A[4000], dp[3090][3090];

int main() {

  //  freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  cin >> N >> S;
  rep(i, N) { cin >> A[i + 1]; }
  dp[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    dp[i][0] = (2 * dp[i - 1][0]) % mod;
    for (int j = 1; j <= S; j++) {
      dp[i][j] = (2 * dp[i - 1][j]) % mod;
      if (j - A[i] >= 0) {
        (dp[i][j] += dp[i - 1][j - A[i]]) %= mod;
      }
    }
  }

  cout << dp[N][S] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 998244353
#define F first
#define S second

// #define COMMENT

/*

const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];

void inverse_modulao() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for(int i = 2;i < NN;i++) {
    fact[i] = fact[i-1]*i%mod;
    inv[i] = mod-mod/i*inv[mod%i]%mod;
    finv[i] = finv[i-1]*inv[i]%mod;
  }
}

ll nCr(int n, int r) {
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}

*/

// 3000 3000 3000

int N, S, A[4000], dp[3090][3090];
ll p2[3090];
unordered_map<ll, ll> uma;

ll solve(int len, int n, int s) {

  if (len == 0) {
    return s == 0;
  }

  if (dp[n][s] == 0)
    return 0;

  ll state = 1LL * len * 1e8 + 1LL * n * 1e4 + s;

  if (uma.count(state) == 1)
    return uma[state];

  if (len > s)
    return 0;
  if (len > n)
    return 0;

  // len <= n and len <= s
  ll u = 0, v = 0;
  u = solve(len, n - 1, s);
  if (s - A[n] >= 0) {
    v = solve(len - 1, n - 1, s - A[n]);
  }

  return uma[state] = (u + v) % mod;
}

int main() {

  //  freopen("input.in","r",stdin);
  //  freopen("output.out","w",stdout);

  cin >> N >> S;
  rep(i, N) {
    cin >> A[i + 1];
    dp[i + 1][0] = 1;
  }
  dp[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= S; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - A[i] >= 0) {
        dp[i][j] |= dp[i - 1][j - A[i]];
      }
    }
  }

  p2[0] = 1;
  for (int i = 1; i <= N; i++)
    p2[i] = (2 * p2[i - 1]) % mod;
  ll ans = 0;

  for (int len = 1; len <= N; len++) {
    ll temp = solve(len, N, S);
    (ans += (temp * p2[N - len]) % mod) %= mod;
  }

  cout << ans << endl;

  return 0;
}