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

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  long long int i, j, k, n, b, ans = 1, po = 0, po0 = 0;
  scanf("%lld", &n);
  long long int a[n];
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (i = 0; i < n; i++) {
    if (log10((double)ans) + log10((double)a[i]) > 18)
      po++;
    ans = ans * a[i];
    if (ans > 1000000000000000000)
      po++;
    if (a[i] == 0)
      po0++;
  }
  if (po0 > 0)
    printf("0");
  else if (po > 0)
    printf("-1");
  else
    printf("%lld", ans);
}

Error: WA

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  long long int i, j, k, n, b, ans = 1, po = 0, po0 = 0,
                               popo = 1000000000000000000, pa;
  scanf("%lld", &n);
  long long int a[n];
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (i = 0; i < n; i++) {
    ans = ans * a[i];
    popo = popo / a[i];
    if (popo == 0)
      pa++;
    if (ans > 1000000000000000000 || ans < 0)
      po++;
    if (a[i] == 0)
      po0++;
    pa++;
  }
  if (po0 > 0)
    printf("0");
  else if (po > 0 || pa > 0)
    printf("-1");
  else
    printf("%lld", ans);
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
// static const unsigned long long MAX 1000000000000000000;

int main() {
  int n;
  cin >> n;
  unsigned long long a = 1;
  unsigned long long b[100000];
  int flag = 0;
  int zerof = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  // sort(b[0], b[n]);
  for (int i = 0; i < n; i++) {
    if (a > 1000000000000000000 / b[i]) {
      cout << -1 << endl;
      return 0;
    }
    a *= b[i];
  }
  if (zerof == 1) {
    cout << 0 << endl;
  } else if (flag == 1) {
    cout << -1 << endl;
  } else {
    cout << a << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// static const unsigned long long MAX 1000000000000000000;

int main() {
  int n;
  cin >> n;
  unsigned long long a = 1;
  unsigned long long b[100000];
  int flag = 0;
  int zerof = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] == 0)
      zerof = 1;
  }
  // sort(b[0], b[n]);
  for (int i = 0; i < n; i++) {
    if (a > 1000000000000000000 / b[i])
      flag = 1;
    a *= b[i];
  }
  if (zerof == 1) {
    cout << 0 << endl;
  } else if (flag == 1) {
    cout << -1 << endl;
  } else {
    cout << a << endl;
  }
}
Problem p02658 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
#define _USE_MATH_DEFINES
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iterator>
#include <set>
#include <unordered_set>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y) {
  if (x < y)
    return x;
  return y;
}
ull max(ull x, ull y) {
  if (x < y)
    return y;
  return x;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x < y)
    return y;
  return x;
}
double min(double x, double y) {
  if (x < y)
    return x;
  return y;
}
double max(double x, double y) {
  if (x < y)
    return y;
  return x;
}
ull gcd(ull x, ull y) {
  if (!x)
    return y;
  if (!y)
    return x;
  if (x > y)
    swap(x, y);
  return gcd(x, y % x);
}
ull inv(ull a, ull c) { // a,c are coprime and a<c
  if (a == 1) {
    return 1;
  }
  return ((c - (c / a)) * inv(c % a, c)) % c;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, ans = 1, a, z = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    if (ans > (ll)1e18 / a) {
      ans = -1;
      break;
    }
    ans *= a;
  }

  while (n > 0) {
    cin >> a;
    if (a == 0) {
      z++;
    }
    n--;
  }
  if (z)
    ans = 0;
  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
#define _USE_MATH_DEFINES
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iterator>
#include <set>
#include <unordered_set>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y) {
  if (x < y)
    return x;
  return y;
}
ull max(ull x, ull y) {
  if (x < y)
    return y;
  return x;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x < y)
    return y;
  return x;
}
double min(double x, double y) {
  if (x < y)
    return x;
  return y;
}
double max(double x, double y) {
  if (x < y)
    return y;
  return x;
}
ull gcd(ull x, ull y) {
  if (!x)
    return y;
  if (!y)
    return x;
  if (x > y)
    swap(x, y);
  return gcd(x, y % x);
}
ull inv(ull a, ull c) { // a,c are coprime and a<c
  if (a == 1) {
    return 1;
  }
  return ((c - (c / a)) * inv(c % a, c)) % c;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, ans = 1, a, z = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    ans *= a;
    if (ans > (ll)1e18) {
      ans = -1;
      break;
    }
  }

  while (n--) {
    cin >> a;
    if (a == 0) {
      z++;
    }
  }
  if (z)
    ans = 0;
  cout << ans << "\\n";

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

#include <iostream>

