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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define ull unsigned long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pul pair<ull, LL>
#define pii pair<int, int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int mod = 998244353;
template <typename T> void chkmin(T &x, T y) {
  if (y < x)
    x = y;
}
template <typename T> void chkmax(T &x, T y) {
  if (y > x)
    x = y;
}
template <typename T> void upd(T &x, T y) {
  if ((x += y) >= mod)
    x -= mod;
}
int n, K, sl, fh, ans, a[3010], f[3010];
int rd() {
  sl = 0;
  fh = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9')
    sl = sl * 10 + ch - '0', ch = getchar();
  return sl * fh;
}
int main() {
  n = rd();
  K = rd();
  for (int i = 1; i <= n; ++i)
    a[i] = rd();
  for (int i = 1; i <= n; ++i) {
    ans = (ans + i * (n - i + 1) * (a[i] == K) +
           1ll * f[K - a[i]] * (n - i + 1)) %
          mod;
    for (int j = K; j >= a[i]; --j)
      upd(f[j], f[j - a[i]]);
    upd(f[a[i]], i);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define ull unsigned long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pul pair<ull, LL>
#define pii pair<int, int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int mod = 998244353;
template <typename T> void chkmin(T &x, T y) {
  if (y < x)
    x = y;
}
template <typename T> void chkmax(T &x, T y) {
  if (y > x)
    x = y;
}
template <typename T> void upd(T &x, T y) {
  if ((x += y) >= mod)
    x -= mod;
}
int n, K, sl, fh, ans, a[3010], f[3010];
int rd() {
  sl = 0;
  fh = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9')
    sl = sl * 10 + ch - '0', ch = getchar();
  return sl * fh;
}
int main() {
  n = rd();
  K = rd();
  for (int i = 1; i <= n; ++i)
    a[i] = rd();
  for (int i = 1; i <= n; ++i)
    if (a[i] <= K) {
      ans = (ans + i * (n - i + 1) * (a[i] == K) +
             1ll * f[K - a[i]] * (n - i + 1)) %
            mod;
      for (int j = K; j >= a[i]; --j)
        upd(f[j], f[j - a[i]]);
      upd(f[a[i]], i);
    }
  printf("%d\n", ans);
  return 0;
}
Problem p02734 (C++)

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

const int mod = 998244353;
int main() {
  int n, s;
  cin >> n >> s;
  int a[n + 1], dp[n + 1][s + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, 0, sizeof(dp));
  // dp[0][0]=1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= s; j++) {
      // if(a[i]==j)continue;
      dp[i][j] += dp[i - 1][j];
      dp[i][j] %= mod;
    }
    dp[i][a[i]] += i;
    dp[i][a[i]] %= mod;
    for (int j = a[i]; j <= s; j++) {
      dp[i][j] += dp[i - 1][j - a[i]];
      dp[i][j] %= mod;
    }

    ans += dp[i][s];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

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

const int mod = 998244353;
int main() {
  int n, s;
  cin >> n >> s;
  int a[n + 1], dp[n + 1][3005];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, 0, sizeof(dp));
  // dp[0][0]=1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= s; j++) {
      // if(a[i]==j)continue;
      dp[i][j] += dp[i - 1][j];
      dp[i][j] %= mod;
    }
    dp[i][a[i]] += i;
    dp[i][a[i]] %= mod;
    for (int j = a[i]; j <= s; j++) {
      dp[i][j] += dp[i - 1][j - a[i]];
      dp[i][j] %= mod;
    }

    ans += dp[i][s];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-8;

long long int N, M, K, L, R, H, W;
// int N, M, K, L, R, H, W;

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

  cin >> N >> M;
  vector<int> v(N);
  for (auto &i : v)
    cin >> i;
  vector<vector<long long int>> dp(N + 1, vector<long long int>(M + 1));
  for (int i = 0; i < N; i++) {
    dp[i + 1][v[i]] = i + 1;
    for (int j = 0; j <= M; j++) {
      if (j != M)
        dp[i + 1][j] += dp[i][j];
      if (j >= v[i])
        dp[i + 1][j] += dp[i][j - v[i]];
      dp[i + 1][j] %= MOD;
    }
  }
  long long int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += dp[i][M] * (N - i + 1) % MOD;
  }
  cout << ans % MOD << endl;
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-8;

