alexjercan/bugnet huggingface dataset:
Problem p02661 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define DBG(x)                                                                 \\
  (void)(cout << "L" << __LINE__ << ": " << #x << " = " << (x) << '\\n')
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
typedef long long ll;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const long double PI = (acos(-1));
const long double EPS = 0.0000000001;
const int INF = 0x3f3f3f3f;
const int maxn = 1100000;
int a[maxn];
int b[maxn];
void run_case() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);
  int ans = 0;
  if (n & 1) {
    int l = a[n / 2 + 1];
    int r = b[n / 2 + 1];
    ans = r - l + 1;
  } else {
    int l = a[n / 2] + a[n / 2 + 1];
    int r = b[n / 2] + b[n / 2 + 1];
    ans = r - l + 1;
  }
  cout << ans << "\\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(12);
  run_case();
  cout.flush();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long

using namespace std;
typedef pair<int, int> ii;

int n, a[100001], b[100001];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //    freopen("INP.INP", "r", stdin);
  //    freopen("OUT.OUT", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  if (n % 2 != 0) {
    cout << abs(b[(n + 1) / 2] - a[(n + 1) / 2]) + 1;
  }
  if (n % 2 == 0) {
    double mida = (a[n / 2] + a[n / 2 + 1]) / 2;
    double midb = (b[n / 2] + b[n / 2 + 1]) / 2;
    double dist = abs(mida - midb);
    cout << dist * 2 + 1;
  }
  return 0;
}
Problem p02661 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n, a[N], b[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  reverse(b, b + n);
  if (n & 1) {
    cout << b[n / 2] - a[n / 2] + 1;
  } else {
    pair<ll, ll> le = {a[n / 2 - 1], b[n / 2]};
    pair<ll, ll> re = {a[n / 2], b[n / 2 - 1]};
    cout << (le.second + re.second) - (le.first + re.first) + 1;
  }
  return 0;
}

