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

#include <bits/stdc++.h>

#define int long long
#define fast_io()                                                              \\
  ;                                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define vct vector
#define stk stack
#define qu queue
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define br break
#define con continue
#define re return
#define tc(T)                                                                  \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define q(q)                                                                   \\
  int q;                                                                       \\
  cin >> q;                                                                    \\
  while (q--)
#define subs substr
#define er erase

const int pinf = ((int)2e18);
const int ninf = ((int)-2e18);
const int mod = 1000000007;

using namespace std;

int power(int x, int n, int M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return power((x * x) % M, n / 2, M);
  else
    return (x * power((x * x) % M, (n - 1) / 2, M)) % M;
}

int fact(int n, int M) {
  int r = 1;
  loop(i, 0, n) { r = (r * (i + 1)) % M; }
  re r;
}

int cons_freq(int a[], int n, int p, int q) {
  int c = 0;
  for (int i = p; i < n; i++) {
    if (a[i] == q)
      c++;
    else
      br;
  }
  re c;
}

int binary_search(int a[], int low, int high, int key) {
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (a[mid] < key)
      low = mid + 1;
    else if (a[mid] > key)
      high = mid - 1;
    else
      re mid;
  }
  re - 1;
}

bool check_perfect_square(int n) { re(sqrt(n) - floor(sqrt(n)) == 0); }

bool check_prime(int n) {
  if (n < 2)
    re false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      re false;
  }

  re true;
}

bool check_pali(string s) {
  string str = s;
  reverse(str.begin(), str.end());

  re str == s;
}

void dfs(vct<int> adj[], bool vis[], int s) {
  vis[s] = true;
  for (int i = 0; i < adj[s].size(); i++) {
    if (!vis[adj[s][i]])
      dfs(adj, vis, adj[s][i]);
  }
}

void bfs(vct<int> adj[], bool vis[], int s) {
  qu<int> q;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < adj[v].size(); i++) {
      if (!vis[adj[v][i]]) {

        vis[adj[v][i]] = true;
        q.push(adj[v][i]);
      }
    }
  }
}

void dijkstra(vct<pair<int, int>> adj[], int src, bool vis[], int dist[],
              int n) {
  // First is distance from source vertex, second is index of the vertex
  ms<pair<int, int>> s;
  s.insert(make_pair(0, src));
  while (!s.empty()) {
    pair<int, int> p = *s.begin();
    s.erase(s.begin());
    int d = p.fi, v = p.se;
    if (vis[v])
      continue;
    vis[v] = true;
    for (int i = 0; i < adj[v].size(); i++) {
      if (dist[v] + adj[v][i].se < dist[adj[v][i].fi]) {
        dist[adj[v][i].fi] = dist[v] + adj[v][i].se;
        s.insert(mp(dist[adj[v][i].fi], adj[v][i].fi));
      }
    }
  }
}

int Kadane(int a[], int n) {
  int dp[n];
  dp[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = max(dp[i + 1] + a[i], a[i]);
  }
  re *_max(dp, dp + n);
}

int digits(int n) {
  int c = 0;
  while (n) {
    c++;
    n /= 10;
  }
  re c;
}

int setbits(int n) { re __builtin_popcount(n); }

int lcm(int a, int b) { re(a * b) / __gcd(a, b); }

