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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define l_ength size
using ll = long long;
const int mod = 1000000007;
const ll infll = (1ll << 60);
using namespace std;

ll dp[110][100010];
ll sum[100010];

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &k : a)
    cin >> k;
  dp[0][0] = 1;
  for (int i = 0; i <= k; ++i) {
    sum[i + 1] = (sum[i] + dp[0][i]) % mod;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      dp[i + 1][j] = (sum[j + 1] - sum[j - a[i]] + mod) % mod;
    }
    for (int j = 0; j <= k; ++j) {
      sum[j + 1] = (sum[j] + dp[i + 1][j]) % mod;
    }
  }
  cout << dp[n][k] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define l_ength size
using ll = long long;
const int mod = 1000000007;
const ll infll = (1ll << 60);
using namespace std;

ll dp[110][100010];
ll sum[100010];

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &k : a)
    cin >> k;
  dp[0][0] = 1;
  for (int i = 0; i <= k; ++i) {
    sum[i + 1] = (sum[i] + dp[0][i]) % mod;
  }
  for (int i = 0; i < n; ++i) {
    for (ll j = 0; j <= k; ++j) {
      dp[i + 1][j] = (sum[j + 1] - sum[max(0ll, j - a[i])] + mod) % mod;
    }
    for (int j = 0; j <= k; ++j) {
      sum[j + 1] = (sum[j] + dp[i + 1][j]) % mod;
    }
  }
  cout << dp[n][k] << endl;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dp[2][100010] = {};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, K;
  cin >> N >> K;
  ll a[110];
  rep(i, 0, N) cin >> a[i];
  dp[0][0] = 1;
  ll cur = 0, nxt = 1;
  rep(i, 0, N) {
    rep(j, 0, K + 1) dp[nxt][j] = 0;
    rrep(j, K, 0) {
      dp[nxt][j] += dp[cur][j];
      dp[nxt][j] %= MOD;
      dp[nxt][j + a[i] + 1] = (dp[nxt][j + a[i] + 1] - dp[cur][j] + MOD) % MOD;
    }
    rep(j, 0, K + 1)(dp[nxt][j + 1] += dp[nxt][j]) %= MOD;
    nxt ^= 1;
    cur ^= 1;
  }
  print(dp[cur][K]);
}


Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dp[2][100010] = {};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, K;
  cin >> N >> K;
  ll a[110];
  rep(i, 0, N) cin >> a[i];
  dp[0][0] = 1;
  ll cur = 0, nxt = 1;
  rep(i, 0, N) {
    rep(j, 0, K + 1) dp[nxt][j] = 0;
    rrep(j, K, 0) {
      dp[nxt][j] += dp[cur][j];
      dp[nxt][j] %= MOD;
      if (j + a[i] + 1 <= K + 1)
        dp[nxt][j + a[i] + 1] =
            (dp[nxt][j + a[i] + 1] - dp[cur][j] + MOD) % MOD;
    }
    rep(j, 0, K + 1)(dp[nxt][j + 1] += dp[nxt][j]) %= MOD;
    nxt ^= 1;
    cur ^= 1;
  }
  print(dp[cur][K]);
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
// Om Sree Sai Ram
#include "bits/stdc++.h"
using namespace std;

const int mod = 1e9 + 7;

int add_self(int &a, int b) {
  a += b;
  if (a >= mod) {
    a -= mod;
  }
}

int sub_self(int &a, int b) {
  a -= b;
  if (a <= 0) {
    a += mod;
  }
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> dp(k + 1);
  // dp[i] represents number of ways to get into state in which i candies are
  // distributed.
  dp[0] = 1;

  for (int i = 0; i < n; i++) {
    int upto;
    cin >> upto;
    vector<int> diff(k + 1);
    for (int j = k; j >= 0; j--) {
      int tmp = dp[j];
      // for order(n*k^2) algorithm
      int L = j + 1;
      int R = j + upto;
      if (L <= R) {
        add_self(diff[L], tmp);
        if (R + 1 <= k) {
          sub_self(diff[R + 1], tmp);
        }
      }
      // for(int dat=L;dat<=R;dat++)
      // {
      //   if(dat<=k)
      //   {
      //   dp[dat] += tmp;
      //   }
      // }
    }

    int prefix = 0;
    for (int i = 0; i <= k; i++) {
      // cout<<"diff "<<i<<endl;
      add_self(prefix, diff[i]);
      // cout<<"prefix "<<prefix<<endl;
      add_self(dp[i], prefix);
    }
  }

  cout << dp[k] << endl;
}


Fixed submission:
// Om Sree Sai Ram
#include "bits/stdc++.h"
using namespace std;

const int mod = 1e9 + 7;

int add_self(int &a, int b) {
  a += b;
  if (a >= mod) {
    a -= mod;
  }
}

