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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

ll dp[3020][3020];
const ll MOD = 998244353LL;
int main() {
  int N, S;
  cin >> N >> S;
  vector<ll> two(1, 1);
  for (int i = 0; i < N; i++)
    two.push_back(two.back() * 2LL % MOD);
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  dp[0][0] = two[N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= S; j++) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= MOD;
      const int u = j + A[i];
      if (u <= S) {
        ll v = dp[i][j];
        if (v % 2 == 0)
          v /= 2LL;
        else
          v = (v + MOD) / 2LL;
        dp[i + 1][u] += v;
        dp[i + 1][u] %= MOD;
      }
    }
    //    REP(j,S+1)
    //      cout << "(" << j << "," << dp[i+1][j] << ")";
    //    cout << endl;
  }
  cout << dp[N][S] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

map<pint, ll> cnt[2];
const ll MOD = 998244353LL;
int main() {
  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  cnt[0][make_pair(0, 0)] = 1LL;
  REP(i, N) {
    int from = i % 2, to = 1 - from;
    cnt[to].clear();
    for (auto a : cnt[from]) {
      const int c = a.first.first;
      const int v1 = a.first.second;
      const int v2 = v1 + A[i];
      cnt[to][make_pair(c, v1)] += a.second;
      cnt[to][make_pair(c, v1)] %= MOD;
      if (v2 <= S) {
        cnt[to][make_pair(c + 1, v2)] += a.second;
        cnt[to][make_pair(c + 1, v2)] %= MOD;
      }
    }
    //    for(auto a: cnt[to%2]) {
    //      cout << i << ":" << a.first.first << " " << a.first.second << " " <<
    //      a.second << endl;
    //    }
  }
  vector<ll> two(1, 1);
  ll ans = 0;
  for (int i = 0; i < N; i++)
    two.push_back(two.back() * 2LL % MOD);
  for (auto a : cnt[N % 2]) {
    if (a.first.second == S) {
      //      cout << a.first.first << " " << a.first.second << " " << a.second
      //      << endl;
      ll work = (two[N - a.first.first] * a.second) % MOD;
      ans = (ans + work) % MOD;
    }
  }
  cout << ans % MOD << endl;
  return 0;
}