prithivMLmods/Coder-Stat huggingface dataset:
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PL = pair<ll, ll>;
// Welcome to my source code!

const int INF = 1e9;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  ll ans = INF;
  for (int bit = 0; bit < (1 << h - 1); bit++) {
    ll cnt = __builtin_popcount(bit);
    int nb = bit | (1 << h - 1);
    vector<int> prev(10, 0);
    bool div = false;
    bool over = false;
    for (int j = 0; j < w; j++) {
      vector<int> cur(10);
      if (div) {
        cnt++;
        for (int i = 0; i < 10; i++)
          cur[i] = 0;
        div = false;
      } else {
        for (int i = 0; i < 10; i++)
          cur[i] = prev[i];
      }
      int idx = 0;
      for (int i = 0; i < h; i++) {
        if (s[i][j] == '1')
          cur[idx]++;
        if (cur[idx] > k) {
          if (div) {
            over = true;
            break;
          }
          div = true;
          break;
        }
        if (nb & (1 << i))
          idx++;
        if (i == h - 1 && div)
          div = false;
      }
      if (over)
        break;
      if (div) {
        j--;
        continue;
      } else {
        for (int i = 0; i < 10; i++)
          prev[i] = cur[i];
      }
    }
    if (over)
      continue;
    ans = min(ans, cnt);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PL = pair<ll, ll>;
// Welcome to my source code!

const int INF = 1e9;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  ll ans = INF;
  for (int bit = 0; bit < (1 << h - 1); bit++) {
    ll cnt = __builtin_popcount(bit);
    int nb = bit | (1 << h - 1);
    vector<int> prev(10, 0);
    bool div = false;
    bool over = false;
    for (int j = 0; j < w; j++) {
      vector<int> cur(10);
      if (div) {
        cnt++;
        for (int i = 0; i < 10; i++)
          cur[i] = 0;
      } else {
        for (int i = 0; i < 10; i++)
          cur[i] = prev[i];
      }
      int idx = 0;
      for (int i = 0; i < h; i++) {
        if (s[i][j] == '1')
          cur[idx]++;
        if (cur[idx] > k) {
          if (div) {
            over = true;
            break;
          }
          div = true;
          break;
        }
        if (nb & (1 << i))
          idx++;
        if (i == h - 1 && div)
          div = false;
      }
      if (over)
        break;
      if (div) {
        j--;
        continue;
      } else {
        for (int i = 0; i < 10; i++)
          prev[i] = cur[i];
      }
    }
    if (over)
      continue;
    ans = min(ans, cnt);
  }
  cout << ans << endl;
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fs first
#define sc second

void reset(vector<int> &v) {
  rep(i, v.size()) { v[i] = 0; }
}

int main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool>> s(h, vector<bool>(w, false));
  rep(i, h) {
    string tmp;
    cin >> tmp;
    rep(j, w) {
      if (tmp[j] == '1')
        s[i][j] = true;
    }
  }
  int ans = h + w;
  rep(c, 1 << (h - 1)) {
    bitset<9> b(c);
    int cut = b.count();
    vector<int> blocks(cut + 1, 0);
    int wc = 0;
    int hc = 0;
    int bc = 0;
    bool cutted = false;
    while (1) {
      blocks[bc] += s[hc][wc];
      if (blocks[bc] > k) {
        if (cutted) {
          cut = h + w;
          break;
        }
        cut++;
        cutted = true;
        bc = 0;
        hc = 0;
        reset(blocks);
        continue;
      }
      if (b.test(hc)) {
        bc++;
      }
      hc++;
      if (hc == h) {
        bc = 0;
        hc = 0;
        wc++;
        cutted = false;
        if (wc == w)
          break;
      }
    }
    ans = min(ans, cut);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fs first
#define sc second

void reset(vector<int> &v) {
  rep(i, v.size()) { v[i] = 0; }
}

int main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool>> s(h, vector<bool>(w, false));
  rep(i, h) {
    string tmp;
    cin >> tmp;
    rep(j, w) {
      if (tmp[j] == '1')
        s[i][j] = true;
    }
  }
  int ans = h + w;
  rep(c, 1 << (h - 1)) {
    bitset<9> b(c);
    int cut = b.count();
    vector<int> blocks(cut + 1, 0);
    int wc = 0;
    int hc = 0;
    int bc = 0;
    bool cutted = false;
    while (1) {
      blocks[bc] += s[hc][wc];
      if (blocks[bc] > k) {
        if (cutted) {
          cut = h + w;
          break;
        }
        cut++;
        cutted = true;
        bc = 0;
        hc = 0;
        reset(blocks);
        continue;
      }
      if (hc < h - 1) {
        if (b.test(hc)) {
          bc++;
        }
      }
      hc++;
      if (hc == h) {
        bc = 0;
        hc = 0;
        wc++;
        cutted = false;
        if (wc == w)
          break;
      }
    }
    ans = min(ans, cut);
  }
  cout << ans << endl;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
ll MOD = 998244353;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  ll N, S;
  cin >> N >> S;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<ll> coef(S + 5, 0);

  ll ans = 0;

  rep(i, N) {
    vector<ll> now(S + 5);
    ++now[0];
    ++now[A[i]];
    rep(j, S + 5) {
      now[j] += coef[j];
      if (j + A[i] <= S)
        now[j + A[i]] += coef[j];

      now[j] %= MOD;
    }
    coef = now;

    ans += coef[S], ans %= MOD;
  }

  print(ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
ll MOD = 998244353;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  ll N, S;
  cin >> N >> S;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<ll> coef(S + 5, 0);

  ll ans = 0;

  rep(i, N) {
    vector<ll> now(S + 5);
    ++now[0];
    if (A[i] <= S)
      ++now[A[i]];
    rep(j, S + 5) {
      now[j] += coef[j];
      if (j + A[i] <= S)
        now[j + A[i]] += coef[j];

      now[j] %= MOD;
    }
    coef = now;

    ans += coef[S], ans %= MOD;
  }

  print(ans);

  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < (b); i++)
#define repr(i, b, a) for (int i = int(b); i >= (a); i--)
#define all(x) x.begin(), x.end()

const ll mod = 998244353;
const ll INF = 1e9;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool valid(int lx, int ux, int ly, int uy, int x, int y) {
  return lx <= x && x < ux && ly <= y && y < uy;
}
ll power(ll x, ll p) {
  if (p == 0)
    return 1;
  ll res = power(x * x % mod, p / 2);
  if (p % 2 == 1)
    res = res * x % mod;
  return res;
}

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

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

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < (b); i++)
#define repr(i, b, a) for (int i = int(b); i >= (a); i--)
#define all(x) x.begin(), x.end()

const ll mod = 998244353;
const ll INF = 1e9;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool valid(int lx, int ux, int ly, int uy, int x, int y) {
  return lx <= x && x < ux && ly <= y && y < uy;
}
ll power(ll x, ll p) {
  if (p == 0)
    return 1;
  ll res = power(x * x % mod, p / 2);
  if (p % 2 == 1)
    res = res * x % mod;
  return res;
}

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

  vector<ll> dp(3030, 0);
  ll ans = 0;
  rep(i, n) {
    repr(j, s, a[i]) {
      if (j == a[i]) {
        dp[j] += i + 1;
        dp[j] %= mod;
      } else {
        dp[j] += dp[j - a[i]];
        dp[j] %= mod;
      }
    }
    ans += dp[s];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2010, P = 998244353;
int n, s, a[MAXN], dp[MAXN], np[MAXN];
ll ans;
int main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    for (int j = s; j >= 1; j--) {
      dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % P;
    }
    dp[a[i]] = (dp[a[i]] + i) % P;
    np[i] = dp[s];
    ans = (ans + (1ll * ((np[i] - np[i - 1] + P) % P) * (n - i + 1))) % P;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 6010, P = 998244353;
int n, s, a[MAXN], dp[MAXN], np[MAXN];
ll ans;
int main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    for (int j = s; j >= 1; j--) {
      dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % P;
    }
    dp[a[i]] = (dp[a[i]] + i) % P;
    np[i] = dp[s];
    ans = (ans + (1ll * ((np[i] - np[i - 1] + P) % P) * (n - i + 1))) % P;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)

/*
https://maspypy.com/atcoder-%E5%8F%82%E5%8A%A0%E6%84%9F%E6%83%B3-2019-03-22abc-159


ans = sum_(i<=j){ f(i,j) }

    = f(1,1) +f(1,2) +f(1,3) +...
             +f(2,2) +f(2,3) +...
                     +f(3,3) +...

形式的べき級数で考えると


f(i,j-1) {i<j} について *(x^A[j] + 1) をやることで f(i,j) の分を足す
(x^A[j] + 1)を足すことで f(j,j) を足す

*/

int main() {
  ll ans = 0;
  ll mod = 998244353;
  ll N, S;
  cin >> N >> S;
  vector<ll> coefficient(S + 1, 0);
  FOR(i, 0, N) {
    ll a;
    cin >> a;
    vector<ll> t = coefficient;
    FOR(j, a, S + 1) { (t[j] += coefficient[j - a]) %= mod; }
    t[a]++;
    t[0]++;
    coefficient = t;
    (ans += coefficient[S]) %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)

/*
https://maspypy.com/atcoder-%E5%8F%82%E5%8A%A0%E6%84%9F%E6%83%B3-2019-03-22abc-159


ans = sum_(i<=j){ f(i,j) }

    = f(1,1) +f(1,2) +f(1,3) +...
             +f(2,2) +f(2,3) +...
                     +f(3,3) +...

形式的べき級数で考えると


f(i,j-1) {i<j} について *(x^A[j] + 1) をやることで f(i,j) の分を足す
(x^A[j] + 1)を足すことで f(j,j) を足す

*/

int main() {
  ll ans = 0;
  ll mod = 998244353;
  ll N, S;
  cin >> N >> S;
  vector<ll> coefficient(3003, 0);
  FOR(i, 0, N) {
    ll a;
    cin >> a;
    vector<ll> t = coefficient;
    FOR(j, a, S + 1) { (t[j] += coefficient[j - a]) %= mod; }
    t[a]++;
    t[0]++;
    coefficient = t;
    (ans += coefficient[S]) %= mod;
  }
  cout << ans << endl;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
using namespace std;
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb(X) push_back(X)
#define x first
#define y second
#define all(x) (x).begin(), (x).end()
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);
typedef long long ll;
typedef vector<int> VI;
typedef vector<long long> VL;
typedef pair<int, int> pii;
const ll MOD = 998244353;
// ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0);
// for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int INF = 1e9 + 7;
int _;
int n, k;
ll ans;

int main() {
  INIT cin >> n >> k;
  VI A(n);
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 2));
  rep(i, 0, n) { cin >> A[i]; }
  rep(i, 0, n) {
    dp[i + 1] = dp[i];
    (dp[i + 1][A[i]] += i + 1) %= MOD;
    for (int j = 0; j + A[i] <= k; ++j) {
      (dp[i + 1][j + A[i]] += dp[i][j]) %= MOD;
    }
    (ans += dp[i + 1][k]) %= MOD;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
using namespace std;
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb(X) push_back(X)
#define x first
#define y second
#define all(x) (x).begin(), (x).end()
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);
typedef long long ll;
typedef vector<int> VI;
typedef vector<long long> VL;
typedef pair<int, int> pii;
const ll MOD = 998244353;
// ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0);
// for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int INF = 1e9 + 7;
int _;
int n, k;
ll ans;

int main() {
  INIT cin >> n >> k;
  VI A(n);
  vector<vector<ll>> dp(n + 1, vector<ll>(3030));
  rep(i, 0, n) { cin >> A[i]; }
  rep(i, 0, n) {
    dp[i + 1] = dp[i];
    (dp[i + 1][A[i]] += i + 1) %= MOD;
    for (int j = 0; j + A[i] <= k; ++j) {
      (dp[i + 1][j + A[i]] += dp[i][j]) %= MOD;
    }
    (ans += dp[i + 1][k]) %= MOD;
  }
  cout << ans;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll mod = 998244353;
vector<int> a(4000);
int n, s;
ll dp[4000][4000][3];
int ways(int l, int r, int S) {
  if (S == 0) {
    return 1;
  }
  if (r == l) {
    return 0;
  }
  return ways(l, r - 1, S) + (S >= a[r - 1] ? ways(l, r - 1, S - a[r - 1]) : 0);
}

int main() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  /*
   int ans  = 0;
  for(int i = 0;i<n;++i){
      for(int j = i;j < n;++j){
          ans += ways(i,j+1,s);
          if(ans >= mod){
              ans-=mod;
          }
      }
  }

  cout << ans <<"\n";
  */
  auto add = [&](ll &x, ll y) {
    x += y;
    x %= y;
  };

  memset(dp, 0LL, sizeof dp);
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= s; ++j) {
      add(dp[i + 1][j][0], dp[i][j][0]);
      add(dp[i + 1][j][1], dp[i][j][0] + dp[i][j][1]);
      add(dp[i + 1][j][2], dp[i][j][0] + dp[i][j][1] + dp[i][j][2]);
      if (j + a[i] <= s) {
        add(dp[i + 1][j + a[i]][1], dp[i][j][0] + dp[i][j][1]);
        add(dp[i + 1][j + a[i]][2], dp[i][j][0] + dp[i][j][1]);
      }
    }
  }
  cout << dp[n][s][2] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll mod = 998244353;
