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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
/*

*/
ll MOD = 1e9 + 7;
ll modpow(ll a, ll n) {
  if (n == 1)
    return a % MOD;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1)) % MOD;
  else {
    ll t = modpow(a, n / 2) % MOD;
    return (t * t) % MOD;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> A(N, {0, 0});
  rep(i, N) {
    ll a;
    cin >> a;
    if (a >= 0)
      A[i] = {a, 1};
    else
      A[i] = {-a, -1};
  }
  sort(A.begin(), A.end(), [](auto &x, auto &y) { return x.first > y.first; });
  ll ans = 1;
  rep(i, K) {
    ll a1 = A[i].first;
    (ans *= a1) %= MOD;
  }
  (ans += MOD) %= MOD;
  // 上からとって負数をみる
  ll tmp = 1;
  for (ll i = 0; i < K; i++) {
    ll a2 = A[i].second;
    tmp *= a2;
  }
  if (tmp == 1 || N == K) { // 素直にとれる
    ans *= tmp;
    (ans += MOD) %= MOD;
    cout << ans << endl;
    return 0;
  }
  // 負の場合
  // 1,負をのぞいて正をとる
  ll c1 = 1e10, c2 = 1e10, d1 = 1e10, d2 = 1e10;
  for (ll i = K - 1; i >= 0; i--) {
    ll a2 = A[i].second;
    if (a2 == -1) { // 小さい負を除く
      c1 = A[i].first;
      break;
    }
  }
  for (ll i = K; i < N; i++) {
    ll a2 = A[i].second;
    if (a2 == 1) {
      c2 = A[i].first;
      break;
    }
  }
  // 2,正をのぞいて負をとる
  for (ll i = K - 1; i >= 0; i--) {
    ll a2 = A[i].second;
    if (a2 == 1) { // 小さい正を除く
      d1 = A[i].first;
      break;
    }
  }
  for (ll i = K; i < N; i++) {
    ll a2 = A[i].second;
    if (a2 == -1) {
      d2 = A[i].first;
      break;
    }
  }
  // printf("pre:%lld,c1:%lld,c2:%lld,d1:%lld,d2:%lld\\n",ans,c1,c2,d1,d2);
  if ((c1 != 1e10 && c2 != 1e10 && (d1 == 1e10 || d2 == 1e10)) ||
      ((c1 != 1e10 && c2 != 1e10 && d1 != 1e10 && d2 != 1e10) &&
       c2 * d1 > c1 * d2)) {
    (ans *= modpow(c1, MOD - 2)) %= MOD;
    (ans *= c2) %= MOD;
    // printf("root:1\\n");
  } else if (((c1 == 1e10 || c2 == 1e10) && d1 != 1e10 && d2 != 1e10) ||
             ((c1 != 1e10 && c2 != 1e10 && d1 != 1e10 && d2 != 1e10) &&
              c2 * d1 <= c1 * d2)) {
    (ans *= modpow(d1, MOD - 2)) %= MOD;
    (ans *= d2) %= MOD;
    // printf("root:2\\n");
  } else { // 全部マイナスで奇数個とか
    ans = 1;
    for (ll i = 0; i < K; i++) {
      ll a1 = A[N - 1 - i].first;
      ll a2 = A[N - 1 - i].second;
      (ans *= (a1 * a2)) %= MOD;
    }
    // printf("root:3\\n");
  }
  (ans += MOD) %= MOD;
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
/*
+は上からとる
-は偶数個とる
*/
int main() {
  ll MOD = 1e9 + 7, N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> A(N, {0, 0});
  vector<float> X(0, 0), Y(0, 0);
  rep(i, N) {
    ll a;
    cin >> a;
    if (a < 0)
      A[i] = {-a, 1};
    else
      A[i] = {a, 0};
  }
  sort(all(A), [](auto &x, auto &y) { return x.first > y.first; });
  //+
  for (ll i = 0; i < N; i++) {
    ll a1 = A[i].first;
    ll a2 = A[i].second;
    if (a2 == 1)
      Y.push_back(log(a1));
    else
      X.push_back(log(a1));
  }
  ll xsz = X.size(), ysz = Y.size();
  for (ll i = 0; i < xsz - 1; i++)
    X[i + 1] += X[i];
  for (ll i = 0; i < ysz - 1; i++)
    Y[i + 1] += Y[i];
  float ans = -1e9;
  ll j = 0;
  for (; j < K / 2; j++) { //-からとるかず
    if ((xsz + j * 2) < K)
      continue;
    float tmp = X[xsz - (K - j * 2) - 1];
    tmp += Y[j * 2];
    if (ans >= tmp)
      break;
    ans = tmp;
  }
  if (j > 0)
    j--;
  if (ans == -1e12) {
    ll res = 1, asz = A.size();
    for (ll i = 0; i < K; i++) {
      (res *= A[asz - 1 - i].first) %= MOD;
      if (A[asz - 1 - i].second == 1)
        res *= -1;
    }
    if (res < 0)
      res += MOD;
    cout << res << endl;
    return 0;
  }
  ll xi = K - j * 2, yi = j * 2;
  ll res = 1;
  for (ll i = 0; i < N; i++) {
    if (A[i].second == 0 && xi > 0) {
      (res *= A[i].first) %= MOD;
      xi--;
    } else if (A[i].second == 1 && yi > 0) {
      (res *= A[i].first) %= MOD;
      yi--;
    }
  }
  if (res < 0)
    res += MOD;
  cout << res << endl;
}