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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
const ll Nmax = 1000000007;

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(void) {
  int N, K;
  cin >> N >> K;
  ll Neg[N];
  ll Pos[N];
  int Nn, Np;
  Nn = 0;
  Np = 0;
  ll tmp;
  rep(i, N) {
    cin >> tmp;
    if (tmp >= 0) {
      Pos[Np] = tmp;
      Np++;
    } else {
      Neg[Nn] = tmp;
      Nn++;
    }
  }

  tmp = 1;
  if (N == K) {
    rep(i, Nn) tmp = (tmp * Neg[i]) % Nmax;
    rep(i, Np) tmp = (tmp * Pos[i]) % Nmax;
    cout << (Nmax + tmp) % Nmax << endl;
    return 0;
  }

  tmp = 1;
  if ((Np == 0 && K % 2 == 1)) {
    sort(Neg, Neg + Nn, greater<>());
    rep(i, K) tmp = (tmp * Neg[i]) % Nmax;
    cout << (Nmax + tmp) % Nmax << endl;
    return 0;
  }

  /*
  sort(Neg, Neg+Nn);
  sort(Pos, Pos+Np, greater<>());

  ll ans;
  vector<ll> pairs;
  for (int i = 0; i <= Nn-2; i+=2) {
          pairs.push_back(Neg[i]*Neg[i+1]);
  }
  if (K%2 == 0){
          ans = 1;
          for (int i = 0; i <= Np-2; i+=2) {
                  pairs.push_back(Pos[i]*Pos[i+1]);
          }
  }
  else {
          ans = Pos[0];
          for (int i = 1; i <= Np-2; i+=2) {
                  pairs.push_back(Pos[i]*Pos[i+1]);
          }
  }

  sort(pairs.rbegin(), pairs.rend());
  rep(i,K/2) {
          ans *= pairs[i];
          ans %= Nmax;
  }
  */
  mint ans = 1;
  vector<ll> s(Pos, Pos + Np);
  vector<ll> t(Neg, Neg + Nn);
  sort(s.begin(), s.end());
  sort(t.rbegin(), t.rend());
  if (K % 2 == 1) {
    ans *= s.back();
    s.pop_back();
  }
  vector<ll> p;
  while (s.size() >= 2) {
    ll x = s.back();
    s.pop_back();
    x *= s.back();
    s.pop_back();
    p.push_back(x);
  }
  while (t.size() >= 2) {
    ll x = t.back();
    t.pop_back();
    x *= t.back();
    t.pop_back();
    p.push_back(x);
  }
  sort(p.rbegin(), p.rend());
  rep(i, K / 2) { ans *= p[i]; }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
const ll Nmax = 1000000007;

int main(void) {
  int N, K;
  cin >> N >> K;
  ll Neg[N];
  ll Pos[N];
  int Nn, Np;
  Nn = 0;
  Np = 0;
  ll tmp;
  rep(i, N) {
    cin >> tmp;
    if (tmp >= 0) {
      Pos[Np] = tmp;
      Np++;
    } else {
      Neg[Nn] = tmp;
      Nn++;
    }
  }

  tmp = 1;
  if (N == K) {
    rep(i, Nn) tmp = (tmp * Neg[i]) % Nmax;
    rep(i, Np) tmp = (tmp * Pos[i]) % Nmax;
    cout << (Nmax + tmp) % Nmax << endl;
    return 0;
  }

  tmp = 1;
  if ((Np == 0 && K % 2 == 1)) {
    sort(Neg, Neg + Nn, greater<>());
    rep(i, K) tmp = tmp * Neg[i];
    cout << (Nmax + tmp) % Nmax << endl;
    return 0;
  }

  sort(Neg, Neg + Nn);
  sort(Pos, Pos + Np, greater<>());

  tmp = 1;
  ll Pall = 1;
  rep(i, K) Pall *= Pos[i];
  ll maxp = Pall;

  ll pairNeg[Nn / 2];
  for (int i = 0; i <= Nn - 2; i += 2) {
    pairNeg[i] = Neg[i] * Neg[i + 1];
  }
  for (int i = 1; i <= K - 1; i += 2) {
    if ((i - 1) / 2 > Nn / 2 - 1)
      break;
    Pall /= Pos[K - i] * Pos[K - i - 1];
    tmp = Pall * pairNeg[(i - 1) / 2];
    maxp = max(maxp, tmp);
  }
  cout << maxp % Nmax << endl;
  return 0;
}