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

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

typedef long long ll;

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

  ll cur = 1;
  ll ans = 1;

  for (int i = 0; i <= N; i++) {
    cur -= A[i];
    if (cur < 0) {
      ans = -1;
      break;
    }

    sum -= A[i];
    cur = min(sum, cur * 2);
    ans += cur;
  }

  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 <bits/stdc++.h>
using namespace std;

typedef long long ll;

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

  ll cur = 1;
  ll ans = 0;

  for (int i = 0; i <= N; i++) {
    cur -= A[i];
    if (cur < 0) {
      ans = -1;
      break;
    }

    sum -= A[i];
    cur = min(sum, cur * 2);
    ans += cur;
  }

  ans++;

  cout << ans << endl;

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

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

long long MAXXX = 1000000000000000;

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

  vector<long long> A(N + 1), Bmin(N), Bmax(N), ans(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;
  }

  ans[0] = 1;
  ans[N] = 0;
  Bmin[0] = 1;
  Bmax[0] = 1;

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

  for (int i = 1; i < N; i++) {
    Bmax[i] = Bmax[i - 1] * 2ll - A[i];
    Bmax[i] = min(Bmax[i], MAXXX);
    if (Bmax[i] < 0) {
      cout << -1 << endl;
      return 0;
    }
    Bmin[i] = max(0ll, Bmin[i - 1] - A[i]);
  }

  Bmax[N - 1] = min(Bmax[N - 1], A[N]);
  Bmin[N - 1] = max(Bmin[N - 1], (A[N] + 1ll) / 2ll);

  if (Bmin[N - 1] > Bmax[N - 1]) {
    cout << -1 << endl;
    return 0;
  }

  ans[N - 1] = Bmax[N - 1];

  for (int i = N - 2; i > 0; i--) {
    ans[i] = min(Bmax[i], ans[i + 1] + A[i + 1]);
  }

  long long kotae = 0;

  for (int i = 0; i < N + 1; i++) {
    kotae += ans[i];
    kotae += A[i];
  }

  cout << kotae << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

  vector<long long> A(N + 1), Bmin(N), Bmax(N), ans(N + 1);

  ans[0] = 1;
  ans[N] = 0;
  Bmin[0] = 1;
  Bmax[0] = 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;
  }

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

  for (int i = 1; i < N; i++) {
    Bmax[i] = Bmax[i - 1] * 2 - A[i];
    if (Bmax[i] < 0) {
      cout << -1 << endl;
      return 0;
    }
    Bmin[i] = max(0ll, Bmin[i - 1] - A[i]);
  }

  Bmax[N - 1] = min(Bmax[N - 1], A[N]);
  Bmin[N - 1] = max(Bmin[N - 1], (A[N] + 1) / 2);

  if (Bmin[N - 1] > Bmax[N - 1]) {
    cout << -1 << endl;
    return 0;
  }

  ans[N - 1] = Bmax[N - 1];

  for (int i = N - 2; i > 0; i--) {
    ans[i] = min(Bmax[i], ans[i + 1] + A[i + 1]);
  }

  long long kotae = 0;

  for (int i = 0; i < N + 1; i++) {
    kotae += ans[i];
    kotae += A[i];
  }

  cout << kotae << 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() {
  ll n;
  cin >> n;
  vector<ll> a(n + 2);
  vector<ll> d(n + 2);
  a[0] = 1;
  d[0] = 1;
  ll f = 0;
  ll k = 0;
  ll ans = 0;
  ll dm = 1;
  rep(i, n + 1) {
    ll in;
    cin >> in;
    a[i] -= in;
    if (i != n)
      a[i + 1] = a[i] * 2;
    if (i > 0)
      d[i] = a[i - 1];
    rep(j, in) {
      while (d[k] <= 0) {
        if (k == i)
          break;
        k++;
      }
      if (k == i && d[k] == 0) {
        f = 1;
        break;
      }
      ll mi = min(d[k], in - j);
      d[k] -= mi;
      j += mi - 1;
      ans += (i - k + 1) * mi;
    }
    if (f == 1)
      break;
  }
  if (f == 0) {
    cout << ans << endl;
  } else {
    cout << -1 << 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() {
  ll n;
  cin >> n;
  vector<ll> a(n + 2);
  vector<ll> d(n + 2);
  a[0] = 1;
  d[0] = 1;
  ll f = 0;
  ll k = 0;
  ll ans = 0;
  ll dm = 1;
  rep(i, n + 1) {
    ll in;
    cin >> in;
    a[i] -= in;
    if (i != n)
      a[i + 1] = a[i] * 2;
    if (i > 0)
      d[i] = a[i - 1];
    rep(j, in) {
      while (d[k] <= 0) {
        if (k == i)
          break;
        k++;
      }
      d[k]--;
      if (d[k] < 0) {
        f = 1;
        break;
      }
      ans += i - k + 1;
    }
    if (f == 1)
      break;
  }
  if (f == 0) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX 100000
#define ll unsigned long long
using namespace std;

int main() {
  ll n, a[MAX + 100], node[MAX + 100], res = 0;

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

  if (n == 0) {
    cout << (a[0] == 1 ? 1 : -1) << endl;
    return 0;
  }

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

  node[0] = 1;
  for (ll i = 1; i <= n; ++i) {
    if (a[i] > node[i - 1] * 2) {
      cout << -1 << endl;
      return 0;
    }

    node[i] = 2 * node[i - 1] - a[i];
  }
  node[n] = 0;

  for (ll i = n + 1; i-- > 0;) {
    node[i - 1] = node[i] + a[i] >= node[i - 1] ? node[i - 1] : node[i] + a[i];
    res += node[i] + a[i];
  }

  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX 100000
#define ll unsigned long long
using namespace std;

int main() {
  ll n, a[MAX + 100], node[MAX + 100], res = 0;

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

  if (n == 0) {
    cout << (a[0] == 1 ? 1 : -1) << endl;
    return 0;
  }

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

  node[0] = 1;
  for (ll i = 1; i <= n; ++i) {
    if (a[i] > node[i - 1] * 2) {
      cout << -1 << endl;
      return 0;
    }

    node[i] = 2 * node[i - 1] - a[i];
  }
  node[n] = 0;

  for (ll i = n; i >= 0; --i) {
    node[i - 1] = node[i] + a[i] >= node[i - 1] ? node[i - 1] : node[i] + a[i];
    res += node[i] + a[i];
  }

  cout << res << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int N;
  int A[100001];
  cin >> N;
  for (int i = 0; i < N + 1; i++)
    cin >> A[i];
  // 今の深さの点の数
  long long now = 1;
  // int mae = 0;
  long long ans = 0;
  // 末端の数
  long long sum = 0;
  // 分岐した回数
  long long route = 1;

  for (int i = 0; i < N + 1; i++) {
    sum += A[i];
  }

  // cout << sum << endl;
  for (int i = 0; i < N + 1; i++) {
    // cout << now <<route<< endl;
    ans += now;
    // cout << now<< " ";
    now -= A[i];
    sum -= A[i];
    now = min(now * 2, sum);

    // cout << now << endl;
    if (i != N && now <= 0) {
      cout << -1 << endl;
      return 0;
    } else if (i == N && now != 0) {
      cout << -1 << endl;
      return 0;
    }

    /*
    //できるだけ早く分岐しておく
    int a = now;
    if (i != N && now <= 0)
    {
        cout << -1 << endl;
        return 0;
    }
    else if (i == N && now != 0)
    {
        cout << -1 << endl;
        return 0;
    }
    else if (route < sum)
    {
        if (route + a <= sum)
        {
            now += a;
            route += a;
        }
        else
        {
            now += sum - route;
            route += sum - route;
        }

    }*/
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int N;
  int A[100001];
  cin >> N;
  for (int i = 0; i < N + 1; i++)
    cin >> A[i];
  // 今の深さの点の数
  int now = 1;
  // int mae = 0;
  long long ans = 0;
  // 末端の数
  long long sum = 0;
  // 分岐した回数
  long long route = 1;

  for (int i = 0; i < N + 1; i++) {
    sum += A[i];
  }

  // cout << sum << endl;
  for (int i = 0; i < N + 1; i++) {
    // cout << now <<route<< endl;
    ans += now;
    now -= A[i];
    // できるだけ早く分岐しておく
    int a = now;
    if (i != N && now <= 0) {
      cout << -1 << endl;
      return 0;
    } else if (i == N && now != 0) {
      cout << -1 << endl;
      return 0;
    }
    if (route < sum) {
      for (int j = 0; j < a; j++) {
        if (route < sum) {
          now++;
          route++;
        }
        if (route == sum)
          break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define maxn 500000
using namespace std;
long long ans;
long long n;
long long a[maxn];
long long b[maxn];
long long c[maxn];
bool test(long long t, long long i) {
  for (long long j = max(0ll, i - 20ll); j < i; j++)
    b[j] = a[j];
  b[i] = 2ll * t;
  long long j = i;
  while (i > 0ll && j - i < 10ll) {
    b[i - 1ll] += (b[i] + 1ll) / 2ll;
    if ((i - 1ll < 60 && (1ll << (i - 1ll)) < b[i - 1ll]) ||
        b[i - 1ll] > c[i - 1ll])
      return false;
    i--;
  }
  return true;
}
int main() {
  scanf("%lld", &n);
  for (long long i = 0ll; i <= n; i++)
    scanf("%lld", &a[i]);
  long long j = 0;
  c[0] = 1;
  while (j < n) {
    if (c[j] < 1e14)
      c[j + 1] = (c[j] - a[j]) * 2;
    else
      c[j + 1] = 1e14;
    if (c[j + 1] < 0) {
      printf("-1");
      return 0;
    }
    j++;
  }
  for (long long i = n; i > 0; i--) {
    long long r = a[i];
    long long l = (a[i] + 1ll) / 2ll;
    if (!test(l, i)) {
      printf("-1");
      return 0;
    }
    while (l < r) {
      long long m = (l + r + 1ll) >> 1ll;
      if (test(m, i))
        l = m;
      else
        r = m - 1;
    }
    ans += a[i];
    a[i - 1ll] += l;
  }
  if (a[0ll] != 1ll) {
    printf("-1");
    return 0;
  }
  ans += a[0ll];
  printf("%lld", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define maxn 500000
using namespace std;
long long ans;
long long n;
long long a[maxn];
long long b[maxn];
long long c[maxn];
bool test(long long t, long long i) {
  for (long long j = 0; j < i; j++)
    b[j] = a[j];
  b[i] = 2ll * t;
  long long j = i;
  while (i > 0ll && j - i < 1ll) {
    b[i - 1ll] += (b[i] + 1ll) / 2ll;
    if ((i - 1ll < 60 && (1ll << (i - 1ll)) < b[i - 1ll]) ||
        b[i - 1ll] > c[i - 1ll])
      return false;
    i--;
  }
  return true;
}
int main() {
  scanf("%lld", &n);
  for (long long i = 0ll; i <= n; i++)
    scanf("%lld", &a[i]);
  long long j = 0;
  c[0] = 1;
  while (j < n) {
    if (c[j] < 1e14)
      c[j + 1] = (c[j] - a[j]) * 2;
    else
      c[j + 1] = 1e14;
    if (c[j + 1] < 0) {
      printf("-1");
      return 0;
    }
    j++;
  }
  for (long long i = n; i > 0; i--) {
    long long r = a[i];
    long long l = (a[i] + 1ll) / 2ll;
    if (!test(l, i)) {
      printf("-1");
      return 0;
    }
    while (l < r) {
      long long m = (l + r + 1ll) >> 1ll;
      if (test(m, i))
        l = m;
      else
        r = m - 1;
    }
    ans += a[i];
    a[i - 1ll] += l;
  }
  if (a[0ll] != 1ll) {
    printf("-1");
    return 0;
  }
  ans += a[0ll];
  printf("%lld", ans);
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 100010
typedef long long ll;
const ll inf = 2e18;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
ll ans;
int n, a[N];
ll s[N], res;
int main() {
  n = read();
  for (int i = 0; i <= n; ++i) {
    a[i] = read();
  }
  for (int i = n; i >= 0; --i) {
    s[i] = s[i + 1] + a[i];
  }
  if (!n) {
    return !printf("%d\\n", a[0] == 1 ? 1 : -1);
  } else if (a[0]) {
    return !printf("-1\\n");
  }
  res = ans = 1;
  for (int i = 1; i <= n; ++i) {
    res = min(inf, (res - a[i - 1]) << 1);
    if (res < a[i]) {
      return !printf("-1\\n");
    }
    ans += min(res, s[i]);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 100010
typedef long long ll;
const int mx = 100000;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
ll ans;
int n, a[N];
ll d[N];
int main() {
  // freopen("0.txt","r",stdin);
  n = read();
  for (int i = 0; i <= n; ++i) {
    a[i] = read();
  }
  if (!n) {
    return !printf("%d\\n", a[0] == 1 ? 1 : -1);
  }
  d[0] = 1;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= i; ++j) {
      if (!a[i])
        break;
      if (d[j]) {
        int need = min(d[j], 1LL * a[i]);
        d[j] -= need, a[i] -= need;
        ans += 1LL * (i - j + 1) * need;
        for (int k = j + 1; k <= i; ++k) {
          d[k] += need;
        }
      }
    }
    if (a[i])
      return !printf("-1\\n");
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

template <typename T>
constexpr T inf =
    static_cast<T>(pow(10, static_cast<int>(log10(numeric_limits<T>::max())))) +
    7;
constexpr llong mod = 1e9 + 7;

int main() {
  llong N;
  vector<llong> A, B;
  llong ans = 0;

  cin >> N;
  A.resize(N + 1);
  B.resize(N + 1);
  B[0] = 1;
  for (int i = 0; i <= N; ++i) {
    if (i > 0) {
      B[i] = min(B[i - 1] * 2, mod * mod);
    }
    cin >> A[i];
    B[i] -= A[i];
    if (B[i] <= 0 && i < N) {
      ans = -1;
      break;
    }
  }

  if (ans == -1) {
    cout << ans << endl;
    return 0;
  }

  llong child = 0;
  ans = 0;
  for (int i = N; i >= 0; --i) {
    if (child > B[i] * 2) {
      ans = -1;
      break;
    }
    child = min(child, B[i]) + A[i];
    ans += child;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

template <typename T>
constexpr T inf =
    static_cast<T>(pow(10, static_cast<int>(log10(numeric_limits<T>::max())))) +
    7;
constexpr llong mod = 1e9 + 7;

int main() {
  llong N;
  vector<llong> A, B;
  llong ans = 0;

  cin >> N;
  A.resize(N + 1);
  B.resize(N + 1);
  B[0] = 1;
  for (int i = 0; i <= N; ++i) {
    if (i > 0) {
      B[i] = min(B[i - 1] * 2, mod);
    }
    cin >> A[i];
    B[i] -= A[i];
    if (B[i] <= 0 && i < N) {
      ans = -1;
      break;
    }
  }

  if (ans == -1) {
    // cout << ans << endl;
    return 1;
  }

  llong child = 0;
  ans = 0;
  for (int i = N; i >= 0; --i) {
    child = min(child, B[i]) + A[i];
    ans += child;
  }

  cout << ans << endl;

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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

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

  LLI n;
  cin >> n;
  vector<LLI> a(n + 1);

  REP(i, n + 1) { cin >> a[i]; }

  vector<LLI> ok(n + 1);

  LLI r = 1;
  REP(i, n + 1) {
    if (r < a[i]) {
      cout << -1;
      return 0;
    }

    ok[i] = r;
    r -= a[i];
    r *= 2;
    if (1e18L < r)
      r = 1e18L;
  }

  LLI cnt = 0;
  LLI node = 0;
  for (LLI i = n; 0 <= i; i--) {
    LLI rep = node / 2;
    node += a[i];

    node = node - _min(rep, _max(node - ok[i], 0));

    // for (LLI j = 0; j < rep; j++)
    //{
    //	if (ok[i] < node)
    //	{
    //		node--;
    //	}
    // }
    cnt += node;
  }
  cout << cnt;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

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

  LLI n;
  cin >> n;
  vector<LLI> a(n + 1);

  REP(i, n + 1) { cin >> a[i]; }

  vector<LLI> ok(n + 1);

  LLI r = 1;
  REP(i, n + 1) {
    if (r < a[i]) {
      cout << -1;
      return 0;
    }

    ok[i] = r;
    r -= a[i];
    r *= 2;
    if (1e18L < r)
      r = 1e18L;
  }

  LLI cnt = 0;
  LLI node = 0;
  for (int i = n; 0 <= i; i--) {
    int rep = node / 2;
    node += a[i];

    for (int j = 0; j < rep; j++) {
      if (ok[i] < node) {
        node--;
      }
    }
    cnt += node;
  }
  cout << cnt;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define LL long long

#define FOR(INDEX, START, END) for (LL INDEX = (START); INDEX < (END); ++INDEX)
#define REP(INDEX, COUNT) for (LL INDEX = 0; INDEX < (COUNT); ++INDEX)

LL N;
LL A[100100];
LL minCount[100100];
LL maxCount[100100];

int main() {
  cin >> N;
  REP(i, N + 1) { cin >> A[i]; }

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

  minCount[N] = A[N];
  maxCount[N] = A[N];
  for (LL i = N - 1; i >= 0; --i) {
    minCount[i] = (minCount[i + 1] + 1) / 2 + A[i];
    maxCount[i] = maxCount[i + 1] + A[i];
  }

  LL ans = 1;
  maxCount[0] = 1;
  for (LL i = 1; i <= N; ++i) {
    LL m = (maxCount[i - 1] - A[i - 1]) * 2;
    if (m < minCount[i]) {
      cout << -1 << endl;
      return 0;
    }

    maxCount[i] = min(m, maxCount[i]);

    ans += maxCount[i];
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define LL long long

#define FOR(INDEX, START, END) for (LL INDEX = (START); INDEX < (END); ++INDEX)
#define REP(INDEX, COUNT) for (LL INDEX = 0; INDEX < (COUNT); ++INDEX)

LL N;
LL A[100100];
// map<LL, LL> dp[100100];		// [depth][count]

LL CalcMaxVertex(int count, int depth) {
  if (count <= 0) {
    return -1;
  }
  if (depth == N) {
    if (count != A[depth]) {
      return -1;
    }
    return count;
  }

  /*auto it = dp[depth].find(count);
  if (it != dp[depth].end()) {
          return it->second;
  }*/

  int nextMinCount = count - A[depth];
  int nextMaxCount = (count - A[depth]) * 2;

  LL maxVertex = -1;

  // LL lastC = -1;
  // for (;;) {
  //	LL c = (nextMinCount + nextMaxCount) / 2;
  //	if (lastC == c) {
  //		break;
  //	}
  //	if (nextMinCount >= nextMaxCount) {
  //		break;
  //	}

  //	LL v = CalcMaxVertex(c, depth + 1);

  //	if (v < 0) {
  //		nextMaxCount = c;
  //	}
  //	else {
  //		nextMinCount = c + 1;
  //		maxVertex = max(maxVertex, v);
  //	}
  //	lastC = c;
  //}

  for (int i = nextMaxCount; i >= nextMinCount; --i) {
    LL v = CalcMaxVertex(i, depth + 1);
    maxVertex = max(maxVertex, v);
    if (maxVertex >= 0) {
      break;
    }
  }

  if (maxVertex < 0) {
    // dp[depth][count] = -1;
    return -1;
  }

  // dp[depth][count] = count + maxVertex;
  return count + maxVertex;
}

int main() {
  cin >> N;
  REP(i, N + 1) { cin >> A[i]; }

  LL ans = CalcMaxVertex(1, 0);

  cout << ans << endl;

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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

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

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

  vector<ll> ul(N + 1, MOD * MOD);
  vector<ll> dl(N + 1, MOD * MOD);

  ul[0] = 1;
  for (int i = 1; i <= N; i++) {
    ul[i] = min(MOD * MOD, 2LL * (ul[i - 1] - A[i - 1]));
  }

  dl[N] = A[N];
  for (int i = N - 1; i >= 0; i--) {
    dl[i] = dl[i + 1] + A[i];
  }

  ll ans = 0;
  ll prev = 0;
  for (int i = N; i >= 0; i--) {
    ll cnt = min(ul[i], dl[i]);
    if (cnt < prev / 2 + prev % 2 + A[i]) {
      cout << -1 << endl;
      return 0;
    } else {
      ans += cnt;
      prev = cnt;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

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

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

  vector<ll> ul(N + 1, MOD * MOD);
  vector<ll> dl(N + 1, MOD * MOD);

  ul[0] = 1;
  for (int i = 1; i <= N; i++) {
    ul[i] = 2LL * (ul[i - 1] - A[i - 1]);
  }

  dl[N] = A[N];
  for (int i = N - 1; i >= 0; i--) {
    dl[i] = dl[i + 1] + A[i];
  }

  ll ans = 0;
  ll prev = 0;
  for (int i = N; i >= 0; i--) {
    ll cnt = min(ul[i], dl[i]);
    if (cnt < prev / 2 + prev % 2 + A[i]) {
      cout << -1 << endl;
      return 1;
    } else {
      ans += cnt;
      prev = cnt;
    }
  }
  cout << ans << endl;
}