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

#include <bits/stdc++.h>
#include <queue>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const ll MOD = 1000000007;
int n, k, z;
vector<ll> p, m;

void solve_p() {
  sort(p.begin(), p.end(), greater<ll>());
  sort(m.begin(), m.end(), greater<ll>());

  queue<pair<ll, ll>> pq;
  rep(i, (int)p.size() - 1) pq.emplace(p[i], p[i] * p[i + 1]);
  if (p.size())
    pq.emplace(p[p.size() - 1], p[p.size() - 1]);
  queue<ll> mq;
  for (int i = 0; i < (int)m.size() - 1; i += 2)
    mq.push(m[i] * m[i + 1]);

  ll ans = 1;
  rep(i, k) {
    if (pq.empty() && mq.empty()) {
      cout << 0 << endl;
      return;
    }
    if (mq.empty()) {
      ans = (ans * pq.front().first) % MOD;
      pq.pop();
    } else if (pq.empty()) {
      ans = (ans * (mq.front() % MOD)) % MOD;
      mq.pop();
      i++;
    } else if (pq.size() == 1) {
      int nokori = k - i;
      if (i == k - 1) {
        ans = (ans * pq.front().first) % MOD;
        pq.pop();
      } else if (nokori % 2) {
        ans = (ans * pq.front().first) % MOD;
        pq.pop();
      } else {
        ans = (ans * (mq.front() % MOD)) % MOD;
        mq.pop();
        i++;
      }
    } else if (i == k - 1) {
      ans = (ans * pq.front().first) % MOD;
      pq.pop();
    } else if (mq.front() > pq.front().second) {
      ans = (ans * (mq.front() % MOD)) % MOD;
      mq.pop();
      i++;
    } else {
      ans = (ans * pq.front().first) % MOD;
      pq.pop();
    }
  }
  cout << ans << endl;
}

void solve_m() {
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  queue<ll> pq, mq;
  for (ll i : p)
    pq.push(i);
  for (ll i : m)
    mq.push(i);

  ll ans = 1;
  rep(i, k) {
    if (pq.empty() && mq.empty()) {
      ans = 0;
      break;
    }
    if (pq.empty()) {
      ans *= -(mq.front() % MOD);
      mq.pop();
    } else if (mq.empty()) {
      ans *= (pq.front() % MOD);
      pq.pop();
    } else if (mq.front() < pq.front()) {
      ans *= -(mq.front() % MOD);
      mq.pop();
    } else {
      ans *= (pq.front() % MOD);
      pq.pop();
    }
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
  }
  cout << ans << endl;
}

signed main() {
  cin >> n >> k;

  z = 0;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a == 0)
      ++z;
    else if (a > 0)
      p.push_back(a);
    else
      m.push_back(-a);
  }

  bool minus;
  if (k == n)
    minus = m.size() % 2 == 1;
  else if (p.size() == 0)
    minus = k % 2 == 1;
  else
    minus = false;

  if (minus && z) {
    cout << 0 << endl;
    return 0;
  }

  if (minus)
    solve_m();
  else
    solve_p();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const ll MOD = 1000000007;
int n, k, z;
vector<ll> p, m;

void solve_p() {
  sort(p.begin(), p.end(), greater<ll>());
  sort(m.begin(), m.end(), greater<ll>());

  queue<pair<ll, ll>> pq;
  rep(i, p.size() - 1) pq.emplace(p[i], p[i] * p[i + 1]);
  pq.emplace(p[p.size() - 1], p[p.size() - 1]);
  queue<ll> mq;
  for (int i = 0; i < (int)m.size() - 1; i += 2)
    mq.push(m[i] * m[i + 1]);

  ll ans = 1;
  rep(i, k) {
    if (pq.empty() && mq.empty()) {
      cout << 0 << endl;
      return;
    }
    if (mq.empty()) {
      ans = (ans * pq.front().first) % MOD;
      pq.pop();
      continue;
    } else if (pq.empty()) {
      ans = (ans * mq.front()) % MOD;
      mq.pop();
      i++;
    } else if (pq.size() == 1) {
      int nokori = k - i;
      if (i == k - 1) {
        ans = (ans * pq.front().first) % MOD;
        pq.pop();
      } else if (nokori % 2) {
        ans = (ans * pq.front().first) % MOD;
        pq.pop();
      } else {
        ans = (ans * mq.front()) % MOD;
        mq.pop();
        i++;
      }
    } else if (mq.front() > pq.front().second) {
      ans = (ans * mq.front()) % MOD;
      mq.pop();
      i++;
      continue;
    } else {
      ans = (ans * pq.front().first) % MOD;
      pq.pop();
    }
  }
  cout << ans << endl;
}

void solve_m() {
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  queue<ll> pq, mq;
  for (ll i : p)
    pq.push(i);
  for (ll i : m)
    mq.push(i);

  ll ans = 1;
  rep(i, k) {
    if (pq.empty() && mq.empty()) {
      ans = 0;
      break;
    }
    if (pq.empty()) {
      ans *= -mq.front();
      mq.pop();
    } else if (mq.empty()) {
      ans *= pq.front();
      pq.pop();
    } else if (mq.front() < pq.front()) {
      ans *= -mq.front();
      mq.pop();
    } else {
      ans *= pq.front();
      pq.pop();
    }
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
  }
  cout << ans << endl;
}

signed main() {
  cin >> n >> k;

  z = 0;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a == 0)
      ++z;
    else if (a > 0)
      p.push_back(a);
    else
      m.push_back(-a);
  }

  bool minus;
  if (k == n)
    minus = m.size() % 2 == 1;
  else if (p.size() == 0)
    minus = k % 2 == 1;
  else
    minus = false;

  if (minus)
    solve_m();
  else
    solve_p();
}