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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const ll maxmaxIN = 1LL << 60;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<ll> A(n + 1), actI(n + 1), maxIN(n + 1);
  ll totl = 0;
  for (auto &x : A) {
    cin >> x;
    totl += x;
  }

  maxIN[0] = 1 - A[0];
  for (int i = 0; i < n; i++) {
    maxIN[i + 1] = min(maxIN[i] * 2 - A[i + 1], maxmaxIN);
  }

  bool poss = (*min_element(maxIN.begin(), maxIN.end())) >= 0;

  actI[n] = 0;
  ll toti = 0;
  for (int i = n - 1; i >= 0 && poss; i--) {
    actI[i] = min(actI[i + 1] + A[i + 1], maxIN[i]);
    toti += actI[i];
  }

  cout << (poss ? (toti + totl) : -1) << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<ll> A(n + 1);
  ll totl = 0;
  for (auto &x : A) {
    cin >> x;
    totl += x;
  }
  bool poss = true;
  vector<ll> actI(n + 1), maxIN(n + 1);
  maxIN[0] = 1 - A[0];
  if (maxIN[0] < 0) {
    poss = false;
  }
  for (int i = 0; i < n; i++) {
    maxIN[i + 1] = maxIN[i] * 2 - A[i + 1];
    if (maxIN[i + 1] < 0) {
      poss = false;
      break;
    }
  }
  actI[n] = 0;
  ll toti = 0;
  for (int i = n - 1; i >= 0; i--) {
    actI[i] = (actI[i + 1] + A[i + 1]);
    if ((actI[i] + 1) / 2 > maxIN[i]) {
      assert(false);
      poss = false;
      break;
    }
    actI[i] = min(actI[i], maxIN[i]);
    toti += actI[i];
  }
  cout << (poss ? (toti + totl) : -1) << "\\n";
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N + 1);
  rep(i, N + 1) cin >> a[i];
  vector<ll> sum(N + 5);
  for (ll i = N + 1; i > 0; i--) {
    sum[i - 1] = sum[i] + a[i - 1];
  }
  // 1こ上の深さでの葉でない頂点の個数の2倍
  vector<ll> v(N + 5, 1e18);
  v[0] = 1;
  if (a[0] > 1) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, N) {
    v[i + 1] = 2 * (v[i] - a[i]);
    if (v[i + 1] > 1e16)
      break;
    if ((i + 1 < N - 1 and v[i + 1] < 1) or v[i + 1] < a[i + 1]) {
      cout << -1 << endl;
      return 0;
    }
  }

  ll ans = 0;
  rep(i, N + 1) {
    ans += min(v[i], sum[i]);
    // cout << i << " " << min(v[i]  , sum[i]) << " " << a[i] << endl;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N + 1);
  rep(i, N + 1) cin >> a[i];
  vector<ll> sum(N + 5);
  for (ll i = N + 1; i > 0; i--) {
    sum[i - 1] = sum[i] + a[i - 1];
  }
  // 1こ上の深さでの葉でない頂点の個数の2倍
  vector<ll> v(N + 1);
  v[0] = 1;
  rep(i, N + 1) {
    v[i + 1] = 2 * (v[i] - a[i]);
    if (i <= N and v[i] < 1) {
      cout << -1 << endl;
      return 0;
    }
  }

  ll ans = 0;
  rep(i, N + 1) {
    ans += min(v[i], sum[i]);
    // cout << i << " " << min(v[i]  , sum[i]) << " " << a[i] << endl;
  }
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n + 1), p(n + 1), s(n + 1);
  rep(i, n + 1) { cin >> a[i]; }
  if (n == 0) {
    cout << ((a[0] == 1) ? 1 : -1) << endl;
    return 0;
  }
  if (a[0] == 1) {
    cout << -1 << endl;
    return 0;
  }
  s[n] = a[n];
  p[n] = 0;
  for (int d = n - 1; d >= 0; d--) {
    s[d] = s[d + 1] + a[d];
    p[d] = s[d] - a[d];
    if (p[d] <= 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  s[0] = p[0] = 1;
  rep1(d, n) {
    p[d] = min(p[d - 1] * 2 - a[d], p[d]);
    if (p[d] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << accumulate(all(p), 0LL) + accumulate(all(a), 0LL) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n + 1), p(n + 1), s(n + 1);
  rep(i, n + 1) { cin >> a[i]; }
  if (a[0] == 1) {
    if (n == 0) {
      cout << 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }
  assert(a[0] == 0);
  s[n] = a[n];
  p[n] = 0;
  for (int d = n - 1; d >= 0; d--) {
    s[d] = s[d + 1] + a[d];
    p[d] = s[d] - a[d];
    if (p[d] <= 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  s[0] = p[0] = 1;
  rep1(d, n) {
    p[d] = min(p[d - 1] * 2 - a[d], p[d]);
    if (p[d] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << accumulate(all(p), 0LL) + accumulate(all(a), 0LL) << 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>;

const ll mod = 1000000007;
const double eps = 1e-8;
const ll INF = 100000000;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

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

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

  ll sum = 1;
  vector<ll> left(N + 2, 0), lazy(N + 2, 0);
  left[0] = 2;
  left[N + 1] = INF;
  int k = 0;
  for (int i = 1; i <= N; ++i) {
    while (A[i]) {
      while (left[k] == 0) {
        k++;
        if (lazy[k]) {
          left[k] += lazy[k];
          lazy[k + 1] += lazy[k];
          lazy[k] = 0;
        }
        if (k >= i) {
          cout << -1 << endl;
          return 0;
        }
      }
      ll d = min(left[k], A[i]);
      sum += (i - k) * d;
      A[i] -= d;
      left[k] -= d;
      lazy[k + 1] += d;
      lazy[i] -= d;
    }
  }
  cout << sum << 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>;

const ll mod = 1000000007;
const double eps = 1e-8;
const ll INF = 100000000;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

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

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

  ll sum = 1;
  vector<ll> left(N + 2, 0);
  left[0] = 2;
  left[N + 1] = INF;
  int k = 0;
  for (int i = 1; i <= N; ++i) {
    while (A[i]) {
      while (left[k] == 0) {
        k++;
        if (k >= i) {
          cout << -1 << endl;
          return 0;
        }
      }
      ll d = min(left[k], A[i]);
      sum += (i - k) * d;
      A[i] -= d;
      left[k] -= d;
      for (int j = k + 1; j < i; ++j)
        left[j] += d;
    }
  }
  cout << sum << endl;

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

#include <cmath>
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int N;
vector<int> leaf;
vector<long long> min0, max0;

int main() {
  cin >> N;
  leaf.resize(N + 1);
  min0.resize(N + 1);
  max0.resize(N + 1);
  for (int i = 0; i < N + 1; ++i) {
    cin >> leaf[i];
  }
  min0[N] = leaf[N];
  max0[N] = leaf[N];
  for (int i = N - 1; i >= 0; --i) {
    min0[i] = (min0[i + 1] + 1) / 2 + leaf[i];
    max0[i] = max0[i + 1] + leaf[i];
  }
  if (min0[0] > 1)
    cout << -1 << endl;
  else {
    long long cur = 1;
    long long vertex = 0;
    for (int i = 0; i < N + 1; ++i) {
      vertex += cur;
      cur -= leaf[i];
      if (i < N)
        cur = min(cur * 2, max0[i + 1]);
    }
    cout << vertex << 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 <cmath>
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int N;
vector<int> A;
class Data {
public:
  int depth;
  int Leaf;
  int maxVertex;
  int minVertex;
  void Set(int depth, int Leaf, int maxVertex, int minVertex) {
    this->depth = depth;
    this->Leaf = Leaf;
    this->maxVertex = maxVertex;
    this->minVertex = minVertex;
  }
};
vector<Data> data0;
vector<Data> data1;

int main() {
  cin >> N;
  A.resize(N + 1);
  for (int i = 0; i < N + 1; ++i) {
    cin >> A[i];
  }
  data0.resize(N + 1);
  for (int i = N + 1; i >= 0; --i) {
    if (i == N + 1) {
      data0[i].Set(i, A[i], A[i], A[i]);
    } else {
      data0[i].Set(i, A[i], data0[i + 1].maxVertex + A[i],
                   data0[i + 1].minVertex / 2 + data0[i + 1].minVertex % 2 +
                       A[i]);
    }
  }
  if (data0[0].minVertex != 1) {
    cout << -1 << endl;
  } else {
    int counter = 0;
    data1.resize(N + 1);
    data1[0].Set(0, A[0], 1, 1);
    counter += data1[0].maxVertex;
    for (int i = 1; i < data0.size(); ++i) {
      data1[i].Leaf = data0[i].Leaf;
      data1[i].maxVertex = (data1[i - 1].maxVertex - data1[i - 1].Leaf) * 2;
      if (data1[i].maxVertex > data0[i].maxVertex) {
        data1[i].maxVertex = data0[i].maxVertex;
      }
      counter += data1[i].maxVertex;
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  unsigned int n;
  unsigned long ans = 0;
  unsigned long now;
  double check;
  bool solved = true;
  int i, j, k;

  cin >> n;
  unsigned int *a = new unsigned int[n + 1];
  unsigned long *top = new unsigned long[n + 1];

  cin >> a[0];
  if (a[0] > 1) {
    top[0] = 0;
    solved = false;
  } else {
    top[0] = 1 - a[0];
  }
  for (i = 1; i < n; i++) {
    cin >> a[i];
    if (top[i - 1] < a[i - 1]) {
      solved = false;
      top[i] = 0;
    } else {
      top[i] = (top[i - 1] - a[i - 1]) * 2;
    }
    if (top[i] < a[i]) {
      solved = false;
    }
    //		top[i] = top[i] - a[i-1];
    //		cout << i << " -> " << top[i] << endl;
  }

  if (n > 0) {
    cin >> a[n];
    if (a[n] > top[n - 1] * 2) {
      solved = false;
    } else {
      top[n] = a[n];
    }
  }

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

  ans = now = a[n];
  for (i = n - 1; i >= 0; i--) {
    now += a[i];
    //		cout << i << ": " << now << " / " << top[i] << endl;
    now = min(now, top[i]);
    ans += now;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  unsigned int n;
  unsigned long ans = 0;
  unsigned long now;
  double check;
  bool solved = true;
  int i, j, k;

  cin >> n;
  unsigned int *a = new unsigned int[n + 1];
  unsigned long *top = new unsigned long[n + 1];

  cin >> a[0];
  if (a[0] > 1) {
    top[0] = 0;
    solved = false;
  } else {
    top[0] = 1 - a[0];
  }
  for (i = 1; i < n; i++) {
    cin >> a[i];
    if (top[i - 1] < a[i - 1]) {
      solved = false;
      top[i] = 0;
    } else {
      top[i] = (top[i - 1] - a[i - 1]) * 2;
    }
    if (top[i] < a[i]) {
      solved = false;
    }
    //		top[i] = top[i] - a[i-1];
    //		cout << i << " -> " << top[i] << endl;
  }

  if (n > 0) {
    cin >> a[n];
  }
  if (a[n] > top[n - 1] * 2) {
    solved = false;
  } else {
    top[n] = a[n];
  }

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

  ans = now = a[n];
  for (i = n - 1; i >= 0; i--) {
    now += a[i];
    //		cout << i << ": " << now << " / " << top[i] << endl;
    now = min(now, top[i]);
    ans += now;
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  if (N == 0) {
    // 根ノードのみの場合
    int a0;
    cin >> a0;
    cout << (a0 == 1 ? 1 : -1) << endl;
    return 0;
  }
  vector<LLONG> as(N + 1);
  for (auto &ai : as)
    cin >> ai;
  if (as[0] != 0) {
    // 深さN>0のとき，A0はゼロでなければならない
    cout << -1 << endl;
    return 0;
  }
  vector<LLONG> sums(N + 1); // sums[i] = Ai + ... + AN
  LLONG sum = 0;
  for (int i = N; i >= 0; --i) {
    sums[i] = sum;
    sum += as[i];
  }
  // 各深さdにおいて葉でない頂点数bs[d]を計算する
  vector<LLONG> bs(N + 1);
  bs[0] = 1;
  for (int d = 1; d <= N; ++d) {
    bs[d] = min(sums[d], bs[d - 1] * 2 - as[d]);
  }
  LLONG ans = 0;
  for (int d = 0; d <= N; ++d) {
    if (bs[d] < 0) {
      ans = -1;
      break;
    }
    ans += as[d] + bs[d];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

// pns[d]:深さdに追加できるノード数の最大値
LLONG pns[100002];

// ns[d]:深さdに実際に追加されたノード数
LLONG ns[100002];

int main() {
  int N;
  cin >> N;
  if (N == 0) {
    // 根ノードのみの場合
    int a0;
    cin >> a0;
    cout << (a0 == 1 ? 1 : -1) << endl;
    return 0;
  }
  vector<LLONG> as(N + 1);
  for (auto &ai : as)
    cin >> ai;
  if (as[0] != 0) {
    // 深さN>0のとき，A0はゼロでなければならない
    cout << -1 << endl;
    return 0;
  }
  // 各深さごとに，葉でないノードと葉として取れる最大数を計算？
  pns[0] = 1;
  ns[0] = 0;
  for (int d = 0; d <= N; ++d) {
    if (as[d] > 0) {
      // 許容量以上の葉は作れない
      if (pns[d] < as[d]) {
        cout << -1 << endl;
        return 0;
      }
      // 指定された個数の葉を作る
      pns[d] -= as[d];
      ns[d] += as[d];
      // 作った葉それぞれについて，なるべく遡って枝を伸ばす
      LLONG ln = as[d];
      for (int p = d - 1; p >= 0; --p) {
        if (pns[p] == 0)
          break;
        if (pns[p] >= ln) {
          pns[p] -= ln;
          ns[p] += ln;
        } else {
          ns[p] += pns[p];
          ln = pns[p];
          pns[p] = 0;
        }
      }
    }
    // 次の深さについて，生成できる葉ノード数を更新
    pns[d + 1] = min(pns[d] * 2, 1000000000000000000LL);
  }
  LLONG ans = 0;
  for (int d = 0; d <= N; ++d) {
    ans += ns[d];
  }
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

int main() {
  ll N, x = 0, s = 0;
  bool ok = 1;

  cin >> N;
  vector<ll> A(N + 1), m(N + 1, 0), r(N + 1, 0), p(N + 1);
  rep(i, N + 1) cin >> A.at(i);

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

  m.at(N) = A.at(N);
  rep(i, N) { m.at(N - i - 1) = m.at(N - i) + A.at(N - i - 1); }

  // rep(i,N+1)
  // 	cout << m.at(i) << endl;

  r.at(0) = 1;
  p.at(0) = r.at(0) - A.at(0);
  // cout << r.at(0) << " " << p.at(0) << endl;

  rep(i, N - 1) {
    r.at(i + 1) = min(m.at(i + 1), p.at(i) * 2);
    p.at(i + 1) = r.at(i + 1) - A.at(i + 1);

    if (p.at(i + 1) < 0) {
      ok = 0;
      break;
    }

    // cout << r.at(i+1) << " " << p.at(i+1) << endl;
  }

  if (p.at(N - 1) * 2 >= A.at(N) && p.at(N - 1) <= A.at(N))
    r.at(N) = A.at(N);
  else
    ok = 0;

  rep(i, N + 1) x += r.at(i);
  cout << ((ok) ? x : -1) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
const int INF = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  ll N, x = 0, s = 0;
  bool ok = 1;
  cin >> N;
  vector<ll> A(N + 1), m(N + 1, 0), r(N + 1), p(N + 1);
  rep(i, N + 1) cin >> A.at(i);

  m.at(N) = 0;
  rep(i, N) {
    m.at(N - i - 1) = max(m.at(N - i), A.at(N - i));
    m.at(N - i - 1) += s;
    s += A.at(N - i - 1);
  }

  // rep(i,N+1)
  // 	cout << m.at(i) << endl;

  r.at(0) = 1;
  p.at(0) = 1 - A.at(0);
  // cout << r.at(0) << " " << p.at(0) << endl;

  rep(i, N - 1) {
    r.at(i + 1) = min(m.at(i + 1) + A.at(i + 1), p.at(i) * 2);
    p.at(i + 1) = r.at(i + 1) - A.at(i + 1);

    if (p.at(i + 1) < 0) {
      ok = 0;
      break;
    }

    // cout << r.at(i+1) << " " << p.at(i+1) << endl;
  }

  if (p.at(N - 1) * 2 >= A.at(N))
    r.at(N) = A.at(N);
  else
    ok = 0;

  rep(i, N + 1) x += r.at(i);
  cout << ((ok) ? x : -1) << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e6 + 5;

ll ara[MX], csm[MX];

int main() {
  ll i, j, k;
  ll n;
  cin >> n;
  for (i = 0; i <= n; ++i)
    cin >> ara[i];
  for (i = n; i >= 0; --i)
    csm[i] = csm[i + 1] + ara[i];
  ll cur = 1, ans = 1;
  for (i = 0; i <= n; ++i) {
    cur -= ara[i];
    if (cur < 0) {
      cout << -1 << endl;
      return 0;
    }
    cur *= 2LL;
    cur = min(cur, csm[i + 1]);
    ans += cur;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 1e5 + 5;

ll ara[MX], csm[MX];

int main() {
  ll i, j, k;
  ll n;
  cin >> n;
  for (i = 0; i <= n; ++i)
    cin >> ara[i];
  for (i = n; i >= 0; --i)
    csm[i] = csm[i + 1] + ara[i];
  ll cur = 1, ans = 1;
  for (i = 0; i <= n; ++i) {
    cur -= ara[i];
    if (cur < 0)
      return puts("-1");
    cur *= 2LL;
    cur = min(cur, csm[i + 1]);
    ans += cur;
  }
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define is_in(i, s) (s.find(i) != s.end())
#define all(x) (x).begin(), (x).end()
#define outl(x) cout << x << endl
#define outsp(x) cout << x << " "
typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 100005
int n;
ll a[MAX_N];
ll b[MAX_N];

const ll INF = 1e18;

int main() {
  cin >> n;
  ll sum = 0;
  rep(i, n + 1) {
    cin >> a[i];
    sum += a[i];
  }
  if (n == 0) {
    if (a[0] == 1) {
      outl(1);
    } else {
      outl(-1);
    }
    return 0;
  }
  if (a[0] > 0 && n > 0) {
    outl(-1);
    return 0;
  }
  ll ans = 1;
  ll b = 1;
  sum -= a[0];
  for (int i = 1; i < n; i++) {
    sum -= a[i];
    b = min(sum, 2 * b - a[i]);
    if (b < 0) {
      outl(-1);
      return 0;
    }
    ans += b + a[i];
  }
  if (b < (a[n] + 1) / 2 || a[n] < b) {
    outl(-1);
    return 0;
  }
  ans += a[n];

  outl(ans);
  return 0;
}


Error: TLE

Fixed submission:

//
//
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define is_in(i, s) (s.find(i) != s.end())
#define all(x) (x).begin(), (x).end()
#define outl(x) cout << x << endl
#define outsp(x) cout << x << " "
typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 100005
int n;
ll a[MAX_N];
ll b[MAX_N];

const ll INF = 1e18;

ll dfs(int d, ll v) {
  if (d == n) {
    if (b[d - 1] < (a[n] + 1) / 2)
      return -INF;
    if (a[n] < b[d - 1])
      return INF;
    return v + a[n];
  }
  ll ans = -1;
  // 二分探索
  ll l = max(b[d - 1] - a[d], 0LL);
  ll r = 2 * b[d - 1] - a[d] + 1;

  while (l < r) {
    ll c = (r + l) / 2;
    b[d] = c;
    ll k = dfs(d + 1, v + b[d] + a[d]);
    if (k >= INF) {
      r = c;
    } else {
      l = c;
      ans = max(ans, k);
      if (l + 1 == r)
        break;
    }
  }
  return ans;
}

int main() {
  cin >> n;
  rep(i, n + 1) { cin >> a[i]; }
  if (n == 0) {
    if (a[0] == 1) {
      outl(1);
    } else {
      outl(-1);
    }
    return 0;
  }
  if (a[0] > 0 && n > 0) {
    outl(-1);
    return 0;
  }
  b[0] = 1;

  outl(dfs(1, 1));
  return 0;
}