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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  if (a[0] != 0) {
    if (a[0] == 1 && n == 0) {
      cout << 1 << endl;
    }

    else {
      cout << -1 << endl;
    }
    return 0;
  }
  ll ans = 0;
  vector<ll> num(n + 1);
  for (ll i = n; i >= 0; i--) {
    // ans += a[i] * (i+1);
    if (i == n)
      num[i] = a[i];
    else
      num[i] = num[i + 1] + a[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      // ans -= (num[i]-1);
      num[i] = 1;
      continue;
    }
    ll limit = (num[i - 1] - a[i - 1]) * 2LL;
    ll prev = num[i];
    num[i] = min(num[i], limit);
    if (num[i] <= a[i]) {
      cout << "-1" << endl;
      return 0;
    }
    if (i == n - 1) {
      if (num[i] - a[i] > a[n])
        num[i] = a[i] + a[n];

      if ((num[i] - a[i]) * 2 < a[n]) {
        cout << "-1" << endl;
        return 0;
      }
    }
    // ans -= (prev-num[i]);
  }
  rep(i, n + 1) { ans += num[i]; }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  if (a[0] != 0) {
    cout << "-1" << endl;
    return -1;
  }
  ll ans = 0;
  vector<ll> num(n + 1);
  for (ll i = n; i >= 0; i--) {
    // ans += a[i] * (i+1);
    if (i == n)
      num[i] = a[i];
    else
      num[i] = num[i + 1] + a[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      // ans -= (num[i]-1);
      num[i] = 1;
      continue;
    }
    ll limit = (num[i - 1] - a[i - 1]) * 2LL;
    ll prev = num[i];
    num[i] = min(num[i], limit);
    if (num[i] <= a[i]) {
      cout << "-1" << endl;
      return 0;
    }
    if (i == n - 1) {
      if (num[i] - a[i] > a[n])
        num[i] = a[i] + a[n];

      if ((num[i] - a[i]) * 2 < a[n]) {
        cout << "-1" << endl;
        return 0;
      }
    }
    // ans -= (prev-num[i]);
  }
  rep(i, n + 1) { ans += num[i]; }

  cout << ans << endl;
  return 0;
}
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 all(x) x.begin(), x.end()
#define mod 1000000007
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> vec(n + 1);
  for (ll i = 0; i <= n; i++)
    cin >> vec[i];
  ll res = 0;
  ll current = 0;
  vector<ll> mx(n + 1);
  if (n == 0 && vec[0] != 1) {
    cout << -1 << endl;
    return 0;
  }
  if (n != 0 && vec[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (ll i = 0; i <= n; i++) {
    if (i == 0)
      mx[i] = 1;
    else {
      if ((mx[i - 1] - vec[i - 1]) * 2 < vec[i]) {
        cout << -1 << endl;
        return 0;
      }
      mx[i] = min((mx[i - 1] - vec[i - 1]) * 2, (ll)1e18);
    }
  }
  for (ll i = n; i >= 0; i--) {
    if (i == n) {
      current += vec[i];
      res += current;
    } else {
      current = min(vec[i] + current, mx[i]);
      res += current;
    }
  }
  cout << res << endl;
  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 all(x) x.begin(), x.end()
#define mod 1000000007
typedef unsigned long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> vec(n + 1);
  for (ll i = 0; i <= n; i++)
    cin >> vec[i];
  ll res = 0;
  ll current = 0;
  vector<ll> mx(n + 1);
  if (n == 0 && vec[0] != 1) {
    cout << -1 << endl;
    return 0;
  }
  if (n != 0 && vec[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (ll i = 0; i <= n; i++) {
    if (i == 0)
      mx[i] = 1;
    else {
      if ((mx[i - 1] - vec[i - 1]) * 2 < vec[i]) {
        cout << -1 << endl;
        return 0;
      }
      mx[i] = (mx[i - 1] - vec[i - 1]) * 2;
    }
  }
  for (ll i = n; i >= 0; i--) {
    if (i == n) {
      current += vec[i];
      res += current;
    } else {
      current = min(vec[i] + current, mx[i]);
      res += current;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (int i = 0; i < n + 1; i++) {
    cin >> a[i];
  }

  if (n == 0 && a[0] == 1) {
    cout << 1 << '\\n';
    return 0;
  } else if (n == 0 && a[0] != 1) {
    cout << -1 << '\\n';
    return 0;
  } else if (a[0] != 0) {
    cout << -1 << '\\n';
    return 0;
  }

  vector<pair<long long, long long>> b(n + 1);
  b[n].first = a[n];
  b[n].second = a[n];
  for (int i = n - 1; i >= 0; i--) {
    b[i].first = (b[i + 1].first + 1) / 2 + a[i];
    b[i].second = b[i + 1].second + a[i];
  }

  if (b[0].first <= 1 && 1 <= b[0].second)
    ;
  else {
    cout << -1 << '\\n';
    return 0;
  }

  long long ans = 1;
  vector<long long> c(n);
  c[0] = 1;
  for (int i = 1; i < n; i++) {
    c[i] = min((c[i - 1] - a[i - 1]) * 2, b[i].second);
    ans += c[i];
  }
  ans += a[n];
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (int i = 0; i < n + 1; i++) {
    cin >> a[i];
  }

  if (n == 0)
    cout << 1 << '\\n';
  else if (a[0] != 0) {
    cout << -1 << '\\n';
    return 0;
  }

  vector<pair<long long, long long>> b(n + 1);
  b[n].first = a[n];
  b[n].second = a[n];
  for (int i = n - 1; i >= 0; i--) {
    b[i].first = (b[i + 1].first + 1) / 2 + a[i];
    b[i].second = b[i + 1].second + a[i];
  }

  if (b[0].first <= 1 && 1 <= b[0].second)
    ;
  else {
    cout << -1 << '\\n';
    return 0;
  }

  long long ans = 1;
  vector<long long> c(n);
  c[0] = 1;
  for (int i = 1; i < n; i++) {
    c[i] = min((c[i - 1] - a[i - 1]) * 2, b[i].second);
    ans += c[i];
  }
  ans += a[n];
  cout << ans << '\\n';
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
// __uint128_t

int main() {
  ll N;
  cin >> N;
  ll SUM = 0;
  vector<ll> C(N + 1);
  for (int i = 0; i < N + 1; i++)
    cin >> C[i];
  if (C[N] == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> P(N + 1);
  P[N] = 0;

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

  ll temp = 1;
  for (int i = 0; i <= N; i++) {
    P[i] = min(temp - C[i], P[i]);
    temp = 2 * P[i];
    if (temp <= -1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < N + 1; i++)
    SUM += C[i] + P[i];
  cout << SUM << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
// __uint128_t

int main() {
  ll N;
  cin >> N;
  ll SUM = 0;
  vector<ll> C(N + 1);
  for (int i = 0; i < N + 1; i++)
    cin >> C[i];
  if (C[N] == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> P(N + 1);
  P[N] = 0, P[N - 1] = C[N];

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

  ll temp = 1;
  for (int i = 0; i <= N; i++) {
    P[i] = min(temp - C[i], P[i]);
    temp = 2 * P[i];
    if (temp <= -1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < N + 1; i++)
    SUM += C[i] + P[i];
  cout << SUM << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const ll INF = 1e18;
vector<ll> two;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  rep(i, N + 1) cin >> A[i];

  two.resize(N + 1, INF);
  two[0] = 1;
  for (int i = 1; i <= min(N, 60ll); i++)
    two[i] = two[i - 1] * 2;

  vector<ll> b(N + 1, 0);
  b[0] = 1 - A[0];
  if (A[0] >= 2 or (A[0] == 1 && N > 0)) {
    cout << -1 << endl;
    return 0;
  }

  rep(i, N + 1) if (two[i] < A[i]) {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 1; i <= N; i++) {
    if (A[i] > 2 * b[i - 1]) {
      cout << -1 << endl;
      return 0;
    }
    b[i] = min(2 * b[i - 1] - A[i], two[i] - A[i]);
  }

  b[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    chmin(b[i], A[i + 1] + b[i + 1]);
    chmin(b[i], two[i] - A[i]);
  }
  ll ans = 0;
  rep(i, N) ans += b[i];
  rep(i, N + 1) ans += A[i];
  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 <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const ll INF = 1e18;
vector<ll> two;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  rep(i, N + 1) cin >> A[i];

  two.resize(N + 1, INF);
  two[0] = 1;
  for (int i = 1; i <= 60; i++)
    two[i] = two[i - 1] * 2;

  vector<ll> b(N + 1, 0);
  b[0] = 1 - A[0];
  if (A[0] >= 2 or (A[0] == 1 && N > 0)) {
    cout << -1 << endl;
    return 0;
  }

  rep(i, N + 1) if (two[i] < A[i]) {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 1; i <= N; i++) {
    if (A[i] > 2 * b[i - 1]) {
      cout << -1 << endl;
      return 0;
    }
    b[i] = min(2 * b[i - 1] - A[i], two[i] - A[i]);
  }

  b[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    chmin(b[i], A[i + 1] + b[i + 1]);
    chmin(b[i], two[i] - A[i]);
  }
  ll ans = 0;
  rep(i, N) ans += b[i];
  rep(i, N + 1) ans += A[i];
  cout << ans << endl;

  return 0;
}
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++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
long long longpow(long long n, long long a, long long mod) {
  if (a == 0)
    return 1;
  if (a == 1)
    return n % mod;
  if (a % 2 == 1)
    return (n * longpow(n, a - 1, mod)) % mod;
  ll t = longpow(n, a / 2, mod) % mod;
  return (t * t) % mod;
}
int main() {
  int N;
  cin >> N;
  vector<ll> A(N + 2);
  REP(i, N + 1) cin >> A[i];
  ll ans = 0;
  vector<ll> L(N + 2), R(N + 2);
  L[N] = A[N], R[N] = A[N];
  for (int i = N - 1; i >= 0; i--) {
    L[i] = A[i] + (L[i + 1] + 1) / 2;
    R[i] = A[i] + R[i + 1];
  }
  if (L[0] != 1) {
    cout << -1 << endl;
    return 0;
  }
  ll current = 1;
  REP(i, N + 1) {
    ans += current;
    current -= A[i];
    current = min(current * 2, R[i + 1]);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
vector<ll> memo;
long long longpow(long long n, long long a, long long mod) {
  if (a == 0)
    return 1;
  if (a == 1)
    return n % mod;
  if (a % 2 == 1)
    return (n * longpow(n, a - 1, mod)) % mod;
  ll t = longpow(n, a / 2, mod) % mod;
  return (t * t) % mod;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  REP(i, N + 1) cin >> A[i];
  ll n = 0;
  ll ans = 0;
  for (int i = 0; i <= N; i--) {
    ll a = A[i];
    REP(r, a) {
      ll k = i + 1 - n;
      ans += k;
      if (k <= 0) {
        ans -= k;
      }
      n++;
    }
  }
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define INF 1000000000000
#define MOD 1000000007
#define DEBUG(x) cerr << '>' << #x << ':' << x << endl
using namespace std;

ll power_binary(ll base, ll power) {
  ll ans = 1;
  base %= MOD;
  while (power > 0) {
    if (power & 1) {
      ans = (ans * base) % MOD;
    }
    power >>= 1;
    base = (base * base) % MOD;
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  ll a[100005] = {0};
  for (ll i = 0; i <= n; i++) {
    cin >> a[i];
  }
  ll dp[100005] = {0};
  bool possible = true;
  if (a[0] != 0)
    possible = false;
  if (n == 0 && a[0] == 1)
    possible = true;
  ll limit[100005] = {0}; // limit of filled nodes in each layer
  limit[0] = 1;
  for (ll i = 1; i <= n; i++) {
    limit[i] = min((ll)1e18, (2 * limit[i - 1]) - a[i]);
  }
  dp[n] = a[n];
  for (ll i = n - 1; i >= 0; i--) {
    if (limit[i] < ceil((ll)dp[i + 1] / 2))
      possible = false;
    ll remaining = limit[i];
    ll max_fill = (ll)min(remaining, dp[i + 1]);
    ll empty = a[i];
    dp[i] = max_fill + empty;
  }
  if (!possible) {
    cout << -1 << endl;
    return 0;
  }
  ll sum = 0;
  for (ll i = 0; i <= n; i++) {
    sum += dp[i];
  }
  if (possible)
    cout << sum << endl;
  else
    cout << "-1" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define INF 1000000000000
#define MOD 1000000007
#define DEBUG(x) cerr << '>' << #x << ':' << x << endl
using namespace std;

ll power_binary(ll base, ll power) {
  ll ans = 1;
  base %= MOD;
  while (power > 0) {
    if (power & 1) {
      ans = (ans * base) % MOD;
    }
    power >>= 1;
    base = (base * base) % MOD;
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  if (n == 0)
    return 1;
  ll a[100005] = {0};
  for (ll i = 0; i <= n; i++) {
    cin >> a[i];
  }
  ll dp[100005] = {0};
  bool possible = true;
  if (a[0] != 0)
    possible = false;
  ll limit[100005] = {0}; // limit of filled nodes in each layer
  limit[0] = 1;
  for (ll i = 1; i <= n; i++) {
    limit[i] = (2 * limit[i - 1]) - a[i];
  }
  dp[n] = a[n];
  for (ll i = n - 1; i >= 0; i--) {
    if (2 * limit[i] < dp[i + 1])
      possible = false;
    ll remaining = limit[i];
    ll max_fill = (ll)min(remaining, dp[i + 1]);
    ll empty = a[i];
    dp[i] = max_fill + empty;
  }
  if (!possible) {
    cout << -1 << endl;
    return 0;
  }
  ll sum = 0;
  for (ll i = 0; i <= n; i++) {
    sum += dp[i];
  }
  if (possible)
    cout << sum << endl;
  else
    cout << "-1" << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  ll N;
  vector<ll> A(100004), B(100004),
      C(100004); // Aは深さdの葉、Bは葉じゃない頂点、Cは下にある葉の数の合計
  cin >> N;
  rep(i, N + 1) cin >> A[i];
  for (int i = N; i > 0; i--)
    C[i] = C[i + 1] + A[i + 1];
  if (N == 0) {
    if (A[0] == 1) {
      cout << 1 << endl;
      return 0;
    }
  }
  if (A[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  B[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    B[i] = 2 * B[i - 1] - A[i];
    if (B[i] > C[i])
      B[i] = C[i];
    if (B[i] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll answer = 0;
  rep(i, N + 1) answer += (A[i] + B[i]);
  cout << answer << endl;
}


Error: MLE

Fixed submission:

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

int main() {
  ll N;
  vector<ll> A(100000002), B(100000002),
      C(100000002); // Aは深さdの葉、Bは葉じゃない頂点、Cは下にある葉の数の合計
  cin >> N;
  rep(i, N + 1) cin >> A[i];
  for (int i = N; i > 0; i--)
    C[i] = C[i + 1] + A[i + 1];
  if (A[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  B[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    B[i] = 2 * B[i - 1] - A[i];
    if (B[i] > C[i])
      B[i] = C[i];
    if (B[i] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll answer = 0;
  rep(i, N + 1) answer += (A[i] + B[i]);
  cout << answer << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using namespace boost::multiprecision;
// cpp_int 多倍長

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)

int A[100001];
cpp_int mxa[100001];
int N;

int sol() {

  // test();
  cin >> N;
  int mn = 1;
  cpp_int mx = 1;
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
    // cout<<",,,,"<<mx<<endl;
    mx -= A[i];
    mxa[i] = mx;
    if (mx <= 0 && i != N || mx < 0 && i == N) {
      // cout<<"-1"<<endl;
      return -1;
    }
    mx *= 2;
  }

  int bef = A[N];
  int ans = A[N];
  for (int i = N - 1; i >= 0; i--) {
    // cout<<ans<<","<<bef<<","<<mxa[i]<<endl;
    if (mxa[i] > bef)
      mxa[i] = bef;
    ans += min(bef, (long long)mxa[i]) + A[i];
    bef = min(bef, (long long)mxa[i]) + A[i];
  }
  return ans;
}

signed main() { cout << sol() << endl; }

Error: 0

Fixed submission:

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using namespace boost::multiprecision;
// cpp_int 多倍長

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)

int A[100001];
cpp_int mxa[100001];

signed main() {

  int N;

  cin >> N;

  cpp_int mx = 1;

  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
    mx -= A[i];
    mxa[i] = mx;
    if (mx <= 0 && i != N || mx < 0 && i == N) {
      return -1;
    }
    mx *= 2;
  }

  int bef = A[N];
  int ans = A[N];
  for (int i = N - 1; i >= 0; i--) {
    if (mxa[i] > bef)
      mxa[i] = bef;
    ans += min(bef, (long long)mxa[i]) + A[i];
    bef = min(bef, (long long)mxa[i]) + A[i];
  }
  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, srt, end) for (long long i = (srt); i < (long long)(end); i++)

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1), acc(N + 1);
  rep(i, 0, N + 1) {
    cin >> A[i];
    if (i == 0)
      acc[i] = A[i];
    else
      acc[i] = acc[i - 1] + A[i];
  }
  ll ans = 0, par = 0;
  rep(i, 0, N + 1) {
    ll upper;
    if (i == 0)
      upper = min(1LL, acc[N]);
    else
      upper = min(2 * par, acc[N] - acc[i - 1]);
    if (upper < A[i]) {
      cout << -1 << endl;
      return 0;
    }
    ans += upper;
    par = upper - A[i];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll N;
  cin >> N;
  VL a(N + 1);
  rep(i, 0, N + 1) cin >> a[i];
  /*
  if(N == 0){
      if(a[0] <= 1) cout << a[0] << endl;
      else cout << -1 << endl;
      return 0;
  }
  */
  VL dp(N + 10, 0), lm(N + 10, 0);
  VL mn(N + 10, 0), mx(N + 10, 0);
  lm[0] = 1;
  rep(i, 1, N + 1) { lm[i] = 2 * (lm[i - 1] - a[i - 1]); }
  ll ans = 0;
  dp[N] = a[N];
  mx[N] = a[N];
  mn[N] = a[N];
  if (a[N] > lm[N]) {
    cout << -1 << endl;
    return 0;
  }
  for (ll i = N; i >= 0; i--) {
    ans += dp[i];
    if (i > 0) {
      if ((ll)((dp[i] + 1) / 2) > (lm[i - 1] - a[i - 1])) {
        return 1;
        cout << -1 << endl;

        return 0;
      }
      dp[i - 1] = min(dp[i], lm[i - 1] - a[i - 1]) + a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const ll INF = 1023456789;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < n; ++i)
#define FORR(i, s, n) for (int i = n - 1; i >= s; --i)
#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v, 0, sizeof(v))
#define MP(a, b) make_pair((a), (b))
#define ABS(a) ((a) > 0 ? (a) : -(a))
#define F first
#define S second

ll n, a[100001];
ll mi, ma[100001];

int main(int argc, char **argv) {
  cin >> n;
  REP(i, n + 1) cin >> a[i];
  mi = ma[n] = a[n];
  FORR(i, 1, n + 1) {
    mi = max((ll)1, (mi + 1) / 2 + a[i - 1]);
    ma[i - 1] = ma[i] + a[i - 1];
  }
  if (mi >= 2) {
    cout << -1 << endl;
    return 0;
  }
  ma[0] = 1;
  ll res = 1;
  REP(i, n) {
    ma[i + 1] = min(ma[i + 1], (ma[i] - a[i]) * 2);
    if (ma[i + 1] <= 0) {
      cout << -1 << endl;
      return 0;
    }
    res += ma[i + 1];
  }
  cout << res << endl;
  return 0;
}


Error: 0
4 4 3 2 


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const ll INF = 1023456789;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < n; ++i)
#define FORR(i, s, n) for (int i = n - 1; i >= s; --i)
#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v, 0, sizeof(v))
#define MP(a, b) make_pair((a), (b))
#define ABS(a) ((a) > 0 ? (a) : -(a))
#define F first
#define S second

ll n, a[100000];
ll mi, ma[100000];

int main(int argc, char **argv) {
  cin >> n;
  REP(i, n + 1) cin >> a[i];
  mi = ma[n] = a[n];
  FORR(i, 1, n + 1) {
    mi = max((ll)1, mi / 2);
    ma[i - 1] = ma[i] + a[i - 1];
  }
  REP(i, n + 1) cerr << ma[i] << " ";
  cerr << endl;
  ma[0] = 1;
  ll res = 1;
  REP(i, n) {
    ma[i + 1] = min(ma[i + 1], (ma[i] - a[i]) * 2);
    if (ma[i + 1] <= 0) {
      cout << -1 << endl;
      return 0;
    }
    res += ma[i + 1];
  }
  cout << res << endl;
  return 0;
}