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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> p, n;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0)
      p.push_back(a);
    else
      n.push_back(a);
  }

  int lp = p.size(), ln = n.size(), cnt = 0;
  if (K == N) {
    ll b = 1;
    for (int i = 0; i < lp; i++) {
      b *= p[i];
      b %= DIV;
    }
    for (int i = 0; i < ln; i++) {
      b *= n[i] + DIV;
      b %= DIV;
    }
    cout << b << endl;
  } else if (K % 2 == 1 && ln == N) {
    sort(n.begin(), n.end(), greater<ll>());
    ll b = 1;
    for (int i = 0; i < K; i++) {
      b *= (n[i] + DIV);
      b %= DIV;
    }
    cout << b << endl;
  }

  else {
    if (lp != 0)
      sort(p.begin(), p.end(), greater<ll>());
    if (ln != 0)
      sort(n.begin(), n.end());
    int px = 0, nx = 0;
    if (lp == 0)
      nx = K;
    else if (ln == 0)
      px = K;
    else {
      while (cnt < K) {
        if ((px < lp && nx >= ln) || abs(p[px]) >= abs(n[nx]))
          px++;
        else if ((px >= lp && nx < ln) || abs(p[px]) <= abs(n[nx]))
          nx++;
        cnt++;
      }
    }

    if (nx % 2 == 1) {
      if (px == 0)
        px++, nx--;
      else {
        ll s = p[px] * p[px - 1], t = n[nx] * n[nx - 1];
        if (s >= t)
          px++, nx--;
        else
          px--, nx++;
      }
    }

    ll b = 1;
    for (int i = 0; i < px; i++) {
      b *= p[i];
      b %= DIV;
    }
    for (int i = 0; i < nx; i++) {
      b *= n[i] + DIV;
      b %= DIV;
    }
    cout << b << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> p, n;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0)
      p.push_back(a);
    else
      n.push_back(a);
  }

  int lp = p.size(), ln = n.size(), cnt = 0;
  if (K % 2 == 1 && ln == N) {
    sort(n.begin(), n.end(), greater<ll>());
    ll b = 1;
    for (int i = 0; i < K; i++) {
      b *= (n[i] + DIV);
      b %= DIV;
    }
    cout << b << endl;
  } else {
    sort(p.begin(), p.end(), greater<ll>());
    sort(n.begin(), n.end());
    int px = 0, nx = 0;
    while (cnt < K) {
      if ((px < lp && nx >= ln) || abs(p[px]) >= abs(n[nx]))
        px++;
      else if ((px >= lp && nx < ln) || abs(p[px]) <= abs(n[nx]))
        nx++;
      cnt++;
    }

    if (nx % 2 == 1) {
      if (px == 0)
        px++, nx--;
      else {
        ll s = p[px] * p[px - 1], t = n[nx] * n[nx - 1];
        if (s >= t)
          px++, nx--;
        else
          px--, nx++;
      }
    }

    ll b = 1;
    for (int i = 0; i < px; i++) {
      b *= p[i];
      b %= DIV;
    }
    for (int i = 0; i < nx; i++) {
      b *= n[i] + DIV;
      b %= DIV;
    }
    cout << b << endl;
  }
}