Problem p02665 (C++) — buggy submission, verdict: Runtime Error

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set,multiset
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const int MOD = 1000000007;
typedef pair<long long, long long> P;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

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

  int N;
  cin >> N;
  vector<long long> retu(N + 1);
  for (int i = 0; i < N + 1; i++)
    cin >> retu.at(i);
  if (N == 0) {
    if (retu.at(0) == 1)
      cout << 1 << endl;
    if (retu.at(0) != 1)
      cout << -1 << endl;
    return 0;
  }
  long long beki2 = 1;
  for (int i = 0; i < N + 1; i++) {
    if (beki2 > 100000000) {
      break;
    }
    if (retu.at(i) > beki2) {
      cout << -1 << endl;
      return 0;
    }
    beki2 *= 2;
  }
  if (retu.at(0) + retu.at(1) > 1 && N != 1) {
    cout << -1 << endl;
    return 0;
  }
  vector<long long> rui(N + 2);
  for (int i = 0; i < N + 1; i++) {
    rui.at(i + 1) = rui.at(i) + retu.at(i);
  }
  long long ans = 1;
  long long prvF = 0, prvC = 1;
  if (retu.at(0) == 1)
    prvF = 1, prvC = 0;
  for (int i = 1; i <= N; i++) {

    if (prvC > rui.at(N + 1) - rui.at(i)) {
      cout << -1 << endl;
      return 0;
    }
    long long nowF = retu.at(i);
    long long nowC = 2 * prvC - nowF;
    if (nowC < 0) {
      cout << -1 << endl;
      return 0;
    }
    if (prvC > nowF + nowC || nowF + nowC > 2 * prvC) {
      cout << -1 << endl;
      return 0;
    }
    if (nowC > rui.at(N + 1) - rui.at(i + 1)) {
      nowC = rui.at(N + 1) - rui.at(i + 1);
    }
    ans += nowF + nowC;
    prvF = nowF;
    prvC = nowC;
    // cout << i << " " << nowF << " " << nowC << " " << ans << endl;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set,multiset
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const int MOD = 1000000007;
typedef pair<long long, long long> P;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

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

  int N;
  cin >> N;
  vector<long long> retu(N + 1);
  for (int i = 0; i < N + 1; i++)
    cin >> retu.at(i);
  long long beki2 = 1;
  for (int i = 0; i < N + 1; i++) {
    if (beki2 > 100000000) {
      break;
    }
    if (retu.at(i) > beki2) {
      cout << -1 << endl;
      return 0;
    }
    beki2 *= 2;
  }
  if (retu.at(0) + retu.at(1) > 1 && N != 1) {
    cout << -1 << endl;
    return 0;
  }
  vector<long long> rui(N + 2);
  for (int i = 0; i < N + 1; i++) {
    rui.at(i + 1) = rui.at(i) + retu.at(i);
  }
  long long ans = 1;
  long long prvF = 0, prvC = 1;
  if (retu.at(0) == 1)
    prvF = 1, prvC = 0;
  for (int i = 1; i <= N; i++) {

    if (prvC > rui.at(N + 1) - rui.at(i)) {
      cout << -1 << endl;
      return 0;
    }
    long long nowF = retu.at(i);
    long long nowC = 2 * prvC - nowF;
    if (prvC > nowF + nowC || nowF + nowC > 2 * prvC) {
      cout << -1 << endl;
      return 0;
    }
    if (nowC > rui.at(N + 1) - rui.at(i + 1)) {
      nowC = rui.at(N + 1) - rui.at(i + 1);
    }
    ans += nowF + nowC;
    prvF = nowF;
    prvC = nowC;
    // cout << i << " " << nowF << " " << nowC << " " << ans << endl;
  }
  cout << ans << endl;
}