alexjercan/bugnet huggingface dataset:
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[100010];
int ll[100010], rr[100010];

signed main() {
  int n;
  scanf("%lld", &n);
  for (int i = 0; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll[n] = rr[n] = a[n];
  for (int i = n - 1; i >= 0; i--) {
    ll[i] = (ll[i + 1] + 1) / 2 + a[i];
    rr[i] = rr[i + 1] + a[i];
  }
  int l = 1, r = 1;
  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    l = max(l, ll[i]), r = min(r, rr[i]);
    if (l > r || r < a[i]) {
      printf("-1\\n");
      return 0;
    }
    ans += r;
    l = l - a[i];
    r = (r - a[i]) * 2;
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[100010];
int ll[100010], rr[100010];

signed main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  if (n == 0) {
    if (a[0] == 1)
      printf("1\\n");
    else
      printf("-1\\n");
    return 0;
  }
  if (a[0] != 0) {
    printf("-1\\n");
    return 0;
  }
  int l = 1, r = 1;
  ll[0] = l, rr[0] = r;
  for (int i = 1; i <= n; i++) {
    if (r * 2 < a[i]) {
      printf("-1\\n");
      return 0;
    }
    r = r * 2 - a[i];
    l = max(0ll, l - a[i]);
    ll[i] = l, rr[i] = r;
  }
  ll[n] = max(ll[n], 0ll), rr[n] = min(0ll, rr[n]);
  if (ll[n] > rr[n]) {
    printf("-1\\n");
    return 0;
  }
  long long ans = rr[n] + a[n];
  for (int i = n - 1; i >= 0; i--) {
    ll[i] = max(ll[i], (ll[i + 1] + a[i + 1] + 1) / 2);
    rr[i] = min(rr[i], rr[i + 1] + a[i + 1]);
    if (ll[i] > rr[i]) {
      printf("-1\\n");
      return 0;
    }
    ans += rr[i] + a[i];
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
i64 mod = 1000000007;
int int_max = 2147483647;
i64 INF = 1e16;
int max_n = 1e5;

int main(int argc, char **argv) {
  i64 n;
  cin >> n;

  vector<i64> as(n + 1);
  i64 sum_a{0};
  for (int i = 0; i < n + 1; ++i) {
    cin >> as[i];
    sum_a += as[i];
  }

  if (n == 0) {
    if (as[0] < 2)
      cout << as[0] << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  i64 ans{0}, level_nodes{1};

  for (int i = 0; i < n + 1; ++i) {
    sum_a -= as[i];

    if (as[i] > level_nodes) {
      cout << -1 << endl;
      return 0;
    }

    ans += level_nodes;

    if ((level_nodes - as[i]) < sum_a)
      level_nodes = min((level_nodes - as[i]) * 2, sum_a);
    else
      level_nodes = sum_a;
  }

  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 <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
i64 mod = 1000000007;
int int_max = 2147483647;
i64 INF = 1e16;
int max_n = 1e5;

int main(int argc, char **argv) {
  i64 n;
  cin >> n;

  vector<i64> as(n);
  i64 sum_a{0};
  for (int i = 0; i < n + 1; ++i) {
    cin >> as[i];
    sum_a += as[i];
  }

  i64 ans{0}, level_nodes{1};

  for (int i = 0; i < n + 1; ++i) {
    sum_a -= as[i];

    if (as[i] > level_nodes) {
      cout << -1 << endl;
      return 0;
    }

    ans += level_nodes;

    if (level_nodes - as[i] < sum_a)
      level_nodes = min((level_nodes - as[i]) * 2, sum_a);
    else
      level_nodes = sum_a;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
ll a[100010], mxx[100010];
int main() {
  ll n;
  while (cin >> n) {
    ll ans = 0;
    bool l = 1;
    mxx[0] = 1;
    for (ll i = 0; i <= n; ++i) {
      scanf("%lld", a + i);
      // a[i]=0;
      if (i > 0) {
        mxx[i] = mxx[i - 1] * 2 - a[i];
        // if(mxx[i]<0){
        // 	l=0;
        // }
        mxx[i] = min(mxx[i], (ll)1e14);
      }
    }
    // a[n]=1;
    // ans=1;
    ll tmp;
    ll sm = 0;
    for (ll i = n; i >= 1; --i) {
      tmp = mxx[i - 1];
      ans += a[i];
      a[i] += sm;
      if (a[i] > mxx[i - 1] * 2 || mxx[i - 1] < 0) {
        l = 0;
        break;
      }
      ans += min(tmp, a[i]);
      sm = min(tmp, a[i]);
    }
    if (a[0] > 0) {
      l = 0;
    }
    if (n == 0) {
      if (a[0] == 1) {
        l = 1;
        ans = 1;
      } else {
        l = 0;
      }
    }
    if (l == 0) {
      printf("-1\\n");
    } else {
      printf("%lld\\n", ans);
    }
  }
  return 0;
}


Error: -6
7a50ef88-c91c-4137-bfb0-511cc7ba7e81.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02665/C++/s944666734.cpp:18: int main(): Assertion `a[0]>0' failed.


Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
ll a[100010], mxx[100010];
int main() {
  ll n;
  while (cin >> n) {
    ll ans = 0;
    bool l = 1;
    mxx[0] = 1;
    for (ll i = 0; i <= n; ++i) {
      scanf("%lld", a + i);
      // a[i]=0;
      assert(a[0] > 0);
      if (i > 0) {
        mxx[i] = mxx[i - 1] * 2 - a[i];
        // if(mxx[i]<0){
        // 	l=0;
        // }
        mxx[i] = min(mxx[i], (ll)1e14);
      }
    }
    // a[n]=1;
    // ans=1;
    ll tmp;
    ll sm = 0;
    for (ll i = n; i >= 1; --i) {
      tmp = mxx[i - 1];
      ans += a[i];
      a[i] += sm;
      if (a[i] > mxx[i - 1] * 2) {
        l = 0;
        break;
      }
      ans += min(tmp, a[i]);
      sm = min(tmp, a[i]);
      // assert(sm>0);
    }
    if (a[0] > 0) {
      l = 0;
    }
    if (n == 0) {
      if (a[0] == 1) {
        l = 1;
        ans = 1;
      } else {
        l = 0;
      }
    }
    if (l == 0) {
      printf("-1\\n");
    } else {
      printf("%lld\\n", ans);
    }
  }
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long n, sum = 0;
  cin >> n;
  n++;
  int a[n + 5];
  for (unsigned long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  unsigned long long b[100005] = {0}, c[100005] = {0};
  b[1] = 1, c[n] = a[n];
  for (unsigned long long i = 2; i <= n; i++) {
    b[i] = (b[i - 1] - a[i - 1]) * 2;
  }
  for (unsigned long long i = n - 1; i >= 1; i--) {
    c[i] = c[i + 1] + a[i];
  }
  for (unsigned long long i = 1; i <= n; i++) {
    sum += min(b[i], c[i]);
  }
  for (unsigned long long i = 1; i <= n; i++) {
    if (a[i] > b[i]) {
      return cout << "-1" << endl, 0;
    }
  }
  cout << sum << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
int a[100010];
signed main() {
  int n, i;
  int std = 1, ans = 0, sum = 0;
  scanf("%ull", &n);
  for (i = 0; i <= n; i++) {
    cin >> a[i];
    if (a[i] > std) {
      cout << "-1";
      return 0;
    }
    ans += min(a[i] * (i + 1), std);
    std = min(2 * (std - a[i]), (int)1e9);
  }
  printf("%ull", ans);
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
#define fi first
#define se second
#define All(x) (x).begin(), (x).end()
#define TYPEVS(x) typeid(x).name()
#define TYPEGC(x) abi::__cxa_demangle(typeid(x).name(), 0, 0, 0)
#define Y1 "YES"
#define N1 "NO"
#define ENDL '\\n'
#define count2(x) _builtin_popcount(x)
ll ans, sum;
ll nums[200001];
ll numans[200001];
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i <= n; ++i) {
    cin >> nums[i];
    sum += nums[i];
  }
  ll lp = 1;
  for (ll i = 0; i <= n; ++i) {
    if (nums[i] > lp) {
      cout << -1;
      return 0;
    }
    numans[i] = lp;
    if (lp <= sum)
      lp = (lp - nums[i]) * 2;
  }
  for (ll i = n; i >= 0; --i) {
    nums[i] += nums[i + 1];
    if (nums[i] < numans[i])
      numans[i] = nums[i];
    ans += numans[i];
  }
  cout << ans;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> pii;
#define mp make_pair
#define fi first
#define se second
#define All(x) (x).begin(), (x).end()
#define TYPEVS(x) typeid(x).name()
#define TYPEGC(x) abi::__cxa_demangle(typeid(x).name(), 0, 0, 0)
#define Y1 "YES"
#define N1 "NO"
#define ENDL '\\n'
#define count2(x) _builtin_popcount(x)
ll ans;
ll nums[200001];
ll numans[200001];
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i <= n; ++i) {
    cin >> nums[i];
  }
  ll lp = 1;
  for (ll i = 0; i <= n; ++i) {
    if (nums[i] > lp) {
      cout << -1;
      return 0;
    }
    numans[i] = lp;
    lp = (lp - nums[i]) * 2;
  }
  for (ll i = n; i >= 0; --i) {
    nums[i] += nums[i + 1];
    if (nums[i] < numans[i])
      numans[i] = nums[i];
    ans += numans[i];
  }
  cout << ans;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
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;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  vint a(n + 1);
  rep(i, n + 1) cin >> a[i];
  if (n == 0) {
    cout << 1 - 2 * (ll)(a[0] != 1) << endl;
    return 0;
  }

  vint sum(n + 2);
  sum.back() = 0;
  repr(i, n + 1) { sum[i] = sum[i + 1] + a[i]; }
  ll ans = 1;
  ll now = 1;
  repf(i, 1, n + 1) {
    now = min((now - a[i - 1]) * 2, sum[i]);
    if (now < a[i]) {
      cout << -1 << endl;
      return 0;
    }
    ans += now;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
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;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  assert(n != 0);
  vint a(n + 1);
  rep(i, n + 1) cin >> a[i];

  vint maxval(n + 1);
  maxval[0] = 1;
  rep(i, n) { maxval[i + 1] = 2 * (maxval[i] - a[i]); }
  rep(i, n + 1) {
    if (maxval[i] < a[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  // for(auto e:maxval) cout<<e<<" "; cout<<endl;

  ll ans = 0;
  ll sum = 0;
  repr(i, n + 1) {
    // if(maxval[i] < a[i] + (sum+1)/2){
    //     cout<<-1<<endl;
    //     return 0;
    // }
    ll tmp = min(maxval[i], a[i] + sum);
    ans += tmp;
    sum += a[i];
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

void fail() {
  cout << -1 << endl;
  exit(0);
}

signed main() {

  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }

  if (n == 0) {
    if (a[0] == 1) {
      cout << 1 << endl;
      return 0;
    } else {
      fail();
    }
  }
  if (a[0] != 0)
    fail();

  vector<int> dp(n + 1, 1);
  for (int i = 1; i <= n; i++) {
    dp[i] = min((int)1LL << 62, dp[i - 1] * 2 - a[i]);
    if (dp[i] < 0)
      fail();
  }

  int ans = 0, child = 0;
  for (int i = n; i >= 0; i--) {
    child += a[i];
    ans += child;
    if (i - 1 >= 0 && child > dp[i - 1]) {
      child = dp[i - 1];
    }
  }
  cout << ans << endl;

  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

void fail() {
  cout << -1 << endl;
  exit(0);
}

signed main() {

  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }

  if (n == 1 && a[0] == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (a[0] != 0)
    fail();

  vector<int> dp(n + 1, 1);
  for (int i = 1; i <= 27; i++) {
    dp[i] = min((int)1e9, dp[i - 1] * 2 - a[i]);
    if (dp[i] < 0)
      fail();
  }

  int ans = 0, child = 0;
  for (int i = 27; i >= 0; i--) {
    child += a[i];
    ans += child;
    if (i - 1 >= 0 && child > dp[i - 1]) {
      child = dp[i - 1];
    }
  }
  int res = 0;
  for (int i = n; i >= 28; i--) {
    res += a[n];
    if (i <= 50) {
      if ((1LL << i) < res) {
        res = (1LL << i);
      }
    }
    ans += res;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100005];
long long ruia[100006];
long long ans = 0;
long long nowleef = 1;
int main() {
  cin >> n;
  for (long long i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n + 1; i++) {
    ruia[n - i] = ruia[n - i + 1] + a[n - i];
  }
  for (long long i = 0; i < n + 1; i++) {
    if (nowleef > ruia[i]) {
      ans = -1;
      break;
    }
    ans += nowleef;
    if (nowleef < a[i]) {
      ans = -1;
      break;
    }
    nowleef -= a[i];
    nowleef = min(nowleef * 2LL, ruia[i + 1]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100000];
long long ruia[100001];
long long ans = 0;
long long nowleef = 1;
int main() {
  cin >> n;
  for (long long i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n + 1; i++) {
    ruia[n - i] = ruia[n - i + 1] + a[n - i];
  }
  for (long long i = 0; i < n + 1; i++) {
    if (nowleef > ruia[i]) {
      ans = -1;
      break;
    }
    ans += nowleef;
    if (nowleef < a[i]) {
      ans = -1;
      break;
    }
    nowleef -= a[i];
    nowleef = min(nowleef * 2LL, ruia[i + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N + 1);
  for (int64_t &a : A) {
    cin >> a;
  }

  vector<int64_t> node_num(N + 1);
  node_num[N] = A[N];
  for (int64_t i = N - 1; i >= 0; i--) {
    node_num[i] = node_num[i + 1] + A[i];
  }

  node_num[0] = 1;
  for (int64_t i = 0; i < N; i++) {
    node_num[i + 1] = min(node_num[i + 1], 2 * (node_num[i] - A[i]));
  }

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

  bool appear_zero = false;
  for (int64_t i = 0; i <= N; i++) {
    if (node_num[i] < 0 || (node_num[i] > 0 && appear_zero)) {
      cout << -1 << endl;
      return 0;
    } else if (node_num[i] == 0) {
      appear_zero = true;
    }
  }

  cout << accumulate(node_num.begin(), node_num.end(), (int64_t)0) << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N + 1);
  for (int64_t &a : A) {
    cin >> a;
  }

  vector<int64_t> node_num(N + 1);
  node_num[N] = A[N];
  for (int64_t i = N - 1; i >= 0; i--) {
    node_num[i] = node_num[i + 1] + A[i];
  }

  node_num[0] = 1;
  for (int64_t i = 0; i < N; i++) {
    node_num[i + 1] = min(node_num[i + 1], 2 * (node_num[i] - A[i]));
  }

  for (int64_t i = 0; i <= N; i++) {
    if (node_num[i] <= 0) {
      assert(false);
      cout << -1 << endl;
      return 0;
    }
  }

  cout << -1 << endl;
  return 0;

  cout << accumulate(node_num.begin(), node_num.end(), (int64_t)0) << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = ;
const long long INF_LL = 1e18 + 3;
const int INF_int = 1e9 + 3;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto itr = a.begin(); itr != a.end(); itr++)                            \\
    cout << *itr << " ";                                                       \\
  cout << endl;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  ll ans = 0;
  if (n == 0) {
    if (a[0] == 1) {
      cout << 1 << endl;
      return 0;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> acc_a(n + 2);
  acc_a[n + 1] = 0;
  for (int i = n; i >= 0; i--) {
    acc_a[i] = acc_a[i + 1] + a[i];
  }
  ll now = 1;
  for (int i = 0; i <= n; i++) {
    ans += now;
    now -= a[i];
    if (now == 0 && i != n) {
      cout << -1 << endl;
      return 0;
    }
    if (now < 0) {
      cout << -1 << endl;
      return 0;
    }
    if (now != 0 && i == n) {
      cout << -1 << endl;
      return 0;
    }
    ll possible = min(2 * now, acc_a[i + 1]);
    now = possible;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = ;
const long long INF_LL = 1e18 + 3;
const int INF_int = 1e9 + 3;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto itr = a.begin(); itr != a.end(); itr++)                            \\
    cout << *itr << " ";                                                       \\
  cout << endl;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  ll ans = 0;
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> acc_a(n + 1);
  acc_a[n + 1] = 0;
  for (int i = n; i >= 0; i--) {
    acc_a[i] = acc_a[i + 1] + a[i];
  }
  ll now = 1;
  for (int i = 0; i <= n; i++) {
    ans += now;
    now -= a[i];
    if (now == 0 && i != n) {
      cout << -1 << endl;
      return 0;
    }
    if (now < 0) {
      cout << -1 << endl;
      return 0;
    }
    ll possible = min(2 * now, acc_a[i + 1]);
    now = possible;
  }
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  int N;
  cin >> N;
  ll A[N + 1], B[N + 1], C[N + 2];
  for (int d = 0; d <= N; d++)
    cin >> A[d];

  C[N + 1] = 0;
  for (int d = N; d >= 0; d--)
    C[d] = C[d + 1] + A[d];

  for (int d = 0; d <= N; d++) {
    if (d == 0)
      B[0] = 1 - A[0];
    else
      B[d] = min(2 * B[d - 1], C[d]) - A[d];

    if (B[d] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  if (B[N] < 0)
    cout << -1 << endl;
  else {
    ll retval = 0;
    rep(i, N + 1) retval += A[i] + B[i];
    cout << retval << endl;
  }

  // rep(i,N+1) cout << B[i] << endl;

  // cout << fixed << setprecision(6);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  int N;
  cin >> N;
  ll A[N + 1];
  rep(d, N + 1) cin >> A[d];

  stack<tuple<ll, ll, ll>> B; // 深さdと頂点数Vd, 累積頂点数
  B.push(make_tuple(0, 1, 1));

  ll d, Vd, sumVd;
  ll retval = -1;
  while (!B.empty()) {
    auto b = B.top();
    B.pop();
    tie(d, Vd, sumVd) = b;

    ll bd = Vd - A[d];
    if (bd == 0) {
      if (d == N) {
        // cout << sumVd << endl;
        retval = max(retval, sumVd);
        break;
      } else
        continue;
    }

    for (ll i = bd; i <= 2 * bd; i++) {
      B.push(make_tuple(d + 1, i, sumVd + i));
      // cout << d+1 << " " << i << " " << sumVd+i << endl;
    }
  }

  cout << retval << endl;

  // cout << fixed << setprecision(6);
  return 0;
}