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

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
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;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e18;

template <std::int_fast64_t Modulus> class modint {
  using u64 = std::int_fast64_t;

public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
typedef modint<MOD> ModInt;

ModInt mod_pow(ModInt x, ModInt n) {
  ModInt ret = 1;
  while (n.a > 0) {
    if (n.a & 1)
      (ret *= x);
    (x *= x);
    n.a >>= 1;
  }
  return ret;
}

ModInt func[200000];
void funcinit(int N) {
  func[0] = 1;
  for (int i = 1; i <= N; i++) {
    func[i] = func[i - 1] * i;
  }
}
ModInt comb(ModInt n, ModInt r) {
  if (n.a <= 0 || n.a < r.a) {
    return 0;
  }
  return func[n.a] / (func[r.a] * func[(n - r).a]);
}

lint N, K, _K, zero_cnt = 0;
lint arr[200000];
priority_queue<lint> pl;
priority_queue<lint> mn;
int main() {
  cin >> N >> K;
  _K = K;
  bool flag = true;
  REP(i, N) {
    cin >> arr[i];
    if (arr[i] >= 0) {
      pl.push(arr[i]);
      flag = false;
    } else
      mn.push(-arr[i]);
    if (arr[i] == 0)
      zero_cnt++;
  }

  ModInt ans = 1;

  if (flag && K % 2 == 1) {
    sort(arr, arr + N, greater<lint>());
    REP(i, K) { ans *= arr[i]; }
    cout << ans.a + MOD << endl;
    return 0;
  }

  while (K > 1) {
    lint plv = 1;
    if (pl.size() > 1) {
      plv *= pl.top();
      lint tmp = plv;
      pl.pop();
      plv *= pl.top();
      if (pl.size() > 1 && K % 2 == 1) {
        lint tmp2 = pl.top();
        pl.pop();
        if (pl.top() == 0) {
          plv *= pl.top();
        }
        pl.push(tmp2);
      }
      pl.push(tmp);
    }

    lint mnv = 1;
    if (mn.size() > 1) {
      mnv *= mn.top();
      lint tmp = mnv;
      mn.pop();
      mnv *= mn.top();
      mn.push(tmp);
    }

    if ((plv >= mnv && ((pl.size() - zero_cnt > 2 && K % 2 == 1) ||
                        (pl.size() > 1 && K % 2 == 0))) ||
        mn.size() < 2) {
      ans *= pl.top();
      pl.pop();
      K--;
    } else {
      ans *= mn.top();
      mn.pop();
      ans *= mn.top();
      mn.pop();
      K -= 2;
    }
  }
  if (pl.size() > 0 && K == 1) {
    ans *= pl.top();
  } else if (mn.size() > 0 && K == 1) {
    ans *= mn.top();
    ans = MOD - ans.a;
  }
  cout << ans.a << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
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;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e18;

template <std::int_fast64_t Modulus> class modint {
  using u64 = std::int_fast64_t;

public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
typedef modint<MOD> ModInt;

ModInt mod_pow(ModInt x, ModInt n) {
  ModInt ret = 1;
  while (n.a > 0) {
    if (n.a & 1)
      (ret *= x);
    (x *= x);
    n.a >>= 1;
  }
  return ret;
}

ModInt func[200000];
void funcinit(int N) {
  func[0] = 1;
  for (int i = 1; i <= N; i++) {
    func[i] = func[i - 1] * i;
  }
}
ModInt comb(ModInt n, ModInt r) {
  if (n.a <= 0 || n.a < r.a) {
    return 0;
  }
  return func[n.a] / (func[r.a] * func[(n - r).a]);
}

lint N, K, _K, zero_cnt = 0;
lint arr[200000];
priority_queue<lint> pl;
priority_queue<lint> mn;
int main() {
  cin >> N >> K;
  _K = K;
  bool flag = true;
  REP(i, N) {
    cin >> arr[i];
    if (arr[i] >= 0) {
      pl.push(arr[i]);
      flag = false;
    } else
      mn.push(-arr[i]);
    if (arr[i] == 0)
      zero_cnt++;
  }

  ModInt ans = 1;

  if (flag && K % 2 == 1) {
    sort(arr, arr + N, greater<lint>());
    REP(i, K) { ans *= arr[i]; }
    cout << ans.a + MOD << endl;
    return 0;
  }

  while (K > 1) {
    lint plv = 1;
    bool fl = false;
    if (pl.size() > 1) {
      plv *= pl.top();
      lint tmp = plv;
      pl.pop();
      plv *= pl.top();
      if (pl.size() > 1 && K % 2 == 1) {
        lint tmp2 = pl.top();
        pl.pop();
        if (pl.top() == 0) {
          flag = true;
        }
        pl.push(tmp2);
      }
      pl.push(tmp);
    }

    lint mnv = 1;
    if (mn.size() > 1) {
      mnv *= mn.top();
      lint tmp = mnv;
      mn.pop();
      mnv *= mn.top();
      mn.push(tmp);
    }

    if (flag ||
        (plv >= mnv && ((pl.size() - zero_cnt > 2 && K % 2 == 1) ||
                        (pl.size() > 1 && K % 2 == 0))) ||
        mn.size() < 2) {
      ans *= pl.top();
      pl.pop();
      ans *= pl.top();
      pl.pop();
      K -= 2;
    } else {
      ans *= mn.top();
      mn.pop();
      ans *= mn.top();
      mn.pop();
      K -= 2;
    }
  }
  if (pl.size() > 0 && K == 1) {
    ans *= pl.top();
  } else if (mn.size() > 0 && K == 1) {
    ans *= mn.top();
    ans = MOD - ans.a;
  }
  cout << ans.a << endl;
}