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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

ll mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;

  int c_p = 0, c_n = 0;
  ll *a = new ll[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      c_p++;
    else
      c_n++;
  }

  ll ans = 1;

  if (N == K) {
    for (int i = 0; i < N; i++) {
      ans *= a[i];
      ans %= mod;
    }
    ans = (ans + mod) % mod;
    cout << ans << endl;

    return 0;
  }

  if (K % 2 == 1 && c_p == 0) {
    sort(a, a + N, greater<ll>());
    for (int i = 0; i < K; i++) {
      ans *= a[i];
      ans %= mod;
    }
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }

  vector<ll> b, c;
  priority_queue<ll> que_p, que_n;
  for (int i = 0; i < N; i++) {
    if (a[i] >= 0)
      que_p.push(a[i]);
    else
      que_n.push(-a[i]);
  }

  for (int i = 0; i < K; i++) {
    if (que_p.empty()) {
      c.push_back(que_n.top());
      que_n.pop();
    } else if (que_n.empty()) {
      b.push_back(que_p.top());
      que_p.pop();
    } else {
      ll p = que_p.top(), n = que_n.top();
      if (p >= n) {
        b.push_back(p);
        que_p.pop();
      } else {
        c.push_back(n);
        que_n.pop();
      }
    }
  }

  if (c.size() % 2 == 0) {
    for (int i = 0; (ll)i < (ll)b.size(); i++) {
      ans *= b[i];
      ans %= mod;
    }
    for (int i = 0; (ll)i < (ll)c.size(); i++) {
      ans *= c[i];
      ans %= mod;
    }
    ans = (ans + mod) % mod;
    cout << ans << endl;

    return 0;
  }

  if ((ll)b.size() == (ll)c_p) {
    b.pop_back();
    c.push_back(que_n.top());
  } else if ((ll)c.size() == (ll)c_n) {
    c.pop_back();
    b.push_back(que_p.top());
  } else {
    int k = b.size(), l = c.size();
    if (k >= 1 && l >= 1) {
      if (b[k - 1] * que_p.top() <= c[l - 1] * que_n.top()) {
        b.pop_back();
        c.push_back(que_n.top());
      } else {
        c.pop_back();
        b.push_back(que_p.top());
      }
    }

    else if (l >= 1) {
      c.pop_back();
      b.push_back(que_p.top());
    } else {
      b.pop_back();
      c.push_back(que_n.top());
    }
  }

  for (int i = 0; (ll)i < (ll)b.size(); i++) {
    ans *= b[i];
    ans %= mod;
  }
  for (int i = 0; (ll)i < (ll)c.size(); i++) {
    ans *= c[i];
    ans %= mod;
  }

  ans = (ans + mod) % mod;

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

ll mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;

  int c_p = 0, c_n = 0;
  ll *a = new ll[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      c_p++;
    else
      c_n++;
  }

  ll ans = 1;

  if (N == K) {
    for (int i = 0; i < N; i++) {
      ans *= a[i];
      ans %= mod;
    }
    ans = (ans + mod) % mod;
    cout << ans << endl;

    return 0;
  }

  if (K % 2 == 1 && c_p == 0) {
    sort(a, a + N, greater<ll>());
    for (int i = 0; i < K; i++) {
      ans *= a[i];
      ans %= mod;
    }
    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
  }

  vector<ll> b, c;
  priority_queue<ll> que_p, que_n;
  for (int i = 0; i < N; i++) {
    if (a[i] >= 0)
      que_p.push(a[i]);
    else
      que_n.push(-a[i]);
  }

  for (int i = 0; i < K; i++) {
    if (que_p.empty()) {
      c.push_back(que_n.top());
      que_n.pop();
    } else if (que_n.empty()) {
      b.push_back(que_p.top());
      que_p.pop();
    } else {
      ll p = que_p.top(), n = que_n.top();
      if (p >= n) {
        b.push_back(p);
        que_p.pop();
      } else {
        c.push_back(n);
        que_n.pop();
      }
    }
  }

  if (c.size() % 2 == 0) {
    for (int i = 0; (ll)i < (ll)b.size(); i++) {
      ans *= b[i];
      ans %= mod;
    }
    for (int i = 0; (ll)i < (ll)c.size(); i++) {
      ans *= c[i];
      ans %= mod;
    }
    ans = (ans + mod) % mod;
    cout << ans << endl;

    return 0;
  }

  if ((ll)b.size() == (ll)c_p) {
    b.pop_back();
    c.push_back(que_n.top());
  } else if ((ll)c.size() == (ll)c_n) {
    c.pop_back();
    b.push_back(que_p.top());
  } else {
    int k = b.size(), l = c.size();
    if (b[k - 1] * que_p.top() <= c[l - 1] * que_n.top()) {
      b.pop_back();
      c.push_back(que_n.top());
    } else {
      c.pop_back();
      b.push_back(que_p.top());
    }
  }

  for (int i = 0; (ll)i < (ll)b.size(); i++) {
    ans *= b[i];
    ans %= mod;
  }
  for (int i = 0; (ll)i < (ll)c.size(); i++) {
    ans *= c[i];
    ans %= mod;
  }

  ans = (ans + mod) % mod;

  cout << ans << endl;

  return 0;
}