Problem p03743 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fbo find_by_order
#define ook order_of_key
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define MOD 998244353
#define MID (l + r) / 2
#define ALL(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << endl
#define mx 300003
#define pc(x) putchar_unlocked(x);
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int n, d, a[mx], di[mx], b[mx];
int main() {
  cin >> n >> d;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  di[0] = d;
  for (int i = 1; i <= n; i++) {
    di[i] = di[i - 1];
    if (abs(di[i - 1] - a[i]) < di[i - 1]) {
      di[i] = abs(di[i - 1] - a[i]);
    }
  }
  b[n] = 1;
  if (a[n] == 1)
    b[n]++;
  for (int i = n - 1; i > 0; i--) {
    // ga pake
    int sa = 1e9 + 7;
    if (abs(b[i + 1] - a[i]) >= b[i + 1])
      sa = b[i + 1];
    // pake kurang
    int du = b[i + 1] + a[i];
    // pake lebih a[i]-cari==b[i+1]
    int ti = 1e9 + 7;
    if (a[i] >= b[i + 1] && a[i] - b[i + 1] >= b[i + 1])
      ti = a[i] - b[i + 1];
    b[i] = min(sa, min(du, ti));
  }
  for (int i = 1; i <= n; i++) {
    // debug(b[i]);
  }
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    if (x == n) {
      if (di[x - 1] > 0) {
        cout << "YES\n";
      } else
        cout << "NO\n";
      continue;
    }
    if (di[x - 1] >= b[x + 1]) {
      cout << "YES\n";
    } else
      cout << "NO\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fbo find_by_order
#define ook order_of_key
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define MOD 998244353
#define MID (l + r) / 2
#define ALL(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << endl
#define mx 1000003
#define pc(x) putchar_unlocked(x);
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int n, d, a[mx], di[mx], b[mx];
int main() {
  cin >> n >> d;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  di[0] = d;
  for (int i = 1; i <= n; i++) {
    di[i] = di[i - 1];
    if (abs(di[i - 1] - a[i]) < di[i - 1]) {
      di[i] = abs(di[i - 1] - a[i]);
    }
  }
  b[n] = 1;
  if (a[n] == 1)
    b[n]++;
  for (int i = n - 1; i > 0; i--) {
    // ga pake
    int sa = 1e9 + 7;
    if (abs(b[i + 1] - a[i]) >= b[i + 1])
      sa = b[i + 1];
    // pake kurang
    int du = b[i + 1] + a[i];
    // pake lebih a[i]-cari==b[i+1]
    int ti = 1e9 + 7;
    if (a[i] >= b[i + 1] && a[i] - b[i + 1] >= b[i + 1])
      ti = a[i] - b[i + 1];
    b[i] = min(sa, min(du, ti));
  }
  for (int i = 1; i <= n; i++) {
    // debug(b[i]);
  }
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    if (x == n) {
      if (di[x - 1] > 0) {
        cout << "YES\n";
      } else
        cout << "NO\n";
      continue;
    }
    if (di[x - 1] >= b[x + 1]) {
      cout << "YES\n";
    } else
      cout << "NO\n";
  }
}