Problem p02662 (C++) — buggy submission, verdict: Runtime Error


// Problem : F - Knapsack for All Subsets
// Contest : AtCoder - AtCoder Beginner Contest 169
// URL : https://atcoder.jp/contests/abc169/tasks/abc169_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
// #include "testlib.h"
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define M 1000000007
#define MM 998244353
#define inputarr(a, n)                                                         \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> a[i]
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define mii map<ll, ll>
#define msi map<string, ll>
#define rep(a, b) for (ll i = a; i < b; i++)
#define rep0(n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define ppb pop_back
#define endl '\\n'
#define asdf                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define inputoutput                                                            \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define Set(a, s) (a, s, sizeof(a))
#define FOR repi
#define vii vector<pii>
#define pii pair<int, int>
#define REVERSE(v) reverse(all(v))
#define trav(a, x) for (auto &a : x)
#define display(x)                                                             \\
  trav(a, x) cout << a << " ";                                                 \\
  cout << endl
#define debug cerr << "bhau" << endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}

ll max(ll a, ll b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }

int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}

int solve() {

  int n, s;
  cin >> n >> s;
  int a[n];
  inputarr(a, n);
  // int dp[2*s];
  vi dp(2 * s + 1, 0);
  // memset(dp,0,sizeof(dp));
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    vi temp(2 * s + 1, 0);
    for (int j = 0; j < s; j++) {
      if (a[i] + j > s)
        break;
      if (j == 0) {
        temp[a[i]] = power(2, i, MM);
      } else
        temp[a[i] + j] += dp[j];
      temp[a[i] + j] %= MM;
    }
    for (int j = 1; j <= s; j++) {
      dp[j] *= 2;
      dp[j] += temp[j];
      dp[j] %= MM;
    }
    // trace(dp[a[i]],temp[a[i]]);
  }
  cout << dp[s];

  return 0;
}
signed main() {
  asdf int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:


// Problem : F - Knapsack for All Subsets
// Contest : AtCoder - AtCoder Beginner Contest 169
// URL : https://atcoder.jp/contests/abc169/tasks/abc169_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
// #include "testlib.h"
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define M 1000000007
#define MM 998244353
#define inputarr(a, n)                                                         \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> a[i]
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define mii map<ll, ll>
#define msi map<string, ll>
#define rep(a, b) for (ll i = a; i < b; i++)
#define rep0(n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define ppb pop_back
#define endl '\\n'
#define asdf                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define inputoutput                                                            \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define Set(a, s) (a, s, sizeof(a))
#define FOR repi
#define vii vector<pii>
#define pii pair<int, int>
#define REVERSE(v) reverse(all(v))
#define trav(a, x) for (auto &a : x)
#define display(x)                                                             \\
  trav(a, x) cout << a << " ";                                                 \\
  cout << endl
#define debug cerr << "bhau" << endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}

ll max(ll a, ll b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }

int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}

int solve() {

  int n, s;
  cin >> n >> s;
  int a[n];
  inputarr(a, n);
  // int dp[2*s];
  vi dp(2 * s + 1, 0);
  // memset(dp,0,sizeof(dp));
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    vi temp(2 * s + 1, 0);
    for (int j = 0; j < s; j++) {
      if (j == 0) {
        temp[a[i]] = power(2, i, MM);
      } else
        temp[a[i] + j] += dp[j];
      temp[a[i] + j] %= MM;
    }
    for (int j = 1; j <= s; j++) {
      dp[j] *= 2;
      dp[j] += temp[j];
      dp[j] %= MM;
    }
    // trace(dp[a[i]],temp[a[i]]);
  }
  cout << dp[s];

  return 0;
}
signed main() {
  asdf int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}