int sub_self(int &a, int b) {
  a -= b;
  if (a <= 0) {
    a += mod;
  }
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> dp(k + 1);
  // dp[i] represents number of ways to get into state in which i candies are
  // distributed.
  dp[0] = 1;

  for (int i = 0; i < n; i++) {
    int upto;
    cin >> upto;
    vector<int> diff(k + 1);
    for (int j = k; j >= 0; j--) {
      int tmp = dp[j];
      // for order(n*k^2) algorithm
      int L = j + 1;
      int R = j + min(upto, k - j);
      if (L <= R) {
        add_self(diff[L], tmp);
        if (R + 1 <= k) {
          sub_self(diff[R + 1], tmp);
        }
      }
      // for(int dat=L;dat<=R;dat++)
      // {
      //   if(dat<=k)
      //   {
      //   dp[dat] += tmp;
      //   }
      // }
    }

    int prefix = 0;
    for (int i = 0; i <= k; i++) {
      // cout<<"diff "<<i<<endl;
      add_self(prefix, diff[i]);
      // cout<<"prefix "<<prefix<<endl;
      add_self(dp[i], prefix);
    }
  }

  cout << dp[k] << endl;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

int main() {
  using namespace std;
  constexpr unsigned int MOD = 1000000007;
  unsigned long N, K;
  cin >> N >> K;
  vector<unsigned int> dp(K + 1);
  dp[0] = 1;
  for (unsigned long i{0}, a; i < N; ++i) {
    cin >> a;
    for (unsigned long j{K}; j > a; --j)
      dp[j] += dp[j] > dp[j - a - 1] ? -dp[j - a - 1] : MOD - dp[j - a - 1];
  }
  for (unsigned long i{0}; i < N; ++i)
    partial_sum(dp.begin(), dp.end(), dp.begin(),
                [](auto i, auto j) { return (i + j) % MOD; });
  cout << dp.back() << endl;
  return 0;
}

Fixed submission:
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

int main() {
  using namespace std;
  constexpr unsigned int MOD = 1000000007;
  unsigned long N, K;
  cin >> N >> K;
  vector<unsigned int> dp(K + 1);
  dp[0] = 1;
  for (unsigned long i{0}, a; i < N; ++i) {
    cin >> a;
    for (unsigned long j{K}; j > a; --j)
      dp[j] += dp[j] > dp[j - a - 1] ? -dp[j - a - 1] : MOD - dp[j - a - 1];
  }
  for (unsigned long i{0}; i < N; ++i)
    partial_sum(dp.begin(), dp.end(), dp.begin(),
                [](auto i, auto j) { return (i + j) % MOD; });
  cout << dp.back() << endl;
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9L + 7;
int n;
vector<int> inp;
vector<vector<ll>> dp;

ll candp(int k, int i = 0) {
  // cout<<i<<" "<<j<<" "<<c<<" "<<sum<<'\n';
  if (k == 0)
    return 1;
  if (i == n)
    return 0;
  if (dp[i][k] != -1)
    return dp[i][k];

  ll ans = 0;
  for (int a = 0; a <= inp[i]; a++) {
    if (k - a >= 0) {
      ans += candp(k - a, i + 1);
      ans %= mod;
    } else
      break;
  }
  return dp[i][k] = ans;
}

ll cf_solver(int k) {
  vector<vector<ll>> cf_dp(k + 1, vector<ll>(n + 1, 1));

  for (int a = 1; a <= k; a++) {
    for (int i = 1; i <= n; i++) {
      ll ans = 0;
      int sub = min(a, inp[i]);
      sub++;
      if (a - sub == -1)
        ans = cf_dp[a][i - 1];
      else
        ans = cf_dp[a][i - 1] - cf_dp[a - sub][i - 1];
      if (ans < 0)
        ans += mod;
      ans += cf_dp[a - 1][i];
      ans %= mod;

      cf_dp[a][i] = ans;
    }
  }

  ll ret_val = cf_dp[k][n] - cf_dp[k - 1][n];
  if (ret_val < 0)
    ret_val += mod;
  ret_val %= mod;
  return ret_val;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int k;
  cin >> n >> k;
  inp.resize(n + 1);
  dp.assign(n + 1, vector<ll>(k + 1, -1));

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

  // cout<<candp(k)<<'\n';
  cout << cf_solver(k) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9L + 7;
int n;
vector<int> inp;
vector<vector<ll>> dp;

ll candp(int k, int i = 0) {
  // cout<<i<<" "<<j<<" "<<c<<" "<<sum<<'\n';
  if (k == 0)
    return 1;
  if (i == n)
    return 0;
  if (dp[i][k] != -1)
    return dp[i][k];

  ll ans = 0;
  for (int a = 0; a <= inp[i]; a++) {
    if (k - a >= 0) {
      ans += candp(k - a, i + 1);
      ans %= mod;
    } else
      break;
  }
  return dp[i][k] = ans;
}

ll cf_solver(int k) {
  vector<vector<ll>> cf_dp(k + 1, vector<ll>(n + 1, 1));

  for (int a = 1; a <= k; a++) {
    for (int i = 1; i <= n; i++) {
      ll ans = 0;
      int sub = min(a, inp[i]);
      sub++;
      if (a - sub == -1)
        ans = cf_dp[a][i - 1];
      else
        ans = cf_dp[a][i - 1] - cf_dp[a - sub][i - 1];
      if (ans < 0)
        ans += mod;
      ans += cf_dp[a - 1][i];
      ans %= mod;

      cf_dp[a][i] = ans;
    }
  }

  ll ret_val = cf_dp[k][n];
  if (k > 0)
    ret_val -= cf_dp[k - 1][n];
  if (ret_val < 0)
    ret_val += mod;
  ret_val %= mod;
  return ret_val;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int k;
  cin >> n >> k;
  inp.resize(n + 1);
  dp.assign(n + 1, vector<ll>(k + 1, -1));

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

  // cout<<candp(k)<<'\n';
  cout << cf_solver(k) << '\n';
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define db long double
#define ull unsigned long long int
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define Nmax 1000005
#define INF 1000000000
#define MOD 1000000007
#define MAXN 1000005

using namespace std;

ll dp[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    ll x, i, j, n, k;
    cin >> n >> k;
    // 	vector<ll>pre;
    dp[0] = 1;
    rep(i, 1, n) {
      cin >> x;
      // 		vector<ll>pre(k+1,0);
      // pre.resize(k+1,0LL);
      ll pre[k + 1LL];
      memset(pre, 0, sizeof(pre));
      for (j = 0; j <= k; j++) {
        ll left = j + 1;
        ll right = j + x;
        if (right >= left) {
          pre[left] += dp[j];
          pre[left] %= MOD;
          if (right + 1 <= k) {
            pre[right + 1] -= dp[j];
            pre[right + 1] = (pre[right + 1] + MOD) % MOD;
          }
        }
      }
      rep(j, 1, k) {
        pre[j] += pre[j - 1];
        pre[j] = (pre[j] + MOD) % MOD;
        dp[j] += pre[j];
        dp[j] = (dp[j] + MOD) % MOD;
      }
    }
    cout << dp[k];
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define db long double
#define ull unsigned long long int
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define Nmax 1000005
#define INF 1000000000
#define MOD 1000000007
#define MAXN 1000005

using namespace std;

ll dp[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    ll x, i, j, n, k;
    cin >> n >> k;
    // 	vector<ll>pre;
    dp[0] = 1;
    rep(i, 1, n) {
      cin >> x;
      // 		vector<ll>pre(k+1,0);
      // pre.resize(k+1,0LL);
      ll pre[k + 50LL];
      memset(pre, 0, sizeof(pre));
      for (j = 0; j <= k; j++) {
        ll left = j + 1;
        ll right = j + x;
        if (right >= left) {
          pre[left] += dp[j];
          pre[left] %= MOD;
          if (right + 1 <= k) {
            pre[right + 1] -= dp[j];
            pre[right + 1] = (pre[right + 1] + MOD) % MOD;
          }
        }
      }
      rep(j, 1, k) {
        pre[j] += pre[j - 1];
        pre[j] = (pre[j] + MOD) % MOD;
        dp[j] += pre[j];
        dp[j] = (dp[j] + MOD) % MOD;
      }
    }
    cout << dp[k];
  }
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PI 3.14159265358979
#define EPS 1e-8
#define mod 1000000007
#define fi first
#define se second
#define mp make_pair
#define pb push_back

const ll INF = 1e18;
const int N = 1e5 + 50;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
  }

  vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, 0));
  for (int i = 0; i <= k; i++) {
    dp[i][0] = 1;
  }

  for (int j = 1; j <= n; j++) {
    for (int i = 0; i <= k; i++) {
      dp[i][j] = dp[i][j - 1];
      if ((i - v[j - 1]) > 0) {
        dp[i][j] -= dp[i - v[j - 1] - 1][j - 1];
      }
      dp[i][j] %= mod;
      dp[i][j] += mod;
      dp[i][j] %= mod;
      if (i > 0)
        dp[i][j] += dp[i - 1][j];

      dp[i][j] %= mod;
    }
  }
  cout << (dp[k][n] - dp[k - 1][n]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PI 3.14159265358979
#define EPS 1e-8
#define mod 1000000007
#define fi first
#define se second
#define mp make_pair
#define pb push_back

const ll INF = 1e18;
const int N = 1e5 + 50;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
  }

  vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, 0));
  for (int i = 0; i <= k; i++) {
    dp[i][0] = 1;
  }

  for (int j = 1; j <= n; j++) {
    for (int i = 0; i <= k; i++) {
      dp[i][j] = dp[i][j - 1];
      if ((i - v[j - 1]) > 0) {
        dp[i][j] -= dp[i - v[j - 1] - 1][j - 1];
      }
      dp[i][j] %= mod;
      dp[i][j] += mod;
      dp[i][j] %= mod;
      if (i > 0)
        dp[i][j] += dp[i - 1][j];

      dp[i][j] %= mod;
    }
  }
  ll ans = dp[k][n];
  if (k)
    ans -= dp[k - 1][n];
  ans %= mod;
  ans += mod;
  ans %= mod;
  cout << ans;
}
Problem p03172 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6;
int n;
vector<vector<int>> dp(n);
int query(int pos1, int l, int r) {
  l += maxn;
  r += maxn;
  int res = 0;
  while (l <= r) {
    if (l % 2 == 1)
      res += dp[pos1][l++];
    if (r % 2 == 0)
      res += dp[pos1][r--];
    l /= 2;
    r /= 2;
  }
  return res % mod;
}
void change(int pos1, int pos2, int val) {
  pos2 += maxn;
  dp[pos1][pos2] += val;
  for (pos2 /= 2; pos2 >= 1; pos2 /= 2) {
    dp[pos1][pos2] = dp[pos1][pos2 * 2] + dp[pos1][pos2 * 2 + 1];
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp = vector<vector<int>>(n, vector<int>(2 * maxn));
  for (int i = 0; i <= a[0]; i++) {
    change(0, i, 1);
    change(0, i + 1, -1);
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      int get = query(i - 1, 0, j);
      if (get > 0) {
        int left = j, right = min(k, j + a[i]);
        change(i, left, get);
        change(i, right + 1, -get);
      }
    }
  }
  int ans = query(n - 1, 0, k);
  cout << ans % mod;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 1e4;
int n;
vector<vector<int>> dp(n);
int query(int pos1, int l, int r) {
  l += maxn;
  r += maxn;
  int res = 0;
  while (l <= r) {
    if (l % 2 == 1)
      res += dp[pos1][l++];
    if (r % 2 == 0)
      res += dp[pos1][r--];
    l /= 2;
    r /= 2;
  }
  return res % mod;
}
void change(int pos1, int pos2, int val) {
  pos2 += maxn;
  dp[pos1][pos2] += val;
  for (pos2 /= 2; pos2 >= 1; pos2 /= 2) {
    dp[pos1][pos2] = dp[pos1][pos2 * 2] + dp[pos1][pos2 * 2 + 1];
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp = vector<vector<int>>(n, vector<int>(2 * maxn));
  for (int i = 0; i <= a[0]; i++) {
    change(0, i, 1);
    change(0, i + 1, -1);
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      int get = query(i - 1, 0, j);
      if (get > 0) {
        int left = j, right = min(k, j + a[i]);
        change(i, left, get);
        change(i, right + 1, -get);
      }
    }
  }
  int ans = query(n - 1, 0, k);
  cout << ans % mod;
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#define Re register int
#define mod int(1e9 + 7)

int N, K, A[105];
long long s[105], f[105][100005];

template <typename T> inline void read(T &var) {
  T x = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  var = w ? -x : x;
}
int main(int argc, char const *argv[]) {
  read(N), read(K);
  f[0][0] = 1;
  for (Re i = 1; i <= N; ++i)
    read(A[i]);
  for (Re i = 1; i <= N; ++i) {
    s[0] = 0;
    for (Re j = 0; j <= K; ++j)
      s[j + 1] = s[j] + f[i - 1][j];
    for (Re j = 0; j <= K; ++j)
      (f[i][j] = s[j + 1] - s[std::max(0, j - A[i])]) %= mod;
  }
  printf("%lld\n", f[N][K]);
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#define Re register int
#define mod int(1e9 + 7)

int N, K, A[105];
long long s[100005], f[105][100005];

template <typename T> inline void read(T &var) {
  T x = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  var = w ? -x : x;
}
int main(int argc, char const *argv[]) {
  read(N), read(K);
  f[0][0] = 1;
  for (Re i = 1; i <= N; ++i)
    read(A[i]);
  for (Re i = 1; i <= N; ++i) {
    s[0] = 0;
    for (Re j = 0; j <= K; ++j)
      s[j + 1] = s[j] + f[i - 1][j];
    for (Re j = 0; j <= K; ++j)
      (f[i][j] = s[j + 1] - s[std::max(0, j - A[i])]) %= mod;
  }
  printf("%lld\n", f[N][K]);
  return 0;
}
Problem p03172 (C++)

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

int main() {
  ll n, i, j, k;
  cin >> n >> k;
  ll a[n + 3];
  for (i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 0;
  sort(a, a + n + 1);
  vector<vector<ll>> dp(k + 2, vector<ll>(n + 2, 1));
  ll ans = 1;
  ll mod = 1000000007;
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= n; j++) {
      ll x = min(i, a[j]);
      x++;
      if (x > i)
        ans = dp[i][j - 1] % mod;
      else {
        ans = dp[i][j - 1] - dp[i - x][j - 1] + mod;
        ans %= mod;
      }
      dp[i][j] = dp[i - 1][j] % mod + ans;
      dp[i][j] %= mod;
    }
  }
  cout << (dp[k][n] - dp[k - 1][n] + mod) % mod;
  return 0;
}


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

int main() {
  ll n, i, j, k;
  cin >> n >> k;
  ll a[n + 3];
  for (i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 0;
  sort(a, a + n + 1);
  vector<vector<ll>> dp(k + 2, vector<ll>(n + 2, 1));
  ll ans = 1;
  ll mod = 1000000007;
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= n; j++) {
      ll x = min(i, a[j]);
      x++;
      if (x > i)
        ans = dp[i][j - 1] % mod;
      else {
        ans = dp[i][j - 1] - dp[i - x][j - 1] + mod;
        ans %= mod;
      }
      dp[i][j] = dp[i - 1][j] % mod + ans;
      dp[i][j] %= mod;
    }
  }
  cout << ans;
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll dp[101][100001];
ll mod = 1e9 + 7;
int main() {
  int n, k, A[200001];
  cin >> n >> k;
  rep(i, n) cin >> A[i];
  fill(dp[0], dp[0] + k + 1, 0);
  dp[0][0] = 1;
  rep(i, n) {
    rep(j, k)(dp[i][j + 1] += dp[i][j]) %= mod;
    rep(j, k + 1) dp[i + 1][j] =
        (dp[i][j] - dp[i][j - A[i] - 1] * (j - A[i] - 1 >= 0) + mod) % mod;
  }
  cout << dp[n][k] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll dp[101][100001];
ll mod = 1e9 + 7;
int main() {
  int n, k, A[200001];
  cin >> n >> k;
  rep(i, n) cin >> A[i];
  fill(dp[0], dp[0] + k + 1, 0);
  dp[0][0] = 1;
  rep(i, n) {
    rep(j, k)(dp[i][j + 1] += dp[i][j]) %= mod;
    rep(j, k + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j - A[i] - 1 >= 0)
        (dp[i + 1][j] += mod - dp[i][j - A[i] - 1]) %= mod;
    }
  }
  cout << dp[n][k] << "\n";
}
Problem p03172 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define INF (1ll << 60)

long long M = 1000000007;

vector<vector<long long>> dp;
vector<long long> a;

long long solve(int i, int r) {
  if (r < 0)
    return 0;
  if (dp[i][r] != -1)
    return dp[i][r];
  if (i == 0) {
    if (r == 0)
      return 1;
    else
      return 0;
  }
  dp[i][r] = 0;
  for (int k = 0; k <= a[i - 1]; ++k) {
    dp[i][r] += solve(i - 1, r - k);
    dp[i][r] %= M;
  }
  return dp[i][r];
}

int main(int argc, char const *argv[]) {
  long long n, k;
  cin >> n >> k;
  a = vector<long long>(n);
  dp = vector<vector<long long>>(n + 1, vector<long long>(k + 1, -1));
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  cout << solve(n, k) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define INF (1ll << 60)

long long M = 1000000007;

vector<vector<long long>> dp;
vector<long long> a;

long long solve(int i, int r) {
  if (r < 0)
    return 0;
  if (dp[i][r] != -1)
    return dp[i][r];
  if (i == 0) {
    if (r == 0)
      return 1;
    else
      return 0;
  }
  dp[i][r] = 0;
  // dp[i][r+1] - dp[i][r] = solve(i-1,r+1) - solve(i-1,r-a[i-1]);
  // for(int k=0;k<=a[i-1];++k){
  //     dp[i][r] += solve(i-1,r-k);
  //     dp[i][r] %= M;
  // }
  dp[i][r] = solve(i, r - 1) + solve(i - 1, r) - solve(i - 1, r - a[i - 1] - 1);
  dp[i][r] %= M;
  dp[i][r] += M;
  dp[i][r] %= M;
  return dp[i][r];
}

int main(int argc, char const *argv[]) {
  long long n, k;
  cin >> n >> k;
  a = vector<long long>(n);
  dp = vector<vector<long long>>(n + 1, vector<long long>(k + 1, -1));
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  cout << solve(n, k) << endl;
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

const int mod = 1000000007;

int N, K;
int a[110];

int dp[110][100010];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    // 累積和を取る
    int sum[100010];
    sum[0] = 0;
    for (int j = 0; j <= K; j++)
      sum[j + 1] = (sum[j] + dp[i - 1][j]) % mod;

    for (int j = 0; j <= K; j++)
      // 負の mod に気を付ける
      dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + mod) % mod;
  }

  cout << dp[N][K] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

const int mod = 1000000007;

int N, K;
int a[110];

int dp[110][100010];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    // 累積和を取る
    int sum[100010];
    sum[0] = 0;
    for (int j = 0; j <= K; j++)
      sum[j + 1] = (sum[j] + dp[i][j]) % mod;

    for (int j = 0; j <= K; j++)
      // 負の mod に気を付ける
      dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + mod) % mod;
  }

  cout << dp[N][K] << endl;

  return 0;
}
Problem p03172 (C++)

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

