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

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

int main() {
  constexpr size_t MOD = 1e9 + 7;
  size_t N, K;
  cin >> N >> K;

  vector<size_t> negative;
  vector<size_t> non_negative;
  for (size_t i = 0; i < N; i++) {
    long long A;
    cin >> A;
    if (A < 0) {
      negative.push_back(-A);
    } else {
      non_negative.push_back(A);
    }
  }
  size_t ans = 1;
  if (N == K) {
    for (size_t n : negative) {
      ans *= n;
      ans %= MOD;
    }
    for (size_t n : non_negative) {
      ans *= n;
      ans %= MOD;
    }
    if (negative.size() % 2) {
      ans = (MOD - ans) % MOD;
    }
  } else if (non_negative.empty() && K % 2 == 1) {
    // The answer is negative
    sort(negative.begin(), negative.end());
    for (size_t i = 0; i < K; i++) {
      ans *= negative[i];
      ans %= MOD;
    }
    ans = (MOD - ans) % MOD;
  } else {
    // The answer is non-negative
    sort(negative.rbegin(), negative.rend());
    sort(non_negative.rbegin(), non_negative.rend());
    non_negative.push_back(1);

    size_t i = 0;
    size_t j = 0;
    size_t count = 0;

    while (count < K) {

      if (i + 1 < non_negative.size() &&
          ((j + 1) >= negative.size() || count + 2 > K ||
           non_negative[i] * non_negative[i + 1] >=
               negative[j] * negative[j + 1])) {
        assert(i + 1 < non_negative.size());
        ans *= non_negative[i];
        ans %= MOD;
        i++;
        count++;
      } else {
        assert(j + 1 < negative.size());
        ans *= negative[j];
        ans %= MOD;
        ans *= negative[j + 1];
        ans %= MOD;
        j += 2;
        count += 2;
      }
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  constexpr size_t MOD = 1e9 + 7;
  size_t N, K;
  cin >> N >> K;

  vector<size_t> negative;
  vector<size_t> non_negative;
  for (size_t i = 0; i < N; i++) {
    long long A;
    cin >> A;
    if (A < 0) {
      negative.push_back(-A);
    } else {
      non_negative.push_back(A);
    }
  }

  size_t ans = 1;
  if (non_negative.empty() && K % 2 == 1) {
    // The answer is negative
    sort(negative.begin(), negative.end());
    for (size_t i = 0; i < K; i++) {
      ans *= negative[i];
      ans %= MOD;
    }
    ans = (MOD - ans) % MOD;
  } else {
    // The answer is non-negative
    sort(negative.rbegin(), negative.rend());
    sort(non_negative.rbegin(), non_negative.rend());
    negative.push_back(1);
    non_negative.push_back(1);

    size_t i = 0;
    size_t j = 0;
    size_t count = 0;

    while (count < K) {

      if (i + 1 < non_negative.size() &&
          ((j + 2) >= negative.size() ||
           non_negative[i] * non_negative[i + 1] >=
               negative[j] * negative[j + 1])) {
        assert(i + 1 < non_negative.size());
        ans *= non_negative[i];
        i++;
        ans %= MOD;
        count++;
      } else {
        assert(j + 1 + 1 < negative.size());
        ans *= negative[j];
        ans %= MOD;
        ans *= negative[j + 1];
        ans %= MOD;
        j += 2;
        count += 2;
      }
    }
  }
  cout << ans << endl;
}