int gcd(int a, int b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

int32_t main() {
  fast_io();

  // FILE *fptr1=freopen("in.txt","r",stdin);
  // FILE *fptr2=freopen("out.txt","w",stdout);

  int n;
  cin >> n;
  vector<long double> a(n);
  long double x = 1.0;
  loop(i, 0, n) { cin >> a[i]; }
  sort(all(a));
  loop(i, 0, n) {
    x = x * ((long double)a[i]);
    if (x == 0) {
      cout << "0";
      return 0;
    }
    // cout<<x<< "\\n";
    if (x > 1000000000000000000.0) {
      cout << "-1";
      re 0;
    }
  }
  int ans = (int)x;
  cout << ans;

  re 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define fast_io()                                                              \\
  ;                                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define vct vector
#define stk stack
#define qu queue
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define br break
#define con continue
#define re return
#define tc(T)                                                                  \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define q(q)                                                                   \\
  int q;                                                                       \\
  cin >> q;                                                                    \\
  while (q--)
#define subs substr
#define er erase

const int pinf = ((int)2e18);
const int ninf = ((int)-2e18);
const int mod = 1000000007;

using namespace std;

int power(int x, int n, int M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return power((x * x) % M, n / 2, M);
  else
    return (x * power((x * x) % M, (n - 1) / 2, M)) % M;
}

int fact(int n, int M) {
  int r = 1;
  loop(i, 0, n) { r = (r * (i + 1)) % M; }
  re r;
}

int cons_freq(int a[], int n, int p, int q) {
  int c = 0;
  for (int i = p; i < n; i++) {
    if (a[i] == q)
      c++;
    else
      br;
  }
  re c;
}

int binary_search(int a[], int low, int high, int key) {
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (a[mid] < key)
      low = mid + 1;
    else if (a[mid] > key)
      high = mid - 1;
    else
      re mid;
  }
  re - 1;
}

bool check_perfect_square(int n) { re(sqrt(n) - floor(sqrt(n)) == 0); }

bool check_prime(int n) {
  if (n < 2)
    re false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      re false;
  }

  re true;
}

bool check_pali(string s) {
  string str = s;
  reverse(str.begin(), str.end());

  re str == s;
}

void dfs(vct<int> adj[], bool vis[], int s) {
  vis[s] = true;
  for (int i = 0; i < adj[s].size(); i++) {
    if (!vis[adj[s][i]])
      dfs(adj, vis, adj[s][i]);
  }
}

void bfs(vct<int> adj[], bool vis[], int s) {
  qu<int> q;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < adj[v].size(); i++) {
      if (!vis[adj[v][i]]) {

        vis[adj[v][i]] = true;
        q.push(adj[v][i]);
      }
    }
  }
}

void dijkstra(vct<pair<int, int>> adj[], int src, bool vis[], int dist[],
              int n) {
  // First is distance from source vertex, second is index of the vertex
  ms<pair<int, int>> s;
  s.insert(make_pair(0, src));
  while (!s.empty()) {
    pair<int, int> p = *s.begin();
    s.erase(s.begin());
    int d = p.fi, v = p.se;
    if (vis[v])
      continue;
    vis[v] = true;
    for (int i = 0; i < adj[v].size(); i++) {
      if (dist[v] + adj[v][i].se < dist[adj[v][i].fi]) {
        dist[adj[v][i].fi] = dist[v] + adj[v][i].se;
        s.insert(mp(dist[adj[v][i].fi], adj[v][i].fi));
      }
    }
  }
}

int Kadane(int a[], int n) {
  int dp[n];
  dp[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = max(dp[i + 1] + a[i], a[i]);
  }
  re *_max(dp, dp + n);
}

int digits(int n) {
  int c = 0;
  while (n) {
    c++;
    n /= 10;
  }
  re c;
}

int setbits(int n) { re __builtin_popcount(n); }

int lcm(int a, int b) { re(a * b) / __gcd(a, b); }

int gcd(int a, int b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

int32_t main() {
  fast_io();

  FILE *fptr1 = freopen("in.txt", "r", stdin);
  // FILE *fptr2=freopen("out.txt","w",stdout);

  int n;
  cin >> n;
  vector<int> a(n);
  int p2 = 0, p5 = 0;
  loop(i, 0, n) { cin >> a[i]; }
  sort(all(a));
  int p = 1;
  loop(i, 0, n - 1) {
    p = p * a[i];
    if (p == 0) {
      cout << "0";
      re 0;
    }
    if (p > ((1000000000000000000LL) / (a[i + 1] * 1.0))) {
      cout << "-1";
      return 0;
    }
  }
  if (p > (1000000000000000000LL) / (a[n - 1] * 1.0)) {
    cout << "-1";

  } else {
    p *= a[n - 1];
    cout << p;
  }

  re 0;
}