using namespace std;

static const int kModulo = 1000 * 1000 * 1000 + 7;

int main() {
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;

  vector<int> ways(K + 1, 0);
  ways[0] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    vector<int> partial(N + 1);
    for (int j = 0; j <= K; ++j) {
      partial[j] = ways[j];
      if (j > 0)
        partial[j] = (partial[j] + partial[j - 1]) % kModulo;
    }

    for (int j = 0; j <= K; ++j) {
      ways[j] = partial[j];
      if (j > a)
        ways[j] = (ways[j] + kModulo - partial[j - a - 1]) % kModulo;
    }
  }

  cout << ways[K] << "\n";
}


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

using namespace std;

static const int kModulo = 1000 * 1000 * 1000 + 7;

int main() {
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;

  vector<int> ways(K + 1, 0);
  ways[0] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    vector<int> partial(K + 1);
    for (int j = 0; j <= K; ++j) {
      partial[j] = ways[j];
      if (j > 0)
        partial[j] = (partial[j] + partial[j - 1]) % kModulo;
    }

    for (int j = 0; j <= K; ++j) {
      ways[j] = partial[j];
      if (j > a)
        ways[j] = (ways[j] + kModulo - partial[j - a - 1]) % kModulo;
    }
  }

  cout << ways[K] << "\n";
}
Problem p03172 (C++)

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

