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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double

ll power(ll b, ll e, ll m) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}

#define N 1000000000000000000

void solve() {

  ll n;
  cin >> n;
  ll a[n];
  ll ans = 1;
  bool flag1 = 0, flag = 1;
  for (auto &x : a) {
    cin >> x;
    if (!x)
      flag1 = 1;
  }
  for (ll i = 0; i < n - 1; i++) {
    ans *= a[i];
    if ((N / ans) < a[i + 1]) {
      flag = 0;
      break;
      ;
    }
  }
  if (flag1)
    cout << 0;
  else {
    if (flag)
      cout << ans * a[n - 1];
    else
      cout << -1;
  }
}
int main() {
  fastio ll t = 1;
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double

ll power(ll b, ll e, ll m) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}

#define max 1000000000000000000

void solve() {
  ll n, p = 1, x, q = 1;
  cin >> n;
  vi a;
  rep(i, 0, n) {
    cin >> x, a.pb(x);
    p = p * x;
  }
  if (n == 1) {
    cout << p << "\\n";
    return;
  }
  rep(i, 0, n - 1) q = q * a[i];
  if ((p / q) != a[n - 1] || p > max)
    cout << "-1"
         << "\\n";
  else
    cout << p << "\\n";
}
int main() {
  fastio ll t = 1;
  while (t--)
    solve();
  return 0;
}