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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author champon
 */

//<editor-fold desc="definitions">
#include <bits/stdc++.h>
#define se cout << "test ok" << endl;
#define sp << " " <<
#define enl "\\n"
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using viii = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using vlll = vector<vector<vector<ll>>>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
ll mod = ll(1e9) + 7;
ll inf = ll(1e18);
void bl(bool b, ostream &out = cout) { out << (b ? "Yes" : "No") << endl; }
void bl2(bool b, ostream &out = cout) { out << (b ? "YES" : "NO") << endl; }
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

#define TO_STRING(varName) #varName
#include <iostream>
#include <map>
#include <vector>
using namespace std;

namespace dbg {

using ll = long long;

class Debug {
  void prefix() {
    cout << "\\033[7m\\033[35m"
         << " DEBUG "
         << "\\033[0m" << endl;
  }

  void out_helper() { cout << endl; }

  void out_with_name_helper() { cout << endl; }

  template <typename T1, typename... T2> void out_helper(T1 v, T2... args);

  template <typename T1, typename... T2>
  void out_with_name_helper(string s, T1 v, T2... args);

  // value
  template <typename T> void print(T val);

  // pair
  template <typename A, typename B> void print(pair<A, B> p);

  // tuple
  template <typename A, typename B, typename C> void print(tuple<A, B, C> t);

  // vector
  template <typename T> void print(vector<T> &vec);

  // set
  template <typename T> void print(set<T> &st);

  // map
  template <typename A, typename B> void print(map<A, B> mp);

public:
  Debug() {}

  template <typename... T> void out(T... args) {
    prefix();
    out_helper(args...);
  }

  template <typename... T> void out_with_name(T... args) {
    prefix();
    out_with_name_helper(args...);
  }

  template <typename T> void outg(vector<vector<T>> &g) {
    prefix();
    for (auto &v1 : g) {
      for (auto &v2 : v1) {
        cout << v2 << " ";
      }
      cout << endl;
    }
  }
};

template <typename T1, typename... T2>
void Debug::out_helper(T1 v, T2... args) {
  print(v);
  cout << endl;
  out_helper(args...);
}

template <typename T1, typename... T2>
void Debug::out_with_name_helper(string s, T1 v, T2... args) {
  cout << s << ": ";
  print(v);
  cout << endl;
  out_with_name_helper(args...);
}

template <typename T> void Debug::print(T val) { cout << val; }

template <typename A, typename B> void Debug::print(pair<A, B> p) {
  cout << "(";
  print(p.first);
  cout << ", ";
  print(p.second);
  cout << ")";
}

template <typename A, typename B, typename C>
void Debug::print(tuple<A, B, C> t) {
  cout << "(";
  print(get<0>(t));
  cout << ", ";
  print(get<1>(t));
  cout << ", ";
  print(get<2>(t));
  cout << ")";
}

template <typename T> void Debug::print(vector<T> &vec) {
  cout << "[";
  bool fir = true;
  for (auto &e : vec) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "]";
}

template <typename T> void Debug::print(set<T> &st) {
  cout << "{";
  bool fir = true;
  for (auto &e : st) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "}";
}

template <typename A, typename B> void Debug::print(map<A, B> mp) {
  cout << "{";
  bool fir = true;
  for (auto &p : mp) {
    if (!fir)
      cout << " ";
    print(p), fir = false;
  }
  cout << "}";
}

} // namespace dbg

#endif // INCLUDE_DEBUG_HPP

dbg::Debug debug;
//</editor-fold>

class CFolia {
public:
  void solve(std::istream &in, std::ostream &out) {
    int n;
    in >> n;
    vl a(n + 1);
    for (int i = 0; i <= n; i++)
      in >> a[i];

    if (n == 0) {
      if (a[0] == 1)
        out << 1 << endl;
      else
        out << -1 << endl;
      return;
    }

    if (n > 0 && a[0] != 0) {
      out << -1 << endl;
      return;
    }

    bool judge = true;
    vl mxv(n + 1);
    mxv[0] = 1;
    ll d = 1;
    while (d < n) {
      mxv[d] = min((mxv[d - 1] - a[d - 1]) * 2, (ll)4e18);
      ++d;
    }

    vl v(n + 1);
    v[n] = a[d];
    d = n;
    ll ans = a[d];
    while (d > 0) {
      if (mxv[d - 1] - a[d - 1] < ceil((double)v[d] / 2)) {
        judge = false;
      }
      v[d - 1] = min(mxv[d - 1] - a[d - 1], v[d]) + a[d - 1];
      ans += v[d - 1];
      --d;
    }

    // debug.out(v);

    if (!judge)
      out << -1 << endl;
    else
      out << ans << endl;
  }
};

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  CFolia solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author champon
 */