vector<int> a(4000);
int n, s;
ll dp[4000][4000][3];
int ways(int l, int r, int S) {
  if (S == 0) {
    return 1;
  }
  if (r == l) {
    return 0;
  }
  return ways(l, r - 1, S) + (S >= a[r - 1] ? ways(l, r - 1, S - a[r - 1]) : 0);
}

int main() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  /*
   int ans  = 0;
  for(int i = 0;i<n;++i){
      for(int j = i;j < n;++j){
          ans += ways(i,j+1,s);
          if(ans >= mod){
              ans-=mod;
          }
      }
  }

  cout << ans <<"\n";
  */
  auto add = [&](ll &x, ll y) {
    x += y;
    x %= mod;
  };

  memset(dp, 0LL, sizeof dp);
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= s; ++j) {
      add(dp[i + 1][j][0], dp[i][j][0]);
      add(dp[i + 1][j][1], dp[i][j][0] + dp[i][j][1]);
      add(dp[i + 1][j][2], dp[i][j][0] + dp[i][j][1] + dp[i][j][2]);
      if (j + a[i] <= s) {
        add(dp[i + 1][j + a[i]][1], dp[i][j][0] + dp[i][j][1]);
        add(dp[i + 1][j + a[i]][2], dp[i][j][0] + dp[i][j][1]);
      }
    }
  }
  cout << dp[n][s][2] << "\n";
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#define MOD 998244353

