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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  ll x;
  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;
  }
  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; }
};
typedef vector<mint> vm;
typedef vector<vm> vvm;

int main() {
  int N, K;
  cin >> N >> K;
  vl A(N);
  int pos = 0, neg = 0, zero = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] < 0) {
      neg++;
    } else if (A[i] > 0) {
      pos++;
    } else {
      zero++;
    }
  }
  sort(rng(A), [](ll a, ll b) { return abs(a) > abs(b); });
  if (pos + neg < K) {
    cout << 0 << endl;
    return 0;
  }
  if (pos == 0 && (K & 1)) {
    reverse(rng(A));
    mint ans = 1;
    rep(i, K) { ans *= A[i]; }
    cout << ans.x << endl;
    return 0;
  }
  mint dp = 1;
  bool isneg = false;
  rep(i, K) {
    dp *= abs(A[i]);
    if (A[i] < 0)
      isneg = !isneg;
  }
  if (!isneg) {
    cout << dp.x << endl;
    return 0;
  }
  if (N == K) {
    dp *= -1;
    cout << dp.x << endl;
    return 0;
  }
  ll posm = INF, negm = INF;
  rep(i, K) {
    if (A[i] <= 0)
      continue;
    chmin(posm, abs(A[i]));
  }
  rep(i, K) {
    if (A[i] >= 0)
      continue;
    chmin(negm, abs(A[i]));
  }
  ll p2 = -INF, n2 = -INF;
  srep(i, K, N) {
    if (A[i] < 0)
      continue;
    chmax(p2, abs(A[i]));
    break;
  }
  srep(i, K, N) {
    if (A[i] > 0)
      continue;
    chmax(n2, abs(A[i]));
    break;
  }
  // (1) remove min negative(negm), add max positive(p2)
  // (2) remove min positive(posm), add max negative(n2)
  // p2/negm > n2/posm ? (1) : (2)
  // => p2*posm > n2*negm ? (1) : (2)

  if (p2 < 0 || negm == INF) { // (2)
    dp /= posm;
    dp *= n2;
  } else if (n2 < 0 || posm == INF) { // (1)
    dp /= negm;
    dp *= p2;
  } else if (p2 * posm > n2 * negm) {
    dp /= negm;
    dp *= p2;
  } else {
    dp /= posm;
    dp *= n2;
  }
  cout << dp.x << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  ll x;
  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;
  }
  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; }
};
typedef vector<mint> vm;
typedef vector<vm> vvm;

int main() {
  int N, K;
  cin >> N >> K;
  vl A(N);
  int pos = 0, neg = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] < 0) {
      neg++;
    } else if (A[i] > 0) {
      pos++;
    }
  }
  sort(rng(A), [](ll a, ll b) { return abs(a) > abs(b); });
  if (A[K - 1] == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (pos == 0 && (K & 1)) {
    reverse(rng(A));
    mint ans = 1;
    rep(i, K) { ans *= A[i]; }
    cout << ans.x << endl;
    return 0;
  }
  vm dp(2);
  dp[0] = 1;
  dp[1] = 0;
  rep(i, K) {
    vm dp2(2);
    if (A[i] < 0) {
      dp2[0] = dp[1] * abs(A[i]);
      dp2[1] = dp[0] * abs(A[i]);
    } else {
      dp2[1] = dp[1] * abs(A[i]);
      dp2[0] = dp[0] * abs(A[i]);
    }
    dp = dp2;
  }
  if (dp[0].x == 0) {
    mint ans = dp[1];
    ans /= dp[K - 1];
    srep(i, K, N) {
      if (A[i] > 0) {
        ans *= A[i];
        cout << ans.x << endl;
        return 0;
      }
    }
  }
  cout << dp[0].x << endl;
  return 0;
}