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

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#ifdef _DEBUG
#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\\n"
#else
#define DUMP(x)
#endif
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define erep(i, n) EREP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) (r).begin(), (r).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
  os << "}";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  bool first = true;
  os << "{";
  for (const auto &e : m) {
    if (!first)
      os << ", ";
    os << "{" << e.first << ": " << e.second << "}";
    first = false;
  }
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{";
  bool first = true;
  for (const auto &e : s) {
    if (!first)
      os << ", ";
    os << e;
    first = false;
  }
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &s) {
  os << "{";
  bool first = true;
  for (const auto &e : s) {
    if (!first)
      os << ", ";
    os << e;
    first = false;
  }
  os << "}";
  return os;
}
template <typename T> T dup(T x, T y) { return (x + y - 1) / y; };
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
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;
}
struct in {
  const size_t n = 0;
  in() = default;
  in(size_t n) : n(n){};
  template <typename T> operator T() {
    T ret;
    cin >> ret;
    return ret;
  }
  template <typename T> operator vector<T>() {
    assert(n != 0);
    vector<T> ret(n);
    for (T &x : ret) {
      T tmp = in();
      x = tmp;
    }
    return ret;
  }
  template <typename T, typename U> operator pair<T, U>() {
    pair<T, U> ret;
    ret.first = in();
    ret.second = in();
    return ret;
  }
};

namespace fiore::impl {
template <typename T> inline void out_impl(const T &x, char end_char) {
  std::cout << x << end_char;
}
template <typename T> inline void out_impl(const vector<T> &x, char end_char) {
  bool first = true;
  for (const auto &e : x) {
    if (!first)
      std::cout << ' ';
    std::cout << e;
    first = false;
  }
  std::cout << end_char;
}
} // namespace fiore::impl
template <typename T> inline void out(const T &x) {
  fiore::impl::out_impl(x, '\\n');
};
template <typename T, typename U, typename... Args>
inline void out(const T &x, const U &y, const Args &...args) {
  fiore::impl::out_impl(x, ' ');
  out(y, args...);
}

using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using setint = set<int32_t>;
using qint = queue<int32_t>;
using qpint = queue<pint>;

constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;

constexpr int mod = 1'000'000'007;
// constexpr int mod = 998'244'353;
class mint {
private:
  std::int64_t m_x;

public:
  mint(const std::int64_t m_x = 0) : m_x((m_x % mod + mod) % mod) {}
  std::int64_t get() const { return m_x; }
  mint operator-() const { return mint(-m_x); }
  bool operator==(const mint a) const { return m_x == a.get(); }
  bool operator!=(const mint a) const { return !(*this == a); }
  mint &operator+=(const mint a) {
    if ((m_x += a.m_x) >= mod)
      m_x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((m_x += mod - a.m_x) >= mod)
      m_x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (m_x *= a.m_x) %= mod;
    return *this;
  }
  mint &operator++() { return *this += 1; }
  mint operator++(int) {
    mint tmp = *this;
    ++*this;
    return tmp;
  }
  mint operator--() { return *this -= 1; }
  mint operator--(int) {
    mint tmp = *this;
    --*this;
    return tmp;
  }
  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(const std::int64_t t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  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;
  }
};
istream &operator>>(istream &is, mint &a) {
  ll x;
  is >> x;
  a = x;
  return is;
}
ostream &operator<<(ostream &os, const mint &a) {
  os << a.get();
  return os;
}

void Main() {
  int n = in(), k = in();
  vll a = in(n);
  sort(ALL(a), [](ll lhs, ll rhs) { return abs(lhs) > abs(rhs); });

  if (a[k - 1] == 0) {
    out(0);
    return;
  }

  int neg_cnt = 0, pos_cnt = 0;
  rep(i, n) {
    if (a[i] < 0)
      ++neg_cnt;
    if (a[i] > 0)
      ++pos_cnt;
  }

  if (pos_cnt == 0 && k % 2 == 1) {
    if (a.back() == 0) {
      out(0);
    } else {
      mint ret = 1;
      sort(ALL(a), greater<ll>());
      rep(i, k) { ret *= a[i]; }
      out(ret);
    }
    return;
  }

  mint ans = 1;
  ll sgn = 1;
  vll pos, neg;
  rep(i, k) {
    if (a[i] > 0) {
      pos.pb(a[i]);
      sgn *= 1;
    }
    if (a[i] < 0) {
      neg.pb(a[i]);
      sgn *= -1;
    }
    ans *= a[i];
  }
  DUMP(sgn);
  if (sgn > 0 || n == k) {
    out(ans);
    return;
  }

  vll rest_pos, rest_neg;
  REP(i, k, n) {
    if (a[i] >= 0) {
      rest_pos.pb(a[i]);
    }
    if (a[i] <= 0) {
      rest_neg.pb(a[i]);
    }
  }

  DUMP(pos);
  DUMP(neg);
  DUMP(rest_pos);
  DUMP(rest_neg);

  DUMP(ans);

  if (pos.empty() || rest_neg.empty()) {
    // negを減らしてposを増やす
    out(ans / neg.back() * rest_pos.front());
    return;
  }
  if (neg.empty() || rest_pos.empty()) {
    // posを減らしてnegを減らす
    out(ans / pos.back() * rest_neg.front());
    return;
  }

  if (abs(pos.back()) * abs(rest_pos.front()) >
      abs(neg.back()) * abs(rest_neg.front())) {
    out(ans / neg.back() * rest_pos.front());
  } else {
    out(ans / pos.back() * rest_neg.front());
  }
}

