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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
const int mod = 1000000007;
typedef long long ll;
class mint {
  long long x;

public:
  mint(long long 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;
  }

  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> s(2e5, 0), f(2e5, 0);
  vector<ll> A;
  ll cntz = 0, cntpl = 0, cntmi = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(abs(a));
    if (a == 0)
      cntz++;
    if (a > 0) {
      s.push_back(a);
      cntpl++;
    }
    if (a < 0) {
      f.push_back(abs(a));
      cntmi++;
    }
  }
  sort(all(A));
  sort(all(s), greater<>());
  sort(all(f), greater<>());
  mint ans = 1;
  if (N == K) {
    rep(i, K) ans *= A[i];
    if (cntmi & 1)
      ans = -ans;
    cout << ans << endl;
    return 0;
  }
  // K < N;
  ll idx[] = {0, 0};
  if (K & 1) {
    if (cntpl > 0)
      ans *= s[idx[0]++];
    else {
      rep(i, K) ans *= A[i];
      ans = -ans;
      cout << ans << endl;
      return 0;
    }
  }
  // 残りの偶数個を決めていく
  ll len = K / 2;
  while (len--) {
    ll tmp[2];
    tmp[0] = s[idx[0]] * s[idx[0] + 1];
    tmp[1] = f[idx[1]] * f[idx[1] + 1];
    if (tmp[0] < tmp[1]) {
      ans *= tmp[1];
      idx[1] += 2;
    } else {
      ans *= tmp[0];
      idx[0] += 2;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;
int N, K;
ll A[300001];

bool comp(const ll &l, const ll &r) { return abs(l) > abs(r); }

void solve() {
  ll ans = 1;

  // Aiが全て負かつKが奇数のとき
  bool is_all_neg = true;
  if (K % 2 == 0)
    is_all_neg = false;
  else {
    for (int i = 0; i < N; i++) {
      if (A[i] >= 0)
        is_all_neg = false;
    }
  }
  if (is_all_neg) {
    // 降順
    sort(A, A + N, greater<int>());
    for (int i = 0; i < K; i++) {
      ans *= A[i];
    }
    printf("%lld\\n", (ans + MOD) % MOD);
    return;
  }

  // 絶対値の降順
  sort(A, A + N, comp);

  ll last_neg = -1; // 選んだ中での絶対値最小の負数
  ll last_pos = 0;  // 選んだ中での絶対値最小の正数
  for (int i = 0; i < K; i++) {
    if (A[i] < 0)
      last_neg = A[i];
    else
      last_pos = A[i];
    ans = (ans * A[i]) % MOD;
  }

  // 負数の場合
  if (ans < 0) {
    ll new_pos = -1, new_neg = 0;
    for (int i = K; i < N; i++) {
      if (A[i] < 0 && new_neg == 0)
        new_neg = A[i];
      else if (A[i] >= 0 && new_pos == -1)
        new_pos = A[i];
    }

    ll ans_a = ans;
    ll ans_b = ans;

    // 絶対値最小の負数を正数と入れ替える
    if (new_pos >= 0 && last_neg < 0) {
      ans_a /= last_neg;
      ans_a *= new_pos;
    }
    // 絶対値最小の正数を負数と入れ替える
    if (new_neg < 0 && last_pos >= 0) {
      ans_b /= last_pos;
      ans_b *= new_neg;
    }

    ll ans_c;
    ans_c = max(ans_a, ans_b);
    ans = ans_c;
  }

  printf("%lld\\n", (ans + MOD) % MOD);
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  solve();
}