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

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

#define ll long long
#define ar array

#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#define F_OR(i, a, b, s)                                                       \\
  for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)

#define E_ACH2(x, a) for (auto &x : a)
#define E_ACH3(x, y, a) for (auto &[x, y] : a)
#define E_ACH4(x, y, z, a) for (auto &[x, y, z] : a)
#define E_ACHC(...) GET5(__VA_ARGS__, E_ACH4, E_ACH3, E_ACH2)
#define EACH(...) E_ACHC(__VA_ARGS__)(__VA_ARGS__)

const int M = 1e9 + 7;
int n, k;
vector<ll> ap, an;

ll solve() {

  cin >> n >> k;
  FOR(n) {
    ll a;
    cin >> a;
    if (a > 0)
      ap.push_back(a);
    else
      an.push_back(-a);
  }
  sort(all(ap));
  sort(all(an));
  if (k == n ? an.size() % 2 == 0 : ap.size() || k % 2 == 0) {
    reverse(all(ap));
    reverse(all(an));
    ll ans = 1;
    int i = 0, j = 0;
    for (; k; --k) {
      if (i < ap.size() && (j >= an.size() || ap[i] > an[j])) {
        ans = ans * ap[i] % M;
        ++i;
      } else {
        ans = ans * an[j] % M;
        ++j;
      }
    }
    if (j & 1) {
      ans = 1;
      if (i < ap.size() && j - 1 >= 0 &&
          (j >= an.size() || i - 1 < 0 ||
           ap[i] * ap[i - 1] > an[j] * an[j - 1])) {
        FOR(k, i + 1)
        ans = ans * ap[k] % M;
        FOR(k, j - 1)
        ans = ans * an[k] % M;
      } else {
        FOR(k, i - 1)
        ans = ans * ap[k] % M;
        FOR(k, j + 1)
        ans = ans * an[k] % M;
      }
    }
    return ans;
  }
  ll ans = 1;
  for (int i = 0, j = 0; k; --k) {
    if (i < ap.size() && (j >= an.size() || ap[i] < an[j])) {
      ans = ans * ap[i] % M;
      ++i;
    } else {
      ans = ans * an[j] % M;
      ++j;
    }
  }
  return (M - ans) % M;
}

int main() { cout << solve(); }

Error: WA

Fixed submission:

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

typedef long long ll;
#define endl "\\n"

const int M = (int)1e9 + 7;
int n, k;
ll ans = 1;
vector<ll> pos, neg;
int pcnt = 0, ncnt = 0;

ll solve() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    ll foo;
    cin >> foo;
    if (foo > 0)
      pos.push_back(foo);
    else if (foo < 0)
      neg.push_back(abs(foo));
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  int P = pos.size(), N = neg.size();
  if (k == n ? N % 2 == 0 : P || k % 2 == 0) {
    reverse(pos.begin(), pos.end());
    reverse(neg.begin(), neg.end());
    for (; k; --k) {
      if (pcnt < P && (ncnt >= N || pos[pcnt] > neg[ncnt])) {
        ans = ans * pos[pcnt] % M;
        ++pcnt;
      } else {
        ans = ans * neg[ncnt] % M;
        ++ncnt;
      }
    }
    if (ncnt & 1) {
      ans = 1;
      if (pcnt < P && ncnt >= 1 &&
          (ncnt >= N || pcnt < 1 ||
           pos[pcnt] * pos[pcnt - 1] > neg[ncnt] * neg[ncnt - 1])) {
        for (int tt = 0; tt < pcnt + 1; ++tt)
          ans = ans * pos[tt] % M;
        for (int tt = 0; tt < ncnt - 1; ++tt)
          ans = ans * neg[tt] % M;
      } else {
        for (int tt = 0; tt < pcnt - 1; ++tt)
          ans = ans * pos[tt] % M;
        for (int tt = 0; tt < ncnt + 1; ++tt)
          ans = ans * neg[tt] % M;
      }
    }
    return ans % M;
  }
  ans = 1;
  for (int i = 0, j = 0; k; --k) {
    if (i < pos.size() && (j >= neg.size() || pos[i] < neg[j])) {
      ans = ans * pos[i] % M;
      ++i;
    } else {
      ans = ans * neg[j] % M;
      ++j;
    }
  }
  return (M - ans) % M;
}

int main() {
  // INT VS LL
  // freopen("input.txt", "r", stdin);
  //    INT VS LL
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << solve();
  return 0;
}