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

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <boost/multiprecision/cpp_int.hpp>
#include <cassert>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
typedef long long LL;
typedef long double LD;
typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N, K;
  cin >> N >> K;
  VLL maj, min;
  int Z = 0, M = 0;
  for (int n = 0; n < N; n++) {
    LL a;
    cin >> a;
    if (a > 0)
      maj.push_back(a);
    else if (a < 0)
      min.push_back(a);
  }
  Z = N - (int)(maj.size() + min.size());
  M = maj.size();
  N = min.size();
  sort(maj.begin(), maj.end(), [](LL a, LL b) { return a > b; });
  sort(min.begin(), min.end(), [](LL a, LL b) { return a < b; });
  if (M + N < K) {
    cout << 0 << "\\n";
    return 0;
  }
  int n = max(0, K - M);
  int m = K - n;
  if (n % 2 == 1) {
    n++;
    m--;
  }
  bool flag = false;
  while (0 <= n && n <= N && 0 <= m && m <= M) {
    flag = true;
    if (0 <= n + 2 && n + 2 <= N && 0 <= m - 2 && m - 2 <= M) {
      if (min[n] * min[n + 1] >= maj[m - 1] * maj[m - 2]) {
        n += 2;
        m -= 2;
      } else
        break;
    } else
      break;
  }
  if (flag) {
    LL ans = 1;
    for (int mm = 0; mm < m; mm++) {
      ans = ans * (maj[mm]);
      ans %= MOD;
    }
    for (int nn = 0; nn < n; nn++) {
      LL temp = min[nn] % MOD;
      temp += MOD;
      ans = (ans * temp) % MOD;
    }
    cout << ans << "\\n";
    return 0;
  }
  if (Z != 0) {
    cout << 0 << "\\n";
    return 0;
  }
  n = max(0, K - M);
  m = K - n;
  if (n % 2 == 0) {
    n++;
    m--;
  }
  for (int n = 0; n < N; n++)
    min[n] = -min[n];
  sort(min.begin(), min.end(), [](LL a, LL b) { return a < b; });
  while (0 <= n && n <= N && 0 <= m && m <= M) {
    if (0 <= n + 2 && n + 2 <= N && 0 <= m - 2 && m - 2 <= M) {
      if (min[n] * min[n + 1] <= maj[m - 1] * maj[m - 2]) {
        n += 2;
        m -= 2;
      } else {
        break;
      }
    } else
      break;
  }
  LL ans = 1;
  for (int mm = 0; mm < m; mm++) {
    ans = (ans * maj[mm]) % MOD;
  }
  for (int nn = 0; nn < n; nn++) {
    ans = (ans * min[nn]) % MOD;
  }
  ans *= -1;
  ans %= MOD;
  ans += MOD;
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <boost/multiprecision/cpp_int.hpp>
#include <cassert>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
typedef long long LL;
typedef long double LD;
typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N, K;
  cin >> N >> K;
  VLL maj, min;
  int Z = 0, M = 0;
  for (int n = 0; n < N; n++) {
    LL a;
    cin >> a;
    if (a > 0)
      maj.push_back(a);
    else if (a < 0)
      min.push_back(a);
  }
  Z = N - (int)(maj.size() + min.size());
  M = maj.size();
  N = min.size();
  sort(maj.begin(), maj.end(), [](LL a, LL b) { return a > b; });
  sort(min.begin(), min.end(), [](LL a, LL b) { return a < b; });
  if (M + N < K) {
    cout << 0 << "\\n";
    return 0;
  }
  int n = max(0, M - K);
  int m = K - n;
  if (n % 2 == 1) {
    n++;
    m--;
  }
  bool flag = false;
  while (0 <= n && n <= N && 0 <= m && m <= M) {
    flag = true;
    if (0 <= n + 2 && n + 2 <= N && 0 <= m - 2 && m - 2 <= M) {
      if (min[n] * min[n + 1] >= maj[m - 1] * maj[m - 2]) {
        n += 2;
        m -= 2;
      } else
        break;
    } else
      break;
  }
  if (flag) {
    LL ans = 1;
    for (int mm = 0; mm < m; mm++) {
      ans = ans * (maj[mm]);
      ans %= MOD;
    }
    for (int nn = 0; nn < n; nn++) {
      LL temp = min[nn] % MOD;
      temp += MOD;
      ans = (ans * temp) % MOD;
    }
    cout << ans << "\\n";
    return 0;
  }
  if (Z != 0) {
    cout << 0 << "\\n";
    return 0;
  }
  n = max(0, K - M);
  m = K - n;
  if (n % 2 == 0) {
    n++;
    m--;
  }
  for (int n = 0; n < N; n++)
    min[n] = -min[n];
  sort(min.begin(), min.end(), [](LL a, LL b) { return a < b; });
  while (0 <= n && n <= N && 0 <= m && m <= M) {
    if (0 <= n + 2 && n + 2 <= N && 0 <= m - 2 && m - 2 <= M) {
      if (min[n] * min[n + 1] <= maj[m - 1] * maj[m - 2]) {
        n += 2;
        m -= 2;
      } else {
        break;
      }
    } else
      break;
  }
  LL ans = 1;
  for (int mm = 0; mm < m; mm++) {
    ans = (ans * maj[mm]) % MOD;
  }
  for (int nn = 0; nn < n; nn++) {
    ans = (ans * min[nn]) % MOD;
  }
  ans *= -1;
  ans %= MOD;
  ans += MOD;
  cout << ans << "\\n";
  return 0;
}