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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\\n"
#define ENDL cout << "\\n"
#define DF(x) x.erase(x.begin()) // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\\n"
#define RETURN(x)                                                              \\
  cout << x << "\\n";                                                           \\
  return 0
clock_t CLOCK;
#define START_TIMER CLOCK = clock()
#define SHOW_TIMER cerr << "time: " << (ld)(clock() - CLOCK) / 1000000 << "\\n"
#define init()                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\\n";
#define debugV(v)                                                              \\
  cerr << "[debugV] " << #v << ":";                                            \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << "\\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

const ll mod = 1000000007;
struct mint {
  ll x;
  // コンストラクタ。引数のxが負でもxは正になるようにしている
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

signed main() {
  init();
  ll N, K;
  cin >> N >> K;

  vll a(N), s, t;
  rep(i, N) {
    ll b;
    cin >> b;
    a[i] = b;
    if (b < 0)
      t.pb(b);
    else
      s.pb(b);
  }

  ll S = s.size();
  ll T = t.size();

  bool ok = false;
  if (S > 0) {
    if (N == K)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (K % 2 == 0);
  }

  mint ans = 1;
  if (!ok) {
    sort(ALL(a), [](ll x, ll y) { return abs(x) < abs(y); });
    rep(i, K) ans *= a[i];
  } else {
    sort(ALL(s));
    sort(ALL(t), greater<ll>());

    if (K % 2 == 1) {
      ans *= s.back();
      s.pop_back();
    }

    vll p;
    while (s.size() >= 2) {
      ll x = s.back();
      s.pop_back();
      x *= s.back();
      s.pop_back();
      p.pb(x);
    }

    while (t.size() >= 2) {
      ll x = t.back();
      t.pop_back();
      x *= t.back();
      t.pop_back();
      p.pb(x);
    }

    sort(ALL(p), greater<ll>());
    rep(i, K / 2) ans *= p[i];
  }
  ANS;
  return 0;
}

Error: 0
[debug] line: 163
[debug] line: 182
[debug] count: 0
[debug] a: 2
[debug] b: 12


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\\n"
#define ENDL cout << "\\n"
#define DF(x) x.erase(x.begin()) // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\\n"
#define RETURN(x)                                                              \\
  cout << x << "\\n";                                                           \\
  return 0
clock_t CLOCK;
#define START_TIMER CLOCK = clock()
#define SHOW_TIMER cerr << "time: " << (ld)(clock() - CLOCK) / 1000000 << "\\n"
#define init()                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\\n";
#define debugV(v)                                                              \\
  cerr << "[debugV] " << #v << ":";                                            \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << "\\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

const ll mod = 1000000007;
struct mint {
  ll x;
  // コンストラクタ。引数のxが負でもxは正になるようにしている
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

// bool comp(ll a, ll b) {
//   return abs(a) < abs(b);
// }

signed main() {
  init();
  ll N, K;
  cin >> N >> K;

  bool zero_flag = false;
  vll PP;
  vll M;
  vll PM(N);
  rep(i, N) {
    cin >> PM[i];
    if (PM[i] >= 0) {
      PP.pb(PM[i]);
    } else {
      M.pb(PM[i]);
    }
    if (PM[i] == 0)
      zero_flag = true;
  }

  mint ans = 0;
  ll m_use = max(0ll, K - (ll)PP.size());
  LINE;
  if (N == K || m_use % 2 == 1) {
    LINE;
    if (zero_flag) {
      COUT(0);
      return 0;
    }
    ans = 1;
    // debugV(PM);
    // LINE;
    // sort(ALL(PM), comp);

    // sort(ALL(PM), [](int i, int j) -> bool { return abs(i) > abs(j); });

    // debugV(PM);
    rep(i, K) { ans *= PM[i]; }
  } else {
    LINE;
    sort(ALL(PP), greater<ll>());
    sort(ALL(M), less<ll>());

    ans = 1;
    ll count = 0;
    ll index_p = 0;
    ll index_m = 0;
    while (count != K) {
      debug(count);
      if (count == K - 1) {
        ans *= PP[index_p];
        index_p++;
        count++;
      } else {
        ll a = PP[index_p];
        if (index_p + 1 < (ll)PP.size()) {
          a *= PP[index_p + 1];
        }

        ll b = M[index_m];

        if (index_m + 1 < (ll)M.size()) {
          b *= M[index_p + 1];
        } else {
          ans *= PP[index_p];
          index_p++;
          count++;
          continue;
        }
        debug(a);
        debug(b);

        if (a > b) {
          ans *= PP[index_p];
          index_p++;
          count++;
        } else {
          ans *= b;
          index_m++;
          count += 2;
        }
      }
    }
  }
  ANS;
  return 0;
}