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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2 * acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 100010;
const int MX = 123456;
const int mod = (int)1e9 + 7;
const int base = 97;
const ull BS1 = 10000019ULL;
const int INF = (1 << 29);

template <class T> inline void fastScan(T &x) {
  register char c = getchar();
  int neg = 0;
  x = 0;
  for (; (c < 48 || c > 57) && (c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg)
    x = -x;
}

// OrderedSet
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template < typename T > using ordered_set = tree < T, null_type, greater < T
// >, rb_tree_tag, tree_order_statistics_node_update >;

// typedef uint64_t hash_t;
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// const int HASH_COUNT = 2;
// const hash_t HASH_P = (uint32_t)-13337;
// uniform_int_distribution<hash_t> MULT_DIST(0.25 * HASH_P, 0.75 * HASH_P);
// const hash_t HASH_MULT[HASH_COUNT] = {MULT_DIST(rng), MULT_DIST(rng)};

ll limit = 1ll * 1e18;

int main() {
  int n;
  ll ans = 1;
  fastScan(n);
  for (int i = 1; i <= n; i++) {
    ll x;
    fastScan(x);
    if (x == 0) {
      ans = 0;
      continue;
    }
    if (ans == -1 || ans == 0)
      continue;
    if ((limit / ans) < x) {
      ans = -1;
    } else {
      ans *= x;
    }
  }
  if (ans > limit)
    ans = -1;
  printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2 * acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 100010;
const int MX = 123456;
const int mod = (int)1e9 + 7;
const int base = 97;
const ull BS1 = 10000019ULL;
const int INF = (1 << 29);

template <class T> inline void fastScan(T &x) {
  register char c = getchar();
  int neg = 0;
  x = 0;
  for (; (c < 48 || c > 57) && (c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg)
    x = -x;
}

// OrderedSet
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template < typename T > using ordered_set = tree < T, null_type, greater < T
// >, rb_tree_tag, tree_order_statistics_node_update >;

// typedef uint64_t hash_t;
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// const int HASH_COUNT = 2;
// const hash_t HASH_P = (uint32_t)-13337;
// uniform_int_distribution<hash_t> MULT_DIST(0.25 * HASH_P, 0.75 * HASH_P);
// const hash_t HASH_MULT[HASH_COUNT] = {MULT_DIST(rng), MULT_DIST(rng)};

ll limit = 1ll * 1e18;

int main() {
  int n;
  ll ans = 1;
  fastScan(n);
  for (int i = 1; i <= n; i++) {
    ll x;
    fastScan(x);
    if (x == 0) {
      ans = 0;
      continue;
    }
    if (ans == -1)
      continue;
    if ((limit / ans) < x) {
      ans = -1;
    } else {
      ans *= x;
    }
  }
  if (ans > limit)
    ans = -1;
  printf("%lld\\n", ans);
}