Problem p02658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long
#define FF(i, a, b) for (int i = a; i < b; i++)
#define BF(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define MAP map<int, int>
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define PIE 3.1415926535897932384626433832795
#define FastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
// #define ordered_set
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
/*

    order_of_key (k) : Number of items strictly smaller than k .
    find_by_order(k) : K-th element in a set (counting from zero).

*/
/**************************************************************************************/
/*Global Variables*/
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 3e18;

/**************************************************************************************/
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}
int add(int a, int b) { return (a % mod + b % mod + mod) % mod; }
int sub(int a, int b) { return (a % mod - b % mod + mod) % mod; }
int mul(int a, int b) { return ((a % mod) * (b % mod) + mod) % mod; }
int Power(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
signed main() {
  FastIO;
  int T = 1;
  // cin>>T;
  while (T--) {
    int n;
    cin >> n;
    vi a(n);
    cin >> a;
    int ans = 1;
    int k = 1e18;
    sort(all(a));
    bool ok = true;
    for (auto it : a) {
      if (it > (k / ans)) {
        cout << -1 << "\n";
        ok = false;
        break;
      } else
        ans *= it;
    }
    if (ok)
      cout << ans << "\n";
  }
}
/*
|| Do what you can with all you have,wherever you are. ||

|| Knowing is not enough, we must apply. Wishing is not enough, we must do. ||

|| A goal is a dream with deadline. ||

|| Whether you think you can or you think you can't, you're right. ||

|| A year from now you may wish you had started today ||
*/


Fixed submission:
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long
#define FF(i, a, b) for (int i = a; i < b; i++)
#define BF(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define MAP map<int, int>
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define PIE 3.1415926535897932384626433832795
#define FastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
// #define ordered_set
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
/*

    order_of_key (k) : Number of items strictly smaller than k .
    find_by_order(k) : K-th element in a set (counting from zero).

*/
/**************************************************************************************/
/*Global Variables*/
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 3e18;

/**************************************************************************************/
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}
int add(int a, int b) { return (a % mod + b % mod + mod) % mod; }
int sub(int a, int b) { return (a % mod - b % mod + mod) % mod; }
int mul(int a, int b) { return ((a % mod) * (b % mod) + mod) % mod; }
int Power(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
signed main() {
  FastIO;
  int T = 1;
  // cin>>T;
  while (T--) {
    int n;
    cin >> n;
    vi a(n);
    cin >> a;
    int ans = 1;
    int k = 1e18;
    sort(all(a));
    bool ok = true;
    for (auto it : a) {
      if (ans == 0) {
        cout << 0 << "\n";
        ok = false;
        break;
      }
      if (it > (k / ans)) {
        cout << -1 << "\n";
        ok = false;
        break;
      } else
        ans *= it;
    }
    if (ok)
      cout << ans << "\n";
  }
}
/*
|| Do what you can with all you have,wherever you are. ||

|| Knowing is not enough, we must apply. Wishing is not enough, we must do. ||

|| A goal is a dream with deadline. ||

|| Whether you think you can or you think you can't, you're right. ||

|| A year from now you may wish you had started today ||
*/