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

/*
I am A NOOB.
You CANNOT LEARN Anything from My CODE.
So Just Go please.
Happy Coding.
*/
#include <bits/stdc++.h>
#define PI acos(-1)
#define pb push_back
#define pf push_front
#define mx(v) *max_element(v.begin(), v.end())
#define mn(v) *min_element(v.begin(), v.end())
#define sum(v) accumulate(v.begin(), v.end(), 0)
#define uni(v) v.erase(unique(v.begin(), v.end()), v.end());
#define scan(n) scanf("%d", &n);
#define arInput0(ar, n)                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    scan(ar[i]);
#define arInput1(ar, n)                                                        \\
  for (int i = 1; i <= n; i++)                                                 \\
    scan(ar[i]);
#define arOut0(ar, n)                                                          \\
  for (int i = 0; i < n; i++) {                                                \\
    printf("%lld ", ar[i]);                                                    \\
  }
#define arOut1(ar, n)                                                          \\
  for (int i = 1; i <= n; i++) {                                               \\
    printf("%lld ", ar[i]);                                                    \\
  }
#define gcd(a, b) __gcd(a, b);
#define lcm(a, b) (a / (gcd(a, b)) * b);
#define all(a) a.begin(), a.end()
#define priority_queue p_q
#define boost                                                                  \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define INF int(1e18)
#define mem(x, val) memset(x, val, sizeof(x))
#define io                                                                     \\
  {                                                                            \\
    freopen("in.txt", "r", stdin);                                             \\
    freopen("out.txt", "w", stdout);                                           \\
  }
#define popcount(x) __builtin_popcount(x)
#define X first
#define Y second
#define PII pair<int, int>
typedef long long int lli;
typedef unsigned long long int ulli;
using namespace std;

int main() {
  // io;
  boost;
  lli n;
  cin >> n;
  lli ar[n + 5];
  lli mul = 1;
  lli prevMal = 1;
  lli mxx = 1000000000000000000;
  bool f = true;
  bool z = false;
  /*for (int i = 1; i <= n; i++) {
          cin >> ar[i];
          prevMal = mul;
          mul = mul * ar[i];
          if (ar[i] == 0)z = true;
          if (mul < prevMal) {f = false;}
  }
  if (z)cout << 0 << endl;
  else if (mul > mxx) {cout << -1 << endl;}
  else if (f) {cout << mul << endl;}
  else cout << -1 << endl;*/

  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    if (ar[i] == 0)
      z = true;

    double x = log10(ar[i]);
    double z = log10(mul);
    if (x > 18)
      f = false;
    else {
      if (x + z > 18)
        f = false;
      else {
        mul = mul * ar[i];
        if (mul > mxx)
          f = false;
      }
    }
  }
  if (z)
    cout << 0 << endl;
  else if (f == false)
    cout << -1 << endl;
  else
    cout << mul << endl;
}


Error: 0

Fixed submission:


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

int main() {
  long long int n, i;
  long long int b, x, y, sum = 1, a[10000];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum *= a[i];
  }
  if (sum > (pow(10, 18))) {
    cout << -1 << endl;
  } else
    cout << sum << endl;
  return 0;
}