const int mod = 1000000007;
int n, k, i, lim;
int dp[100001];
int pre[100001];

int main() {
  scanf("%d%d", &n, &k);
  fill(pre, pre + k + 1, 1);
  while (n--) {
    scanf("%d", &lim);
    for (i = 0; i <= k; i++) {
      dp[i] = pre[i];
      if (i - lim - 1 >= 0) {
        dp[i] -= pre[i - lim - i];
        if (dp[i] < 0)
          dp[i] += mod;
      }
    }
    for (i = 1; i <= k; i++) {
      pre[i] = pre[i - 1] + dp[i];
      if (pre[i] >= mod)
        pre[i] -= mod;
    }
  }
  printf("%d\n", dp[k]);
  return 0;
}


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

const int mod = 1000000007;
int n, k, i, lim;
int dp[100001];
int pre[100001];

int main() {
  scanf("%d%d", &n, &k);
  fill(pre, pre + k + 1, 1);
  while (n--) {
    scanf("%d", &lim);
    for (i = 0; i <= k; i++) {
      dp[i] = pre[i];
      if (i - lim - 1 >= 0) {
        dp[i] -= pre[i - lim - 1];
        if (dp[i] < 0)
          dp[i] += mod;
      }
    }
    for (i = 1; i <= k; i++) {
      pre[i] = pre[i - 1] + dp[i];
      if (pre[i] >= mod)
        pre[i] -= mod;
    }
  }
  printf("%d\n", dp[k]);
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, k;
  cin >> n >> k;
  vec a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  mat dp(n + 1, vec(k + 10));
  dp[0][0] = 1;
  for (ll i = 0; i < n; i++) {
    vec sum(k + 1);
    for (ll j = 0; j <= k; j++) {
      (sum[j + 1] = sum[j] + dp[i][j]) %= mod;
    }
    for (ll j = 0; j <= k; j++) {
      dp[i + 1][j] = (sum[j + 1] - sum[max(0LL, j - a[i])] + mod) % mod;
    }
  }
  cout << dp[n][k] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, k;
  cin >> n >> k;
  vec a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  mat dp(n + 1, vec(k + 10));
  dp[0][0] = 1;
  for (ll i = 0; i < n; i++) {
    vec sum(k + 10);
    for (ll j = 0; j <= k; j++) {
      (sum[j + 1] = sum[j] + dp[i][j]) %= mod;
    }
    for (ll j = 0; j <= k; j++) {
      dp[i + 1][j] = (sum[j + 1] - sum[max(0LL, j - a[i])] + mod) % mod;
    }
  }
  cout << dp[n][k] << endl;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
template <class T> inline int chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline int chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1 << 30;
const ll LINF = 1LL << 58;
const int mod = 1000000007;

ll dp[110][101010];

int main() {
  int n, k;
  cin >> n >> k;
  int A[110];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int a = 0;
  vector<ll> S(k + 1, 0LL);
  dp[0][0] = 1LL;
  for (int i = 0; i < n; i++) {
    a += A[i];

    for (int j = 0; j <= min(k, a); j++) {
      S[j + 1] = (S[j] + dp[i][j]) % mod;
    }

    for (int j = 0; j <= min(k, a); j++) {
      dp[i + 1][j] = S[j + 1] - S[max(0, j - A[i])];
      dp[i + 1][j] %= mod;
      if (dp[i + 1][j] < 0)
        dp[i + 1][j] += mod;
    }
  }

  cout << dp[n][k] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
template <class T> inline int chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline int chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1 << 30;
const ll LINF = 1LL << 58;
const int mod = 1000000007;

ll dp[110][101010];

int main() {
  int n, k;
  cin >> n >> k;
  int A[110];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int a = 0;
  vector<ll> S(k + 2, 0LL);
  dp[0][0] = 1LL;
  for (int i = 0; i < n; i++) {
    a += A[i];

    for (int j = 0; j <= min(k, a); j++) {
      S[j + 1] = (S[j] + dp[i][j]) % mod;
    }

    for (int j = 0; j <= min(k, a); j++) {
      dp[i + 1][j] = S[j + 1] - S[max(0, j - A[i])];
      dp[i + 1][j] %= mod;
      if (dp[i + 1][j] < 0)
        dp[i + 1][j] += mod;
    }
  }

  cout << dp[n][k] << endl;
}
Problem p03172 (C++)

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

const int mod = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  vector<long long> dp(k + 1);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;

    vector<long long> dp2(k + 1);
    for (int j = k - 1; j >= 0; j--) {
      dp2[j + 1] = (dp2[j + 1] + dp[j]) % mod;
      if (j + x + 1 <= k)
        dp2[j + x + 1] += -dp[j];
      if (dp2[j + x + 1] < 0)
        dp2[j + x + 1] += mod;
      dp2[j + x + 1] %= mod;
    }
    long long z{};
    for (int j = 0; j <= k; j++) {
      z = (z + dp2[j]) % mod;
      dp[j] = (dp[j] + z) % mod;
    }
  }
  cout << dp[k];
}

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

