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 std;
using namespace __gnu_pbds;
#define faster ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define ld long double
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define bins(v, x) binary_search(v.begin(), v.end(), x)
#define pi 2 * acos(0.0)
const int INF = 1e7;
const int mod = 1e9 + 7;

ll modExp(ll base, ll exp, ll mod) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      res = (res * base) % mod;
    }
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}

ll nCr(ll n, ll k) {
  ll C[n + 1][k + 1];

  REP(i, 0, n + 1) {
    REP(j, 0, min(i, k) + 1) {
      if (j == 0 || j == i) {
        C[i][j] = 1;
      } else {
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
      }
    }
  }
  return C[n][k];
}

bool compare(pair<int, int> &i, pair<int, int> &j) {
  return (i.S > j.S || (i.S == j.S && i.F > j.F));
}

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  int n;
  cin >> n;
  ld max = pow(10, 18);
  ll a[n], prod = 1;
  REP(i, 0, n) { cin >> a[i]; }

  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    if (a[i] > max / prod) {
      p1(-1);
      return 0;
    }
    prod *= a[i];
  }
  p1(prod);
}

Error: 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;
#define faster ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define ld long double
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define bins(v, x) binary_search(v.begin(), v.end(), x)
#define pi 2 * acos(0.0)
const int INF = 1e7;
const int mod = 1e9 + 7;

ll modExp(ll base, ll exp, ll mod) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      res = (res * base) % mod;
    }
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}

ll nCr(ll n, ll k) {
  ll C[n + 1][k + 1];

  REP(i, 0, n + 1) {
    REP(j, 0, min(i, k) + 1) {
      if (j == 0 || j == i) {
        C[i][j] = 1;
      } else {
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
      }
    }
  }
  return C[n][k];
}

bool compare(pair<int, int> &i, pair<int, int> &j) {
  return (i.S > j.S || (i.S == j.S && i.F > j.F));
}

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  int n;
  cin >> n;

  ll prod = 1;
  ll a[n];
  REP(i, 0, n) {
    cin >> a[i];
    if (a[i] > (ll)pow(10, 18) / a[i]) {
      p1(-1);
      return 0;
    } else {
      prod *= a[i];
    }
  }
  p1(prod);
}