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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  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;
  }

  // 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 { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;

  vector<ll> a(n), plus, minus;
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      plus.push_back(a[i]);
    else
      minus.push_back(a[i]);
  }

  int plusnum = plus.size(), minusnum = minus.size();

  sort(plus.begin(), plus.end());
  reverse(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());

  mint ans = 1;

  if (plusnum == 0) {
    if (k % 2 == 0) {
      for (int i = 0; i < k; i++) {
        ans *= minus[i];
      }
    } else {
      reverse(minus.begin(), minus.end());
      for (int i = 0; i < k; i++) {
        ans *= minus[i];
      }
    }
  } else if (minusnum % 2 == 1 && k == n) {
    reverse(minus.begin(), minus.end());
    reverse(plus.begin(), plus.begin());
    int pcnt = 0, mcnt = 0;
    ll p, m;
    for (int i = 0; i < k; i++) {
      if (pcnt < plusnum)
        p = plus[pcnt];
      else
        p = 10000000000000;
      if (mcnt < minusnum)
        m = minus[mcnt];
      else
        m = 10000000000000;
      if (abs(p) <= abs(m)) {
        ans *= p;
        pcnt++;
      } else {
        ans *= m;
        mcnt++;
      }
    }
  } else if (k % 2 == 0) {
    int pcnt = 0, mcnt = 0;
    ll p, m;
    for (int i = 0; i < k; i += 2) {
      if (pcnt < plusnum - 1)
        p = plus[pcnt] * plus[pcnt + 1];
      else
        p = -1;
      if (mcnt < minusnum - 1)
        m = minus[mcnt] * minus[mcnt + 1];
      else
        m = -1;
      if (p >= m) {
        ans *= p;
        pcnt += 2;
      } else {
        ans *= m;
        mcnt += 2;
      }
    }
  } else {
    ans *= plus[0];
    int pcnt = 1, mcnt = 0;
    ll p, m;
    for (int i = 1; i < k; i += 2) {
      if (pcnt < plusnum - 1)
        p = plus[pcnt] * plus[pcnt + 1];
      else
        p = -1;
      if (mcnt < minusnum - 1)
        m = minus[mcnt] * minus[mcnt + 1];
      else
        m = -1;
      if (p >= m) {
        ans *= p;
        pcnt += 2;
      } else {
        ans *= m;
        mcnt += 2;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  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;
  }

  // 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 { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;

  vector<ll> a(n), plus, minus;
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      plus.push_back(a[i]);
    else
      minus.push_back(a[i]);
  }

  int plusnum = plus.size(), minusnum = minus.size();

  sort(plus.begin(), plus.end());
  reverse(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());

  mint ans = 1;

  if (plusnum == 0) {
    if (k % 2 == 0) {
      for (int i = 0; i < k; i++) {
        ans *= minus[i];
      }
    } else {
      reverse(minus.begin(), minus.end());
      for (int i = 0; i < k; i++) {
        ans *= minus[i];
      }
    }
  } else if (minusnum % 2 == 1 && k == n) {
    reverse(minus.begin(), minus.end());
    for (int i = 0; i < k; i++) {
      ans *= minus[i];
    }
  } else if ((minusnum % 2 == 0 && k % 2 == 0) ||
             (minusnum % 2 == 1 && k % 2 == 1)) {
    int pcnt = 0, mcnt = 0;
    ll p, m;
    for (int i = 0; i < k; i += 2) {
      if (pcnt < plusnum - 1)
        p = plus[pcnt] * plus[pcnt + 1];
      else
        p = -1;
      if (mcnt < minusnum - 1)
        m = minus[mcnt] * minus[mcnt + 1];
      else
        m = -1;
      if (p >= m) {
        ans *= p;
        pcnt += 2;
      } else {
        ans *= m;
        mcnt += 2;
      }
    }
  } else {
    ans *= plus[0];
    int pcnt = 1, mcnt = 0;
    ll p, m;
    for (int i = 1; i < k; i += 2) {
      if (pcnt < plusnum - 1)
        p = plus[pcnt] * plus[pcnt + 1];
      else
        p = -1;
      if (mcnt < minusnum - 1)
        m = minus[mcnt] * minus[mcnt + 1];
      else
        m = -1;
      if (p >= m) {
        ans *= p;
        pcnt += 2;
      } else {
        ans *= m;
        mcnt += 2;
      }
    }
  }

  cout << ans << endl;

  return 0;
}