alexjercan/bugnet huggingface dataset:
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 3e3 + 5;
const int M = 998244353;
int n, s;
int arr[maxr], dp[maxr][maxr];
int pre1, pre2;

int power(int a, int b) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 1;
  int small = power(a, b / 2);
  int ans = (small * small) % M;
  if ((b & 1))
    ans = (a * ans) % M;

  return ans;
}

int solve(int idx, int sum) {
  if (sum < 0)
    return 0;

  if (idx == n) {
    if (sum == 0)
      return pre2;
    return 0;
  }
  int &ans = dp[idx][sum];
  if (ans > -1)
    return ans;
  ans = 0;
  int a = solve(idx + 1, sum);
  int b = (solve(idx + 1, sum - arr[idx]) * pre1) % M;
  ans = (a + b) % M;
  return ans;
}

int32_t main() {
  GO;
  cin >> n >> s;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  pre1 = power(2, M - 2);
  pre2 = power(2, n);
  memset(dp, -1, sizeof(dp));
  int ans = solve(0, s);
  cout << ans << endl;
  return 0;
}

//!

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 3e3 + 5;
const int M = 998244353;
int n, s;
int arr[maxr], dp[maxr][maxr];

int power(int a, int b) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 1;
  int small = power(a, b / 2);
  int ans = (small * small) % M;
  if ((b & 1))
    ans = (a * ans) % M;

  return ans;
}

int solve(int idx, int sum) {
  if (sum < 0)
    return 0;

  if (idx == n) {
    if (sum == 0)
      return power(2, n);
    return 0;
  }
  int &ans = dp[idx][sum];
  if (ans > -1)
    return ans;
  ans = 0;
  int a = solve(idx + 1, sum);
  int b = (solve(idx + 1, sum - arr[idx]) * power(2, M - 2)) % M;
  ans = (a + b) % M;
  return ans;
}

int32_t main() {
  GO;
  cin >> n >> s;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  memset(dp, -1, sizeof(dp));
  int ans = solve(0, s);
  cout << ans << endl;
  return 0;
}

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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 998244353;

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}
template <typename T> void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}

int main() {
  int N, S;
  cin >> N >> S;
  vector<ll> A(N);
  for (auto &a : A) {
    cin >> a;
  }
  sort(A.begin(), A.end());

  vector<ll> dp(S + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    for (int k = S; k >= 0; k--) {
      if (k + A[i] <= S) {
        dp[k + A[i]] += dp[k];
        dp[k + A[i]] %= MOD;
      }
      dp[k] = dp[k] * 2 % MOD;
    }
  }
  // cout_vec(dp);
  /*
  vector<ll> p2(3001,1);
  for(int i=0; i<3000; i++){
      p2[i+1]=p2[i]*2%MOD;
  }

  ll ans=0;
  for(int i=0; i<=N; i++){
      //cout << dp[i][S] << endl;
      ans+=dp[i][S]*p2[N-i]%MOD;
      ans%=MOD;
  }
  */
  cout << dp[S] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 998244353;

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}
template <typename T> void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}

int main() {
  int N, S;
  cin >> N >> S;
  vector<ll> A(N);
  for (auto &a : A) {
    cin >> a;
  }
  sort(A.begin(), A.end());

  vector<vector<ll>> dp(N + 1, vector<ll>(S + 1, 0));
  dp[0][0] = 1;
  ll sum_ = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i; j >= 0; j--) {
      for (int k = 0; k <= min(sum_, S - A[i]); k++) {
        dp[j + 1][k + A[i]] += dp[j][k] % MOD;
        dp[j + 1][k + A[i]] % MOD;
      }
      sum_ += A[i];
    }
  }

  vector<ll> p2(3001, 1);
  for (int i = 0; i < 3000; i++) {
    p2[i + 1] = p2[i] * 2 % MOD;
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    // cout << dp[i][S] << endl;
    ans += dp[i][S] * p2[N - i] % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 998244353

int main() {
  int n, s;
  cin >> n >> s;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, 0));
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] += (2 * dp[i - 1][j]) % MOD;
      dp[i][j] %= MOD;
      if (j + a[i - 1] <= s) {
        dp[i][j + a[i - 1]] += dp[i - 1][j];
        dp[i][j + a[i - 1]] %= MOD;
      }
    }
  }
  cout << dp[n][s] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 998244353
