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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long double arr[N] = {};
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  sort(arr + 1, arr + 1 + n);
  if (arr[1] == 0) {
    cout << "0\\n";
  } else {
    long double ans = arr[1];
    for (int i = 2; i <= n; i++) {
      if (ans > (1e18 / arr[i])) {
        ans = -1;
        break;
      } else
        ans *= arr[i];
    }
    cout << (long long)ans << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define LL long long int
#define SZ(x) x.size()
inline LL rit() {
  int key = 1, f = 0;
  char c = getchar();
  while (c > '9' && c < '0') {
    if (c == '-')
      key = -1;
    c = getchar();
  }
  while (c < '9' && c < '0') {
    f = f * 10 + c - '0';
    c = getchar();
  }
  return f * key;
}
struct Bigint {
  static const int LEN = 100; // 大數總長度
  static const int BIGMOD = 10000;
  int s;
  int vl, v[LEN];
  Bigint() : s(1) { vl = 0; }
  Bigint(long long a) {
    s = 1;
    vl = 0;
    if (a < 0) {
      s = -1;
      a = -a;
    }
    while (a) {
      push_back(a % BIGMOD);
      a /= BIGMOD;
    }
  }
  Bigint(string str) {
    s = 1;
    vl = 0;
    int stPos = 0, num = 0;
    if (!str.empty() && str[0] == '-') {
      stPos = 1;
      s = -1;
    }
    for (int i = SZ(str) - 1, q = 1; i >= stPos; i--) {
      num += (str[i] - '0') * q;
      if ((q *= 10) >= BIGMOD) {
        push_back(num);
        num = 0;
        q = 1;
      }
    }
    if (num)
      push_back(num);
    n();
  }
  int len() const { return vl; }
  bool empty() const { return len() == 0; }
  int back() const { return v[vl - 1]; }
  void push_back(int x) {
    v[vl++] = x;
    // v.PB(x);
  }
  void pop_back() {
    vl--;
    // v.pop_back();
  }
  void n() {
    while (!empty() && !back())
      pop_back();
  }
  void resize(int nl) {
    vl = nl;
    for (int i = 0; i < nl; i++) {
      v[i] = 0;
    }
  }
  void print() const {
    if (empty()) {
      putchar('0');
      return;
    }
    if (s == -1)
      putchar('-');
    printf("%d", back());
    for (int i = len() - 2; i >= 0; i--)
      printf("%.4d", v[i]);
  }
  friend std::ostream &operator<<(std::ostream &out, const Bigint &a) {
    if (a.empty()) {
      out << "0";
      return out;
    }
    if (a.s == -1)
      out << "-";
    out << a.back();
    for (int i = a.len() - 2; i >= 0; i--) {
      char str[10];
      snprintf(str, 5, "%.4d", a.v[i]);
      out << str;
    }
    return out;
  }
  int cp3(const Bigint &b) const {
    if (s != b.s)
      return s - b.s;
    if (s == -1)
      return -(-*this).cp3(-b);
    if (len() != b.len())
      return len() - b.len(); // int
    for (int i = len() - 1; i >= 0; i--)
      if (v[i] != b.v[i])
        return v[i] - b.v[i];
    return 0;
  }
  bool operator<(const Bigint &b) const { return cp3(b) < 0; }
  bool operator<=(const Bigint &b) const { return cp3(b) <= 0; }
  bool operator==(const Bigint &b) const { return cp3(b) == 0; }
  bool operator!=(const Bigint &b) const { return cp3(b) != 0; }
  bool operator>(const Bigint &b) const { return cp3(b) > 0; }
  bool operator>=(const Bigint &b) const { return cp3(b) >= 0; }
  Bigint operator-() const {
    Bigint r = (*this);
    r.s = -r.s;
    return r;
  }
  Bigint operator+(const Bigint &b) const {
    if (s == -1)
      return -(-(*this) + (-b));
    if (b.s == -1)
      return (*this) - (-b);
    Bigint r;
    int nl = max(len(), b.len());
    r.resize(nl + 1);
    for (int i = 0; i < nl; i++) {
      if (i < len())
        r.v[i] += v[i];
      if (i < b.len())
        r.v[i] += b.v[i];
      if (r.v[i] >= BIGMOD) {
        r.v[i + 1] += r.v[i] / BIGMOD;
        r.v[i] %= BIGMOD;
      }
    }
    r.n();
    return r;
  }
  Bigint operator-(const Bigint &b) const {
    if (s == -1)
      return -(-(*this) - (-b));
    if (b.s == -1)
      return (*this) + (-b);
    if ((*this) < b)
      return -(b - (*this));
    Bigint r;
    r.resize(len());
    for (int i = 0; i < len(); i++) {
      r.v[i] += v[i];
      if (i < b.len())
        r.v[i] -= b.v[i];
      if (r.v[i] < 0) {
        r.v[i] += BIGMOD;
        r.v[i + 1]--;
      }
    }
    r.n();
    return r;
  }
  Bigint operator*(const Bigint &b) {
    Bigint r;
    r.resize(len() + b.len() + 1);
    r.s = s * b.s;
    for (int i = 0; i < len(); i++) {
      for (int j = 0; j < b.len(); j++) {
        r.v[i + j] += v[i] * b.v[j];
        if (r.v[i + j] >= BIGMOD) {
          r.v[i + j + 1] += r.v[i + j] / BIGMOD;
          r.v[i + j] %= BIGMOD;
        }
      }
    }
    r.n();
    return r;
  }
  Bigint operator/(const Bigint &b) {
    Bigint r;
    r.resize(max(1, len() - b.len() + 1));
    int oriS = s;
    Bigint b2 = b; // b2 = abs(b)
    s = b2.s = r.s = 1;
    for (int i = r.len() - 1; i >= 0; i--) {
      int d = 0, u = BIGMOD - 1;
      while (d < u) {
        int m = (d + u + 1) >> 1;
        r.v[i] = m;
        if ((r * b2) > (*this))
          u = m - 1;
        else
          d = m;
      }
      r.v[i] = d;
    }
    s = oriS;
    r.s = s * b.s;
    r.n();
    return r;
  }
  Bigint operator%(const Bigint &b) { return (*this) - (*this) / b * b; }
};
inline Bigint gcd(Bigint a, Bigint b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  Bigint ans("1");
  string x;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    ans = ans * Bigint(x);
  }
  if (ans > Bigint("1000000000000000000"))
    cout << "-1\\n";
  else
    cout << ans << '\\n';
  return 0;
}