Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
// #define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(), (c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) int((a).size())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define PER(i, n) ROF(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int LARGE_INT = 1e9 + 100;
const int INF = 2e9 + 100;
const LL INF_LL = (LL)INF * (LL)INF;
const int MOD = 1e9 + 7;

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

LL modpow(LL a, LL n) {
  LL res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

void Main() {
  int n, k;
  cin >> n >> k;

  VL p, m, z;
  REP(i, n) {
    LL a;
    cin >> a;
    if (a == 0) {
      z.PB(a);
    }
    if (a > 0) {
      p.PB(a);
    }
    if (a < 0) {
      m.PB(a);
    }
  }

  int np = SIZE(p);
  int nm = SIZE(m);
  int nz = SIZE(z);

  if (k > np + nm) {
    std::cout << 0 << endl;
    return;
  }

  LL fugo = 1;
  if (k == np + nm) {
    if (nm % 2 == 1) {
      fugo = -1;
    }
  }
  if (np == 0) {
    if (k % 2 == 1) {
      fugo = -1;
    }
  }

  if (fugo == -1) {
    if (nz != 0) {
      std::cout << 0 << endl;
      return;
    }
  }

  if (fugo == 1) {
    SORT_DESC(p);
    SORT_ASC(m);
    int i = 0;
    int j = 0;
    LL result = 1;

    REP(kk, k) {
      bool f;
      if (kk == k - 1) {
        f = true;
      } else if (j >= nm - 1) {
        f = true;
      } else if (i <= np - 1) {
        if (p[i] > m[j] * m[j + 1]) {
          f = true;
        } else {
          f = false;
        }
      } else if (p[i] * p[i + 1] > m[j] * m[j + 1]) {
        f = true;
      } else {
        f = false;
      }

      if (f) {
        result *= p[i];
        i++;
      } else {
        LL x = m[j] * m[j + 1];
        x %= MOD;
        result *= x;
        j += 2;
        kk++;
      }

      result %= MOD;
    }
    std::cout << result << endl;

  } else {
    SORT_ASC(p);
    SORT_DESC(m);
    int i = 0;
    int j = 0;
    LL result = 1;
    // dump(result)

    REP(kk, k) {
      bool f;
      if (i >= np) {
        f = false;
      } else if (j >= nm) {
        f = true;
      } else if (p[i] < -m[j]) {
        f = true;
      } else {
        f = false;
      }

      if (f) {
        result *= p[i];
        i++;
      } else {
        result *= m[j];
        result %= MOD;
        result += MOD;
        j++;
      }

      result %= MOD;
    }
    std::cout << result << endl;
  }

  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  std::cout << fixed << setprecision(15);
  Main();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
// #define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(), (c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) int((a).size())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define PER(i, n) ROF(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int LARGE_INT = 1e9 + 100;
const int INF = 2e9 + 100;
const LL INF_LL = (LL)INF * (LL)INF;
const int MOD = 1e9 + 7;

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

LL modpow(LL a, LL n) {
  LL res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

void Main() {
  int n, k;
  cin >> n >> k;

  VL p, m, z;
  REP(i, n) {
    LL a;
    cin >> a;
    if (a == 0) {
      z.PB(a);
    }
    if (a > 0) {
      p.PB(a);
    }
    if (a < 0) {
      m.PB(a);
    }
  }

  int np = SIZE(p);
  int nm = SIZE(m);
  int nz = SIZE(z);

  if (k > np + nm) {
    std::cout << 0 << endl;
    return;
  }

  LL fugo = 1;
  if (k == np + nm) {
    if (nm % 2 == 1) {
      fugo = -1;
    }
  }
  if (np == 0) {
    if (k % 2 == 1) {
      fugo = -1;
    }
  }

  if (fugo == -1) {
    if (nz != 0) {
      std::cout << 0 << endl;
      return;
    }
  }

  if (fugo == 1) {
    SORT_DESC(p);
    SORT_ASC(m);
    int i = 0;
    int j = 0;
    LL result = 1;

    REP(kk, k) {
      bool f;
      if (kk == k - 1) {
        f = true;
      } else if (j >= nm - 1) {
        f = true;
      } else if (i >= np) {
        f = false;
      } else if (i >= np - 1) {
        if (p[i] > m[j] * m[j + 1]) {
          f = true;
        } else {
          f = false;
        }
      } else if (p[i] * p[i + 1] > m[j] * m[j + 1]) {
        f = true;
      } else {
        f = false;
      }

      if (f) {
        result *= p[i];
        i++;
      } else {
        LL x = m[j] * m[j + 1];
        x %= MOD;
        result *= x;
        j += 2;
        kk++;
      }

      result %= MOD;
    }
    std::cout << result << endl;

  } else {
    SORT_ASC(p);
    SORT_DESC(m);
    int i = 0;
    int j = 0;
    LL result = 1;
    // dump(result)

    REP(kk, k) {
      bool f;
      if (i >= np) {
        f = false;
      } else if (j >= nm) {
        f = true;
      } else if (p[i] < -m[j]) {
        f = true;
      } else {
        f = false;
      }

      if (f) {
        result *= p[i];
        i++;
      } else {
        result *= m[j];
        result %= MOD;
        result += MOD;
        j++;
      }

      result %= MOD;
    }
    std::cout << result << endl;
  }

  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  std::cout << fixed << setprecision(15);
  Main();
  return 0;
}