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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

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

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

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

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

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

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
const ll mod = 1e9 + 7;
using modint = ModInt<mod>;

int N, K;
vector<ll> A;
void input() {
  fast_io();
  cin >> N >> K;
  A = fetch_vec<ll>(N);
}

int solve() {
  if (K == N) {
    modint ans = 1;
    for (auto a : A)
      ans *= a;
    cout << ans << endl;
    return 0;
  }
  int pnum = 0, mnum = 0;
  for (auto a : A) {
    if (a > 0LL)
      pnum++;
    else if (a < 0LL)
      mnum++;
  }
  auto cmp = [](ll x, ll y) { return abs(x) > abs(y); };
  sort(A.begin(), A.end(), cmp);

  modint ans = 1;
  if (mnum % 2 == 0 && (pnum + mnum) < K) {
    cout << 0 << endl;
    return 0;
  }
  if (mnum % 2 == 1 && (pnum + mnum - 1) < K) {
    cout << 0 << endl;
    return 0;
  }

  if (mnum == 0) {
    for (int i = 0; i < K; i++)
      ans *= A[i];
  } else if (pnum == 0) {
    if (K % 2) {
      reverse(A.begin(), A.end());
      for (int i = 0; i < K; i++)
        ans *= A[i];
    } else {
      for (int i = 0; i < K; i++)
        ans *= A[i];
    }
  } else {
    int minus = 0;
    ll xp = 0LL, xm = 0LL;
    for (int i = 0; i < K; i++) {
      if (A[i] < 0LL) {
        minus++;
        xm = A[i];
      } else {
        xp = A[i];
      }
      ans *= A[i];
    }

    if (minus % 2) {
      ll yp = 0LL, ym = 0LL;
      for (int i = N - 1; i >= K; i--) {
        if (A[i] < 0LL) {
          ym = A[i];
        } else {
          yp = A[i];
        }
      }
      if (ym * xm > yp * xp && xp != 0LL && ym != 0) {
        ans *= ym;
        ans /= xp;
      } else {
        ans *= yp;
        ans /= xm;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

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

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

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

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

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

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
const ll mod = 1e9 + 7;
using modint = ModInt<mod>;

int N, K;
vector<ll> A;
void input() {
  fast_io();
  cin >> N >> K;
  A = fetch_vec<ll>(N);
}

int solve() {
  int pnum = 0, mnum = 0;
  for (auto a : A) {
    if (a > 0)
      pnum++;
    else if (a < 0)
      mnum++;
  }
  auto cmp = [](ll x, ll y) { return abs(x) >= abs(y); };
  sort(A.begin(), A.end(), cmp);

  modint ans = 1;
  if (mnum % 2 == 0 && (pnum + mnum) < K) {
    cout << 0 << endl;
    return 0;
  }
  if (mnum % 2 == 1 && (pnum + mnum - 1) < K) {
    cout << 0 << endl;
    return 0;
  }

  if (mnum == 0) {
    for (int i = 0; i < K; i++)
      ans *= A[i];
  } else if (pnum == 0) {
    if (K % 2) {
      reverse(A.begin(), A.end());
      for (int i = 0; i < K; i++)
        ans *= A[i];
    } else {
      for (int i = 0; i < K; i++)
        ans *= A[i];
    }
  } else {
    int minus = 0;
    ll xp = 0, xm = 0;
    for (int i = 0; i < K; i++) {
      if (A[i] < 0) {
        minus++;
        xm = A[i];
      } else {
        xp = A[i];
      }
      ans *= A[i];
    }

    if (minus % 2) {
      ll yp = 0, ym = 0;
      for (int i = N - 1; i >= K; i--) {
        if (A[i] < 0) {
          ym = A[i];
        } else {
          yp = A[i];
        }
      }
      if (ym * xm > yp * xp && xp != 0) {
        ans *= ym;
        ans /= xp;
      } else {
        ans *= yp;
        ans /= xm;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}