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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template <long long M> class modint {
public:
  long long a;
  modint(const long long x = 0LL) : a((x % M + M) % M) {}
  long long &value() { return a; }
  modint operator+(const modint rhs) const { return modint(*this) += rhs; }
  modint operator-(const modint rhs) const { return modint(*this) -= rhs; }
  modint operator*(const modint rhs) const { return modint(*this) *= rhs; }
  modint operator/(const modint rhs) const { return modint(*this) /= rhs; }
  modint &operator+=(const modint rhs) {
    a += rhs.a;
    if (a >= M) {
      a -= M;
    }
    return *this;
  }
  modint &operator-=(const modint rhs) {
    if (a < rhs.a) {
      a += M;
    }
    a -= rhs.a;
    return *this;
  }
  modint &operator*=(const modint rhs) {
    a = a * rhs.a % M;
    return *this;
  }
  modint pow(long long n) const {
    modint ret(1);
    modint x = *this;
    while (n) {
      if ((n & 1) == 1) {
        ret *= x;
      }
      n >>= 1;
      x *= x;
    }
    return ret;
  }
  modint inv() const { return pow(M - 2); }
  modint &operator/=(const modint rhs) { return *this *= rhs.inv(); }
};

const long long M = 1000000007;

using mint = modint<M>;

int main(void) {
  ll N, K;

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

  ll num_negative = 0;
  for (auto e : A) {
    if (e < 0) {
      num_negative++;
    }
  }

  bool is_positive;
  if (N == K) {
    is_positive = (num_negative % 2 == 0);
  } else if (N > K) {
    if (num_negative == N) {
      is_positive = (K % 2 == 0);
    } else {
      is_positive = true;
    }
  }

  mint ans(1);
  if (!is_positive) {
    sort(A.begin(), A.end(), [](auto &x, auto &y) { return abs(x) < abs(y); });
    for (ll i = 0; i < K; i++) {
      ans = (ans * mint(A[i]));
    }
  } else {
    vector<ll> positive, negative;
    for (auto e : A) {
      if (e >= 0) {
        positive.push_back(e);
      } else {
        negative.push_back(e);
      }
    }
    sort(positive.begin(), positive.end());
    sort(negative.rbegin(), negative.rend());

    vector<ll> p;
    if (K % 2 == 1) {
      ans = ans * mint(positive.back());
      positive.pop_back();
    }

    for (auto it = positive.rbegin(); it < positive.rend(); it += 2) {
      p.push_back(*it * *(it + 1));
    }
    for (auto it = negative.rbegin(); it < negative.rend(); it += 2) {
      p.push_back(*it * *(it + 1));
    }
    sort(p.rbegin(), p.rend());

    for (ll i = 0; i < K / 2; i++) {
      ans = ans * mint(p[i]);
    }
  }

  cout << ans.a << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template <long long M> class modint {
public:
  long long a;
  modint(const long long x = 0LL) : a((x % M + M) % M) {}
  long long &value() { return a; }
  modint operator+(const modint rhs) const { return modint(*this) += rhs; }
  modint operator-(const modint rhs) const { return modint(*this) -= rhs; }
  modint operator*(const modint rhs) const { return modint(*this) *= rhs; }
  modint operator/(const modint rhs) const { return modint(*this) /= rhs; }
  modint &operator+=(const modint rhs) {
    a += rhs.a;
    if (a >= M) {
      a -= M;
    }
    return *this;
  }
  modint &operator-=(const modint rhs) {
    if (a < rhs.a) {
      a += M;
    }
    a -= rhs.a;
    return *this;
  }
  modint &operator*=(const modint rhs) {
    a = a * rhs.a % M;
    return *this;
  }
  modint pow(long long n) const {
    modint ret(1);
    modint x = *this;
    while (n) {
      if ((n & 1) == 1) {
        ret *= x;
      }
      n >>= 1;
      x *= x;
    }
    return ret;
  }
  modint inv() const { return pow(M - 2); }
  modint &operator/=(const modint rhs) { return *this *= rhs.inv(); }
};

const long long M = 1000000007;

using mint = modint<M>;

int main(void) {
  ll N, K;

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

  ll num_negative = 0;
  for (auto e : A) {
    if (e < 0) {
      num_negative++;
    }
  }

  bool is_positive;
  if (N == K) {
    is_positive = (num_negative % 2 == 0);
  } else if (N > K) {
    if (num_negative == N) {
      is_positive = (K % 2 == 0);
    } else {
      is_positive = true;
    }
  }

  mint ans(1);
  if (!is_positive) {
    vector<ll> absolute(N);
    for (ll i = 0; i < N; i++) {
      absolute[i] = A[i] >= 0 ? A[i] : -1 * A[i];
    }
    sort(absolute.begin(), absolute.end());

    for (ll i = 0; i < K; i++) {
      ans = (ans * mint(absolute[i]));
    }
    ans = ans * mint(-1);
  } else {
    vector<ll> positive, negative;
    for (auto e : A) {
      if (e >= 0) {
        positive.push_back(e);
      } else {
        negative.push_back(e);
      }
    }
    // 10, 9, 8, ...
    sort(positive.rbegin(), positive.rend());
    // -10, -9, -8, ...
    sort(negative.begin(), negative.end());

    ll p_now, n_now;
    p_now = n_now = 0;
    while (K > 1) {
      ll x, y;
      x = positive.size() - p_now > 0 ? positive[p_now] * positive[p_now + 1]
                                      : 0;
      y = negative.size() - n_now > 0 ? negative[n_now] * negative[n_now + 1]
                                      : 0;
      if (x > y) {
        ans = ans * mint(x);
        p_now += 2;
      } else {
        ans = ans * mint(y);
        n_now += 2;
      }
      K -= 2;
    }
    if (K == 1) {
      ans = ans * mint(positive[p_now]);
    }
  }

  cout << ans.a << endl;
}