int n, s;

int a[3010];
int memo[3010][3010][2];

int dp(int i, int num, int start) {
  if (num == 0) {
    return n - i + 1;
  }

  if (memo[i][num][start] != -1)
    return memo[i][num][start];

  int ans = 0;
  if (start == 0) {
    ans = dp(i + 1, num, 0);
    ans = (ans + dp(i + 1, num, 1)) % MOD;
    if (num - a[i] >= 0)
      ans = (ans + dp(i + 1, num - a[i], 1)) % MOD;
  } else {
    ans = dp(i + 1, num, 1);
    if (num - a[i] >= 0)
      ans = (ans + dp(i + 1, num - a[i], 1)) % MOD;
  }

  return memo[i][num][start] = ans;
}

signed main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  memset(memo, -1, sizeof(memo));

  cout << dp(0, s, 0) << endl;

  return 0;
}


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

#define int long long
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#define MOD 998244353

int n, s;

int a[3010];
int memo[3010][3010][2];

int dp(int i, int num, int start) {
  if (num == 0) {
    return n - i + 1;
  }

  if (i == n)
    return 0;

  if (memo[i][num][start] != -1)
    return memo[i][num][start];

  int ans = 0;
  if (start == 0) {
    ans = dp(i + 1, num, 0);
    ans = (ans + dp(i + 1, num, 1)) % MOD;
    if (num - a[i] >= 0)
      ans = (ans + dp(i + 1, num - a[i], 1)) % MOD;
  } else {
    ans = dp(i + 1, num, 1);
    if (num - a[i] >= 0)
      ans = (ans + dp(i + 1, num - a[i], 1)) % MOD;
  }

  return memo[i][num][start] = ans;
}

