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

#include <bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 300010, p = 1e9 + 7;
ll bin_pow(ll v, ll t) {
  ll res = 1;
  for (; t; t >>= 1, v = v * v % p)
    if (t & 1)
      res = res * v % p;
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &i : a)
    cin >> i;
  ll all = 1;
  priority_queue<ll> pos, neg;
  int c0 = 0;
  for (auto i : a) {
    all = all * i % p;
    if (i == 0) {
      ++c0;
      continue;
    }
    (i > 0 ? pos : neg).push(abs(i));
  }
  if (k == n) {
    if (all < 0)
      all += p;
    cout << all << '\\n';
    return 0;
  }
  // cerr << c0 << '\\n';
  int op = pos.size(), on = neg.size();
  if (c0)
    if (pos.size() + neg.size() < k ||
        (pos.size() + neg.size() == k && (neg.size() & 1)) ||
        (pos.empty() && (k & 1))) {
      return cout << 0 << '\\n', 0;
    }

  if (pos.empty() && (k & 1)) {
    if (c0)
      return cout << 0 << '\\n', 0;
    sort(a.begin(), a.end(), greater<>());
    ll res = 1;
    for (int i = 0; i < k; ++i)
      res = res * a[i] % p;
    if (res < 0)
      res += p;
    cout << res << '\\n';
    return 0;
  }
  //	for (int i = 0;i < n;++i)
  //		pos.pb(0), neg.pb(0);

  ll res = 1, si = 1, cnt = 0;
  int i = 0, j = 0;
  int lstp = -1, lstn = -1;
  while (cnt < k) {
    if (pos.empty() || (neg.size() && neg.top() >= pos.top())) {
      res = res * neg.top() % p;
      lstn = neg.top();
      neg.pop();
      si *= -1;
    } else {
      res = res * pos.top() % p;
      lstp = pos.top();
      pos.pop();
    }
    ++cnt;
  }
  if (res < 0)
    res += p;
  if (si == -1) {
    ll a, b, c, d;
    a = lstp, b = lstn, c = (pos.size() ? pos.top() : -1),
    d = (neg.size() ? neg.top() : -1);
    cerr << "A B C D " << a << ' ' << b << ' ' << c << ' ' << d << '\\n';
    if ((a == -1 || d == -1) && (c == -1 || b == -1))
      ;
    else if (a != -1 && d != -1 && (b == -1 || c == -1 || (d * b >= a * c)))
      res = res * bin_pow(a, p - 2) % p * d % p;
    else
      res = res * bin_pow(b, p - 2) % p * c % p;
  }
  if (res < 0)
    res += p;
  cout << res << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 300010, p = 1e9 + 7;
ll bin_pow(ll v, ll t) {
  ll res = 1;
  for (; t; t >>= 1, v = v * v % p)
    if (t & 1)
      res = res * v % p;
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &i : a)
    cin >> i;

  priority_queue<ll> pos, neg;
  int c0 = 0;
  for (auto i : a) {
    if (i == 0) {
      ++c0;
      continue;
    }
    (i > 0 ? pos : neg).push(abs(i));
  }
  // cerr << c0 << '\\n';
  int op = pos.size(), on = neg.size();
  if (c0)
    if (pos.size() + neg.size() < k ||
        (pos.size() + neg.size() == k && (neg.size() & 1)) ||
        (pos.empty() && (k & 1))) {
      return cout << 0 << '\\n', 0;
    }

  if (pos.empty() && (k & 1)) {
    if (c0)
      return cout << 0 << '\\n', 0;
    sort(a.begin(), a.end(), greater<>());
    ll res = 1;
    for (int i = 0; i < k; ++i)
      res = res * a[i] % p;
    if (res < 0)
      res += p;
    cout << res << '\\n';
    return 0;
  }
  //	for (int i = 0;i < n;++i)
  //		pos.pb(0), neg.pb(0);

  ll res = 1, si = 1, cnt = 0;
  int i = 0, j = 0;
  int lstp = -1, lstn = -1;
  while (cnt < k) {
    if (pos.empty() || (neg.size() && neg.top() >= pos.top())) {
      res = res * neg.top() % p;
      lstn = neg.top();
      neg.pop();
      si *= -1;
    } else {
      res = res * pos.top() % p;
      lstp = pos.top();
      pos.pop();
    }
    ++cnt;
  }
  if (res < 0)
    res += p;
  if (si == -1) {
    ll a, b, c, d;
    a = lstp, b = lstn, c = (pos.size() ? pos.top() : -1),
    d = (neg.size() ? neg.top() : -1);
    cerr << "A B C D " << a << ' ' << b << ' ' << c << ' ' << d << '\\n';
    if ((a == -1 || d == -1) && (c == -1 || b == -1))
      assert(op + on != k);
    if (a != -1 && d != -1 && (b == -1 || c == -1 || (d * b >= a * c)))
      res = res * bin_pow(a, p - 2) % p * d % p;
    else
      res = res * bin_pow(b, p - 2) % p * c % p;
  }
  if (res < 0)
    res += p;
  cout << res << '\\n';
}