int main() {
  int n, s;
  cin >> n >> s;
  vector<ll> a(n);
  for (auto &it : a) {
    cin >> it;
  }
  vector<vector<int>> dp(n + 1, vector<int>(s + 1, 0));
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] += 2 * dp[i - 1][j];
      dp[i][j] %= MOD;
      if (j + a[i - 1] <= s) {
        dp[i][j + a[i - 1]] += dp[i - 1][j];
        dp[i][j + a[i]] %= MOD;
      }
    }
  }
  cout << dp[n][s] << endl;
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

constexpr auto M = 998244353L;

int main() {
  int64_t n, s;
  cin >> n >> s;

  vector<int64_t> a(n);
  for (auto &&aa : a) {
    cin >> aa;
  }

  vector<int64_t> dp(s + 1);
  dp[0] = 1;
  for (auto i : irange(0L, n)) {
    dp[0] *= 2;
    dp[0] %= M;
  }

  int64_t inv2 = M / 2 + 1;
  for (auto aa : a) {
    for (auto ss : irange(0L, max(0L, s - aa + 1)) | reversed) {
      dp[ss + aa] += dp[ss] * inv2 % M;
      dp[ss + aa] %= M;
    }
  }

  cout << dp[s] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

constexpr auto M = 998244353L;

int main() {
  int64_t n, s;
  cin >> n >> s;

  vector<int64_t> a(n);
  for (auto &&aa : a) {
    cin >> aa;
  }

  vector<int64_t> dp(s + 1);
  dp[0] = 1;
  for (auto i : irange(0L, n)) {
    dp[0] *= 2;
    dp[0] %= M;
  }

  int64_t inv2 = M / 2 + 1;
  for (auto aa : a) {
    for (auto ss : irange(0L, s - aa + 1) | reversed) {
      dp[ss + aa] += dp[ss] * inv2;
      dp[ss + aa] %= M;
    }
  }

  cout << dp[s] << endl;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const long long INIT = -1;
const long long MOD = 998244353;
long long dp[3001][3001];
int _array[3005];
long long fact[3005];
long long dfs(int n, int value);
int main() {
  int n;
  int sum;
  cin >> n >> sum;
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> _array[i];
    fact[i] = fact[i - 1] * i;
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i][j] = INIT;
    }
  }

  cout << dfs(n, sum) << endl;
  return (0);
}

long long dfs(int n, int value) {
  if (n == 0) {
    if (value == 0) {
      return (1);
    } else {
      return (0);
    }
  }
  if (dp[n][value] != INIT) {
    return (dp[n][value]);
  }

  long long s = dfs(n - 1, value) * 2 % MOD;
  if (value >= _array[n]) {
    s = (s + dfs(n - 1, value - _array[n] % MOD)) % MOD;
  }
  //  cout<<"n:"<<n<<endl;
  //  cout<<"s:"<<s<<endl<<endl;
  return (dp[n][value] = s);
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
const long long INIT = -1;
const long long MOD = 998244353;
long long dp[3001][3001];
int _array[3005];
long long fact[3005];
long long dfs(int n, int value);
int main() {
  int n;
  int sum;
  cin >> n >> sum;
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> _array[i];
    fact[i] = fact[i - 1] * i;
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i][j] = INIT;
    }
  }

  cout << dfs(n, sum) << endl;
  return (0);
}

