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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
long long M = 1e9 + 7;
long long power(long long a, long long p) {
  if (p == 0)
    return 1;
  long long ans = power(a, p / 2);
  ans = (ans * ans) % M;
  if (p % 2)
    ans = (ans * a) % M;
  return ans;
}

long long SieveOfEratosthenes(long long n, long long a) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (long long p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (long long p = 2; p <= n; p++)
    if (prime[p]) {
      if (a % p == 0) {
        return (a - p);
      }
    }
}
long long nCr(int n, int r) {
  if (r > n - r)
    r = n - r; // because C(n, r) == C(n, n - r)
  long long ans = 1;
  int i;
  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }
  return ans;
}

// int vis[1001][1001];
// int x[]={1,0};
// int y[]={0,-1};
/*int dfs_visit(long long r, long long c)
{
   int flag=0;
   vis[r][c]=1;
   for(int i=0; i<2; i++)
   {

       int p=r+x[i];
       int q=c+y[i];
       if(p==(a-1) && q==(b-1))
       {
           flag=1;
           break;
       }
       if(p>=0 && p<a && q>=0 && q<b && vis[p][q]==0 && v[p][q]!='#')
       {
          // cout<<p<<" "<<q<<endl;

           vis[p][q]=1;
           dfs_visit(p,q);

       }
   }
   if(flag==1)
   {
       return 2;
   }
   else
   {
       return 3;
   }

}
*/

long long fun(long long abc) {
  long long coo;
  while (abc > 0) {
    if (abc % 2 != 0) {
      coo++;
    }
    abc /= 2;
  }
  return coo;
}
#define mx 100005
int arr[mx];
int tree[mx * 4];
void init(int node, int b, int e) {
  if (b == e) {
    tree[node] = arr[b];
    return;
  }
  int Left = node * 2;
  int Right = node * 2 + 1;
  int mid = (b + e) / 2;
  init(Left, b, mid);
  init(Right, mid + 1, e);
  tree[node] = tree[Left] + tree[Right];
}
int query(int node, int b, int e, int i, int j) {
  if (i > e || j < b)
    return 0; // বাইরে চলে গিয়েছে
  if (b >= i && e <= j)
    return tree[node]; // রিলেভেন্ট সেগমেন্ট
  int Left = node * 2; // আরো ভাঙতে হবে
  int Right = node * 2 + 1;
  int mid = (b + e) / 2;
  int p1 = query(Left, b, mid, i, j);
  int p2 = query(Right, mid + 1, e, i, j);
  return p1 + p2; // বাম এবং ডান পাশের যোগফল
}
void update(int node, int b, int e, int i, int newvalue) {
  if (i > e || i < b)
    return;               // বাইরে চলে গিয়েছে
  if (b >= i && e <= i) { // রিলেভেন্ট সেগমেন্ট
    tree[node] = newvalue;
    return;
  }
  int Left = node * 2; // আরো ভাঙতে হবে
  int Right = node * 2 + 1;
  int mid = (b + e) / 2;
  update(Left, b, mid, i, newvalue);
  update(Right, mid + 1, e, i, newvalue);
  tree[node] = tree[Left] + tree[Right];
}

int main() {
  IOS;
  long long a, b, c, d, e, h, l, n, t, f, m, x, y, z,
      f1 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, sum = 0, sum1 = 0,
      ma = 0, ma1 = 0, mi = 1000000000000000001;
  long long g, co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0, mul = 1,
               sum2 = 0, sum3 = 0, mu = 1, mu1 = 1;
  long long a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, b6;
  double pi = 2 * acos(0.0);
  string str, str1, str2, str3, str4, str5, str6;
  char ch, ch1;
  // int x[]={1,1,1,0,0,-1,-1,-1};
  // int y[]={0,1,-1,1,-1,1,-1,0};
  // int x[]={1,0,0,-1};
  // int y[]={0,1,-1,0};
  b = 1e18;
  cin >> n;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a == 0) {
      flag1 = 1;
    }
    v.push_back(a);
  }
  if (flag1 == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (v[i] <= (b / mul)) {
      mul *= v[i];
    } else {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    cout << mul << endl;
  } else
    cout << -1 << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
long long M = 1e9 + 7;
long long power(long long a, long long p) {
  if (p == 0)
    return 1;
  long long ans = power(a, p / 2);
  ans = (ans * ans) % M;
  if (p % 2)
    ans = (ans * a) % M;
  return ans;
}

long long SieveOfEratosthenes(long long n, long long a) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (long long p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (long long p = 2; p <= n; p++)
    if (prime[p]) {
      if (a % p == 0) {
        return (a - p);
      }
    }
}
long long nCr(int n, int r) {
  if (r > n - r)
    r = n - r; // because C(n, r) == C(n, n - r)
  long long ans = 1;
  int i;
  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }
  return ans;
}

