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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define INF 9223372036854775807
const ll mod = 1e9 + 7;

bool sort1(ll a, ll b) { return abs(a) > abs(b); }

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll p = 0, nz = 0, z = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      z = 1;
    if (a[i] != 0)
      nz++;
    if (a[i] > 0)
      p++;
  }
  if (k == 1) {
    sort(a.begin(), a.end(), greater<ll>());
    cout << a[0];
    return 0;
  }
  if (k > nz) {
    cout << 0;
    return 0;
  }
  vector<ll> b = a;
  a.clear();
  for (ll i = 0; i < n; i++) {
    if (b[i] != 0)
      a.pb(b[i]);
  }
  n = a.size();
  if (k == n) {
    ll ans = 1;
    ll neg = 0;
    for (ll i = 0; i < n; i++)
      if (a[i] < 0)
        neg++;
    if (neg % 2 != 0 && z == 1) {
      cout << 0;
      return 0;
    }
    for (ll i = 0; i < n; i++)
      ans = (ans * a[i]) % mod;
    if (ans < 0)
      ans += mod;
    cout << ans;
    return 0;
  }
  if (p == 0) {
    if (k % 2 == 0)
      sort(a.begin(), a.end());
    else {
      if (z == 1) {
        cout << 0;
        return 0;
      }
      sort(a.begin(), a.end(), greater<ll>());
    }
    ll ans = 1;
    for (ll i = 0; i < k; i++)
      ans = (ans * a[i]) % mod;
    if (ans < 0)
      ans += mod;
    cout << ans;
    return 0;
  }
  if (k > p) {
    sort(a.begin(), a.end(), sort1);
    ll ans = 1, mnp = 1e10, mnn = 1e10;
    ll neg = 0;
    vector<ll> fin;
    for (ll i = 0; i < k; i++) {
      fin.pb(a[i]);
      if (a[i] > 0)
        mnp = a[i];
      if (a[i] < 0)
        mnn = a[i];
      if (a[i] < 0)
        neg++;
    }
    ll pp = 0, nn = 0;
    if (neg % 2 != 0) {
      // assert(a.size()==0);
      if (a[k] > 0) {
        pp = a[k];
        while (a[k] > 0 && k < n)
          k++;
        if (k != n)
          nn = a[k];
      } else {
        nn = a[k];
        while (a[k] < 0 && k < n)
          k++;
        if (k != n)
          pp = a[k];
      }
      if (pp * mnp > nn * mnn) {
        assert(mnn != 1e10);
        fin.erase(find(fin.begin(), fin.end(), mnn));
        fin.pb(pp);
      } else {
        assert(mnp != 1e10);
        fin.erase(find(fin.begin(), fin.end(), mnp));
        fin.pb(nn);
      }
    }
    for (ll i = 0; i < fin.size(); i++)
      ans = ((ans * fin[i]) % mod + mod) % mod;
    // if(ans<0) ans+=mod;
    assert(ans > 0);
    cout << ans;
    return 0;
  }
  vector<ll> pos, neg;
  for (ll i = 0; i < n; i++) {
    if (a[i] > 0)
      pos.pb(a[i]);
    else if (a[i] < 0)
      neg.pb(a[i]);
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end(), greater<ll>());
  vector<ll> fin, fin1;
  for (ll i = 0; i < k; i++)
    fin.pb(pos[i]);
  // cout<<fin.size()<<" ";
  while (neg.size() > 1 && fin.size() > 1) {
    ll n1 = fin.size(), n2 = neg.size();
    ll a1 = fin[n1 - 1], a2 = fin[n1 - 2];
    ll b1 = neg.back();
    neg.pop_back();
    ll b2 = neg.back();
    neg.pop_back();
    if (a1 * a2 >= b1 * b2)
      break;
    else {
      fin.pop_back();
      fin.pop_back();
      fin1.pb(b1);
      fin1.pb(b2);
    }
  }
  ll ans = 1;
  for (ll i = 0; i < fin.size(); i++)
    ans = (ans * fin[i]) % mod;
  for (ll i = 0; i < fin1.size(); i++)
    ans = (ans * abs(fin1[i])) % mod;
  if (ans < 0)
    ans += mod;
  // assert(ans>0);
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define INF 9223372036854775807
const ll mod = 1e9 + 7;

