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

#include <bits/stdc++.h>
using namespace std;
#define SORT(A) std::sort(A.begin(), A.end())
#define RSORT(A) std::sort(A.begin(), A.end(), std::greater<>())
#define FR(A, e) for (auto &e : A)
#define SHOW(A)                                                                \\
  for (auto &e : A) {                                                          \\
    cout << e << " ";                                                          \\
  };                                                                           \\
  cout << endl;
#define within(x, _left, _right) (_left <= x && x <= _right)
#define rwithin(x, _l, _r) (_l < x && x < _r)
#define ALL(A) A.begin(), A.end()
#define RALL(A) (A).rbegin(), (A).rend()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define sz(x) ((int)x.size())
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define bitcnt(x) (__builtin_popcountll(x))
#define fi first
#define se second
#define bg(A) A.begin()
#define ed(A) A.end()
#define rbg(A) A.rbegin()
#define red(A) A.rend()
using LL = long long;
using db = double;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VL = vector<LL>;
using VVL = vector<VL>;
const LL MOD = 1e9 + 7;
const LL LMAX = 0x7fffffffffffffff;
const LL LMIN = 0x8000000000000000;
typedef std::pair<int, int> PII;
typedef std::pair<LL, LL> PLL;
typedef std::pair<int, char> PIC;
// int dx[4] = {1, -1, 0, 0}; // 冯诺依曼型邻居
// int dy[4] = {0, 0, 1, -1};
// int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; // 摩尔型邻居
// int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// #include<iomanip> cout << fixed <<  setprecision(k) 输出k位精度

int N, K;
VL pos, neg, zero;

void mul(LL &a, LL b) { a = a * b % MOD; }

int main() {
  std::cin >> N >> K;

  pos.clear(), neg.clear(), zero.clear();

  int t;
  for (int i = 0; i < N; ++i) {
    cin >> t;
    if (t > 0)
      pos.emplace_back(t);
    else if (t == 0)
      zero.emplace_back(t);
    else
      neg.emplace_back(t);
  }

  RSORT(pos);
  SORT(neg);

  int a = sz(pos), b = sz(neg), c = sz(zero);
  LL res = 1;

  if (a + b < K) {
    cout << 0 << endl;
    return 0;
  }

  if (a + b == K) {
    if ((b & 1) && c != 0) {
      cout << 0 << endl;
    } else {
      for (auto &x : pos)
        res = (res * 1LL * x) % MOD;
      for (auto &x : neg)
        res = ((res * 1LL * x) + MOD) % MOD;
      cout << (res + MOD) % MOD << endl;
    }
    return 0;
  }

  int pp = 0, pn = 0;
  if ((K & 1) && a != 0) {
    res = (1LL * res * pos[pp++]) % MOD;
    --K;
  } else if ((K & 1) && a == 0 && c != 0) {
    cout << 0 << endl;
    return 0;
  } else if ((K & 1) && a == 0 && c == 0) {
    for (int i = 0, x = 0; i < K; ++i)
      x = neg[b - 1 - i], res = ((res * 1LL * x) + MOD) % MOD;
    cout << (res + MOD) % MOD << endl;
    return 0;
  }

  while (K) {
    LL pmax = 0, nmax = 0;
    // if (pp + 1 < a) pmax = pos[pp] * pos[pp + 1] % MOD;
    // if (pn + 1 < b) nmax = neg[pn] * neg[pn + 1] % MOD;
    LL v1 = (a - pp >= 2 ? pos[pp] * pos[pp + 1] : 0);
    LL v2 = (b - pn >= 2 ? neg[pn] * neg[pn + 1] : 0);
    if (v1 >= v2) {
      // res = (1LL * res * pmax) % MOD;
      mul(res, v1 % MOD);
      pp += 2;
    } else {
      // res = (1LL * res * nmax) % MOD;
      mul(res, v2 % MOD);
      pn += 2;
    }
    K -= 2;
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SORT(A) std::sort(A.begin(), A.end())
#define RSORT(A) std::sort(A.begin(), A.end(), std::greater<>())
#define FR(A, e) for (auto &e : A)
#define SHOW(A)                                                                \\
  for (auto &e : A) {                                                          \\
    cout << e << " ";                                                          \\
  };                                                                           \\
  cout << endl;
#define within(x, _left, _right) (_left <= x && x <= _right)
#define rwithin(x, _l, _r) (_l < x && x < _r)
#define ALL(A) A.begin(), A.end()
#define RALL(A) (A).rbegin(), (A).rend()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define sz(x) ((int)x.size())
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define bitcnt(x) (__builtin_popcountll(x))
#define fi first
#define se second
#define bg(A) A.begin()
#define ed(A) A.end()
#define rbg(A) A.rbegin()
#define red(A) A.rend()
using LL = long long;
using db = double;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VL = vector<LL>;
using VVL = vector<VL>;
const LL MOD = 1e9 + 7;
const LL LMAX = 0x7fffffffffffffff;
const LL LMIN = 0x8000000000000000;
typedef std::pair<int, int> PII;
typedef std::pair<LL, LL> PLL;
typedef std::pair<int, char> PIC;
// int dx[4] = {1, -1, 0, 0}; // 冯诺依曼型邻居
// int dy[4] = {0, 0, 1, -1};
// int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; // 摩尔型邻居
// int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// #include<iomanip> cout << fixed <<  setprecision(k) 输出k位精度

int N, K;
VI pos, neg, zero;

void mul(LL &a, LL b) { a = a * b % MOD; }

int main() {
  std::cin >> N >> K;

  pos.clear(), neg.clear(), zero.clear();

  int t;
  for (int i = 0; i < N; ++i) {
    cin >> t;
    if (t > 0)
      pos.emplace_back(t);
    else if (t == 0)
      zero.emplace_back(t);
    else
      neg.emplace_back(t);
  }

  RSORT(pos);
  SORT(neg);

  int a = sz(pos), b = sz(neg), c = sz(zero);
  LL res = 1;

  if (a + b < K) {
    cout << 0 << endl;
    return 0;
  }

  if (a + b == K) {
    if ((b & 1) && c != 0) {
      cout << 0 << endl;
    } else {
      for (auto &x : pos)
        res = (res * 1LL * x) % MOD;
      for (auto &x : neg)
        res = ((res * 1LL * x) + MOD) % MOD;
      cout << (res + MOD) % MOD << endl;
    }
    return 0;
  }

  int pp = 0, pn = 0;
  if ((K & 1) && a != 0) {
    res = (1LL * res * pos[pp++]) % MOD;
    --K;
  } else if ((K & 1) && a != 0 && c != 0) {
    cout << 0 << endl;
    return 0;
  } else if ((K & 1) && a == 0 && c == 0) {
    for (int i = 0, x = 0; i < K; ++i)
      x = neg[b - 1 - i], res = ((res * 1LL * x) + MOD) % MOD;
    cout << (res + MOD) % MOD << endl;
    return 0;
  }

  while (K) {
    LL pmax = 0, nmax = 0;
    if (pp + 1 < a)
      pmax = (1LL * pos[pp] * pos[pp + 1]) % MOD;
    if (pn + 1 < b)
      nmax = (1LL * neg[pn] * neg[pn + 1]) % MOD;

    if (pmax >= nmax) {
      // res = (1LL * res * pmax) % MOD;
      mul(res, pmax % MOD);
      pp += 2;
    } else {
      // res = (1LL * res * nmax) % MOD;
      mul(res, nmax % MOD);
      pn += 2;
    }
    K -= 2;
  }

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

  return 0;
}