alexjercan/bugnet huggingface dataset:
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ull;
typedef long long int ll;
typedef pair<ll, ll> P;

int main() {
  ll N;
  cin >> N;
  ll a[N + 1];
  REP(i, N + 1) { cin >> a[i]; }
  ll low[N + 1], high[N + 1];
  low[N] = a[N];
  high[N] = a[N];
  for (int n = N - 1; n >= 0; n--) {
    low[n] = (low[n + 1] + 1) / 2 + a[n];
    high[n] = high[n + 1] + a[n];
  }

  if (low[0] > 1 || high[0] < 1) {
    cout << -1 << endl;
    return 0;
  }

  ll d[N + 1];
  d[0] = 1;
  ll sum = 1;
  for (int n = 1; n <= N; n++) {
    d[n] = min(high[n], (d[n - 1] - a[n - 1]) * 2);
    sum += d[n];
  }
  cout << sum << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ull;
typedef long long int ll;
typedef pair<ll, ll> P;

int main() {
  int N;
  cin >> N;
  int a[N + 1];
  int max_a = 0;
  REP(i, N + 1) {
    cin >> a[i];
    if (a[i] > 0) {
      if (log2(a[i]) > i) {
        cout << -1 << endl;
        return 0;
      }
    }
    max_a = max(max_a, a[i]);
  }
  vector<vector<int>> d(N + 1);
  // REP(i,N+1){
  //     vector<int> dd;
  //     d.pb(dd);
  // }
  d[N] = vector<int>(a[N] + 1);
  d[N][a[N]] = a[N];
  // d[0] = vector<int>(2);
  // d[0][1] = -1;
  int low[N + 1], high[N + 1];
  REP(i, N) {
    low[i] = INF;
    high[i] = 0;
  }
  low[N] = d[N][a[N]];
  high[N] = d[N][a[N]];
  for (int i = 1; i <= N; i++) {
    int n = N - i;
    // cout << "n=" << n << endl;
    d[n] = vector<int>(high[n + 1] + a[n] + 1);
    for (int t = 0; t <= high[n + 1] + a[n]; t++) {
      d[n][t] = -1;
    }
    int l = ceil(low[n + 1] / 2.0) + a[n];
    int h = high[n + 1] + a[n];
    if (log2(l) > n) {
      cout << -1 << endl;
      return 0;
    }
    if (log2(h) > n) {
      h = (int)pow(2, n);
    }
    for (int pre = (h - a[n]); pre <= (h - a[n]) * 2; pre++) {
      if (low[n + 1] <= pre && pre <= high[n + 1]) {
        d[n][h] = max(d[n][h], d[n + 1][pre] + h);
        low[n] = min(low[n], h);
        high[n] = max(high[n], h);
        // cout << "d[" << n <<"," << t << "] = " << d[n][t] << "(" <<
        // d[n+1][pre] << "+" << t << ")" <<endl;
      }
    }
  }
  cout << d[0][1] << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> v(n + 1);
  cin >> v;
  ll sum = 0;
  ll ans = 0;
  for (auto &e : v)
    sum += e;
  ll xmin = 1, xmax = 1;
  for (ll i = 0; i < n + 1; ++i) {
    ans += xmax;
    if (v[i] > xmax) {
      cout << -1 << endl;
      return 0;
    }
    xmin -= v[i];
    xmax -= v[i];
    xmin = max(0LL, xmin);
    xmax *= 2;
    sum -= v[i];
    if (xmax > sum)
      xmax = sum;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int n;
  cin >> n;
  vector<ll> v(n + 1);
  cin >> v;
  map<ll, ll> mp;
  mp[1] = 1;
  for (int i = 0; i < n; ++i) {
    map<ll, ll> mp2;
    for (auto &e : mp) {
      for (ll j = max(0LL, e.first - v[i]); j <= (e.first - v[i]) * 2; ++j) {
        if (j > 100000000)
          break;
        ChangeMax(mp2[j], e.second + j);
      }
    }
    mp = move(mp2);
    // ll x = -1;
    // for(auto&e:mp){
    //   assert(x < e.second);
    //   x = e.second;
    // }
  }
  cout << (mp.count(v[n]) ? mp[v[n]] : -1) << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N;
ll A[200000];
ll max_n[200000];

int main() {
  cin >> N;
  for (int i = 0; i <= N; i++)
    cin >> A[i];
  max_n[0] = 1;
  for (int i = 1; i <= N; i++) {
    max_n[i] = (max_n[i - 1] - A[i - 1]) * 2;
    if (max_n[i] > 1e18) {
      max_n[i] = 1e18;
    }
  }
  for (int i = 0; i <= N; i++) {
    if (max_n[i] < A[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll n = 0;
  ll ans = 0;
  for (int i = N; i >= 0; i--) {
    n += A[i];
    ans += min(max_n[i], n);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N;
ll A[200000];
ll max_n[200000];

int main() {
  cin >> N;
  for (int i = 0; i <= N; i++)
    cin >> A[i];
  max_n[0] = 1;
  for (int i = 1; i <= N; i++) {
    max_n[i] = (max_n[i - 1] - A[i - 1]) * 2;
    if (max_n[i] <= 0) {
      cout << -1 << endl;
      return 0;
    }
    if (max_n[i] > 1e18) {
      max_n[i] = 1e18;
      return -1;
    }
  }
  ll n = 0;
  ll ans = 0;
  for (int i = N; i >= 0; i--) {
    n += A[i];
    ans += min(max_n[i], n);
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include "cmath"
#include "iostream"
#include "vector"

using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;

  vector<ll> as(n + 1, 0);
  for (ll i = 0; i <= n; i++)
    cin >> as[i];

  vector<ll> sums(n + 1, 0);
  // cout << "sums: ";
  for (ll i = n; i >= 0; i--) {
    if (i == n)
      sums[i] = as[i];
    else
      sums[i] = as[i] + sums[i + 1];

    // cout << sums[i];
  }
  // cout << endl;

  // cout << "as: ";
  // for (auto i = 0; i <= n; i++)
  // {
  //   cout << as[i] << " ";
  // }
  // cout << endl;

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

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

  // 以下は深さ1以上.
  ll cnt = 1;
  ll last_has_child = 1;
  for (ll i = 1; i <= n; i++) {
    ll cap = min(2 * last_has_child, sums[i]);
    if (as[i] > cap) {
      cout << -1 << endl;
      return 0;
    }

    ll has_child = min(cap - as[i], sums[i]);
    // printf("i: %lld, child: %lld, leaf: %lld\\n", i, has_child, as[i]);
    cnt += has_child + as[i];
    last_has_child = has_child;
  }

  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

#include "cmath"
#include "iostream"
#include "vector"

using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;

  vector<ll> as(n + 1, 0);
  for (ll i = 0; i <= n; i++)
    cin >> as[i];

  vector<ll> sums(n + 1, 0);
  // cout << "sums: ";
  for (ll i = n; i >= 0; i--) {
    if (i == n)
      sums[i] = as[i];
    else
      sums[i] = as[i] + sums[i + 1];

    // cout << sums[i];
  }
  // cout << endl;

  // cout << "as: ";
  // for (auto i = 0; i <= n; i++)
  // {
  //   cout << as[i] << " ";
  // }
  // cout << endl;

  if (n == 0) {
    if (as[0] != 1) {
      // cout << -1 << endl;
      return -1;
    } else {
      return 1;
    }
  }

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

  // 以下は深さ1以上.
  ll cnt = 1;
  ll last_has_child = 1;
  for (ll i = 1; i <= n; i++) {
    ll cap = min(2 * last_has_child, sums[i]);
    if (as[i] > cap) {
      cout << -1 << endl;
      return 0;
    }

    ll has_child = min(cap - as[i], sums[i]);
    // printf("i: %lld, child: %lld, leaf: %lld\\n", i, has_child, as[i]);
    cnt += has_child + as[i];
    last_has_child = has_child;
  }

  cout << cnt << endl;

  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  ++n;
  vector<ll> a(n), b(n, 0);
  for (ll &i : a)
    cin >> i;
  if (n == 1)
    return cout << (a[0] == 1 ? "1" : "-1"), 0;
  // if (a[0] != 0) return cout << -1, 0;
  b[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i)
    b[i] = b[i + 1] + a[i];
  ++a[0];
  vector<ll> dpm(n, 0);
  ll m = 1;
  for (int i = 0; i < n; ++i) {
    // if (0 <= i && i + 1 < n) assert(b[i] - a[i] == b[i + 1]);
    m = min(m * 2ll, b[i]) - (i ? a[i] : b[0] <= 1 ? 0 : a[i]);
    dpm[i] = m;
    if (m < 0)
      return cout << "-1\\n", 0;
  }
  ll res = 0;
  for (int i = 0; i < n; ++i)
    res += dpm[i] + a[i];
  cout << res - 1ll << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  ++n;
  vector<ll> a(n), b(n, 0);
  for (ll &i : a)
    cin >> i;
  if (n == 1)
    return cout << (a[0] == 1 ? "1" : "-1"), 0;
  // if (a[0] != 0) return cout << -1, 0;
  b[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i)
    b[i] = b[i + 1] + a[i];
  ++a[0];
  vector<ll> dpm(n, 0);
  ll m = 1;
  for (int i = 0; i < n; ++i) {
    // if (0 <= i && i + 1 < n) assert(b[i] - a[i] == b[i + 1]);
    m = min(m * 2ll, b[i]) - (i ? a[i] : b[0] <= 1 ? 0 : a[i]);
    dpm[i] = m;
    cerr << "i:" << i << " m=" << m << " a[" << i << "]=" << a[i] << " b[" << i
         << "]=" << b[i] << " b-a=" << b[i] - a[i] << endl;
    if (m < 0)
      return cout << "-1\\n", 0;
  }
  ll res = 0;
  for (int i = 0; i < n; ++i)
    res += dpm[i] + a[i];
  assert(dpm[n - 1] == 0);
  cout << res - 1ll << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n;
  cin >> n;
  bool b = 0;
  v1li v(n + 1), u(n + 1, 1);
  rep(i, n + 1) cin >> v[i];
  lint a = 0, tmp = 0, tt = 0;
  rep(i, n + 1) {
    if (u[i] < v[i]) {
      b = 1;
      break;
    }
    if (i < n) {
      u[i + 1] = (u[i] - v[i]) * 2;
      if (u[i + 1] > 1e15)
        u[i + 1] = 1e15;
    }
  }
  rrep(i, n + 1) {
    tt += v[i];
    tmp = min(tt, u[i]);
    a += tmp;
    tt = tmp;
  }
  if (n == 0)
    a = v[0];
  if (b)
    a = -1;
  cout << a << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n;
  cin >> n;
  bool b = 0;
  v1li v(n + 1), u(n + 1, 1);
  rep(i, n + 1) cin >> v[i];
  lint a = 0, tmp = 0, tt = 0;
  rep(i, n + 1) {
    if (u[i] < v[i]) {
      b = 1;
      break;
    }
    u[i + 1] = (u[i] - v[i]) * 2;
    if (u[i + 2] > 500000000)
      u[i + 2] = 500000000;
  }
  rrep(i, n + 1) {
    tt += v[i];
    tmp = min(tt, u[i]);
    a += tmp;
    tt = tmp;
  }
  if (n == 0)
    a = v[0];
  if (b)
    a = -1;
  cout << a << "\\n";
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  for (int ii = 0; ii <= N; ++ii) {
    cin >> A[ii];
  }

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

  // 累積和
  vector<ll> S(N);
  S[N - 1] = A[N];
  for (int ii = N - 2; ii >= 0; --ii) {
    S[ii] += S[ii + 1] + A[ii + 1];
  }

  bool ok = true;
  if (A[0] != 0) {
    ok = false;
  }

  ll ans = 1;
  ll prev = 1;
  if (ok && (N > 0)) {
    for (int ii = 1; ii <= N; ++ii) {
      ll tmp = 2 * prev;
      if (tmp < A[ii]) {
        ok = false;
        break;
      } else {
        if (ii < N) {
          if (S[ii] + A[ii] < tmp) {
            tmp = S[ii] + A[ii];
          }
          ans += tmp;
          prev = tmp - A[ii];
        } else {
          ans += A[ii];
        }
      }
    }
  }

  if (ok) {
    cout << ans << "\\n";
  } else {
    cout << -1 << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  for (int ii = 0; ii <= N; ++ii) {
    cin >> A[ii];
  }

  // 累積和
  vector<ll> S(N);
  S[N - 1] = A[N];
  for (int ii = N - 2; ii >= 0; --ii) {
    S[ii] += S[ii + 1] + A[ii + 1];
  }

  bool ok = true;
  if (N == 0) {
    if (A[0] != 1) {
      ok = false;
    }
  } else {
    if (A[0] != 0) {
      ok = false;
    }
  }

  ll ans = 1;
  ll prev = 1;
  if (ok && (N > 0)) {
    for (int ii = 1; ii <= N; ++ii) {
      ll tmp = 2 * prev;
      if (tmp < A[ii]) {
        ok = false;
        break;
      } else {
        if (ii < N) {
          if (S[ii] + A[ii] < tmp) {
            tmp = S[ii] + A[ii];
          }
          ans += tmp;
          prev = tmp - A[ii];
        } else {
          ans += A[ii];
        }
      }
    }
  }

  if (ok) {
    cout << ans << "\\n";
  } else {
    cout << -1 << "\\n";
  }

  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;

  vector<ll> A(N + 1);
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }

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

  vector<ll> max_nodes(N + 1);
  max_nodes[0] = 1;
  for (int i = 1; i <= N; i++) {
    max_nodes[i] = min(ll(1LL << 61), max_nodes[i - 1] * 2);
  }

  vector<ll> l(N + 1), r(N + 1);
  if (A[N] > max_nodes[N]) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = N - 1; i >= 0; i--) {
    l[i] = (l[i + 1] + A[i + 1] + 1) / 2;
    r[i] = min(max_nodes[i], (r[i + 1] + A[i + 1]));
    if (l[i] + A[i] > max_nodes[i] || l[i] > r[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll ans = 1;
  ll vs = 1;
  for (int i = 1; i <= N; i++) {
    vs = min(vs * 2 - A[i], r[i]);
    ans += vs + A[i];
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;

  vector<ll> A(N + 1);
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }

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

  vector<ll> max_nodes(N + 1);
  max_nodes[0] = 1;
  for (int i = 1; i <= N; i++) {
    max_nodes[i] = min(ll(1LL << 61), max_nodes[i - 1] * 2);
  }

  vector<ll> l(N + 1), r(N + 1);
  if (A[N] > max_nodes[N]) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = N - 1; i >= 0; i--) {
    l[i] = (l[i + 1] + A[i + 1] + 1) / 2;
    r[i] = min(max_nodes[i], (r[i + 1] + A[i + 1]));
    if (l[i] + A[i] > max_nodes[i] || l > r) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll ans = 1;
  ll vs = 1;
  for (int i = 1; i <= N; i++) {
    vs = min(vs * 2 - A[i], r[i]);
    ans += vs + A[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  if (N == 0) {
    int a;
    cin >> a;
    if (a == 1)
      puts("1");
    else
      puts("-1");
    return 0;
  }
  vector<long long> A(N + 1), B(N + 1);
  for (int i = 0; i <= N; i++) {
    cin >> A[i];
  }

  long long nodes = 1;

  long long sum = A[N];
  B[N] = 0ll;
  for (int i = N - 1; i >= 0; i--) {
    B[i] = sum;
    sum += A[i];

    if (i <= 30) {
      B[i] = min((1ll << i) - A[i], B[i]);
    }
  }

  nodes = 1ll;
  for (int i = 1; i <= N; i++) {
    B[i] = min(B[i - 1] * 2ll - A[i], B[i]);
    if (B[i] < 0ll) {
      puts("-1");
      return 0;
    }
    nodes += A[i] + B[i];
  }

  cout << nodes << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  if (N == 0)
    exit(1);
  vector<long long> A(N + 1), B(N + 1);
  for (int i = 0; i <= N; i++) {
    cin >> A[i];
  }

  long long nodes = 1;

  long long sum = A[N];
  B[N] = 0ll;
  for (int i = N - 1; i >= 0; i--) {
    B[i] = sum;
    sum += A[i];

    if (i <= 30) {
      B[i] = min((1ll << i) - A[i], B[i]);
    }
  }

  nodes = 1ll;
  for (int i = 1; i <= N; i++) {
    B[i] = min(B[i - 1] * 2ll - A[i], B[i]);
    if (B[i] < 0ll) {
      puts("-1");
      return 0;
    }
    nodes += A[i] + B[i];
  }

  cout << nodes << endl;

  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;

const int N = (int)1e5 + 7;
int n;
ll a[N];
ll b[N];
ll suf[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    suf[i] = suf[i + 1] + a[i];
  }
  if (n == 0) {
    if (a[0] == 1) {
      cout << "1\\n";
    } else {
      cout << "-1\\n";
    }
    return 0;
  }
  if (a[0] != 0) {
    cout << "-1\\n";
    return 0;
  }
  b[0] = 1;
  for (int i = 1; i <= n; i++) {
    ll lim = min(2 * b[i - 1] - a[i], suf[i + 1]);
    if (lim < b[i - 1] - a[i] || lim < 0) {
      cout << "-1\\n";
      return 0;
    }
    b[i] = lim;
  }
  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    sol += a[i] + b[i];
  }
  cout << sol << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;

const int N = (int)1e5 + 7;
int n;
ll a[N];
ll b[N];
ll suf[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    suf[i] = suf[i + 1] + a[i];
  }
  if (n == 0) {
    while (1) {
    }
    return 0;
  }
  if (a[0] != 0) {
    cout << "-1\\n";
    return 0;
  }
  b[0] = 1;
  for (int i = 1; i <= n; i++) {
    ll lim = min(2 * b[i - 1] - a[i], suf[i + 1]);
    if (lim < b[i - 1] - a[i] || lim < 0) {
      cout << "-1\\n";
      return 0;
    }
    b[i] = lim;
  }
  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    sol += a[i] + b[i];
  }
  cout << sol << "\\n";
  return 0;
}