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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// 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;
// const int mod = 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; }

int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> a_plus, a_minus;
  rep(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      a_plus.push_back(a[i]);
    if (a[i] <= 0)
      a_minus.push_back(-a[i]);
  }
  sort(a_plus.rbegin(), a_plus.rend());
  sort(a_minus.rbegin(), a_minus.rend());
  mint ans = 1;
  if (n == k) {
    rep(i, n) ans *= a[i];
    cout << ans << endl;
    return 0;
  } else if (a_plus.size() == 0 && k % 2 == 1) {
    rep(i, k) ans *= -a_minus[a_minus.size() - i - 1];
  } else {
    int rem = k;
    int plus_idx = 0;
    int minus_idx = 0;
    if (k % 2 == 1) {
      rem--;
      plus_idx++;
      ans *= a_plus[0];
    }
    while (rem > 0) {
      if (plus_idx + 1 > a_plus.size()) {
        ans *= a_minus[minus_idx];
        ans *= a_minus[minus_idx + 1];
        minus_idx += 2;
      } else if (minus_idx + 1 > a_minus.size()) {
        ans *= a_plus[plus_idx];
        ans *= a_plus[plus_idx + 1];
        plus_idx += 2;
      } else {
        if (a_plus[plus_idx] * a_plus[plus_idx + 1] >
            a_minus[minus_idx] * a_minus[minus_idx + 1]) {
          ans *= a_plus[plus_idx];
          ans *= a_plus[plus_idx + 1];
          plus_idx += 2;
        } else {
          ans *= a_minus[minus_idx];
          ans *= a_minus[minus_idx + 1];
          minus_idx += 2;
        }
      }
      rem -= 2;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// 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;
// const int mod = 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; }

int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> a_plus, a_minus;
  rep(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      a_plus.push_back(a[i]);
    if (a[i] <= 0)
      a_minus.push_back(-a[i]);
  }
  sort(a_plus.rbegin(), a_plus.rend());
  sort(a_minus.rbegin(), a_minus.rend());
  mint ans = 1;
  if (n == k) {
    rep(i, n) ans *= a[i];
    cout << ans << endl;
    return 0;
  } else if (a_plus.size() == 0 || k % 2 == 1) {
    rep(i, k) ans *= -a_minus[a_minus.size() - i - 1];
  } else {
    int rem = k;
    int plus_idx = 0;
    int minus_idx = 0;
    if (k % 2 == 1) {
      rem -= 1;
      plus_idx++;
      ans *= a_plus[0];
    }
    while (rem > 0) {
      if (plus_idx + 1 > a_plus.size()) {
        ans *= a_minus[minus_idx];
        ans *= a_minus[minus_idx + 1];
        minus_idx += 2;
      } else if (minus_idx + 1 > a_minus.size()) {
        ans *= a_plus[plus_idx];
        ans *= a_plus[plus_idx + 1];
        plus_idx += 2;
      } else {
        if (a_plus[plus_idx] * a_plus[plus_idx + 1] >
            a_minus[minus_idx] * a_minus[minus_idx + 1]) {
          ans *= a_plus[plus_idx];
          ans *= a_plus[plus_idx + 1];
          plus_idx += 2;
        } else {
          ans *= a_minus[minus_idx];
          ans *= a_minus[minus_idx + 1];
          minus_idx += 2;
        }
      }
      rem -= 2;
    }
  }
  cout << ans << endl;
}