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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  const ll mod = 1e9 + 7;
  ll n, k;
  cin >> n >> k;
  ll ek = (k / 2) * 2;
  VL a(n), neg, pos;
  ll zero = 0;

  map<ll, ll> mpp, mpn;

  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] == 0)
      zero++;
    else if (a[i] > 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }

  ll pn = pos.size(), nn = neg.size();
  if (pn != 0)
    sort(ALL(pos), greater<ll>());
  if (nn != 0)
    sort(ALL(neg));

  priority_queue<ll> que;

  if (k == n) {
    ll ans = 1;
    rep(i, 0, k) {
      ans *= a[i];
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }

  if (n == nn && k % 2 == 1) {
    ll ans = 1;
    sort(ALL(a), greater<ll>());
    rep(i, 0, k) {
      // cerr << i << ": " << a[i] << endl;
      ans *= a[i];
      ans %= mod;
    }
    cerr << "flr: " << ans << endl;
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }

  for (ll i = 0; 2 * i + 1 < pn; i++) {
    if (2 * (i + 1) + 1 >= pn) {
      if (pn % 2 != 0 || k % 2 != 1) {
        que.push((pos[2 * i] * pos[2 * i + 1]));
        mpp[(pos[2 * i] * pos[2 * i + 1])]++;
      }
    } else {
      mpp[(pos[2 * i] * pos[2 * i + 1])]++;
      que.push((pos[2 * i] * pos[2 * i + 1]));
    }
  }
  for (ll i = 0; 2 * i + 1 < nn; i++) {
    que.push((neg[2 * i] * neg[2 * i + 1]));
    mpn[(neg[2 * i] * neg[2 * i + 1])]++;
  }

  ll ans = 1;
  ll cnt = 0;
  ll pused = 0;
  while (cnt < ek && !que.empty()) {
    auto q = que.top();
    if (mpn[q] > 0) {
      mpn[q]--;
    } else {
      mpp[q]--;
      pused += 2;
    }
    q %= mod;
    ans *= q;
    ans %= mod;
    // cerr << q << endl;
    que.pop();
    cnt += 2;
  }

  ll rem = k - cnt;
  if (rem == 0) {
    cerr << "pek: " << cnt << endl;
    cout << ans << endl;
    return 0;
  }

  if (pused != pn) {
    ans *= pos[pused];
    ans %= mod;
    rem--;
  }

  if (rem == 0) {
    cerr << "mar: " << cnt << endl;
    cout << ans << endl;
    return 0;
  }

  if (zero > 0) {
    cout << 0 << endl;
    return 0;
  }

  if (nn > 0 && nn % 2 == 1) {
    cerr << "rus" << endl;
    ans *= neg[nn - 1];
    ans %= mod;
  }

  cerr << "noe" << endl;

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  const ll mod = 1e9 + 7;
  ll n, k;
  cin >> n >> k;
  ll ek = (k / 2) * 2;
  VL a(n), neg, pos;
  ll zero = 0;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] == 0)
      zero++;
    else if (a[i] > 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(ALL(pos), greater<ll>());
  sort(ALL(neg));
  priority_queue<ll> que;
  ll pn = pos.size(), nn = neg.size();

  if (k == n) {
    ll ans = 1;
    rep(i, 0, k) {
      ans *= a[i];
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }

  if (n == nn && k % 2 == 1) {
    ll ans = 1;
    sort(ALL(a), greater<ll>());
    rep(i, 0, k) {
      // cerr << i << ": " << a[i] << endl;
      ans *= a[i];
      ans %= mod;
    }
    cerr << "flr: " << ans << endl;
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }

  for (ll i = 0; 2 * i + 1 < pn; i++) {
    que.push((pos[2 * i] * pos[2 * i + 1]) % mod);
  }
  for (ll i = 0; 2 * i + 1 < nn; i++) {
    que.push((neg[2 * i] * neg[2 * i + 1]) % mod);
  }
  ll ans = 1;
  ll cnt = 0;
  while (cnt < ek && !que.empty()) {
    auto q = que.top();
    ans *= q;
    ans %= mod;
    // cerr << q << endl;
    que.pop();
    cnt += 2;
  }
  ll rem = k - cnt;
  if (rem == 0) {
    // cerr << "pek: " << cnt << endl;
    cout << ans << endl;
    return 0;
  }
  if (pn % 2 != 0) {
    ans *= pos[pn - 1];
    ans %= mod;
    rem--;
  }
  if (rem == 0) {
    cout << ans << endl;
    return 0;
  }
  if (zero > 0) {
    cout << 0 << endl;
    return 0;
  }
  ans *= neg[nn - 1];
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
  return 0;
}