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

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

using ll = long long;
const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  vector<ll> po, ne;
  int z = 0;

  for (int i = 0; i < n; ++i) {
    if (a[i] < 0)
      ne.push_back(a[i]);
    else if (a[i] > 0)
      po.push_back(a[i]);
    else
      z++;
  }

  int sp = po.size();
  int sn = ne.size();

  bool fu = false;
  if (n == k)
    fu = (sn % 2 == 1);
  else if (sn == n)
    fu = (k % 2 == 1);

  ll ans = 1;
  if (fu) {
    sort(a.begin(), a.end(), [&](ll v1, ll v2) {
      if (abs(v1) < abs(v2))
        return true;
      else
        return false;
    });
    for (int i = 0; i < k; i++)
      ans = ans * abs(a[i]) % mod;
    ans = mod - ans;
  } else {
    sort(a.begin(), a.end(), [&](ll v1, ll v2) {
      if (abs(v1) > abs(v2))
        return true;
      else
        return false;
    });

    int cn = 0;
    for (int i = 0; i < k; i++)
      if (a[i] < 0)
        cn++;

    if (cn % 2 == 0) {
      for (int i = 0; i < k; i++)
        ans = ans * abs(a[i]) % mod;
    } else {
      int lp = -1, ln = -1, np = -1, nn = -1;
      for (int i = k - 1; i >= 0; i--)
        if (a[i] >= 0) {
          lp = i;
          break;
        }
      for (int i = k - 1; i >= 0; i--)
        if (a[i] < 0) {
          ln = i;
          break;
        }
      for (int i = k; i < n; i++)
        if (a[i] >= 0) {
          np = i;
          break;
        }
      for (int i = k; i < n; i++)
        if (a[i] < 0) {
          nn = i;
          break;
        }
      if (lp != -1 && np != -1 && ln != -1 && nn != -1) {
        if (abs(a[lp]) * abs(a[np]) < abs(a[ln]) * abs(a[nn])) {
          swap(a[lp], a[nn]);
        } else {
          swap(a[ln], a[np]);
        }
      } else if (lp != -1 && nn != -1)
        swap(a[lp], a[nn]);
      else
        swap(a[ln], a[np]);

      for (int i = 0; i < k; i++)
        ans = ans * abs(a[i]) % mod;
    }
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
const ll mod = 1000000007;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  vector<ll> nv, pv;
  int z = 0;

  for (int i = 0; i < n; i++) {
    if (a[i] > 0)
      pv.push_back(a[i]);
    else if (a[i] == 0)
      z++;
    else
      nv.push_back(a[i]);
  }

  int N = nv.size();
  int P = pv.size();

  bool neg = false;
  if (n == k)
    neg = (N % 2 == 1);
  else if (n == N)
    neg = (k % 2 == 1);

  ll ans = 1;
  if (neg) {
    for (int i = 0; i < n; i++)
      a[i] = abs(a[i]);
    sort(a.begin(), a.end());
    for (int i = 0; i < k; i++)
      ans = ans * a[i] % mod;
    ans = mod - ans;
  } else {
    sort(a.begin(), a.end(), [&](ll a, ll b) {
      if (abs(a) > abs(b))
        return true;
      else
        return false;
    });
    int cn = 0;
    for (int i = 0; i < k; i++)
      if (a[i] < 0)
        cn++;
    if (cn % 2 == 0) {
      for (int i = 0; i < k; i++)
        ans = ans * a[i] % mod;
    } else {
      ll ln = -1, lp = -1, nn = -1, np = -1;
      for (int i = k - 1; i >= 0; i--) {
        if (a[i] > 0)
          lp = i;
        break;
      }
      for (int i = k - 1; i >= 0; i--) {
        if (a[i] < 0)
          ln = i;
        break;
      }
      for (int i = k; i < n; i++) {
        if (a[i] > 0)
          np = i;
        break;
      }
      for (int i = k; i < n; i++) {
        if (a[i] < 0)
          nn = i;
        break;
      }

      if (z > n - k) {
        cout << 0 << endl;
        return 0;
      }

      bool a_0 = false;
      if (a[ln] == 0 || a[lp] == 0) {
        a_0 = true;
      } else if (a[np] == 0 && a[nn] == 0) {
        a_0 = true;
      } else if (a[np] != 0) {
        swap(a[np], a[ln]);
      } else if (a[nn] != 0) {
        swap(a[nn], a[lp]);
      } else if (ln != -1 && np != -1 && lp != -1 && nn != -1) {
        if (abs((double)a[ln] / a[np]) > abs((double)a[lp] / a[nn]))
          swap(a[lp], a[nn]);
        else
          swap(a[ln], a[np]);
      } else if (lp != -1 && nn != -1) {
        swap(a[lp], a[nn]);
      } else {
        swap(a[ln], a[np]);
      }
      for (int i = 0; i < k; i++)
        ans = ans * a[i] % mod;
      if (a_0)
        ans = 0;
    }
  }

  cout << ans << endl;
  return 0;
}