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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
// #include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i, x, n) for (int i = x; i < n; i++)
#define repd(i, x, n) for (int i = x; i <= n; i++)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define gbtb ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)                                                                 \\
  if (DEBUG_Switch)                                                            \\
  cout << "[" << #x << " " << (x) << "]" << endl
#define du3(a, b, c) scanf("%d %d %d", &(a), &(b), &(c))
#define du2(a, b) scanf("%d %d", &(a), &(b))
#define du1(a) scanf("%d", &(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll powmod(ll a, ll b, ll MOD) {
  if (a == 0ll) {
    return 0ll;
  }
  a %= MOD;
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
ll poww(ll a, ll b) {
  if (a == 0ll) {
    return 0ll;
  }
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
void Pv(const vector<int> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%d", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
void Pvl(const vector<ll> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%lld", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
inline long long readll() {
  long long tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
inline int readint() {
  int tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
void pvarr_int(int *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%d%c", arr[i], i == n ? '\\n' : ' '); }
}
void pvarr_LL(ll *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%lld%c", arr[i], i == n ? '\\n' : ' '); }
}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0

class BigNum {
public:
  static const int MOD = 100000000;
  static const int BIT = 8, SIZE = 105;
  mutable int n, o;
  long long u[SIZE];
  BigNum() {}
  BigNum(const string &s) {
    memset(this, 0, sizeof(BigNum));
    int num = 0, cnt = 1;
    for (int i = s.size() - 1; ~i; i--) {
      if (s[i] == '-')
        o ^= 1;
      if (s[i] >= '0' && s[i] <= '9') {
        num += (s[i] - '0') * cnt;
        cnt *= 10;
        if (cnt == MOD)
          u[n++] = num, num = 0, cnt = 1;
      }
    }
    if (!n || cnt >= 10)
      u[n++] = num;
    if (!u[0] && n == 1)
      o = 0;
  }
  BigNum(long long x) {
    memset(this, 0, sizeof(BigNum));
    if (x < 0)
      o = 1, x = -x;
    do
      u[n++] = x % MOD;
    while (x /= MOD);
  }
  operator string() const {
    static char s[SIZE * BIT + 10];
    char *c = s + sprintf(s, "%s%d", o ? "-" : "", int(u[n - 1]));
    for (int i = n - 2; ~i; i--)
      c += sprintf(c, "%0*d", BIT, int(u[i]));
    return s;
  }
  int operator[](int pos) const {
    static int e[BIT] = {1};
    for (static int i = 1; i < BIT; i++)
      e[i] = e[i - 1] * 10;
    return u[pos / BIT] / e[pos % BIT] % 10;
  }
  int length() const {
    int ret = (n - 1) * BIT + 1;
    for (int x = u[n - 1] / 10; x; x /= 10)
      ret++;
    return ret;
  }
  friend int cmp(const BigNum &l, const BigNum &r) {
    if (l.o != r.o)
      return (l.o ? -1 : 1);
    if (l.n != r.n)
      return (l.o ? -1 : 1) * (l.n - r.n);
    for (int i = l.n - 1; ~i; i--)
      if (l.u[i] - r.u[i])
        return (l.o ? -1 : 1) * (l.u[i] - r.u[i]);
    return 0;
  }
  // 运算符
  bool operator<(const BigNum &r) const { return cmp(*this, r) < 0; }
  bool operator>(const BigNum &r) const { return cmp(*this, r) > 0; }
  bool operator<=(const BigNum &r) const { return cmp(*this, r) <= 0; }
  bool operator>=(const BigNum &r) const { return cmp(*this, r) >= 0; }
  bool operator==(const BigNum &r) const { return cmp(*this, r) == 0; }
  bool operator!=(const BigNum &r) const { return cmp(*this, r) != 0; }
  BigNum operator+(const BigNum &r) const { return BigNum(*this) += r; }
  BigNum operator-(const BigNum &r) const { return BigNum(*this) -= r; }
  BigNum operator*(int x) const { return BigNum(*this) *= x; }
  BigNum operator/(int x) const { return BigNum(*this) /= x; }
  BigNum &operator*=(const BigNum &r) { return *this = *this * r; }
  BigNum &operator/=(const BigNum &r) { return *this = *this / r; }
  BigNum &operator%=(const BigNum &r) { return *this = *this % r; }
  BigNum &operator%=(int x) { return *this = *this % x; }
  BigNum operator-() const {
    BigNum s = *this;
    if (s.u[0] || s.n >= 2)
      s.o ^= 1;
    return s;
  }
  BigNum &operator+=(const BigNum &r) {
    if (r.n == 1 && !r.u[0])
      return *this;
    if (r.o ^ o)
      return r.o ^= 1, *this -= r, r.o ^= 1, *this;
    if (r.n > n)
      n = r.n;
    for (int i = 0; i < r.n; i++)
      u[i] += r.u[i];
    for (int i = 0; i < n; i++)
      if (u[i] >= MOD)
        u[i + 1]++, u[i] -= MOD;
    if (u[n])
      n++;
    return *this;
  }
  BigNum &operator-=(const BigNum &r) {
    if (r.n == 1 && !r.u[0])
      return *this;
    if (r.o ^ o)
      return r.o ^= 1, *this += r, r.o ^= 1, *this;
    if (cmp(*this, r) * (r.o ? -1 : 1) < 0) {
      o ^= 1, n = r.n;
      for (int i = 0; i < r.n; i++)
        u[i] = r.u[i] - u[i];
    } else {
      for (int i = 0; i < r.n; i++)
        u[i] = u[i] - r.u[i];
    }
    for (int i = 0; i < n; i++)
      if (u[i] < 0)
        u[i + 1]--, u[i] += MOD;
    while (!u[n - 1] && n >= 2)
      --n;
    if (!u[0] && n == 1)
      o = 0;
    return *this;
  }
  BigNum operator*(const BigNum &r) const {
    BigNum s = 0;
    if (!u[n - 1] || !r.u[r.n - 1])
      return s;
    s.n = r.n + n - 1;
    s.o = r.o ^ o;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < r.n; j++)
        s.u[i + j] += u[i] * r.u[j];
    for (int i = 0; i < s.n; i++)
      if (s.u[i] >= MOD) {
        s.u[i + 1] += s.u[i] / MOD;
        s.u[i] %= MOD;
        if (i == s.n - 1)
          s.n++;
      }
    return s;
  }
  BigNum operator/(const BigNum &r) const {
    BigNum e[35], s = 0, c = 0;
    int m = 0, ro = r.o, lo = o;
    r.o ^= ro, o ^= lo;
    for (e[m] = r; MOD >> ++m; e[m] = e[m - 1] + e[m - 1])
      ;
    for (int i = n - 1; ~i; i--) {
      int tag = 0;
      (s *= MOD) += u[i];
      for (int x = m - 1; ~x; x--)
        if (s >= e[x])
          s -= e[x], tag |= 1 << x;
      (c *= MOD) += tag;
    }
    r.o ^= ro, o ^= lo;
    if (c.u[0] || c.n >= 2)
      c.o = r.o ^ o;
    return c;
  }
  BigNum operator%(const BigNum &r) const {
    BigNum e[35], s = 0;
    int m = 0, ro = r.o, lo = o;
    r.o ^= ro, o ^= lo;
    for (e[m] = r; MOD >> ++m; e[m] = e[m - 1] + e[m - 1])
      ;
    for (int i = n - 1; ~i; i--) {
      (s *= MOD) += u[i];
      for (int x = m - 1; ~x; x--)
        if (s >= e[x])
          s -= e[x];
    }
    r.o ^= ro, o ^= lo;
    if (s.u[0] || s.n >= 2)
      s.o = o;
    return s;
  }
  BigNum &operator*=(int x) {
    if (!x)
      return *this = 0;
    if (x < 0)
      o ^= 1, x = -x;
    for (int i = 0; i < n; i++)
      u[i] *= x;
    for (int i = 0; i < n; i++)
      if (u[i] >= MOD) {
        u[i + 1] += u[i] / MOD;
        u[i] %= MOD;
        if (i == n - 1)
          n++;
      }
    if (!u[0] && n == 1)
      o = 0;
    return *this;
  }
  BigNum &operator/=(int x) {
    if (x < 0)
      o ^= 1, x = -x;
    for (int i = n - 1; i; u[i--] /= x)
      u[i - 1] += u[i] % x * MOD;
    for (u[0] /= x; n >= 2; n--)
      if (u[n - 1])
        break;
    if (!u[0] && n == 1)
      o = 0;
    return *this;
  }
  int operator%(int x) const {
    long long c = 0;
    for (int i = n - 1; ~i; i--)
      c = (c * MOD + u[i]) % x;
    return (1 - o - o) * int(c);
  }
};
BigNum gcd(BigNum x, BigNum y) {
  while (y != BigNum(0))
    swap(x %= y, y);
  return x;
}

int n;
ll a[maxn];
BigNum x;
int main() {
#if DEBUG_Switch
  freopen("C:\\\\code\\\\input.txt", "r", stdin);
#endif
  // freopen("C:\\\\code\\\\output.txt","r",stdin);
  cin >> n;
  repd(i, 1, n) { cin >> a[i]; }
  x = 1;
  repd(i, 1, n) {
    if (a[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  repd(i, 1, n) {
    x *= (BigNum)(a[i]);
    if (x > 1000000000000000000ll) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << (string)(x) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
// #include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i, x, n) for (int i = x; i < n; i++)
#define repd(i, x, n) for (int i = x; i <= n; i++)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define gbtb ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)                                                                 \\
  if (DEBUG_Switch)                                                            \\
  cout << "[" << #x << " " << (x) << "]" << endl
#define du3(a, b, c) scanf("%d %d %d", &(a), &(b), &(c))
#define du2(a, b) scanf("%d %d", &(a), &(b))
#define du1(a) scanf("%d", &(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll powmod(ll a, ll b, ll MOD) {
  if (a == 0ll) {
    return 0ll;
  }
  a %= MOD;
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
ll poww(ll a, ll b) {
  if (a == 0ll) {
    return 0ll;
  }
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
void Pv(const vector<int> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%d", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
void Pvl(const vector<ll> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%lld", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
inline long long readll() {
  long long tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
inline int readint() {
  int tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
void pvarr_int(int *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%d%c", arr[i], i == n ? '\\n' : ' '); }
}
void pvarr_LL(ll *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%lld%c", arr[i], i == n ? '\\n' : ' '); }
}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
const int MAXN = 150;
struct bign {
  int len, s[MAXN];
  bign() {
    memset(s, 0, sizeof(s));
    len = 1;
  }
  bign(int num) { *this = num; }
  bign(const char *num) { *this = num; }
  bign operator=(const int num) {
    char s[MAXN];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
  }
  bign operator=(const char *num) {
    for (int i = 0; num[i] == '0'; num++)
      ; // 去前导0
    len = strlen(num);
    for (int i = 0; i < len; i++)
      s[i] = num[len - i - 1] - '0';
    return *this;
  }
  bign operator+(const bign &b) const //+
  {
    bign c;
    c.len = 0;
    for (int i = 0, g = 0; g || i < max(len, b.len); i++) {
      int x = g;
      if (i < len)
        x += s[i];
      if (i < b.len)
        x += b.s[i];
      c.s[c.len++] = x % 10;
      g = x / 10;
    }
    return c;
  }
  bign operator+=(const bign &b) {
    *this = *this + b;
    return *this;
  }
  void clean() {
    while (len > 1 && !s[len - 1])
      len--;
  }
  bign operator*(const bign &b) //*
  {
    bign c;
    c.len = len + b.len;
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < b.len; j++) {
        c.s[i + j] += s[i] * b.s[j];
      }
    }
    for (int i = 0; i < c.len; i++) {
      c.s[i + 1] += c.s[i] / 10;
      c.s[i] %= 10;
    }
    c.clean();
    return c;
  }
  bign operator*=(const bign &b) {
    *this = *this * b;
    return *this;
  }
  bign operator-(const bign &b) {
    bign c;
    c.len = 0;
    for (int i = 0, g = 0; i < len; i++) {
      int x = s[i] - g;
      if (i < b.len)
        x -= b.s[i];
      if (x >= 0)
        g = 0;
      else {
        g = 1;
        x += 10;
      }
      c.s[c.len++] = x;
    }
    c.clean();
    return c;
  }
  bign operator-=(const bign &b) {
    *this = *this - b;
    return *this;
  }
  bign operator/(const bign &b) {
    bign c, f = 0;
    for (int i = len - 1; i >= 0; i--) {
      f = f * 10;
      f.s[0] = s[i];
      while (f >= b) {
        f -= b;
        c.s[i]++;
      }
    }
    c.len = len;
    c.clean();
    return c;
  }
  bign operator/=(const bign &b) {
    *this = *this / b;
    return *this;
  }
  bign operator%(const bign &b) {
    bign r = *this / b;
    r = *this - r * b;
    return r;
  }
  bign operator%=(const bign &b) {
    *this = *this % b;
    return *this;
  }
  bool operator<(const bign &b) {
    if (len != b.len)
      return len < b.len;
    for (int i = len - 1; i >= 0; i--) {
      if (s[i] != b.s[i])
        return s[i] < b.s[i];
    }
    return false;
  }
  bool operator>(const bign &b) {
    if (len != b.len)
      return len > b.len;
    for (int i = len - 1; i >= 0; i--) {
      if (s[i] != b.s[i])
        return s[i] > b.s[i];
    }
    return false;
  }
  bool operator==(const bign &b) { return !(*this > b) && !(*this < b); }
  bool operator!=(const bign &b) { return !(*this == b); }
  bool operator<=(const bign &b) { return *this < b || *this == b; }
  bool operator>=(const bign &b) { return *this > b || *this == b; }
  string str() const {
    string res = "";
    for (int i = 0; i < len; i++)
      res = char(s[i] + '0') + res;
    return res;
  }
} a, b;
istream &operator>>(istream &in, bign &x) {
  string s;
  in >> s;
  x = s.c_str();
  return in;
}
ostream &operator<<(ostream &out, const bign &x) {
  if (x.str() == "")
    out << 0;
  else
    out << x.str();
  return out;
}

int main() {
#if DEBUG_Switch
  freopen("C:\\\\code\\\\input.txt", "r", stdin);
#endif
  // freopen("C:\\\\code\\\\output.txt","r",stdin);
  cin >> n;
  cin >> a;
  repd(i, 1, n - 1) {
    cin >> b;
    a *= b;
  }
  bign c = "1000000000000000000";
  if (a > c) {
    cout << -1 << endl;
  } else {
    cout << a << endl;
  }
  return 0;
}