Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
  const int MOD = 1000000007;

  int N, K;
  cin >> N >> K;

  vector<long long> p(N);
  vector<long long> n(N);
  int Po = 0;
  int Ne = 0;
  bool has_zero = false;
  rep(i, N)
  {
    int tmp;
    cin >> tmp;
    if (tmp < 0)
    {
      n[Ne] = tmp;
      Ne++;
    }
    else if (tmp > 0)
    {
      p[Po] = tmp;
      Po++;
    }
    else if (tmp == 0)
    {
      has_zero = true;
    }
  }
  p.resize(Po);
  n.resize(Ne);

  bool minus = false;
  int k = K - Po;
  if (k > Ne)
  {
    cout << 0 << endl;
    return 0;
  }
  else
  {
    if (k % 2 != 0)
    {
      if (Po > 0 && k + 1 <= Ne)
      {
        // ans is positive value
      }
      else
      {
        if (has_zero)
        {
          cout << 0 << endl;
          return 0;
        }
        else
        {
          minus = true;
        }
      }
    }
  }

  long long num = 1;
  if (K == N)
  {
    for (const auto a : n)
    {
      num = num * a % MOD;
      if (num < 0)
      {
        num += MOD;
      }
    }

    for (const auto a : p)
    {
      num = num * a % MOD;
    }
  }
  else if (!minus)
  {
    std::sort(n.begin(), n.end());
    std::sort(p.begin(), p.end(), std::greater<int>());
    
    if (K % 2 != 0) {
      num *= p[0];
      p.erase(0);
      Po--;
      K--;
    }

    int i_n = 0;
    int i_p = 0;
    while (K > 1)
    {
      long long positive_n = -1;
      if (Po - i_p >= 2)
      {
        positive_n = p[i_p] * p[i_p + 1];
      }
      long long negative_n = -1;
      if (Ne - i_n >= 2)
      {
        negative_n = n[i_n] * n[i_n + 1];
      }

      if (positive_n > negative_n)
      {
        num = positive_n % MOD * num % MOD;
        i_p++;
        i_p++;
      }
      else
      {
        num = negative_n % MOD * num % MOD;
        i_n++;
        i_n++;
      }
      K--;
      K--;
    }

    if (K == 1)
    {
      num = num * p[i_p] % MOD;
    }
  }
  else
  {
    std::sort(n.begin(), n.end(), std::greater<int>());
    std::sort(p.begin(), p.end());

    int i_n = 0;
    int i_p = 0;
    while (K > 1)
    {
      long long positive_n = -1;
      if (Po - i_p >= 2)
      {
        positive_n = p[i_p] * p[i_p + 1];
      }
      long long negative_n = -1;
      if (Ne - i_n >= 2)
      {
        negative_n = n[i_n] * n[i_n + 1];
      }

      if (negative_n == -1 || (positive_n != -1 && (positive_n < negative_n)))
      {
        num = positive_n % MOD * num % MOD;
        i_p++;
        i_p++;
      }
      else
      {
        num = negative_n % MOD * num % MOD;
        i_n++;
        i_n++;
      }
      K--;
      K--;
    }

    if (K == 1)
    {
      num = num * n[i_n] % MOD;
      if (num < 0)
        num += MOD;
    }
  }

  cout << num << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:102:14: error: no matching function for call to 'std::vector<long long int>::erase(int)'
  102 |       p.erase(0);
      |       ~~~~~~~^~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_vector.h:1536:7: note: candidate: 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(const_iterator) [with _Tp = long long int; _Alloc = std::allocator<long long int>; iterator = std::vector<long long int>::iterator; const_iterator = std::vector<long long int>::const_iterator]'
 1536 |       erase(const_iterator __position)
      |       ^~~~~
/usr/include/c++/14/bits/stl_vector.h:1536:28: note:   no known conversion for argument 1 from 'int' to 'std::vector<long long int>::const_iterator'
 1536 |       erase(const_iterator __position)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1564:7: note: candidate: 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(const_iterator, const_iterator) [with _Tp = long long int; _Alloc = std::allocator<long long int>; iterator = std::vector<long long int>::iterator; const_iterator = std::vector<long long int>::const_iterator]'
 1564 |       erase(const_iterator __first, const_iterator __last)
      |       ^~~~~
/usr/include/c++/14/bits/stl_vector.h:1564:7: note:   candidate expects 2 arguments, 1 provided