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

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

typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define D1(x)                                                                  \\
  { cerr << " [" << #x << ": " << x << "]\\n"; }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "]\\n";                                                             \\
  }

const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = LONG_LONG_MAX;

bool isoverflow(ll a, ll b) {
  if (a == 0 || b == 0)
    return false;
  ll res = a * b;
  if (a == res / b)
    return false;
  else
    return true;
}
void solve() {
  int n;
  cin >> n;
  ll a[n];
  bool zero = 0;
  F0R(i, n) {
    cin >> a[i];
    if (a[i] == 0)
      zero = 1;
  }

  if (zero) {
    cout << 0;
    return;
  }
  // sort(ALLn(a,n));

  ll ans = 1;

  F0R(i, n) {

    if (a[i] > 1000000000000000000 / ans) {
      cout << -1;
      return;
    }
    ans *= a[i];
  }

  cout << ans;
}

int32_t main() {
  _

#ifndef ONLINE_JUDGE
      freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  // tc(t)
  solve();
}


Error: -8

Fixed submission:

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

typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define D1(x)                                                                  \\
  { cerr << " [" << #x << ": " << x << "]\\n"; }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "]\\n";                                                             \\
  }

const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = LONG_LONG_MAX;

bool isoverflow(ll a, ll b) {
  if (a == 0 || b == 0)
    return false;
  ll res = a * b;
  if (a == res / b)
    return false;
  else
    return true;
}
void solve() {
  int n;
  cin >> n;
  ll a[n];
  F0R(i, n) cin >> a[i];
  sort(ALLn(a, n));

  ll ans = 1;
  F0R(i, n) {

    if (a[i] > 1000000000000000000 / ans) {
      cout << -1;
      return;
    }
    ans *= a[i];
  }

  cout << ans;
}

int32_t main() {
  _

#ifndef ONLINE_JUDGE
      freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  // tc(t)
  solve();
}