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


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define lli unsigned long long int
using namespace std;
#define S second
#define F first
#define pb push_back
#define M1 1000000007
#define M2 998244353
#define pint pair<int, int>
#define setit set<int>::iterator seti
#define mapit map<int, int>::iterator mapi
#define MP make_pair
#define MT make_tuple
#define PI 3.14159265358979323846
// map      iterator->first  iterator.first
// map      mp[key]=value
// set      *s.begin()
// vector   v[j]=value
// pair     p.first p.second
// vector of pairs v[j].first v[j].second
// tuple    get<index>(tuple_name)
// vector of tuples get<index>(v[j])  sort(v.begin(),v.end())
// priority queue .top() .pop() .push()
// __gcd(x,y)
#define mini(x, y) (y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1))))
#define maxi(x, y) (x - ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1))))
#define fr(j, a, b) for (j = a; j < b; j++)

int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

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

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  lli t, i, j, n, x;
  cin >> n;
  lli a[n];
  x = 1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      cout << "0";
      return 0;
    }
  }
  for (i = 0; i < n; i++) {
    //    x = x * a[i];
    if (a[i] <= 1000000000000000000 / x) {
      x = x * a[i];
    } else {
      cout << "-1";
      return 0;
    }
  }
  cout << x;
}

Error: -11

Fixed submission:


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define lli unsigned long long int
using namespace std;
#define S second
#define F first
#define pb push_back
#define M1 1000000007
#define M2 998244353
#define pint pair<int, int>
#define setit set<int>::iterator seti
#define mapit map<int, int>::iterator mapi
#define MP make_pair
#define MT make_tuple
#define PI 3.14159265358979323846
// map      iterator->first  iterator.first
// map      mp[key]=value
// set      *s.begin()
// vector   v[j]=value
// pair     p.first p.second
// vector of pairs v[j].first v[j].second
// tuple    get<index>(tuple_name)
// vector of tuples get<index>(v[j])  sort(v.begin(),v.end())
// priority queue .top() .pop() .push()
// __gcd(x,y)
#define mini(x, y) (y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1))))
#define maxi(x, y) (x - ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1))))
#define fr(j, a, b) for (j = a; j < b; j++)

int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

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

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  lli t, i, j, n, x;
  cin >> n;
  lli a[n];
  x = 1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (a[0] == 0) {
    cout << "0";
    return 0;
  }
  for (i = n - 1; i >= 0; i--) {
    if (x > 1000000000000000000) {
      cout << "-1";
      return 0;
    }
    x = x * a[i];
  }
  cout << x;
}