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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

const uint64_t mod = 1'000'000'007;

int main() {
  uint32_t N = 0, K = 0;
  std::cin >> N >> K;
  std::vector<uint64_t> plus, minus;
  for (size_t i = 0; i < N; i++) {
    int64_t a = 0;
    std::cin >> a;
    if (a >= 0)
      plus.push_back(a);
    else
      minus.push_back(-a);
  }

  if (N == K) {
    uint64_t result = 1;
    for (size_t i = 0; i < plus.size(); i++)
      result = (result * plus[i]) % mod;
    for (size_t i = 0; i < minus.size(); i++)
      result = (result * minus[i]) % mod;

    std::cout << ((result && minus.size() % 2) ? mod - result : result)
              << std::endl;
    return 0;
  }

  if (plus.empty() && K % 2) {
    std::sort(minus.begin(), minus.end());

    uint64_t result = 1;
    for (size_t i = 0; i < K; i++)
      result = (result * minus[i]) % mod;
    std::cout << mod - result << std::endl;
    return 0;
  }

  std::sort(plus.rbegin(), plus.rend());
  std::sort(minus.rbegin(), minus.rend());

  uint32_t p = 0, m = 0;
  for (size_t i = 0; i < K; i++) {
    if (plus.empty() || p == plus.size())
      m++;
    else if (minus.empty() || m == minus.size())
      p++;
    else if (plus[p] >= minus[m])
      p++;
    else
      m++;
  }
  if (m % 2) {
    if (p == plus.size())
      p--, m++;
    else if (m == minus.size() || p == 0)
      p++, m--;
    else if (plus[p] * plus[p - 1] >= minus[m] * minus[m - 1])
      p++, m--;
    else
      p--, m++;
  }

  uint64_t result = 1;
  for (size_t i = 0; i < p; i++)
    result = (result * plus[i]) % mod;
  for (size_t i = 0; i < m; i++)
    result = (result * minus[i]) % mod;

  std::cout << result << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

const uint64_t mod = 1'000'000'007;

int main() {
  uint32_t N = 0, K = 0;
  std::cin >> N >> K;
  std::vector<uint64_t> plus, minus;
  for (size_t i = 0; i < N; i++) {
    int64_t a = 0;
    std::cin >> a;
    if (a >= 0)
      plus.push_back(a);
    else
      minus.push_back(-a);
  }

  if (N == K) {
    uint64_t result = 1;
    for (size_t i = 0; i < plus.size(); i++)
      result = (result * plus[i]) % mod;
    for (size_t i = 0; i < minus.size(); i++)
      result = (result * minus[i]) % mod;

    std::cout << ((result && minus.size() % 2) ? mod - result : result)
              << std::endl;
    return 0;
  }

  if (plus.empty() && K % 2) {
    std::sort(minus.begin(), minus.end());

    uint64_t result = 1;
    for (size_t i = 0; i < K; i++)
      result = (result * minus[i]) % mod;
    std::cout << mod - result << std::endl;
    return 0;
  }

  std::sort(plus.rbegin(), plus.rend());
  std::sort(minus.rbegin(), minus.rend());

  uint32_t p = 0, m = 0;
  for (size_t i = 0; i < K; i++) {
    if (!plus.empty() || p == plus.size())
      m++;
    else if (!minus.empty() || m == minus.size())
      p++;
    else if (plus[p] >= minus[m])
      p++;
    else
      m++;
  }
  if (m % 2) {
    if (p == plus.size())
      p--, m++;
    else if (m == minus.size() || p == 0)
      p++, m--;
    else if (plus[p] * plus[p - 1] >= minus[m] * minus[m - 1])
      p++, m--;
    else
      p--, m++;
  }

  uint64_t result = 1;
  for (size_t i = 0; i < p; i++)
    result = (result * plus[i]) % mod;
  for (size_t i = 0; i < m; i++)
    result = (result * minus[i]) % mod;

  std::cout << result << std::endl;

  return 0;
}