Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Jul  5 21:54:22 JST 2020
 **/

template <class T> T pow(T x, long long n, const T UNION = 1) {
  T ret = UNION;
  while (n) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
}

/// @docs src/Math/ModInt.md
template <int Mod> struct ModInt {
  int x;
  static int runtime_mod;
  static std::unordered_map<int, int> to_inv;
  // テンプレート引数が負のときは実行時ModInt
  static int mod() { return Mod < 0 ? runtime_mod : Mod; }
  static void set_runtime_mod(int mod) {
    static_assert(Mod < 0,
                  "template parameter Mod must be negative for runtime ModInt");
    runtime_mod = mod;
    to_inv.clear();
  }
  ModInt() : x(0) {}
  ModInt(long long x_) {
    if ((x = x_ % mod() + mod()) >= mod())
      x -= mod();
  }

  ModInt &operator+=(ModInt rhs) {
    if ((x += rhs.x) >= mod())
      x -= mod();
    return *this;
  }
  ModInt &operator*=(ModInt rhs) {
    x = (unsigned long long)x * rhs.x % mod();
    return *this;
  }
  ModInt &operator-=(ModInt rhs) {
    if ((x -= rhs.x) < 0)
      x += mod();
    return *this;
  }
  ModInt &operator/=(ModInt rhs) {
    x = (unsigned long long)x * rhs.inv().x % mod();
    return *this;
  }
  ModInt operator-() const { return -x < 0 ? mod() - x : -x; }
  ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
  bool operator==(ModInt rhs) const { return x == rhs.x; }
  bool operator!=(ModInt rhs) const { return x != rhs.x; }
  ModInt inv() const {
    return to_inv.count(this->x) ? to_inv[this->x]
                                 : (to_inv[this->x] = pow(*this, mod() - 2).x);
  }

  friend std::ostream &operator<<(std::ostream &s, ModInt<Mod> a) {
    s << a.x;
    return s;
  }
  friend std::istream &operator>>(std::istream &s, ModInt<Mod> &a) {
    long long tmp;
    s >> tmp;
    a = ModInt<Mod>(tmp);
    return s;
  }
  friend std::string to_string(ModInt<Mod> a) { return std::to_string(a.x); }
};
template <int Mod> std::unordered_map<int, int> ModInt<Mod>::to_inv;
template <int Mod> int ModInt<Mod>::runtime_mod;

#ifndef CALL_FROM_TEST
using mint = ModInt<1000000007>;
#endif

signed main() {
  Int n, K;
  std::cin >> n >> K;
  std::vector<Int> a(n);
  REP(i, n) std::cin >> a[i];
  std::sort(a.begin(), a.end());
  mint ans = 1;
  if (K == n) {
    REP(i, n) ans *= a[i];
  } else if (a[0] >= 0 && a.back() >= 0) {
    for (Int i = n - 1; i >= n - K; i--)
      ans *= a[i];
  } else if (a[0] <= 0 && a.back() <= 0) {
    if (K % 2)
      std::reverse(a.begin(), a.end());
    REP(i, K) ans *= a[i];
  } else {
    std::vector<Int> minus, plus;
    REP(i, n) { (a[i] >= 0 ? plus : minus).push_back(std::abs(a[i])); }
    std::sort(minus.rbegin(), minus.rend());
    std::sort(plus.rbegin(), plus.rend());
    Int p = std::max(0LL, K - (Int)plus.size());
    if (p % 2)
      p++;
    while (1) {
      if (p + 1 >= minus.size())
        break;
      if (minus[p] * minus[p + 1] < plus[K - p - 1] * plus[K - p - 2])
        break;
      p += 2;
    }
    assert(K - p <= plus.size());
    REP(i, p) ans *= minus[i];
    REP(i, K - p) ans *= plus[i];
  }
  std::cout << ans << std::endl;
}


Fixed submission:
#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Jul  5 21:54:22 JST 2020
 **/

template <class T> T pow(T x, long long n, const T UNION = 1) {
  T ret = UNION;
  while (n) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
}

/// @docs src/Math/ModInt.md
template <int Mod> struct ModInt {
  int x;
  static int runtime_mod;
  static std::unordered_map<int, int> to_inv;
  // テンプレート引数が負のときは実行時ModInt
  static int mod() { return Mod < 0 ? runtime_mod : Mod; }
  static void set_runtime_mod(int mod) {
    static_assert(Mod < 0,
                  "template parameter Mod must be negative for runtime ModInt");
    runtime_mod = mod;
    to_inv.clear();
  }
  ModInt() : x(0) {}
  ModInt(long long x_) {
    if ((x = x_ % mod() + mod()) >= mod())
      x -= mod();
  }

  ModInt &operator+=(ModInt rhs) {
    if ((x += rhs.x) >= mod())
      x -= mod();
    return *this;
  }
  ModInt &operator*=(ModInt rhs) {
    x = (unsigned long long)x * rhs.x % mod();
    return *this;
  }
  ModInt &operator-=(ModInt rhs) {
    if ((x -= rhs.x) < 0)
      x += mod();
    return *this;
  }
  ModInt &operator/=(ModInt rhs) {
    x = (unsigned long long)x * rhs.inv().x % mod();
    return *this;
  }
  ModInt operator-() const { return -x < 0 ? mod() - x : -x; }
  ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
  bool operator==(ModInt rhs) const { return x == rhs.x; }
  bool operator!=(ModInt rhs) const { return x != rhs.x; }
  ModInt inv() const {
    return to_inv.count(this->x) ? to_inv[this->x]
                                 : (to_inv[this->x] = pow(*this, mod() - 2).x);
  }

  friend std::ostream &operator<<(std::ostream &s, ModInt<Mod> a) {
    s << a.x;
    return s;
  }
  friend std::istream &operator>>(std::istream &s, ModInt<Mod> &a) {
    long long tmp;
    s >> tmp;
    a = ModInt<Mod>(tmp);
    return s;
  }
  friend std::string to_string(ModInt<Mod> a) { return std::to_string(a.x); }
};
template <int Mod> std::unordered_map<int, int> ModInt<Mod>::to_inv;
template <int Mod> int ModInt<Mod>::runtime_mod;

#ifndef CALL_FROM_TEST
using mint = ModInt<1000000007>;
#endif

signed main() {
  Int n, K;
  std::cin >> n >> K;
  std::vector<Int> a(n);
  REP(i, n) std::cin >> a[i];
  std::sort(a.begin(), a.end());
  mint ans = 1;
  if (K == n) {
    REP(i, n) ans *= a[i];
  } else if (a[0] >= 0 && a.back() >= 0) {
    for (Int i = n - 1; i >= n - K; i--)
      ans *= a[i];
  } else if (a[0] <= 0 && a.back() <= 0) {
    if (K % 2)
      std::reverse(a.begin(), a.end());
    REP(i, K) ans *= a[i];
  } else {
    std::vector<Int> minus, plus;
    REP(i, n) { (a[i] >= 0 ? plus : minus).push_back(std::abs(a[i])); }
    std::sort(minus.rbegin(), minus.rend());
    std::sort(plus.rbegin(), plus.rend());
    Int p = std::max(0LL, K - (Int)plus.size());
    if (p % 2)
      p++;
    while (1) {
      if (p + 1 >= minus.size())
        break;
      if (K - p - 2 < 0)
        break;
      if (minus[p] * minus[p + 1] < plus[K - p - 1] * plus[K - p - 2])
        break;
      p += 2;
    }
    assert(K - p <= plus.size());
    REP(i, p) ans *= minus[i];
    REP(i, K - p) ans *= plus[i];
  }
  std::cout << ans << std::endl;
}