Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define double long double
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("avx,avx2,sse")
using namespace std;

bool comp(int a, int b) { return abs(a) >= abs(b); }

const int MOD = 1e9 + 7;
const double EPS = 1e-9;

bool can(int a, int b, int k) {
  if (b % 2) {
    b--;
  }
  if (k % 2) {
    return a + b >= k && a > 0;
  } else {
    return a + b >= k;
  }
}

int n, k;
vector<int> arr;
vector<double> pa;
vector<double> pb;
vector<int> ra;
vector<int> rb;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;
  arr.resize(n);
  int a = 0;
  int b = 0;
  int z = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > 0) {
      a++;
    } else if (arr[i] < 0) {
      b++;
    } else {
      z++;
    }
  }
  sort(arr.begin(), arr.end(), comp);
  if (can(a, b, k)) {
    pa.push_back(0);
    pb.push_back(0);
    ra.push_back(1);
    rb.push_back(1);
    for (int x : arr) {
      if (x > 0) {
        pa.push_back(pa.back() + log(x));
        ra.push_back((ra.back() * x) % MOD);
      } else if (x < 0) {
        pb.push_back(pb.back() + log(-x));
        rb.push_back((rb.back() * -x) % MOD);
      }
    }
    double fin = -1;
    int ans = INT_MIN;
    for (int i = 0; i <= k; i += 2) {
      if (i < pb.size() && k - i < pa.size()) {
        if (pb[i] + pa[k - i] > fin) {
          fin = pb[i] + pa[k - i];
          ans = (rb[i] * ra[k - i]) % MOD;
        }
      }
    }
    cout << ans << '\n';
  } else if (z > 0) {
    cout << 0 << '\n';
  } else {
    int res = 1;
    for (int i = n - 1; i >= n - k; i--) {
      res = (((res * arr[i]) % MOD) + MOD) % MOD;
    }
    cout << res << '\n';
  }

  fflush(stdout);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define double long double
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("avx,avx2,sse")
using namespace std;

bool comp(int a, int b) { return abs(a) > abs(b); }

const int MOD = 1e9 + 7;
const double EPS = 1e-9;

bool can(int a, int b, int k) {
  if (b % 2) {
    b--;
  }
  if (k % 2) {
    return a + b >= k && a > 0;
  } else {
    return a + b >= k;
  }
}

int n, k;
vector<int> arr;
vector<double> pa;
vector<double> pb;
vector<int> ra;
vector<int> rb;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;
  arr.resize(n);
  int a = 0;
  int b = 0;
  int z = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > 0) {
      a++;
    } else if (arr[i] < 0) {
      b++;
    } else {
      z++;
    }
  }
  sort(arr.begin(), arr.end(), comp);
  if (can(a, b, k)) {
    pa.push_back(0);
    pb.push_back(0);
    ra.push_back(1);
    rb.push_back(1);
    for (int x : arr) {
      if (x > 0) {
        pa.push_back(pa.back() + log(x));
        ra.push_back((ra.back() * x) % MOD);
      } else if (x < 0) {
        pb.push_back(pb.back() + log(-x));
        rb.push_back((rb.back() * -x) % MOD);
      }
    }
    double fin = -1;
    int ans = INT_MIN;
    for (int i = 0; i <= k; i += 2) {
      if (i < pb.size() && k - i < pa.size()) {
        if (pb[i] + pa[k - i] > fin) {
          fin = pb[i] + pa[k - i];
          ans = (rb[i] * ra[k - i]) % MOD;
        }
      }
    }
    cout << ans << '\n';
  } else if (z > 0) {
    cout << 0 << '\n';
  } else {
    int res = 1;
    for (int i = n - 1; i >= n - k; i--) {
      res = (((res * arr[i]) % MOD) + MOD) % MOD;
    }
    cout << res << '\n';
  }

  fflush(stdout);
  return 0;
}