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

#include <bits/stdc++.h>

using namespace std;
#define rep(j, n) for (int j = 0; j < (n); j++)

using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;
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, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main(int, char **) {
  int n, k;
  cin >> n >> k;

  vector<int> pos, neg;
  rep(i, n) {
    int tmp;
    cin >> tmp;
    if (tmp > 0)
      pos.push_back(tmp);
    if (tmp < 0)
      neg.push_back(-tmp);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  int ns = neg.size();
  int ps = pos.size();

  mint ans = 1;

  bool canpos = false;
  if (k <= ns / 2 * 2) {
    if (k % 2 == 0 || ps > 0)
      canpos = true;
  } else {
    if (ps >= k - ns / 2 * 2)
      canpos = true;
  }

  if (!canpos) {
    if (ns == n && k % 2 == 1) {
      rep(i, k) ans *= neg[i];
      cout << -ans << endl;
    } else if (k == n && ns % 2 == 1) {
      rep(i, ns) ans *= neg[i];
      rep(i, ps) ans *= pos[i];
      cout << -ans << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }

  if (k % 2 == 1) {
    ans *= pos.back();
    pos.pop_back();
    k--;
  }

  vector<ll> dbl;
  while (pos.size() > 1) {
    ll tmp = pos.back();
    pos.pop_back();
    tmp *= pos.back();
    pos.pop_back();
    dbl.push_back(tmp);
  }
  while (neg.size() > 1) {
    ll tmp = neg.back();
    neg.pop_back();
    tmp *= neg.back();
    neg.pop_back();
    dbl.push_back(tmp);
  }
  sort(dbl.rbegin(), dbl.rend());
  rep(i, k / 2) ans *= dbl[i];

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(j, n) for (int j = 0; j < (n); j++)

using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;
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, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main(int, char **) {
  int n, k;
  cin >> n >> k;

  vector<int> neg, pos;
  bool zeros = false;
  rep(i, n) {
    int a;
    cin >> a;
    if (a > 0)
      pos.push_back(a);
    if (a < 0)
      neg.push_back(-a);
    if (a == 0)
      zeros = true;
  }

  sort(pos.begin(), pos.end(), greater<int>());
  sort(neg.begin(), neg.end(), greater<int>());

  mint ans = 1;
  if (!zeros && pos.size() == 0 && k % 2 == 1) {
    rep(i, k) ans *= neg[n - i - 1];
    cout << -ans << endl;
    return 0;
  }
  if (k == n) {
    if (zeros)
      ans = 0;
    else {
      for (auto p : pos)
        ans *= p;
      for (auto n : neg)
        ans *= -n;
    }
    cout << ans << endl;
    return 0;
  }
  if (pos.size() + neg.size() < k) {
    cout << 0 << endl;
    return 0;
  }

  int posi = 0, negi = 0;
  ll pose, nege;
  while (k >= 2) {
    pose = 0, nege = 0;
    if (posi + 1 < pos.size())
      pose = pos[posi] * pos[posi + 1];
    if (negi + 1 < neg.size())
      nege = neg[negi] * neg[negi + 1];
    if (!pose && !nege)
      break;
    if (pose >= nege) {
      ans *= pose;
      posi += 2;
    } else {
      ans *= nege;
      negi += 2;
    }
    k -= 2;
  }
  if (k)
    ans *= pos[posi];

  cout << ans << endl;

  return 0;
}