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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
#define ll long long
int n, k;
vector<ll> pos, neg, res;
ll ans = 1, cons = 1000000007;

signed main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll v;
    cin >> v;
    if (v >= 0) {
      pos.push_back(v);
    } else
      neg.push_back(v * (-1));
    ans = (ans * v) % cons;
  }
  if (k == n) {
    cout << (ans + cons) % cons << endl;
    return 0;
  }
  ans = 1;
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end());
  if (pos.empty()) {
    ans = 1;
    if (k % 2) {
      for (int i = 0; i < k; i++)
        ans = (ans * neg[i] + cons) % cons;
      ans *= -1;
    } else {
      for (int i = neg.size() - 1; i >= neg.size() - k; i--) {
        ans = (ans * neg[i] + cons) % cons;
      }
    }
    cout << (ans + cons) % cons;
    return 0;
  } else {
    if (k % 2 == 1) {
      res.push_back(pos.back());
      pos.pop_back();
    }
    while (res.size() < k) {
      bool ppos = 0, pneg = 0;
      ll pos1, neg1, pos2, neg2;
      if (pos.size() >= 2) {
        ppos = 1;
        pos1 = pos.back();
        pos2 = pos[pos.size() - 2];
      }
      if (neg.size() >= 2) {
        pneg = 1;
        neg1 = neg.back();
        neg2 = neg[neg.size() - 2];
      }
      if (ppos && pneg) {
        if (pos1 * pos2 > neg1 * neg2) {
          pos.pop_back();
          pos.pop_back();
          res.push_back(pos1);
          res.push_back(pos2);
        } else {
          neg.pop_back();
          neg.pop_back();
          res.push_back(neg1);
          res.push_back(neg2);
        }
      }
      if (pneg && !ppos) {
        neg.pop_back();
        neg.pop_back();
        res.push_back(neg1);
        res.push_back(neg2);
      }
      if (ppos && !pneg) {
        pos.pop_back();
        pos.pop_back();
        res.push_back(pos1);
        res.push_back(pos2);
      }
      if (!ppos && !pneg) {
        res.push_back(pos.back());
        pos.pop_back();
        res.push_back(neg.back());
        neg.pop_back();
      }
    }
  }
  for (int i = 0; i < k; i++)
    ans = (ans * res[i] + cons) % cons;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
#define ll long long
int n, k;
vector<ll> pos, neg;
ll ans = 1, cons = 1000000007;

signed main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll v;
    cin >> v;
    if (v >= 0) {
      pos.push_back(v);
    } else
      neg.push_back(v);
  }
  sort(pos.begin(), pos.end());
  reverse(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  while (k > 0) {
    if ((!pos.empty() && pos[0] == 0) &&
        (neg.size() == 0 || (k > 1 && neg.size() == 1))) {
      cout << 0 << endl;
      return 0;
    }
    if (k == 1) {
      if ((!pos.empty() && pos[0] > 0) || neg.empty()) {
        ans *= pos[0];
      } else {
        ans *= neg[0];
      }
      if (ans < 0) {
        ans = cons - abs(ans) % cons;
        break;
      }
      ans %= cons;
      break;
    }
    if (((!pos.empty() && pos[0] == 0) ||
         (pos.size() == 1 && neg.size() % 2 == 0) || pos.empty()) &&
        neg.size() >= 2) {
      if (k % 2 == 1) {
        reverse(neg.begin(), neg.end());
      }
      ans *= neg[0];
      ans *= neg[1];
      ans %= cons;
      neg.erase(neg.begin());
      neg.erase(neg.begin());
      k -= 2;
      continue;
    }
    if (!pos.empty() &&
        (pos[1] == 0 || (pos.size() == 1 && k % 2 == 1) || neg.empty())) {
      ans *= pos[0];
      ans %= cons;
      pos.erase(pos.begin());
      k--;
      continue;
    }
    reverse(neg.begin(), neg.end());
    ll n1 = neg[0], n2 = neg[1], p1 = pos[0], p2 = pos[1];
    n1 = abs(n1), n2 = abs(n2);
    if (p1 * p2 > n1 * n2) {
      ans *= pos[0] % cons;
      ans *= pos[1] % cons;
      pos.erase(pos.begin());
      pos.erase(pos.begin());
      k -= 2;
    } else {
      ans *= neg[0] % cons;
      ans *= neg[1] % cons;
      neg.erase(neg.begin());
      neg.erase(neg.begin());
      k -= 2;
    }
    reverse(neg.begin(), neg.end());
  }

  cout << ans << endl;
  return 0;
}