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

#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
ifstream cinn("in.txt");
ofstream coutt("out.txt");
int poww(const int &a, int b, const int &m = MOD) {
  if (b == 0)
    return 1;
  int x = poww(a, b / 2, m);
  x = x * x % m;
  if (b & 1)
    x = x * a % m;
  return x;
}
int ceil(const int &a, const int &b) { return (a - 1) / b + 1; }
// Read:
// Use __builtin_popcountll for ll

int n, s, p2;
int v[3005], two[3005], dp[3005][3005];
int f(int i, int sum) {
  if (sum == s) {
    return two[n];
  }
  if (sum > s || i > n)
    return 0;
  int &ans = dp[i][sum];
  if (ans != -INF)
    return ans;

  ans = f(i + 1, sum + v[i]) * p2 % MOD2;
  ans = (ans + f(i + 1, sum)) % MOD2;
  return ans;
}

signed main() {
  two[0] = 1;
  p2 = poww(2, MOD2 - 2, MOD2);
  for (int i = 1; i <= 3004; i++)
    two[i] = two[i - 1] * 2 % MOD2;
  for (int i = 0; i < 3005; i++)
    for (int j = 0; j < 3005; j++)
      dp[i][j] = -INF;
  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  cout << f(1, 0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
ifstream cinn("in.txt");
ofstream coutt("out.txt");
int poww(const int &a, int b, const int &m = MOD) {
  if (b == 0)
    return 1;
  int x = poww(a, b / 2, m);
  x = x * x % m;
  if (b & 1)
    x = x * a % m;
  return x;
}
int ceil(const int &a, const int &b) { return (a - 1) / b + 1; }
// Read:
// Use __builtin_popcountll for ll

int n, s;
int v[3005], two[3005];
vector<vvi> dp;

int f(int i, int sum, int cnt) {
  if (sum == s) {
    return two[n - cnt];
  }
  if (sum > s || i > n || cnt > n / s + 100)
    return 0;
  int &ans = dp[i][sum][cnt];
  if (ans != -INF)
    return ans;

  ans = f(i + 1, sum + v[i], cnt + 1);
  ans = (ans + f(i + 1, sum, cnt)) % MOD2;
  return ans;
}

signed main() {
  two[0] = 1;
  for (int i = 1; i <= 3004; i++)
    two[i] = two[i - 1] * 2 % MOD2;
  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  dp = vector<vvi>(n + 1, vvi(s + 1, vi(n / s + 100, -INF)));
  cout << f(1, 0, 0);
}