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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 200005;
char en = '\\n';
ll inf = 1e16;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  ll pwr2[n + 5];
  ll a[n + 5];
  ll limit = 1e18;
  for (ll i = 0; i <= n; i++) {
    cin >> a[i];
    if (i == 0)
      pwr2[i] = 1;
    else {
      pwr2[i] = min(limit, pwr2[i - 1] * 2);
    }
  }

  if (n == 0) {
    cout << ((a[0] == 1) ? 1 : -1) << en;
    return 0;
  }
  ll x[n + 5];
  x[n] = a[n];

  for (ll i = n - 1; i >= 0; i--) {
    x[i] = a[i] + min(pwr2[i] - a[i], x[i + 1]);
  }

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

  if (x[0] != 1) {
    cout << -1 << en;
    return 0;
  }

  for (ll i = 1; i <= n; i++) {
    ll useful = x[i - 1] - a[i - 1];

    x[i] = min(x[i], useful * 2);

    if (x[i] < a[i]) {
      cout << -1 << en;
      return 0;
    }

    if (x[i] < useful) {
      cout << -1 << en;
      return 0;
    }

    if (i == n && x[i] != a[i]) {
      cout << -1 << en;
      return 0;
    }
  }

  for (ll i = n - 1; i >= 0; i--) {
    ll childs = x[i + 1];
    ll validNodes = x[i] - a[i];
    if (childs > (validNodes * 2)) {
      assert(false);
      return 0;
    }

    if (childs < validNodes) {
      assert(false);
      return 0;
    }
  }

  for (ll i = 0; i <= n; i++) {
    if (x[i] > pwr2[i]) {
      assert(false);
      return 0;
    }
    if (x[i] < a[i]) {
      assert(false);
      return 0;
    }
  }

  ll res = 0;
  for (ll i = 0; i <= n; i++) {
    res += x[i];
  }

  cout << res << en;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 200005;
char en = '\\n';
ll inf = 1e16;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  ll pwr2[n + 5];
  ll a[n + 5];
  ll limit = 1e11;
  for (ll i = 0; i <= n; i++) {
    cin >> a[i];
    if (i == 0)
      pwr2[i] = 1;
    else {
      pwr2[i] = min(limit, pwr2[i - 1] * 2);
    }
  }

  if (n == 0) {
    cout << ((a[0] == 1) ? 1 : -1) << en;
    return 0;
  }
  ll x[n + 5];
  x[n] = a[n];

  for (ll i = n - 1; i >= 0; i--) {
    x[i] = a[i] + min(pwr2[i] - a[i], x[i + 1]);
  }

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

  if (x[0] != 1) {
    cout << -1 << en;
    return 0;
  }

  for (ll i = 1; i <= n; i++) {
    ll useful = x[i - 1] - a[i - 1];

    x[i] = min(x[i], useful * 2);

    if (x[i] < a[i]) {
      assert(false);
      cout << -1 << en;
      return 0;
    }

    if (x[i] < useful) {
      assert(false);
      cout << -1 << en;
      return 0;
    }
  }

  ll res = 0;
  for (ll i = 0; i <= n; i++) {
    res += x[i];
  }

  cout << res << en;

  return 0;
}