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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define all(x) (x).begin(), (x).end()
// #define repitr(itr, st) for(auto itr = st.begin(); itr != st.end(); ++itr)
#define P_B push_back
#define PO_B pop_back
#define E_B emplace_back
#define M_P make_pair
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << endl
// #define print(x) printf("%d\\n", x)

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
// int dx[4] = {1, 0, -1, 0};
// int dy[4] = {0, 1, 0, -1};
// const ll LINF = (ll)1e18 - 1;
// const int INF = 1e9 - 1;
// int:±2*10^9くらい　ll:±9*10^18くらい

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007; // 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

using VM = vector<mint>;
using VVM = vector<vector<mint>>;

void Main() {
  ll N, K, zeroc = 0;
  mint ans = 1;
  cin >> N >> K;

  if (N == K) {
    rep(i, N) {
      int a;
      cin >> a;
      ans *= a;
    }
    cout << ans << endl;
    return;
  }

  VL pos, neg;
  rep(i, N) {
    int a;
    cin >> a;
    if (a > 0)
      pos.P_B(a);
    if (a < 0)
      neg.P_B(a);
    if (a == 0)
      zeroc++;
  }

  sort(all(pos));
  sort(all(neg));
  reverse(all(neg));

  if (pos.size() == 0) {
    if (K % 2 == 0) {
      reverse(all(neg));
      if (K > neg.size()) {
        cout << 0 << endl;
        return;
      }
      rep(i, K) { ans *= neg[i]; }
      cout << ans << endl;
      return;
    } else {
      if (zeroc == 0) {
        rep(i, K) { ans *= neg[i]; }
        cout << ans << endl;
        return;
      } else {
        cout << 0 << endl;
        return;
      }
    }
  }

  if (K % 2 == 1) {
    ans *= pos.back();
    pos.PO_B();
    K--;
  }
  K /= 2;
  rep(i, K) {
    ll POS = 1, NEG = 1;
    if (pos.size() >= 2)
      POS *= pos.back() * pos[pos.size() - 2];
    else
      POS = 0;
    if (neg.size() >= 2)
      NEG *= neg.back() * neg[neg.size() - 2];
    else
      NEG = 0;

    if (POS >= NEG) {
      ans *= POS;
      if (POS == 0) {
        cout << 0 << endl;
        return;
      }
      pos.PO_B();
      pos.PO_B();
    } else {
      ans *= NEG;
      neg.PO_B();
      neg.PO_B();
    }
  }

  cout << ans << endl;
  return;
}

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

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define all(x) (x).begin(), (x).end()
// #define repitr(itr, st) for(auto itr = st.begin(); itr != st.end(); ++itr)
#define P_B push_back
#define PO_B pop_back
#define E_B emplace_back
#define M_P make_pair
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << endl
// #define print(x) printf("%d\\n", x)

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
// int dx[4] = {1, 0, -1, 0};
// int dy[4] = {0, 1, 0, -1};
// const ll LINF = (ll)1e18 - 1;
// const int INF = 1e9 - 1;
// int:±2*10^9くらい　ll:±9*10^18くらい

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007; // 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

using VM = vector<mint>;
using VVM = vector<vector<mint>>;

void Main() {
  ll N, K, zeroc = 0;
  mint ans = 1;
  cin >> N >> K;
  VL pos, neg;
  rep(i, N) {
    int a;
    cin >> a;
    if (a > 0)
      pos.P_B(a);
    if (a < 0)
      neg.P_B(a);
    if (a == 0)
      zeroc++;
  }

  sort(all(pos));
  sort(all(neg));
  reverse(all(neg));

  if (pos.size() == 0) {
    if (zeroc != 0 && K % 2 == 1) {
      cout << 0 << endl;
      return;
    } else {
      if (K % 2 == 0)
        reverse(all(neg));
      rep(i, K) { ans *= neg[i]; }
      cout << ans << endl;
      return;
    }
  }

  if (K % 2 == 1) {
    ans *= pos.back();
    pos.PO_B();
    K--;
  }
  K /= 2;
  rep(i, K) {
    ll POS = 1, NEG = 1;
    if (pos.size() >= 2)
      POS *= pos.back() * pos[pos.size() - 2];
    else
      POS = 0;
    if (neg.size() >= 2)
      NEG *= neg.back() * neg[neg.size() - 2];
    else
      NEG = 0;

    if (POS >= NEG) {
      ans *= POS;
      if (POS == 0)
        continue;
      pos.PO_B();
      pos.PO_B();
    } else {
      ans *= NEG;
      neg.PO_B();
      neg.PO_B();
    }
  }

  cout << ans << endl;
  return;
}

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

  return 0;
}