Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define deb(x) cerr << "\n" << #x << "=" << x << "\n";
#define deb2(x, y)                                                             \
  cerr << "\n" << #x << "=" << x << "\n" << #y << "=" << y << "\n";
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> pos, neg;
  int ans = 1, z = 0;
  for (int i = 0; i < n; i++) {
    int foo;
    cin >> foo;
    if (foo <= 0)
      neg.push_back(foo);
    else
      pos.push_back(foo);
  }
  sort(pos.begin(), pos.end());   // 1 2 3 4 5
  sort(neg.rbegin(), neg.rend()); // -1 -2 -3 -4 -5
  if (k % 2) {
    if (!pos.empty()) {
      if (pos.size() - 1 + (neg.size() / 2) * 2 >= k - 1)
        ans = pos.back(), pos.pop_back();
      else
        ans = pos[0], pos.erase(pos.begin());
    } else
      ans = *neg.begin(), neg.erase(neg.begin());
  }
  bool ok = 1;
  if (ans < 0)
    ok = 0;
  ans = (ans + mod) % mod;
  k -= k % 2;

  if (pos.size() + (neg.size() / 2) * 2 < k)
    ok = 0;

  for (int i = 1; i <= k; i += 2) {
    int maxm = 0;
    if (neg.size() >= 2) {
      if (ok)
        maxm = neg.back() * neg[neg.size() - 2];
      else
        maxm = neg[0] * neg[1];
    }
    if (pos.size() >= 2) {
      if ((ok && maxm < pos.back() * pos[pos.size() - 2]) ||
          (!ok && maxm > pos[0] * pos[1])) {
        if (ok) {
          maxm = pos.back();
          pos.pop_back();
          maxm = maxm * pos.back();
          pos.pop_back();
        } else {
          maxm = pos[0] * pos[1];
          pos.erase(pos.begin());
          pos.erase(pos.begin());
        }
      } else {
        if (ok) {
          neg.pop_back();
          neg.pop_back();
        } else {
          neg.erase(neg.begin());
          neg.erase(neg.begin());
        }
      }
    } else {
      if (neg.size() >= 2) {
        if (ok) {
          neg.pop_back();
          neg.pop_back();
        } else {
          neg.erase(neg.begin());
          neg.erase(neg.begin());
        }
      } else {
        assert(!neg.empty() && !pos.empty());
        maxm = (*neg.begin()) * pos.back();
        pos.pop_back();
        neg.erase(neg.begin());
      }
    }
    maxm = (maxm + mod * (int)1e9) % mod;
    ans = ((ans % mod * maxm % mod) % mod + mod) % mod;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define deb(x) cerr << "\n" << #x << "=" << x << "\n";
#define deb2(x, y)                                                             \
  cerr << "\n" << #x << "=" << x << "\n" << #y << "=" << y << "\n";
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> pos, neg;
  int ans = 1, z = 0;
  for (int i = 0; i < n; i++) {
    int foo;
    cin >> foo;
    if (foo <= 0)
      neg.push_back(foo);
    else
      pos.push_back(foo);
  }
  sort(pos.begin(), pos.end());   // 1 2 3 4 5
  sort(neg.rbegin(), neg.rend()); // -1 -2 -3 -4 -5
  if (k % 2) {
    if (!pos.empty()) {
      if (pos.size() - 1 + (neg.size() / 2) * 2 >= k - 1)
        ans = pos.back(), pos.pop_back();
      else
        ans = pos[0], pos.erase(pos.begin());
    } else
      ans = *neg.begin(), neg.erase(neg.begin());
  }
  bool ok = 1;
  if (ans < 0)
    ok = 0;
  ans = (ans + mod) % mod;
  k -= k % 2;

  if (pos.size() + (neg.size() / 2) * 2 < k)
    ok = 0;

  for (int i = 1; i <= k; i += 2) {
    int maxm = 0;
    if (neg.size() >= 2) {
      if (ok)
        maxm = neg.back() * neg[neg.size() - 2];
      else
        maxm = neg[0] * neg[1];
    }
    if (pos.size() >= 2) {
      if ((ok && maxm < pos.back() * pos[pos.size() - 2]) ||
          (!ok && maxm > pos[0] * pos[1]) || neg.size() < 2) {
        if (ok) {
          maxm = pos.back();
          pos.pop_back();
          maxm = maxm * pos.back();
          pos.pop_back();
        } else {
          maxm = pos[0] * pos[1];
          pos.erase(pos.begin());
          pos.erase(pos.begin());
        }
      } else {
        if (ok) {
          neg.pop_back();
          neg.pop_back();
        } else {
          neg.erase(neg.begin());
          neg.erase(neg.begin());
        }
      }
    } else {
      if (neg.size() >= 2) {
        if (ok) {
          neg.pop_back();
          neg.pop_back();
        } else {
          neg.erase(neg.begin());
          neg.erase(neg.begin());
        }
      } else {
        assert(!neg.empty() && !pos.empty());
        maxm = (*neg.begin()) * pos.back();
        pos.pop_back();
        neg.erase(neg.begin());
      }
    }
    maxm = (maxm + mod * (int)1e9) % mod;
    ans = ((ans % mod * maxm % mod) % mod + mod) % mod;
  }
  cout << ans;
  return 0;
}