long long dfs(int n, int value) {
  if (n == 0) {
    if (value == 0) {
      return (1);
    } else {
      return (0);
    }
  }
  if (dp[n][value] != INIT) {
    return (dp[n][value]);
  }

  long long s =
      (dfs(n - 1, value) * 2 % MOD + dfs(n - 1, value - _array[n]) % MOD) % MOD;
  //  cout<<"n:"<<n<<endl;
  //  cout<<"s:"<<s<<endl<<endl;
  return (dp[n][value] = s);
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n, s;
  cin >> n >> s;
  v1i v(n + 1);
  rep(i, n) cin >> v[i];
  v2li dp(n + 1, v1li(s + 1, 0));
  dp[0][0] = 1;
  rep(i, n) {
    int tmp = v[i];
    rep(j, s + 1) {
      dp[i + 1][j] += dp[i][j] * 2;
      dp[i + 1][j] %= mod2;
      if (tmp + j <= s) {
        dp[i + 1][j + tmp] += dp[i][j];
        dp[i + 1][j + tmp] %= mod2;
      }
    }
  }
  cout << dp[n][s] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n, s;
  cin >> n >> s;
  v1li v(n + 1);
  rep(i, n) cin >> v[i];
  v2li dp(n, v1li(s + 1, 0));
  rep(i, n) {
    int tmp = v[i];
    rep(j, s + 1) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= mod2;
      if (tmp + j <= s) {
        dp[i + 1][j + tmp] += dp[i][j];
        dp[i + 1][j + tmp] %= mod2;
      }
    }
  }
  cout << dp[n][s] << endl;
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll MOD = 998244353;