const int mod = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  vector<long long> dp(k + 1);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;

    vector<long long> dp2(k + 1);
    for (int j = k - 1; j >= 0; j--) {
      dp2[j + 1] = (dp2[j + 1] + dp[j]) % mod;
      if (j + x + 1 <= k)
        dp2[j + x + 1] = ((dp2[j + x + 1] - dp[j]) % mod + mod) % mod;
    }
    long long z{};
    for (int j = 0; j <= k; j++) {
      z = (z + dp2[j]) % mod;
      dp[j] = (dp[j] + z) % mod;
    }
  }
  cout << dp[k];
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MODYYQ = 1e9 + 7;
int n, k, a[105], dp[100][100005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    int sum = 0;
    for (int j = 0; j <= k; j++) {
      sum += dp[i - 1][j];
      if (j - a[i] - 1 >= 0)
        sum -= dp[i - 1][j - a[i] - 1];
      sum = (sum % MODYYQ + MODYYQ) % MODYYQ;
      dp[i][j] = sum;
    }
  }
  cout << dp[n][k] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MODYYQ = 1e9 + 7;
int n, k, a[105], dp[105][100005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    int sum = 0;
    for (int j = 0; j <= k; j++) {
      sum += dp[i - 1][j];
      if (j - a[i] - 1 >= 0)
        sum -= dp[i - 1][j - a[i] - 1];
      sum = (sum % MODYYQ + MODYYQ) % MODYYQ;
      dp[i][j] = sum;
    }
  }
  cout << dp[n][k] << endl;
}
Problem p03172 (C++)

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

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

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

