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

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
const int MAX_N = 1e6 + 1;
vector<long long> factorialMemo(MAX_N, -1); // factorialMemo[i] := i! mod p
vector<long long> inverseFactorialMemo(
    MAX_N, -1); // inverseFactorialMemo[i] := (factorialMemo[i])^(-1)
bool initFinished = false;
template <long long mod> struct ModInt {
  long long 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 = (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 {
    long long 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 long long get_mod() { return mod; }
  static void factorialInit() {
    factorialMemo[0] = 1;
    for (ll i = 0; i < MAX_N - 1; i++) {
      factorialMemo[i + 1] = ModInt<mod>(factorialMemo[i] * (i + 1)).x;
    }
    initFinished = true;
  }
  static ModInt factorial(ll n) {
    if (!initFinished) {
      factorialInit();
    }
    return factorialMemo[n];
  }

  static ModInt Combination(ll n, ll k) {
    if (n < k)
      return 0;
    return factorial(n) / (factorial(n - k) * factorial(k));
  }
};
using mint = ModInt<(int)1e9 + 7>;
int main() {
  int N, K;
  cin >> N >> K;
  vll A(N);
  mint ans = 1;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(all(A), [](ll a, ll b) { return abs(a) > abs(b); });
  int minusNum = 0;
  set<int> choice;
  for (size_t i = 0; i < K; i++) {
    choice.insert(i);
    if (A[i] < 0)
      minusNum++;
  }
  if (minusNum % 2 == 0) {
    for (auto &&i : choice) {
      ans *= A[i];
    }
    cout << ans;
    return 0;
  } else // このままだと負になる
  {
    // マイナスを1個減らしてプラスを1個増やす
    int deleteMinusIndex = -1, insertPlusIndex = -1;
    ll Num1 = -1, Denom1 = -1, Num2 = -1, Denom2 = -1;
    for (int i = N - 1; i >= 0; i--) {
      if (A[i] < 0 and choice.count(i) == 1) {
        deleteMinusIndex = i;
        Denom1 = abs(A[i]);
        break;
      }
    }
    for (size_t i = 0; i < N; i++) {
      if (A[i] > 0 and choice.count(i) == 0) {
        insertPlusIndex = i;
        Num1 = abs(A[i]);
        break;
      }
    }

    // プラスを1個減らしてマイナスを1個増やす
    int deletePlusIndex = -1, insertMinusIndex = -1;
    for (int i = N - 1; i >= 0; i--) {
      if (A[i] > 0 and choice.count(i) == 1) {
        deletePlusIndex = i;
        Denom2 = abs(A[i]);
        break;
      }
    }
    for (size_t i = 0; i < N; i++) {
      if (A[i] < 0 and choice.count(i) == 0) {
        insertMinusIndex = i;
        Num2 = abs(A[i]);
        break;
      }
    }
    if ((Num1 == -1 or Denom1 == -1) and
        (Num2 == -1 or Denom2 == -1)) // どうやってもプラスにならない
    {
      for (int i = 0; i < K; i++) {
        ans *= A[N - 1 - i];
      }
      cout << ans;
      return 0;
    } else if (Num1 == -1 or
               Denom1 == -1) // プラスを1個減らしてマイナスを1個増やすしかない
    {
      choice.erase(deletePlusIndex);
      choice.insert(insertMinusIndex);
    } else if (Num2 == -1 or
               Denom2 == -1) // マイナスを1個減らしてプラスを1個増やすしかない
    {
      choice.erase(deleteMinusIndex);
      choice.insert(insertPlusIndex);
    } else if (
        Num1 * Denom2 >
        Num2 *
            Denom1) // どっちもできるけどマイナスを1個減らしてプラスを1個増やすほうが大きくなるとき
    {
      choice.erase(deleteMinusIndex);
      choice.insert(insertPlusIndex);
    } else // どっちもできるけどプラスを1個減らしてマイナスを1個増やすほうが大きくなるとき
    {
      choice.erase(deletePlusIndex);
      choice.insert(insertMinusIndex);
    }
  }
  for (auto &&i : choice) {
    ans *= A[i];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
const int MAX_N = 1e6 + 1;
vector<long long> factorialMemo(MAX_N, -1); // factorialMemo[i] := i! mod p
vector<long long> inverseFactorialMemo(
    MAX_N, -1); // inverseFactorialMemo[i] := (factorialMemo[i])^(-1)
bool initFinished = false;
template <long long mod> struct ModInt {
  long long 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 = (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 {
    long long 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 long long get_mod() { return mod; }
  static void factorialInit() {
    factorialMemo[0] = 1;
    for (ll i = 0; i < MAX_N - 1; i++) {
      factorialMemo[i + 1] = ModInt<mod>(factorialMemo[i] * (i + 1)).x;
    }
    initFinished = true;
  }
  static ModInt factorial(ll n) {
    if (!initFinished) {
      factorialInit();
    }
    return factorialMemo[n];
  }

  static ModInt Combination(ll n, ll k) {
    if (n < k)
      return 0;
    return factorial(n) / (factorial(n - k) * factorial(k));
  }
};
using mint = ModInt<(int)1e9 + 7>;
int main() {
  int N, K;
  cin >> N >> K;
  mint ans = 1;
  vll A(N), B;
  bool containsZero = false;
  int mincnt = 0;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > 0)
      B.push_back(A[i]);

    if (A[i] == 0)
      containsZero = true;
    if (A[i] < 0)
      mincnt++;
  }

  sort(all(A), [](ll a, ll b) { return abs(a) > abs(b); });
  if (B.size() == 0 and K % 2 == 1) // 答えが0または負
  {
    if (containsZero) {
      ans = 0;
    } else {
      for (int i = 0; i < K; i++) {
        ans *= A[N - 1 - i];
      }
    }
    cout << ans;
    return 0;
  }
  set<ll> choice;
  int minusNum = 0;
  for (int i = 0; i < K; i++) {
    choice.insert(i);
    if (A[i] < 0) {
      minusNum++;
    }
  }
  if (minusNum % 2 == 0) // 答えが正になる
  {
    for (auto &&i : choice) {
      ans *= A[i];
    }
  } else {
    int idxp = 0, idxm = 0;
    bool flag = false, flag2 = false;
    for (int i = N - 1; i >= 0; i--) {
      if (A[i] <= 0 and choice.count(i) == 1) {
        idxm = i;
        flag = true;
        break;
      }
    }

    for (size_t i = 0; i < N; i++) {
      if (A[i] >= 0 and choice.count(i) == 0) {
        idxp = i;
        flag2 = true;
        break;
      }
    }
    if (flag and flag2) // 答えが正になる
    {
      choice.erase(idxm);
      choice.insert(idxp);
      for (auto &&i : choice) {
        ans *= A[i];
      }
    } else {
      assert(0);
    }
  }
  cout << ans << endl;
  return 0;
}