signed main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  memset(memo, -1, sizeof(memo));

  cout << dp(0, s, 0) << endl;

  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
const ll MOD = 998244353;
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]; }

  vector<map<ll, ll>> mp_v(N);
  ll ans = 0;
  rep(i, N) {
    if (i == 0) {
      mp_v[0][0] = 1;
      if (A[i] <= S) {
        ++mp_v[0][A[i]] = 1;
      }
    } else {
      for (auto v : mp_v[i - 1]) {
        if (v.first == 0) {
          mp_v[i][v.first] += v.second;
          if (v.first + A[i] < S) {
            mp_v[i][v.first + A[i]] += v.second;
            mp_v[i][v.first + A[i]] += i;
          }
          if (v.first + A[i] == S) {
            ans += (i + 1) * (N - i);
            ans = ans % MOD;
          }
        } else {
          mp_v[i][v.first] += v.second;
          if (v.first + A[i] < S) {
            mp_v[i][v.first + A[i]] += v.second;
            mp_v[i][v.first + A[i]] %= MOD;
          }
          if (v.first + A[i] == S) {
            ans += (v.second * (N - i)) % MOD;
            ans %= MOD;
          }
        }
      }
    }
    if (mp_v[i].find(S) != mp_v[i].end()) {
      ans += mp_v[i][S] * (N - i);
    }
  }

  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
const ll MOD = 998244353;
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]; }

  vector<unordered_map<ll, ll>> mp_v(N);
  ll ans = 0;
  rep(i, N) {
    if (i == 0) {
      mp_v[0][0] = 1;
      if (A[i] <= S) {
        ++mp_v[0][A[i]] = 1;
      }
    } else {
      for (auto v : mp_v[i - 1]) {
        if (v.first == 0) {
          mp_v[i][v.first] += v.second;
          if (v.first + A[i] < S) {
            mp_v[i][v.first + A[i]] += v.second;
            mp_v[i][v.first + A[i]] += i;
          }
          if (v.first + A[i] == S) {
            ans += (i + 1) * (N - i);
            ans = ans % MOD;
          }
        } else {
          mp_v[i][v.first] += v.second;
          if (v.first + A[i] < S) {
            mp_v[i][v.first + A[i]] += v.second;
            mp_v[i][v.first + A[i]] %= MOD;
          }
          if (v.first + A[i] == S) {
            ans += (v.second * (N - i)) % MOD;
            ans %= MOD;
          }
        }
      }
    }
    if (mp_v[i].find(S) != mp_v[i].end()) {
      ans += mp_v[i][S] * (N - i);
    }
  }

  cout << ans << "\n";
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ggdem = b; i < ggdem; ++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define mset(a, v) memset((a), (v), sizeof(a))
#define FIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long ll;

ll n, s;
ll a[3030];
ll MOD = 998244353;
ll dp[3030][3030];

ll f(ll x, ll y) {
  ll &r = dp[x][y];
  if (r >= 0)
    return r;
  if (y == a[x]) {
    r = x + 1;
    if (x > 0)
      r += f(x - 1, y), r %= MOD;
    return r;
  }
  if (x == 0) {
    return r = 0;
  }
  /*if(y==s){
          r=(f(x-1,y-a[x])*(n-x))%MOD;
          return r;
  }else{*/
  r = (f(x - 1, y) + f(x - 1, y - a[x])) % MOD;
  //	cout<<x<<" "<<y<<" "<<r<<"\n";
  return r;
  //}
}