const int MOD = 1000000007;

int dp[100001];

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  FAST_IO;
  int n, k, sum = 0;
  cin >> n >> k;
  vector<int> a(n + 1);
  dp[0] = 1;
  int up_to;
  for (int child = 1; child <= n; ++child) {
    cin >> up_to;
    vector<int> fake(k + 1);
    for (int used = k; used >= 0; --used) {
      int tmp = dp[used];
      int L = used + 1;
      int R = used + min(up_to, k - used);
      if (L <= R) {
        add_self(fake[L], tmp);
        if (R + 1 <= k)
          sub_self(fake[R + 1], tmp);
      }
    }
    int prefix_sum = 0;
    for (int i = 0; i <= k; ++i) {
      add_self(prefix_sum, fake[i]);
      add_self(dp[i], prefix_sum);
    }
  }
  cout << dp[k];
  return 0;
}


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

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

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

const int MOD = 1000000007;

int dp[100001];

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

int main() {

  FAST_IO;
  int n, k, sum = 0;
  cin >> n >> k;
  vector<int> a(n + 1);
  dp[0] = 1;
  int up_to;
  for (int child = 1; child <= n; ++child) {
    cin >> up_to;
    vector<int> fake(k + 1);
    for (int used = k; used >= 0; --used) {
      int tmp = dp[used];
      int L = used + 1;
      int R = used + min(up_to, k - used);
      if (L <= R) {
        add_self(fake[L], tmp);
        if (R + 1 <= k)
          sub_self(fake[R + 1], tmp);
      }
    }
    int prefix_sum = 0;
    for (int i = 0; i <= k; ++i) {
      add_self(prefix_sum, fake[i]);
      add_self(dp[i], prefix_sum);
    }
  }
  cout << dp[k];
  return 0;
}
Problem p03172 (C++)

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

