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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7, INF = mod * mod * 3; // M_PI
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rep1(i, N) for (ll i = 1; i < (N); i++)
#define pb push_back

ll N, K;
ll pos[200005];
ll neg[200005];

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  ll p1 = 0;
  ll n1 = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a >= 0) {
      pos[p1++] = a;
    } else {
      neg[n1++] = -a;
    }
  }
  sort(pos, pos + p1);
  sort(neg, neg + n1);
  if (N == K) {
    ll ans = 1;
    rep(i, p1) ans = ans * pos[i] % mod;
    rep(i, n1) ans = ans * (mod - neg[i]) % mod;
    cout << ans << endl;
    exit(0);
  }
  if (K % 2 == 1 && n1 == N) {
    ll ans = 1;
    rep(i, K) ans = ans * (mod - neg[i]) % mod;
    cout << ans << endl;
    exit(0);
  }
  ll ans = 1;
  while (K) {
    if (K >= 2 && n1 >= 2 &&
        (p1 <= 1 ||
         (p1 >= 2 && neg[n1 - 1] * neg[n1 - 2] >= pos[p1 - 1] * pos[p1 - 2]))) {
      ans = ans * neg[--n1] % mod;
      ans = ans * neg[--n1] % mod;
      K -= 2;
    } else {
      ans = ans * pos[--p1] % mod;
      K--;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7, INF = mod * mod * 3; // M_PI
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rep1(i, N) for (ll i = 1; i < (N); i++)
#define pb push_back

ll N, K;
P B[200005];
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a > 0) {
      B[i].first = a;
      B[i].second = 1;
    }
    if (a == 0) {
      B[i].first = a;
      B[i].second = 0;
    }
    if (a < 0) {
      B[i].first = -a;
      B[i].second = -1;
    }
  }
  sort(B, B + N);
  ll cnt = 0;
  ll x1 = INF;
  ll y1 = INF;
  ll x2 = INF;
  ll y2 = INF;
  for (ll i = N - 1; i >= N - K; i--) {
    if (B[i].second == 0) {
      cout << 0 << endl;
      exit(0);
    }
    if (B[i].second == -1) {
      cnt++;
      x1 = i;
    }
    if (B[i].second == 1) {
      y1 = i;
    }
  }
  rep(i, N - K) {
    if (B[i].second >= 0)
      x2 = i;
    if (B[i].second <= 0)
      y2 = i;
  }
  ll ans = 1;
  if (cnt % 2 == 0) {
    for (ll i = N - 1; i >= N - K; i--)
      ans = ans * B[i].first % mod;
    cout << ans << endl;
    exit(0);
  }
  if ((x1 == INF || x2 == INF) && (y1 == INF || y2 == INF)) {
    rep(i, K) {
      ll mul;
      if (B[i].second == -1)
        mul = -B[i].first + mod;
      else
        mul = B[i].first;
      ans = ans * mul % mod;
    }
    cout << ans << endl;
    exit(0);
  }
  ll ans1 = 1;
  ll ans2 = 1;
  for (ll i = N - 1; i >= N - K; i--) {
    if (i == x1)
      ans1 = ans1 * B[x2].first % mod;
    else
      ans1 = ans1 * B[i].first % mod;
    if (i == y1)
      ans2 = ans2 * B[y2].first % mod;
    else
      ans2 = ans2 * B[i].first % mod;
  }
  if (y1 == INF || y2 == INF) {
    cout << ans1 << endl;
    exit(0);
  } else {
    cout << ans2 << endl;
    exit(0);
  }
  if (B[y1].first * B[x2].first >= B[y2].first * B[x1].first) {
    cout << ans1 << endl;
  } else {
    cout << ans2 << endl;
  }
}