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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 20
#else
#define SIZE 6666
#endif

int N, S, A[SIZE];
// i番目まで見て、合計がjになる答え
int DP[SIZE][SIZE];
int MOD = 998244353;

void solve() {
  cin >> N >> S;
  REP(i, N) cin >> A[i];
  DP[0][0] = 2;
  DP[0][A[0]] = 1;
  REP1(i, N - 1) REP(j, S + 1) {
    DP[i][j] += DP[i - 1][j] * 2;
    if (j >= A[i]) {
      DP[i][j] += DP[i - 1][j - A[i]];
    }
    DP[i][j] %= MOD;
  }
  cout << DP[N - 1][S] << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 6666
#endif

int N, S, A[SIZE];
// 合計がkになる種類数
map<int, int> DP[SIZE];
int MOD = 998244353;

int mod_pow(int b, int e, int m = MOD) {
  if (!e)
    return 1;
  int result = 1;
  b %= m;
  while (e > 0) {
    if ((e & 1) == 1)
      result = (result * b) % m;
    e >>= 1;
    b = (b * b) % m;
  }
  return result;
}

void solve() {
  cin >> N >> S;
  REP(i, N) cin >> A[i];
  REP(i, N) {
    int j = A[i];
    RREP(k, S + 1) {
      for (auto p : DP[k])
        if (k + j <= S) {
          (DP[k + j][p.first + 1] += p.second) %= MOD;
        }
    }
    DP[j][1]++;
  }
  int ans = 0;
  for (auto p : DP[S]) {
    ans += mod_pow(2, N - p.first) * p.second;
    ans %= MOD;
  }
  cout << ans << endl;
}