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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#line 1 "lib/modint.hpp"

#include <iostream>

// Modint
struct Mint {
  static const long long mod = 998244353;
  long long val;

  Mint() { val = 0; }
  Mint(long long a) {
    val = a;
    verify_value();
  }

  void verify_value() {
    if (val >= mod)
      val %= mod;
    if (val < 0)
      val %= mod, val += mod;
  }

  Mint pow(long long p) const {
    Mint cur = Mint(val), ret = 1;
    while (p > 0) {
      if (p & 1)
        ret *= cur;
      cur *= cur;
      p >>= 1LL;
    }
    return ret;
  }
  Mint inv() const {
    if (val == 0)
      std::cerr << "WARNING: inv() is called with 0." << std::endl;
    return pow(mod - 2);
  }

  Mint operator+() const { return *this; }
  Mint operator-() const { return Mint(mod - val); }

  Mint operator+=(const Mint &a) {
    val += a.val;
    if (val >= mod)
      val -= mod;
    return Mint(val);
  }
  Mint operator*=(const Mint &a) {
    val *= a.val;
    if (val >= mod)
      val %= mod;
    return Mint(val);
  }
  Mint operator-=(const Mint &a) { return *this += -a; }
  Mint operator/=(const Mint &a) { return *this *= a.inv(); }

  Mint operator++() { return *this += Mint(1); }
  Mint operator--() { return *this -= Mint(1); }
  Mint operator++(int) {
    Mint ret = *this;
    ++(*this);
    return ret;
  }
  Mint operator--(int) {
    Mint ret = *this;
    --(*this);
    return ret;
  }

  operator long long() const { return val; }
};

Mint operator+(const Mint &a, const Mint &b) {
  long long ret = a.val + b.val;
  if (ret >= Mint::mod)
    ret -= Mint::mod;
  return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
  long long ret = a.val * b.val;
  if (ret >= Mint::mod)
    ret %= Mint::mod;
  return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }

std::ostream &operator<<(std::ostream &out, const Mint &a) {
  return out << a.val;
}
std::istream &operator>>(std::istream &in, Mint &a) {
  in >> a.val;
  a.verify_value();
  return in;
}

Mint pow(Mint a, int b) { return a.pow(b); }

int main() {
  int n, s;
  std::cin >> n >> s;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    std::cin >> a[i];
  std::vector<Mint> dp(s + 1);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = s; j >= 0; j--) {
      if (j + a[i] <= s)
        dp[j + a[i]] += dp[j];
      dp[j] += dp[j];
    }
  }
  std::cout << dp[s] << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#line 1 "lib/modint.hpp"

#include <iostream>

// Modint
struct Mint {
  static const long long mod = 998244353;
  long long val;

  Mint() { val = 0; }
  Mint(long long a) {
    val = a;
    verify_value();
  }

  void verify_value() {
    if (val >= mod)
      val %= mod;
    if (val < 0)
      val %= mod, val += mod;
  }

  Mint pow(long long p) const {
    Mint cur = Mint(val), ret = 1;
    while (p > 0) {
      if (p & 1)
        ret *= cur;
      cur *= cur;
      p >>= 1LL;
    }
    return ret;
  }
  Mint inv() const {
    if (val == 0)
      std::cerr << "WARNING: inv() is called with 0." << std::endl;
    return pow(mod - 2);
  }

  Mint operator+() const { return *this; }
  Mint operator-() const { return Mint(mod - val); }

  Mint operator+=(const Mint &a) {
    val += a.val;
    if (val >= mod)
      val -= mod;
    return Mint(val);
  }
  Mint operator*=(const Mint &a) {
    val *= a.val;
    if (val >= mod)
      val %= mod;
    return Mint(val);
  }
  Mint operator-=(const Mint &a) { return *this += -a; }
  Mint operator/=(const Mint &a) { return *this *= a.inv(); }

  Mint operator++() { return *this += Mint(1); }
  Mint operator--() { return *this -= Mint(1); }
  Mint operator++(int) {
    Mint ret = *this;
    ++(*this);
    return ret;
  }
  Mint operator--(int) {
    Mint ret = *this;
    --(*this);
    return ret;
  }

  operator long long() const { return val; }
};

Mint operator+(const Mint &a, const Mint &b) {
  long long ret = a.val + b.val;
  if (ret >= Mint::mod)
    ret -= Mint::mod;
  return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
  long long ret = a.val * b.val;
  if (ret >= Mint::mod)
    ret %= Mint::mod;
  return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }

std::ostream &operator<<(std::ostream &out, const Mint &a) {
  return out << a.val;
}
std::istream &operator>>(std::istream &in, Mint &a) {
  in >> a.val;
  a.verify_value();
  return in;
}

Mint pow(Mint a, int b) { return a.pow(b); }

Mint dp[3003][3003];

int main() {
  int n, s;
  std::cin >> n >> s;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    std::cin >> a[i];
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= 0; j--) {
      for (int k = 0; k < s; k++) {
        if (k + a[i] <= s)
          dp[j + 1][k + a[i]] += dp[j][k];
      }
    }
  }
  Mint ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += dp[i][s] * pow(Mint(2), n - i);
  }
  std::cout << ans << std::endl;
  return 0;
}