long long int N, M, K, L, R, H, W;
// int N, M, K, L, R, H, W;

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

  cin >> N >> M;
  vector<int> v(N);
  for (auto &i : v)
    cin >> i;
  vector<vector<long long int>> dp(N + 1, vector<long long int>(M + 1));
  for (int i = 0; i < N; i++) {
    if (v[i] <= M)
      dp[i + 1][v[i]] = i + 1;
    for (int j = 0; j <= M; j++) {
      if (j != M)
        dp[i + 1][j] += dp[i][j];
      if (j >= v[i])
        dp[i + 1][j] += dp[i][j - v[i]];
      dp[i + 1][j] %= MOD;
    }
  }
  long long int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += dp[i][M] * (N - i + 1) % MOD;
  }
  cout << ans % MOD << endl;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
#define int ll
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const int MAX = 2003, MOD = 998244353;
int dp[MAX][MAX], arr[MAX], n, s;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    dp[i][arr[i]] = i;

    for (int j = 0; j + arr[i] <= s; j++) {
      dp[i][j + arr[i]] += dp[i - 1][j];
      dp[i][j + arr[i]] %= MOD;
    }
    ans = (ans + (n - i + 1) * dp[i][s]) % MOD;
    for (int j = 0; j <= s; j++)
      dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
#define int ll
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const int MAX = 3003, MOD = 998244353;
int dp[MAX][MAX], arr[MAX], n, s;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    dp[i][arr[i]] = i;

    for (int j = 0; j + arr[i] <= s; j++) {
      dp[i][j + arr[i]] += dp[i - 1][j];
      dp[i][j + arr[i]] %= MOD;
    }
    ans = (ans + (n - i + 1) * dp[i][s]) % MOD;
    for (int j = 0; j <= s; j++)
      dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD;
  }
  cout << ans;

  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int main() {
  // dp[i + 1][j] A[i]までで和がjにできるような場合の数
  ll N, S, MOD = 998244353;
  cin >> N >> S;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(S + 1, 0));
  rep(i, N) {
    dp[i + 1][A[i]] += i + 1;
    dp[i + 1][A[i]] %= MOD;
    rep(j, S + 1) {
      dp[i + 1][j] += 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;
      }
    }
  }
  int ans = 0;
  rep(i, N + 1) {
    ans += dp[i][S];
    ans %= MOD;
  }
  cout << ans << "\n";
  /*
  rep(i, N + 1) {
      rep(j, S + 1) {
          cout << dp[i][j] << " ";
      }
      cout << "\n";
  }
  */
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int main() {
  // dp[i + 1][j] A[i]までで和がjにできるような場合の数
  ll N, S, MOD = 998244353;
  cin >> N >> S;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(3001, 0));
  rep(i, N) {
    dp[i + 1][A[i]] += i + 1;
    dp[i + 1][A[i]] %= MOD;
    rep(j, S + 1) {
      dp[i + 1][j] += 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;
      }
    }
  }
  int ans = 0;
  rep(i, N + 1) {
    ans += dp[i][S];
    ans %= MOD;
  }
  cout << ans << "\n";
  /*
  rep(i, N + 1) {
      rep(j, S + 1) {
          cout << dp[i][j] << " ";
      }
      cout << "\n";
  }
  */
}
Problem p02734 (C++)

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

#define int long long
#define pii pair<int, int>
#define pb push_back

int n, s;
int const N = 1e3 + 10, M = 998244353;
int a[N];
int dp[N][2 * N][3];

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int ans = 0;
  dp[n + 1][s][2] = 1;
  for (int i = n; i >= 1; i--) {
    for (int sum = s; sum >= 0; sum--) {
      for (int t = 0; t <= 2; t++) {
        if (t == 0) {
          dp[i][sum][0] = (dp[i + 1][sum][0] + dp[i + 1][sum + a[i]][1] +
                           dp[i + 1][sum][1]) %
                          M;
        } else if (t == 1) {
          dp[i][sum][1] = (dp[i + 1][sum][1] + dp[i + 1][sum + a[i]][1] +
                           dp[i + 1][sum + a[i]][2] + dp[i + 1][sum][2]) %
                          M;
        } else {
          dp[i][sum][2] = dp[i + 1][sum][2] % M;
        }
      }
    }
    (ans += dp[i][0][1]) %= M;
  }
  cout << ans << endl;
  return 0;
}

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

#define int long long
#define pii pair<int, int>
#define pb push_back

