Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;
#define endl '\n'

template <typename T> inline bool chmin(T &x, T a) {
  if (x >= a) {
    x = a;
    return true;
  }
  return false;
}

template <typename T> inline bool chmax(T &x, T a) {
  if (x <= a) {
    x = a;
    return true;
  }
  return false;
}

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

  int N;
  int a;
  cin >> N >> a;
  if (N == 0) {
    if (a != 1) {
      cout << -1 << endl;
    } else {
      cout << 1 << endl;
    }
    return 0;
  } else if (a != 0) {
    cout << -1 << endl;
    return -1;
  }

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

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

    if (B[i] > (1LL << 50)) {
      B[i] = 1LL << 50;
    }
  }

  ll ans = 0;
  ll temp = 0;
  for (int i = N - 1; i >= -1; i--) {
    if (i == -1) {
      ans++;
      break;
    }
    temp += A[i];
    ans += temp;
    if (i > 0) {
      chmin(temp, B[i - 1]);
    } else {
      chmin(temp, 1LL);
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;
#define endl '\n'

template <typename T> inline bool chmin(T &x, T a) {
  if (x >= a) {
    x = a;
    return true;
  }
  return false;
}

template <typename T> inline bool chmax(T &x, T a) {
  if (x <= a) {
    x = a;
    return true;
  }
  return false;
}

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

  int N;
  int a;
  cin >> N >> a;
  if (N == 0) {
    if (a != 1) {
      cout << -1 << endl;
    } else {
      cout << 1 << endl;
    }
    return 0;
  } else if (a != 0) {
    cout << -1 << endl;
    return 0;
  }

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

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

    if (B[i] > (1LL << 50)) {
      B[i] = 1LL << 50;
    }
  }

  ll ans = 0;
  ll temp = 0;
  for (int i = N - 1; i >= -1; i--) {
    if (i == -1) {
      ans++;
      break;
    }
    temp += A[i];
    ans += temp;
    if (i > 0) {
      chmin(temp, B[i - 1]);
    } else {
      chmin(temp, 1LL);
    }
  }

  cout << ans << endl;

  return 0;
}