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

#include <bits/stdc++.h>
#include <functional>
using namespace std;
using ll = long long;

int main() {
  ll n, k;
  cin >> n >> k;
  if (k == 1) {
    ll ans = -1e10;
    for (int i = 0; i < n; i++) {
      ll x;
      cin >> x;
      ans = max(ans, x);
    }
    cout << ans << endl;
    return 0;
  }
  const ll MOD = 1e9 + 7;
  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll x;
      cin >> x;
      ans *= x;
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  priority_queue<ll> ppq;
  priority_queue<ll, vector<ll>, greater<ll>> npq;
  vector<ll> a(n);
  bool alln = true;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x >= 0)
      ppq.emplace(x);
    if (x < 0)
      npq.emplace(x);
    a[i] = x;
    if (x > 0)
      alln = false;
  }
  if (alln) {
    if (k % 2)
      sort(a.rbegin(), a.rend());
    else
      sort(a.begin(), a.end());
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= a[i];
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  ll ans = 1;
  priority_queue<ll, vector<ll>, greater<ll>> ansp;
  priority_queue<ll> ansn;
  for (int i = 0; i < k; i++) {
    if (ppq.empty()) {
      ansn.push(npq.top());
      npq.pop();
      continue;
    }
    if (npq.empty()) {
      ansp.push(ppq.top());
      ppq.pop();
      continue;
    }

    if (ppq.top() >= -npq.top()) {
      ansp.emplace(ppq.top());
      ppq.pop();
    } else {
      ansn.emplace(npq.top());
      npq.pop();
    }
  }
  if (ansn.size() % 2) {
    if (ppq.empty()) {
      ansp.pop();
      ansn.push(npq.top());
    } else if (npq.empty()) {
      ansn.pop();
      ansp.push(ppq.top());
    } else if (ansp.empty()) {
      ansp.push(ppq.top());
      ansn.pop();
    } else {

      if (ansp.top() * ppq.top() > ansn.top() * npq.top()) {
        ansn.pop();
        ansp.push(ppq.top());
      } else {
        ansp.pop();
        ansn.push(npq.top());
      }
    }
  }
  while (ansp.size()) {
    ans = ans * ansp.top() % MOD;
    ansp.pop();
  }

  while (ansn.size()) {
    ans = ans * (ansn.top() + MOD) % MOD;
    ansn.pop();
  }
  cout << ans % MOD << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <functional>
using namespace std;
using ll = long long;

int main() {
  ll n, k;
  cin >> n >> k;
  if (k == 1) {
    ll ans = -1e10;
    for (int i = 0; i < n; i++) {
      ll x;
      cin >> x;
      ans = max(ans, x);
    }
    cout << ans << endl;
    return 0;
  }
  const ll MOD = 1e9 + 7;
  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll x;
      cin >> x;
      ans *= x;
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  priority_queue<ll> ppq;
  priority_queue<ll, vector<ll>, greater<ll>> npq;
  vector<ll> a(n);
  bool alln = true;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x >= 0)
      ppq.emplace(x);
    if (x < 0)
      npq.emplace(x);
    a[i] = x;
    if (x > 0)
      alln = false;
  }
  if (alln) {
    if (k % 2)
      sort(a.rbegin(), a.rend());
    else
      sort(a.begin(), a.end());
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= a[i];
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  ll ans = 1;
  priority_queue<ll, vector<ll>, greater<ll>> ansp;
  priority_queue<ll> ansn;
  for (int i = 0; i < k; i++) {
    if (ppq.empty()) {
      ansn.push(npq.top());
      npq.pop();
      continue;
    }
    if (npq.empty()) {
      ansp.push(ppq.top());
      ppq.pop();
      continue;
    }

    if (ppq.top() >= -npq.top()) {
      ansp.emplace(ppq.top());
      ppq.pop();
    } else {
      ansn.emplace(npq.top());
      npq.pop();
    }
  }
  if (ansn.size() % 2) {
    if (ppq.empty()) {
      ansp.pop();
      ansn.push(npq.top());
    } else if (npq.empty()) {
      ansn.pop();
      ansp.push(ppq.top());
    } else {

      if (ansp.top() * ppq.top() > ansn.top() * npq.top()) {
        ansn.pop();
        ansp.push(ppq.top());
      } else {
        ansp.pop();
        ansn.push(npq.top());
      }
    }
  }
  while (ansp.size()) {
    ans = ans * ansp.top() % MOD;
    ansp.pop();
  }

  while (ansn.size()) {
    ans = ans * (ansn.top() + MOD) % MOD;
    ansn.pop();
  }
  cout << ans % MOD << endl;
}