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

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
const ll MOD = 1e9 + 7;
int main() {
  ll n, k;
  std::cin >> n >> k;
  vector<ll> a(n);
  ll cntz = 0;
  ll cntn = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    if (a[i] == 0)
      cntz++;
    if (a[i] < 0)
      cntn++;
  }
  sort(a.begin(), a.end());
  if (k == 1) {
    std::cout << (a[n - 1] + MOD) % MOD << std::endl;
    return 0;
  }

  // 負で終わるケースを除く
  if (a[n - 1] < 0) {
    if (k % 2 == 1) {
      ll ans = 1;
      for (int i = 0; i < k; i++) {
        ans *= a[n - 1 - i] + MOD;
        ans %= MOD;
      }
      std::cout << ans << std::endl;
      return 0;
    }
  }
  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ans *= a[i] + MOD;
      ans %= MOD;
    }
    std::cout << ans << std::endl;
    return 0;
  }

  if (n - cntz < k) {
    std::cout << 0 << std::endl;
    return 0;
  }

  if (cntz + cntn == n && k % 2 == 1) {
    std::cout << 0 << std::endl;
    return 0;
  }

  ll nowk = k;
  ll cur = n - 1;
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  while (nowk) {
    if (a[cur] > 0) {
      pq.push(a[cur]);
      cur--;
      nowk--;
    } else {
      break;
    }
  }
  if (nowk % 2 == 1) {
    pq.pop();
    nowk++;
  }
  // nowkは2の倍数
  ll add = 1;
  for (int i = 0; i < cntn / 2; i++) {
    // 入れるしかなかったら入れる
    if (nowk >= 2) {
      add *= a[i * 2] + MOD;
      add %= MOD;
      add *= a[i * 2 + 1] + MOD;
      add %= MOD;
      nowk -= 2;
    } else {
      // 先客がいる
      if (pq.size() >= 2) {
        auto n1 = pq.top();
        pq.pop();
        auto n2 = pq.top();
        pq.pop();
        if (n1 * n2 < (-1 * a[i * 2]) * (-1 * a[i * 2 + 1])) {
          add *= a[i * 2] + MOD;
          add %= MOD;
          add *= a[i * 2 + 1] + MOD;
          add %= MOD;
        } else {
          pq.push(n1);
          pq.push(n2);
          break;
        }
      } else {
        break;
      }
    }
  }

  while (pq.size()) {
    auto tmp = pq.top();
    pq.pop();
    add *= tmp + MOD;
    add %= MOD;
  }

  std::cout << add << std::endl;
}

Error: 0

Fixed submission:

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
const ll MOD = 1e9 + 7;
int main() {
  ll n, k;
  std::cin >> n >> k;
  vector<ll> a(n);
  ll cntz = 0;
  ll cntn = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    if (a[i] == 0)
      cntz++;
    if (a[i] < 0)
      cntn++;
  }
  sort(a.begin(), a.end());
  if (k == 1) {
    std::cout << a[n - 1] << std::endl;
    return 0;
  }
  // 負で終わるケースを除く
  if (a[n - 1] < 0) {
    if (k % 2 == 1) {
      ll ans = 1;
      for (int i = 0; i < k; i++) {
        ans *= a[n - 1 - i] + MOD;
        ans %= MOD;
      }
      std::cout << ans << std::endl;
      return 0;
    }
  }
  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ans *= a[i] + MOD;
      ans %= MOD;
    }
    std::cout << ans << std::endl;
    return 0;
  }

  if (n - cntz < k) {
    std::cout << 0 << std::endl;
    return 0;
  }

  ll nowk = k;
  ll cur = n - 1;
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  while (nowk) {
    if (a[cur] > 0) {
      pq.push(a[cur]);
      cur--;
      nowk--;
    } else {
      break;
    }
  }
  if (nowk % 2 == 1) {
    pq.pop();
    nowk++;
  }
  // nowkは2の倍数
  ll add = 1;
  for (int i = 0; i < cntn / 2; i++) {
    // 入れるしかなかったら入れる
    if (nowk >= 2) {
      add *= a[i * 2] + MOD;
      add %= MOD;
      add *= a[i * 2 + 1] + MOD;
      add %= MOD;
      nowk -= 2;
    } else {
      // 先客がいる
      if (pq.size() >= 2) {
        auto n1 = pq.top();
        pq.pop();
        auto n2 = pq.top();
        pq.pop();
        if (n1 * n2 < a[i * 2] * a[i * 2 + 1]) {
          add *= a[i * 2] + MOD;
          add %= MOD;
          add *= a[i * 2 + 1] + MOD;
          add %= MOD;
        } else {
          pq.push(n1);
          pq.push(n2);
          break;
        }
      } else {
        break;
      }
    }
  }
  while (pq.size()) {
    auto tmp = pq.top();
    pq.pop();
    add *= tmp;
    add %= MOD;
  }

  std::cout << add << std::endl;
}