int n, s;
int const N = 3e3 + 10, M = 998244353;
int a[N];
int dp[N][2 * N][3];

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int ans = 0;
  dp[n + 1][s][2] = 1;
  for (int i = n; i >= 1; i--) {
    for (int sum = s; sum >= 0; sum--) {
      for (int t = 0; t <= 2; t++) {
        if (t == 0) {
          dp[i][sum][0] = (dp[i + 1][sum][0] + dp[i + 1][sum + a[i]][1] +
                           dp[i + 1][sum][1]) %
                          M;
        } else if (t == 1) {
          dp[i][sum][1] = (dp[i + 1][sum][1] + dp[i + 1][sum + a[i]][1] +
                           dp[i + 1][sum + a[i]][2] + dp[i + 1][sum][2]) %
                          M;
        } else {
          dp[i][sum][2] = dp[i + 1][sum][2] % M;
        }
      }
    }
    (ans += dp[i][0][1]) %= M;
  }
  cout << ans << endl;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
#pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;
const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];

signed main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 0; i <= s; i++)
    dp[i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int w = s - a[i]; w >= 1; w--) {
      dp[w + a[i]] += dp[w];
      dp[w + a[i]] -= MOD * (dp[w + a[i]] >= MOD);
    }
    dp[a[i]] += i;
    dp[a[i]] -= MOD * (dp[a[i]] >= MOD);
    ans += dp[s];
    ans -= MOD * (ans >= MOD);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
#pragma GCC optimize("unroll-loops,inline")
#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;
const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];

signed main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 0; i <= s; i++)
    dp[i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int w = s - a[i]; w >= 1; w--) {
      dp[w + a[i]] += dp[w];
      dp[w + a[i]] -= MOD * (dp[w + a[i]] >= MOD);
    }
    dp[a[i]] += i;
    dp[a[i]] -= MOD * (dp[a[i]] >= MOD);
    ans += dp[s];
    ans -= MOD * (ans >= MOD);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02734 (C++)

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

using namespace std;
ll n, S, dp[3030][3030], a[3030];
const ll Mod = 998244353;

void Add_Self(ll &x, ll y) { x = (x + y) % Mod; }

ll Rec(int i, int Have) {

  if (i == n)
    return (Have == S ? 1 : 0);
  if (Have == S)
    return n - (i - 1);

  if (dp[i][Have] != -1)
    return dp[i][Have];

  ll Res = 0;
  Add_Self(Res, Rec(i + 1, Have + a[i]));
  Add_Self(Res, Rec(i + 1, Have));

  return dp[i][Have] = Res;
}

int main() {

  memset(dp, -1, sizeof(dp));
  cin >> n >> S;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll Ans = 0;
  for (int i = 0; i < n; i++)
    Add_Self(Ans, Rec(i, 0));
  cout << Ans;
  return 0;
}


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

using namespace std;
ll n, S, dp[3030][3030], a[3030];
const ll Mod = 998244353;

void Add_Self(ll &x, ll y) { x = (x + y) % Mod; }

ll Rec(int i, int Have) {

  if (i == n)
    return (Have == S ? 1 : 0);
  if (Have == S)
    return n - (i - 1);

  if (dp[i][Have] != -1)
    return dp[i][Have];

  ll Res = 0;
  Add_Self(Res, Rec(i + 1, min((ll)3001, Have + a[i])));
  Add_Self(Res, Rec(i + 1, Have));

  return dp[i][Have] = Res;
}