using namespace std;

const int MAXN = 1e2 + 10, MD = 1e9 + 7;
int n, k, a[MAXN];
ll dp[MAXN][MAXN << 2];

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

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    ll ans = 0;
    for (int j = 0; j <= k; ++j) {
      ans += dp[i - 1][j];
      if (j - a[i] - 1 >= 0)
        ans -= dp[i - 1][j - a[i] - 1];
      ans = (ans + MD) % MD;
      dp[i][j] = ans;
    }
  }

  cout << dp[n][k] << '\n';

  return 0;
}


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

using namespace std;

const int MAXN = 1e5 + 10, MD = 1e9 + 7;
int n, k, a[110];
ll dp[110][MAXN];

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

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    ll ans = 0;
    for (int j = 0; j <= k; ++j) {
      ans += dp[i - 1][j];
      if (j - a[i] - 1 >= 0)
        ans -= dp[i - 1][j - a[i] - 1];
      ans = (ans + MD) % MD;
      dp[i][j] = ans;
    }
  }

  cout << dp[n][k] << '\n';

  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define ll long long int
#define hell 1000000007
#define vi vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define vii vector<pii>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sep(i, a, b) for (ll i = a - 1; i >= b; i--)
#define inf (ll)(1e16)

using namespace std;

ll helper(vi &a, ll st, ll k, vector<vi> &dp) {
  if (st == a.size() - 1 && k <= a[st])
    return 1;
  if (st >= a.size())
    return 0;
  if (dp[st][k] != -1)
    return dp[st][k];
  ll ans = 0;
  rep(i, 0, a[st] + 1) {
    if (i <= k)
      ans = (ans + helper(a, st + 1, k - i, dp)) % hell;
    else
      break;
  }
  return dp[st][k] = ans;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, 0, n) cin >> a[i];
  vector<vi> dp(n + 1, vi(k + 1, -1));
  cout << helper(a, 0, k, dp);
}

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


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

#define ll long long int
#define hell 1000000007
#define vi vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define vii vector<pii>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sep(i, a, b) for (ll i = a - 1; i >= b; i--)
#define inf (ll)(1e16)

using namespace std;

ll helper(vi &a, ll st, ll k, vector<vi> &dp) {
  if (st == a.size() - 1 && k <= a[st])
    return 1;
  if (st >= a.size())
    return 0;
  if (dp[st][k] != -1)
    return dp[st][k];
  ll ans = 0;
  rep(i, 0, a[st] + 1) {
    if (i <= k)
      ans = (ans + helper(a, st + 1, k - i, dp)) % hell;
    else
      break;
  }
  return dp[st][k] = ans;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, 0, n) cin >> a[i];
  vector<vi> dp(n + 1, vi(k + 1, 0));
  rep(i, 0, n + 1) {
    ll p = 0;
    rep(j, 0, k + 1) {
      if (i >= 1)
        p = (p + dp[i - 1][j]) % hell;
      if (i == 0 && j == 0)
        dp[i][j] = 1;
      else if (i == 0)
        dp[i][j] = 0;
      else if (j == 0)
        dp[i][j] = 1;
      else
        dp[i][j] = p;
      if (i > 0 && j >= a[i - 1])
        p = (p - dp[i - 1][j - a[i - 1]] + hell) % hell;
      // cout<<dp[i][j]<<" ";
    }
    // cout<<"\n";
  }
  cout << dp[n][k];
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7, arr[105], pre[105], sum[105], dp[105][100005];
ll solve(ll n, ll k) {
  dp[0][0] = 1;
  for (int i = 1; i <= k; ++i)
    dp[0][i] = 0;
  for (int i = 1; i <= n; ++i) {
    sum[0] = dp[i - 1][0];
    for (int j = 1; j <= k; ++j)
      sum[j] = (sum[j - 1] + dp[i - 1][j]) % mod;
    ll bound = min(k, pre[i]);
    for (int j = 0; j <= bound; ++j) {
      dp[i][j] = sum[j];
      if (j > arr[i])
        dp[i][j] = (dp[i][j] - sum[j - arr[i] - 1] + mod) % mod;
    }
  }
  return dp[n][k];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
    pre[i] = pre[i - 1] + arr[i];
  }
  cout << solve(n, k) << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7, arr[105], pre[105], sum[100005], dp[105][100005];
