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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"

#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

template <typename T> T gcd(T a, T b) {
  while (b != 0) {
    T temp = a;
    a = b;
    b = temp % b;
  }

  return a;
}

// cout << setfill('0') << setw(5) << number < endl;
// while(getline(cin, s)) { .. } | `s` is a string

void solve() {
  int n;
  cin >> n;
  std::vector<ull> a(n);
  ull sum = 1, limit = 1e18;
  bool flag = true;
  for (auto &it : a) {
    cin >> it;
  }

  sort(a.rbegin(), a.rend());
  if (a.back() == 0) {
    cout << 0 << endl;
    return;
  }
  sum = a.back();
  for (int i = a.size() - 2; i >= 0; --i) {
    if (a[i] > limit / sum) {
      flag = false;
      break;
    } else {
      sum *= a[i];
    }
  }

  if (flag) {
    cout << sum << endl;
  } else {
    cout << -1 << endl;
  }
}

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"

#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

template <typename T> T gcd(T a, T b) {
  while (b != 0) {
    T temp = a;
    a = b;
    b = temp % b;
  }

  return a;
}

// cout << setfill('0') << setw(5) << number < endl;
// while(getline(cin, s)) { .. } | `s` is a string

void solve() {
  int n;
  cin >> n;
  std::vector<ull> a(n);
  ull sum = 1, limit = 1e18;
  bool flag = true;
  for (auto &it : a) {
    cin >> it;
  }

  sort(a.rbegin(), a.rend());
  sum = a.back();
  for (int i = a.size() - 2; i >= 0; --i) {
    if (a[i] > limit / sum) {
      flag = false;
      break;
    } else {
      sum *= a[i];
    }
  }

  if (flag) {
    cout << sum << endl;
  } else {
    cout << -1 << endl;
  }
}

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}