Problem p03172 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long int
#define pll pair<int, int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define mod 1000000007
#define eps 0.00000001
#define inf 10000000000001
#define all(x) (x).begin(), (x).end()
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x, y) make_pair((x), (y))
#define vv(dt) vector<vector<dt>>
#define fastio(x)                                                              \
  ios_base::sync_with_stdio(x);                                                \
  cin.tie(NULL)
#define init(v, s) memset(v, s, sizeof(v))
#define bug(x)                                                                 \
  cerr << "LINE: " << __LINE__ << " || " << #x << " = " << x << endl
#define loop(i, s, n) for (auto i = s; i < n; i++)
#define rloop(i, n, b) for (auto i = n - 1; i >= b; i--)
using namespace std;

signed main() {
  fastio(0);
  int n, k;
  cin >> n >> k;
  int a[n];
  loop(i, 0, n) cin >> a[i];
  int dp[k + 1][n + 1];
  init(dp, 0);
  // no of ways for n=1, fill upto a[0], is 1.
  loop(i, 0, k + 1) {
    if (i <= a[0])
      dp[i][1] = 1;
    else
      dp[i][1] = 0;
    // use dp[k][0] as prefix array
    if (i == 0)
      dp[i][0] = dp[i][1];
    else
      dp[i][0] = (dp[i - 1][0] + dp[i][1]) % mod;
  }

  // dp[k][n] = sum(dp[k-ai][n-1]) ; ai E [0,min(k,an)].
  for (int N = 2; N <= n; N++) {
    for (int K = 0; K <= k; K++) {
      if (a[N - 1] < K) {
        bug(K);
        dp[K][N] = (dp[K][0] - dp[K - a[N - 1] - 1][0] + mod) % mod;
      } else
        dp[K][N] = dp[K][0] % mod;
    }
    // update prefix array
    for (int K = 0; K <= k; K++) {
      if (K == 0)
        dp[K][0] = dp[K][N] % mod;
      else
        dp[K][0] = (dp[K - 1][0] + dp[K][N]) % mod;
    }

    // bug("DP");
    // loop(i,0,k+1)
    // {
    // 	loop(j,0,n+1) cout<<dp[i][j]<<" ";
    // 	cout<<endl;
    // }
  }

  // ans = dp[k][n]
  cout << dp[k][n] % mod << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define pll pair<int, int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define mod 1000000007
#define eps 0.00000001
#define inf 10000000000001
#define all(x) (x).begin(), (x).end()
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x, y) make_pair((x), (y))
#define vv(dt) vector<vector<dt>>
#define fastio(x)                                                              \
  ios_base::sync_with_stdio(x);                                                \
  cin.tie(NULL)
#define init(v, s) memset(v, s, sizeof(v))
#define bug(x)                                                                 \
  cerr << "LINE: " << __LINE__ << " || " << #x << " = " << x << endl
#define loop(i, s, n) for (auto i = s; i < n; i++)
#define rloop(i, n, b) for (auto i = n - 1; i >= b; i--)
using namespace std;

signed main() {
  fastio(0);
  int n, k;
  cin >> n >> k;
  int a[n];
  loop(i, 0, n) cin >> a[i];
  int dp[k + 1][n + 1];
  init(dp, 0);
  // no of ways for n=1, fill upto a[0], is 1.
  loop(i, 0, k + 1) {
    if (i <= a[0])
      dp[i][1] = 1;
    else
      dp[i][1] = 0;
    // use dp[k][0] as prefix array
    if (i == 0)
      dp[i][0] = dp[i][1];
    else
      dp[i][0] = (dp[i - 1][0] + dp[i][1]) % mod;
  }

  // dp[k][n] = sum(dp[k-ai][n-1]) ; ai E [0,min(k,an)].
  for (int N = 2; N <= n; N++) {
    for (int K = 0; K <= k; K++) {
      if (a[N - 1] < K) {
        // bug(K);
        dp[K][N] = (dp[K][0] - dp[K - a[N - 1] - 1][0] + mod) % mod;
      } else
        dp[K][N] = dp[K][0] % mod;
    }
    // update prefix array
    for (int K = 0; K <= k; K++) {
      if (K == 0)
        dp[K][0] = dp[K][N] % mod;
      else
        dp[K][0] = (dp[K - 1][0] + dp[K][N]) % mod;
    }

    // bug("DP");
    // loop(i,0,k+1)
    // {
    // 	loop(j,0,n+1) cout<<dp[i][j]<<" ";
    // 	cout<<endl;
    // }
  }

  // ans = dp[k][n]
  cout << dp[k][n] % mod << endl;
  return 0;
}