int main() {

  memset(dp, -1, sizeof(dp));
  cin >> n >> S;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll Ans = 0;
  for (int i = 0; i < n; i++)
    Add_Self(Ans, Rec(i, 0));
  cout << Ans;
  return 0;
}
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];
  dp[0][0] = 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 = 1; 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];
  dp[0][0] = 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 = 1; 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):
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
#define ll long long
#define pb push_back
int a[3005];
ll dp[3005];
const ll mod = 998244353;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
int main(int argc, char **argv) {
  int n, s;
  cin >> n >> s;
  rep(i, 1, n) { scanf("%d", &a[i]); }
  ll ans = 0;
  rep(i, 1, n) {
    if (s == a[i])
      ans = ad(ans, ap(i, n - i + 1));
    if (dp[s - a[i]])
      ans = ad(ans, ap((dp[s - a[i]]), (n - i + 1)));
    //	cout<<s-a[i]<<" "<<dp[s-a[i]]<<" "<<i<<endl;
    rep2(j, s, 1) {
      if (j + a[i] > s)
        continue;
      dp[j + a[i]] = ad(dp[j + a[i]], dp[j]);
    }
    dp[a[i]] = ad(dp[a[i]], i);
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
#define ll long long
#define pb push_back
int a[3005];
ll dp[3005];
const ll mod = 998244353;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
int main(int argc, char **argv) {
  int n, s;
  cin >> n >> s;
  rep(i, 1, n) { scanf("%d", &a[i]); }
  ll ans = 0;
  rep(i, 1, n) {
    if (s < a[i])
      continue;
    if (s == a[i])
      ans = ad(ans, ap(i, n - i + 1));
    if (dp[s - a[i]])
      ans = ad(ans, ap((dp[s - a[i]]), (n - i + 1)));
    //	cout<<s-a[i]<<" "<<dp[s-a[i]]<<" "<<i<<endl;
    rep2(j, s, 1) {
      if (j + a[i] > s)
        continue;
      dp[j + a[i]] = ad(dp[j + a[i]], dp[j]);
    }
    dp[a[i]] = ad(dp[a[i]], i);
  }
  cout << ans;

  return 0;
}
Problem p02734 (C++)

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

using namespace std;

#define LL long long

int main() {
  // freopen("a.txt", "r", stdin);
  int mod = 998244353;
  int N, S, res = 0;
  cin >> N >> S;

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

  vector<int> dp(S + 1, 0);
  for (int i = 1; i <= N; ++i) {
    for (int j = S; j >= A[i]; --j)
      dp[j] = (dp[j] + dp[j - A[i]]) % mod;
    dp[A[i]] = (dp[A[i]] + i) % mod;
    res = (res + dp[S] * (N - i + 1)) % mod;
    dp[S] = 0;
  }

  cout << res << endl;
  return 0;
}

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

using namespace std;

#define LL long long

int main() {
  // freopen("a.txt", "r", stdin);
  int mod = 998244353;
  int N, S, res = 0;
  cin >> N >> S;

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

  vector<int> dp(S + 1, 0);
  for (int i = 1; i <= N; ++i) {
    for (int j = S; j >= A[i]; --j)
      dp[j] = (dp[j] + dp[j - A[i]]) % mod;
    if (S >= A[i])
      dp[A[i]] = (dp[A[i]] + i) % mod;
    res = (res + 1ll * dp[S] * (N - i + 1)) % mod;
    dp[S] = 0;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 998244353;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 998244353;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n, s;
  cin >> n >> s;
  vector<LL> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  LL ans = 0;
  vector<LL> dp(5000, 0);
  for (LL i = 0; i < n; i++) {
    for (int j = s; j >= 0; j--) {
      if (j + a[i] <= s)
        dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % mod;
    }
    dp[a[i]] = (dp[a[i]] + i + 1) % mod;
    ans = (ans + dp[s]) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 3003, mod = 998244353;
int a[N], dp[2][N], n, s, ans;
int M(int x) {
  int ret = x % mod;
  ret += mod;
  ret %= mod;
  return ret;
}
signed main() {
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[i % 2][a[i]] = i;
    for (int j = 1; j <= s; j++) {
      if (j != a[i])
        dp[i % 2][j] = 0;
      dp[i % 2][j] += M(dp[1 - (i % 2)][j] + dp[1 - (i % 2)][j - a[i]]);
      dp[i % 2][j] = M(dp[i % 2][j]);
      //   cout << dp[i%2][j] << " ";
    }
    //  cout << endl;
    ans += M((dp[i % 2][s] - dp[1 - (i % 2)][s]) * (n - i + 1));
    ans = M(ans);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 3003, mod = 998244353;
int a[N], dp[2][N], n, s, ans;
int M(int x) {
  int ret = x % mod;
  ret += mod;
  ret %= mod;
  return ret;
}
signed main() {
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[i % 2][a[i]] = i;
    for (int j = 1; j <= s; j++) {
      if (j != a[i])
        dp[i % 2][j] = 0;
      dp[i % 2][j] += dp[1 - (i % 2)][j];
      dp[i % 2][j] = M(dp[i % 2][j]);
      if (j >= a[i])
        dp[i % 2][j] += dp[1 - (i % 2)][j - a[i]];
      dp[i % 2][j] = M(dp[i % 2][j]);
      //   cout << dp[i%2][j] << " ";
    }
    //  cout << endl;
    ans += M((dp[i % 2][s] - dp[1 - (i % 2)][s]) * (n - i + 1));
    ans = M(ans);
  }
  cout << ans;
}
Problem p02734 (C++)

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

using namespace std;

void solve(int TestCase) {
  constexpr int MOD = 998244353;
  int n, s;
  cin >> n >> s;
  auto A = vector<int>(n);
  for (auto &x : A)
    cin >> x;

  auto dp = vector<vector<long long>>(n, vector<long long>(s + 1));
  auto sum = vector<long long>(s + 1);
  for (auto i = 0; i < n; ++i) {
    dp[i][A[i]] = i + 1;
    for (auto j = A[i] + 1; i && j < s + 1; ++j) {
      dp[i][j] = sum[j - A[i]];
    }

    for (auto j = 0; j < s + 1; ++j) {
      sum[j] = (sum[j] + dp[i][j]) % MOD;
    }
  }

  long long ret = 0;
  for (auto i = 0; i < n; ++i) {
    ret = (ret + dp[i][s] * (n - i) % MOD) % MOD;
  }

  cout << ret << endl;
}

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

  int t = 1;
  // cin >> t;
  for (auto i = 1; i <= t; ++i) {
    //    cout << "Case #"<< i << ": ";
    solve(i);
  }
}

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

using namespace std;

void solve(int TestCase) {
  constexpr int MOD = 998244353;
  int n, s;
  cin >> n >> s;
  auto A = vector<int>(n);
  for (auto &x : A)
    cin >> x;

  auto dp = vector<vector<long long>>(n, vector<long long>(3001));
  auto sum = vector<long long>(3001);
  for (auto i = 0; i < n; ++i) {
    dp[i][A[i]] = i + 1;
    for (auto j = A[i] + 1; i && j < s + 1; ++j) {
      dp[i][j] = sum[j - A[i]];
    }

    for (auto j = 0; j < s + 1; ++j) {
      sum[j] = (sum[j] + dp[i][j]) % MOD;
    }
  }

  long long ret = 0;
  for (auto i = 0; i < n; ++i) {
    ret = (ret + dp[i][s] * (n - i) % MOD) % MOD;
  }

  cout << ret << endl;
}

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

  int t = 1;
  // cin >> t;
  for (auto i = 1; i <= t; ++i) {
    //    cout << "Case #"<< i << ": ";
    solve(i);
  }
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
#pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];

signed main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 0; i <= s; i++)
    dp[i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int w = s - a[i]; w >= 1; w--) {
      int nxt = w + a[i];
      dp[nxt] = add(dp[nxt], dp[w]);
    }
    int nxt = a[i];
    if (nxt <= s)
      dp[nxt] = add(dp[nxt], i);
    ans = add(ans, dp[s]);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
// #pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];

signed main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 0; i <= s; i++)
    dp[i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int w = s - a[i]; w >= 1; w--) {
      int nxt = w + a[i];
      dp[nxt] = add(dp[nxt], dp[w]);
    }
    int nxt = a[i];
    if (nxt <= s)
      dp[nxt] = add(dp[nxt], i);
    ans = add(ans, dp[s]);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02734 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

n, s = map(int, input().split())
aaa = list(map(int, input().split()))

fwd_acc = np.zeros((n + 1, s + 1), dtype=np.int64)
fwd_acc[0][0] = 1

ans = 0
MOD = 998244353
for i, a in enumerate(aaa, start=1):
    fwd_acc[i] = fwd_acc[i - 1]
    fwd_acc[i][0] = i
    if a <= s:
        fwd_acc[i][a:] += fwd_acc[i][:-a]
    ans = (ans + fwd_acc[i - 1][s - a] * (n - i + 1)) % MOD
print(ans)


Fixed submission:
import numpy as np

n, s = map(int, input().split())
aaa = list(map(int, input().split()))

fwd_acc = np.zeros((n + 1, s + 1), dtype=np.int64)
fwd_acc[0][0] = 1

ans = 0
MOD = 998244353
for i, a in enumerate(aaa, start=1):
    fwd_acc[i] = fwd_acc[i - 1]
    fwd_acc[i][0] = i
    if a <= s:
        fwd_acc[i][a:] = fwd_acc[i][a:] + fwd_acc[i][:-a]
    fwd_acc[i] %= MOD
    ans = (ans + fwd_acc[i][s]) % MOD
    # print(fwd_acc)
    # print(ans)
print(ans)
Problem p02734 (C++)

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

#define mp make_pair
#define pb push_back

#define int int64_t

const int MOD = 998244353;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, s;
  cin >> n >> s;
  int arr[n + 1];
  arr[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }

  int ans = 0;
  int dp[n + 1][s + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i][arr[i]] += i;
    for (int j = 0; j <= s; j++) {
      if (j + arr[i] <= s) {
        dp[i][j + arr[i]] += dp[i - 1][j];
        dp[i][j + arr[i]] %= MOD;
      }
      dp[i][j] += dp[i - 1][j];
      dp[i][j] %= MOD;
      // cout<<dp[i][j]<<" ";
    }
    // cout<<"\n";
    ans += (dp[i][s] * (n - i + 1));
    ans %= MOD;
    dp[i][s] = 0;
  }
  cout << ans;

  return 0;
}

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

