Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1001001001001001001;

// N^P(modM)

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 5);
  rep(i, n + 1) cin >> a[i];

  vector<ll> A(n + 5); // 上限
  ll now = 1;

  rep(i, n + 1) {
    A[i] = now;
    now -= a[i];

    if (now < 0) {
      cout << -1;
      return 0;
    }

    if (now < 100100100100100)
      now *= 2;
    //  cout<<A[i]<<endl;
  }

  if (n == 0) {
    if (a[0] == 1)
      cout << 1;
    else
      cout << -1;
    return 0;
  } else if (n == 1) {
    if (a[0] == 1) {
      cout << -1;
      return 0;
    } else {
      if (a[1] > 2) {
        cout << -1;
        return 0;
      }
      if (a[1] == 2) {
        cout << 3;
        return 0;
      }
      if (a[1] == 1) {
        cout << 2;
        return 0;
      }
    }
  }

  else {

    vector<ll> ans(n + 5);
    bool check = true;
    if (a[n] <= A[n]) {
      ans[n] = a[n];
    } else {
      cout << -1;
      return 0;
    }
    ll cnt = 0;
    cnt += ans[n];
    // cout<<cnt<<"c"<<endl;

    if (a[0] == 1) {
      cout << -1;
      return 0;
    }

    for (ll i = n - 1; i >= 0; i--) {
      if (a[i] > A[i]) {
        check = false;
        break;
      }
      ans[i] = ans[i + 1] + a[i];
      if (ans[i] > A[i]) {
        ans[i] = A[i];
      }
      cnt += ans[i];
      //  cout<<ans[i]<<endl;
    }

    if (check == false) {
      cout << -1;
      return 0;
    } else {
      cout << cnt;
      return 0;
    }
  }
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1001001001001001001;

// N^P(modM)

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];

  vector<ll> A(n + 1); // 上限
  ll now = 1;

  rep(i, n + 1) {
    A[i] = now;
    now -= a[i];
    if (now < 100100100100100)
      now *= 2;
    //  cout<<A[i]<<endl;
  }

  if (n == 0) {
    if (a[0] == 1)
      cout << 1;
    else
      cout << -1;
    return 0;
  } else if (n == 1) {
    if (a[0] == 1) {
      cout << -1;
      return 0;
    } else {
      if (a[1] > 2) {
        cout << -1;
        return 0;
      }
      if (a[1] == 2) {
        cout << 3;
        return 0;
      }
      if (a[1] == 1) {
        cout << 2;
        return 0;
      }
    }
  }

  else {

    vector<ll> ans(n);
    bool check = true;
    if (a[n] <= A[n]) {
      ans[n] = a[n];
    } else {
      cout << -1;
      return 0;
    }
    ll cnt = 0;
    cnt += ans[n];
    // cout<<cnt<<"c"<<endl;

    if (a[0] == 1) {
      cout << -1;
      return 0;
    }

    for (ll i = n - 1; i >= 0; i--) {
      if (a[i] > A[i]) {
        check = false;
        break;
      }
      ans[i] = ans[i + 1] + a[i];
      if (ans[i] > A[i]) {
        ans[i] = A[i];
      }
      cnt += ans[i];
      //  cout<<ans[i]<<endl;
    }

    if (check == false) {
      cout << -1;
      return 0;
    } else {
      cout << cnt;
    }
  }
}