ll mypow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return a * mypow(a, b - 1) % MOD;

  ll d = mypow(a, b / 2) % MOD;
  return d * d % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, s;
  cin >> n >> s;

  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll INF = 1e17;
  ll dp[n + 1][s + 1];
  rep(i, n + 1) rep(j, s + 1) dp[i][j] = INF;
  dp[0][0] = mypow(2, n);

  ll inv2 = mypow(2, MOD - 2);

  rep(i, n) {
    rep(j, s + 1) {

      if (dp[i][j] < INF) {
        if (dp[i + 1][j] == INF)
          dp[i + 1][j] = 0;
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= MOD;

        if (j + a[i] <= s) {
          if (dp[i + 1][j + a[i]] == INF)
            dp[i + 1][j + a[i]] = 0;
          dp[i + 1][j + a[i]] += dp[i][j] * inv2 % MOD;
          dp[i + 1][j + a[i]] %= MOD;
        }
      }
    }
  }

  if (dp[n][s] == INF) {
    cout << 0 << endl;
  } else {
    cout << dp[n][s] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll MOD = 998244353;

ll mypow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return a * mypow(a, b - 1) % MOD;

  ll d = mypow(a, b / 2) % MOD;
  return d * d % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, s;
  cin >> n >> s;

  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll INF = 1e17;
  ll dp[n + 1][s + 1];
  rep(i, n + 1) rep(j, s + 1) dp[i][j] = INF;
  dp[0][0] = mypow(2, n);

  rep(i, n) {
    rep(j, s + 1) {

      if (dp[i][j] < INF) {
        if (dp[i + 1][j] == INF)
          dp[i + 1][j] = 0;
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= MOD;

        if (j + a[i] <= s) {
          if (dp[i + 1][j + a[i]] == INF)
            dp[i + 1][j + a[i]] = 0;
          dp[i + 1][j + a[i]] += dp[i][j] * mypow(2, MOD - 2) % MOD;
          dp[i + 1][j + a[i]] %= MOD;
        }
      }
    }
  }

  if (dp[n][s] == INF) {
    cout << 0 << endl;
  } else {
    cout << dp[n][s] << endl;
  }
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

#define ll long long

using namespace std;

ll mod = 998244353;

void solve() {
  ll n, s;
  cin >> n >> s;

  vector<ll> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, 0));
  dp[0][0] = 1;

  for (int i = 0; i < n; i++)
    for (int j = 0; j <= s; j++) {
      dp[i + 1][j] += 2 * dp[i][j];
      dp[i + 1][j] %= mod;

      if (j + a[i] <= s) {
        dp[i + 1][j + a[i]] += dp[i][j];
        dp[i + 1][j + a[i]] %= mod;
      }
    }

  cout << dp[n][s] << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int t = 1;
  // cin>>t;

  while (t > 0) {
    t--;
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

#define ll long long

using namespace std;

ll mod = 998244353;

ll power(ll a, ll b) {
  if (b == 0)
    return 1;

  ll ret = power(a * a % mod, b / 2);
  if (b % 2)
    ret = (ret * a) % mod;

  return ret;
}

ll choose(ll a, ll b) {
  if (b - a < a)
    a = b - a;

  ll value = 1;
  for (int i = 0; i < a; i++) {
    value = (value * (b - i)) % mod;
    value = (value * power(i + 1, mod - 2)) % mod;
  }

  return value;
}

void find(ll index, vector<pair<ll, ll>> &data, ll &ans, ll tot, ll cnt, ll mul,
          ll s, ll n) {
  if (index < data.size()) {
    for (ll v = 0; v <= data[index].second; v++) {
      if (v * data[index].first + tot > s)
        break;

      ll temp_mul = (mul * choose(v, data[index].second)) % mod;
      ll temp_tot = tot + v * data[index].first;
      ll temp_cnt = cnt + v;

      if (temp_tot == s) {
        ans = (ans + temp_mul * power(2, n - temp_cnt) % mod) % mod;
      } else {
        find(index + 1, data, ans, temp_tot, temp_cnt, temp_mul, s, n);
      }
    }
  }
}

void solve() {
  ll n, s;
  cin >> n >> s;

  map<ll, ll> cnt;
  ll t;
  for (int i = 0; i < n; i++) {
    cin >> t;
    cnt[t]++;
  }

  vector<pair<ll, ll>> data;
  for (auto p : cnt)
    data.push_back(p);

  ll ans = 0;
  find(0, data, ans, 0, 0, 1, s, n);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int t = 1;
  // cin>>t;

  while (t > 0) {
    t--;
    solve();
  }

  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ldouble long double
using namespace std;

/*
  I need to know how many times this subset will happen in the 2^n - 1 subsets
*/

const int maxN = 3e3 + 1;
int n, s;
const lli mod = 998244353;
int a[maxN];

lli modPow(lli x, lli y) {
  lli ans = 1;
  while (y) {
    if (y & 1LL)
      ans = ans * x % mod;
    x = x * x % mod, y >>= 1LL;
  }
  return ans;
}
lli inv(lli x) { return modPow(x, mod - 2); }

lli memo[maxN][maxN];
lli solve(int i = 0, int sum = 0, int cnt = 0) {
  if (sum == s)
    return modPow(2, n - cnt * 0);
  if (sum > s || i == n)
    return 0;
  if (memo[i][sum] != -1)
    return memo[i][sum];
  lli ans = (solve(i + 1, sum, cnt) % mod +
             solve(i + 1, sum + a[i], cnt + 1) * inv(2) % mod) %
            mod;
  return memo[i][sum] = ans;
}

int main() {
  int t = 0;
  while (scanf("%d %d", &n, &s) != EOF) {
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);

    memset(memo, -1, sizeof(memo));
    lli ans = solve();
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ldouble long double
using namespace std;

/*
  I need to know how many times this subset will happen in the 2^n - 1 subsets
*/

const int maxN = 3e3 + 1;
int n, s;
const lli mod = 998244353;
int a[maxN];

lli modPow(lli x, lli y) {
  lli ans = 1;
  while (y) {
    if (y & 1LL)
      ans = ans * x % mod;
    x = x * x % mod, y >>= 1LL;
  }
  return ans;
}

unordered_map<int, lli> memo[maxN][maxN];
lli solve(int i = 0, int sum = 0, int cnt = 0) {
  if (sum == s)
    return modPow(2, n - cnt);
  if (sum > s || i == n)
    return 0;
  if (memo[i][sum].count(cnt))
    return memo[i][sum][cnt];
  lli ans = (solve(i + 1, sum, cnt) + solve(i + 1, sum + a[i], cnt + 1)) % mod;
  return memo[i][sum][cnt] = ans;
}

int main() {
  int t = 0;
  while (scanf("%d %d", &n, &s) != EOF) {
    if (t++) {
      for (int i = 0; i < maxN; i++)
        for (int j = 0; j < maxN; j++)
          memo[i][j].clear();
    }
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);

    lli ans = solve();
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define DIV 998244353

long long N, S;
long long A[3005];
long long dp[3005][3005];

long long modpow(long long ori, long long po) {
  long long res = 1;
  while (po > 0) {
    if (po & 1) {
      res *= ori;
      res %= DIV;
    }
    ori *= ori;
    ori %= DIV;
    po >>= 1;
  }
  return res;
}

int main() {
  cin >> N >> S;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }

  dp[0][0] = modpow(2, N);
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < 3005; j++) {
      // そのまま
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= DIV;

      // A[i]を使う
      if (j + A[i] <= S) {
        dp[i + 1][j + A[i]] += dp[i][j] * modpow(2, DIV - 2);
        dp[i + 1][j + A[i]] %= DIV;
      }
    }
  }
  cout << dp[N][S] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define DIV 998244353

long long N, S;
long long A[3005];
//            num, sum: count
map<pair<long long, long long>, long long> memo;

long long modpow(long long ori, long long po) {
  long long res = 1;
  while (po > 0) {
    if (po & 1) {
      res *= ori;
      res %= DIV;
    }
    ori *= ori;
    ori %= DIV;
    po >>= 1;
  }
  return res;
}

int main() {
  cin >> N >> S;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }

  memo[make_pair(0, 0)] = 1;
  for (long long i = 0; i < N; i++) {
    map<pair<long long, long long>, long long> next;
    for (auto item : memo) {
      next[item.first] += item.second;
      long long num = item.first.first;
      long long sum = item.first.second;
      long long cou = item.second;
      if (sum + A[i] <= S) {
        next[make_pair(num + 1, sum + A[i])] += cou;
      }
    }
    swap(memo, next);
  }

  long long ans = 0;
  for (auto item : memo) {
    long long num = item.first.first;
    long long sum = item.first.second;
    long long cou = item.second;
    if (sum == S) {
      ans += modpow(2, N - num) * cou;
      ans %= DIV;
    }
  }
  cout << ans << endl;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 998244353;

int main() {
  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<vector<long long>> dp(N + 1, vector<long long>(S + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    dp[0][0] = (dp[0][0] * 2) % MOD;
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= S; j++) {
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD;
      if (j + A[i] <= S) {
        if (dp[i][j] % 2 == 0) {
          dp[i + 1][j + A[i]] = (dp[i + 1][j + A[i]] + dp[i][j] / 2) % MOD;
        } else {
          dp[i + 1][j + A[i]] =
              (dp[i + 1][j + A[i]] + (dp[i][j] + MOD) / 2) % MOD;
        }
      }
    }
  }

  cout << dp[N][S] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 998244353;

int main() {
  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  // 1: i-1番目まで
  // 2: 和がj
  // map: mこ使ったやつがnとおり
  vector<vector<map<int, long long>>> dp(N + 1,
                                         vector<map<int, long long>>(S + 1));
  dp[0][0][0] = 1;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= S; j++) {
      for (auto k : dp[i][j]) {
        if (dp[i + 1][j].count(k.first) == 0) {
          dp[i + 1][j][k.first] = k.second;
        } else {
          dp[i + 1][j][k.first] += k.second;
        }
        dp[i + 1][j][k.first] %= MOD;
        if (j + A[i] <= S) {
          if (dp[i + 1][j + A[i]].count(k.first + 1) == 0) {
            dp[i + 1][j + A[i]][k.first + 1] = k.second;
          } else {
            dp[i + 1][j + A[i]][k.first + 1] += k.second;
          }
          dp[i + 1][j + A[i]][k.first + 1] %= MOD;
        }
      }
    }
  }

  vector<long long> tp(N + 1);
  tp[0] = 1;
  for (int i = 0; i < N; i++) {
    tp[i + 1] = tp[i] * 2;
    tp[i + 1] %= MOD;
  }

  long long result = 0;
  for (auto k : dp[N][S]) {
    result += (k.second * tp[N - k.first]) % MOD;
    result %= MOD;
  }
  cout << result << endl;
}