#define mp make_pair
#define pb push_back

#define int int64_t

const int MOD = 998244353;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, s;
  cin >> n >> s;
  int arr[n + 1];
  arr[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }

  int ans = 0;
  int dp[n + 1][s + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] <= s)
      dp[i][arr[i]] += i;
    for (int j = 0; j <= s; j++) {
      if (j + arr[i] <= s) {
        dp[i][j + arr[i]] += dp[i - 1][j];
        dp[i][j + arr[i]] %= MOD;
      }
      dp[i][j] += dp[i - 1][j];
      dp[i][j] %= MOD;
      // cout<<dp[i][j]<<" ";
    }
    // cout<<"\n";
    ans += (dp[i][s] * (n - i + 1));
    ans %= MOD;
    dp[i][s] = 0;
  }
  cout << ans;

  return 0;
}
Problem p02734 (C++)

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

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 3e2 + 5;
ll mod = 998244353;
ll a[N], dp[N][N], pre[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = k; j >= 1; j--) {
      if (j >= a[i]) {
        (dp[i][j] = pre[j - a[i]]) %= mod;
        (pre[j] += dp[i][j]) %= mod;
      }
    }
    (pre[a[i]] += i) %= mod;
    (dp[i][a[i]] += i) %= mod;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = (ans + dp[i][k] * (n - i + 1)) % mod;
  }
  cout << ans;
}


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

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 3e3 + 5;
ll mod = 998244353;
ll a[N], dp[N][N], pre[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = k; j >= 1; j--) {
      if (j >= a[i]) {
        (dp[i][j] = pre[j - a[i]]) %= mod;
        (pre[j] += dp[i][j]) %= mod;
      }
    }
    (pre[a[i]] += i) %= mod;
    (dp[i][a[i]] += i) %= mod;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = (ans + dp[i][k] * (n - i + 1)) % mod;
  }
  cout << ans;
}
Problem p02734 (C++)

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

