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


// Problem : B - Multiplication 2
// Contest : AtCoder - AtCoder Beginner Contest 169
// URL : https://atcoder.jp/contests/abc169/tasks/abc169_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbp(x)                                                                 \\
  cout << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define dbv(v)                                                                 \\
  {                                                                            \\
    cout << #v << " : ";                                                       \\
    for (auto x : v)                                                           \\
      cout << x << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die(x) return cout << x << '\\n', 0;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int MAX = 1e18;
int32_t main() {
  fastio;

  int testcases;
  testcases = 1;
  // cin>>testcases;

  while (testcases--) {
    int n;
    cin >> n;
    vector<int> vec(n);
    int ans = 1;
    int flag = 1;
    rep(i, 0, n - 1) {
      cin >> vec[i];
      if (ans != 0 && vec[i] > MAX / ans) {
        flag = -1;
        ans = 1;
      } else
        ans = ans * vec[i];
      if (vec[i] == 0) {
        flag = 0;
      }
    }
    //    cout << ans;
    if (flag != 0 && flag == -1)
      die(-1);
    die(ans);
  }

  return 0;
}


Error: 0

Fixed submission:


// Problem : B - Multiplication 2
// Contest : AtCoder - AtCoder Beginner Contest 169
// URL : https://atcoder.jp/contests/abc169/tasks/abc169_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbp(x)                                                                 \\
  cout << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define dbv(v)                                                                 \\
  {                                                                            \\
    cout << #v << " : ";                                                       \\
    for (auto x : v)                                                           \\
      cout << x << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die(x) return cout << x << '\\n', 0;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int MAX = 1e18;
int32_t main() {
  fastio;

  int testcases;
  testcases = 1;
  // cin>>testcases;

  while (testcases--) {
    int n;
    cin >> n;
    vector<int> vec(n);
    int ans = 1;
    int flag = 1;
    rep(i, 0, n - 1) {
      cin >> vec[i];
      if (vec[i] > MAX / ans && flag != 0) {
        flag = -1;
      }
      if (vec[i] == 0) {
        flag = 0;
      }
      ans = ans * vec[i];
    }
    //    cout << ans;
    if (flag != 0 && flag == -1)
      die(-1);
    die(ans);
  }

  return 0;
}