Problem p03172 (C++)

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

using namespace std;

#define fto(i, s, e) for (int i = s; i <= e; ++i)
#define fto1(i, s, e) for (int i = s; i < e; ++i)
#define fdto(i, s, e) for (int i = s; i >= e; --i)
#define fit(var, it)                                                           \
  for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define frit(var, it)                                                          \
  for (__typeof(var.rbegin()) it = var.rbegin(); it != var.rend(); ++it)

#define newl '\n'
#define debugt cerr << 0.001 * clock() << newl
#define debug1(x, i) cout << #x << '[' << i << "] = " << x[i] << newl
#define debug(v, l, r) fto(_i, l, r) debug1(v, _i)
#define debug2(x, i, j)                                                        \
  cout << #x << '[' << i << "][" << j << "] = " << x[i][j] << newl
#define debug3(x, i, j, k)                                                     \
  cout << #x << '[' << i << "][" << j << "][" << k << "] = " << x[i][j][k]     \
       << newl

#define ll long long
#define ii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define glen(v) int((v).size())

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> const &v) {
  return os << '(' << v.ff << ", " << v.ss << ')';
}

#define eps 1e-15
#define oo 1000000007
#define OO 1000000000000000003LL
#define maxn 103

int n, k;
int a[maxn], sum[maxn];
int f[maxn][100003];

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

  scanf("%d%d", &n, &k);

  fto(i, 1, n) scanf("%d", &a[i]);

  fto(i, 0, k) sum[i] = 1;

  fto(i, 1, n) {
    fto(j, 0, k) {
      int mn = min(j, a[i]);
      f[i][j] = (sum[j] - (j - mn == 0 ? 0 : sum[j - mn - 1])) % oo;
      if (f[i][j] < 0)
        f[i][j] += oo;
    }
    sum[0] = f[i][0];
    fto(j, 1, k) sum[j] = (sum[j - 1] + f[i][j]) % oo;
  }

  printf("%d\n", f[n][k]);

  return 0;
}


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

using namespace std;

#define fto(i, s, e) for (int i = s; i <= e; ++i)
#define fto1(i, s, e) for (int i = s; i < e; ++i)
#define fdto(i, s, e) for (int i = s; i >= e; --i)
#define fit(var, it)                                                           \
  for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define frit(var, it)                                                          \
  for (__typeof(var.rbegin()) it = var.rbegin(); it != var.rend(); ++it)

#define newl '\n'
#define debugt cerr << 0.001 * clock() << newl
#define debug1(x, i) cout << #x << '[' << i << "] = " << x[i] << newl
#define debug(v, l, r) fto(_i, l, r) debug1(v, _i)
#define debug2(x, i, j)                                                        \
  cout << #x << '[' << i << "][" << j << "] = " << x[i][j] << newl
#define debug3(x, i, j, k)                                                     \
  cout << #x << '[' << i << "][" << j << "][" << k << "] = " << x[i][j][k]     \
       << newl

#define ll long long
#define ii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define glen(v) int((v).size())

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> const &v) {
  return os << '(' << v.ff << ", " << v.ss << ')';
}

#define eps 1e-15
#define oo 1000000007
#define OO 1000000000000000003LL
#define maxn 103

int n, k;
int a[maxn], sum[100003];
int f[maxn][100003];

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

  scanf("%d%d", &n, &k);

  fto(i, 1, n) scanf("%d", &a[i]);

  fto(i, 0, k) sum[i] = 1;

  fto(i, 1, n) {
    fto(j, 0, k) {
      int mn = min(j, a[i]);
      f[i][j] = (sum[j] - (j - mn == 0 ? 0 : sum[j - mn - 1])) % oo;
      if (f[i][j] < 0)
        f[i][j] += oo;
    }
    sum[0] = f[i][0];
    fto(j, 1, k) sum[j] = (sum[j - 1] + f[i][j]) % oo;
  }

  printf("%d\n", f[n][k]);

  return 0;
}