alexjercan/bugnet huggingface dataset:
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define per(i, s, n) for (int i = n - 1; i >= s; i--)
#define fi first
#define se second
#define all(c) c.begin(), c.end()
typedef long long ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 1000000007;
const ll u = 1000000000000000000;
int main() {
  ll n;
  cin >> n;

  ld ans = 1;
  ll an = 1;
  ll k;
  int c = 0;
  rep(i, n) {
    cin >> k;
    if (c == 0 || k == 0) {
      ans *= k;
      an *= k;
      if (ans > u) {
        c++;
      }
    }
  }

  if (c == 0 || ans == 0) {
    cout << an << endl;
  } else {
    cout << -1 << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define per(i, s, n) for (int i = n - 1; i >= s; i--)
#define fi first
#define se second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;

  ll ans = 1;
  ll k;
  int c = 0;
  rep(i, n) {
    cin >> k;
    if (k > 1000000000000000000 / ans) {
      c++;
    }
    ans *= k;
  }

  if (c == 0 || ans == 0) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  bool t = true;
  long long a[n], k = 1, big = 1000000000000000000;
  for (int i = 0; i < n; i++) {
    long long l;
    cin >> l;
    a[i] = l;
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    if (k != 0) {
      if (big / k < a[i]) {
        t = false;
        break;
      }
    }
    k *= a[i];
  }
  if (!t || k > big)
    cout << -1 << endl;
  else
    cout << k << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  bool t = true;
  long long a[n], k = 1, big = 1000000000000000001;
  for (int i = 0; i < n; i++) {
    if (big / k < a[i]) {
      t = false;
      break;
    } else
      k *= a[i];
  }
  if (!t || k >= big)
    cout << -1 << endl;
  else
    cout << k << endl;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
constexpr long long int LLINT_MAX = 1e18;

void Multiplication1() {
  int A, B;
  cin >> A >> B;
  cout << A * B;
}

void Multiplication2() {
  int N;
  bool isOverflow = false;
  long long int ans = 1;
  cin >> N;
  for (int i = 0; i < N; i++) {
    long long int num;
    cin >> num;
    if (num == 0) {
      cout << 0 << endl;
      return;
    }
    if (isOverflow || num > LLINT_MAX / ans) {
      isOverflow = true;
    }
    ans *= num;
  }
  if (isOverflow)
    cout << -1 << endl;
  else
    cout << ans;
}

void Multiplication3() {
  long long int A, ans;
  long double B;
  cin >> A >> B;
  ans = A * B;
  cout << ans;
}

bool IsPrime(long int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

  long double sqrtNum = sqrt(num);
  for (long int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

void DivGame() {
  long long int N;
  int e = 1;
  int ans = 0;
  cin >> N;
  if (N == 1) {
    cout << ans;
    return;
  }
  if (IsPrime(N)) {
    cout << 1;
    return;
  }
  for (long long int p = 2; p <= (long double)sqrt(N); p++) {
    if (IsPrime(p)) {
      long int z = p;
      while (N % z == 0) {
        N /= z;
        z *= p;
        ans++;
      }
    }
  }
  cout << ans;
}

int main() {
  Multiplication2();
  // DivGame();
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
constexpr long long int LLINT_MAX = 1e18;

void Multiplication1() {
  int A, B;
  cin >> A >> B;
  cout << A * B;
}

void Multiplication2() {
  int N;
  bool isOverflow = false;
  long long int ans = 1;
  cin >> N;
  for (int i = 0; i < N; i++) {
    long long int num;
    cin >> num;
    if (num > LLINT_MAX / ans) {
      isOverflow = true;
    } else {
      ans *= num;
    }
  }
  if (isOverflow)
    cout << -1 << endl;
  else
    cout << ans;
}

void Multiplication3() {
  long long int A, ans;
  long double B;
  cin >> A >> B;
  ans = A * B;
  cout << ans;
}

bool IsPrime(long int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

  long double sqrtNum = sqrt(num);
  for (long int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

void DivGame() {
  long long int N;
  int e = 1;
  int ans = 0;
  cin >> N;
  if (N == 1) {
    cout << ans;
    return;
  }
  if (IsPrime(N)) {
    cout << 1;
    return;
  }
  for (long long int p = 2; p <= (long double)sqrt(N); p++) {
    if (IsPrime(p)) {
      long int z = p;
      while (N % z == 0) {
        N /= z;
        z *= p;
        ans++;
      }
    }
  }
  cout << ans;
}

int main() {
  Multiplication2();
  // DivGame();
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n = 0;
  cin >> n;
  vector<ll> a(n);
  ll res = 0;
  res = 1;
  bool ok = true;
  bool zero = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    string copy, now;
    copy = to_string(res);
    now = to_string(a[i]);
    // cout << (ll)pow(10,18) / a[i] << " " << res << endl;
    if (a[i] == 0) {
      res = 0;
      zero = true;
      break;
    }
    if (res > ((ll)pow(10, 18) / a[i]) + (ll)1) {
      // cout << "!" << endl;
      ok = false;
    }
    if (ok)
      res *= a[i];
    if (res > (ll)pow(10, 18))
      ok = false;
    // cout << res << endl;
    // cout << pow(10,18) << endl;
    if (a[i] == 0)
      zero = true;
  }
  if (ok)
    cout << res << endl;
  else {
    if (zero)
      cout << 0 << endl;
    else
      cout << -1 << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n = 0;
  cin >> n;
  vector<ll> a(n);
  ll res = 0;
  res = 1;
  bool ok = true;
  bool zero = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    string copy, now;
    copy = to_string(res);
    now = to_string(a[i]);
    // cout << (ll)pow(10,18) / a[i] << " " << res << endl;
    if (res > ((ll)pow(10, 18) / a[i]) + (ll)1) {
      // cout << "!" << endl;
      ok = false;
    }
    if (ok)
      res *= a[i];
    if (res > (ll)pow(10, 18))
      ok = false;
    // cout << res << endl;
    // cout << pow(10,18) << endl;
    if (a[i] == 0)
      zero = true;
  }
  if (ok)
    cout << res << endl;
  else {
    if (zero)
      cout << 0 << endl;
    else
      cout << -1 << endl;
  }
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll n, i;
  cin >> n;
  ll a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ll p = 1;
  ll cou = 1000000000000000000;
  for (i = 0; i < n; i++) {
    p *= a[i];
    if (p >= 1000000000000000000 && (i != n - 1)) {
      cout << "-1" << endl;
      return 0;
    }
    if (a[i] == 0) {
      cout << '0' << endl;
      return 0;
    }
    cou /= a[i];
    if (cou < 1) {
      cout << "-1" << endl;
      return 0;
    }
  }
  if (p > 1000000000000000000) {
    cout << "-1" << endl;
    return 0;
  }
  if (p < 0)
    p = -1;
  cout << p << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, i;
ll *a = new ll[n];
vector<ll> v;
ll p = 1, ans = 0;

int solve() {
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    p *= a[i];
    if (p >= 1000000000000000000 && (i != n - 1)) {
      ans = -1;
      return ans;
    }
    if (a[i] == 0) {
      ans = 0;
      return ans;
    }
  }
  if (p > 1000000000000000000) {
    ans = -1;
    return ans;
  }
  if (p < 0)
    p = -1;
  ans = p;
  return ans;
}

int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(a[i]);
  }
  if (v.size() == n)
    solve();
  cout << ans << endl;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll a[n];
  bool zero = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      zero = true;
  }
  if (zero) {
    cout << "0"
         << "\\n";
    return 0;
  } else {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      if (a[i] > 1000000000000000000 / ans) {
        cout << "-1"
             << "\\n";
        return 0;
      } else
        ans *= a[i];
    }
    cout << ans << "\\n";
    return 0;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll ans = 1;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a == 0) {
      cout << "0"
           << "\\n";
      return 0;
    }
    if (a > 1000000000000000000 / ans)
      flag = true;
    ans *= a;
  }
  if (flag)
    ans = -1;
  cout << ans << "\\n";
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, mul = 1, a[100005], i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    if (a[i] == 0) {
      cout << 0;
      return 0;
    }
  }
  for (i = 0; i < n; i++) {
    if (a[i] > 1000000000000000000 / mul) {
      cout << -1;
      return 0;
    } else
      mul *= a[i];
  }
  cout << mul;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, mul = 1, a;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a;
    if (a == 0) {
      mul = 0;
      continue;
    }

    if (a > 1000000000000000000 / mul) {
      mul = -1;
      continue;
    }
    mul *= a;
  }
  cout << mul;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  long long mul = 1;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    if (A[i] == 0LL) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < N; ++i) {
    if (mul > 1000'000'000'000'000'000LL / A[i]) {
      cout << -1 << endl;
      return 0;
    }
    mul *= A[i];
  }
  cout << mul << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int mul = 1;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    if (mul > 1000'000'000'000'000'000 / A[i]) {
      cout << -1 << endl;
      return 0;
    }
    mul *= A[i];
  }
  cout << mul << endl;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define lint long long
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
#define MP make_pair
using namespace std;