//<editor-fold desc="definitions">
#include <bits/stdc++.h>
#define se cout << "test ok" << endl;
#define sp << " " <<
#define enl "\\n"
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using viii = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using vlll = vector<vector<vector<ll>>>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
ll mod = ll(1e9) + 7;
ll inf = ll(1e18);
void bl(bool b, ostream &out = cout) { out << (b ? "Yes" : "No") << endl; }
void bl2(bool b, ostream &out = cout) { out << (b ? "YES" : "NO") << endl; }
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

#define TO_STRING(varName) #varName
#include <iostream>
#include <map>
#include <vector>
using namespace std;

namespace dbg {
using ll = long long;

class Debug {
  void prefix() {
    cout << "\\033[7m\\033[35m"
         << " DEBUG "
         << "\\033[0m" << endl;
  }

  void out_helper() { cout << endl; }

  void out_with_name_helper() { cout << endl; }

  template <typename T1, typename... T2> void out_helper(T1 v, T2... args);

  template <typename T1, typename... T2>
  void out_with_name_helper(string s, T1 v, T2... args);

  // value
  template <typename T> void print(T val);

  // pair
  template <typename A, typename B> void print(pair<A, B> p);

  // tuple
  template <typename A, typename B, typename C> void print(tuple<A, B, C> t);

  // vector
  template <typename T> void print(vector<T> &vec);

  // set
  template <typename T> void print(set<T> &st);

  // map
  template <typename A, typename B> void print(map<A, B> mp);

public:
  Debug() {}

  template <typename... T> void out(T... args) {
    prefix();
    out_helper(args...);
  }

  template <typename... T> void out_with_name(T... args) {
    prefix();
    out_with_name_helper(args...);
  }

  template <typename T> void outg(vector<vector<T>> &g) {
    prefix();
    for (auto &v1 : g) {
      for (auto &v2 : v1) {
        cout << v2 << " ";
      }
      cout << endl;
    }
  }
};

template <typename T1, typename... T2>
void Debug::out_helper(T1 v, T2... args) {
  print(v);
  cout << endl;
  out_helper(args...);
}

template <typename T1, typename... T2>
void Debug::out_with_name_helper(string s, T1 v, T2... args) {
  cout << s << ": ";
  print(v);
  cout << endl;
  out_with_name_helper(args...);
}

template <typename T> void Debug::print(T val) { cout << val; }

template <typename A, typename B> void Debug::print(pair<A, B> p) {
  cout << "(";
  print(p.first);
  cout << ", ";
  print(p.second);
  cout << ")";
}

template <typename A, typename B, typename C>
void Debug::print(tuple<A, B, C> t) {
  cout << "(";
  print(get<0>(t));
  cout << ", ";
  print(get<1>(t));
  cout << ", ";
  print(get<2>(t));
  cout << ")";
}

template <typename T> void Debug::print(vector<T> &vec) {
  cout << "[";
  bool fir = true;
  for (auto &e : vec) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "]";
}

template <typename T> void Debug::print(set<T> &st) {
  cout << "{";
  bool fir = true;
  for (auto &e : st) {
    if (!fir)
      cout << " ";
    print(e), fir = false;
  }
  cout << "}";
}

template <typename A, typename B> void Debug::print(map<A, B> mp) {
  cout << "{";
  bool fir = true;
  for (auto &p : mp) {
    if (!fir)
      cout << " ";
    print(p), fir = false;
  }
  cout << "}";
}

} // namespace dbg

#endif // INCLUDE_DEBUG_HPP

dbg::Debug debug;
//</editor-fold>

class CFolia {
public:
  void solve(std::istream &in, std::ostream &out) {
    int n;
    in >> n;
    vl a(n + 1);
    for (int i = 0; i <= n; i++)
      in >> a[i];

    if (n == 0) {
      if (a[0] == 1)
        out << 1 << endl;
      else
        out << -1 << endl;
      return;
    }

    vl maxv(30);
    maxv[0] = 1;
    ll noleaf = 1;
    for (int i = 1; i <= min(n, 30); i++) {
      maxv[i] = noleaf * 2LL;
      noleaf = maxv[i] - a[i];
    }
    // debug.out(maxv);

    ll sum = 0, node = a[n];
    for (int i = n; i > 0; i--) {
      sum += node;
      if (maxv[i - 1] - a[i - 1] < (ll)ceil((double)node / 2)) {
        out << -1 << endl;
        return;
      }
      if (i - 1 >= 27) {
        node = a[i];
        continue;
      }
      node = min(maxv[i - 1], node + a[i - 1]);
      // debug.out(node, sum);
    }

    out << sum + 1 << endl;
  }
};

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  CFolia solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}