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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n, k;
ll a[200200], ans = 1, mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + 1 + n);

  int l = 1, r = n;
  if (k % 2)
    ans = a[r], r--, k--;
  while (k) {
    k -= 2;
    ll x = a[l] * a[l + 1] * (ans < 0 ? -1 : 1),
       y = a[r] * a[r - 1] * (ans < 0 ? -1 : 1);
    if (x > y)
      ans = ans * a[l] % mod * a[l + 1] % mod, l += 2;
    else
      ans = ans * a[r] % mod * a[r - 1] % mod, r -= 2;
  }
  cout << (ans + mod) % mod;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, k;
ll ans = 1, a[200200], mod = 1e9 + 7;
vector<ll> v[3];
queue<ll> q[2];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      v[0].push_back(a[i]);
    if (a[i] > 0)
      v[1].push_back(a[i]);
    if (a[i] == 0)
      v[2].push_back(a[i]);
  }

  if (v[2].size() && v[0].size() - v[0].size() % 2 + v[1].size() < k) {
    cout << "0";
    return 0;
  }

  sort(v[0].begin(), v[0].end());
  sort(v[1].rbegin(), v[1].rend());

  for (ll x : v[0])
    q[0].push(x);
  for (ll x : v[1])
    q[1].push(x);

  if (k % 2) {
    if (q[1].empty()) {
      if (!v[2].empty()) {
        cout << "0";
        return 0;
      }
      for (int i = 1; i <= k; i++) {
        ans = (ans * v[0][v[0].size() - i] + mod) % mod;
      }
      cout << ans;
      return 0;
    }

    ans = (ans * q[1].front()) % mod;
    q[1].pop();
    k--;
  }

  if (q[0].size() < 2) {
    while (!q[1].empty() && k--) {
      ans = ans * q[1].front() % mod;
      q[1].pop();
    }
    stack<int> s;
    while (!q[0].empty())
      s.push(q[0].front()), q[0].pop();
    while (k--)
      ans = (ans * s.top() + mod) % mod, s.pop();
    cout << ans;
    return 0;
  }
  if (q[1].size() < 2) {
    while (k--) {
      ans = ans * q[0].front() % mod;
      q[0].pop();
    }
    cout << ans;
    return 0;
  }

  ll A = q[0].front(), B = q[1].front();
  q[0].pop(), q[1].pop();
  A *= q[0].front(), B *= q[1].front();
  q[0].pop(), q[1].pop();
  while (k > 0) {
    k -= 2;
    if (A >= B) {
      ans = ans * A % mod;
      if (q[0].size() < 2)
        break;
      A = q[0].front();
      q[0].pop();
      if (q[0].empty())
        break;
      A *= q[0].front();
      q[0].pop();
    } else {
      ans = ans * B % mod;
      if (q[1].size() < 2)
        break;
      B = q[1].front();
      q[1].pop();
      B *= q[1].front();
      q[1].pop();
    }
  }

  if (q[0].size() < 2 && k) {
    k -= 2;
    ans *= B;
    while (!q[1].empty() && k--) {
      ans = ans * q[1].front() % mod;
      q[1].pop();
    }
    stack<int> s;
    while (!q[0].empty())
      s.push(q[0].front()), q[0].pop();
    while (k--)
      ans = (ans * s.top() + mod) % mod, s.pop();
    cout << ans;
    return 0;
  }
  if (q[1].size() < 2 && k) {
    k -= 2;
    ans *= A;
    while (k--) {
      ans = ans * q[0].front() % mod;
      q[0].pop();
    }
    cout << ans;
    return 0;
  }

  cout << ans;
}