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

#include <bits/stdc++.h>

using namespace std;

const long long mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> pos, neg;
  for (int i = 0; i < N; i++) {
    long long A;
    cin >> A;
    if (A >= 0)
      pos.push_back(A);
    else
      neg.push_back(-A);
  }

  int n_pos = int(pos.size()), n_neg = int(neg.size());

  long long ans = 1;

  if (K == N) {
    for (int i = 0; i < n_pos; i++) {
      ans *= pos[i];
      ans %= mod;
    }

    for (int i = 0; i < n_neg; i++) {
      ans *= -neg[i];
      ans %= mod;
    }
  } else if (n_neg == N && (K & 1)) {
    sort(neg.begin(), neg.end());

    ans = -1ll;
    for (int i = 0; i < K; i++) {
      ans *= neg[i];
      ans %= mod;
    }
  } else {
    sort(pos.begin(), pos.end(), greater<long long>());
    sort(neg.begin(), neg.end(), greater<long long>());

    int idx_pos = 0, idx_neg = 0;

    if (K & 1) {
      --K;
      ans = pos[idx_pos++];
    }

    for (int i = 0; i < K; i += 2) {
      if (idx_pos + 1 < n_pos && idx_neg + 1 < n_neg) {
        if (pos[idx_pos] * pos[idx_pos + 1] < neg[idx_neg] * neg[idx_neg + 1]) {
          ans *= neg[idx_neg] * neg[idx_neg + 1] % mod;
          idx_neg += 2;
        } else {
          ans *= pos[idx_pos] * pos[idx_pos + 1] % mod;
          idx_pos += 2;
        }
      } else if (idx_pos + 1 < n_pos) {
        ans *= pos[idx_pos] * pos[idx_pos + 1] % mod;
        idx_pos += 2;
      } else {
        ans *= neg[idx_neg] * neg[idx_neg + 1] % mod;
        idx_neg += 2;
      }

      ans %= mod;
    }
  }

  cout << (ans + mod) % mod << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> pos, neg;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    if (A >= 0)
      pos.push_back(A);
    else
      neg.push_back(-A);
  }

  sort(pos.begin(), pos.end(), greater<long long>());
  sort(neg.begin(), neg.end(), greater<long long>());

  int n_pos = pos.size(), n_neg = neg.size();

  long long ls = -1;
  int idx = -100;
  for (int i = 0; i < n_neg; i += 2) {
    if (K - i - 2 >= 0 && K - i - 2 < n_pos &&
        min(neg[i], pos[K - i - 2]) >= ls) {
      ls = min(neg[i], pos[K - i - 2]);
      idx = i;
    }
  }

  long long ans = 1;

  if (idx >= 0) {
    for (int i = 0; i < idx; i++) {
      ans *= -neg[i];
      ans %= mod;
    }

    for (int i = 0; i < K - idx - 2; i++) {
      ans *= pos[i];
      ans %= mod;
    }
  } else {
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());

    int idx_pos = 0, idx_neg = 0;
    for (int i = 0; i < K; i++) {
      if (idx_pos < n_pos && idx_neg < n_neg) {
        if (pos[idx_pos] < neg[idx_neg]) {
          ans *= pos[idx_pos++];
        } else {
          ans *= -neg[idx_neg++];
        }
      } else if (idx_pos < n_pos) {
        ans *= pos[idx_pos++];
      } else {
        ans *= -neg[idx_neg++];
      }
      ans %= mod;
    }
  }

  if (n_pos > 0 && n_pos <= K && ls <= pos[K - 1]) {
    ans = 1;
    for (int i = 0; i < K; i++) {
      ans *= pos[i];
      ans %= mod;
    }
  }
  if (!(n_neg & 1) && n_neg > 0 && n_neg <= K && ls <= neg[K - 1]) {
    ans = 1;
    for (int i = 0; i < n_neg; i++) {
      ans *= -neg[i];
      ans %= mod;
    }
  }

  cout << (ans + mod) % mod << endl;

  return 0;
}