using namespace std;
typedef unsigned long long uLL;
const uLL eps = 1e18;
const uLL N = 1e5 + 9;
uLL A[N];
int main() {

  int n;
  cin >> n;
  uLL ans = 1;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] == 0)
      zero++;
  }
  if (zero > 0) {
    printf("0\\n");
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (A[i] <= eps / ans)
      ans *= A[i];
    else {
      printf("-1\\n");
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>

using namespace std;
typedef unsigned long long uLL;
const uLL eps = 1e18;
const uLL N = 1e5 + 9;
uLL A[N];
int main() {

  int n;
  cin >> n;
  uLL ans = 1;
  int flag = 1;
  for (int i = 0; i < n; i++) {
    cin >> A[i];

    if (A[i] <= eps / ans)
      ans = ans * A[i];
    else {
      flag = 0;
    }
  }
  if (flag || ans == 0)
    cout << ans;
  else
    printf("-1");
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#define ll long long int
#define CON 100010
const long long mod = 1e9 + 7;
const int INF = 1001001001;
const ll inf = 1e9 + 7;
const ll ful = 1e18;
using namespace std;
namespace mp = boost::multiprecision;
int main() {
  int N;
  mp::cpp_int ans = 1;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0)
      ans = 0;
  }
  sort(A.begin(), A.end());
  bool flag = true;
  for (int i = 0; i < N; i++) {
    ans *= A[i];
    if (ans > ful) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#define ll long long int
#define CON 100010
const long long mod = 1e9 + 7;
const int INF = 1001001001;
const ll inf = 1e9 + 7;
const ll ful = 1e18;
using namespace std;
namespace mp = boost::multiprecision;
int main() {
  int N;
  mp::cpp_int ans = 1;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0)
      ans = 0;
  }
  for (int i = 0; i < N; i++) {
    ans *= A[i];
  }
  if (ans > ful) {
    cout << -1 << endl;
    ;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

/*   ꧁༒☬महाकाल☬༒꧂ ꧁༒ঔमहाकालঔ༒꧂

                   ঔৣ۝महाकालᴳᵒᵈ۝ঔৣ

 */
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T, typename cmp=less<T>> using oset =tree<T, null_type,
// cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define mod 1000000007
#define inf 100000000000000000
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define _it iterator
#define all(_x) _x.begin(), _x.end()
#define f first
#define s second
#define pb push_back
void mahakal() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
const ll N = 1e6 + 1;
void solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr.begin(), arr.end());
  ll ans = 1, boundary = 1e18;
  for (ll i = 0; i < n; i++) {
    if (arr[i] == 0 || ans == 0) {
      cout << 0 << endl;
      return;
    }
    if (arr[i] > boundary / ans || ans < 0) {
      cout << -1 << endl;
      return;
    }
    ans = ans * arr[i];
  }
  cout << ans << endl;
}
int main() {
  // mahakal(),fast;
  ll t = 1;
  // cin>> t;
  while (t--)
    solve();

  return 0;
}

Error: 0

Fixed submission:

/*   ꧁༒☬महाकाल☬༒꧂ ꧁༒ঔमहाकालঔ༒꧂

                   ঔৣ۝महाकालᴳᵒᵈ۝ঔৣ

 */
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T, typename cmp=less<T>> using oset =tree<T, null_type,
// cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define mod 1000000007
#define inf 100000000000000000
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define _it iterator
#define all(_x) _x.begin(), _x.end()
#define f first
#define s second
#define pb push_back
void mahakal() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
const ll N = 1e6 + 1;
void solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr.begin(), arr.end());
  ll ans = 1, boundary = 1e18;
  for (ll i = 0; i < n; i++) {
    if ((arr[i] != 0 && arr[i] > boundary / ans) || ans < 0) {
      cout << -1 << endl;
      return;
    }
    ans = ans * arr[i];
  }
  cout << ans << endl;
}
int main() {
  // mahakal(),fast;
  ll t = 1;
  // cin>> t;
  while (t--)
    solve();

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

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

int main() {
  int n = 0;
  vector<long long> a(100010);

  cin >> n;

  for (int i = 0; i < n; i++) {
    /* code */
    cin >> a.at(i);
  }

  int zero = 0;
  for (int i = 0; i < n; i++) {
    if (a.at(i) == 0)
      zero++;
  }
  if (zero > 0) {
    cout << 0 << endl;
    return 0;
  }

  long long ans = 1;
  for (int i = 0; i < n; i++) {
    if (a.at(i) <= 1000000000000000000 / ans) {
      ans *= a.at(i);
    } else {
      ans = -1;
      cout << ans << endl;
      return 0;
    }
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n = 0;
  vector<int> a(100010);

  cin >> n;

  for (int i = 0; i < n; i++) {
    /* code */
    cin >> a.at(i);
  }

  long long ans = 1;
  for (int i = 0; i < n; i++) {
    if (a.at(i) <= 1000000000000000000 / ans) {
      ans *= a.at(i);
    } else {
      ans = -1;
      cout << ans << endl;
      return 0;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INF 1000000000000
#define ALL(a) (a).begin(), (a).end()
#define large 1000000000000000000

int main() {
  ll n;
  cin >> n;
  ll a[n + 10];
  ll ans = 1;
  bool ok = true;
  bool zero = false;
  for (ll i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    if (temp == 0) {
      cout << 0 << endl;
      return 0;
    }
    a[i] = temp;
  }
  ll prev = large;
  for (int i = 0; i < n; i++) {
    if (a[i] > prev) {
      // ans = -1;
      ok = false;
      break;
    } else {
      ans *= a[i];
      prev = large / ans;
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INF 1000000000000
#define ALL(a) (a).begin(), (a).end()
#define large 1000000000000000000

unsigned GetDigit(unsigned num) {
  unsigned digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll ans = 1;
  bool ok = true;
  for (ll i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    a[i] = temp;
  }
  ll prev = large;
  for (int i = 0; i < n; i++) {
    if (a[i] > prev) {
      // ans = -1;
      ok = false;
    } else {
      ans *= a[i];
      prev = large / ans;
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

long long MAX = 1000000000000000000;

int main() {

  int n;

  cin >> n;

  vector<long long> list;

  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    list.push_back(a);
  }

  long long mult = 1;
  int count = 0;
  long long current = list[count];

  // cout << mult << "  " << MAX << endl;

  while (count < n && MAX >= 1) {
    if (current == 0) {
      cout << 0 << endl;
      return 0;
    }
    MAX /= current;
    mult *= current;
    count++;
    if (count < n)
      current = list[count];
    // cout << current << " " << MAX <<  endl;
  }

  while (count < n) {
    if (list[count] == 0) {
      cout << 0 << endl;
      return 0;
    }
    count++;
  }

  if (MAX < 1) {
    cout << -1 << endl;
  } else {
    cout << mult << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

long long MAX = 1000000000000000000;

int main() {

  int n;

  cin >> n;

  vector<int> list;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    list.push_back(a);
  }

  long long mult = 1;
  int count = 0;

  while (mult <= (MAX / list[count]) && count < n) {
    MAX /= list[count];
    mult *= list[count];
    count++;
  }

  if (count < n) {
    cout << -1 << endl;
  } else {
    cout << mult << endl;
  }

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

/* Life is all about calculated risks.
    But man am I bad at math. */

#include <bits/stdc++.h>
#define ll long long int
#define INF (ll)1e12
#define MOD 1000000007

#define DEBUG(x) cerr << "> " << #x << ':' << x << endl;

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  if (a[0] == 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 1, old = 1;

  for (ll i = 0; i < n; i++) {
    ans *= a[i];
    if (ans > (ll)1e18 || ans < old) {
      cout << -1 << endl;
      return 0;
    }
    old = ans;
  }
  cout << ans << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

/* Life is all about calculated risks.
    But man am I bad at math. */

#include <bits/stdc++.h>
#define ll unsigned long long int
#define INF (ll)1e12
#define MOD 1000000007

#define DEBUG(x) cerr << "> " << #x << ':' << x << endl;

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  ll end = (ll)1e18;
  vector<ll> a(n, 0);
  cin >> n;
  bool zer = false;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      zer = true;
  }
  if (zer) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 1;
  for (ll i = 0; i < n; i++) {
    if (ans > end / a[i]) {
      cout << -1 << endl;
      return 0;
    }
    ans *= a[i];
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
long long a[100010];
int main() {
  int i, n, flg;
  long long ans;

  cin >> n;
  flg = 0;
  for (i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] == 0)
      ++flg;
  }

  ans = 0;
  if (flg == 0) {
    ans = a[0];
    for (i = 1; i < n; ++i) {
      if (a[i] > 1000000000000000000 / ans) {
        ans = -1;
        break;
      } else {
        ans *= a[i];
      }
    }
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

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

int main() {
  int i, n;
  long long ans, a[10 ^ 5 + 10];

  cin >> n;
  for (i = 0; i < n; ++i)
    cin >> a[i];

  ans = a[0];
  for (i = 1; i < n; ++i) {
    if (ans * a[i] > 10 ^ 18) {
      ans = -1;
      break;
    } else
      ans *= a[i];
  }
  cout << ans << endl;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

#define PI 3.14159265358979323846264338327950L

long long res;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  bool flag = true;
  // long long *array = new long long [N];
  long long array[100010];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> array[i];
    if (array[i] == 0) {
      flag = false;
    }
  }

  res = 1;
  if (flag == false) {
    res = 0;
  } else {
    for (int i = 0; i < N; i++) {

      if (array[i] <= 1000000000000000000 / res)

      {
        res *= array[i];
      } else {
        res = -1;
        break;
      }
    }
  }

  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

#define PI 3.14159265358979323846264338327950L

long long res;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  bool flag = true;
  long long *array = new long long[N];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> array[i];
    if (array[i] == 0) {
      flag = false;
    }
  }

  res = 1;
  if (flag == false) {
    res = 0;
  } else {
    for (int i = 0; i < N; i++) {

      if (array[i] <= 1000000000000000000 / res)

      {
        res *= array[i];
      } else {
        res = -1;
        break;
      }
    }
  }

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

#include <bits/stdc++.h>
#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;
ll MAX = 1000000000000000000;
using namespace std;

int main() {
  int n, flag;
  ll ans, an;
  ans = 1;
  flag = 1;

  cin >> n;
  REP(int, i, 0, n) {
    cin >> an;
    if (an == 0) {
      cout << 0 << endl;
      return 0;
    } else if (an > MAX / ans || ans * an > MAX) {
      flag = 0;
    }
    if (flag)
      ans = ans * an;
  }
  if (flag)
    cout << ans << endl;
  else
    cout << -1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;
ll MAX = 1000000000000000000;
using namespace std;

int main() {
  int n, flag;
  ll ans, an;
  ans = 1;
  flag = 1;

  cin >> n;
  REP(int, i, 0, n) {
    cin >> an;
    if (an == 0) {
      ans = 0;
      cout << 0 << endl;
      return 0;
    } else if (an > MAX / ans || ans * an > MAX) {
      flag = 0;
    }
    ans = ans * an;
  }
  if (flag)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, a;
  cin >> n >> a;
  ll ans = a;
  bool flag = false;
  for (int i = 1; i < n; i++) {
    cin >> a;
    if (a == 0) {
      cout << 0 << endl;
      return 0;
    }
    if (a > (1000000000000000000 / ans)) {
      flag = true;
    }
    if (!flag)
      ans *= a;
  }
  cout << (flag ? -1 : ans) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ll n, a;
  cin >> n >> a;
  ll ans = a;
  bool flag = false;
  for (int i = 1; i < n; i++) {
    cin >> a;
    if (a == 0) {
      cout << 0 << endl;
      return 0;
    }
    if (a > ((ll)1e+18 / ans)) {
      flag = true;
    }
    ans *= a;
  }
  cout << (flag ? -1 : ans) << endl;
  return 0;
}
Problem p02658 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
using namespace std;
ll lim = 1e18;
int main() {
  fast int n;
  cin >> n;

  ll res = 1;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x == 0)
      res = 0;
    if (res != 0 && (x > lim / res || res * x > lim))
      res = -1;
    if (res != -1)
      res = res * x;
  }
  cout << res;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
using namespace std;
ll lim = 1e18;

int main() {
  fast int n;
  cin >> n;
  ll res = 1;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x == 0)
      res = 0;
    if (res * x <= lim)
      res = res * x;
    if ((res * x > lim || x > lim / res) && res != 0)
      res = -1;
  }

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

#include <cmath>
#include <iostream>
#define PI 3.141592
using namespace std;

int main() {
  int N = 0;
  cin >> N;
  int Max = 100000;
  unsigned long long int A[Max] = {}, num;
  int S = 0;

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

    if (A[i] == 0) {
      S = 1;
      cout << 0;
      break;
    }
  }

  num = A[0];
  for (int i = 1; i < N && S == 0; i++) {

    if (log10(num) + log10(A[i]) > 18) {
      S = 2;
      cout << -1;
      break;
    }

    num *= A[i];

    if (num > 1000000000000000000) {
      cout << -1;
      S = 2;
      break;
    }
  }

  if (S == 0) {
    cout << num;
  }
}


Error: WA

Fixed submission:

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

int main() {
  unsigned long long A[10 ^ 5], out = 0;
  int N = 0;
  cin >> N;
  int S = 0;
  const unsigned long long max = pow(10, 18);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == 0) {
      cout << 0;
      S = 2;
      break;
    }
  }

  for (int i = 0; i < N && S != 2; i++) {
    if (A[i] > max) {
      cout << -1;
      S = 1;
      break;
    }
    if (i == 0) {
      out = A[i];
    } else {
      out *= A[i];
      if (out > max) {
        cout << -1;
        S = 2;
        break;
      }
    }
  }

  if (S == 0)
    cout << out;
}