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

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
ll mod = 1e9 + 7;
int main() {
  FAST ll n, k;
  bool allneg = 1;
  cin >> n >> k;
  ll A[n];
  FOR(i, 0, n - 1) cin >> A[i];
  FOR(i, 0, n - 1) allneg &= A[i] < 0;
  sort(A, A + n, [](ll x, ll y) { return llabs(x) > llabs(y); });
  ll neg = 0, dc = 0;
  FOR(i, 0, k - 1) {
    neg += A[i] < 0;
    if (A[i] == 0)
      dc = 1;
  }
  if (!dc && (neg & 1) && !allneg && n != k) {
    ll lpos = -1, lneg = -1, fpos = -1, fneg = -1;
    FOR(i, 0, k - 1) {
      if (A[i] < 0)
        lneg = i;
      if (A[i] > 0)
        lpos = i;
    }
    DEC(i, n - 1, k) {
      if (A[i] < 0)
        fneg = i;
      if (A[i] > 0)
        fpos = i;
    }
    if ((lpos != -1 && fneg != -1) || (lneg != -1 && fpos != -1)) {
      ll opt = 0;
      if (lpos == -1 || fneg == -1)
        opt = 2;
      else if (lneg == -1 || fpos == -1)
        opt = 1;
      else {
        ld sum1 = 0, sum2 = 0;
        FOR(i, 0, k - 1) {
          if (i ^ lpos)
            sum1 += log2l(llabs(A[i]));
          if (i ^ lneg)
            sum2 += log2l(llabs(A[i]));
        }
        sum1 += log2l(llabs(A[fneg]));
        sum2 += log2l(llabs(A[fpos]));
        if (sum1 > sum2)
          opt = 1;
        else
          opt = 2;
      }
      ll ans = 1;
      if (opt == 1) {
        FOR(i, 0, k - 1)
        if (i ^ lpos) ans *= A[i],
            ans %= mod, ans += mod, ans %= mod;
        ans *= A[fneg], ans %= mod, ans += mod, ans %= mod;
      } else {
        FOR(i, 0, k - 1)
        if (i ^ lneg) ans *= A[i],
            ans %= mod, ans += mod, ans %= mod;
        ans *= A[fpos], ans %= mod, ans += mod, ans %= mod;
      }
      cout << ans << '\\n';
      return 0;
    }
  }

  if (neg & 1) {
    sort(A, A + n, [](ll x, ll y) { return llabs(x) < llabs(y); });
  }
  ll ans = 1;
  FOR(i, 0, k - 1) { ans *= A[i], ans %= mod, ans += mod, ans %= mod; }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
ll mod = 1e9 + 7;
int main() {
  FAST ll n, k;
  bool allneg = 1;
  cin >> n >> k;
  ll A[n];
  FOR(i, 0, n - 1) cin >> A[i];
  FOR(i, 0, n - 1) allneg &= A[i] < 0;
  sort(A, A + n, [](ll x, ll y) { return llabs(x) > llabs(y); });
  ll neg = 0, dc = 0;
  FOR(i, 0, k - 1) {
    neg += A[i] < 0;
    if (A[i] == 0)
      dc = 1;
  }
  if (!dc && (neg & 1) && !allneg) {
    ll lpos = -1, lneg = -1, fpos = -1, fneg = -1;
    FOR(i, 0, k - 1) {
      if (A[i] < 0)
        lneg = i;
      if (A[i] > 0)
        lpos = i;
    }
    DEC(i, n - 1, k) {
      if (A[i] < 0)
        fneg = i;
      if (A[i] > 0)
        fpos = i;
    }
    ll opt = 0;
    if (lpos == -1 || fneg == -1)
      assert(lneg != -1 && fpos != -1), opt = 2;
    else {
      ld sum1 = 0, sum2 = 0;
      FOR(i, 0, k - 1) {
        if (i ^ lpos)
          sum1 += log2l(llabs(A[i]));
        if (i ^ lneg)
          sum2 += log2l(llabs(A[i]));
      }
      sum1 += log2l(llabs(A[fneg]));
      sum2 += log2l(llabs(A[fpos]));
      if (sum1 > sum2)
        opt = 1;
      else
        opt = 2;
    }
    ll ans = 1;
    if (opt == 1) {
      FOR(i, 0, k - 1)
      if (i ^ lpos) ans *= A[i],
          ans %= mod, ans += mod, ans %= mod;
      ans *= A[fneg], ans %= mod, ans += mod, ans %= mod;
    } else {
      FOR(i, 0, k - 1)
      if (i ^ lneg) ans *= A[i],
          ans %= mod, ans += mod, ans %= mod;
      ans *= A[fpos], ans %= mod, ans += mod, ans %= mod;
    }
    cout << ans << '\\n';
  } else {
    if (neg & 1) {
      sort(A, A + n, greater<>());
    }
    ll ans = 1;
    FOR(i, 0, k - 1) { ans *= A[i], ans %= mod, ans += mod, ans %= mod; }
    cout << ans << '\\n';
  }
}