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

// 2020-07-05 22:00:23
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
struct mint {
  ll divisor = 1000000007;
  ll val;
  constexpr mint(ll v = 0) noexcept : val(v % divisor) {
    if (val < 0)
      val += divisor;
  }
  constexpr mint operator+(const mint &rhs) const noexcept {
    return mint(*this) += rhs;
  }
  constexpr mint operator-(const mint &rhs) const noexcept {
    return mint(*this) -= rhs;
  }
  constexpr mint operator*(const mint &rhs) const noexcept {
    return mint(*this) *= rhs;
  }
  constexpr mint operator/(const mint &rhs) const noexcept {
    return mint(*this) /= rhs;
  }
  constexpr mint operator~() const noexcept {
    ll a = this->val, b = divisor, u = 1, v = 0;
    while (b > 0) {
      ll t = a / b;
      a -= t * b;
      u -= t * v;
      ll c = a;
      a = b;
      b = c;
      ll w = u;
      u = v;
      v = w;
    }
    if (u < 0)
      u += divisor;
    u %= divisor;
    return u;
  }
  constexpr mint &operator+=(const mint &rhs) noexcept {
    val += rhs.val;
    if (val >= divisor)
      val -= divisor;
    return *this;
  }
  constexpr mint &operator-=(const mint &rhs) noexcept {
    if (val < rhs.val)
      val += divisor;
    val -= rhs.val;
    return *this;
  }
  constexpr mint &operator*=(const mint &rhs) noexcept {
    val = val * rhs.val % divisor;
    return *this;
  }
  constexpr mint &operator/=(const mint &rhs) noexcept {
    val = val * (~(mint(rhs))).val % divisor;
    return *this;
  }
  friend mint modpow(mint a, ll n) noexcept {
    auto res = mint(1);
    while (n > 0) {
      if (n & 1)
        res *= a;
      a *= a;
      n >>= 1;
    }
    return res;
  }
  friend std::ostream &operator<<(std::ostream &os, const mint &a) noexcept {
    return os << a.val;
  }
  friend std::string debug_s(mint a) noexcept { return std::to_string(a.val); }
};

