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

/*
 * Created by: WeirdBugsButOkay
 */

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ff first
#define ss second

using namespace std;

void solve() {
  ll k, n;
  cin >> n >> k;
  ll inf = 1000000007;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> neg, pos;
  for (ll i = 0; i < n; i++)
    if (a[i] < 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  sort(neg.begin(), neg.end());
  sort(pos.rbegin(), pos.rend());
  if (pos.size() == 0 && k % 2 == 1) {
    ll ans = 1;
    reverse(neg.begin(), neg.end());
    for (ll i = 0; i < k; i++)
      ans = (ans * (-neg[i])) % inf;
    ans = (inf - ans) % inf;
    cout << ans << endl;
    return;
  }
  if (n == k) {
    ll ans = 1;
    bool neg = 0;
    for (ll i = 0; i < n; i++) {
      ans = (ans * abs(a[i])) % inf;
      neg ^= (a[i] < 0);
    }
    if (neg)
      ans = (inf - ans) % inf;
    cout << ans << endl;
    return;
  }
  ll cnt = max(0ll, k - (ll)pos.size());
  if (cnt % 2 == 1)
    cnt++;
  ll rc = k - cnt;
  // cout << cnt << " " << rc << endl;
  while (cnt + 1 < (ll)neg.size() && cnt + 1 < k) {
    ll np = neg[cnt] * neg[cnt + 1];
    ll pp = pos[rc - 1] * pos[rc - 2];
    if (pp >= np)
      break;
    else {
      rc -= 2;
      cnt += 2;
    }
  }
  ll ans = 1;
  for (ll i = 0; i < rc; i++)
    ans = (ans * pos[i]) % inf;
  for (ll i = 0; i < cnt; i++)
    ans = (ans * (-neg[i])) % inf;
  cout << ans << endl;
}

int main() {
  int q = 1;
  // scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

/*
 * Created by: WeirdBugsButOkay
 */

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ff first
#define ss second

using namespace std;

void solve() {
  ll k, n;
  cin >> n >> k;
  ll inf = 1000000007;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> neg, pos;
  for (ll i = 0; i < n; i++)
    if (a[i] < 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  sort(neg.begin(), neg.end());
  sort(pos.rbegin(), pos.rend());
  if ((ll)pos.size() + (ll)neg.size() < k) {
    cout << 0 << endl;
    return;
  }
  if (pos.size() == 0 && k % 2 == 1) {
    ll ans = 1;
    reverse(neg.begin(), neg.end());
    for (ll i = 0; i < k; i++)
      ans = (ans * (-neg[i])) % inf;
    ans = (inf - ans) % inf;
    cout << ans << endl;
    return;
  }
  if (n == k) {
    ll ans = 1;
    bool neg = 0;
    for (ll i = 0; i < n; i++) {
      ans = (ans * abs(a[i])) % inf;
      neg ^= (a[i] < 0);
    }
    if (neg)
      ans = (inf - ans) % inf;
    cout << ans << endl;
    return;
  }
  ll cnt = max(0ll, k - (ll)pos.size());
  if (cnt % 2 == 1)
    cnt++;
  ll rc = k - cnt;
  while (cnt + 1 < (ll)neg.size()) {
    ll np = neg[cnt] * neg[cnt + 1];
    ll pp = pos[rc - 1] * pos[rc - 2];
    if (pp >= np)
      break;
    else {
      rc -= 2;
      cnt += 2;
    }
  }
  ll ans = 1;
  for (ll i = 0; i < rc; i++)
    ans = (ans * pos[i]) % inf;
  for (ll i = 0; i < cnt; i++)
    ans = (ans * (-neg[i])) % inf;
  cout << ans << endl;
}

int main() {
  int q = 1;
  // scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}