int main() {
  FIN;
  cin >> n >> s;
  fore(i, 0, n) cin >> a[i];
  mset(dp, -1);
  ll res = 0;
  fore(i, 0, n) res += f(i, s), res %= MOD;
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ggdem = b; i < ggdem; ++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define mset(a, v) memset((a), (v), sizeof(a))
#define FIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long ll;

ll n, s;
ll a[3030];
ll MOD = 998244353;
ll dp[3030][3030];

ll f(ll x, ll y) {
  if (y < 0)
    return 0;
  ll &r = dp[x][y];
  if (r >= 0)
    return r;
  if (y == a[x]) {
    r = x + 1;
    if (x > 0)
      r += f(x - 1, y), r %= MOD;
    return r;
  }
  if (x == 0) {
    return r = 0;
  }
  /*if(y==s){
          r=(f(x-1,y-a[x])*(n-x))%MOD;
          return r;
  }else{*/
  r = (f(x - 1, y) + f(x - 1, y - a[x])) % MOD;
  //	cout<<x<<" "<<y<<" "<<r<<"\n";
  return r;
  //}
}

int main() {
  FIN;
  cin >> n >> s;
  fore(i, 0, n) cin >> a[i];
  mset(dp, -1);
  ll res = 0;
  fore(i, 0, n) res += f(i, s), res %= MOD;
  cout << res;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
#define ld long double
#define pb push_back
#define pf push_front
#define ft first
#define sc second
#define all(v) v.begin(), v.end()
using namespace std;
const int maxn = 3011, mod = 998244353;
int dp[maxn][maxn];
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, s;
  cin >> n >> s;
  vector<int> mas(n, 0);
  for (int d = 0; d < n; d++) {
    cin >> mas[d];
  }
  dp[0][0] = 1;
  for (int d = 1; d <= n; d++) {
    dp[d][0] = d + 1;
    dp[d][mas[d - 1]] = dp[d - 1][mas[d - 1]] + d;
    // cout << dp[d][0] << " ";
    for (int s1 = 1; s1 <= s; s1++) {
      if (s1 != mas[d - 1]) {
        dp[d][s1] = dp[d - 1][s1];
        if (s1 >= mas[d - 1]) {
          dp[d][s1] += dp[d - 1][s1 - mas[d - 1]];
        }
      }
      dp[d][s1] %= mod;
      // cout << dp[d][s1] << " ";
    }
    // cout << "\n";
  }
  int ans = 0;
  int tk = 1;
  for (int d = n - 1; d >= 0; d--) {
    ans += tk * dp[d][s - mas[d]];
    ans %= mod;
    tk++;
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define ld long double
#define pb push_back
#define pf push_front
#define ft first
#define sc second
#define all(v) v.begin(), v.end()
using namespace std;
const int maxn = 3011, mod = 998244353;
int dp[maxn][maxn];
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, s;
  cin >> n >> s;
  vector<int> mas(n, 0);
  for (int d = 0; d < n; d++) {
    cin >> mas[d];
  }
  dp[0][0] = 1;
  for (int d = 1; d <= n; d++) {
    dp[d][0] = d + 1;
    dp[d][mas[d - 1]] = dp[d - 1][mas[d - 1]] + d;
    // cout << dp[d][0] << " ";
    for (int s1 = 1; s1 <= s; s1++) {
      if (s1 != mas[d - 1]) {
        dp[d][s1] = dp[d - 1][s1];
        if (s1 >= mas[d - 1]) {
          dp[d][s1] += dp[d - 1][s1 - mas[d - 1]];
        }
      }
      dp[d][s1] %= mod;
      // cout << dp[d][s1] << " ";
    }
    // cout << "\n";
  }
  int ans = 0;
  int tk = 1;
  for (int d = n - 1; d >= 0; d--) {
    if (s >= mas[d]) {
      ans += tk * dp[d][s - mas[d]];
      ans %= mod;
    }
    tk++;
  }
  cout << ans;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <queue>
#include <vector>

using namespace std;
typedef long long int64;

const int P = 998244353;
const int MAXN = 3011;
int dp[MAXN];
int n, s, ans;

inline int add(int x, int y) {
  x += y;
  x -= x >= P ? P : 0;
  return x;
}
inline int mul(int x, int y) { return 1ll * x * y % P; }
inline void incr(int &x, int y) { x = add(x, y); }

int main(void) {
  cin >> n >> s;
  for (int i = 1; i <= n; ++i) {
    int v;
    cin >> v;
    for (int j = s - v; ~j; --j)
      incr(dp[j + v], dp[j]);
    incr(dp[v], i);
    incr(ans, dp[s]);
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <queue>
#include <vector>

using namespace std;
typedef long long int64;

const int P = 998244353;
const int MAXN = 3011;
int dp[MAXN];
int n, s, ans;

inline int add(int x, int y) {
  x += y;
  x -= x >= P ? P : 0;
  return x;
}
inline int mul(int x, int y) { return 1ll * x * y % P; }
inline void incr(int &x, int y) { x = add(x, y); }

int main(void) {
  cin >> n >> s;
  for (int i = 1; i <= n; ++i) {
    int v;
    cin >> v;
    for (int j = s - v; j >= 0; --j)
      incr(dp[j + v], dp[j]);
    incr(dp[v], i);
    incr(ans, dp[s]);
  }
  cout << ans << '\n';
  return 0;
}
Problem p02734 (Python)

Buggy submission (status: Runtime Error):
# https://atcoder.jp/contests/abc159/submissions/11139734


def main():
    MOD = 998244353

    N, S = map(int, input().split())
    (*a,) = map(int, input().split())

    dp = [0] * (S + 1)

    ret = 0
    for i, x in enumerate(a):
        dp[0] += 1
        ret = (ret + dp[S - x] * (N - i)) % MOD
        for k in range(S - 1, x - 1, -1):
            dp[k] = (dp[k] + dp[k - x]) % MOD

    print(ret)


if __name__ == "__main__":
    main()


Fixed submission:
# https://atcoder.jp/contests/abc159/submissions/11139734


def main():
    MOD = 998244353

    N, S = map(int, input().split())
    (*a,) = map(int, input().split())

    dp = [0] * (S + 1)

    ret = 0
    for i, x in enumerate(a):
        dp[0] += 1
        if x > S:
            continue
        ret = (ret + dp[S - x] * (N - i)) % MOD
        for k in range(S - 1, x - 1, -1):
            dp[k] = (dp[k] + dp[k - x]) % MOD

    print(ret)


if __name__ == "__main__":
    main()
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

constexpr Int mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Int n, s;
  cin >> n >> s;
  vector<Int> a(n);
  for (auto &i : a)
    cin >> i;
  vector<vector<Int>> dp(n, vector<Int>(s + 1, 0));
  Int ans = 0;
  for (Int i = 0; i < n; ++i) {
    if (i > 0) {
      for (Int j = 0; j <= s; ++j) {
        dp[i][j] += dp[i - 1][j];
        dp[i][j] %= mod;
        if (j + a[i] <= s) {
          dp[i][j + a[i]] += dp[i - 1][j];
          dp[i][j + a[i]] %= mod;
        }
      }
    }
    dp[i][a[i]] += i + 1;
    dp[i][a[i]] %= mod;
    ans += dp[i][s];
    ans %= mod;
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

constexpr Int mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Int n, s;
  cin >> n >> s;
  vector<Int> a(n);
  for (auto &i : a)
    cin >> i;
  vector<vector<Int>> dp(n, vector<Int>(s + 1, 0));
  Int ans = 0;
  for (Int i = 0; i < n; ++i) {
    if (i > 0) {
      for (Int j = 0; j <= s; ++j) {
        dp[i][j] += dp[i - 1][j];
        dp[i][j] %= mod;
        if (j + a[i] <= s) {
          dp[i][j + a[i]] += dp[i - 1][j];
          dp[i][j + a[i]] %= mod;
        }
      }
    }
    if (a[i] <= s) {
      dp[i][a[i]] += i + 1;
      dp[i][a[i]] %= mod;
    }
    ans += dp[i][s];
    ans %= mod;
  }
  cout << ans << "\n";
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert

const int MAX = 3005;
ll dp[MAX][MAX];

void solve() {
  mod = 998244353;
  ll n, s;
  cin >> n >> s;
  ll ans = 0;
  ll a[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    memcpy(dp[i], dp[i - 1], sizeof dp[0]);
    (dp[i][a[i]] += i) %= mod;
    for (ll j = 0; j + a[i] <= s; j++) {
      (dp[i][j + a[i]] += dp[i - 1][j]) %= mod;
    }
    ans += dp[i][s];
    ans %= mod;
  }
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input00.txt", "r", stdin);
#endif
  IOS;
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert

const int MAX = 3005;
ll dp[MAX][MAX];

void solve() {
  mod = 998244353;
  ll n, s;
  cin >> n >> s;
  ll ans = 0;
  ll a[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    memcpy(dp[i], dp[i - 1], sizeof dp[0]);
    (dp[i][a[i]] += i) %= mod;
    for (ll j = 0; j + a[i] <= s; j++) {
      (dp[i][j + a[i]] += dp[i - 1][j]) %= mod;
    }
    ans += dp[i][s];
    ans %= mod;
  }
  cout << ans << endl;
}

int main() {

  IOS;
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
// Etavioxy
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#define il inline
#define ll long long
#define rep(i, s, t) for (register int i = (s); i <= (t); i++)
#define rev_rep(i, s, t) for (register int i = (s); i >= (t); i--)
#define each(i, u) for (int i = head[u]; i; i = bow[i].nxt)
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define pt(x) putchar(x)
using namespace std;
il int ci() {
  register char ch;
  int f = 1;
  while (!isdigit(ch = getchar()))
    f = ch == '-' ? -1 : 1;
  register int x = ch ^ '0';
  while (isdigit(ch = getchar()))
    x = (x * 10) + (ch ^ '0');
  return f * x;
}

const int mod = 998244353;
enum { N = 3023 };

ll f[N];
int main() {
  int n = ci(), s = ci();
  ll ans = 0;
  rep(i, 1, n) {
    int x = ci();
    ans = (ans + (n - i + 1) * f[s - x]) % mod;
    rev_rep(j, s, x) f[j] = (f[j] + f[j - x]);
    f[x] += i;
  }
  printf("%lld\n", (ans + mod) % mod);
  return 0;
}


Fixed submission:
// Etavioxy
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#define il inline
#define ll long long
#define rep(i, s, t) for (register int i = (s); i <= (t); i++)
#define rev_rep(i, s, t) for (register int i = (s); i >= (t); i--)
#define each(i, u) for (int i = head[u]; i; i = bow[i].nxt)
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define pt(x) putchar(x)
using namespace std;
il int ci() {
  register char ch;
  int f = 1;
  while (!isdigit(ch = getchar()))
    f = ch == '-' ? -1 : 1;
  register int x = ch ^ '0';
  while (isdigit(ch = getchar()))
    x = (x * 10) + (ch ^ '0');
  return f * x;
}

const int mod = 998244353;
enum { N = 3023 };

ll f[N];
int main() {
  int n = ci(), s = ci();
  ll ans = 0;
  rep(i, 1, n) {
    int x = ci();
    if (x > s)
      continue;
    ans = (ans + (n - i + 1) * (f[s - x] + (x == s ? i : 0))) % mod;
    rev_rep(j, s, x) f[j] = (f[j] + f[j - x]) % mod;
    f[x] = (f[x] + i) % mod;
  }
  printf("%lld\n", (ans + mod) % mod);
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef vector<ll> vll;

#define repi(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) repi(i, 0, a)
#define rrep(i, a) for (ll i = a - 1; i >= 0; i--)
#define MOD 998244353

// debug
#define debug(arr)                                                             \
  cerr << #arr << "(l" << __LINE__ << ") : ";                                  \
  for (auto x : arr)                                                           \
    cerr << x << " ";                                                          \
  cerr << endl;

int main() {
  ll N, S;
  cin >> N >> S;
  vll A(N);
  rep(i, N) cin >> A[i];

  vector<vll> dp(2, vll(S + 1, 0));
  ll ans = 0;
  rep(i, N) {
    rep(s, S + 1) dp[(i + 1) % 2][s] = dp[i % 2][s];
    rep(s, S + 1 - A[i]) dp[(i + 1) % 2][s + A[i]] =
        (dp[(i + 1) % 2][s + A[i]] + dp[i % 2][s]) % MOD;
    dp[(i + 1) % 2][A[i]] = (dp[(i + 1) % 2][A[i]] + i + 1) % MOD;
    ans = (ans + dp[(i + 1) % 2][S]) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef vector<ll> vll;

#define repi(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) repi(i, 0, a)
#define rrep(i, a) for (ll i = a - 1; i >= 0; i--)
#define MOD 998244353

// debug
#define debug(arr)                                                             \
  cerr << #arr << "(l" << __LINE__ << ") : ";                                  \
  for (auto x : arr)                                                           \
    cerr << x << " ";                                                          \
  cerr << endl;

int main() {
  ll N, S;
  cin >> N >> S;
  vll A(N);
  rep(i, N) cin >> A[i];

  vector<vll> dp(2, vll(S + 1, 0));
  ll ans = 0;
  rep(i, N) {
    rep(s, S + 1) dp[(i + 1) % 2][s] = dp[i % 2][s];
    rep(s, S + 1 - A[i]) dp[(i + 1) % 2][s + A[i]] =
        (dp[(i + 1) % 2][s + A[i]] + dp[i % 2][s]) % MOD;
    if (A[i] <= S)
      dp[(i + 1) % 2][A[i]] = (dp[(i + 1) % 2][A[i]] + i + 1) % MOD;
    ans = (ans + dp[(i + 1) % 2][S]) % MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 3e3 + 10;
const int mod = 998244353;

int n, s;

int memo[maxn][maxn], vs[maxn];

using namespace std;

int sum(int x, int v) { return (x % mod + v % mod) % mod; }

int dp(int pos, int soma) {
  if (pos > n)
    return 0;
  if (soma == s)
    return n - pos + 1;
  int &val = memo[pos][soma];
  if (val != -1)
    return val;
  return val = sum(dp(pos + 1, soma), dp(pos + 1, soma + vs[pos]));
}

void solve() {
  memset(memo, -1, sizeof memo);
  cin >> n >> s;
  int ans = 0;
  for (int i = 0; i < n; ++i)
    cin >> vs[i];
  for (int i = 0; i < n; ++i) {
    // cout<<dp(i,0)<<endl;
    ans = sum(ans, dp(i, 0));
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 3e3 + 10;
const int mod = 998244353;

int n, s;

int memo[maxn][maxn], vs[maxn];

using namespace std;

int sum(int x, int v) { return (x % mod + v % mod) % mod; }

int dp(int pos, int soma) {
  if (pos > n)
    return 0;
  if (soma == s)
    return n - pos + 1;
  if (soma > s)
    return 0;
  int &val = memo[pos][soma];
  if (val != -1)
    return val;
  return val = sum(dp(pos + 1, soma), dp(pos + 1, soma + vs[pos]));
}

void solve() {
  memset(memo, -1, sizeof memo);
  cin >> n >> s;
  int ans = 0;
  for (int i = 0; i < n; ++i)
    cin >> vs[i];
  for (int i = 0; i < n; ++i) {
    // cout<<dp(i,0)<<endl;
    ans = sum(ans, dp(i, 0));
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 998244353;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int n, m;

int main() {
  cin >> n >> m;
  vl dp(m + 1);
  ll res = 0;
  for (int j = 0; j < n; j++) {
    int x;
    cin >> x;
    vl DP(m + 1);
    (DP[x] += j + 1) %= mod;
    for (int j = 0; j <= m - x; j++)
      (DP[j + x] += dp[j]) %= mod;
    (res += DP[m] * (n - j)) %= mod;
    for (int j = 0; j <= m; j++)
      (dp[j] += DP[j]) %= mod;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 998244353;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int n, m;

int main() {
  cin >> n >> m;
  vl dp(m + 1);
  ll res = 0;
  for (int j = 0; j < n; j++) {
    int x;
    cin >> x;
    if (x > m)
      continue;
    vl DP(m + 1);
    (DP[x] += j + 1) %= mod;
    for (int j = 0; j <= m - x; j++)
      (DP[j + x] += dp[j]) %= mod;
    (res += DP[m] * (n - j)) %= mod;
    for (int j = 0; j <= m; j++)
      (dp[j] += DP[j]) %= mod;
  }
  cout << res << endl;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

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

  ll ret = 0;
  vector<ll> dp(s + 1);
  for (int i = 0; i < n; i++) {
    dp[a[i]] += i + 1;
    for (int j = s; j >= a[i]; j--) {
      dp[j] += dp[j - a[i]];
      if (j - a[i] == a[i])
        dp[j] -= i + 1;
      dp[j] += MOD, dp[j] %= MOD;
    }
    ret += dp[s] % MOD, ret %= MOD;
  }

  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

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

  ll ret = 0;
  vector<ll> dp(s + 1);
  for (int i = 0; i < n; i++) {
    if (a[i] <= s) {
      dp[a[i]] += i + 1;
      for (int j = s; j >= a[i]; j--) {
        dp[j] += dp[j - a[i]];
        if (j - a[i] == a[i])
          dp[j] -= i + 1;
        dp[j] += MOD, dp[j] %= MOD;
      }
    }
    ret += dp[s] % MOD, ret %= MOD;
  }

  cout << ret << endl;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(n) n.begin(), n.end()
#define eb emplace_back
#define endl "\n"

const ll maxn = 2e3 + 50;

const ll maxx = 1e13;
const ll mod1 = 1000000007;
const ll mod = 998244353;

ll dp[maxn][maxn][4];
ll a[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("test.txt","r",stdin);
  // freopen("FRAC.INP", "r", stdin);
  //  freopen("WALKING.OUT", "w", stdout);
  ll n, s;
  cin >> n >> s;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      for (int k = 0; k <= 2; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j][0] = (dp[i][j][0] + dp[i - 1][j][0]) % mod;
      dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j][0] + dp[i - 1][j][1]) % mod;
      dp[i][j][2] =
          (dp[i][j][2] + dp[i - 1][j][0] + dp[i - 1][j][1] + dp[i - 1][j][2]) %
          mod;
      if (j >= a[i]) {
        dp[i][j][1] =
            (dp[i][j][1] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) %
            mod;
        dp[i][j][2] =
            (dp[i][j][2] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) %
            mod;
      }
    }
  }
  cout << dp[n][s][2];
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(n) n.begin(), n.end()
#define eb emplace_back
#define endl "\n"

const ll maxn = 3e3 + 50;

const ll maxx = 1e13;
const ll mod1 = 1000000007;
const ll mod = 998244353;

ll dp[maxn][maxn][4];
ll a[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("test.txt","r",stdin);
  // freopen("FRAC.INP", "r", stdin);
  //  freopen("WALKING.OUT", "w", stdout);
  ll n, s;
  cin >> n >> s;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      for (int k = 0; k <= 2; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j][0] = (dp[i][j][0] + dp[i - 1][j][0]) % mod;
      dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j][0] + dp[i - 1][j][1]) % mod;
      dp[i][j][2] =
          (dp[i][j][2] + dp[i - 1][j][0] + dp[i - 1][j][1] + dp[i - 1][j][2]) %
          mod;
      if (j >= a[i]) {
        dp[i][j][1] =
            (dp[i][j][1] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) %
            mod;
        dp[i][j][2] =
            (dp[i][j][2] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) %
            mod;
      }
    }
  }
  cout << dp[n][s][2];
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")   */

// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define ss second
#define ff first
#define int long long
// #define double long double
#define pb push_back

const int N = 2e6 + 51, MOD = 1e9 + 7, mod = 998244353, INF = 2e18;

mt19937 Flash_new_51(51515);

using vi = vector<int>;
using vp = vector<pair<int, int>>;

int a[N];

signed main() {
#ifdef Flash
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vi dp(s + 1, 0);
  for (int i = 0; i < n; i++) {
    int x = a[i];
    vi tek(s + 1, 0);
    for (int j = 0; j < s; j++) {
      if (j == 0) {
        tek[x] = i + 1;
      } else {
        if (i) {
          tek[j] += dp[j];
          if (j + x <= s) {
            tek[j + x] += dp[j];
            tek[j + x] %= mod;
          }
          tek[j] %= mod;
        }
      }
    }
    dp = tek;
    ans += dp[s] * (n - i);
    // cout << ans << ' ';
    ans %= mod;
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>

/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")   */

// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define ss second
#define ff first
#define int long long
// #define double long double
#define pb push_back

const int N = 2e6 + 51, MOD = 1e9 + 7, mod = 998244353, INF = 2e18;

mt19937 Flash_new_51(51515);

using vi = vector<int>;
using vp = vector<pair<int, int>>;

int a[N];

signed main() {
#ifdef Flash
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vi dp(s + 1, 0);
  for (int i = 0; i < n; i++) {
    int x = a[i];
    vi tek(s + 1, 0);
    for (int j = 0; j < s; j++) {
      if (j == 0 && x <= s) {
        tek[x] = i + 1;
      } else {
        if (i) {
          tek[j] += dp[j];
          if (j + x <= s) {
            tek[j + x] += dp[j];
            tek[j + x] %= mod;
          }
          tek[j] %= mod;
        }
      }
    }
    dp = tek;
    ans += dp[s] * (n - i);
    // cout << ans << ' ';
    ans %= mod;
  }
  cout << ans;
}