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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) FOR(i, a, b + 1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n + 1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr << __LINE__ << ' ' << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }

namespace ModInt {
constexpr long long MOD = (long long)1e9 + 7;
struct mint {
  long long x;

  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    mint res = 1, a(*this);
    while (t) {
      if (t & 1)
        res *= a;
      t >>= 1, a *= a;
    }
    return res;
  }
  mint invPow(long long t) const { return pow(MOD - 1 - t); }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend istream &operator>>(istream &in, mint &a) {
    long long _v;
    in >> _v;
    a = mint(_v);
    return in;
  }
  friend ostream &operator<<(ostream &out, const mint &a) { return out << a.x; }
  friend bool operator==(const mint &a, const mint &b) { return a.x == b.x; }
  friend bool operator!=(const mint &a, const mint &b) { return a.x != b.x; }
};
template <typename T, typename U> mint pow(T a, U x) {
  // return a^x mod MOD
  return mint(a).pow(x);
}
template <typename T, typename U> mint invPow(T a, U x) {
  // return 1 / (a^x) mod MOD
  return mint(a).pow(MOD - 1 - x);
}
mint operator+(const int a, const mint b) { return b + a; }
mint operator+(const long long a, const mint b) { return b + a; }
mint operator-(const int a, const mint b) { return mint(MOD - b.x) + a; }
mint operator-(const long long a, const mint b) { return mint(MOD - b.x) + a; }
mint operator*(const int a, const mint b) { return b * a; }
mint operator*(const long long a, const mint b) { return b * a; }
mint operator/(const int a, const mint b) { return b.inv() * a; }
mint operator/(const long long a, const mint b) { return b.inv() * a; }
} // namespace ModInt

using namespace ModInt;

bool cmp(lint &x1, lint &x2) { return abs(x1) < abs(x2); }

class EMultiplication4 {
public:
  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    int N, K;
    in >> N >> K;
    vc<lint> A(N), pos, neg;
    for (auto &a : A) {
      in >> a;
      if (a < 0)
        neg.pb(a);
      else
        pos.pb(a);
    }
    bool ok = false;
    if (N == K) {
      if (LEN(neg) % 2 == 0)
        ok = true;
    } else {
      if (LEN(pos) or K % 2 == 0)
        ok = true;
    }
    mint ans = 1;
    if (not ok) {
      sort(RALL(A));
      REP(i, K) ans *= A[i];
      out << ans << '\\n';
      return;
    }
    sort(RALL(pos));
    sort(ALL(neg));
    if (K % 2) {
      ans *= pos.front();
      pos.erase(pos.begin());
    }
    vc<lint> B;
    for (int i = 0; i + 1 < LEN(pos); i += 2)
      B.pb(pos[i] * pos[i + 1]);
    for (int i = 0; i + 1 < LEN(neg); i += 2)
      B.pb(neg[i] * neg[i + 1]);
    sort(RALL(B));
    REP(i, K / 2) ans *= B[i];
    out << ans << '\\n';
  }
};

int main() {
  EMultiplication4 solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) FOR(i, a, b + 1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n + 1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr << __LINE__ << ' ' << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }

namespace ModInt {
constexpr long long MOD = (long long)1e9 + 7;
struct mint {
  long long x;

  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    mint res = 1, a(*this);
    while (t) {
      if (t & 1)
        res *= a;
      t >>= 1, a *= a;
    }
    return res;
  }
  mint invPow(long long t) const { return pow(MOD - 1 - t); }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend istream &operator>>(istream &in, mint &a) {
    long long _v;
    in >> _v;
    a = mint(_v);
    return in;
  }
  friend ostream &operator<<(ostream &out, const mint &a) { return out << a.x; }
  friend bool operator==(const mint &a, const mint &b) { return a.x == b.x; }
  friend bool operator!=(const mint &a, const mint &b) { return a.x != b.x; }
};
template <typename T, typename U> mint pow(T a, U x) {
  // return a^x mod MOD
  return mint(a).pow(x);
}
template <typename T, typename U> mint invPow(T a, U x) {
  // return 1 / (a^x) mod MOD
  return mint(a).pow(MOD - 1 - x);
}
mint operator+(const int a, const mint b) { return b + a; }
mint operator+(const long long a, const mint b) { return b + a; }
mint operator-(const int a, const mint b) { return mint(MOD - b.x) + a; }
mint operator-(const long long a, const mint b) { return mint(MOD - b.x) + a; }
mint operator*(const int a, const mint b) { return b * a; }
mint operator*(const long long a, const mint b) { return b * a; }
mint operator/(const int a, const mint b) { return b.inv() * a; }
mint operator/(const long long a, const mint b) { return b.inv() * a; }
} // namespace ModInt

using namespace ModInt;

bool cmp(lint &x1, lint &x2) { return abs(x1) < abs(x2); }

class EMultiplication4 {
public:
  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    int N, K, cn = 0;
    in >> N >> K;
    vc<lint> A(N);
    for (auto &a : A) {
      in >> a;
      if (a < 0)
        ++cn;
    }
    mint ans = 1;
    if (N == K or cn == N) {
      sort(RALL(A));
      REP(i, K) ans *= A[i];
      out << ans << '\\n';
      return;
    }
    sort(RALL(A), cmp);
    vc<lint> pos, neg;
    REP(i, K) {
      ans *= A[i];
      if (A[i] >= 0)
        pos.pb(A[i]);
      else
        neg.pb(A[i]);
    }
    if (LEN(neg) % 2 == 0)
      out << ans << '\\n';
    else {
      lint pos_mx = -1, neg_mx = 1;
      FOR(i, K, N) {
        if (A[i] >= 0 and pos_mx == -1)
          pos_mx = A[i];
        if (A[i] < 0 and neg_mx == 1)
          neg_mx = A[i];
      }
      if (pos_mx == -1 and neg_mx < 0 and not pos.empty()) {
        ans = ans / pos.back() * neg_mx;
      } else if (pos_mx >= 0 and neg_mx == 1) {
        ans = ans / neg.back() * pos_mx;
      } else if (pos_mx != -1 and neg_mx != 1) {
        if (pos_mx * (pos.empty() ? lint(2e9) : pos.back()) >
            neg_mx * neg.back()) {
          ans = ans / neg.back() * pos_mx;
        } else {
          ans = ans / pos.back() * neg_mx;
        }
      }
      out << ans << '\\n';
    }
  }
};

int main() {
  EMultiplication4 solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}