Problem p03172 (C++)

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
#define ppi pair<int, int>
#define endl "\n"
#define nl cout << "\n"
#define deb(x) cout << #x << " " << x << endl;

int n, k;
const int MAX_N = 102, MAX_K = 1e5 + 2;
const int mod = 1e9 + 7;
int arr[MAX_N];
int dp[MAX_N][MAX_K];
int s[MAX_N][MAX_K];
// O(N*K*K) time = O(NK^2) time
// O(NK) space
int Top_Down(int i = n, int candies = k) {
  // Base Case
  if (i == 0)
    return (candies == 0);
  if (dp[i][candies] != -1)
    return dp[i][candies] % mod;
  // Choice
  int ways = 0;
  for (int x = 0; x <= arr[i]; ++x) {
    if (candies >= x)
      ways += (Top_Down(i - 1, candies - x) % mod);
  }
  return (dp[i][candies] = ways % mod);
}

int Bottom_Up() {
  // Base
  dp[0][0] = 1;
  for (int j = 1; j <= k; ++j)
    dp[0][j] = 0;
  for (int i = 1; i <= n; ++i)
    dp[i][0] = 1;
  // Choice

  for (int i = 1; i <= n; ++i) {
    for (int candies = 1; candies <= k; ++candies) {
      int ways = 0;
      for (int x = 0; x <= arr[i]; ++x) {
        if (candies >= x)
          ways += dp[i - 1][candies - x] % mod;
      }
      dp[i][candies] = ways % mod;
    }
  }
  return dp[n][k];
}

/*
dp[0][0] = 1;
for(int j = 1; j <= k; ++j) dp[0][j] = 0;
for(int i = 1; i <= n; ++i) dp[i][0] = 1;

for(i = 1; i <= n; ++i)
{
    s[i-1][0] = dp[i-1][0];
    for(int j = 1; j <= k; ++j) {
        //compute s[i][j]
        s[i-1][j] = (dp[i-1][j] + s[i-1][j-1])%mod;
    }

    for(int j = 1; j <= k; ++j) {
       //compute dp[i][j]
       dp[i][j] = s[i-1][j]
       if(j-arr[i]-1 >= 0)
        dp[i][j] = (dp[i][j] - s[i-1][j-arr[i]-1] + mod)%mod;
    }
}
s(i,j) = T(i,0) + T(i,1) + ..... + T(i,j)
T(i,j) = s(i-1,j) - s(i-1,j-a[i]-1)

*/

int Bottom_Up2() {
  dp[0][0] = 1;
  for (int j = 1; j <= k; ++j)
    dp[0][j] = 0;
  for (int i = 1; i <= n; ++i)
    dp[i][0] = 1;
  int i, j;
  for (i = 1; i <= n; ++i) {
    s[i - 1][0] = dp[i - 1][0];
    for (j = 1; j <= k; ++j) {
      // compute s[i][j]
      s[i - 1][j] = (dp[i - 1][j] + s[i - 1][j - 1]) % mod;
    }

    for (j = 1; j <= k; ++j) {
      // compute dp[i][j]
      dp[i][j] = s[i - 1][j];
      if (j - arr[i] - 1 >= 0)
        dp[i][j] = (dp[i][j] - s[i - 1][j - arr[i] - 1] + mod) % mod;
    }
  }
  return dp[n][k] % mod;
}

int32_t main() {
  IOS;
  int t;
  cin >> t;
  while (t--) {
    memset(dp, -1, sizeof(dp));
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
      cin >> arr[i];
    cout << Bottom_Up2() % mod << endl;
  }
}

Fixed submission:
/*
      @author: Charan Sai
*/
#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
#define ppi pair<int, int>
#define endl "\n"
#define nl cout << "\n"
#define deb(x) cout << #x << " " << x << endl;

int n, k;
const int MAX_N = 102, MAX_K = 1e5 + 2;
const int mod = 1e9 + 7;
int arr[MAX_N];
int dp[MAX_N][MAX_K];
int s[MAX_N][MAX_K];
// O(N*K*K) time = O(NK^2) time
// O(NK) space
int Top_Down(int i = n, int candies = k) {
  // Base Case
  if (i == 0)
    return (candies == 0);
  if (dp[i][candies] != -1)
    return dp[i][candies] % mod;
  // Choice
  int ways = 0;
  for (int x = 0; x <= arr[i]; ++x) {
    if (candies >= x)
      ways += (Top_Down(i - 1, candies - x) % mod);
  }
  return (dp[i][candies] = ways % mod);
}

int Bottom_Up() {
  // Base
  dp[0][0] = 1;
  for (int j = 1; j <= k; ++j)
    dp[0][j] = 0;
  for (int i = 1; i <= n; ++i)
    dp[i][0] = 1;
  // Choice

  for (int i = 1; i <= n; ++i) {
    for (int candies = 1; candies <= k; ++candies) {
      int ways = 0;
      for (int x = 0; x <= arr[i]; ++x) {
        if (candies >= x)
          ways += dp[i - 1][candies - x] % mod;
      }
      dp[i][candies] = ways % mod;
    }
  }
  return dp[n][k];
}

/*
dp[0][0] = 1;
for(int j = 1; j <= k; ++j) dp[0][j] = 0;
for(int i = 1; i <= n; ++i) dp[i][0] = 1;

for(i = 1; i <= n; ++i)
{
    s[i-1][0] = dp[i-1][0];
    for(int j = 1; j <= k; ++j) {
        //compute s[i][j]
        s[i-1][j] = (dp[i-1][j] + s[i-1][j-1])%mod;
    }

    for(int j = 1; j <= k; ++j) {
       //compute dp[i][j]
       dp[i][j] = s[i-1][j]
       if(j-arr[i]-1 >= 0)
        dp[i][j] = (dp[i][j] - s[i-1][j-arr[i]-1] + mod)%mod;
    }
}
s(i,j) = T(i,0) + T(i,1) + ..... + T(i,j)
T(i,j) = s(i-1,j) - s(i-1,j-a[i]-1)

*/

int Bottom_Up2() {
  dp[0][0] = 1;
  for (int j = 1; j <= k; ++j)
    dp[0][j] = 0;
  for (int i = 1; i <= n; ++i)
    dp[i][0] = 1;
  int i, j;
  for (i = 1; i <= n; ++i) {
    s[i - 1][0] = dp[i - 1][0];
    for (j = 1; j <= k; ++j) {
      // compute s[i][j]
      s[i - 1][j] = (dp[i - 1][j] + s[i - 1][j - 1]) % mod;
    }

    for (j = 1; j <= k; ++j) {
      // compute dp[i][j]
      dp[i][j] = s[i - 1][j];
      if (j - arr[i] - 1 >= 0)
        dp[i][j] = (dp[i][j] - s[i - 1][j - arr[i] - 1] + mod) % mod;
    }
  }
  return dp[n][k] % mod;
}

int32_t main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    memset(dp, -1, sizeof(dp));
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
      cin >> arr[i];
    cout << Bottom_Up2() % mod << endl;
  }
}