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

#include <bits/stdc++.h>
#include <random>
#include <unordered_set>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];

  priority_queue<pll> pq; //(값, 부호)
  for (int i = 0; i < N; i++)
    pq.push({abs(v[i]), v[i] >= 0});

  vector<int> neg, pos;
  for (int i = 0; i < K; i++) {
    pll a = pq.top();
    pq.pop();
    if (a.sc == 1)
      pos.push_back(a.fr);
    else
      neg.push_back(a.fr);
  }

  if (neg.size() % 2 == 1) {
    // 2가지 선택지가 있음 ( pos : a, b , neg : c, d)
    // 1. neg를 하나 더 추가한다. (c, d)
    // 2. neg를 하나 뺀다. (a, b)

    ll a, b, c, d;
    a = c = b = d = -1;
    if (pos.size())
      a = pos.back();
    if (neg.size())
      c = neg.back();

    while (pq.size()) {
      pll a = pq.top();
      pq.pop();
      if (a.sc == 0 && d == -1)
        d = a.fr;
      if (a.sc == 1 && b == -1)
        b = a.fr;

      if (b != -1 && d != -1)
        break;
    }

    bool flag = 1;
    if (b == -1 && d == -1)
      ;
    else if (b == -1) {
      if (pos.size()) {
        neg.push_back(d);
        pos.pop_back();
      }
    } else if (d == -1) {
      if (neg.size()) {
        pos.push_back(b);
        neg.pop_back();
      }
    } else {
      if (a * b > c * d) {
        if (neg.size()) {
          pos.push_back(b);
          neg.pop_back();
        } else if (pos.size()) {
          neg.push_back(d);
          pos.pop_back();
        }
      } else {
        if (pos.size()) {
          neg.push_back(d);
          pos.pop_back();
        } else if (neg.size()) {
          pos.push_back(b);
          neg.pop_back();
        }
      }
    }

    if (neg.size() % 2 == 1) {
      neg.clear();
      pos.clear();
      vector<pll> tmp;
      for (int i = 0; i < N; i++)
        tmp.push_back({abs(v[i]), v[i] >= 0});
      sort(all(tmp));
      for (int i = 0; i < K; i++) {
        if (tmp[i].sc == 1)
          pos.push_back(tmp[i].fr);
        else
          neg.push_back(tmp[i].fr);
      }
    }
  }

  ll ans = 1;
  for (int i = 0; i < pos.size(); i++)
    ans = ans * pos[i] % MOD;
  for (int i = 0; i < neg.size(); i++)
    ans = ans * -neg[i] % MOD;
  ans = (ans + MOD) % MOD;
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <random>
#include <unordered_set>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];

  priority_queue<pll> pq; //(값, 부호)
  for (int i = 0; i < N; i++)
    pq.push({abs(v[i]), v[i] >= 0});

  vector<int> neg, pos;
  for (int i = 0; i < K; i++) {
    pll a = pq.top();
    pq.pop();
    if (a.sc == 1)
      pos.push_back(a.fr);
    else
      neg.push_back(a.fr);
  }

  if (neg.size() % 2 == 1) {
    // 2가지 선택지가 있음 ( pos : a, b , neg : c, d)
    // 1. neg를 하나 더 추가한다. (c, d)
    // 2. neg를 하나 뺀다. (a, b)

    ll a, b, c, d;
    a = c = b = d = -1;
    if (pos.size())
      a = pos.back();
    if (neg.size())
      c = neg.back();

    while (pq.size()) {
      pll a = pq.top();
      pq.pop();
      if (a.sc == 0)
        d = a.fr;
      if (a.sc == 1)
        b = a.fr;

      if (b != -1 && d != -1)
        break;
    }

    bool flag = 1;
    if (b == -1 && d == -1)
      flag = 0;
    else if (b == -1) {
      if (pos.size()) {
        neg.push_back(d);
        pos.pop_back();
      } else
        flag = 0;
    } else if (d == -1) {
      if (neg.size()) {
        pos.push_back(b);
        neg.pop_back();
      } else
        flag = 0;
    } else {
      if (a * b > c * d) {
        pos.push_back(b);
        neg.pop_back();
      } else {
        neg.push_back(d);
        pos.pop_back();
      }
    }

    if (!flag) {
      neg.clear();
      pos.clear();
      vector<pll> tmp;
      for (int i = 0; i < N; i++)
        tmp.push_back({abs(v[i]), v[i] >= 0});
      sort(all(tmp));
      for (int i = 0; i < K; i++) {
        if (tmp[i].sc == 1)
          pos.push_back(tmp[i].fr);
        else
          neg.push_back(tmp[i].fr);
      }
    }
  }

  ll ans = 1;
  for (int i = 0; i < pos.size(); i++)
    ans = ans * pos[i] % MOD;
  for (int i = 0; i < neg.size(); i++)
    ans = ans * -neg[i] % MOD;
  ans = (ans + MOD) % MOD;
  cout << ans << '\\n';
  return 0;
}