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

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define un(x) x.erase(unique(all(x)), x.end())
#define xx first
#define yy second
#define pb push_back
#define all(v) v.begin(), v.end()

#define D(x) cerr << #x " = " << x << '\\n'
#define DBG cerr << "Hi!" << '\\n'

#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))

// inline int setBit(int n, int pos) { return n = n | (1 << pos); }
// inline int resetBit(int n, int pos) { return n = n & ~(1 << pos); }
// inline bool checkBit(int n, int pos) { return (bool)(n & (1 << pos)); }

// int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
// int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// unsigned int num = rng();
// shuffle(V.begin(), V.end(), rng);

/******************************************************************************************/

// const int MAX = 500010;
// const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

inline int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }
inline int mul(int a, int b) { return (a * 1LL * b) % MOD; }

vector<int> pos, neg;
vector<ld> lpos, lneg;

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

  // freopen("in", "r", stdin);
  // freopen("out", "w", stdout);

  int n, k, x;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x >= 0)
      pos.pb(x);
    else
      neg.pb(-x);
  }
  sort(all(pos), greater<int>());
  sort(all(neg), greater<int>());

  for (int i = 0; i < (int)pos.size(); i++)
    lpos.pb(log(pos[i]));
  for (int i = 0; i < (int)neg.size(); i++)
    lneg.pb(log(neg[i]));

  for (int i = 1; i < (int)lpos.size(); i++)
    lpos[i] += lpos[i - 1];
  for (int i = 1; i < (int)lneg.size(); i++)
    lneg[i] += lneg[i - 1];

  ld mx = 0;
  pii ans = {-1, -1};
  int res = 1;

  if ((int)pos.size() >= k) {
    ans = {k, 0};
    mx = lpos[k - 1];
  }

  for (int i = 1; i < (int)neg.size(); i += 2) {
    int j = k - (i + 1) - 1;
    if (j < -1 or j >= (int)pos.size())
      continue;
    ld sum = 0;
    if (j != -1)
      sum += lpos[j];
    sum += lneg[i];
    if (sum >= mx) {
      mx = sum;
      ans = {j + 1, i + 1};
    }
  }

  bool bad = false;

  if (ans.xx == -1) {
    reverse(all(neg));
    reverse(all(pos));
    lpos.clear();
    lneg.clear();

    for (int i = 0; i < (int)pos.size(); i++)
      lpos.pb(log(pos[i]));
    for (int i = 0; i < (int)neg.size(); i++)
      lneg.pb(log(neg[i]));
    for (int i = 1; i < (int)lpos.size(); i++)
      lpos[i] += lpos[i - 1];
    for (int i = 1; i < (int)lneg.size(); i++)
      lneg[i] += lneg[i - 1];

    ld mn = 1e18;
    for (int i = 0; i < (int)neg.size(); i += 2) {
      int j = k - (i + 1) - 1;
      if (j < -1 or j >= (int)pos.size())
        continue;
      ld sum = 0;
      if (j != -1)
        sum += lpos[j];
      sum += lneg[i];
      if (sum <= mn) {
        mn = sum;
        ans = {j + 1, i + 1};
      }
    }
    bad = true;
  }

  for (int i = 0; i < ans.xx; i++)
    res = mul(res, pos[i]);
  for (int i = 0; i < ans.yy; i++)
    res = mul(res, neg[i]);

  if (bad) {

    if (pos.size() and pos[0] == 0) {
      res = 0;
    } else {
      res = MOD - res;
      res %= MOD;
    }
  }
  cout << res << '\\n';
  return 0;
}


Error: WA

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define un(x) x.erase(unique(all(x)), x.end())
#define xx first
#define yy second
#define pb push_back
#define all(v) v.begin(), v.end()

#define D(x) cerr << #x " = " << x << '\\n'
#define DBG cerr << "Hi!" << '\\n'

#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))

// inline int setBit(int n, int pos) { return n = n | (1 << pos); }
// inline int resetBit(int n, int pos) { return n = n & ~(1 << pos); }
// inline bool checkBit(int n, int pos) { return (bool)(n & (1 << pos)); }

// int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
// int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// unsigned int num = rng();
// shuffle(V.begin(), V.end(), rng);

/******************************************************************************************/

// const int MAX = 500010;
// const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

inline int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }
inline int mul(int a, int b) { return (a * 1LL * b) % MOD; }

vector<int> pos, neg;
vector<ld> lpos, lneg;

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

  // freopen("in", "r", stdin);
  // freopen("out", "w", stdout);

  int n, k, x;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x >= 0)
      pos.pb(x);
    else
      neg.pb(-x);
  }
  sort(all(pos), greater<int>());
  sort(all(neg), greater<int>());

  for (int i = 0; i < (int)pos.size(); i++)
    lpos.pb(log(pos[i]));
  for (int i = 0; i < (int)neg.size(); i++)
    lneg.pb(log(neg[i]));

  for (int i = 1; i < (int)lpos.size(); i++)
    lpos[i] += lpos[i - 1];
  for (int i = 1; i < (int)lneg.size(); i++)
    lneg[i] += lneg[i - 1];

  ld mx = 0;
  pii ans = {-1, -1};
  int res = 1;

  if ((int)pos.size() >= k) {
    ans = {k, 0};
    mx = lpos[k - 1];
  } else {
    for (int i = 1; i < (int)neg.size(); i += 2) {
      int j = k - (i + 1) - 1;
      if (j < 0 or j >= (int)pos.size())
        continue;
      if (lpos[j] + lneg[i] >= mx) {
        mx = lpos[j] + lneg[i];
        ans = {j + 1, i + 1};
      }
    }
  }
  if (ans.xx == -1) {
    reverse(all(neg));
    if (k >= (int)neg.size())
      assert(false);
    for (int i = 0; i < k; i++)
      res = mul(res, MOD - neg[i]);
  } else {
    for (int i = 0; i < ans.xx; i++)
      res = mul(res, pos[i]);
    for (int i = 0; i < ans.yy; i++)
      res = mul(res, neg[i]);
  }
  cout << res << '\\n';
  return 0;
}