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

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
const int MOD = 1e9 + 7;
int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> A(N);
  priority_queue<long long> sei, hu, hu2;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A[i] = a;
    if (a < 0) {
      hu.push(-a);
      hu2.push(a);
    } else
      sei.push(a);
  }
  long long ans = 1;
  if (N == K) {
    for (int i = 0; i < N; i++) {
      ans *= A[i];
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  if (sei.size() == 0 && K % 2 == 1) {
    for (int i = 0; i < K; i++) {
      ans *= hu2.top();
      hu2.pop();
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  while (K > 0) {
    if (K == 1) {
      ans *= sei.top();
      K--;
      sei.pop();
    } else if (hu.size() >= 2 && sei.size() >= 2) {
      long long hut1, hut2, seit1, seit2;
      hut1 = hu.top();
      hu.pop();
      hut2 = hu.top();
      seit1 = sei.top();
      sei.pop();
      seit2 = sei.top();
      if (hut1 * hut2 < seit1 * seit2) {
        ans *= seit1;
        hu.push(hut1);
        K--;
      } else {
        ans *= hut1 * hut2 % MOD;
        sei.push(seit1);
        hu.pop();
        K -= 2;
      }
    } else if (hu.size() >= 2 && sei.size() < 2) {
      long long t1, t2;
      t1 = hu.top();
      hu.pop();
      t2 = hu.top();
      hu.pop();
      ans *= t1 * t2 % MOD;
      K -= 2;
    } else if (hu.size() < 2 && sei.size() >= 2) {
      ans *= sei.top();
      sei.pop();
      K--;
    } else {
      ans *= sei.top();
      ans %= MOD;
      sei.pop();
      K--;
    }
    ans %= MOD;
  }
  cout << (ans + MOD) % MOD << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
const int MOD = 1e9 + 7;
int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> sei, hu, zero;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    if (a > 0)
      sei.push_back(a);
    else if (a < 0)
      hu.push_back(a);
    else
      zero.push_back(a);
  }
  sort(hu.begin(), hu.end());
  sort(sei.begin(), sei.end());
  int husize = (int)hu.size();
  int seisize = (int)sei.size();
  if (seisize == 0 && zero.size() > 0) {
    cout << 0 << endl;
    return 0;
  }
  if (husize == N && K % 2 == 1) {
    long long ans = 1;
    for (int i = 0; i < K; i++) {
      ans *= hu[husize - 1 - i];
      ans %= MOD;
    }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  int huko = husize / 2 * 2;
  int seiko = K - huko;
  int ind1 = huko, ind2 = seiko;
  for (int i = huko; i > 0; i -= 2) {
    if (seisize - seiko - 2 >= 0) {
      if (hu[i - 1] * hu[i - 2] <
          sei[seisize - seiko - 1] * sei[seisize - seiko - 2]) {
        ind1 = huko - 2;
        ind2 = K - ind1;
        seiko += 2;
      }
    }
  }
  long long ans = 1;
  for (int i = max(0, ind1 - K); i < ind1; i++) {
    ans *= hu[i];
    ans %= MOD;
  }
  for (int i = seisize - (ind2); i < seisize; i++) {
    ans *= sei[i];
    ans %= MOD;
  }
  cout << (ans + MOD) % MOD << endl;
}