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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
using ll = long long;
using Graph = vector<vector<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 N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A.at(i);

  vector<int> P, M;
  rep(i, N) {
    if (A.at(i) >= 0)
      P.push_back(A.at(i));
    else
      M.push_back(A.at(i));
  }

  int Psize = P.size(), Msize = M.size();
  bool ok = false; // 積が正
  if (Psize > 0) {
    if (N == K)
      ok = (Msize % 2 == 0);
    else
      ok = true;
  } else {
    ok = (K % 2 == 0);
  }

  mint res = 1;

  // 積が負
  if (!ok) {
    sort(A.begin(), A.end(), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, K) res *= A.at(i);
  } else {
    sort(P.begin(), P.end());
    sort(M.rbegin(), M.rend());
    if (K % 2 == 1) {
      res *= P.back();
      P.pop_back();
    }
    vector<ll> tmp;
    while (P.size() >= 2) {
      ll x = P.back();
      P.pop_back();
      x *= P.back();
      P.pop_back();
      tmp.push_back(x);
    }

    while (M.size() >= 2) {
      ll x = M.back();
      M.pop_back();
      x *= M.back();
      M.pop_back();
      tmp.push_back(x);
    }
    sort(tmp.rbegin(), tmp.rend());
    rep(i, K / 2) res *= tmp.at(i);
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using Graph = vector<vector<int>>;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  vector<ll> P, M, Z;
  rep(i, N) {
    if (A.at(i) > 0)
      P.push_back(A.at(i));
    if (A.at(i) < 0)
      M.push_back(A.at(i));
    if (A.at(i) == 0)
      Z.push_back(A.at(i));
  }
  sort(P.begin(), P.end());
  reverse(P.begin(), P.end());
  sort(M.begin(), M.end());

  int cK = K;
  ll res = 1;
  int p = 0, m = 0;
  int sp = P.size(), sm = M.size();
  if (sp == 0 && K % 2 == 1) {
    // res is minus
    reverse(M.begin(), M.end());
    rep(i, K) {
      res *= M.at(i);
      res %= MOD;
    }
    cout << (res + MOD) % MOD << endl;
    return 0;
  }

  while (K) {
    if (m >= sm - 1 || (p == sp - 1 && K % 2 == 1) || K == 1) {
      res *= P.at(p);
      p++;
      K--;
    } else {
      if (P.at(p) * P.at(p + 1) < M.at(m) * M.at(m + 1)) {
        res *= (M.at(m) * M.at(m + 1));
        m += 2;
        K -= 2;
      } else {
        res *= P.at(p);
        p += 1;
        K -= 1;
      }
    }
    res %= MOD;
  }
  cout << res << endl;
}