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 std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define num1 1000000007
#define num2 998244353
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define REPd(i, a, n) for (ll i = a; i >= n; i--)
#define vll vector<long long>
#define pll pair<long long, long long>
#define vpll vector<pair<long long, long long>>
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f, n) std::fixed << std::setprecision(n) << f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.00000000001)
#define mm 1000000000000000000ll
#define watch(x) cerr << (#x) << " is " << (x) << endl
typedef long long ll;

// DEBUG FUNCTIONS FOR TEST
#ifndef ONLINE_JUDGE

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

template <typename T> void __p(T a) { cout << a; }
template <typename T, typename F> void __p(pair<T, F> a) {
  cout << "{";
  __p(a.first);
  cout << ",";
  __p(a.second);
  cout << "}";
}
template <typename T> void __p(std::vector<T> a) {
  cout << "{";
  for (auto it = a.begin(); it < a.end(); it++)
    __p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T, typename... Arg> void __p(T a1, Arg... a) {
  __p(a1);
  __p(a...);
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : ";
  __p(arg1);
  cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  int bracket = 0, i = 0;
  for (;; i++)
    if (names[i] == ',' && bracket == 0)
      break;
    else if (names[i] == '(')
      bracket++;
    else if (names[i] == ')')
      bracket--;
  const char *comma = names + i;
  cout.write(names, comma - names) << " : ";
  __p(arg1);
  cout << " | ";
  __f(comma + 1, args...);
}
#define trace(...)                                                             \
  cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#define endl '\n'
#endif

// DEBUG FUNCTIONS END

ll mod_exp(ll a, ll b, ll c) {
  ll res = 1;
  a = a % c;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a) % c;
    }
    b /= 2;
    a = (a * a) % c;
  }
  return res;
}
ll mymod(ll a, ll b) { return ((a % b) + b) % b; }
// bool prime[200001];
// void SieveOfEratosthenes()
// {
//     memset(prime, true, sizeof(prime));
//     prime[1]=false;
//     for (int p=2; p*p<=200000; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (int i=p*p; i<=200000; i += p)
//                 prime[i] = false;
//         }
//     }
//     return;
// }
// ll powe[200005];
// void power()
// {
//     powe[0]=1;
//     REP(i,1,200005)
//     {
//         powe[i]=mymod(2*powe[i-1],num);
//     }
// }
// ll gcdExtended(ll,ll,ll *,ll *);
// ll modInverse(ll a, ll m)
// {
//     ll x, y;
//     ll g = gcdExtended(a, m, &x, &y);
//         ll res = (x%m + m) % m;
//         return res;
// }
// ll gcdExtended(ll a, ll b, ll *x, ll *y)
// {
//     if (a == 0)
//     {
//         *x = 0, *y = 1;
//         return b;
//     }
//     ll x1, y1;
//     ll gcd = gcdExtended(b%a, a, &x1, &y1);
//     *x = y1 - (b/a) * x1;
//     *y = x1;
//     return gcd;
// }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll A[n];
  ll ans = 1;
  bool z = false;
  bool exc = false;
  REP(i, 0, n) {
    cin >> A[i];
    if (A[i] == 0) {
      z = true;
    }
    if (A[i] >= mm / ans + 1) {
      exc = true;
    } else {
      ans *= A[i];
    }
    if (ans > mm) {
      exc = true;
    }
  }
  if (z) {
    cout << "0\n";
  } else if (exc) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define num1 1000000007
#define num2 998244353
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define REPd(i, a, n) for (ll i = a; i >= n; i--)
#define vll vector<long long>
#define pll pair<long long, long long>
#define vpll vector<pair<long long, long long>>
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f, n) std::fixed << std::setprecision(n) << f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.00000000001)
#define mm 1000000000000000000ll
#define watch(x) cerr << (#x) << " is " << (x) << endl
typedef long long ll;

// DEBUG FUNCTIONS FOR TEST
#ifndef ONLINE_JUDGE

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

template <typename T> void __p(T a) { cout << a; }
template <typename T, typename F> void __p(pair<T, F> a) {
  cout << "{";
  __p(a.first);
  cout << ",";
  __p(a.second);
  cout << "}";
}
template <typename T> void __p(std::vector<T> a) {
  cout << "{";
  for (auto it = a.begin(); it < a.end(); it++)
    __p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T, typename... Arg> void __p(T a1, Arg... a) {
  __p(a1);
  __p(a...);
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : ";
  __p(arg1);
  cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  int bracket = 0, i = 0;
  for (;; i++)
    if (names[i] == ',' && bracket == 0)
      break;
    else if (names[i] == '(')
      bracket++;
    else if (names[i] == ')')
      bracket--;
  const char *comma = names + i;
  cout.write(names, comma - names) << " : ";
  __p(arg1);
  cout << " | ";
  __f(comma + 1, args...);
}
#define trace(...)                                                             \
  cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#define endl '\n'
#endif

// DEBUG FUNCTIONS END

ll mod_exp(ll a, ll b, ll c) {
  ll res = 1;
  a = a % c;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a) % c;
    }
    b /= 2;
    a = (a * a) % c;
  }
  return res;
}
ll mymod(ll a, ll b) { return ((a % b) + b) % b; }
// bool prime[200001];
// void SieveOfEratosthenes()
// {
//     memset(prime, true, sizeof(prime));
//     prime[1]=false;
//     for (int p=2; p*p<=200000; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (int i=p*p; i<=200000; i += p)
//                 prime[i] = false;
//         }
//     }
//     return;
// }
// ll powe[200005];
// void power()
// {
//     powe[0]=1;
//     REP(i,1,200005)
//     {
//         powe[i]=mymod(2*powe[i-1],num);
//     }
// }
// ll gcdExtended(ll,ll,ll *,ll *);
// ll modInverse(ll a, ll m)
// {
//     ll x, y;
//     ll g = gcdExtended(a, m, &x, &y);
//         ll res = (x%m + m) % m;
//         return res;
// }
// ll gcdExtended(ll a, ll b, ll *x, ll *y)
// {
//     if (a == 0)
//     {
//         *x = 0, *y = 1;
//         return b;
//     }
//     ll x1, y1;
//     ll gcd = gcdExtended(b%a, a, &x1, &y1);
//     *x = y1 - (b/a) * x1;
//     *y = x1;
//     return gcd;
// }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll A[n];
  ll ans = 1;
  bool z = false;
  bool exc = false;
  REP(i, 0, n) {
    cin >> A[i];
    if (A[i] == 0) {
      z = true;
    }
    if (!z && A[i] >= mm / ans + 1) {
      exc = true;
    } else {
      ans *= A[i];
    }
    if (ans > mm) {
      exc = true;
    }
  }
  if (z) {
    cout << "0\n";
  } else if (exc) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }
  return 0;
}