Problem p02658 (C++) — buggy submission, verdict: 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 lli long long int
#define all(x) x.begin(), x.end()
#define siz(x) x.size()
#define prs(x, y) x.find(y) != x.end()
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res)
    n -= n & (-n);
  return res;
}
inline int level(int n) {
  int ans = 0;
  while (n % 2 == 0) {
    n /= 2;
    ans++;
  }
  return ans;
}
lli a[100005];
lli MAX = 1e18;
int main() {
  lli n = 0;
  cin >> n;
  bool f = true;
  bool k = false;
  lli mul = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      k = true;
    if (k)
      continue;
    if (MAX / mul < a[i])
      f = false;
    if (f)
      mul *= a[i];
  }
  if (!f)
    mul = -1;
  if (k)
    mul = 0;
  cout << mul;
  return 0;
}


Error: WA

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 lli long long int
#define all(x) x.begin(), x.end()
#define siz(x) x.size()
#define prs(x, y) x.find(y) != x.end()
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res)
    n -= n & (-n);
  return res;
}
inline int level(int n) {
  int ans = 0;
  while (n % 2 == 0) {
    n /= 2;
    ans++;
  }
  return ans;
}
lli a[100005];
lli MAX = 1e18;
int main() {
  lli n = 0;
  cin >> n;
  bool f = true;
  bool k = false;
  lli mul = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      k = true;
    if (MAX / mul < a[i])
      f = false;
    if (f || k)
      mul *= a[i];
  }
  if (!f)
    mul = -1;
  if (k)
    mul = 0;
  cout << mul;
  return 0;
}