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

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

long long power(long long a, unsigned long long b, long long m) {
  long long ret(1);
  while (b) {
    if (b & 1)
      ret = (ret * a) % m;
    a = (a * a) % m;
    b /= 2;
    if (ret < 0)
      ret += m;
    if (a < 0)
      a += m;
  }
  return ret;
}

long long inv(int a, long long m) { // GCD(a, m) = 1
  return power(a, m - 2, m);
}

int main() {
  const int m(998244353);
  long long inv2(inv(2, m));
  int N, S;
  std::cin >> N >> S;
  // dp[i][j] := i番目(1o)までで和jとなる{選んだ数, 個数}
  std::vector<std::vector<long long>> dp(N + 1, std::vector<long long>(S + 1));
  dp[0][0] = power(2, N, m);
  int A;
  for (int i(0); i < N; ++i) {
    for (int j(0); j <= S; ++j) {
      dp[i + 1][j] = dp[i][j];
    }
    std::cin >> A;
    for (int j(A); j <= S; ++j) {
      dp[i + 1][j] += dp[i][j - A] * inv2;
      dp[i + 1][j] %= m;
    }
  }
  std::cout << dp[N][S] << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

long long power(long long a, unsigned long long b, long long m) {
  long long ret(1);
  while (b) {
    if (b & 1)
      ret = (ret * a) % m;
    a = (a * a) % m;
    b /= 2;
    if (ret < 0)
      ret += m;
    if (a < 0)
      a += m;
  }
  return ret;
}

int main() {
  const int m(998244353);
  int N, S;
  std::cin >> N >> S;
  // dp[i][j] := i番目(1o)までで和jとなる{選んだ数, 個数}
  std::vector<std::vector<std::map<int, int>>> dp(
      N + 1, std::vector<std::map<int, int>>(S + 1));
  dp[0][0][0] = 1;
  int A;
  for (int i(0); i < N; ++i) {
    for (int j(0); j <= S; ++j) {
      dp[i + 1][j] = dp[i][j];
    }
    std::cin >> A;
    for (int j(A); j <= S; ++j) {
      for (auto &e : dp[i][j - A]) {
        dp[i + 1][j][e.first + 1] += e.second;
        dp[i + 1][j][e.first + 1] %= m;
      }
    }
  }
  long long ans(0);
  for (auto &e : dp[N][S]) {
    ans += e.second * power(2, N - e.first, m) % m;
    ans %= m;
  }
  std::cout << ans << std::endl;
  return 0;
}