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

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

void solve(std::istream &in, std::ostream &out);

int main() {
#ifdef LOCAL
  freopen("../IO/e.in", "r", stdin);
//  freopen("../IO/f.out", "w", stdout);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
#endif
  solve(std::cin, std::cout);
  return 0;
}

constexpr int P = 1'000'000'007;

void solve(std::istream &in, std::ostream &out) {
  int n, k;
  in >> n >> k;
  vector<int> pos, neg;
  bool zero = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    in >> x;
    zero |= x == 0;
    if (x > 0)
      pos.push_back(x);
    if (x < 0)
      neg.push_back(-x);
  }
  int ps = pos.size();
  int ns = neg.size();
  if (ps + ns < k) {
    out << 0 << '\\n';
    return;
  } else if (ps + ns == k) {
    int ans = 1;
    for (int i = 0; i < ps; ++i) {
      ans = (int64_t)ans * pos[i] % P;
    }
    for (int i = 0; i < ns; ++i) {
      ans = (int64_t)ans * (P - neg[i]) % P;
    }
    out << ans << '\\n';
    return;
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());
  vector<long double> lpos(1, 0), lneg(1, 0);
  for (int i = 0; i < ps; ++i) {
    lpos.push_back(lpos.back() + log((long double)pos[i]) +
                   log2((long double)pos[i]));
  }
  for (int i = 0; i < ns; ++i) {
    lneg.push_back(lneg.back() + log((long double)neg[i]) +
                   log2((long double)neg[i]));
  }
  vector<pair<double, int>> pp;
  if (ps > 0) {
    int ans = 1;
    int itt = (k - min(ps, k)) % 2 == 0 ? min(ps, k) : min(ps, k) - 1;
    int l = itt - 1, r = k - itt;
    while (l > 0 && r + 1 < ns &&
           (int64_t)pos[l] * pos[l - 1] <= (int64_t)neg[r] * neg[r + 1]) {
      l -= 2;
      r += 2;
    }
    for (int i = 0; i <= l; ++i) {
      ans = (int64_t)ans * pos[i] % P;
    }
    for (int i = 0; i < r; ++i) {
      ans = (int64_t)ans * (P - neg[i]) % P;
    }
    out << ans << '\\n';
  } else {
    if (k % 2 == 1) {
      if (zero) {
        cout << 0 << '\\n';
        return;
      }
      reverse(neg.begin(), neg.end());
    }
    int ans = 1;
    for (int i = 0; i < k; ++i) {
      ans = (int64_t)ans * (P - neg[i]) % P;
    }
    out << ans << '\\n';
    return;
  }
}


Error: 0

Fixed submission:

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

void solve(std::istream &in, std::ostream &out);

int main() {
#ifdef LOCAL
  freopen("../IO/e.in", "r", stdin);
//  freopen("../IO/f.out", "w", stdout);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
#endif
  solve(std::cin, std::cout);
  return 0;
}

constexpr int P = 1'000'000'007;

void solve(std::istream &in, std::ostream &out) {
  int n, k;
  in >> n >> k;
  vector<int> pos, neg;
  bool zero = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    in >> x;
    zero |= x == 0;
    if (x > 0)
      pos.push_back(x);
    if (x < 0)
      neg.push_back(-x);
  }
  int ps = pos.size();
  int ns = neg.size();
  if (ps + ns < k) {
    out << 0 << '\\n';
    return;
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());
  vector<long double> lpos(1, 0), lneg(1, 0);
  for (int i = 0; i < ps; ++i) {
    lpos.push_back(lpos.back() + log((long double)pos[i]) +
                   log2((long double)pos[i]));
  }
  for (int i = 0; i < ns; ++i) {
    lneg.push_back(lneg.back() + log((long double)neg[i]) +
                   log2((long double)pos[i]));
  }
  vector<pair<double, int>> pp;
  if (ps > 0) {
    for (int i = 0; i <= k; i += 2) {
      if (i <= ns && k - i <= ps) {
        pp.emplace_back(lneg[i] + lpos[k - i], i);
      }
    }
    if (!pp.empty()) {
      sort(pp.rbegin(), pp.rend());
      int z = pp.begin()->second;
      int ans = 1;
      for (int i = 0; i < k - z; ++i) {
        ans = (int64_t)ans * pos[i] % P;
      }
      for (int i = 0; i < z; ++i) {
        ans = (int64_t)ans * (P - neg[i]) % P;
      }
      out << ans << '\\n';
    } else {
      if (zero) {
        out << 0 << '\\n';
        return;
      }
      reverse(pos.begin(), pos.end());
      reverse(neg.begin(), neg.end());
      vector<double long> lpos(1, 0), lneg(1, 0);
      for (int i = 0; i < ps; ++i) {
        lpos.push_back(lpos.back() + log((long double)pos[i]));
      }
      for (int i = 0; i < ns; ++i) {
        lneg.push_back(lneg.back() + log((long double)neg[i]));
      }
      for (int i = 1; i <= k; i += 2) {
        if (i <= ns && k - i <= ps) {
          pp.emplace_back(lneg[i] + lpos[k - i], i);
        }
      }
      sort(pp.begin(), pp.end());
      int z = pp.begin()->second;
      int ans = 1;
      for (int i = 0; i < k - z; ++i) {
        ans = (int64_t)ans * pos[i] % P;
      }
      for (int i = 0; i < z; ++i) {
        ans = (int64_t)ans * (P - neg[i]) % P;
      }
      out << ans << '\\n';
    }
  } else {
    if (k % 2 == 1) {
      if (zero) {
        cout << 0 << '\\n';
        return;
      }
      reverse(neg.begin(), neg.end());
    }
    int ans = 1;
    for (int i = 0; i < k; ++i) {
      ans = (int64_t)ans * (P - neg[i]) % P;
    }
    out << ans << '\\n';
    return;
  }
}