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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int MOD = (int)1e9 + 7, INF = (int)1e9 + 50;

struct mi {
  typedef decay<decltype(MOD)>::type T;
  /// don't silently convert to T
  T v;
  explicit operator T() const { return v; }
  mi() { v = 0; }
  mi(ll _v) {
    v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
    if (v < 0)
      v += MOD;
  }
  friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }
  //    friend void re(mi& a) { ll x; re(x); a = mi(x); }
  //    friend str ts(mi a) { return ts(a.v); }

  mi &operator+=(const mi &m) {
    if ((v += m.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((v -= m.v) < 0)
      v += MOD;
    return *this;
  }
  mi &operator*=(const mi &m) {
    v = (ll)v * m.v % MOD;
    return *this;
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }
  friend mi pow(mi a, ll p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a)
      if (p & 1)
        ans *= a;
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
  }

  mi operator-() const { return mi(-v); }
  mi &operator++() { return *this += 1; }
  mi &operator--() { return *this -= 1; }
  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }
};

int n, k;
vector<pii> xs;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  bool has_pos = false, has_neg = false;
  rep(i, 0, n) {
    int x;
    cin >> x;
    xs.push_back({abs(x), (x >= 0 ? 1 : -1)});
    if (x >= 0)
      has_pos = true;
    else
      has_neg = true;
  }
  sort(all(xs), greater<pii>());
  if (xs[k - 1].first == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (!has_pos) {
    mi res = 1;
    if (k % 2 == 0)
      rep(i, 0, k) res *= xs[i].first;
    else {
      rep(i, n - k, n) res *= xs[i].first * xs[i].second;
    }
    cout << int(res) << endl;
    return 0;
  }
  mi res = 1;
  int s = 1;
  rep(i, 0, k) res *= xs[i].first, s *= xs[i].second;
  if (s == 1 || k == n || !has_neg)
    cout << (int)(res * s) << '\\n';
  else {
    int mxpos = -1, mxneg = -1;
    int mnpos = -1, mnneg = -1;
    rep(i, 0, k) {
      if (xs[i].second == 1)
        mnpos = xs[i].first;
      else
        mnneg = xs[i].first;
    }
    for (int i = n - 1; i >= k; i--) {
      if (xs[i].second == 1)
        mxpos = xs[i].first;
      else
        mxneg = xs[i].first;
    }
    if (mxpos != -1 && mxneg != -1 && mnpos != -1 && mnneg != -1) {
      if (1LL * mxpos * mnpos > 1LL * mxneg * mnneg)
        cout << (int)(res * mxpos / mnneg) << endl;
      else
        cout << (int)(res * mxneg / mnpos) << endl;
    } else if (mxpos != -1 && mnneg != -1) {
      cout << (int)(res * mxpos / mnneg) << endl;
    } else {
      assert(mxneg != -1 && mnpos != -1);
      cout << (int)(res * mxneg / mnpos) << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int MOD = (int)1e9 + 7, INF = (int)1e9 + 50;

struct mi {
  typedef decay<decltype(MOD)>::type T;
  /// don't silently convert to T
  T v;
  explicit operator T() const { return v; }
  mi() { v = 0; }
  mi(ll _v) {
    v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
    if (v < 0)
      v += MOD;
  }
  friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }
  //    friend void re(mi& a) { ll x; re(x); a = mi(x); }
  //    friend str ts(mi a) { return ts(a.v); }

  mi &operator+=(const mi &m) {
    if ((v += m.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((v -= m.v) < 0)
      v += MOD;
    return *this;
  }
  mi &operator*=(const mi &m) {
    v = (ll)v * m.v % MOD;
    return *this;
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }
  friend mi pow(mi a, ll p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a)
      if (p & 1)
        ans *= a;
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
  }

  mi operator-() const { return mi(-v); }
  mi &operator++() { return *this += 1; }
  mi &operator--() { return *this -= 1; }
  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }
};

int n, k;
vector<pii> xs;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  bool has_pos = false, has_neg = false;
  rep(i, 0, n) {
    int x;
    cin >> x;
    xs.push_back({abs(x), (x >= 0 ? 1 : -1)});
    if (x >= 0)
      has_pos = true;
    else
      has_neg = true;
  }
  sort(all(xs), greater<pii>());
  if (xs[k - 1].first == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (!has_pos) {
    mi res = 1;
    if (k % 2 == 0)
      rep(i, 0, k) res *= xs[i].first;
    else {
      rep(i, n - k, n) res *= xs[i].first * xs[i].second;
    }
    cout << int(res) << endl;
    return 0;
  }
  mi res = 1;
  int s = 1;
  rep(i, 0, k) res *= xs[i].first, s *= xs[i].second;
  if (s == 1 || k == n || !has_neg)
    cout << (int)(res) << '\\n';
  else {
    int mxpos = -1, mxneg = -1;
    int mnpos = -1, mnneg = -1;
    rep(i, 0, k) {
      if (xs[i].first == 0)
        continue;
      if (xs[i].second == 1)
        mnpos = xs[i].first;
      else
        mnneg = xs[i].first;
    }
    for (int i = n - 1; i >= k; i--) {
      if (xs[i].first == 0)
        continue;
      if (xs[i].second == 1)
        mxpos = xs[i].first;
      else
        mxneg = xs[i].first;
    }
    if (mxpos != -1 && mxneg != -1 && mnpos != -1 && mnneg != -1) {
      if (1LL * mxpos * mnpos > 1LL * mxneg * mnneg)
        cout << (int)(res * mxpos / mnneg) << endl;
      else
        cout << (int)(res * mxneg / mnpos) << endl;
    } else if (mxpos != -1 && mnneg != -1) {
      cout << (int)(res * mxpos / mnneg) << endl;
    } else {
      assert(mxneg != -1 && mnpos != -1);
      cout << (int)(res * mxneg / mnpos) << endl;
    }
  }
}