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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

#define MAX 3010
int N, S, A[MAX];

const ll mod = 998244353LL;

int bf() {
  map<string, int> mp;
  int cnt = 0;
  rep(i, (1 << N)) {
    rep(j, (1 << N)) {
      int sum = 0;
      bool fail = false;
      rep(k, N) {
        if (!(i & (1 << k)) && (j & (1 << k))) {
          fail = true;
          break;
        }
        if ((j & (1 << k))) {
          sum += A[k];
        }
      }
      if (fail)
        continue;
      if (sum == S) {
        ++cnt;
        cout << "* " << cnt << " : ";
        string st = "";
        rep(k, N) {
          if ((j & (1 << k))) {
            st += (string(5 - (int)to_string(A[k]).size(), ' ') +
                   to_string(A[k]));
            cout << string(5 - (int)to_string(A[k]).size(), ' ') << A[k];
          } else {
            st += string(5, ' ');
            cout << string(5, ' ');
          }
        }
        puts("");
        mp[st]++;
      }
    }
  }
  puts("----");
  for (auto v : mp) {
    cout << v.fst << " : " << v.snd << endl;
  }
  puts("----");
  return cnt;
}

ll dp[3002];

void solve() {
  vector<ll> pow2;
  pow2.pb(1);
  rep(i, N + 1) { pow2.pb((pow2.back() * 2LL) % mod); }

  // cout << "Answer: " << bf() << endl;;
  dp[0] = 1;
  rep(i, N) {
    // int nex = ( cur + 1 ) & 1;
    // rep(j,S+1) rep(k,N+1) dp[nex][j][k] = dp[cur][j][k];
    for (int j = S; j >= 0; --j) {
      if (j + A[i] <= S) {
        //( dp[nex][j+A[i]][k+1] += dp[cur][j][k] ) %= mod;
        (dp[j + A[i]] += dp[j]) %= mod;
      }
      (dp[j] *= 2LL) %= mod;
    }
  }
  cout << dp[S] << endl;
}

int main() {
  cin >> N >> S;
  rep(i, N) cin >> A[i];
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

#define MAX 3010
int N, S, A[MAX];

const ll mod = 998244353LL;

int bf() {
  map<string, int> mp;
  int cnt = 0;
  rep(i, (1 << N)) {
    rep(j, (1 << N)) {
      int sum = 0;
      bool fail = false;
      rep(k, N) {
        if (!(i & (1 << k)) && (j & (1 << k))) {
          fail = true;
          break;
        }
        if ((j & (1 << k))) {
          sum += A[k];
        }
      }
      if (fail)
        continue;
      if (sum == S) {
        ++cnt;
        cout << "* " << cnt << " : ";
        string st = "";
        rep(k, N) {
          if ((j & (1 << k))) {
            st += (string(5 - (int)to_string(A[k]).size(), ' ') +
                   to_string(A[k]));
            cout << string(5 - (int)to_string(A[k]).size(), ' ') << A[k];
          } else {
            st += string(5, ' ');
            cout << string(5, ' ');
          }
        }
        puts("");
        mp[st]++;
      }
    }
  }
  puts("----");
  for (auto v : mp) {
    cout << v.fst << " : " << v.snd << endl;
  }
  puts("----");
  return cnt;
}

ll dp[3002][3002];

void solve() {
  vector<ll> pow2;
  pow2.pb(1);
  rep(i, N + 1) { pow2.pb((pow2.back() * 2LL) % mod); }

  // cout << "Answer: " << bf() << endl;;
  dp[0][0] = 1;
  ll ans = 0;
  rep(i, N) {
    int cur = i & 1;
    // int nex = ( cur + 1 ) & 1;
    // rep(j,S+1) rep(k,N+1) dp[nex][j][k] = dp[cur][j][k];
    for (int j = S; j >= 0; --j) {
      if (j + A[i] <= S) {
        rep(k, N) {
          //( dp[nex][j+A[i]][k+1] += dp[cur][j][k] ) %= mod;
          (dp[j + A[i]][k + 1] += dp[j][k]) %= mod;
        }
      }
    }
  }
  int lst = N & 1;
  ans = 0;
  REP(i, 1, N + 1) {
    if (dp[S][i]) {
      ans += ((dp[S][i] * pow2[N - i]) % mod);

      ans %= mod;
    }
  }
  cout << ans << endl;
}

int main() {
  cin >> N >> S;
  rep(i, N) cin >> A[i];
  solve();
  return 0;
}