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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void coY() { cout << "Yes" << endl; }
void coN() { cout << "No" << endl; }

// Write From this Line

// now += mint(2).pow(s) - 1; こんな感じで、2のs乗を求められrう
// 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;
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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// const ll mod = 1e9+7;
// const ll mod = 998244353;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> s, t;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      t.push_back(a[i]);
    else
      s.push_back(a[i]);
  }

  int S = s.size();
  int T = t.size();
  bool ok = false;
  if (S > 0) {
    if (n == k)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (k % 2 == 0);
  }

  mint ans = 1;
  if (!ok) {
    // 答えが負になる場合
    sort(a.begin(), a.end(), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, k) ans *= a[i];
  } else {
    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;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void coY() { cout << "Yes" << endl; }
void coN() { cout << "No" << endl; }

// Write From this Line

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll ans = 0;
  SORT(a);
  int sei = 0, zero = 0, minus = 0;
  rep(i, n) {
    if (a[i] > 0)
      sei++;
    if (a[i] == 0)
      zero++;
    else
      minus++;
  }
  vector<ll> SEI(0), MINUS(0);
  rep(i, n) {
    if (a[i] > 0) {
      SEI.push_back(a[i]);
    } else if (a[i] < 0) {
      MINUS.push_back(abs(a[i]));
    }
  }
  // 絶対値で持つと気持がいい
  SORT(SEI);
  SORT(MINUS);
  int p = SEI.size(), q = MINUS.size();
  vector<ll> Rs(p + 1, 1), rs(p + 1, 1), Rm(q + 1, 1), rm(q + 1, 1);
  rep(i, p) {
    Rs[i + 1] = (Rs[i] * SEI[p - 1 - i]) % mod;
    rs[i + 1] = (rs[i] * SEI[i]) % mod;
  }
  rep(i, q) {
    Rm[i + 1] = (Rm[i] * MINUS[q - 1 - i]) % mod;
    rm[i + 1] = (rm[i] * MINUS[i]) % mod;
  }

  ll ans1 = INT_MIN;
  ll ans2 = 0;
  ll ans3 = INT_MAX;

  rep(i, k + 1) {
    // 正を i 個取る
    int hu = k - i;
    if (i > sei)
      continue;
    // 負を hu 個とる
    if (hu > minus)
      continue;
    if (hu % 2 == 1) {
      // 正も負も小さい方から取った方が良い。
      chmin(ans3, (rs[i] * rm[hu]) % mod);
    } else {
      // 正も負も大きい方から取る
      chmax(ans1, (Rs[i] * rm[hu]) % mod);
    }
  }
  if (zero) {
    // ans3 は使わない
    ans = max(ans2, ans1);
  } else {
    if (ans1 > 0) {
      ans = ans1;
    } else {
      cout << mod - ans3 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}