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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

template <int MOD = 1000000007> struct Mint {
  int x;

  Mint() : x(0) {}
  Mint(long long t) {
    x = t % MOD;
    if (x < 0)
      x += MOD;
  }

  Mint pow(int n) {
    Mint res(1), t(x);
    while (n > 0) {
      if (n & 1)
        res *= t;
      t *= t;
      n >>= 1;
    }
    return res;
  }

  Mint inv() const {
    int a = x, b = MOD, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return Mint(u);
  }

  Mint &operator+=(Mint a) {
    x += a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  Mint &operator-=(Mint a) {
    x += MOD - a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  Mint &operator*=(Mint a) {
    x = int(1LL * x * a.x % MOD);
    return *this;
  }

  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(x) += a; }
  Mint operator-(Mint a) const { return Mint(x) -= a; }
  Mint operator*(Mint a) const { return Mint(x) *= a; }
  Mint operator/(Mint a) const { return Mint(x) /= a; }

  Mint operator-() const { return Mint(-x); }

  bool operator==(const Mint a) { return x == a.x; }
  bool operator!=(const Mint a) { return x != a.x; }
  bool operator<(const Mint a) { return x < a.x; }

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

  friend istream &operator>>(istream &is, Mint &a) {
    int t;
    is >> t;
    a = Mint<MOD>(t);
    return (is);
  }
};

signed main() {
  int n, k;
  cin >> n >> k;
  bool pos = false;
  ll a[n];
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      pos = true;
  }
  Mint<> ans = 1;
  if (n == k) {
    rep(i, k) { ans *= a[i]; }
    cout << ans << endl;
    return 0;
  }
  if (!pos && k % 2) {
    sort(a, a + n);
    reverse(a, a + n);
    rep(i, k) { ans *= a[i]; }
    cout << ans << endl;
    return 0;
  }
  vector<ll> vp, vn;
  rep(i, n) {
    if (a[i] >= 0)
      vp.emplace_back(a[i]);
    else
      vn.emplace_back(a[i]);
  }
  sort(ALL(vp));
  reverse(ALL(vp));
  sort(ALL(vn));
  int ppos = 0, npos = 0;
  while (k > 0) {
    if (k == 1) {
      ans *= vp[ppos];
      break;
    }
    if ((ppos + 1 >= (int)vp.size()) ||
        (npos + 1 < (int)vn.size() &&
         vn[npos] * vn[npos + 1] >= vp[ppos] * vp[ppos + 1])) {
      ans *= vn[npos];
      ans *= vn[npos + 1];
      npos += 2;
      k -= 2;
    } else {
      ans *= vp[ppos];
      ppos++;
      k--;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

template <int MOD = 1000000007> struct Mint {
  int x;

  Mint() : x(0) {}
  Mint(long long t) {
    x = t % MOD;
    if (x < 0)
      x += MOD;
  }

  Mint pow(int n) {
    Mint res(1), t(x);
    while (n > 0) {
      if (n & 1)
        res *= t;
      t *= t;
      n >>= 1;
    }
    return res;
  }

  Mint inv() const {
    int a = x, b = MOD, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return Mint(u);
  }

  Mint &operator+=(Mint a) {
    x += a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  Mint &operator-=(Mint a) {
    x += MOD - a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }

  Mint &operator*=(Mint a) {
    x = int(1LL * x * a.x % MOD);
    return *this;
  }

  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(x) += a; }
  Mint operator-(Mint a) const { return Mint(x) -= a; }
  Mint operator*(Mint a) const { return Mint(x) *= a; }
  Mint operator/(Mint a) const { return Mint(x) /= a; }

  Mint operator-() const { return Mint(-x); }

  bool operator==(const Mint a) { return x == a.x; }
  bool operator!=(const Mint a) { return x != a.x; }
  bool operator<(const Mint a) { return x < a.x; }

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

  friend istream &operator>>(istream &is, Mint &a) {
    int t;
    is >> t;
    a = Mint<MOD>(t);
    return (is);
  }
};

signed main() {
  int n, k;
  cin >> n >> k;
  bool pos = false, neg = false, zero = false;
  ll a[n];
  rep(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      pos = true;
    if (a[i] < 0)
      neg = true;
    if (a[i] == 0)
      zero = true;
  }
  Mint<> ans = 1;
  if (n == k) {
    rep(i, n) { ans *= a[i]; }
    cout << ans << endl;
    return 0;
  }
  if (pos == false) {
    if (k % 2 == 0) {
      sort(a, a + n);
      rep(i, k) { ans *= a[i]; }
      cout << ans << endl;
      return 0;
    } else {
      if (zero == true) {
        cout << 0 << endl;
        return 0;
      }
      sort(a, a + n);
      reverse(a, a + n);
      rep(i, k) { ans *= a[i]; }
      cout << ans << endl;
      return 0;
    }
  }
  vector<int> v[2];
  rep(i, n) {
    if (a[i] > 0)
      v[0].emplace_back(a[0]);
    else if (a[i] < 0)
      v[1].emplace_back(a[0]);
  }
  if (k % 2 == 0) {
    priority_queue<ll> que;
    for (int i = 0; i < v[0].size() - 1; i += 2) {
      que.emplace(v[0][i] * v[0][i + 1]);
    }
    for (int i = 0; i < v[1].size() - 1; i += 2) {
      que.emplace(v[1][i] * v[1][i + 1]);
    }
    rep(i, k / 2) {
      ans *= que.top();
      que.pop();
    }
  } else {
    priority_queue<ll> que;
    sort(ALL(v[0]));
    reverse(ALL(v[0]));
    sort(ALL(v[1]));
    bool flag = false;
    int cur[2] = {};
    while (k > 1) {
      if (cur[0] >= v[0].size() - 2) {
        flag = true;
        break;
      }
      if (v[0][cur[0]] * v[0][cur[0] + 1] > v[1][cur[1]] * v[1][cur[1] + 1]) {
        ans *= v[0][cur[0]] * v[0][cur[0] + 1];
        cur[0] += 2;
      } else {
        ans *= v[1][cur[1]] * v[1][cur[1] + 1];
        cur[1] += 2;
      }
      k -= 2;
    }
    if (flag) {
      flag = false;
      while (k > 1) {
        if (cur[1] >= v[1].size() - 2) {
          flag = true;
          break;
        }
        ans *= v[1][cur[1]] * v[1][cur[1] + 1];
        cur[1] += 2;
        k -= 2;
      }
      ans *= v[1][cur[0]];
      if (flag)
        cout << 0 << endl;
      else
        cout << ans << endl;
    } else {
      ans *= v[0][cur[0]];
      cout << ans << endl;
    }
  }
  return 0;
}