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

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const long long MOD = 1000000007;

// modint: mod 計算を int を扱うように扱える構造体
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;
  }
};

// a^n mod を計算する
lli modpow(lli a, lli n, lli mod) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^-1
lli modinv(lli a, lli mod) {
  lli b = mod, u = 1, v = 0;
  while (b) {
    lli t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

// const int MOD = 1000000007;
using mint = Fp<MOD>;

void func(long long N, long long K, std::vector<long long> A) {

  priority_queue<lli> plQ, minQ;

  vector<pair<lli, lli>> anss;

  lli minCnt = 0;
  lli zeroCnt = 0;
  REP(i, 0, N) {
    if (A[i] == 0)
      zeroCnt++;
    else if (A[i] > 0)
      plQ.push(A[i]);
    else {
      minQ.push(-A[i]);
      minCnt++;
    }
  }

  if (zeroCnt > N - K) {
    cout << 0 << endl;
    return;
  }

  lli lefCnt = N - zeroCnt;

  if ((lefCnt == K && minCnt % 2 == 1) || (minCnt == lefCnt && K % 2 == 1)) {
    mint ans = 1;
    vector<lli> c(N);
    REP(i, 0, N) c[i] = abs(A[i]);
    SORT(c);

    REP(i, 0, K) ans = ans * c[i];
    mint ans2 = 0;
    ans2 = ans2 - ans;
    cout << ans2 << endl;
    return;
  }

  vector<pair<lli, lli>> c;
  REP(i, 0, N) {
    if (A[i] == 0)
      continue;
    c.push_back({abs(A[i]), A[i] > 0});
  }
  sort(c.rbegin(), c.rend());

  lli mnCnt = 0;
  mint ans = 1;
  lli targetPlusNum = INF;
  lli targetMinusNum = INF;
  REP(i, 0, K) {
    LOG3(i, c[i].first, c[i].second);
    ans = ans * c[i].first;
    if (c[i].second == 0) {
      mnCnt++;
      chmin(targetMinusNum, c[i].first);
    } else {
      chmin(targetPlusNum, c[i].first);
    }
  }

  if (mnCnt % 2 == 0) {
    cout << ans << endl;
    return;
  }

  // 奇数を一つ外して偶数を一つ増やすか
  // もしくは偶数を一つ外して奇数を一つ増やす

  lli tarPlNum = -1, tarMiNum = -1;
  REP(i, K, c.size()) {
    if (c[i].second == 0) {
      chmax(tarMiNum, c[i].first);
    } else {
      chmax(tarPlNum, c[i].first);
    }
  }

  if (tarPlNum == -1 || targetMinusNum == INF) {
    ans = ans * modinv(targetPlusNum, MOD);
    ans = ans * tarMiNum;
    cout << ans << endl;
  } else if (tarMiNum == -1 || targetPlusNum == INF) {
    ans = ans * modinv(targetMinusNum, MOD);
    ans = ans * tarPlNum;
    cout << ans << endl;
  } else {
    double pl = tarMiNum / targetPlusNum;
    double pr = tarPlNum / targetMinusNum;

    if (tarMiNum * targetMinusNum >= targetPlusNum * tarPlNum) {
      ans = ans * modinv(targetPlusNum, MOD);
      ans = ans * tarMiNum;
      cout << ans << endl;
    } else {
      ans = ans * modinv(targetMinusNum, MOD);
      ans = ans * tarPlNum;
      cout << ans << endl;
    }
  }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  func(N, K, std::move(A));
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const long long MOD = 1000000007;

// modint: mod 計算を int を扱うように扱える構造体
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;
  }
};

// a^n mod を計算する
lli modpow(lli a, lli n, lli mod) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^-1
lli modinv(lli a, lli mod) {
  lli b = mod, u = 1, v = 0;
  while (b) {
    lli t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

// const int MOD = 1000000007;
using mint = Fp<MOD>;

void func(long long N, long long K, std::vector<long long> A) {

  priority_queue<lli> plQ, minQ;

  vector<pair<lli, lli>> anss;

  lli minCnt = 0;
  lli zeroCnt = 0;
  lli plCnt = 0;
  REP(i, 0, N) {
    if (A[i] == 0)
      zeroCnt++;
    else if (A[i] > 0) {
      plQ.push(A[i]);
      plCnt++;
    } else {
      minQ.push(-A[i]);
      minCnt++;
    }
  }

  lli lessZeroCnt = N - plCnt;
  if ((N == K && lessZeroCnt % 2 != 0) || plCnt == 0) {
    // どうやってもマイナスになるパターン
    mint ans = 1;
    vector<lli> c(N);
    REP(i, 0, N) c[i] = abs(A[i]);
    SORT(c);

    REP(i, 0, K) ans = ans * c[i];
    mint ans2 = 0;
    ans2 = ans2 - ans;
    cout << ans2 << endl;
    return;
  }

  vector<pair<lli, lli>> c;
  REP(i, 0, N) {
    if (A[i] == 0)
      continue;
    c.push_back({abs(A[i]), A[i] > 0});
  }
  sort(c.rbegin(), c.rend());

  lli mnCnt = 0;
  mint ans = 1;
  lli targetPlusNum = INF;
  lli targetMinusNum = INF;
  REP(i, 0, K) {
    LOG3(i, c[i].first, c[i].second);
    ans = ans * c[i].first;
    if (c[i].second == 0) {
      mnCnt++;
      chmin(targetMinusNum, c[i].first);
    } else {
      chmin(targetPlusNum, c[i].first);
    }
  }

  if (mnCnt % 2 == 0) {
    cout << ans << endl;
    return;
  }

  // 奇数を一つ外して偶数を一つ増やすか
  // もしくは偶数を一つ外して奇数を一つ増やす

  lli tarPlNum = -1, tarMiNum = -1;
  REP(i, K, c.size()) {
    if (c[i].second == 0) {
      chmax(tarMiNum, c[i].first);
    } else {
      chmax(tarPlNum, c[i].first);
    }
  }

  if (tarPlNum == -1 || targetMinusNum == INF) {
    ans = ans * modinv(targetPlusNum, MOD) * tarMiNum;
    cout << ans << endl;
  } else if (tarMiNum == -1 || targetPlusNum == INF) {
    ans = ans * modinv(targetMinusNum, MOD) * tarPlNum;
    cout << ans << endl;
  } else {
    double pl = tarMiNum / targetPlusNum;
    double pr = tarPlNum / targetMinusNum;

    if (tarMiNum * targetMinusNum >= targetPlusNum * tarPlNum) {
      ans = ans * modinv(targetPlusNum, MOD) * tarMiNum;
      cout << ans << endl;
    } else {
      ans = ans * modinv(targetMinusNum, MOD) * tarPlNum;
      cout << ans << endl;
    }
  }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  func(N, K, std::move(A));
  return 0;
}