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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout << (x) << '\\n'
#define print_vec(v)                                                           \\
  rep(i, v.size()) {                                                           \\
    if (i != 0)                                                                \\
      cout << " ";                                                             \\
    cout << v[i];                                                              \\
  }                                                                            \\
  cout << '\\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << '\\n'
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  rep(i, v.size()) cout << " " << v[i];                                        \\
  cout << '\\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 // 10^9+7
#define fi first
#define sc second
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;

void solve() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  rep(i, N) {
    if (A[i] == 0) {
      print(0);
      return;
    }
  }
  ll max = pow(10, 18);
  ll ans = 1;
  rep(i, N) {
    if (A[i] <= max / ans) {
      ans *= A[i];
    } else {
      print(-1);
      return;
    }
  }
  print(ans);
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  // int q; cin >> q;
  // while (q--)
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout << (x) << '\\n'
#define print_vec(v)                                                           \\
  rep(i, v.size()) {                                                           \\
    if (i != 0)                                                                \\
      cout << " ";                                                             \\
    cout << v[i];                                                              \\
  }                                                                            \\
  cout << '\\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << '\\n'
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  rep(i, v.size()) cout << " " << v[i];                                        \\
  cout << '\\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 // 10^9+7
#define fi first
#define sc second
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;

void solve() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  rep(i, N) {
    if (A[i] == 0) {
      print(0);
      return;
    }
  }

  ll max = pow(10, 18);
  ll ans = 1;
  rep(i, N) {
    ans *= A[i];
    if (max / ans < 1) {
      print(-1);
      return;
    }
  }
  print(ans);
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  // int q; cin >> q;
  // while (q--)
  solve();
  return 0;
}