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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
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 ll MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> M;
  vector<ll> P;
  bool zero = false;
  REP(i, N) {
    ll a;
    cin >> a;
    if (a == 0)
      zero = true;
    if (a < 0)
      M.push_back(-a);
    else
      P.push_back(a);
  }

  ll MS = M.size();
  ll PS = P.size();

  ll ans = 1;

  if (N == K) {
    for (auto &&x : M)
      ans = (ans * x) % MOD;
    for (auto &&x : P)
      ans = (ans * x) % MOD;
    if (MS % 2)
      ans = MOD - ans;
    ans %= MOD;
  }
  // positive
  else if (!(MS == N && K % 2)) {
    if (K == 2 && MS == 1 && PS == 1) {
      ans *= P[0];
      ans %= MOD;
      ans *= M[0];
      ans %= MOD;
    } else {
      sort(M.rbegin(), M.rend());
      sort(P.rbegin(), P.rend());
      P.push_back(-1);

      int num = K;
      int pidx = 0;
      int midx = 0;
      while (num > 0) {
        if (num == 1 || (MS - midx) < 2) {
          ans *= P[pidx];
          ans %= MOD;
          pidx++;
          num--;
        } else {
          ll mmul = M[midx] * M[midx + 1];
          ll pmul = P[pidx] * P[pidx + 1];
          if (mmul >= pmul) {
            ans *= (mmul % MOD);
            ans %= MOD;
            midx += 2;
            num -= 2;
          } else {
            ans *= P[pidx];
            ans %= MOD;
            pidx++;
            num--;
          }
        }
      }
    }
  }
  // negative
  else {
    if (zero)
      ans = 0;
    else {
      priority_queue<ll, vector<ll>, greater<ll>> que;
      for (auto &&x : M)
        que.push(x);
      for (auto &&x : P)
        que.push(x);
      REP(i, K) {
        auto x = que.top();
        que.pop();
        ans *= x;
        ans %= MOD;
      }

      ans = MOD - ans;
      ans %= MOD;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
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 ll MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> M;
  vector<ll> P;
  bool zero = false;
  REP(i, N) {
    ll a;
    cin >> a;
    if (a == 0)
      zero = true;
    if (a < 0)
      M.push_back(-a);
    else
      P.push_back(a);
  }

  ll MS = M.size();
  ll PS = P.size();

  ll ans = 1;

  if (N == K) {
    for (auto &&x : M)
      ans = (ans * x) % MOD;
    for (auto &&x : P)
      ans = (ans * x) % MOD;
    if (MS % 2)
      ans = MOD - ans;
    ans %= MOD;
  }
  // positive
  else if (!(MS == N && K % 2)) {
    if (K == 2 && MS == 1 && PS == 1) {
      ans *= P[0];
      ans %= MOD;
      ans *= M[0];
      ans %= MOD;
    } else {
      sort(M.rbegin(), M.rend());
      sort(P.rbegin(), P.rend());

      int num = K;
      int pidx = 0;
      int midx = 0;
      while (num > 0) {
        if (num == 1 || (MS - midx) < 2) {
          ans *= P[pidx];
          ans %= MOD;
          pidx++;
          num--;
        } else {
          ll mmul = M[midx] * M[midx + 1];
          ll pmul = P[pidx] * P[pidx + 1];
          if (mmul >= pmul) {
            ans *= (mmul % MOD);
            ans %= MOD;
            midx += 2;
            num -= 2;
          } else {
            ans *= P[pidx];
            ans %= MOD;
            pidx++;
            num--;
          }
        }
      }
    }
  }
  // negative
  else {
    if (zero)
      ans = 0;
    else {
      priority_queue<ll, vector<ll>, greater<ll>> que;
      for (auto &&x : M)
        que.push(x);
      for (auto &&x : P)
        que.push(x);
      REP(i, K) {
        auto x = que.top();
        que.pop();
        ans *= x;
        ans %= MOD;
      }

      ans = MOD - ans;
      ans %= MOD;
    }
  }

  cout << ans << endl;

  return 0;
}