// int vis[1001][1001];
// int x[]={1,0};
// int y[]={0,-1};
/*int dfs_visit(long long r, long long c)
{
   int flag=0;
   vis[r][c]=1;
   for(int i=0; i<2; i++)
   {

       int p=r+x[i];
       int q=c+y[i];
       if(p==(a-1) && q==(b-1))
       {
           flag=1;
           break;
       }
       if(p>=0 && p<a && q>=0 && q<b && vis[p][q]==0 && v[p][q]!='#')
       {
          // cout<<p<<" "<<q<<endl;

           vis[p][q]=1;
           dfs_visit(p,q);

       }
   }
   if(flag==1)
   {
       return 2;
   }
   else
   {
       return 3;
   }

}
*/

long long fun(long long abc) {
  long long coo;
  while (abc > 0) {
    if (abc % 2 != 0) {
      coo++;
    }
    abc /= 2;
  }
  return coo;
}
#define mx 100005
int arr[mx];
int tree[mx * 4];
void init(int node, int b, int e) {
  if (b == e) {
    tree[node] = arr[b];
    return;
  }
  int Left = node * 2;
  int Right = node * 2 + 1;
  int mid = (b + e) / 2;
  init(Left, b, mid);
  init(Right, mid + 1, e);
  tree[node] = tree[Left] + tree[Right];
}
int query(int node, int b, int e, int i, int j) {
  if (i > e || j < b)
    return 0; // বাইরে চলে গিয়েছে
  if (b >= i && e <= j)
    return tree[node]; // রিলেভেন্ট সেগমেন্ট
  int Left = node * 2; // আরো ভাঙতে হবে
  int Right = node * 2 + 1;
  int mid = (b + e) / 2;
  int p1 = query(Left, b, mid, i, j);
  int p2 = query(Right, mid + 1, e, i, j);
  return p1 + p2; // বাম এবং ডান পাশের যোগফল
}
void update(int node, int b, int e, int i, int newvalue) {
  if (i > e || i < b)
    return;               // বাইরে চলে গিয়েছে
  if (b >= i && e <= i) { // রিলেভেন্ট সেগমেন্ট
    tree[node] = newvalue;
    return;
  }
  int Left = node * 2; // আরো ভাঙতে হবে
  int Right = node * 2 + 1;
  int mid = (b + e) / 2;
  update(Left, b, mid, i, newvalue);
  update(Right, mid + 1, e, i, newvalue);
  tree[node] = tree[Left] + tree[Right];
}

int main() {
  IOS;
  long long a, b, c, d, e, h, l, n, t, f, m, x, y, z,
      f1 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, sum = 0, sum1 = 0,
      ma = 0, ma1 = 0, mi = 1000000000000000001;
  long long g, co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0, mul = 1,
               sum2 = 0, sum3 = 0, mu = 1, mu1 = 1;
  long long a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, b6;
  double pi = 2 * acos(0.0);
  string str, str1, str2, str3, str4, str5, str6;
  char ch, ch1;
  // int x[]={1,1,1,0,0,-1,-1,-1};
  // int y[]={0,1,-1,1,-1,1,-1,0};
  // int x[]={1,0,0,-1};
  // int y[]={0,1,-1,0};
  b = 1e18;
  cin >> n;
  vector<long long> v(n);
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    if (v[i] <= (b / mul)) {
      mul *= v[i];
    } else {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    cout << mul << endl;
  } else
    cout << -1 << endl;
  return 0;
}