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

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

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; }
};

using modint = ModInt<MOD>;

int main(int argc, char const *argv[]) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  int min_num = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      min_num++;
  }

  if (n == k) {
    modint ans = 1;
    rep(i, k) ans *= a[i];
    cout << ans << '\\n';
    return 0;
  }

  sort(a.begin(), a.end(), [](ll &l, ll &r) { return abs(l) > abs(r); });
  // 前からやってみてpositiveになればそのまま出力
  int cnt = 0;
  rep(i, k) if (a[i] < 0) cnt++;
  if (cnt % 2 == 0) {
    modint ans = 1;
    rep(i, k) ans *= a[i];
    cout << ans << '\\n';
    return 0;
  }

  // どうあがいても0になる場合
  // どうあがいても負の数にしかならない場合
  if (min_num == n) {
    sort(a.begin(), a.end(), greater<>());
    modint ans = 1;
    rep(i, k) ans *= a[i];
    cout << ans << '\\n';
  }
  // やりようによれば正の数になる場合
  else {
    vector<ll> selected, non_selected;
    rep(i, k) selected.push_back(a[i]);
    for (int i = k; i < n; ++i)
      non_selected.push_back(a[i]);
    ll positive_min = LINF;
    ll negative_min = -LINF;
    ll p_idx = -1;
    ll n_idx = -1;
    rep(i, k) {
      ll e = selected[i];
      if (e >= 0) {
        if (chmin(positive_min, e))
          p_idx = i;
      } else {
        if (chmax(negative_min, e))
          n_idx = i;
      }
    }
    ll positive_max = -1;
    ll negative_max = 1;
    for (auto &&e : non_selected) {
      if (e >= 0)
        chmax(positive_max, e);
      else
        chmin(negative_max, e);
    }

    modint ans = 1;
    rep(i, k) {
      if (i == p_idx || i == n_idx)
        continue;
      ans *= selected[i];
    }

    if (positive_min == LINF) {
      if (positive_max != -1) {
        ans *= positive_max;
        cout << ans << '\\n';
        return 0;
      }
    } else if (negative_min == -LINF) {
      if (negative_max != 1) {
        ans *= negative_max;
        cout << ans << '\\n';
        return 0;
      }
    }

    if (positive_max * positive_min >= negative_max * negative_min) {
      ans *= positive_min;
      ans *= positive_max;
    } else {
      ans *= negative_min;
      ans *= negative_max;
    }

    cout << ans << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

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; }
};

using modint = ModInt<MOD>;

int main(int argc, char const *argv[]) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> c(a);
  sort(a.begin(), a.end(),
       [](const ll &l, const ll &r) { return abs(l) > abs(r); });

  /*
  modint min_num = 0;
  modint pul_num = 0;
  modint zer_num = 0;
  rep(i,n) {
      if (a[i] < 0) min_num+=1;
      else if (a[i] > 0) pul_num+=1;
      else zer_num+=1;
  }
  */

  modint ans = 1;
  int cnt = 0;
  vector<ll> b;
  vector<ll> p;
  rep(i, k) {
    if (a[i] < 0ll) {
      cnt++;
      b.push_back(i);
    } else {
      p.push_back(i);
    }
    ans *= a[i];
  }

  if (cnt % 2 == 0) {
    cout << ans << '\\n';
    return 0;
  }

  ans = 1;
  vector<ll> q(a);
  q[b.back()] = 1;
  rep(i, k) { ans *= q[i]; }

  ll tmp = -1;
  for (int i = n - 1; i >= k; --i) {
    chmax(tmp, q[i]);
  }
  if (tmp >= 0) {
    ans *= tmp;
    if (a[b.back()] * a[b.back() + 1] < tmp * p.back()) {
      cout << ans << '\\n';
      return 0;
    }
    ans = 1;
    rep(i, p.back()) ans *= a[i];
    ans *= a[b.back()] * a[b.back() + 1];
    cout << ans << '\\n';
    return 0;
  }

  vector<ll> r(a);

  modint ans_tmp2 = 1;

  sort(c.begin(), c.end(),
       [](const ll &l, const ll &r) { return abs(l) < abs(r); });

  ans = 1;
  rep(i, k) ans *= c[i];
  cout << ans << '\\n';

  return 0;
}