bool sort1(ll a, ll b) { return abs(a) > abs(b); }

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll p = 0, nz = 0, z = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      z = 1;
    if (a[i] != 0)
      nz++;
    if (a[i] > 0)
      p++;
  }
  if (k == 1) {
    sort(a.begin(), a.end(), greater<ll>());
    cout << a[0];
    return 0;
  }
  if (k > nz) {
    cout << 0;
    return 0;
  }
  vector<ll> b = a;
  a.clear();
  for (ll i = 0; i < n; i++) {
    if (b[i] != 0)
      a.pb(b[i]);
  }
  n = a.size();
  if (k == n) {
    ll ans = 1;
    ll neg = 0;
    for (ll i = 0; i < n; i++)
      if (a[i] < 0)
        neg++;
    if (neg % 2 != 0 && z == 1) {
      cout << 0;
      return 0;
    }
    for (ll i = 0; i < n; i++)
      ans = (ans * a[i]) % mod;
    if (ans < 0)
      ans += mod;
    cout << ans;
    return 0;
  }
  if (p == 0) {
    if (k % 2 == 0)
      sort(a.begin(), a.end());
    else {
      if (z == 1) {
        cout << 0;
        return 0;
      }
      sort(a.begin(), a.end(), greater<ll>());
    }
    ll ans = 1;
    for (ll i = 0; i < k; i++)
      ans = (ans * a[i]) % mod;
    if (ans < 0)
      ans += mod;
    cout << ans;
    return 0;
  }
  if (k > p) {
    sort(a.begin(), a.end(), sort1);
    ll ans = 1, mnp = INF, mnn = INF;
    ll neg = 0;
    vector<ll> fin;
    for (ll i = 0; i < k; i++) {
      fin.pb(a[i]);
      if (a[i] > 0)
        mnp = a[i];
      if (a[i] < 0)
        mnn = a[i];
      if (a[i] < 0)
        neg++;
    }
    double pp = 0, nn = 0;
    if (neg % 2 != 0) {
      assert(a.size() == 0);
      if (a[k] > 0) {
        pp = a[k];
        while (a[k] > 0 && k < n)
          k++;
        if (k != n)
          nn = a[k];
      } else {
        nn = a[k];
        while (a[k] < 0 && k < n)
          k++;
        if (k != n)
          pp = a[k];
      }
      if (pp / mnn > nn / mnp) {
        assert(mnn != INF);
        fin.erase(find(fin.begin(), fin.end(), mnn));
        fin.pb(pp);
      } else {
        assert(mnp != INF);
        fin.erase(find(fin.begin(), fin.end(), mnp));
        fin.pb(nn);
      }
    }
    for (ll i = 0; i < fin.size(); i++)
      ans = ((ans * fin[i]) % mod + mod) % mod;
    // if(ans<0) ans+=mod;
    assert(ans > 0);
    cout << ans;
    return 0;
  }
  vector<ll> pos, neg;
  for (ll i = 0; i < n; i++) {
    if (a[i] > 0)
      pos.pb(a[i]);
    else if (a[i] < 0)
      neg.pb(a[i]);
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end(), greater<ll>());
  vector<ll> fin, fin1;
  for (ll i = 0; i < k; i++)
    fin.pb(pos[i]);
  // cout<<fin.size()<<" ";
  while (neg.size() > 1 && fin.size() > 1) {
    ll n1 = fin.size(), n2 = neg.size();
    ll a1 = fin[n1 - 1], a2 = fin[n1 - 2];
    ll b1 = neg.back();
    neg.pop_back();
    ll b2 = neg.back();
    neg.pop_back();
    if (a1 * a2 >= b1 * b2)
      break;
    else {
      fin.pop_back();
      fin.pop_back();
      fin1.pb(b1);
      fin1.pb(b2);
    }
  }
  ll ans = 1;
  for (ll i = 0; i < fin.size(); i++)
    ans = (ans * fin[i]) % mod;
  for (ll i = 0; i < fin1.size(); i++)
    ans = (ans * abs(fin1[i])) % mod;
  if (ans < 0)
    ans += mod;
  // assert(ans>0);
  cout << ans;

  return 0;
}