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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int mod = 1e9 + 7;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  ll n, k;
  cin >> n >> k;
  ll l = 0, r = n - 1;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  ll ret = 1, sgn = 1;
  if (k % 2)
    ret = a[r], r--, k--, sgn = (ret < 0 ? -1 : 1);
  while (k) {
    ll x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * sgn > y * sgn)
      ret *= x % mod, ret %= mod, l += 2;
    else
      ret *= y % mod, ret %= mod, r -= 2;
    k -= 2;
  }
  cout << (ret % mod + mod) % mod << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int mod = 1e9 + 7;

ll eval2(int K, vector<ll> pos, vector<ll> neg, int zero) {
  sort(neg.rbegin(), neg.rend());
  sort(pos.begin(), pos.end());
  ll ret = mod + neg.front();
  int i = 0, j = 1;
  int P = pos.size(), N = neg.size();
  K--;
  for (int k = 0; k < K - (K % 2); k += 2) {
    ll a = LLONG_MAX, b = LLONG_MAX;
    if (i + 1 < P)
      a = pos[i] * pos[i + 1];
    if (j + 1 < N)
      b = neg[j] * neg[j + 1];
    if (a == LLONG_MAX && b == LLONG_MAX)
      return 0;
    if (a <= b)
      ret = (ret * (a % mod)) % mod, i += 2;
    else
      ret = (ret * (b % mod)) % mod, j += 2;
  }
  if (K % 2) {
    if (i < P)
      ret = (ret * pos[i]) % mod;
    else if (zero)
      return 0;
    else if (j < N)
      ret = (ret * neg[j]) % mod;
  }
  return ret;
}

ll eval(int K, vector<ll> pos, vector<ll> neg, int zero) {
  sort(neg.begin(), neg.end());
  sort(pos.rbegin(), pos.rend());
  ll ret = 1;
  int i = 0, j = 0;
  int P = pos.size(), N = neg.size();
  for (int k = 0; k < K - (K % 2); k += 2) {
    ll a = -1, b = -1;
    if (i + 1 < P)
      a = pos[i] * pos[i + 1];
    if (j + 1 < N)
      b = neg[j] * neg[j + 1];
    if (a == -1 && b == -1)
      return eval2(K, pos, neg, zero);
    if (a >= b)
      ret = (ret * (a % mod)) % mod, i += 2;
    else
      ret = (ret * (b % mod)) % mod, j += 2;
  }
  if (K % 2) {
    if (i < P)
      ret = (ret * pos[i]) % mod;
    else if (zero)
      return 0;
    else
      return eval2(K, pos, neg, zero);
  }
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  vector<ll> neg, pos;
  int zero = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] < 0)
      neg.push_back(A[i]);
    else if (A[i] > 0)
      pos.push_back(A[i]);
    else
      zero++;
  }
  if (K == N) {
    ll ret = 1;
    for (auto x : A)
      ret = (ret * ((mod + x) % mod)) % mod;
    cout << ret << "\\n";
    return 0;
  }
  cout << eval(K, pos, neg, zero) << "\\n";
  return 0;
}