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

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

int64_t mod(int64_t a, int64_t b) {
  if (a >= 0) {
    return a % b;
  } else {
    int64_t bb = ((-a) % b);
    if (bb == 0) {
      return 0;
    } else {
      return b - bb;
    }
  }
}
int main() {
  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<int64_t> A(N);

  int64_t DIV = 1000000007LL;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<int64_t> PLUSS;
  vector<int64_t> MINUSS;
  vector<int64_t> ZEROS;
  for (auto a : A) {
    if (a > 0) {
      PLUSS.push_back(a);
    } else if (a < 0) {
      MINUSS.push_back(a);
    } else {
      ZEROS.push_back(a);
    }
  }

  // ゼロ以下
  if (PLUSS.size() + MINUSS.size() < K) {
    cout << "0" << endl;
    return 0;
  } else if (PLUSS.size() == 0 && K % 2 == 1) {
    if (ZEROS.size() > 0) {
      cout << "0" << endl;
      return 0;
    } else {
      sort(MINUSS.begin(), MINUSS.end(),
           [](int64_t a, int64_t b) { return a > b; });
      int64_t cumul = 1;
      int j = 0;
      while (j < K) {
        if (j + 2 <= K) {
          cumul = mod(cumul * mod(MINUSS[j] * MINUSS[j + 1], DIV), DIV);
          j += 2;
        } else {
          cumul = mod(cumul * MINUSS[j], DIV);
          j++;
        }
      }
      cout << cumul << endl;
      return 0;
    }
  } else if (MINUSS.size() + PLUSS.size() == K && MINUSS.size() % 2 == 1) {
    if (ZEROS.size() > 0) {
      cout << "0" << endl;
      return 0;
    } else {
      int64_t cumul = 1;
      for (auto p : PLUSS) {
        cumul = mod(cumul * p, DIV);
      }
      int j = 0;
      while (j < MINUSS.size()) {
        if (j + 2 <= MINUSS.size()) {
          cumul = mod(cumul * mod(MINUSS[j] * MINUSS[j + 1], DIV), DIV);
          j += 2;
        } else {
          cumul = mod(cumul * MINUSS[j], DIV);
          j++;
        }
      }
      cout << cumul << endl;
      return 0;
    }
  }

  sort(PLUSS.begin(), PLUSS.end(), [](int64_t a, int64_t b) { return a > b; });
  sort(MINUSS.begin(), MINUSS.end());

  int64_t cumul = 1;
  int plus_idx = 0;
  int minus_idx = 0;
  int cnt = 0;
  while (true) {
    if (cnt == K) {
      break;
    } else if (cnt + 1 == K) {
      cumul = mod(cumul * PLUSS[plus_idx], DIV);
      plus_idx++;
      cnt++;
    } else if (minus_idx + 1 >= MINUSS.size()) {
      cumul = mod(cumul * PLUSS[plus_idx], DIV);
      plus_idx++;
      cnt++;
    } else if (plus_idx + 1 >= PLUSS.size()) {
      cumul =
          mod(cumul * mod(MINUSS[minus_idx] * MINUSS[minus_idx + 1], DIV), DIV);
      minus_idx += 2;
      cnt += 2;
    } else if ((plus_idx + 2 <= PLUSS.size()) &&
               (minus_idx + 2 <= MINUSS.size())) {
      int64_t plus_pair = PLUSS[plus_idx] * PLUSS[plus_idx + 1];
      int64_t minus_pair = MINUSS[minus_idx] * MINUSS[minus_idx + 1];
      if (plus_pair >= minus_pair) {
        cumul = mod(cumul * PLUSS[plus_idx], DIV);
        plus_idx += 1;
        cnt++;
      } else {
        cumul = mod(cumul * mod(minus_pair, DIV), DIV);
        minus_idx += 2;
        cnt += 2;
      }
    } else {
      cnt++;
    }
  }

  cout << cumul << endl;
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<uint64_t> A(N);

  uint64_t DIV = 1000000007LL;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<uint64_t> PLUSS;
  vector<uint64_t> MINUSS;
  vector<uint64_t> ZEROS;
  for (auto a : A) {
    if (a > 0) {
      PLUSS.push_back(a);
    } else if (a < 0) {
      MINUSS.push_back(a);
    } else {
      ZEROS.push_back(a);
    }
  }

  // ゼロ以下
  if (PLUSS.size() + MINUSS.size() <= K) {
    uint64_t cumul = 1;
    for (auto a : A) {
      cumul = (cumul * a) % DIV;
    }
    cout << cumul << endl;
    return 0;
  } else if (PLUSS.size() == 0 && K % 2 == 1) {
    if (ZEROS.size() >= 0) {
      cout << "0" << endl;
      return 0;
    } else {
      sort(MINUSS.begin(), MINUSS.end(),
           [](uint64_t a, uint64_t b) { return a > b; });
      uint64_t cumul = 1;
      for (int i = 0; i < K; i++) {
        cumul = (cumul * MINUSS[i]) % DIV;
      }
      cout << cumul << endl;
      return 0;
    }
  }

  sort(PLUSS.begin(), PLUSS.end(),
       [](uint64_t a, uint64_t b) { return a > b; });
  sort(MINUSS.begin(), MINUSS.end());

  uint64_t cumul = 1;
  int plus_idx = 0;
  int minus_idx = 0;
  int cnt = 0;
  while (true) {
    if (cnt == K) {
      break;
    } else if (cnt == K - 1) {
      cumul = (cumul * PLUSS[plus_idx]) % DIV;
      plus_idx++;
      cnt++;
    } else if (minus_idx >= MINUSS.size() - 1) {
      cumul = (cumul * PLUSS[plus_idx]) % DIV;
      plus_idx++;
      cnt++;
    } else if (plus_idx >= PLUSS.size() - 1) {
      cumul = (cumul * (MINUSS[minus_idx] * MINUSS[minus_idx + 1]) % DIV) % DIV;
      minus_idx += 2;
      cnt += 2;
    } else if ((plus_idx == PLUSS.size() - 2) && ((K - cnt) % 2) == 1) {
      cumul = (cumul * PLUSS[plus_idx]) % DIV;
      plus_idx++;
      cnt++;
    } else if ((plus_idx <= PLUSS.size() - 2) &&
               (minus_idx <= MINUSS.size() - 2)) {
      int64_t plus_pair = PLUSS[plus_idx] * PLUSS[plus_idx + 1];
      int64_t minus_pair = MINUSS[minus_idx] * MINUSS[minus_idx + 1];
      if (plus_pair >= minus_pair) {
        cumul = (cumul * (plus_pair % DIV)) % DIV;
        plus_idx += 2;
      } else {
        cumul = (cumul * (minus_pair % DIV)) % DIV;
        minus_idx += 2;
      }
      cnt += 2;
    } else {
      cnt++;
    }
  }

  cout << cumul << endl;
  return 0;
}