Error: -6
0c86c144-250f-4d4f-af7f-7348e9342840.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02661/C++/s947088133.cpp:20: int main(): Assertion `false' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n, a[N], b[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  reverse(b, b + n);
  if (n & 1) {
    cout << b[n / 2] - a[n / 2] + 1;
  } else {
    assert(false);
  }
  return 0;
}
Problem p02661 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
using ll = long long;

const int MAX_N = 2e5 + 10;
int N;
int A[MAX_N], B[MAX_N];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i] >> B[i];
  }
  ll mid_a = 0;
  ll mid_b = 0;
  ll result = 0;
  sort(A, A + N);
  sort(B, B + N);
  if (N % 2 == 0) {
    mid_a = (A[N / 2 - 1] + A[N / 2]);
    mid_b = (B[N / 2 - 1] + B[N / 2]);
    result = (mid_b - mid_a) + 1;
  } else {
    mid_a = A[N / 2];
    mid_b = B[N / 2];
    result = mid_b - mid_a + 1;
  }
  cout << result << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
using ll = long long;

const int MAX_N = 1e5 + 10;
int N;
int A[MAX_N], B[MAX_N];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i] >> B[i];
  }
  ll mid_a = 0;
  ll mid_b = 0;
  ll result = 0;
  if (N % 2 == 0) {
    mid_a = (A[N / 2 - 1] + A[N / 2]);
    mid_b = (B[N / 2 - 1] + B[N / 2]);
    result = (mid_b - mid_a) + 1;
  } else {
    mid_a = A[N / 2];
    mid_b = B[N / 2];
    result = mid_b - mid_a + 1;
  }
  cout << result << endl;
}
Problem p02661 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

int main() {
  int n;
  cin >> n;
  vector<ll> l(n), r(n);
  rep(i, 0, n) cin >> l[i] >> r[i];

  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  if (n % 2 == 0) {
    cout << r[n / 2] + r[n / 2 - 1] - (l[n / 2] + l[n / 2 - 1]) + 1;
  } else {
    cout << r[n / 2] - l[n / 2] + 1;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

class Enumeration {
public:
  explicit Enumeration(int n, uint64_t mod)
      : mod(mod), fact(n), inv(n), finv(n) {
    fact[0] = finv[0] = inv[1] = 1;
    for (int i = 2; i < n; ++i)
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    for (int i = 1; i < n; ++i) {
      fact[i] = fact[i - 1] * i % mod;
      finv[i] = finv[i - 1] * inv[i] % mod;
    }
  }
  uint64_t operator()(int n, int k) {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fact[n] * finv[n - k] % mod * finv[k] % mod;
  }
  uint64_t modpow(uint64_t x, uint64_t e) {
    uint64_t res = 1;
    while (e > 0) {
      if (e & 1)
        res = res * x % mod;
      x = x * x % mod;
      e >>= 1;
    }
    return res;
  }

private:
  uint64_t mod;
  vector<uint64_t> fact, inv, finv;
};

constexpr ll MOD = 1000000007;
int main() {
  int n, m;
  cin >> n >> m;

  unordered_map<int, int> mp;
  for (int i = 2; i * i <= m; ++i) {
    while (m % i == 0) {
      m /= i;
      ++mp[i];
    }
  }
  if (m != 1)
    ++mp[m];

  Enumeration c(17, MOD);
  ll ans = 1;
  for (auto p : mp)
    ans = ans * c(n - 1 + p.second, n - 1) % MOD;

  cout << ans << '\\n';
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 998244353LL
#define endl "\\n"

ll modbeki(ll x, ll n) {
  ll res = 1LL;
  while (n > 0) {
    if (n & 1) {
      res *= x % MOD;
      res %= MOD;
    }
    x = x * x % MOD;
    n >>= 1;
  }
  return res % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, S;
  cin >> N >> S;
  vll A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vvll dp(N, vll(S + 1, 0));
  dp.at(0).at(0) = 1;
  for (ll i = 0; i < N; i++) {
    dp.at(i).at(0) = 1;
  }
  if (A.at(0) <= S)
    dp.at(0).at(A.at(0)) = 1;
  for (ll i = 1; i < N; i++) {
    if (A.at(i) <= S)
      dp.at(i).at(A.at(i)) = 1;
    for (ll j = 0; j <= S; j++) {
      dp.at(i).at(j) += dp.at(i - 1).at(j) * 2;
      dp.at(i).at(j) %= MOD;
      if (j + A.at(i) <= S) {
        dp.at(i).at(j + A.at(i)) += dp.at(i - 1).at(j);
        dp.at(i).at(j + A.at(i)) %= MOD;
      }
    }
  }
  cout << dp.at(N - 1).at(S) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 998244353LL
#define endl "\\n"

ll modbeki(ll x, ll n) {
  ll res = 1LL;
  while (n > 0) {
    if (n & 1) {
      res *= x % MOD;
      res %= MOD;
    }
    x = x * x % MOD;
    n >>= 1;
  }
  return res % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, S;
  cin >> N >> S;
  vll A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vvll dp(N, vll(S + 1, 0));
  dp.at(0).at(0) = 1;
  if (A.at(0) <= S)
    dp.at(0).at(A.at(0)) = 1;
  for (ll i = 1; i < N; i++) {
    dp.at(i).at(0) = 1;
    if (A.at(0) <= S)
      dp.at(i).at(A.at(i)) = 1;
    for (ll j = 0; j <= S; j++) {
      dp.at(i).at(j) += dp.at(i - 1).at(j) * 2;
      if (j + A.at(i) <= S)
        dp.at(i).at(j + A.at(i)) += dp.at(i - 1).at(j);
    }
  }
  cout << dp.at(N - 1).at(S) << endl;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll mod = 998244353;

int main() {
  int n, s;
  cin >> n >> s;
  vector<vector<ll>> dp(n, vector<ll>(3005));
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  dp[0][0] = 2;
  dp[0][a[0]] = 1;
  rep(i, n - 1) {
    rep(j, s + 1) {
      dp[i + 1][j] = dp[i][j] * 2 % mod;
      if (j >= a[i + 1])
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j - a[i + 1]]) % mod;
    }
  }
  ll ans = dp[n - 1][s];
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll mod = 998244353;

int main() {
  int n, s;
  cin >> n >> s;
  vector<vector<ll>> dp(n, vector<ll>(s + 5));
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  dp[0][0] = 2;
  dp[0][a[0]] = 1;
  rep(i, n - 1) {
    rep(j, s + 1) {
      dp[i + 1][j] = dp[i][j] * 2 % mod;
      if (j >= a[i + 1])
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j - a[i + 1]]) % mod;
    }
  }
  ll ans = dp[n - 1][s];
  cout << ans << endl;
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int s;
  int i;
  int j;
  int k;
  int n;
  int a[3010];
  long long np[3010][3010];

  cin >> n >> s;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < s + 1; j++) {
      np[i][j] = 0;
    }
  }
  np[0][0] = 2;
  np[0][a[0]] = 1;
  for (i = 1; i < n; i++) {
    for (j = 0; j <= s; j++) {
      np[i][j] = 2 * np[i - 1][j];
    }
    for (j = 0; j <= s - a[i]; j++) {
      k = j + a[i];
      np[i][k] = np[i][k] + np[i - 1][j];
    }
    for (j = 0; j <= s; j++) {
      np[i][j] = np[i][j] % 998244353;
    }
  }
  cout << np[n - 1][s];
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int kotae;
int n;
int a[3010];
int b[3010];

int oriori(int ii, int s, int count) {
  int i;

  if (ii == n) {
    return 0;
  }
  if (s == a[ii]) {
    kotae = kotae + b[n - count - 1];
    kotae = kotae % 998244353;
    oriori(ii + 1, s, count);
  } else if (s > a[ii]) {
    oriori(ii + 1, s - a[ii], count + 1);
    oriori(ii + 1, s, count);
  }
  return 0;
}

int main() {
  int s;
  int i;
  int p;

  cin >> n >> s;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  p = 1;
  b[0] = 1;
  for (i = 1; i < n; i++) {
    p = p * 2;
    p = p % 998244353;
    b[i] = p;
  }
  sort(a, a + n); // a[0:n] を大きい順にソート
  kotae = 0;
  p = oriori(0, s, 0);
  cout << kotae << endl;
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 998244353;

vector<lli> solve(int b, int e, lli s, vector<lli> &a) {
  vector<lli> res(s + 1, 0);
  if (e - b == 1) {
    res[0] = 2;
    if (a[b] <= s) {
      res[a[b]] = 1;
    }
    return res;
  }

  int mid = (b + e) / 2;
  auto ret1 = solve(b, mid, s, a);
  auto ret2 = solve(mid, e, s, a);
  for (int i = 0; i <= s; i++) {
    for (int j = 0; j <= i; j++) {
      res[i] += (ret1[j] * ret2[i - j]) % mod;
      res[i] %= mod;
    }
  }
  /*
  cerr << b << "," << e << endl;
  for(int i=0; i<=s; i++){
      cerr << res[i] << " ";
  }
  cerr << endl;
  */
  return res;
}

int main() {
  lli n, s;
  cin >> n >> s;
  vector<lli> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<lli> dp(s + 1, 0);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    vector<lli> ndp(s + 1, 0);
    for (int j = 0; j <= s; j++) {
      ndp[j] += (2 * dp[j]) % mod;
      ndp[j] %= mod;
    }
    for (int j = 0; j + a[i] <= s; j++) {
      ndp[j + a[i]] += dp[j];
      ndp[j + a[i]] %= mod;
    }
    swap(dp, ndp);
  }
  cout << dp[s] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 998244353;

vector<lli> solve(int b, int e, lli s, vector<lli> &a) {
  vector<lli> res(s + 1, 0);
  if (e - b == 1) {
    res[0] = 2;
    if (a[b] <= s) {
      res[a[b]] = 1;
    }
    return res;
  }

  int mid = (b + e) / 2;
  auto ret1 = solve(b, mid, s, a);
  auto ret2 = solve(mid, e, s, a);
  for (int i = 0; i <= s; i++) {
    for (int j = 0; j <= i; j++) {
      res[i] += (ret1[j] * ret2[i - j]) % mod;
      res[i] %= mod;
    }
  }
  /*
  cerr << b << "," << e << endl;
  for(int i=0; i<=s; i++){
      cerr << res[i] << " ";
  }
  cerr << endl;
  */
  return res;
}

int main() {
  lli n, s;
  cin >> n >> s;
  vector<lli> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  auto ret = solve(0, n, s, a);
  cout << ret[s] << endl;
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

const int mod = 998244353;

long long int mypow(long long int a, long long int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int res = mypow(a, b / 2);
  res = res * res % mod;
  if (b % 2 == 1)
    res = res * a % mod;
  return res;
}

int main() {
  int n, s;
  cin >> n >> s;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int t;
    scanf("%d", &t);
    v.push_back(t);
  }
  unordered_map<int, long long int> m1, m2;
  // [sum] = number
  m1[0] = 1;
  long long int res = 0;
  for (int i = 0; i < n; i++) {
    m2 = m1;
    for (auto it = m2.begin(); it != m2.end(); it++) {
      it->second *= 2;
      it->second %= mod;
    }
    for (auto p : m1) {
      if (p.first + v[i] <= s) {
        m2[p.first + v[i]] += p.second;
        m2[p.first + v[i]] %= mod;
      }
    }
    m1 = m2;
  }
  cout << m1[s] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

const int mod = 998244353;

long long int mypow(long long int a, long long int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int res = mypow(a, b / 2);
  res = res * res % mod;
  if (b % 2 == 1)
    res = res * a % mod;
  return res;
}

int main() {
  int n, s;
  cin >> n >> s;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }
  unordered_map<int, unordered_map<int, long long int>> m;
  // [how many element][sum]
  m[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = i; j >= 0; j--) {
      for (auto p : m[j]) {
        if (p.first + v[i] <= s) {
          m[j + 1][p.first + v[i]] += p.second;
          m[j + 1][p.first + v[i]] %= mod;
        }
      }
    }
  }
  long long int res = 0;
  for (int i = 0; i <= n; i++) {
    if (m[i][s] > 0) {
      res += m[i][s] * mypow(2, n - i) % mod;
      res %= mod;
    }
  }
  cout << res << endl;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t

const static ll MOD = 998244353;
long long modpow(long long a, long long n, long long mod = MOD) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, S;
  cin >> N >> S;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> DP(N + 1, vector<ll>(5000 + 1, 0));
  DP[0][A[0]] = 1;
  for (int i = 1; i < N; i++) {
    ll a = A[i];
    for (int j = 0; j <= S; j++) {
      DP[i][j] += DP[i - 1][j] * 2;
      DP[i][j] %= MOD;
      if (j == a)
        DP[i][j] += modpow(2, i);
      DP[i][j] %= MOD;
      if (j - a >= 0)
        DP[i][j] += DP[i - 1][j - a];
      DP[i][j] %= MOD;
    }
  }
  cout << DP[N - 1][S] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t

const static ll MOD = 998244353;
long long modpow(long long a, long long n, long long mod = MOD) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, S;
  cin >> N >> S;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> DP(N + 1, vector<ll>(S + 1, 0));
  DP[0][A[0]] = 1;
  for (int i = 1; i < N; i++) {
    ll a = A[i];
    for (int j = 0; j <= S; j++) {
      DP[i][j] += DP[i - 1][j] * 2;
      DP[i][j] %= MOD;
      if (j == a)
        DP[i][j] += modpow(2, i);
      DP[i][j] %= MOD;
      if (j - a >= 0)
        DP[i][j] += DP[i - 1][j - a];
      DP[i][j] %= MOD;
    }
  }
  cout << DP[N - 1][S] << endl;
}
Problem p02662 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long N, S;
  cin >> N >> S;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<long long> dp(S + 1);
  dp[0] = 1;
  for (int i = 0; i < N; ++i) {
    vector<long long> p(S + 1);
    swap(dp, p);
    for (int j = 0; j < S + 1; ++j) {
      dp[j] += p[j] * 2;
      dp[j] %= 998244353;
      if (j + A[i] <= S) {
        dp[j + A[i]] += p[j];
        dp[j + A[i]] %= 998244353;
      }
    }
  }

  cout << dp[S] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<int> dp(S + 1);
  dp[0] = 1;
  for (int i = 0; i < N; ++i) {
    vector<int> p(S + 1);
    swap(dp, p);
    for (int j = 0; j < S + 1; ++j) {
      dp[j] += p[j] * 2;
      dp[j] = dp[j] % 998244353;
      if (j + A[i] <= S) {
        dp[j + A[i]] += p[j];
        dp[j + A[j]] = dp[j + A[j]] % 998244353;
      }
    }
  }

  cout << dp[S] << endl;
  return 0;
}
Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define MOD 998244353
using namespace std;
typedef long long ll;

vector<int> v;

ll exp(ll a, ll p) {
  if (!a)
    return 0;
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * a) % MOD;
    a = (a * a) % MOD;
    p >>= 1;
  }
  return ans;
}

vector<vector<ll>> memo;
ll solve(int n, int s) {
  if (memo[n][s] != -1)
    return memo[n][s];
  if (s == 0)
    return exp(2, n);
  if (n == 0)
    return 0;

  ll lhs = solve(n - 1, s), rhs = 0;
  if (s >= v[n - 1])
    rhs = solve(n - 1, s - v[n - 1]);

  lhs = lhs * 2 % MOD;
  return memo[n][s] = (lhs + rhs) % MOD;
}

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

  memo = vector<vector<ll>>(n + 1, vector<ll>(s + 1, -1));

  v.resize(n);
  forn(i, n) cin >> v[i];

  cout << solve(n, s) << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define MOD 998244353
using namespace std;
typedef long long ll;

vector<int> v;

ll exp(ll a, ll p) {
  if (!a)
    return 0;
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * a) % MOD;
    a = (a * a) % MOD;
    p >>= 1;
  }
  return ans;
}

ll solve(int n, int s) {
  if (s == 0)
    return exp(2, n);
  if (n == 0)
    return 0;

  ll lhs = solve(n - 1, s), rhs = 0;
  if (s >= v[n - 1])
    rhs = solve(n - 1, s - v[n - 1]);

  lhs = lhs * 2 % MOD;
  return (lhs + rhs) % MOD;
}

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

  v.resize(n);
  forn(i, n) cin >> v[i];

  cout << solve(n, s) << "\\n";
}