Problem p02658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define ub upper_bound
#define lb lower_bound
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define ff first
#define ss second
#define mp make_pair
#define pll pair<ll, ll>
#define fo(i, n) for (ll i = 0; i < n; i++)
#define foa(i, s, e) for (ll i = (s); i < e; i++)
#define fod(i, s, e) for (ll i = (s); i >= (e); i--)
const ld pi = 3.14159265358979323846;
ll mod1 = 1e9 + 7;
ll mod2 = 998244353;
ll mul(ll x, ll y) { return (1ll * (x % mod1) * (y % mod1)); }
ll modpow(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2)
      z = mul(z, x);
    x = mul(x, x);
    y /= 2;
  }
  return z;
}
ll power(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2)
      z = z * x;
    x = x * x;
    y /= 2;
  }
  return z;
}
ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll sq(ll a) {
  ll ans = (1ll * a * a);
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("inputf.txt", "r", stdin);
  freopen("outputf.txt", "w", stdout);
// freopen("error.txt" , "w" , stderr) ;
#endif
  ll n;
  cin >> n;
  ll a[n];
  ll p = 1;
  fo(i, n) { cin >> a[i]; }
  sort(a, a + n);
  fo(i, n) {
    if (a[i] > LLONG_MAX / p) {
      cout << -1 << "\n";
      return 0;
    }
    p *= a[i];
    if (p > (ll)1e18 || p < 0LL) {
      cout << -1 << "\n";
      return 0;
    }
  }

  cout << p << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define ub upper_bound
#define lb lower_bound
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define ff first
#define ss second
#define mp make_pair
#define pll pair<ll, ll>
#define fo(i, n) for (ll i = 0; i < n; i++)
#define foa(i, s, e) for (ll i = (s); i < e; i++)
#define fod(i, s, e) for (ll i = (s); i >= (e); i--)
const ld pi = 3.14159265358979323846;
ll mod1 = 1e9 + 7;
ll mod2 = 998244353;
ll mul(ll x, ll y) { return (1ll * (x % mod1) * (y % mod1)); }
ll modpow(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2)
      z = mul(z, x);
    x = mul(x, x);
    y /= 2;
  }
  return z;
}
ll power(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2)
      z = z * x;
    x = x * x;
    y /= 2;
  }
  return z;
}
ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll sq(ll a) {
  ll ans = (1ll * a * a);
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("inputf.txt", "r", stdin);
  freopen("outputf.txt", "w", stdout);
// freopen("error.txt" , "w" , stderr) ;
#endif
  ll n;
  cin >> n;
  ll a[n];
  ll p = 1;
  fo(i, n) { cin >> a[i]; }
  sort(a, a + n);
  fo(i, n) {
    if (p && a[i] > LLONG_MAX / p) {
      cout << -1 << "\n";
      return 0;
    }
    p *= a[i];
    if (p > (ll)1e18 || p < 0LL) {
      cout << -1 << "\n";
      return 0;
    }
  }

  cout << p << "\n";
}