Problem p03000 (C++)

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

using namespace std;

/* g++ -g -fsanitize=address -lasan -std=c++11 -Dfoo_ */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define FOR_ALL(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR_ALLR(i, c)                                                         \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define SZ(array) (sizeof(array) / sizeof(array[0]))
#define lc(x) (x << 1)       /* 2*x */
#define rc(x) (x << 1 | 1)   /* 2*x+1 */
#define lowbit(x) (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long LL;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<int> VI;
typedef vector<string> VS;

/* check if a key is in container C */
template <class C> inline bool in_(const typename C::key_type &k, const C &A) {
  return A.find(k) != A.end();
}
inline bool in_(const string &s, const string &S) {
  return S.find(s) != string::npos;
}

int main() {
#ifdef foo_
  freopen("foo", "rt", stdin);
#endif
  ios::sync_with_stdio(false);
  int n, x;
  while (cin >> n >> x) {
    VI L(n + 1);
    FOR(i, 1, n + 1)
    cin >> L[i];
    VI D(2);
    D[1] = 0;
    FOR(i, 2, n + 2)
    D[i] = D[i - 1] + L[i - 1];
    int ans = 0;
    FOR(i, 1, n + 2) if (D[i] <= x) ans++;
    cout << ans << endl;
  }
  return 0;
}


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

using namespace std;

/* g++ -g -fsanitize=address -lasan -std=c++11 -Dfoo_ */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define FOR_ALL(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR_ALLR(i, c)                                                         \
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define SZ(array) (sizeof(array) / sizeof(array[0]))
#define lc(x) (x << 1)       /* 2*x */
#define rc(x) (x << 1 | 1)   /* 2*x+1 */
#define lowbit(x) (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long LL;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<int> VI;
typedef vector<string> VS;

/* check if a key is in container C */
template <class C> inline bool in_(const typename C::key_type &k, const C &A) {
  return A.find(k) != A.end();
}
inline bool in_(const string &s, const string &S) {
  return S.find(s) != string::npos;
}

int main() {
#ifdef foo_
  freopen("foo", "rt", stdin);
#endif
  ios::sync_with_stdio(false);
  int n, x;
  while (cin >> n >> x) {
    VI L(n + 1);
    FOR(i, 1, n + 1)
    cin >> L[i];
    VI D(n + 2);
    D[1] = 0;
    FOR(i, 2, n + 2)
    D[i] = D[i - 1] + L[i - 1];
    int ans = 0;
    FOR(i, 1, n + 2) if (D[i] <= x) ans++;
    cout << ans << endl;
  }
  return 0;
}