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

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define pb push_back
#define mp make_pair
#define pi 3.1415926535897
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(v) ((int)(v).size())
#define mod 1000000007
#define MXM (ll)1e18
#define LIM 9223372036854775807
#define db(x) cout << x << " "
#define db1(x) cout << x << '\\n'
#define db2(x, y) cout << x << " " << y << '\\n'
#define db3(x, y, z) cout << x << " " << y << " " << z << '\\n'
#define REP(i, a, n) for (ll i = a; i < (n); i++)
#define RREPA(i, a, n) for (ll i = a; i >= (n); i--)
#define REPA(i, a, n) for (ll i = a; i <= (n); i++)
using namespace std;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
//////
// prime Seive
/*
bitset<1000005> bt;
//unordered_map<ll,bool> is_prime;
vector<ll> primes;
void pS(ll n){
    ll val = min(n+1,(ll)1000006);
    for(ll i=3;i<=val;i+=2){
        bt[i] =true;
    }
    bt[0] =false;
    bt[1] =false;
    bt[2] =false;
    //is_prime[2] =true;
    primes.pb((ll)2);
    for(ll i=3;i<=val;i+=2){
        if(bt[i])
            for(ll j=i*i;j<=val;j+=2*i)
                bt[j] =false;
    }
    for(ll i=3;i<=val;i+=2){
        if(bt[i]){primes.pb(i);
        //is_prime[i]=true;
        //cout<<i<<" :PRIME\\n";
        }
    }
    return;
}
   */
/////
// Fenwick Tree
/*
BIT[200005] ={0};
//BUILD BIT
// for(int i=1;i<1;i++){ cin>>a[i];update(i,a[i],n);};

void update(int i,int val,int n){
    while(i<=n){
    BIT[i]+= inc;
    i+= (i&(-i));
    }
}
ll query(int i){
    ll ans =0;
    while(i>0){
        ans+= BIT[i];
        i-=(i&(-i));
    }
    return ans;
}

*/
//__builtin_popcount(4)
ll logPow(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
    }
    a *= a;
    b = b >> 1;
  }
  return ans;
}

bool check(ll val, ll ans) {
  if (ans == 0)
    return true;
  if (val > (MXM / ans)) {
    return false;
  }
  return true;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
  */
  int t = 1;

  // memset(arr,0,sizeof(arr));
  // cin>>t;
  // pS(INT_MAX);
  while (t--) {
    ll n;
    cin >> n;
    ll tmp, ans = 1;
    bool ch = false;
    int z = 0;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      if (tmp == 0) {
        z++;
        ans = 0;
      } else if (z == 0) {
        bool test = check(tmp, ans);
        if (test) {
          ans *= tmp;
        } else {
          ch = true;
          ans *= tmp;
          if (ans == 0)
            ans++;
        }
      }
    }
    if (z) {
      cout << 0 << "\\n";
    } else if (ch == true || ans > MXM || ans > LIM) {
      cout << -1 << "\\n";
    } else {
      cout << (ll)ans << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define pb push_back
#define mp make_pair
#define pi 3.1415926535897
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(v) ((int)(v).size())
#define mod 1000000007
#define MXM (ll)1e18
#define LIM 9223372036854775807
#define db(x) cout << x << " "
#define db1(x) cout << x << '\\n'
#define db2(x, y) cout << x << " " << y << '\\n'
#define db3(x, y, z) cout << x << " " << y << " " << z << '\\n'
#define REP(i, a, n) for (ll i = a; i < (n); i++)
#define RREPA(i, a, n) for (ll i = a; i >= (n); i--)
#define REPA(i, a, n) for (ll i = a; i <= (n); i++)
using namespace std;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
//////
// prime Seive
/*
bitset<1000005> bt;
//unordered_map<ll,bool> is_prime;
vector<ll> primes;
void pS(ll n){
    ll val = min(n+1,(ll)1000006);
    for(ll i=3;i<=val;i+=2){
        bt[i] =true;
    }
    bt[0] =false;
    bt[1] =false;
    bt[2] =false;
    //is_prime[2] =true;
    primes.pb((ll)2);
    for(ll i=3;i<=val;i+=2){
        if(bt[i])
            for(ll j=i*i;j<=val;j+=2*i)
                bt[j] =false;
    }
    for(ll i=3;i<=val;i+=2){
        if(bt[i]){primes.pb(i);
        //is_prime[i]=true;
        //cout<<i<<" :PRIME\\n";
        }
    }
    return;
}
   */
/////
// Fenwick Tree
/*
BIT[200005] ={0};
//BUILD BIT
// for(int i=1;i<1;i++){ cin>>a[i];update(i,a[i],n);};

void update(int i,int val,int n){
    while(i<=n){
    BIT[i]+= inc;
    i+= (i&(-i));
    }
}
ll query(int i){
    ll ans =0;
    while(i>0){
        ans+= BIT[i];
        i-=(i&(-i));
    }
    return ans;
}

*/
//__builtin_popcount(4)
ll logPow(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
    }
    a *= a;
    b = b >> 1;
  }
  return ans;
}

bool check(ll val, ll ans) {
  if (val > (MXM / ans)) {
    return false;
  }
  return true;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
  */
  int t = 1;

  // memset(arr,0,sizeof(arr));
  // cin>>t;
  // pS(INT_MAX);
  while (t--) {
    ll n;
    cin >> n;
    ll tmp, ans = 1;
    bool ch = false;
    int z = 0;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      if (tmp == 0) {
        z++;
      } else {
        bool test = check(tmp, ans);
        if (test) {
          ans *= tmp;
        } else {
          ch = true;
          ans *= tmp;
          if (ans == 0)
            ans++;
        }
      }
    }
    if (z) {
      cout << 0 << "\\n";
    } else if (ch == true || ans > MXM || ans > LIM) {
      cout << -1 << "\\n";
    } else {
      cout << (ll)ans << "\\n";
    }
  }
  return 0;
}