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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; // 10e18+10e17ちょっとくらい
const int MOD = 998244353;
const int MAX_N = 200100;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

mint dp[3010][3010];
vi v;
signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  int n, s;
  cin >> n >> s;
  rep(i, n) {
    int t;
    cin >> t;
    v.emplace_back(t);
  }
  dp[0][0] = 1;
  rep(i, n) {
    int now = v[i];
    rep(j, 3001) {
      dp[i + 1][j] += 2 * dp[i][j].x;
      if (j + now < 3010)
        dp[i + 1][j + now] += dp[i][j];
    }
  }
  cout << dp[n][s] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; // 10e18+10e17ちょっとくらい
const int MOD = 998244353;
const int MAX_N = 200100;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

map<int, int> dp[3010][3010];
vi v;
signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  int n, s;
  cin >> n >> s;
  rep(i, n) {
    int t;
    cin >> t;
    v.emplace_back(t);
  }
  dp[0][0][0] = 1;
  rep(i, n) {
    int now = v[i];
    rep(j, 3001) {
      for (auto m : dp[i][j]) {
        int key = m.first;
        int val = m.second;
        dp[i + 1][j][key] += val;
        if (j + now < 3010)
          dp[i + 1][j + now][key + 1] += val;
      }
    }
  }
  mint ans = 0;
  mint sec = 1;
  rep(i, n) {
    if (dp[n][s].count(n - i)) {
      ans += sec * dp[n][s][n - i];
    }
    sec *= 2;
  }
  cout << ans << endl;

  return 0;
}