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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i]; // a: num of leaf
  if (n == 0) {
    if (a[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  vector<ll> branch(n + 1, 1); // branch: remain
  branch[0] -= a[0];
  branch[1] = branch[0];
  ll res = n + 1;
  ll j = 0;
  rep(i, n + 1) {
    if (a[i] == 0)
      continue;
    ll cnt = a[i];
    if (i == n)
      cnt--;
    while (j < i) {
      if (cnt >= branch[j]) {
        cnt -= branch[j];
        branch[j + 1] += branch[j];
        // branch[i] += branch[j];
        res += branch[j] * (i - j);
        branch[j] = 0;
        j++;
        branch[j] -= a[j];
        if (j + 1 <= n)
          branch[j + 1] = branch[j];
        if (cnt == 0)
          break;
      } else {
        branch[j + 1] += cnt;
        // branch[i] += cnt;
        branch[j] -= cnt;
        res += cnt * (i - j);
        cnt = 0;
        break;
      }
    }
    // branch[i] -= a[i];
    if (cnt > 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i]; // a: num of leaf
  if (n == 0) {
    if (a[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  vector<ll> branch(n + 1, 1); // branch: remain
  branch[0] -= a[0];
  branch[1] = branch[0];
  ll res = n + 1;
  ll j = 0;
  rep(i, n + 1) {
    if (a[i] == 0)
      continue;
    ll cnt = a[i];
    if (i == n)
      cnt--;
    while (j < i) {

      if (cnt >= branch[j]) {
        cnt -= branch[j];
        branch[j + 1] += branch[j];
        // branch[i] += branch[j];
        res += branch[j] * (i - j);
        branch[j] = 0;
        j++;
        branch[j] -= a[j];
        branch[j + 1] = branch[j];
        if (cnt == 0)
          break;
      } else {
        branch[j + 1] += cnt;
        // branch[i] += cnt;
        branch[j] -= cnt;
        res += cnt * (i - j);
        cnt = 0;
        break;
      }
    }
    // branch[i] -= a[i];
    if (cnt > 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define int long long

#define override_rep(i, l, r, mes, ...) mes
#define rep1(i, n) for (int i = 0; i < n; i++)
#define rep2(i, l, r) for (int i = l; i < r; i++)
#define rep(...) override_rep(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

using namespace std;
using P = pair<int, int>;

template <typename T, typename U>
istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

// https://twitter.com/vane11ope ←フォローしましょう
// (ノ)>◡<(ヾ)ﾓﾁﾓﾁ
signed main(signed argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  const int MOD = 1e9 + 7;

  int n;
  cin >> n;

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

  int cnt = a[n], ans = a[n];
  rep(i, 1, n + 1) {
    cnt = min(memo[n - i], cnt);
    ans += cnt + a[n - i];
    cnt += a[n - i];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long

#define override_rep(i, l, r, mes, ...) mes
#define rep1(i, n) for (int i = 0; i < n; i++)
#define rep2(i, l, r) for (int i = l; i < r; i++)
#define rep(...) override_rep(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

using namespace std;
using P = pair<int, int>;

template <typename T, typename U>
istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

// https://twitter.com/vane11ope ←フォローしましょう
// (ノ)>◡<(ヾ)ﾓﾁﾓﾁ
signed main(signed argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  const int MOD = 1e9 + 7;

  int n;
  cin >> n;

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

  int cnt = a[n], ans = a[n];
  rep(i, 1, n + 1) {
    cnt = min(memo[n - i], cnt);
    ans += cnt + a[n - i];
    cnt += a[n - i];
  }
  cout << ans << endl;
}
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;

int n;
ll ans = -1;

void dfs(int i, ll leaf, vector<ll> &node, vector<int> &a) {
  if (i == n - 1) {
    if (node.at(i) != a.at(i))
      return;
    ll tot = 0;
    rep(i, n) { tot += node.at(i); }
    ans = max(ans, tot);
    return;
  }

  ll root = node.at(i) - a.at(i);
  leaf -= a.at(i);
  if (root <= 0)
    return;
  node.at(i + 1) = min(root * 2, leaf);
  dfs(i + 1, leaf, node, a);
}

int main() {
  cin >> n;
  n++;
  vector<int> a(n);
  ll leaf = 0;
  rep(i, n) {
    cin >> a.at(i);
    leaf += a.at(i);
  }

  vector<ll> node(n);
  node.at(0) = 1;
  dfs(0, leaf, node, a);
  cout << ans << 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;

int n;
int ans = -1;

void dfs(int i, vector<int> node, vector<int> &a) {
  if (i == n - 1) {
    if (node.at(i) != a.at(i))
      return;
    int tot = 0;
    rep(i, n) { tot += node.at(i); }
    ans = max(ans, tot);
    return;
  }

  int root = node.at(i) - a.at(i);
  if (root <= 0)
    return;
  for (int r = root; r <= root * 2; r++) {
    node.at(i + 1) = r;
    dfs(i + 1, node, a);
  }
}

int main() {
  cin >> n;
  n++;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  vector<int> node(n);
  node.at(0) = 1;
  dfs(0, node, a);
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define nin(n)                                                                 \\
  int n;                                                                       \\
  cin >> n;
#define kin(k)                                                                 \\
  int k;                                                                       \\
  cin >> k;

int main() {
  int n;
  cin >> n;
  vector<long long> a(101010);
  vector<long long> mi(101010);
  vector<long long> max(101010);
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }
  if (n == 0) {
    if (a[0] == 1) {
      cout << "1";
    } else {
      cout << "-1";
    }
  } else {
    mi[n] = a[n];
    max[n] = a[n];
    for (int i = n - 1; i >= 0; i--) {
      max[i] = max[i + 1] + a[i];
      if (mi[i + 1] % 2 == 0) {
        mi[i] = mi[i + 1] / 2 + a[i];
      } else {
        mi[i] = mi[i + 1] / 2 + 1 + a[i];
      }
    }
    if (mi[0] > 1 || max[0] < 1 || a[0] != 0) {
      cout << "-1" << endl;
    } else {
      long long ans = 1;
      long long tmp = 1;
      for (int i = 1; i < n + 1; i++) {
        ans = ans + min(2 * tmp, max[i]);
        tmp = min(2 * tmp, max[i]) - a[i];
      }
      cout << ans << endl;
    }
  }
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define nin(n)                                                                 \\
  int n;                                                                       \\
  cin >> n;
#define kin(k)                                                                 \\
  int k;                                                                       \\
  cin >> k;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> mi(n + 1);
  vector<long long> max(n + 1);
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }
  mi[n] = a[n];
  max[n] = a[n];
  for (int i = n - 1; i >= 0; i--) {
    max[i] = max[i + 1] + a[i];
    if (mi[i + 1] % 2 == 0) {
      mi[i] = mi[i + 1] / 2 + a[i];
    } else {
      mi[i] = mi[i + 1] / 2 + 1 + a[i];
    }
  }
  if (mi[0] > 1 || max[0] < 1 || a[0] != 0) {
    cout << "-1" << endl;
  } else {
    long long ans = 1;
    long long tmp = 1;
    for (int i = 1; i < n + 1; i++) {
      ans = ans + min(2 * tmp, max[i]);
      tmp = min(2 * tmp, max[i]) - a[i];
    }
    cout << ans << endl;
  }
}
Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  vector<long long> vec(N + 1);
  for (long long i = 0; i < N + 1; ++i)
    cin >> vec[i];
  // 可能か否かの判定
  vector<pair<long long, long long>> kanou(N + 1);
  kanou[N].first = vec[N];
  kanou[N].second = vec[N];
  for (long long i = N - 1; i >= 0; --i) {
    kanou[i].first = kanou[i + 1].first / 2 + kanou[i + 1].first % 2 + vec[i];
    kanou[i].second = kanou[i + 1].second + vec[i];
  }
  // for (auto i : kanou)
  //     cout << i.first << " " << i.second << "\\n";
  // グラフが作成できない場合
  if (kanou[0].first > 1) {
    cout << "-1\\n";
    return 0;
  }

  vector<long long> MAX(N + 1);
  MAX[0] = 1;
  long long result = MAX[0];
  for (long long i = 1; i < N + 1; ++i) {
    MAX[i] = min(kanou[i].second, 2 * (MAX[i - 1] - vec[i - 1]));
    result += MAX[i];
  }
  cout << result << "\\n";
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  unsigned long long N;
  cin >> N;
  vector<unsigned long long> vec(N + 1);
  for (unsigned long long i = 0; i < N + 1; ++i)
    cin >> vec[i];
  // 可能か否かの判定
  vector<pair<unsigned long long, unsigned long long>> kanou(N + 1);
  kanou[N].first = vec[N];
  kanou[N].second = vec[N];
  for (unsigned long long i = N - 1; i >= 0; --i) {
    kanou[i].first = kanou[i + 1].first / 2 + kanou[i + 1].first % 2 + vec[i];
    kanou[i].second = kanou[i + 1].second + vec[i];
  }
  // for (auto i : kanou)
  //     cout << i.first << " " << i.second << "\\n";
  // グラフが作成できない場合
  if (kanou[0].first > 1) {
    cout << "-1\\n";
    return 0;
  }

  vector<unsigned long long> MAX(N + 1);
  MAX[0] = 1;
  for (unsigned long long i = 1; i < N + 1; ++i)
    MAX[i] = min(kanou[i].second, 2 * (MAX[i - 1] - vec[i - 1]));
  unsigned long long result = accumulate(MAX.begin(), MAX.end(), 0);
  cout << result << "\\n";
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 0; i < n + 1; i++)
    cin >> a[i];
  vector<long long> parent(n + 1);
  parent[0] = 1 - a[0];
  for (long long i = 1; i < n + 1; i++) {
    if (parent[i - 1] * 2 - a[i] < 100000000000000000)
      parent[i] = parent[i - 1] * 2 - a[i];
    else
      parent[i] = 100000000000000000;
  }
  long long ans = a[n];
  long long x = a[n];
  for (int i = n - 1; i >= 0; i--) {
    if (x % 2 == 0 && x / 2 > parent[i]) {
      ans = -1;
      break;
    } else if (x % 2 == 1 && x / 2 + 1 > parent[i]) {
      ans = -1;
      break;
    } else {
      ans += min(parent[i], x) + a[i];
      x = min(parent[i], x) + a[i];
    }
  }
  if (n != 0)
    cout << ans << endl;
  else {
    if (a[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
  }
}


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;

int main() {
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 0; i < n + 1; i++)
    cin >> a[i];
  vector<long long> parent(n);
  parent[0] = 1 - a[0];
  bool check = false;
  for (long long i = 1; i < n + 1; i++) {
    if (parent[i - 1] * 2 - a[i] < 100000000000000000)
      parent[i] = parent[i - 1] * 2 - a[i];
    else
      parent[i] = 100000000000000000;
    if (parent[i] <= 0)
      check = true;
  }
  long long ans = a[n];
  long long x = a[n];
  for (int i = n - 1; i >= 0; i--) {
    if (x % 2 == 0 && x / 2 > parent[i]) {
      ans = -1;
      break;
    } else if (x % 2 == 1 && x / 2 + 1 > parent[i]) {
      ans = -1;
      break;
    } else {
      ans += min(parent[i], x) + a[i];
      x = min(parent[i], x) + a[i];
      if (ans > 1900000000000000000) {
        ans = -1;
        break;
      }
    }
  }
  if (n != 0)
    cout << ans << endl;
  else {
    if (a[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
  }
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

typedef long long LL;
const int MN = 200005;

int N;
LL A[MN], B[MN], C[MN];

int main() {
  scanf("%d", &N);
  for (int i = 0; i <= N; ++i)
    scanf("%lld", &A[i]);
  if (!N)
    return puts(A[0] == 1 ? "1" : "-1"), 0;
  for (int i = N; i >= 0; --i)
    B[i] = B[i + 1] + A[i];
  C[0] = 1 - A[0];
  for (int i = 1; i <= N; ++i) {
    C[i] = 2 * C[i - 1] - A[i];
    if (C[i] < 0 || C[i] + A[i] == 0)
      return puts("-1"), 0;
    if (C[i] + A[i] > B[i])
      C[i] = B[i] - A[i];
  }
  LL Ans = 0;
  for (int i = 0; i <= N; ++i)
    Ans += A[i] + C[i];
  printf("%lld\\n", Ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

typedef long long LL;
const int MN = 200005;

int N;
LL A[MN], B[MN], C[MN];

int main() {
  scanf("%d", &N);
  for (int i = 0; i <= N; ++i)
    scanf("%lld", &A[i]);
  if (!N)
    return puts(A[0] == 1 ? "1" : "-1");
  for (int i = N; i >= 0; --i)
    B[i] = B[i + 1] + A[i];
  C[0] = 1 - A[0];
  for (int i = 1; i <= N; ++i) {
    C[i] = 2 * C[i - 1] - A[i];
    if (C[i] < 0 || C[i] + A[i] == 0)
      return puts("-1"), 0;
    if (C[i] + A[i] > B[i])
      C[i] = B[i] - A[i];
  }
  LL Ans = 0;
  for (int i = 0; i <= N; ++i)
    Ans += A[i] + C[i];
  printf("%lld\\n", Ans);
  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 rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), limit(n);
  rep(i, n + 1) cin >> a[i];

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

  limit[n - 1] = a[n] + a[n - 1];
  rrep(i, n - 1) {
    // cout<<limit[i+1]<<' '<<a[i]<<endl;
    limit[i] = limit[i + 1] + a[i];
  }
  limit.push_back(a.back());

  //  for(auto c:limit)cout<<c<<' ';cout<<endl;

  ll result = 1;
  ll l = 1;

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

  rep(i, n) {
    ll c = (ll)a[i];

    ll nl = min<ll>((l - c) * 2LL, limit[i + 1]);
    if (l - c < 0 || nl - a[i + 1] < 0) {
      // cout<<nl<<'-'<<a[i+1]<<endl;
      cout << -1 << endl;
      return 0;
    }
    if (l == 0 && a[i] > 0) {
      cout << -1 << endl;
      return 0;
    }

    if (i < n - 1)
      result += nl;
    // cout<<l<<' '<<c<<' '<<l-c<<' '<<nl<<' '<<result<<endl;
    swap(l, nl);
  }
  // cout<<l<<endl;
  if (a.back() > limit.back() || a.back() > l) {
    cout << -1 << endl;
    return 0;
  }

  cout << result + a.back() << endl;

  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 rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), limit(n);
  rep(i, n + 1) cin >> a[i];

  limit[n - 1] = a[n] + a[n - 1];
  rrep(i, n - 1) {
    // cout<<limit[i+1]<<' '<<a[i]<<endl;
    limit[i] = limit[i + 1] + a[i];
  }

  //  for(auto c:limit)cout<<c<<' ';cout<<endl;

  ll result = 1;
  ll l = 1;

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

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

  rep(i, n - 1) {
    ll c = a[i];

    ll nl = min((l - c) * 2, limit[i + 1]);
    if (l - c < 0 || nl - a[i + 1] < 0) {
      cout << -1 << endl;
      return 0;
    }

    result += nl;
    // cout<<l<<' '<<c<<' '<<l-c<<' '<<nl<<endl;
    swap(l, nl);
  }

  cout << result + a.back() << 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++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define maxn 100005
using namespace std;

#define INF 1000000000000000LL
ll A[maxn];
ll lim[maxn];

int main() {
  int n;
  cin >> n;
  rep(i, n + 1) cin >> A[i];
  lim[0] = 1;
  for (int i = 1; i < n + 1; i++) {
    ll m = (lim[i - 1] - A[i - 1]) * 2LL;
    if (m < 0) {
      cout << -1 << endl;
      return 0;
    }
    lim[i] = min(INF, m);
  }

  if (lim[n] < A[n]) {
    cout << -1 << endl;
    return 0;
  }
  ll ret = A[n], now = A[n];
  for (int i = n - 1; i >= 0; i--) {
    now += A[i];
    now = min(now, lim[i]);
    ret += now;
  }
  cout << ret << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define maxn 100005
using namespace std;

#define INF 1000000000000000LL
ll A[maxn];
ll lim[maxn];

int main() {
  int n;
  cin >> n;
  rep(i, n + 1) cin >> A[i];
  lim[0] = 1;
  for (int i = 1; i < n + 1; i++) {
    ll m = (lim[i - 1] - A[i - 1]) * 2LL;
    if (m < 0) {
      cout << -1 << endl;
      return 0;
    }
    lim[i] = min(INF, m);
  }

  if (lim[n] < A[n]) {
    cout << -1 << endl;
    return 0;
  }
  ll ret = A[n], now = A[n];
  for (int i = n - 1; i >= 0; i++) {
    now += A[i];
    now = min(now, lim[i]);
    ret += now;
  }
  cout << ret << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

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

  int n;
  cin >> n;
  vi a(n + 1);
  rep(i, n + 1) cin >> a[i];

  vll mx(n + 1, -1);
  rep(i, n + 1) {
    if (i == 0)
      mx[i] = 1;
    else
      mx[i] = (mx[i - 1] - a[i - 1]) * 2;
    if (mx[i] > 1e18 || mx[i] < 0)
      break;
  }

  ll ret = 0;
  ll cur = 0;

  for (int i = n; i >= 0; i--) {
    if (mx[i] == -1) {
      cur += a[i];
    } else {
      if (mx[i] >= cur + a[i]) {
        cur += a[i];
      } else {
        if (mx[i] >= (cur + 1) / 2 + a[i]) {
          cur = mx[i];
        } else {
          ret = -1;
          break;
        }
      }
    }

    ret += cur;
  }

  cout << ret << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

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

  int n;
  cin >> n;
  vi a(n + 1);
  rep(i, n + 1) cin >> a[i];

  vll mx(n, -1);
  rep(i, n + 1) {
    if (i == 0)
      mx[i] = 1;
    else
      mx[i] = (mx[i - 1] - a[i - 1]) * 2;
    if (mx[i] > 1e9)
      break;
  }

  ll ret = 0;
  ll cur = 0;

  for (int i = n; i >= 0; i--) {
    if (mx[i] == -1) {
      cur += a[i];
    } else {
      if (mx[i] >= cur + a[i]) {
        cur += a[i];
      } else {
        if (mx[i] >= (cur + 1) / 2 + a[i]) {
          cur = mx[i];
        } else {
          ret = -1;
          break;
        }
      }
    }

    ret += cur;
  }

  cout << ret << endl;
}
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 i = 0; i <= N; i++) {
    cin >> A.at(i);
  }
  vector<int64_t> Up(N + 1);
  vector<int64_t> Down(N + 1);
  Up.at(0) = 1;
  for (int64_t i = 1; i <= N; i++) {
    if (Up.at(i - 1) - A.at(i - 1) > 9223372036854775807 / 2 + 1) {
      Up.at(i) = 9223372036854775807;
    } else {
      Up.at(i) = (Up.at(i - 1) - A.at(i - 1)) * 2;
    }
  }
  Down.at(N) = A.at(N);
  for (int64_t i = N - 1; i >= 0; i--) {
    Down.at(i) = Down.at(i + 1) + A.at(i);
  }
  int64_t Ans = 0;
  int64_t M = 0;
  if (A.at(0) == 1 && N == 0) {
    cout << 1 << endl;
    goto finish;
  }
  if (A.at(0) != 0) {
    cout << -1 << endl;
    goto finish;
  }
  for (int64_t i = 0; i <= N; i++) {
    M = min(Up.at(i), Down.at(i));
    if (A.at(i) > M) {
      cout << -1 << endl;
      goto finish;
    } else {
      Ans += M;
    }
  }
  if (N == 0) {
    cout << 1 << endl;
    goto finish;
  }
  cout << Ans << endl;
finish:
  N = 0;
}

Error: 0

Fixed submission:

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

int main() {
  int64_t N;
  cin >> N;
  int F = 0;
  vector<int64_t> A(N + 1);
  for (int64_t i = 0; i <= N; i++) {
    cin >> A.at(i);
  }
  vector<int64_t> Up(N + 1);
  vector<int64_t> Down(N + 1);
  Up.at(0) = 1;
  for (int64_t i = 1; i <= N; i++) {
    if (Up.at(i - 1) - A.at(i - 1) > 9223372036854775807 / 2) {
      Up.at(i) = Up.at(N + 3);
      Up.at(i) = 9223372036854775807;
    } else {
      Up.at(i) = (Up.at(i - 1) - A.at(i - 1)) * 2;
    }
  }
  Down.at(N) = A.at(N);
  for (int64_t i = N - 1; i >= 0; i--) {
    Down.at(i) = Down.at(i + 1) + A.at(i);
  }
  int64_t Ans = 0;
  int64_t M = 0;
  if (A.at(0) != 0) {
    cout << -1 << endl;
    goto finish;
  }
  for (int64_t i = 0; i <= N; i++) {
    M = min(Up.at(i), Down.at(i));
    if (A.at(i) > M) {
      cout << -1 << endl;
      goto finish;
    } else {
      Ans += M;
    }
  }
  if (N == 0) {
    cout << -1 << endl;
    goto finish;
  }
  if (F == 0) {
    cout << Ans << endl;
  }
finish:
  N = 0;
}