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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long unsigned n;
  cin >> n;
  std::vector<long long unsigned> v;
  for (auto i = 0; i < n; i++) {
    long long unsigned a;
    cin >> a;
    v.push_back(a);
    if (a == 0) {
      cout << 0;
      return 0;
    }
  }

  long long unsigned ans = v[0];
  long long unsigned c = 1e18;
  for (long long unsigned i = 1; i < v.size(); i++) {
    if (v[i] > c / ans) {
      cout << -1;
      return 0;
    }
    ans *= v[i];
  }
  if (ans > 0 && ans <= c)
    cout << (long long int)ans;
  else
    cout << -1;

  return 0;
}

Error: WA

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
long long powerexp(long long base, long long exp, int mod) {
  long long res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % mod;
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}
int gcd(int a, int b) {
  int c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
void PrintVector(vector<long long int> v, long long int n) {

  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
void Takevector(vector<long long int> &v, long long int n) {
  for (long long int i = 0; i < n; i++) {
    long long int number;
    cin >> number;
    v.push_back(number);
  }
}
typedef long long int ll;
typedef vector<long long int> vi;
#define sci(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x);
#define scii(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \\
  int x, y, z;                                                                 \\
  scanf("%d %d %d", &x, &y, &z);
#define scl(x)                                                                 \\
  ll x;                                                                        \\
  scanf("%lld", &x);
#define scll(x, y)                                                             \\
  ll x, y;                                                                     \\
  scanf("%lld %lld", &x, &y);
#define sclll(x, y, z)                                                         \\
  ll x, y, z;                                                                  \\
  scanf("%lld %lld %lld", &x, &y, &z);
#define TC(x)                                                                  \\
  sci(x);                                                                      \\
  while (x--)
#define rep(i, x, y) for (__typeof(x) i = x; i <= y; i++)
#define repi(i, x, y) for (__typeof(x) i = x; i >= y; i--)
#define fore(itr, x)                                                           \\
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \\
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
#define nl cout << "\\n";
int min(int x, int y) { return (x < y) ? x : y; }

int main() {
  int flag = 1, flag1 = 0;
  unsigned long long int sum = 0, tot = 0, ans = 0;
  map<int, int> mp;
  vi v, v1;
  set<int> st, st1;
  string s;
  scl(n);
  ans = 1;
  vector<unsigned long long int> vec;
  rep(i, 0, n - 1) {
    cin >> sum;
    if (sum == 0) {
      cout << 0;
      return 0;
    }
    if (sum != 1) {
      vec.push_back(sum);
    }
  }
  unsigned long long int maxe = 1e18;
  rep(i, 0, vec.size() - 1) {
    if (vec[i] > maxe / ans) {
      cout << -1;
      return 0;
    }
    ans *= vec[i];
    // cout<<ans<<endl;
  }
  if (ans <= maxe)
    cout << ans;

  return 0;
}