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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
const ll inf = 1e9 + 7;
ll a[N], sp[N], n, k, sl = 0;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      sl++;
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    sp[i] = (a[i] < 0) ? sp[i - 1] + 1 : sp[i - 1];
  // for (int i = 1; i <= n; i++) cout << sp[i] << " " ; cout << "\\n";
  int getmax = 0, q = ((sp[n] - sp[n - k]) % 2 == 0) ? 1 : -1;
  long double fr = 1, nr = 1;
  for (int i = 1, j = n - k + 2; i <= k; i++, j++) {
    int temp = ((sp[i] + sp[n] - sp[j - 1]) % 2 == 0) ? 1 : -1;
    ll c = (ll)2e18 / fr, d = (ll)2e18 / nr;
    if (c >= abs(a[i]) && d >= abs(a[j - 1]))
      fr = fr * abs(a[i]), nr = nr * abs(a[j - 1]);
    else
      fr = fr / abs(a[j - 1]), nr = nr / abs(a[i]);
    // cout << fr << " " << nr << "\\n";
    if (temp > q) {
      getmax = i;
      q = temp;
    } else if (temp == q && temp == 1) {
      if (fr > nr)
        getmax = i;
      fr = 1;
      nr = 1;
    } else if (temp == q && temp == -1) {
      if (fr < nr)
        getmax = i;
      fr = 1;
      nr = 1;
    }
    // cout << i << " " << j << "\\n";
  }
  long long ans = 1;
  for (int i = 1; i <= getmax; i++)
    ans = (ans * abs(a[i])) % inf;
  for (int i = n - k + getmax + 1; i <= n; i++)
    ans = (ans * abs(a[i])) % inf;
  if ((sp[getmax] + sp[n] - sp[n - k + getmax]) % 2 == 0)
    cout << ans;
  else if (sl > 0)
    cout << 0;
  else
    cout << (-ans + inf + inf) % inf;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
const int inf = 1e9 + 7;
ll a[N], sp[N], n, k, sl = 0;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      sl++;
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    sp[i] = (a[i] < 0) ? sp[i - 1] + 1 : sp[i - 1];
  // for (int i = 1; i <= n; i++) cout << sp[i] << " " ; cout << "\\n";
  int getmax = 0, q = ((sp[n] - sp[n - k]) % 2 == 0) ? 1 : -1;
  long double fr = 1, nr = 1;
  for (int i = 1, j = n - k + 2; i <= k; i++, j++) {
    int temp = ((sp[i] + sp[n] - sp[j - 1]) % 2 == 0) ? 1 : -1;
    ll c = (ll)1e18 / (ll)fr, d = (ll)1e18 / (ll)nr;
    if (c >= abs(a[i]) && d >= abs(a[j - 1]))
      fr = fr * abs(a[i]), nr = nr * abs(a[j - 1]);
    else
      fr = fr / a[j - 1], nr = nr / a[i];
    // cout << fr << " " << nr << "\\n";
    if (temp > q) {
      getmax = i;
      q = temp;
    } else if (temp == q && temp == 1) {
      if (fr > nr)
        getmax = i;
      fr = 1;
      nr = 1;
    } else if (temp == q && temp == -1) {
      if (fr < nr)
        getmax = i;
      fr = 1;
      nr = 1;
    }
    // cout << i << " " << j << "\\n";
  }
  long long ans = 1;
  for (int i = 1; i <= getmax; i++)
    ans = (ans * abs(a[i])) % inf;
  for (int i = n - k + getmax + 1; i <= n; i++)
    ans = (ans * abs(a[i])) % inf;
  if ((sp[getmax] + sp[n] - sp[n - k + getmax]) % 2 == 0)
    cout << ans;
  else if (sl > 0)
    cout << 0;
  else
    cout << (-ans + inf) % inf;
  return 0;
}