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

/***
 **   A S M Atikur Rahman
 **   Update: 18-04-2020
 ***/

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstring>
#include <iomanip> ///setprecision
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define Filein(N) freopen("N", "r", stdin);
#define Fileout(N) freopen("N", "w", stdout);
#define sci(x) scanf("%d%*c", &(x))
#define sci2(x, y) scanf("%d%*c%d%*c", &(x), &(y))
#define sci3(x, y, z) scanf("%d%*c%d%*c%d%*c", &(x), &(y), &(z))
#define scll(x) scanf("%lld%*c", &(x))
#define scll2(x, y) scanf("%lld%*c%lld%*c", &(x), &(y))
#define scll3(x, y, z) scanf("%lld%*c%lld%*c%lld%*c", &(x), &(y), &(z))
#define scd(x) scanf("%lf%*c", &(x))
#define scd2(x, y) scanf("%lf%*c%lf%*c", &(x), &(y))
#define scstr(x) scanf("%s", x)
#define pfi(x) printf("%d", (x))
#define pfi2(x, y) printf("%d %d", (x), (y))
#define pfll(x) printf("%lld", (x))
#define pfll2(x, y) printf("%lld %lld", (x), (y))
#define pfd(x) printf("%f", (x))
#define pfstr(x) printf("%s", (x))
#define ps printf(" ")
#define pn printf("\\n")
#define pfdot printf("..")

#define For(i, a, n) for (int i = (a); i <= (n); i++)
#define rFor(i, n, a) for (int i = n; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define Ms(ara, val) memset(ara, val, sizeof(ara))
#define TestCase                                                               \\
  int testcase, cs = 1;                                                        \\
  cin >> testcase;                                                             \\
  while (testcase--)

#define All(V) V.begin(), V.end()
#define rAll(V) V.rbegin(), V.rend()

#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#define Ceil(a, b) ((a / b) + ((a % b) != 0))
#define Sq(a) ((a) * (a))

#define P(x) cout << "Check X: " << x;
#define P2(x, y) cout << "Check X,Y: " << x << " " << y;
#define P3(x, y, z) cout << "Check X,Y,Z: " << x << " " << y << " " << z;

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using pcc = pair<char, char>;
using pdd = pair<double, double>;

#define V(a) vector<a>
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vc = vector<char>;
using vd = vector<double>;

/*------------------------Bitmask--------------------------*/

// INTEGER
#define Toggle(n, i) (n ^ (1 << i))
#define Check(n, i) (n & (1 << i))
#define Set(n, i) (n | (1 << i))
#define Reset(n, i) (n & (~(1 << i)))

////LONG LONG INTEGER
// #define Toggle(n,i) (n^((1ll)<<i))
// #define Check(n,i) (n&((1ll)<<i))
// #define Set(n,i) (n|((1ll)<<i))
// #define Reset(n,i) (n&(~((1ll)<<i)))

//// Functions for Bitmask
// int Set(int &n, int p){return n= n|(1<<p);}
// ll Set(ll &n, ll p){return n= n|(1ll<<p);}
// int Reset(int &n,int p){return n= n&(~(1<<p));}
// ll Reset(ll &n,ll p){return n= n&(~(1ll<<p));}
// bool Check(int n, int p){return (bool) (n&(1<<p));}
// bool Check(ll n, ll p){return (bool) (n&(1ll<<p));}
// int Toggle(int &n, int p){return n= n^(1<<p);}
// ll Toggle(ll &n, ll p){return n= n^(1ll<<p);}

/*----------------------------------------------------------*/

/*------------------------Declarations------------------------*/

// priority_queue<int,vector<int>, greater<int>>  //priority queue sorted in
// increasing order

/*------------------------------------------------------------*/

