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

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

int mod = 1e9 + 7;

signed main() {

  int n, k, zero = 0;
  vector<int> A, B;

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 0)
      zero++;
    if (0 < a)
      A.push_back(a);
    if (a < 0)
      B.push_back(-a);
  }

  sort(A.begin(), A.end(), greater<int>());
  sort(B.begin(), B.end(), greater<int>());

  // pattern 1
  if (n - k < zero) {
    cout << 0 << endl;
    return 0;
  }

  // pattern 2
  int mul = 1, cnt = k;
  int idxA = 0, idxB = 0;

  while (cnt) {

    int Acnt = (int)A.size() - idxA;
    int Bcnt = (int)B.size() - idxB;

    if (cnt % 2) {

      if (Acnt) {
        mul = mul * A[idxA++] % mod;
        cnt--;
      } else
        break;

    } else {

      if (2 <= Acnt && 2 <= Bcnt) {

        if (A[idxA] * A[idxA + 1] < B[idxB] * B[idxB + 1]) {
          mul = mul * B[idxB++] % mod;
          mul = mul * B[idxB++] % mod;
          cnt -= 2;
        } else {
          mul = mul * A[idxA++] % mod;
          mul = mul * A[idxA++] % mod;
          cnt -= 2;
        }

      } else if (2 <= Acnt) {
        mul = mul * A[idxA++] % mod;
        mul = mul * A[idxA++] % mod;
        cnt -= 2;
      } else if (2 <= Bcnt) {
        mul = mul * B[idxB++] % mod;
        mul = mul * B[idxB++] % mod;
        cnt -= 2;
      } else
        break;
    }
  }

  if (cnt == 0) {
    cout << mul << endl;
    return 0;
  } else if (zero) {
    cout << 0 << endl;
    return 0;
  }

  // pattern 3

  sort(B.begin(), B.end());

  if (n == k) {

    mul = 1;

    for (int a : A)
      mul = mul * a % mod;
    for (int b : B)
      mul = mul * b % mod;

    cout << mod - mul << endl;

  } else {

    mul = 1;

    for (int i = 0; i < k; i++)
      mul = mul * B[i] % mod;

    cout << mod - mul << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int mod = 1e9 + 7;

signed main() {

  int n, k, zero = 0;
  vector<int> A, B;

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 0)
      zero++;
    if (0 < a)
      A.push_back(a);
    if (a < 0)
      B.push_back(-a);
  }

  sort(A.begin(), A.end(), greater<int>());
  sort(B.begin(), B.end(), greater<int>());

  // pattern 1
  if (n - k < zero) {
    cout << 0 << endl;
    return 0;
  }

  // pattern 2
  int mul = 1, cnt = k;
  int idxA = 0, idxB = 0;

  while (cnt) {

    int Acnt = (int)A.size() - idxA;
    int Bcnt = (int)B.size() - idxB;

    if (cnt % 2) {

      if (Acnt) {
        mul = mul * A[idxA++] % mod;
        cnt--;
      } else
        break;

    } else {

      if (2 <= Acnt && 2 <= Bcnt) {

        if (A[idxA] + A[idxA + 1] < B[idxB] + B[idxB + 1]) {
          mul = mul * B[idxB++] % mod;
          mul = mul * B[idxB++] % mod;
          cnt -= 2;
        } else {
          mul = mul * A[idxA++] % mod;
          mul = mul * A[idxA++] % mod;
          cnt -= 2;
        }

      } else if (2 <= Acnt) {
        mul = mul * A[idxA++] % mod;
        mul = mul * A[idxA++] % mod;
        cnt -= 2;
      } else if (2 <= Bcnt) {
        mul = mul * B[idxB++] % mod;
        mul = mul * B[idxB++] % mod;
        cnt -= 2;
      } else
        break;
    }
  }

  if (cnt == 0) {
    cout << mul << endl;
    return 0;
  } else if (zero) {
    assert(0);
    cout << 0 << endl;
    return 0;
  }

  // pattern 3

  sort(B.begin(), B.end());

  if (n == k) {

    mul = 1;

    for (int a : A)
      mul = mul * a % mod;
    for (int b : B)
      mul = mul * b % mod;

    cout << mod - mul << endl;

  } else {

    mul = 1;

    for (int i = 0; i < k; i++)
      mul = mul * B[i] % mod;

    cout << mod - mul << endl;
  }

  return 0;
}