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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ms(a, x) memset(a, x, sizeof(a))
#define fore(i, a, n) for (ll ll i = a; i < n; i++)
#define ford(i, a, n) for (ll ll i = n - 1; i >= a; i--)
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define debug(a) cout << a << endl
#define pr(a) printf("%d ", a)
#define endl '\\n'
#define pi acos(-1.0)
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define IO ios::sync_with_stdio(false), cin.tie(0)
#define ull unsigned ll ll
#define py puts("Yes")
#define pn puts("No")
#define pY puts("YES")
#define pN puts("NO")
#define re(i, a, b) for (int i = a; i <= b; ++i)
#define de(i, a, b) for (int i = a; i >= b; --i)
const double eps = 1e-8;
inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;
template <class t> inline void cmin(t &a, t b) { ((a > b) && (a = b)); }
template <class t> inline void cmax(t &a, t b) { ((a < b) && (a = b)); }
const ll V = 1e6 + 5;
const ll E = 4e6 + 5;
const ll mode = 1e9 + 7;
ll weight[E], to[E], nex[E];
ll cnt = 0;
ll n, m, dis[V];
ll head[V];
bool vis[V];
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll multi(ll a, ll b, ll p) { // 快速乘法
  ll ret = 0;
  while (b) {
    if (b & 1)
      ret = (ret + a) % p;
    a = (a + a) % p;
    b >>= 1;
  }
  return ret;
}

ll Pow(ll a, ll b, ll mode) {
  ll sum = 1;
  a = a % mode;
  while (b > 0) {
    if (b % 2 == 1) // 判断是否是奇数，是奇数的话将多出来的数事先乘如sum
      sum = sum * a % mode;
    b >>= 1;
    a = a * a % mode; // 不断的两两合并再取模，减小a和b的规模
  }
  return sum;
}
void add(ll u, ll v, ll w) {
  cnt++;
  to[cnt] = v;
  weight[cnt] = w;
  nex[cnt] = head[u];
  head[u] = cnt;
}
void init() {
  cnt = 0;
  memset(head, -1, sizeof(head));
}
void SPFA(ll s) {
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[s] = 0;
  vis[s] = 1;
  queue<ll> q;
  q.push(s);

  while (!q.empty()) {
    ll now = q.front();

    q.pop();
    vis[now] = 0;
    for (int i = head[now]; i != -1; i = nex[i]) {

      if (dis[to[i]] > dis[now] + weight[i]) {
        dis[to[i]] = dis[now] + weight[i];
        if (!vis[to[i]]) {
          q.push(to[i]);
          vis[to[i]] = 1;
        }
      }
    }
  }
}
const ll tem = 1e18;
ll t[MAXN];
int main() {
  int n;
  si(n);
  ll sum = 1;
  double ans = 1;
  bool flag = 0;
  re(i, 1, n) sl(t[i]);
  sort(t + 1, t + 1 + n);
  re(i, 1, n) {
    sum *= t[i];
    ans *= (double)t[i];
    if (ans != sum || sum > tem) {
      puts("-1");
      return 0;
    }
  }
  cout << sum << endl;
}


Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

const int maxn = 10005;

struct bign {
  int d[maxn], len;

  void clean() {
    while (len > 1 && !d[len - 1])
      len--;
  }

  bign() {
    memset(d, 0, sizeof(d));
    len = 1;
  }
  bign(int num) { *this = num; }
  bign(char *num) { *this = num; }
  bign operator=(const char *num) {
    memset(d, 0, sizeof(d));
    len = strlen(num);
    for (int i = 0; i < len; i++)
      d[i] = num[len - 1 - i] - '0';
    clean();
    return *this;
  }
  bign operator=(int num) {
    char s[20];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
  }

  bign operator+(const bign &b) {
    bign c = *this;
    int i;
    for (i = 0; i < b.len; i++) {
      c.d[i] += b.d[i];
      if (c.d[i] > 9)
        c.d[i] %= 10, c.d[i + 1]++;
    }
    while (c.d[i] > 9)
      c.d[i++] %= 10, c.d[i]++;
    c.len = max(len, b.len);
    if (c.d[i] && c.len <= i)
      c.len = i + 1;
    return c;
  }
  bign operator-(const bign &b) {
    bign c = *this;
    int i;
    for (i = 0; i < b.len; i++) {
      c.d[i] -= b.d[i];
      if (c.d[i] < 0)
        c.d[i] += 10, c.d[i + 1]--;
    }
    while (c.d[i] < 0)
      c.d[i++] += 10, c.d[i]--;
    c.clean();
    return c;
  }
  bign operator*(const bign &b) const {
    int i, j;
    bign c;
    c.len = len + b.len;
    for (j = 0; j < b.len; j++)
      for (i = 0; i < len; i++)
        c.d[i + j] += d[i] * b.d[j];
    for (i = 0; i < c.len - 1; i++)
      c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;
    c.clean();
    return c;
  }
  bign operator/(const bign &b) {
    int i, j;
    bign c = *this, a = 0;
    for (i = len - 1; i >= 0; i--) {
      a = a * 10 + d[i];
      for (j = 0; j < 10; j++)
        if (a < b * (j + 1))
          break;
      c.d[i] = j;
      a = a - b * j;
    }
    c.clean();
    return c;
  }
  bign operator%(const bign &b) {
    int i, j;
    bign a = 0;
    for (i = len - 1; i >= 0; i--) {
      a = a * 10 + d[i];
      for (j = 0; j < 10; j++)
        if (a < b * (j + 1))
          break;
      a = a - b * j;
    }
    return a;
  }
  bign operator+=(const bign &b) {
    *this = *this + b;
    return *this;
  }

  bool operator<(const bign &b) const {
    if (len != b.len)
      return len < b.len;
    for (int i = len - 1; i >= 0; i--)
      if (d[i] != b.d[i])
        return d[i] < b.d[i];
    return false;
  }
  bool operator>(const bign &b) const { return b < *this; }
  bool operator<=(const bign &b) const { return !(b < *this); }
  bool operator>=(const bign &b) const { return !(*this < b); }
  bool operator!=(const bign &b) const { return b < *this || *this < b; }
  bool operator==(const bign &b) const { return !(b < *this) && !(b > *this); }

  string str() const {
    char s[maxn] = {};
    for (int i = 0; i < len; i++)
      s[len - 1 - i] = d[i] + '0';
    return s;
  }
  friend bool judge(bign a) {
    bign x(0);
    if (a == x)
      return true;
    else
      return false;
  }
  friend bign gcd(bign a, bign b) // 姹傛渶澶у叕绾︽暟
  {
    bign t;
    while (!judge(b)) {
      t = a;
      a = b;
      b = t % b;
    }
    return a;
  }
};

istream &operator>>(istream &in, bign &x) {
  string s;
  in >> s;
  x = s.c_str();
  return in;
}

ostream &operator<<(ostream &out, const bign &x) {
  out << x.str();
  return out;
}
int n;
int main() {

  bign c(1000000000);
  c = c * c;
  bign ans(1);
  cin >> n;
  while (n--) {
    bign x;
    cin >> x;
    ans = ans * x;
  }
  // cout<<ans<<endl;
  if (ans > c) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}