const ll MOD = 998244353;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  vl a(n);
  FOR(i, 0, n) cin >> a[i];

  ll res = 0;
  vl dp(s + 1);

  FOR(i, 0, n) {
    vl ndp = dp;
    ndp[a[i]] = (ndp[a[i]] + i + 1) % MOD;
    FOR(j, a[i], s + 1) { ndp[j] = (ndp[j] + dp[j - a[i]]) % MOD; }

    dp = ndp;
    res = (res + dp[s]) % MOD;
  }
  cout << res << endl;
}


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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

const ll MOD = 998244353;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  vl a(n);
  FOR(i, 0, n) cin >> a[i];

  ll res = 0;
  vl dp(s + 1);

  FOR(i, 0, n) {
    vl ndp = dp;
    if (a[i] <= s)
      ndp[a[i]] = (ndp[a[i]] + i + 1) % MOD;
    FOR(j, a[i], s + 1) { ndp[j] = (ndp[j] + dp[j - a[i]]) % MOD; }

    dp = ndp;
    res = (res + dp[s]) % MOD;
  }
  cout << res << endl;
}
Problem p02734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
int v[N];
long long dp[N];
const int MOD = 998244353;
int main() {
  int n, s;
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    dp[0] = i % MOD;
    ans += (1LL * dp[s - v[i]] * (n - i + 1) % MOD) % MOD;
    ans %= MOD;
    for (int ss = s - v[i]; ss >= 0; ss--) {
      dp[ss + v[i]] += dp[ss];
      dp[ss + v[i]] %= MOD;
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
int v[N];
long long dp[N];
const int MOD = 998244353;
int main() {
  int n, s;
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    dp[0] = i % MOD;
    if (v[i] <= s) {
      ans += (1LL * dp[s - v[i]] * (n - i + 1) % MOD) % MOD;
      ans %= MOD;
      for (int ss = s - v[i]; ss >= 0; ss--) {
        dp[ss + v[i]] += dp[ss];
        dp[ss + v[i]] %= MOD;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02734 (C++)

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

#define MAX_N 3000

using namespace std;

typedef long long lint;

const lint MOD = 998244353LL;

int n, s;
int v[MAX_N + 9];

lint ap[MAX_N + 9];

void modd(lint &a) {
  if (a >= MOD)
    a -= MOD;

  if (a < 0)
    a += MOD;
}

void modd(int &a) {
  if (a >= MOD)
    a -= MOD;

  if (a < 0)
    a += MOD;
}

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

  lint rez = 0;
  // ap[0] = 1;
  for (int i = 1; i <= n; i++) {

    // cout << " SUNTME " << i <<  "ADUNAM " << (n - i + 1) << " RO " << ap[s]
    // << "\n";

    if (v[i] != s)
      rez += 1LL * (n - i + 1) * ap[s - v[i]] % MOD;

    else if (v[i] == s)
      rez += 1LL * (n - i + 1) * i % MOD;

    modd(rez);

    for (int j = s; j > v[i]; j--) {
      ap[j] += ap[j - v[i]];
      modd(ap[j]);
    }

    ap[v[i]] += i;
    modd(ap[v[i]]);
  }

  cout << rez << "\n";

  return 0;
}


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

#define MAX_N 3000

using namespace std;

typedef long long lint;

const lint MOD = 998244353LL;

int n, s;
int v[MAX_N + 9];

lint ap[MAX_N + 9];

void modd(lint &a) {
  if (a >= MOD)
    a -= MOD;

  if (a < 0)
    a += MOD;
}

void modd(int &a) {
  if (a >= MOD)
    a -= MOD;

  if (a < 0)
    a += MOD;
}

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

  lint rez = 0;
  // ap[0] = 1;
  for (int i = 1; i <= n; i++) {

    // cout << " SUNTME " << i <<  "ADUNAM " << (n - i + 1) << " RO " << ap[s]
    // << "\n";

    if (v[i] < s)
      rez += 1LL * (n - i + 1) * ap[s - v[i]] % MOD;

    else if (v[i] == s)
      rez += 1LL * (n - i + 1) * i % MOD;

    modd(rez);

    for (int j = s; j > v[i]; j--) {
      ap[j] += ap[j - v[i]];
      modd(ap[j]);
    }

    ap[v[i]] += i;
    modd(ap[v[i]]);
  }

  cout << rez << "\n";

  return 0;
}
Problem p02734 (C++)

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

int main() {
  ll n, s;
  std::cin >> n >> s;

  vector<ll> a(n);
  const ll MOD = 998244353;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }

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

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i + 1][j] = dp[i][j];
    }
    for (int j = a[i]; j <= s; j++) {
      dp[i + 1][j] += dp[i][j - a[i]];
      dp[i + 1][j] %= MOD;
    }
    dp[i + 1][a[i]] += i;
  }
  for (int i = 1; i <= n; i++) {
    ans += dp[i][s];
    ans %= MOD;
  }
  std::cout << ans << std::endl;
}


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