// int POW(int a, int b) { int x=1;while(b){ if(b&1)x*=a;a*=a;b>>=1; }return x;
// } int mPOW(int a, int b, int M) { int x=1;while(b){ if(b&1)
// x=(x*a)%M;a=(a*a)%M;b>>=1; }return x; }
//
ll POW(ll a, ll b) {
  ll x = 1;
  while (b) {
    if (b & 1)
      x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}
// ll mPOW(ll a, ll b, ll M) { ll x=1;while(b){ if(b&1)
// x=(x*a)%M;a=(a*a)%M;b>>=1; }return x; } ll Sqrt(ll a) { ll x=(ll)sqrt(a);
// if((x+1)*(x+1)==a) x++; return x; }

#define INF 1000000000
#define LINF 4000000000000000000
#define pi (2 * acos(0))
#define Mod 1000000007

int lg10(ll x) {
  int a = -1;
  while (x > 0) {
    a++;
    x /= 10;
  }
  return a;
}

int main(int argc, char *argv[]) {
  ll n, x, ans = 1, fg = 1, zc = 0;
  scll(n);
  while (n--) {
    scll(x);
    if (x == 0)
      zc = 1;
    else if (lg10(ans) + lg10(x) > 18)
      fg = 0;
    else if (lg10(ans) + lg10(x) == 18) {
      ll a, b;
      if (lg10(ans) == 0)
        a = ans;
      else
        a = ans / POW(10, lg10(ans));
      if (lg10(x) == 0)
        b = x;
      else
        b = x / POW(10, lg10(x));
      if (a * b > 1) {
        fg = 0;
      }
    }

    ans *= x;
  }
  if (zc)
    pfi(0), pn;
  else if (fg && ans <= 1000000000000000000)
    pfll(ans), pn;
  else
    pfi(-1), pn;

  return 0;
}


Error: WA

Fixed submission:

/***
 **   A S M Atikur Rahman
 **   Update: 18-04-2020
 ***/

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstring>
#include <iomanip> ///setprecision
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define Filein(N) freopen("N", "r", stdin);
#define Fileout(N) freopen("N", "w", stdout);
#define sci(x) scanf("%d%*c", &(x))
#define sci2(x, y) scanf("%d%*c%d%*c", &(x), &(y))
#define sci3(x, y, z) scanf("%d%*c%d%*c%d%*c", &(x), &(y), &(z))
#define scll(x) scanf("%lld%*c", &(x))
#define scll2(x, y) scanf("%lld%*c%lld%*c", &(x), &(y))
#define scll3(x, y, z) scanf("%lld%*c%lld%*c%lld%*c", &(x), &(y), &(z))
#define scd(x) scanf("%lf%*c", &(x))
#define scd2(x, y) scanf("%lf%*c%lf%*c", &(x), &(y))
#define scstr(x) scanf("%s", x)
#define pfi(x) printf("%d", (x))
#define pfi2(x, y) printf("%d %d", (x), (y))
#define pfll(x) printf("%lld", (x))
#define pfll2(x, y) printf("%lld %lld", (x), (y))
#define pfd(x) printf("%f", (x))
#define pfstr(x) printf("%s", (x))
#define ps printf(" ")
#define pn printf("\\n")
#define pfdot printf("..")

#define For(i, a, n) for (int i = (a); i <= (n); i++)
#define rFor(i, n, a) for (int i = n; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define Ms(ara, val) memset(ara, val, sizeof(ara))
#define TestCase                                                               \\
  int testcase, cs = 1;                                                        \\
  cin >> testcase;                                                             \\
  while (testcase--)

#define All(V) V.begin(), V.end()
#define rAll(V) V.rbegin(), V.rend()

#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#define Ceil(a, b) ((a / b) + ((a % b) != 0))
#define Sq(a) ((a) * (a))

#define P(x) cout << "Check X: " << x;
#define P2(x, y) cout << "Check X,Y: " << x << " " << y;
#define P3(x, y, z) cout << "Check X,Y,Z: " << x << " " << y << " " << z;

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using pcc = pair<char, char>;
using pdd = pair<double, double>;

#define V(a) vector<a>
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vc = vector<char>;
using vd = vector<double>;

/*------------------------Bitmask--------------------------*/

// INTEGER
#define Toggle(n, i) (n ^ (1 << i))
#define Check(n, i) (n & (1 << i))
#define Set(n, i) (n | (1 << i))
#define Reset(n, i) (n & (~(1 << i)))

////LONG LONG INTEGER
// #define Toggle(n,i) (n^((1ll)<<i))
// #define Check(n,i) (n&((1ll)<<i))
// #define Set(n,i) (n|((1ll)<<i))
// #define Reset(n,i) (n&(~((1ll)<<i)))

//// Functions for Bitmask
// int Set(int &n, int p){return n= n|(1<<p);}
// ll Set(ll &n, ll p){return n= n|(1ll<<p);}
// int Reset(int &n,int p){return n= n&(~(1<<p));}
// ll Reset(ll &n,ll p){return n= n&(~(1ll<<p));}
// bool Check(int n, int p){return (bool) (n&(1<<p));}
// bool Check(ll n, ll p){return (bool) (n&(1ll<<p));}
// int Toggle(int &n, int p){return n= n^(1<<p);}
// ll Toggle(ll &n, ll p){return n= n^(1ll<<p);}

/*----------------------------------------------------------*/

/*------------------------Declarations------------------------*/

// priority_queue<int,vector<int>, greater<int>>  //priority queue sorted in
// increasing order

/*------------------------------------------------------------*/

// int POW(int a, int b) { int x=1;while(b){ if(b&1)x*=a;a*=a;b>>=1; }return x;
// } int mPOW(int a, int b, int M) { int x=1;while(b){ if(b&1)
// x=(x*a)%M;a=(a*a)%M;b>>=1; }return x; }
//
// ll POW(ll a, ll b) { ll x=1;while(b){ if(b&1)x*=a;a*=a;b>>=1; }return x; }
// ll mPOW(ll a, ll b, ll M) { ll x=1;while(b){ if(b&1)
// x=(x*a)%M;a=(a*a)%M;b>>=1; }return x; } ll Sqrt(ll a) { ll x=(ll)sqrt(a);
// if((x+1)*(x+1)==a) x++; return x; }

#define INF 1000000000
#define LINF 4000000000000000000
#define pi (2 * acos(0))
#define Mod 1000000007

int lg10(ll x) {
  int a = -1;
  while (x > 0) {
    a++;
    x /= 10;
  }
  return a;
}

int main(int argc, char *argv[]) {
  ll n, x, ans = 1, fg = 1, zc = 0;
  scll(n);
  while (n--) {
    scll(x);
    if (x == 0)
      zc = 1;
    else if (lg10(ans) + lg10(x) > 18)
      fg = 0;
    else if (lg10(ans) + lg10(x) == 18 && (ans / lg10(ans)) * (x / lg10(x)) > 1)
      fg = 0;
    ans *= x;
  }
  if (zc)
    pfi(0), pn;
  else if (fg && ans <= 1000000000000000000)
    pfll(ans), pn;
  else
    pfi(-1), pn;

  return 0;
}