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

#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 998244353
#define curtime chrono::steady_clock::now().time_since_epoch().count
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

void solution() {
  int n, s;
  cin >> n >> s;
  vector<int> dp(s + 1, 0);
  vector<int> last_dp(s + 1, 0);
  last_dp[0] = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    for (int j = s; j >= 0; j--) {
      dp[j] = mod((j - x >= 0 ? last_dp[j - x] : 0LL) + 2 * last_dp[j]);
    }
    for (int j = 0; j <= s; j++) {
      last_dp[j] = dp[j];
      // cout << "dp[" << i << "][" << j << "] = " << dp[j] << endl;
    }
  }
  cout << dp[s] << endl;
}
int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  solution();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 998244353
#define curtime chrono::steady_clock::now().time_since_epoch().count
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int fat[3123];
int ifat[3123];

void prep() {
  fat[0] = 1;
  ifat[0] = 1;
  for (int i = 1; i <= 3005; i++) {
    fat[i] = mod(fat[i - 1] * i);
    ifat[i] = exp(fat[i], M - 2);
  }
}

int nCr(int n, int r) {
  int ans = fat[n];
  ans = mod(ans * ifat[n - r]);
  ans = mod(ans * ifat[r]);
  return ans;
}

void solution() {
  int n, s;
  cin >> n >> s;
  prep();
  map<ii, int> dp;
  dp[ii(0, 0)] = 1;
  vector<int> gt;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    gt.pb(x);
  }
  sort(all(gt));
  vector<ii> v;
  for (int x : gt) {
    if (v.size() == 0 || v.back().ff != x) {
      v.pb(ii(x, 1));
    } else
      v.back().ss++;
  }
  for (ii aux : v) {
    int x = aux.ff;
    map<ii, int> newdp;
    // cout << aux.ff << " " << aux.ss << endl;
    for (auto p : dp) {
      // get x
      for (int cnt = 1; cnt <= aux.ss; cnt++) {
        if (p.ff.ff + x * cnt > s)
          break;
        // cout << cnt << " " << aux.ss << " " << nCr(aux.ss, cnt) << endl;
        newdp[ii(p.ff.ff + x * cnt, p.ff.ss + cnt)] += p.ss * nCr(aux.ss, cnt);
      }
      newdp[p.ff] += p.ss;
    }
    dp.swap(newdp);
    // cout << "curr" << endl;
    // for (auto p : dp) cout << p.ff.ff << " " << p.ff.ss << " " << p.ss <<
    // endl;
  }
  int ans = 0;
  int pot2[3123];
  pot2[0] = 1;
  for (int i = 1; i <= 3005; i++) {
    pot2[i] = mod(pot2[i - 1] * 2LL);
  }

  for (auto p : dp) {
    if (p.ff.ff == s) {
      // cout << p.ff.ff << " " << p.ff.ss << " " << p.ss << endl;
      ans = mod(ans + mod(p.ss * pot2[n - p.ff.ss]));
    }
  }
  cout << ans << endl;
}
int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  solution();
}