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

// 雪花飄飄北風嘯嘯
// 天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \\
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \\
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

#define indexed_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int MOD = 1000000007;

int n, k;
vector<int> pos, neg;
ll ppos[200005], pneg[200005];
long double dpos[200005], dneg[200005];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;

  if (n == k) {
    int t;
    bool negative = false;
    ll ans = 1;

    rep(x, 0, n) {
      cin >> t;

      if (t <= 0)
        ans = ans * -t % MOD, negative ^= true;
      else
        ans = ans * t % MOD;
    }

    if (negative)
      cout << (MOD - ans) % MOD;
    else
      cout << ans << endl;

    return 0;
  }

  int t;
  rep(x, 0, n) {
    cin >> t;

    if (t <= 0)
      neg.push_back(-t);
    else
      pos.push_back(t);
  }

  sort(all(neg)), reverse(all(neg));
  sort(all(pos)), reverse(all(pos));

  pneg[0] = 1;
  dneg[0] = 0;
  rep(x, 0, sz(neg)) {
    pneg[x + 1] = (pneg[x] * neg[x]) % MOD;
    dneg[x + 1] = dneg[x] + log((long double)neg[x]);
  }

  ppos[0] = 1;
  dpos[0] = 0;
  rep(x, 0, sz(pos)) {
    ppos[x + 1] = (ppos[x] * pos[x]) % MOD;
    dpos[x + 1] = dpos[x] + log((long double)pos[x]);
  }

  bool used = false;

  long double best = -1e100;
  ll ans = 0;

  // cerr<<sz(pos)<<" "<<sz(neg)<<endl;

  rep(x, 0, sz(neg) + 1) {
    if (x % 2 == 1 || sz(neg) < x)
      continue;
    int y = k - x;

    if (y < 0 || sz(pos) < y)
      continue;

    // cerr<<x<<" "<<y<<endl;
    // cerr<<best<<endl;

    used = true;
    long double temp = dneg[x] + dpos[y];

    if (best < temp) {
      best = temp;
      ans = pneg[x] * ppos[y] % MOD;
    }
  }

  if (!used) {
    ll ans = 1;

    rep(x, sz(neg) - k, sz(neg)) ans = ans * neg[x] % MOD;

    cout << (MOD - ans) % MOD;
  } else {
    cout << ans;
  }
}


Error: WA

Fixed submission:

// 雪花飄飄北風嘯嘯
// 天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \\
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \\
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

#define indexed_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int MOD = 1000000007;

int n, k;
vector<int> pos, neg;
ll ppos[200005], pneg[200005];
double dpos[200005], dneg[200005];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;

  if (n == k) {
    int t;
    bool negative = false;
    ll ans = 1;

    rep(x, 0, n) {
      cin >> t;

      if (t <= 0)
        ans = ans * -t % MOD, negative ^= true;
      else
        ans = ans * t % MOD;
    }

    if (negative)
      cout << (MOD - ans) % MOD;
    else
      cout << ans << endl;

    return 0;
  }

  int t;
  rep(x, 0, n) {
    cin >> t;

    if (t <= 0)
      neg.push_back(-t);
    else
      pos.push_back(t);
  }

  sort(all(neg)), reverse(all(neg));
  sort(all(pos)), reverse(all(pos));

  pneg[0] = 1;
  dneg[0] = 0;
  rep(x, 0, sz(neg)) {
    pneg[x + 1] = (pneg[x] * neg[x]) % MOD;
    dneg[x + 1] = dneg[x] + log((double)neg[x]);
  }

  ppos[0] = 1;
  dpos[0] = 0;
  rep(x, 0, sz(pos)) {
    ppos[x + 1] = (ppos[x] * pos[x]) % MOD;
    dpos[x + 1] = dpos[x] + log((double)pos[x]);
  }

  double best = -1e30;
  ll ans;

  rep(x, 0, sz(neg) + 1) {
    if (x % 2 == 1 || sz(neg) < x)
      continue;
    int y = k - x;

    if (y < 0 || sz(pos) < y)
      continue;

    // cout<<x<<" "<<y<<endl;

    double temp = dneg[x] + dpos[y];
    if (best < temp) {
      best = temp;
      ans = pneg[x] * ppos[y] % MOD;
    }
  }

  // cout<<best<<endl;
  if (best == -1e30) {
    ll ans = 1;

    rep(x, sz(neg) - k, sz(neg)) ans = ans * neg[x] % MOD;

    cout << (MOD - ans) % MOD;
  } else {
    cout << ans;
  }
}