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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

template <class T> struct BiCoef {
  vector<T> fact_, inv_, finv_;
  constexpr BiCoef() {}
  constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
    init(n);
  }
  constexpr void init(int n) noexcept {
    fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
    int MOD = fact_[0].getmod();
    for (int i = 2; i < n; i++) {
      fact_[i] = fact_[i - 1] * i;
      inv_[i] = -inv_[MOD % i] * (MOD / i);
      finv_[i] = finv_[i - 1] * inv_[i];
    }
  }
  constexpr T com(int n, int k) const noexcept {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fact_[n] * finv_[k] * finv_[n - k];
  }
  constexpr T fact(int n) const noexcept {
    if (n < 0)
      return 0;
    return fact_[n];
  }
  constexpr T inv(int n) const noexcept {
    if (n < 0)
      return 0;
    return inv_[n];
  }
  constexpr T finv(int n) const noexcept {
    if (n < 0)
      return 0;
    return finv_[n];
  }
  constexpr T perm(int n, int k) const noexcept {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fact_[n] * finv_[n - k];
  }
};

using mint = Fp<MOD>;
BiCoef<mint> bc;
// using vec = vector<mint>;
// using mat = vector<vec>;

void solve() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  int plus = 0, minus = 0;

  REP(i, N) {
    cin >> A[i];
    if (A[i] >= 0)
      plus++;
    else
      minus++;
  }
  mint ans = 1;
  if (N == K) {
    REP(i, N) ans *= A[i];
    cout << ans << endl;
    return;
  }
  if ((K & 1) && plus == 0) {
    REP(i, N) if (A[i] < 0) A[i] *= -1;
    sort(A.begin(), A.end());
    REP(i, K) ans *= A[i];
    ans = -ans;
    cout << ans << endl;
    return;
  }
  bool flg = false;
  sort(A.begin(), A.end(), [&](int x, int y) { return abs(x) > abs(y); });
  ll m = -LINF, p = LINF;
  // REP(i,N) cout << A[i] << " ";
  // cout << endl;
  REP(i, K) {
    ans *= A[i];
    if (A[i] < 0) {
      flg = 1 - flg;
      chmax(m, A[i]);
    } else
      chmin(p, A[i]);
  }

  if (!flg) {
    cout << ans << endl;
    return;
  }

  ll c1 = LINF, c2 = LINF;
  for (int i = K; i < N; i++) {
    if (A[i] >= 0 && c1 == LINF) {
      c1 = A[i];
    }
    if (A[i] < 0 && c2 == LINF) {
      c2 = A[i];
    }
  }

  if ((p != LINF && c2 != LINF) && (m == -LINF || c1 == LINF))
    cout << ans / p * c2 << endl;
  else if ((p == LINF || c2 == LINF) && (m != -LINF && c1 != LINF))
    cout << ans / m * c1 << endl;
  else if (abs(c1 * p) <= abs(c2 * m))
    cout << ans / p * c2 << endl;
  else
    cout << ans / m * c1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  // int T; cin >> T; REP(t,T) solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

template <class T> struct BiCoef {
  vector<T> fact_, inv_, finv_;
  constexpr BiCoef() {}
  constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
    init(n);
  }
  constexpr void init(int n) noexcept {
    fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
    int MOD = fact_[0].getmod();
    for (int i = 2; i < n; i++) {
      fact_[i] = fact_[i - 1] * i;
      inv_[i] = -inv_[MOD % i] * (MOD / i);
      finv_[i] = finv_[i - 1] * inv_[i];
    }
  }
  constexpr T com(int n, int k) const noexcept {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fact_[n] * finv_[k] * finv_[n - k];
  }
  constexpr T fact(int n) const noexcept {
    if (n < 0)
      return 0;
    return fact_[n];
  }
  constexpr T inv(int n) const noexcept {
    if (n < 0)
      return 0;
    return inv_[n];
  }
  constexpr T finv(int n) const noexcept {
    if (n < 0)
      return 0;
    return finv_[n];
  }
  constexpr T perm(int n, int k) const noexcept {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fact_[n] * finv_[n - k];
  }
};

using mint = Fp<MOD>;
BiCoef<mint> bc;
// using vec = vector<mint>;
// using mat = vector<vec>;

void solve() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  int plus = 0, minus = 0;
  ll ma = 0;
  REP(i, N) {
    cin >> A[i];
    if (A[i] > 0)
      plus++;
    else if (A[i] < 0)
      minus++;
    chmax(ma, A[i]);
  }

  if (K == 1) {
    cout << ma << endl;
    return;
  }

  if (plus + minus < K) {
    cout << 0 << endl;
    return;
  }

  mint ans = 1;

  if (minus == 0) {
    sort(A.rbegin(), A.rend());
    REP(i, K) ans *= A[i];
    cout << ans << endl;
    return;
  }

  if (K & 1) {
    assert(K % 2 == 0);
    if (plus == 0) {
      sort(A.rbegin(), A.rend());
      REP(i, K) ans *= A[i];
      cout << ans << endl;
      return;
    } else if (plus - (plus % 2 == 0) + minus / 2 * 2 < K) {
      REP(i, N) if (A[i] < 0) A[i] *= -1;
      sort(A.begin(), A.end());
      REP(i, K) ans *= A[i];
      ans = -ans;
      cout << ans << endl;
      return;
    }
  }
  priority_queue<int, vector<int>, greater<int>> pq1, pq2;

  REP(i, N) {
    if (A[i] > 0) {
      pq1.push(A[i]);
      ans *= A[i];
    } else
      pq2.push(A[i]);
  }

  int sz = pq1.size();
  while (sz > K)
    pq1.pop(), sz--;
  while (sz + 2 <= K && (int)pq2.size() > 1) {
    ans *= pq2.top();
    pq2.pop();
    ans *= pq2.top();
    pq2.pop();
    sz += 2;
  }

  while ((int)pq2.size() > 1 && (int)pq1.size() > 1) {
    ll m1 = pq2.top();
    pq2.pop();
    ll m2 = pq2.top();
    pq2.pop();
    ll p1 = pq1.top();
    pq1.pop();
    ll p2 = pq1.top();
    pq1.pop();
    if (m1 * m2 > p1 * p2) {
      ans /= p1;
      ans /= p2;
      ans *= (m1 * m2);
    }
  }

  if (sz < K) {
    REP(i, N) if (A[i] < 0) A[i] *= -1;
    sort(A.begin(), A.end());
    REP(i, K) ans *= A[i];
    ans = -ans;
    cout << ans << endl;
    return;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  // int T; cin >> T; REP(t,T) solve();
}