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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }

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

  if (n == 0) {
    if (A[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
  }

  else {
    ll ans = 0;
    int seigo = 0;
    vector<ll> r(n); // 深さiでの個数
    ll res = 1;
    rep(i, n + 1) {
      r[0] = 1;
      if (i == n) {
        if (res * 2 < A[i]) {
          seigo = -1;
          break;
        }

      }

      else if (i == 0) {
        if (A[i] != 0) {
          seigo = -1;
          break;
        }
      } else {

        res = res * 2 - A[i];
        r[i] = res;
        if (res <= 0) {
          seigo = -1;
          break;
        } else if (res > 1e14)
          res = 1e14;
      }

      if (res > 1e14)
        res = 1e14;
    }
    ll hon = 0;
    for (ll i = n; i >= 1; i--) {
      if (i == n) {
        ans += A[n];
        hon = A[n];
      } else {
        hon = A[i] + min(r[i], hon);
        ans += hon;
      }
    }
    ans++;

    if (seigo == -1)
      cout << -1 << endl;
    if (seigo == 0)
      cout << ans << endl;
  }

  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 <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }

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

  if (n == 0) {
    if (A[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
  }

  else {
    ll ans = 0;
    int seigo = 0;
    vector<ll> r(n); // 深さiでの個数
    ll res = 1;
    rep(i, n + 1) {
      r[0] = 1;
      if (i == n) {
        if (res * 2 < A[i]) {
          seigo = -1;
          break;
        }
      }

      if (i == 0) {
        if (A[i] != 0) {
          seigo = -1;
          break;
        }
      } else if (i != 0) {

        res = res * 2 - A[i];
        r[i] = res;
        if (res <= 0) {
          seigo = -1;
          break;
        } else if (res > 1e14)
          res = 1e14;
      }

      else if (res > 1e14)
        res = 1e14;
    }
    ll hon = 0;
    for (ll i = n; i >= 1; i--) {
      if (i == n) {
        ans += A[n];
        hon = A[n];
      } else {
        hon = A[i] + min(r[i], hon);
        ans += hon;
      }
    }
    ans++;

    if (seigo == -1)
      cout << -1 << endl;
    if (seigo == 0)
      cout << ans << endl;
  }

  return 0;
}