int main() {
  ll n, s;
  std::cin >> n >> s;

  vector<ll> a(n);
  const ll MOD = 998244353;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }

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

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i + 1][j] = dp[i][j];
    }
    for (int j = a[i]; j <= s; j++) {
      dp[i + 1][j] += dp[i][j - a[i]];
      dp[i + 1][j] %= MOD;
    }
    dp[i + 1][a[i]] += i;
  }
  for (int i = 1; i <= n; i++) {
    ans += dp[i][s];
    ans %= MOD;
  }
  std::cout << ans << std::endl;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\n"
#define inf 1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigboi 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(all(v), RNG);
// Use mt19937_64 for 64 bit random numbers.

int dp[200][200];
void solve() {
  int n, m;
  cin >> n >> m;
  char arr[n][m];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      dp[i][j] = inf;
    }
  }
  if (arr[1][1] == '#')
    dp[1][1] = 1;
  else
    dp[1][1] = 0;
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) {
      if (i == 1 && j == 1)
        continue;
      if (arr[i][j] == '#') {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + (arr[i][j] != arr[i][j - 1]));
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + (arr[i][j] != arr[i - 1][j]));
      } else {
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      }
    }
  }

  cout << dp[n][m];
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  FOR(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\n"
#define inf 1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigboi 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(all(v), RNG);
// Use mt19937_64 for 64 bit random numbers.

int dp[200][200];
void solve() {
  int n, m;
  cin >> n >> m;
  char arr[n + 12][m + 12];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      dp[i][j] = inf;
    }
  }
  if (arr[1][1] == '#')
    dp[1][1] = 1;
  else
    dp[1][1] = 0;
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) {
      if (i == 1 && j == 1)
        continue;
      if (arr[i][j] == '#') {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + (arr[i][j] != arr[i][j - 1]));
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + (arr[i][j] != arr[i - 1][j]));
      } else {
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      }
    }
  }

  cout << dp[n][m];
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  FOR(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int solve(int h, int w, vector<vector<int>> &k, vector<vector<int>> &u) {
  if (h == 1 && w == 1) {
    return u[1][1];
  }
  if (h == 0)
    return 100000;
  if (w == 0)
    return 100000;
  else {
    int a = solve(h, w - 1, k, u);
    if (u[h][w - 1] == 0 && u[h][w] == 1) {
      a++;
    }
    int b = solve(h - 1, w, k, u);
    if (u[h - 1][w] == 0 && u[h][w] == 1) {
      b++;
    }
    k[h][w] = min(a, b);
  }
  return k[h][w];
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> k(h + 1, vector<int>(w + 1, 100000));
  vector<vector<int>> u(h + 1, vector<int>(w + 1, 1));
  rep(i, h) {
    rep(j, w) {
      char tmp;
      cin >> tmp;
      if (tmp == '#')
        u[i + 1][j + 1] = 1;
      else
        u[i + 1][j + 1] = 0;
    }
  }
  cout << solve(h, w, k, u) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int solve(int h, int w, vector<vector<int>> &k, vector<vector<int>> &u) {
  if (h == 1 && w == 1) {
    return u[1][1];
  }
  if (k[h][w] <= 10000)
    return k[h][w];
  if (h == 0)
    return 100000;
  if (w == 0)
    return 100000;
  else {
    int a = solve(h, w - 1, k, u);
    if (u[h][w - 1] == 0 && u[h][w] == 1) {
      a++;
    }
    int b = solve(h - 1, w, k, u);
    if (u[h - 1][w] == 0 && u[h][w] == 1) {
      b++;
    }
    k[h][w] = min(a, b);
  }
  return k[h][w];
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> k(h + 1, vector<int>(w + 1, 100000));
  vector<vector<int>> u(h + 1, vector<int>(w + 1, 1));
  rep(i, h) {
    rep(j, w) {
      char tmp;
      cin >> tmp;
      if (tmp == '#')
        u[i + 1][j + 1] = 1;
      else
        u[i + 1][j + 1] = 0;
    }
  }
  cout << solve(h, w, k, u) << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> White(100, vector<bool>(100, false));
vector<vector<int>> Steps(100, vector<int>(100, 100000));
int H, W;

void search(int h, int w, int prevStep, bool prevBlack) {
  if (h >= H || w >= W)
    return;
  if (Steps.at(h).at(w) < prevStep)
    return;
  if (White.at(h).at(w)) {
    Steps.at(h).at(w) = prevStep;
    search(h, w + 1, prevStep, false);
    search(h + 1, w, prevStep, false);
  } else {
    if (prevBlack) {
      Steps.at(h).at(w) = prevStep;
      search(h, w + 1, prevStep, true);
      search(h + 1, w, prevStep, true);
    } else {
      Steps.at(h).at(w) = prevStep + 1;
      search(h, w + 1, prevStep + 1, true);
      search(h + 1, w, prevStep + 1, true);
    }
  }
}

int main() {
  cin >> H >> W;
  char g;
  for (int r = 0; r < H; r++) {
    for (int c = 0; c < W; c++) {
      cin >> g;
      if (g == '.')
        White.at(r).at(c) = true;
    }
  }
  search(0, 0, 0, false);
  cout << Steps.at(H - 1).at(W - 1) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> White(100, vector<bool>(100, false));
vector<vector<int>> Steps(100, vector<int>(100, 100000));
int H, W;

void search(int h, int w, int prevStep, bool prevBlack) {
  if (h >= H || w >= W)
    return;
  if (Steps.at(h).at(w) <= prevStep)
    return;
  if (White.at(h).at(w)) {
    Steps.at(h).at(w) = prevStep;
    search(h, w + 1, prevStep, false);
    search(h + 1, w, prevStep, false);
  } else {
    if (prevBlack) {
      Steps.at(h).at(w) = prevStep;
      search(h, w + 1, prevStep, true);
      search(h + 1, w, prevStep, true);
    } else {
      Steps.at(h).at(w) = prevStep + 1;
      search(h, w + 1, prevStep + 1, true);
      search(h + 1, w, prevStep + 1, true);
    }
  }
}

int main() {
  cin >> H >> W;
  char g;
  for (int r = 0; r < H; r++) {
    for (int c = 0; c < W; c++) {
      cin >> g;
      if (g == '.')
        White.at(r).at(c) = true;
    }
  }
  search(0, 0, 0, false);
  cout << Steps.at(H - 1).at(W - 1) << endl;
}