// cout << fixed << setprecision(12)

int main() {
  int N;
  cin >> N;
  vector<lint> A(N, 0);
  lint ans = 1;
  // bool plus = true;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0) {
      ans = 0;
      // plus = false;
      break;
    }
  }
  if (ans != 0) {
    for (int i = 0; i < N; i++) {
      if (A[i] > 1000000000000000000 / ans) {
        ans = -1;
        break;
      }
      ans *= A[i];
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lint long long
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
#define MP make_pair
using namespace std;

// cout << fixed << setprecision(12)

int main() {
  int N;
  cin >> N;
  vector<lint> A(N, 0);
  lint ans = 1;
  // bool plus = true;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0) {
      ans = 0;
      // plus = false;
      break;
    }
  }

  for (int i = 0; i < N; i++) {
    if (A[i] > 1000000000000000000 / ans) {
      ans = -1;
      break;
    }
    ans *= A[i];
  }

  cout << ans << endl;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

typedef long long ll;

int main() {
  int N;
  std::cin >> N;
  ll a[N];
  for (int i = 0; i < N; ++i) {
    std::cin >> a[i];
    if (a[i] == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
  }

  ll max = 1e18;
  ll multi = 1;

  for (ll i; i < N; ++i) {
    if (multi > max / a[i]) {
      std::cout << -1 << std::endl;
      return 0;
    };
    multi *= a[i];
  }
  if (multi <= max) {
    std::cout << multi << std::endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>

typedef long long ll;

int main() {
  int N;
  std::cin >> N;
  ll a[N];
  for (int i : a) {
    std::cin >> a[i];
    if (a[i] == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
  }

  ll max = 1e18;
  ll multi = 1;

  for (ll i : a) {
    if (multi > max / a[i]) {
      std::cout << -1 << std::endl;
      return 0;
    };
    multi *= a[i];
  }
  std::cout << multi << std::endl;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

typedef long long ll;

int main() {
  int N;
  std::cin >> N;
  ll a[N];
  for (ll i; i < N; ++i) {
    std::cin >> a[i];
    if (a[i] == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
  }

  ll max = 1e18;
  ll multi = 1;

  for (ll e : a) {
    if (multi > max / e) {
      std::cout << -1 << std::endl;
      return 0;
    };
    multi *= e;
  }
  if (multi <= max) {
    std::cout << multi << std::endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>

typedef long long ll;

int main() {
  int N;
  std::cin >> N;
  ll a[N];
  for (ll e : a) {
    std::cin >> e;
    if (e == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
  }

  ll max = 1e18;
  ll multi = 1;

  for (ll e : a) {
    if (multi > max / e) {
      std::cout << -1 << std::endl;
      return 0;
    };
    multi *= e;
  }
  if (multi <= max) {
    std::cout << multi << std::endl;
  }
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  ll ans = 1;
  rep(i, n) {
    ll a;
    cin >> a;
    if (ans == -1) {
      if (a == 0)
        ans = 0;
    } else {
      if (a == 0 || ans == 0) {
        ans *= a;
        continue;
      }
      if (a > 1'000'000'000'000'000'000 / ans) {
        ans = -1;
        continue;
      } else {
        ans *= a;
      }
    }
  }
  if (ans == -1)
    cout << to_string(ans) << endl;
  else
    cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  ll ans = 1;
  rep(i, n) {
    ll a;
    cin >> a;
    if (ans == -1) {
      if (a == 0)
        ans = 0;
    } else {
      if (a > 1'000'000'000'000'000'000 / ans) {
        ans = -1;
        continue;
      } else {
        ans *= a;
      }
    }
  }
  if (ans == -1)
    cout << to_string(ans) << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll ans = 1;
  for (ll i = 0; i < n; i++) {
    cin >> a.at(i);
    if (a.at(i) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans * a.at(i) == 1000000000000000000)
      ans = ans * a.at(i);
    else if (log10(ans) + log10(a.at(i)) >= 18) {
      cout << -1 << endl;
      return 0;
    } else
      ans = ans * a.at(i);
  }
  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 2) >= this->size() (which is 2)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> a.at(n);
    ans = ans * a.at(n);
    if (ans > pow(10, 18)) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
ll M = 1e18;

using namespace std;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  if (a[0] == 0) {
    cout << "0";
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (M / ans < 1 or M / ans < a[i]) {
      cout << "-1";
      return 0;
    }
    if (i != n)
      ans *= a[i];
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
ll M = 1e18;

using namespace std;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  if (a[0] == 0) {
    cout << "0";
    return 0;
  }
  for (int i = 0; i < n + 1; i++) {
    if (M / ans < 1) {
      cout << "-1";
      return 0;
    }
    if (i != n)
      ans *= a[i];
  }
  cout << ans;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()

const char newl = '\\n';

using namespace std;
using ll = long long;

auto main() -> int {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int N;
  cin >> N;
  vector<ll> A(N);

  ll h = 1;

  for (auto &a : A)
    cin >> a;

  sort(A.begin(), A.end());
  for (const auto &a : A) {
    if (h == 0)
      break;
    if (a <= 1000000000000000000 / h) {
      h *= a;
    } else {
      printf("-1");
      return 0;
    }
  }

  printf("%lld\\n", h);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()

const char newl = '\\n';

using namespace std;
using ll = long long;

auto main() -> int {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int N;
  cin >> N;
  vector<ll> A(N);

  ll h = 1;

  for (auto &a : A)
    cin >> a;

  sort(A.begin(), A.end());
  for (const auto &a : A) {
    if (a > 1000000000000000000 / h) {
      printf("-1");
      return 0;
    }

    h *= a;
  }

  printf("%lld\\n", h);

  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int64_t mx = 1e18;

int main() {
  int N;
  cin >> N;
  bool ng = false;
  int64_t ans = 1;

  for (int i = 0; i < N; i++) {
    int64_t A;
    cin >> A;

    if (A == 0) {
      cout << 0 << endl;
      return 0;
    }

    if (ng || (mx + 1) / ans < A || mx < A * ans) {
      ng = true;
    }

    ans *= A;
  }

  if (ng) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const long long mx = 1e18;

int main() {
  int N;
  cin >> N;
  bool ng = false;
  int ans = 1;

  for (int i = 0; i < N; i++) {
    long long A;
    cin >> A;

    if (A == 0) {
      cout << 0 << endl;
      return 0;
    }

    if (ng || (mx + 1) / ans < A || mx < A * ans) {
      ng = true;
    }

    ans *= A;
  }

  if (ng) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int64_t mx = 1e18;

int main() {
  int N;
  cin >> N;
  bool ng = false;
  int64_t ans = 1;

  for (int i = 0; i < N; i++) {
    int64_t A;
    cin >> A;

    if (A == 0) {
      cout << 0 << endl;
      return 0;
    }

    if (ng || mx / ans < A) {
      ng = true;
    }

    ans *= A;
  }

  if (ng) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int64_t mx = 1e18;

int main() {
  int N;
  cin >> N;
  bool ng = false;
  int64_t ans = 1;

  for (int i = 0; i < N; i++) {
    int64_t A;
    cin >> A;

    if (A == 0) {
      cout << 0 << endl;
      return 0;
    }

    if (mx / ans < A) {
      ng = true;
    }

    ans *= A;
  }

  if (ng) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  ll ans = 1;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0) {
      ans = 0;
    }
  }

  if (ans == 0) {
    cout << 0 << endl;
    exit(0);
  }

  ll judge = pow(10, 18);
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  for (ll i = 0; i < N; i++) {
    if (ans <= judge / A[i]) {
      ans *= A[i];
    } else {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  ll ans = 1;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0) {
      ans = 0;
    }
  }
  ll judge = pow(10, 18);
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  for (ll i = 0; i < N; i++) {
    if (ans <= judge / A[i]) {
      ans *= A[i];
    } else {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
}