ll solve(ll n, ll k) {
  dp[0][0] = 1;
  for (int i = 1; i <= k; ++i)
    dp[0][i] = 0;
  for (int i = 1; i <= n; ++i) {
    sum[0] = dp[i - 1][0];
    for (int j = 1; j <= k; ++j)
      sum[j] = (sum[j - 1] + dp[i - 1][j]) % mod;
    ll bound = min(k, pre[i]);
    for (int j = 0; j <= bound; ++j) {
      dp[i][j] = sum[j];
      if (j > arr[i])
        dp[i][j] = (dp[i][j] - sum[j - arr[i] - 1] + mod) % mod;
    }
  }
  return dp[n][k];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
    pre[i] = pre[i - 1] + arr[i];
  }
  cout << solve(n, k) << '\n';
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll MOD = 1e9 + 7;
ll A[110];
ll n, k;
ll B[110][101000];
int main() {
  cin >> n >> k;
  ll a;
  for (ll j = 0; j <= k; j++) {
    B[0][j] = 0;
  }
  B[0][0] = 1;
  A[0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a;
    for (ll j = 0; j <= k; j++) {
      A[j + 1] = (A[j] + B[i - 1][j]) % MOD;
    }
    for (ll j = 0; j <= k; j++) {
      B[i][j] = (A[j + 1] - A[max(0LL, j - a)] + MOD) % MOD;
    }
  }
  cout << B[n][k] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll MOD = 1e9 + 7;
ll A[101000];
ll n, k;
ll B[110][101000];
int main() {
  cin >> n >> k;
  ll a;
  for (ll j = 0; j <= k; j++) {
    B[0][j] = 0;
  }
  B[0][0] = 1;
  A[0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a;
    for (ll j = 0; j <= k; j++) {
      A[j + 1] = (A[j] + B[i - 1][j]) % MOD;
    }
    for (ll j = 0; j <= k; j++) {
      B[i][j] = (A[j + 1] - A[max(0LL, j - a)] + MOD) % MOD;
    }
  }
  cout << B[n][k] << endl;
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int a[110];
long long dp[110][N];
long long ps[110][N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      ps[i - 1][j] = dp[i - 1][j];
      if (j)
        ps[i - 1][j] = (ps[i - 1][j] + ps[i - 1][j - 1]) % mod;
      dp[i][j] = ps[i - 1][j];
      dp[i][j] += mod - ps[i - 1][j - a[i] - 1];
      dp[i][j] %= mod;
    }

  cout << dp[n][k];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int a[110];
long long dp[110][N];
long long ps[110][N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      ps[i - 1][j] = dp[i - 1][j];
      if (j)
        ps[i - 1][j] = (ps[i - 1][j] + ps[i - 1][j - 1]) % mod;
      dp[i][j] = ps[i - 1][j];
      if (j - a[i] - 1 >= 0)
        dp[i][j] += mod - ps[i - 1][j - a[i] - 1];
      dp[i][j] %= mod;
    }

  cout << dp[n][k];

  return 0;
}
Problem p03172 (C++)

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

#define int long long
const int mod = 1e9 + 7;

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int dp[N + 1][K + 1];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < K + 1; j++) {
      dp[i][j] = 0;
    }
  }
  dp[0][0] = 1;

  for (int i = 1; i < N + 1; i++) {
    vector<int> cs(K + 2);
    for (int k = 0; k < K + 1; k++) {
      cs[k + 1] = (cs[k] + dp[i - 1][k]) % mod;
    }
    for (int j = 0; j < K + 1; j++) {
      dp[i][j] = (cs[j + 1] - cs[max(j - A[i - 1], 0LL) + mod]) % mod;
    }
  }
  cout << dp[N][K] << endl;
}

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

#define int long long
const int mod = 1e9 + 7;

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int dp[N + 1][K + 1];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < K + 1; j++) {
      dp[i][j] = 0;
    }
  }
  dp[0][0] = 1;

  for (int i = 1; i < N + 1; i++) {
    vector<int> cs(K + 2);
    for (int k = 0; k < K + 1; k++) {
      cs[k + 1] = (cs[k] + dp[i - 1][k]) % mod;
    }
    for (int j = 0; j < K + 1; j++) {
      dp[i][j] = (cs[j + 1] - cs[max(j - A[i - 1], 0LL)] + mod) % mod;
    }
  }
  cout << dp[N][K] << endl;
}
Problem p03172 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

dp = [0] * (K + 1)
dp[0] = 1

for i, a in enumerate(A, start=1):
    accDp = [0] * (K + 2)
    for k in range(1, K + 2):
        accDp[k] = accDp[k - 1] + dp[k - 1]

    for k in range(K + 1):
        dp[k] += accDp[k] - accDp[max(0, k - a)]

print(dp[K] % MOD)


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

dp = [0] * (K + 1)
dp[0] = 1

for i, a in enumerate(A, start=1):
    accDp = [0] * (K + 2)
    for k in range(1, K + 2):
        accDp[k] = accDp[k - 1] + dp[k - 1]

    for k in range(K + 1):
        dp[k] = (dp[k] + accDp[k] - accDp[max(0, k - a)]) % MOD

print(dp[K] % MOD)
Problem p03172 (C++)

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

int main() {
  int mod = 1e9 + 7;

  int N, K;
  cin >> N >> K;

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

  vector<vector<long long>> dp(N, vector<long long>(K + 1, 0));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K + 1; j++) {
      if (j == 0) {
        dp[i][j] = 1;
        continue;
      }

      if (i == 0) {
        if (j <= a[i])
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
        continue;
      }

      dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
      if (j > a[i])
        dp[i][j] -= dp[i - 1][j - 1 - a[i]];
      while (dp[i][j] > mod)
        dp[i][j] -= mod;
    }
  }

  cout << dp[N - 1][K] << endl;
  return 0;
}


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

int main() {
  int mod = 1e9 + 7;

  int N, K;
  cin >> N >> K;

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

  vector<vector<long long>> dp(N, vector<long long>(K + 1, 0));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K + 1; j++) {
      if (j == 0) {
        dp[i][j] = 1;
        continue;
      }

      if (i == 0) {
        if (j <= a[i])
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
        continue;
      }

      dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
      if (j > a[i])
        dp[i][j] += mod - dp[i - 1][j - 1 - a[i]];
      dp[i][j] %= mod;
    }
  }

  cout << dp[N - 1][K] << endl;
  return 0;
}