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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b <= a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  bool ok = 1;
  rep(i, n + 1) { cin >> a[i]; }
  if (n == 0) {
    if (a[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
    return 0;
  }

  vector<ll> r(n + 1, a[n]);
  for (int i = n - 1; i >= 0; i--) {
    r[i] = r[i + 1] + a[i];
  }

  ll x = 1, tot = 0;
  chmin(r[0], 1ll);
  for (int i = 1; i <= n; i++) {
    tot = min(r[i], 2 * x);
    x = tot - a[i];
    chmin(r[i], tot);
  }

  ll ans = 0;
  a[n]--;
  rep(i, n + 1) {
    if (r[i] <= a[i]) {
      cout << -1 << endl;
      return 0;
    }
    ans += r[i];
  }
  cout << ans << endl;
}


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>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  bool ok = 1;
  rep(i, n + 1) {
    cin >> a[i];
    if (i < 40)
      ok &= a[i] < (1ll << i);
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> u(n + 1, LINF);
  rep(i, 40) u[i] = (1ll << i);
  vector<ll> l(n + 1, 1ll);
  ll x = 1, tot = 1;
  for (int i = 1; i <= n; i++) {
    tot = 2 * x;
    chmin(tot, LINF);
    x = tot - a[i];
    l[i] = tot;
  }
  vector<ll> r(n + 1, a[n]);
  for (int i = n - 1; i >= 0; i--) {
    r[i] = r[i + 1] + a[i];
    chmin(r[i], LINF);
  }

  ll ans = 0;
  rep(i, n + 1) { ans += min({u[i], l[i], r[i]}); }
  cout << ans << endl;
}