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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  ll ans = 1;
  if (N == K)
    rep(i, K) ans = (ans * A[i]) % mod;
  else {
    vector<ll> plus, minus;
    rep(i, N) {
      if (A[i] < 0)
        minus.push_back(A[i]);
      else
        plus.push_back(A[i]);
    }

    if (plus.size() == 0 && K % 2 == 1) {
      sort(minus.begin(), minus.end(), greater<>());
      rep(i, K) ans = (ans * minus[i]) % mod;
    } else {
      vector<pair<ll, pair<ll, ll>>> product;
      vector<ll> rest;
      sort(plus.begin(), plus.end(), greater<>());
      if (K % 2 == 1) {
        ans = (ans * plus[0]) % mod;
        plus.erase(plus.begin());
        --K;
      }
      if (!plus.empty()) {
        for (int i = 0; i < plus.size(); i += 2) {
          if (i == plus.size() - 1) {
            rest.push_back(plus[i]);
            break;
          }
          product.push_back(make_pair(plus[i] * plus[i + 1],
                                      make_pair(-plus[i], -plus[i + 1])));
        }
      }
      if (!minus.empty()) {
        sort(minus.begin(), minus.end());
        for (int i = 0; i < minus.size(); i += 2) {
          if (i == minus.size() - 1) {
            rest.push_back(minus[i]);
            break;
          }
          product.push_back(make_pair(minus[i] * minus[i + 1],
                                      make_pair(-minus[i], -minus[i + 1])));
        }
      }

      sort(product.begin(), product.end(), greater<>());
      for (int i = 0; i * 2 < K; ++i) {
        if (i * 2 == K - 1) {
          for (int j = i; j < product.size(); ++j) {
            rest.push_back(-product[j].second.first);
            rest.push_back(-product[j].second.second);
          }
          sort(rest.begin(), rest.end(), greater<>());
          ans = (ans * rest[0]) % mod;
          break;
        }
        ans = (ans * (product[i].first % mod)) % mod;
      }
    }
  }

  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll mod = 1000000007;

void ansplus(ll &ans, ll &tmpplus, ll &tmpminus, priority_queue<int> &pqplus,
             priority_queue<int, vector<int>, greater<int>> &pqminus) {
  ans = ans * (tmpplus % mod) % mod * (pqplus.top() % mod) % mod;
  pqplus.pop();
  if (!pqplus.empty()) {
    tmpplus = pqplus.top();
    pqplus.pop();
  } else
    tmpplus = -1;
}

void ansminus(ll &ans, ll &tmpplus, ll &tmpminus, priority_queue<int> &pqplus,
              priority_queue<int, vector<int>, greater<int>> &pqminus) {
  ans = ans * (tmpminus % mod) % mod * (pqminus.top() % mod) % mod;
  pqminus.pop();
  if (!pqminus.empty()) {
    tmpminus = pqminus.top();
    pqminus.pop();
  } else
    tmpminus = 1;
}

int main() {
  int N, K;
  ll A;
  cin >> N >> K;
  priority_queue<int> pqplus;
  priority_queue<int, vector<int>, greater<int>> pqminus;
  rep(i, N) {
    cin >> A;
    if (A >= 0)
      pqplus.push(A);
    else
      pqminus.push(A);
  }
  ll ans = 1;
  if (K == 1) {
    if (!pqplus.empty())
      ans *= pqplus.top();
    else {
      rep(i, N - K) pqminus.pop();
      ans *= pqminus.top();
    }
  } else if (K == N) {
    while (!pqplus.empty()) {
      ans = ans * (pqplus.top() % mod) % mod;
      pqplus.pop();
    }
    while (!pqminus.empty()) {
      ans = ans * (pqminus.top() % mod) % mod;
      pqminus.pop();
    }
  } else {
    if (pqplus.empty() == 1 || pqplus.top() == 0) {
      rep(i, N - K) pqminus.pop();
      while (!pqminus.empty()) {
        ans = ans * (pqminus.top() % mod) % mod;
        pqminus.pop();
      }
    } else {
      ll tmpplus, tmpminus;
      tmpplus = pqplus.top();
      pqplus.pop();
      if (!pqminus.empty()) {
        tmpminus = pqminus.top();
        pqminus.pop();
      }
      int i = K;
      if (K % 2 == 1) {
        ans = ans * (tmpplus % mod) % mod;
        if (!pqplus.empty()) {
          tmpplus = pqplus.top();
          pqplus.pop();
        } else
          tmpplus = -1;
        --i;
      }
      while (i > 0) {
        if (tmpplus != -1 && tmpminus != 1 && pqplus.empty() == 1 &&
            pqminus.empty() == 1) {
          ans = ans * (tmpplus % mod) % mod * (tmpminus % mod) % mod;
        } else if (pqplus.empty() != 1 && pqminus.empty() == 1) {
          ansplus(ans, tmpplus, tmpminus, pqplus, pqminus);
        } else if (pqplus.empty() == 1 && pqminus.empty() != 1) {
          ansminus(ans, tmpplus, tmpminus, pqplus, pqminus);
        } else {
          if (tmpplus * pqplus.top() > tmpminus * pqminus.top()) {
            ansplus(ans, tmpplus, tmpminus, pqplus, pqminus);
          } else {
            ansminus(ans, tmpplus, tmpminus, pqplus, pqminus);
          }
        }
        i -= 2;
      }
    }
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}