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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define f first
#define s second
const int MAXN = 7;
const int MAXK = 18;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

ll N, R, C, K, a;
vector<pair<double, ll>> pos, neg;
vi A;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  ll c = 0;
  bool hazero = 0;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    if (a == 0) {
      hazero = 1;
      continue;
    }
    A.pb(abs(a));
    double H = log2((double)abs(a));
    if (a > 0)
      pos.pb(H, c);
    else if (a < 0)
      neg.pb(H, c);
    ++c;
  }
  N = SZ(pos) + SZ(neg);
  if (N < K) {
    cout << 0;
    return 0;
  }
  sort(ALL(pos));
  reverse(ALL(pos));
  sort(ALL(neg));
  reverse(ALL(neg));
  if ((SZ(pos) == 0) && K % 2 == 1) {
    if (hazero) {
      cout << 0;
      return 0;
    }
    reverse(ALL(neg));
    // choose the smallest few
    ll ans = 1;
    for (int i = 0; i < K; ++i) {
      ans = ans * abs(A[neg[i].s]) % MOD;
    }
    cout << (MOD - ans) % MOD;
    return 0;
  } else if (N == K && SZ(neg) % 2 == 1) {
    if (hazero) {
      cout << 0;
      return 0;
    }
    // choose the smallest few
    ll ans = 1;
    for (auto i : A)
      ans = ans * i % MOD;
    cout << (MOD - ans) % MOD;
    return 0;
  } else {
    ld maxsum = -1;
    ll num_neg = 0;
    for (int n = 0; n <= K; n += 2) {
      ll p = K - n;
      // cerr<<p<<' '<<n<<'\\n';
      ld cur = 0;
      if (p > SZ(pos) || n > SZ(neg))
        continue;
      for (int i = 0; i < p; ++i)
        cur += pos[i].f;
      for (int i = 0; i < n; ++i)
        cur += neg[i].f;
      // cout<<cur<<'\\n';
      if (cur > maxsum) {
        num_neg = n;
        maxsum = cur;
      }
    }
    ll p = K - num_neg;
    ll cur = 1;
    for (int i = 0; i < p; ++i)
      cur = cur * A[pos[i].s] % MOD;
    for (int i = 0; i < num_neg; ++i)
      cur = cur * A[neg[i].s] % MOD;
    cout << cur;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define f first
#define s second
const int MAXN = 7;
const int MAXK = 18;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

ll N, R, C, K, a;
vector<pair<double, ll>> pos, neg;
vi A;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  ll c = 0;
  bool hazero = 0;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    if (a == 0) {
      hazero = 1;
      continue;
    }
    A.pb(abs(a));
    double H = log2((double)abs(a));
    if (a > 0)
      pos.pb(H, c);
    else if (a < 0)
      neg.pb(H, c);
    ++c;
  }
  if (N < K) {
    cout << 0;
    return 0;
  }
  N = SZ(pos) + SZ(neg);
  sort(ALL(pos));
  reverse(ALL(pos));
  sort(ALL(neg));
  reverse(ALL(neg));
  if (SZ(pos) == 0 && K % 2 == 1) {
    if (hazero) {
      cout << 0;
      return 0;
    }
    // choose the smallest few
    ll ans = 1;
    for (int i = 0; i < K; ++i) {
      ans = ans * abs(A[neg[i].s]) % MOD;
    }
    cout << (MOD - ans) % MOD;
    return 0;
  } else {
    ld maxsum = -1;
    ll num_neg = 0;
    for (int n = 0; n <= K; n += 2) {
      ll p = K - n;
      // cerr<<p<<' '<<n<<'\\n';
      ld cur = 0;
      if (p > SZ(pos) || n > SZ(neg))
        continue;
      for (int i = 0; i < p; ++i)
        cur += pos[i].f;
      for (int i = 0; i < n; ++i)
        cur += neg[i].f;
      // cout<<cur<<'\\n';
      if (cur > maxsum) {
        num_neg = n;
        maxsum = cur;
      }
    }
    ll p = K - num_neg;
    ll cur = 1;
    for (int i = 0; i < p; ++i)
      cur = cur * A[pos[i].s] % MOD;
    for (int i = 0; i < num_neg; ++i)
      cur = cur * A[neg[i].s] % MOD;
    cout << cur;
  }
}