void neqk(int n) {
  mint ans = 1;
  rep(i, n) {
    int a;
    cin >> a;
    ans *= a;
  }
  cout << ans << '\\n';
}
void answer() {
  int n, k, zc = 0;
  cin >> n >> k;
  if (n == k) {
    neqk(n);
    return;
  }
  vector<int> ps, ms;
  rep(i, n) {
    int a;
    cin >> a;
    if (a > 0)
      ps.emplace_back(a);
    else if (a < 0)
      ms.emplace_back(-a);
    else
      zc++;
  }
  sort(ALL(ps), greater<int>());
  sort(ALL(ms), greater<int>());
  int psz = ps.size(), msz = ms.size();
  if (psz == 0 && msz == 0) {
    cout << 0 << '\\n';
    return;
  }
  if (psz + msz < k && zc) {
    cout << 0 << '\\n';
    return;
  }
  if (psz == 0 && k % 2 && zc) {
    cout << 0 << '\\n';
    return;
  }
  if (psz + msz == k && msz % 2 && zc) {
    cout << 0 << '\\n';
    return;
  }
  if (psz == 0) {
    {
      mint ans = 1;
      if (k % 2) {
        reverse(ALL(ms));
        ans *= -1;
      }
      rep(i, k) ans *= ms[i];
      cout << ans << '\\n';
    }
    return;
  }
  {
    int pi = 0, mi = 0, ki = 0;
    mint ans = 1;
    if (k % 2)
      ans *= ps[0], pi++, ki++;
    auto mulp = [&]() {
      ans *= ps[pi];
      ans *= ps[pi + 1];
      pi += 2;
    };
    auto mulm = [&]() {
      ans *= ms[mi];
      ans *= ms[mi + 1];
      mi += 2;
    };
    while (ki < k) {
      if (pi + 1 >= psz) {
        mulm();
      } else if (mi + 1 >= msz) {
        mulp();
      } else if (ll(ps[pi]) * ll(ps[pi + 1]) >= ll(ms[mi]) * ll(ms[mi + 1])) {
        mulp();
      } else {
        mulm();
      }
      ki += 2;
    }
    cout << ans << '\\n';
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Error: 0

Fixed submission:

// 2020-07-05 22:00:23
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
struct mint {
  ll divisor = 1000000007;
  ll val;
  constexpr mint(ll v = 0) noexcept : val(v % divisor) {
    if (val < 0)
      val += divisor;
  }
  constexpr mint operator+(const mint &rhs) const noexcept {
    return mint(*this) += rhs;
  }
  constexpr mint operator-(const mint &rhs) const noexcept {
    return mint(*this) -= rhs;
  }
  constexpr mint operator*(const mint &rhs) const noexcept {
    return mint(*this) *= rhs;
  }
  constexpr mint operator/(const mint &rhs) const noexcept {
    return mint(*this) /= rhs;
  }
  constexpr mint operator~() const noexcept {
    ll a = this->val, b = divisor, u = 1, v = 0;
    while (b > 0) {
      ll t = a / b;
      a -= t * b;
      u -= t * v;
      ll c = a;
      a = b;
      b = c;
      ll w = u;
      u = v;
      v = w;
    }
    if (u < 0)
      u += divisor;
    u %= divisor;
    return u;
  }
  constexpr mint &operator+=(const mint &rhs) noexcept {
    val += rhs.val;
    if (val >= divisor)
      val -= divisor;
    return *this;
  }
  constexpr mint &operator-=(const mint &rhs) noexcept {
    if (val < rhs.val)
      val += divisor;
    val -= rhs.val;
    return *this;
  }
  constexpr mint &operator*=(const mint &rhs) noexcept {
    val = val * rhs.val % divisor;
    return *this;
  }
  constexpr mint &operator/=(const mint &rhs) noexcept {
    val = val * (~(mint(rhs))).val % divisor;
    return *this;
  }
  friend mint modpow(mint a, ll n) noexcept {
    auto res = mint(1);
    while (n > 0) {
      if (n & 1)
        res *= a;
      a *= a;
      n >>= 1;
    }
    return res;
  }
  friend std::ostream &operator<<(std::ostream &os, const mint &a) noexcept {
    return os << a.val;
  }
  friend std::string debug_s(mint a) noexcept { return std::to_string(a.val); }
};

void neqk(int n) {
  mint ans = 1;
  rep(i, n) {
    int a;
    cin >> a;
    ans *= a;
  }
  cout << ans << '\\n';
}
void answer() {
  int n, k, zc;
  cin >> n >> k;
  if (n == k) {
    neqk(n);
    return;
  }
  vector<int> ps, ms;
  rep(i, n) {
    int a;
    cin >> a;
    if (a > 0)
      ps.emplace_back(a);
    else if (a < 0)
      ms.emplace_back(-a);
    else
      zc++;
  }
  sort(ALL(ps), greater<int>());
  sort(ALL(ms), greater<int>());
  int psz = ps.size(), msz = ms.size();
  if (psz == 0 && msz == 0) {
    cout << 0 << '\\n';
    return;
  }
  if (psz + msz < k && zc) {
    cout << 0 << '\\n';
    return;
  }
  if (psz == 0 && k % 2 && zc) {
    cout << 0 << '\\n';
    return;
  }
  if (psz == 0) {
    {
      mint ans = 1;
      if (k % 2) {
        reverse(ALL(ms));
        ans *= -1;
      }
      rep(i, k) ans *= ms[i];
      cout << ans << '\\n';
    }
    return;
  }
  {
    int pi = 0, mi = 0, ki = 0;
    mint ans = 1;
    if (k % 2)
      ans *= ps[0], pi++, ki++;
    while (ki < k) {
      if (ll(ps[pi]) * ll(ps[pi + 1]) >= ll(ms[mi]) * ll(ms[mi + 1])) {
        ans *= ps[pi];
        ans *= ps[pi + 1];
        pi += 2;
      } else {
        ans *= ms[mi];
        ans *= ms[mi + 1];
        mi += 2;
      }
      ki += 2;
    }
    cout << ans << '\\n';
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}