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

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char **argv) {

  long long int N, K;
  cin >> N >> K;
  vector<long long> pos, neg;
  for (int c = 0; c < N; c++) {
    long long z;
    cin >> z;
    if (z >= 0)
      pos.push_back(z);
    else
      neg.push_back(z);
  }

  sort(pos.rbegin(), pos.rend());
  sort(neg.begin(), neg.end());
  long long MOD = 1e9 + 7;

  if (K == N) {
    long long res = 1;
    for (auto z : pos) {
      res *= z;
      res %= MOD;
    }
    for (auto z : neg) {
      res *= z;
      res %= MOD;
    }
    cout << (res + 10 * MOD) % MOD;
    return 0;
  }

  if (neg.size() == 0) {
    long long res = 1;
    for (int c = 0; c < K; c++) {
      res *= pos[c];
      res %= MOD;
    }
    cout << res;
    return 0;
  }

  if (pos.size() == 0 && K % 2 == 1) {
    sort(neg.rbegin(), neg.rend());
    long long res = 1;
    for (int c = 0; c < K; c++) {
      res *= neg[c];
      res %= MOD;
    }
    cout << (res + 10LL * MOD) % MOD;
    return 0;
  }

  if (pos.size() == 0) {
    long long res = 1;
    for (int c = 0; c < K; c++) {
      res *= neg[c];
      res %= MOD;
    }

    cout << (res + 10LL * MOD) % MOD;
    return 0;
  }

  int a = 0, b = 0;

  for (int c = 0; c < K; c++) {
    if (a >= pos.size() || (b < neg.size() && -neg[b] > pos[a])) {
      b++;
    } else {
      a++;
    }
  }

  if (b % 2 == 1) {

    vector<pair<long long, long long>> cases(2, make_pair(MOD + 10, MOD + 10));
    if (a > 0 && b < neg.size()) {
      cases[0] = make_pair(pos[a - 1], -neg[b]);
    }
    if (a < pos.size()) {
      cases[1] = make_pair(-neg[b - 1], pos[a]);
    }
    if (cases[0].first > MOD ||
        (cases[1].first <= MOD && cases[1].first * cases[0].second <=
                                      cases[1].second * cases[0].first)) {
      b--;
      a++;
    } else if (cases[0].first <= MOD) {
      a--;
      b++;
    }
  }

  long long res = 1;
  for (int c = 0; c < a; c++) {
    res *= pos[c];
    res %= MOD;
  }

  for (int c2 = 0; c2 < b; c2++) {
    res *= neg[c2];
    res %= MOD;
  }

  if (b % 2 == 1) {
    sort(pos.begin(), pos.end());
    sort(neg.rbegin(), neg.rend());
    long long trueRes = 1;
    int a2 = 0, b2 = 0;
    for (int c = 0; c < K; c++) {
      if (a2 >= pos.size() || (b2 < neg.size() && (-neg[b2]) < pos[a2])) {
        trueRes *= neg[b2++];
        trueRes %= MOD;
      } else {
        trueRes *= pos[a2++];
        trueRes %= MOD;
      }
    }
    cout << (trueRes + MOD) % MOD;
    return 0;
  }

  cout << (res + MOD) % MOD;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char **argv) {

  int N, K;
  cin >> N >> K;
  vector<long long> pos, neg;
  for (int c = 0; c < N; c++) {
    long long z;
    cin >> z;
    if (z >= 0)
      pos.push_back(z);
    else
      neg.push_back(z);
  }

  sort(pos.rbegin(), pos.rend());
  sort(neg.begin(), neg.end());

  if (pos.size() == 0 && K % 2 == 1) {
    sort(neg.rbegin(), neg.rend());
  }

  int a = 0, b = 0;

  for (int c = 0; c < K; c++) {
    if (a >= pos.size() || (b < neg.size() && -neg[b] > pos[a])) {
      b++;
    } else {
      a++;
    }
  }
  long long MOD = 1e9 + 7;

  if (b % 2 == 1) {

    vector<pair<long long, long long>> cases(2, make_pair(MOD + 10, MOD + 10));
    if (a > 0 && b < neg.size()) {
      cases[0] = make_pair(pos[a - 1], -neg[b]);
    }
    if (a < pos.size()) {
      cases[1] = make_pair(-neg[b - 1], pos[a]);
    }
    if (cases[0].first > MOD ||
        (cases[1].first < MOD &&
         cases[1].first * cases[0].second < cases[1].second * cases[0].first)) {
      b--;
      a++;
    } else if (cases[0].first < MOD) {
      a--;
      b++;
    }
  }

  long long res = 1;
  for (int c = 0; c < a; c++) {
    res *= pos[c];
    res %= MOD;
  }

  for (int c2 = 0; c2 < b; c2++) {
    res *= neg[c2];
    res %= MOD;
  }
  cout << (res + 10 * MOD) % MOD;
}