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

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];
struct mll {
  ll val;
  mll(ll val = 0) : val((val % MOD + MOD) % MOD) {}

  mll pow(ll t) const {
    if (!t)
      return 1;
    mll a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mll inv() const { return pow(MOD - 2); }

  mll operator-() const { return mll(-val); }
  mll &operator+=(const mll a) {
    if ((val += a.val) >= MOD)
      val -= MOD;
    return *this;
  }
  mll &operator-=(const mll a) {
    if ((val += MOD - a.val) >= MOD)
      val -= MOD;
    return *this;
  }
  mll &operator*=(const mll a) {
    (val *= a.val % MOD + MOD) %= MOD;
    return *this;
  }
  mll &operator/=(const mll a) { return (*this) *= a.inv(); }

  mll operator+(const mll a) const {
    mll res(*this);
    return res += a;
  }
  mll operator-(const mll a) const {
    mll res(*this);
    return res -= a;
  }
  mll operator*(const mll a) const {
    mll res(*this);
    return res *= a;
  }
  mll operator/(const mll a) const {
    mll res(*this);
    return res /= a;
  }

  mll &operator++() {
    val++;
    return *this;
  }
  mll operator++(int) {
    mll res(*this);
    val++;
    return res;
  }
  mll &operator--() {
    val--;
    return *this;
  }
  mll operator--(int) {
    mll res(*this);
    val--;
    return res;
  }

  bool operator<(const mll a) const { return val < a.val; }
  bool operator<=(const mll a) const { return val <= a.val; }
  bool operator>(const mll a) const { return val > a.val; }
  bool operator>=(const mll a) const { return val >= a.val; }
  bool operator==(const mll a) const { return val == a.val; }

  friend istream &operator>>(istream &input, mll &a) {
    input >> a.val;
    return input;
  }

  friend ostream &operator<<(ostream &output, mll &a) {
    output << a.val;
    return output;
    ;
  }
};

int main(void) {
  ll i, j, k;

  ll N, K;
  cin >> N >> K;
  vector<ll> a(N);

  for (i = 0; i < N; i++)
    cin >> a[i];

  sort(a.rbegin(), a.rend(),
       [](const ll &a, const ll &b) { return abs(a) < abs(b); });

  mll ans = 1;
  ll lst_pos = -INF, lst_neg = -INF; // K個中の絶対値最小
  ll pc = 0, nc = 0;
  for (i = 0; i < K; i++) {
    ans *= a[i];
    if (a[i] > 0) {
      lst_pos = a[i];
      pc++;
    } else if (a[i] < 0) {
      lst_neg = a[i];
      nc++;
    }
  }

  if (nc % 2 == 0 || ans == 0) { // 積>0 || 積==0
    pt(ans);
    return 0;
  }

  ll fst_pos = -INF, fst_neg = -INF; // K個から漏れた中の絶対数最大
  for (i = N - 1; i >= K; i--) {
    if (lst_neg != -INF && a[i] > 0)
      fst_pos = a[i];
    if (lst_pos != -INF && a[i] < 0)
      fst_neg = a[i];
  }

  if (fst_pos == -INF) {
    if (fst_neg ==
        -INF) { // 積<=0が確定 -> 絶対値をなるべく小さくする方向に切り替え
      ans = 1;
      sort(a.begin(), a.end(),
           [](const ll &a, const ll &b) { return abs(a) < abs(b); });
      for (i = 0; i < K; i++)
        ans *= a[i];
    } else {
      ans = ans / lst_pos * fst_neg;
    }
  } else {
    if (fst_neg == -INF) {
      ans = ans / lst_neg * fst_pos;
    } else {
      if (fst_pos * lst_pos > fst_neg * lst_neg)
        ans = ans / lst_neg * fst_pos;
      else
        ans = ans / lst_pos * fst_neg;
    }
  }

  pt(ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];
struct mll {
  ll val;
  mll(ll val = 0) : val((val % MOD + MOD) % MOD) {}

  mll pow(ll t) const {
    if (!t)
      return 1;
    mll a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mll inv() const { return pow(MOD - 2); }

  mll operator-() const { return mll(-val); }
  mll &operator+=(const mll a) {
    if ((val += a.val) >= MOD)
      val -= MOD;
    return *this;
  }
  mll &operator-=(const mll a) {
    if ((val += MOD - a.val) >= MOD)
      val -= MOD;
    return *this;
  }
  mll &operator*=(const mll a) {
    (val *= a.val % MOD + MOD) %= MOD;
    return *this;
  }
  mll &operator/=(const mll a) { return (*this) *= a.inv(); }

  mll operator+(const mll a) const {
    mll res(*this);
    return res += a;
  }
  mll operator-(const mll a) const {
    mll res(*this);
    return res -= a;
  }
  mll operator*(const mll a) const {
    mll res(*this);
    return res *= a;
  }
  mll operator/(const mll a) const {
    mll res(*this);
    return res /= a;
  }

  mll &operator++() {
    val++;
    return *this;
  }
  mll operator++(int) {
    mll res(*this);
    val++;
    return res;
  }
  mll &operator--() {
    val--;
    return *this;
  }
  mll operator--(int) {
    mll res(*this);
    val--;
    return res;
  }

  bool operator<(const mll a) const { return val < a.val; }
  bool operator<=(const mll a) const { return val <= a.val; }
  bool operator>(const mll a) const { return val > a.val; }
  bool operator>=(const mll a) const { return val >= a.val; }
  bool operator==(const mll a) const { return val == a.val; }

  friend istream &operator>>(istream &input, mll &a) {
    input >> a.val;
    return input;
  }

  friend ostream &operator<<(ostream &output, mll &a) {
    output << a.val;
    return output;
    ;
  }
};

int main(void) {
  ll i, j, k;

  ll N, K;
  cin >> N >> K;
  ll a[MAX * 2];

  vector<ll> nv, pv;
  for (i = 0; i < N; i++) {
    cin >> a[i];
    if (a[i] > 0)
      pv.push_back(a[i]);
    else if (a[i] < 0)
      nv.push_back(a[i]);
  }
  sort(a, a + N);
  reverse(a, a + N);            // 降順
  sort(pv.rbegin(), pv.rend()); // 降順
  sort(nv.begin(), nv.end());   // 昇順

  ll nc = nv.size();
  ll pc = pv.size();

  mll ans = 1;
  if (pc == 0) {
    for (i = 0; i < K; i++)
      ans *= a[i];
  } else if (pc >= K) {

    if (K & 1) {
      ans *= pv[0];
      pv.erase(pv.begin());
    }

    priority_queue<ll> q;
    for (i = 0; i < K / 2; i++) {
      j = i * 2;
      ans *= pv[j];
      ans *= pv[j + 1];
      q.push(-pv[j] * pv[j + 1]);
    }

    mll t = ans;
    for (i = 0; i < min(nc / 2, K / 2); i++) {
      j = i * 2;
      t *= nv[j];
      t *= nv[j + 1];

      t /= -q.top();
      q.pop();
      q.push(-nv[j] * nv[j + 1]);

      chmax(ans, t);
    }

  } else {
    ll f = (pc % 2 != K % 2);
    if ((pc - f) & 1) {
      ans *= pv[0];
      pv.erase(pv.begin());
    }

    priority_queue<mll> q;
    for (i = 0; i < (pc - f) / 2; i++) {
      j = i * 2;
      ans *= pv[j];
      ans *= pv[j + 1];
      q.push(-pv[j] * pv[j + 1]);
    }

    for (i = 0; i < (K - pc + f) / 2; i++) {
      j = i * 2;
      ans *= nv[j];
      ans *= nv[j + 1];
      q.push(-nv[j] * nv[j + 1]);
    }

    if (nc + pc - f < K) {
      ll b[MAX * 2];
      for (i = 0; i < N; i++)
        b[i] = abs(a[i]);
      sort(b, b + N);
      ans = -1;
      for (i = 0; i < K; i++)
        ans *= b[i];
      pt(ans);
    } else {
      mll t = ans;
      for (i = (K - pc + f) / 2; i < nc / 2; i++) {
        j = i * 2;
        t *= nv[j];
        t *= nv[j + 1];

        t /= -q.top();
        q.pop();
        q.push(-nv[j] * nv[j + 1]);

        chmax(ans, t);
      }
    }
  }

  pt(ans);
}