signed main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#ifdef _DEBUG
#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\\n"
#else
#define DUMP(x)
#endif
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define erep(i, n) EREP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) (r).begin(), (r).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
  os << "}";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  bool first = true;
  os << "{";
  for (const auto &e : m) {
    if (!first)
      os << ", ";
    os << "{" << e.first << ": " << e.second << "}";
    first = false;
  }
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{";
  bool first = true;
  for (const auto &e : s) {
    if (!first)
      os << ", ";
    os << e;
    first = false;
  }
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &s) {
  os << "{";
  bool first = true;
  for (const auto &e : s) {
    if (!first)
      os << ", ";
    os << e;
    first = false;
  }
  os << "}";
  return os;
}
template <typename T> T dup(T x, T y) { return (x + y - 1) / y; };
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
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;
}
struct in {
  const size_t n = 0;
  in() = default;
  in(size_t n) : n(n){};
  template <typename T> operator T() {
    T ret;
    cin >> ret;
    return ret;
  }
  template <typename T> operator vector<T>() {
    assert(n != 0);
    vector<T> ret(n);
    for (T &x : ret) {
      T tmp = in();
      x = tmp;
    }
    return ret;
  }
  template <typename T, typename U> operator pair<T, U>() {
    pair<T, U> ret;
    ret.first = in();
    ret.second = in();
    return ret;
  }
};

namespace fiore::impl {
template <typename T> inline void out_impl(const T &x, char end_char) {
  std::cout << x << end_char;
}
template <typename T> inline void out_impl(const vector<T> &x, char end_char) {
  bool first = true;
  for (const auto &e : x) {
    if (!first)
      std::cout << ' ';
    std::cout << e;
    first = false;
  }
  std::cout << end_char;
}
} // namespace fiore::impl
template <typename T> inline void out(const T &x) {
  fiore::impl::out_impl(x, '\\n');
};
template <typename T, typename U, typename... Args>
inline void out(const T &x, const U &y, const Args &...args) {
  fiore::impl::out_impl(x, ' ');
  out(y, args...);
}

using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using setint = set<int32_t>;
using qint = queue<int32_t>;
using qpint = queue<pint>;

constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;

constexpr int mod = 1'000'000'007;
// constexpr int mod = 998'244'353;
class mint {
private:
  std::int64_t m_x;

public:
  mint(const std::int64_t m_x = 0) : m_x((m_x % mod + mod) % mod) {}
  std::int64_t get() const { return m_x; }
  mint operator-() const { return mint(-m_x); }
  bool operator==(const mint a) const { return m_x == a.get(); }
  bool operator!=(const mint a) const { return !(*this == a); }
  mint &operator+=(const mint a) {
    if ((m_x += a.m_x) >= mod)
      m_x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((m_x += mod - a.m_x) >= mod)
      m_x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (m_x *= a.m_x) %= mod;
    return *this;
  }
  mint &operator++() { return *this += 1; }
  mint operator++(int) {
    mint tmp = *this;
    ++*this;
    return tmp;
  }
  mint operator--() { return *this -= 1; }
  mint operator--(int) {
    mint tmp = *this;
    --*this;
    return tmp;
  }
  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(const std::int64_t t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  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;
  }
};
istream &operator>>(istream &is, mint &a) {
  ll x;
  is >> x;
  a = x;
  return is;
}
ostream &operator<<(ostream &os, const mint &a) {
  os << a.get();
  return os;
}

void Main() {
  int n = in(), k = in();
  vll a = in(n);
  sort(ALL(a), [](ll lhs, ll rhs) { return abs(lhs) > abs(rhs); });

  if (a[k - 1] == 0) {
    out(0);
    return;
  }

  int neg_cnt = 0, pos_cnt = 0;
  rep(i, n) {
    if (a[i] < 0)
      ++neg_cnt;
    if (a[i] > 0)
      ++pos_cnt;
  }

  if (pos_cnt == 0 && k % 2 == 1) {
    if (a.back() == 0) {
      out(0);
    } else {
      mint ret = 1;
      sort(ALL(a), greater<ll>());
      rep(i, k) { ret *= a[i]; }
      out(ret);
    }
    return;
  }

  mint ans = 1;
  ll sgn = 1;
  vll pos, neg;
  rep(i, k) {
    if (a[i] > 0) {
      pos.pb(a[i]);
      sgn *= 1;
    }
    if (a[i] < 0) {
      neg.pb(a[i]);
      sgn *= -1;
    }
    ans *= a[i];
  }
  DUMP(sgn);
  if (sgn > 0) {
    out(ans);
    return;
  }

  vll rest_pos, rest_neg;
  REP(i, k, n) {
    if (a[i] >= 0) {
      rest_pos.pb(a[i]);
    }
    if (a[i] <= 0) {
      rest_neg.pb(a[i]);
    }
  }

  DUMP(pos);
  DUMP(neg);
  DUMP(rest_pos);
  DUMP(rest_neg);

  DUMP(ans);

  if (pos.empty() || rest_neg.empty()) {
    // negを減らしてposを増やす
    out(ans / neg.back() * rest_pos.front());
    return;
  }
  if (neg.empty() || rest_pos.empty()) {
    // posを減らしてnegを減らす
    out(ans / pos.back() * rest_neg.front());
    return;
  }

  if (pos.back() * rest_pos.front() < neg.back() * rest_neg.front()) {
    out(ans / neg.back() * rest_pos.front());
  } else {
    out(ans / pos.back() * rest_neg.front());
  }
}

signed main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}