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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

class Fp {
public:
  ll val;
  static ll mod;

  Fp() { val = 0; }
  Fp(ll v) : val(v % mod) {
    if (val < 0)
      val += mod;
  }
  Fp operator+=(const Fp &in) {
    val += in.val;
    calc_mod(val);
    return *this;
  }
  Fp operator+(const Fp &in) const { return Fp(*this) += in; }
  Fp &operator-=(const Fp &in) {
    val -= in.val;
    calc_mod(val);
    if (val < 0)
      val += mod;
    return *this;
  }
  Fp operator-(const Fp &in) const { return Fp(*this) -= in; }
  Fp &operator*=(const Fp &in) {
    val = val * in.val % mod;
    return *this;
  }
  Fp operator*(const Fp &in) const { return Fp(*this) *= in; }
  Fp &operator/=(const Fp &in) {
    *this *= in.modpow(mod - 2);
    return *this;
  }
  Fp operator/(const Fp &in) const { return Fp(*this) /= in; }
  bool operator==(const Fp &in) const { return val == in.val; }
  bool operator!=(const Fp &in) const { return val != in.val; }
  Fp modpow(ll n) const {
    if (n == 0)
      return 1;
    if (n & 1) {
      return (*this) * ((*this) * (*this)).modpow(n / 2);
    } else {
      return ((*this) * (*this)).modpow(n / 2);
    }
  }
  friend istream &operator>>(istream &is, Fp &fp) {
    is >> fp.val;
    return is;
  }
  friend ostream &operator<<(ostream &os, Fp &fp) {
    os << fp.val;
    return os;
  }
  friend ostream &operator<<(ostream &os, Fp &&fp) {
    os << fp.val;
    return os;
  }

private:
  void calc_mod(ll &v) {
    if (v >= mod)
      v -= mod;
  }
};

Fp modpow(const Fp &in, const ll n) { return in.modpow(n); }
Fp modpow(const ll a, const ll n) { return Fp(a).modpow(n); }

using mint = Fp;
ll mint::mod = MOD;

class Factorial {
public:
  vector<mint> fact, finv;
  Factorial(ll n) : fact(n + 1, mint(1)), finv(n + 1, mint(1)) {
    for (int i = 2; i <= n; i++) {
      mint tmp(i);
      fact[i] = fact[i - 1] * tmp;
      finv[i] = finv[i - 1] / tmp;
    }
  }
  mint perm(const ll &n, const ll &k) { return fact[n] * finv[n - k]; }
  mint com(const ll &n, const ll &k) {
    if (n < k || n < 0 || k < 0)
      return mint(0);
    return fact[n] * finv[k] * finv[n - k];
  }
};

signed main() {
  cin >> N >> K;
  vector<ll> a(N);
  cin >> a;
  vector<ll> an;
  vector<ll> ap;
  int zeroN = 0;
  REP(i, N) {
    if (a[i] < 0)
      an.push_back(a[i]);
    else if (a[i] > 0)
      ap.push_back(a[i]);
    else
      zeroN++;
  }
  if (K > ap.size() + an.size()) {
    cout << 0 << endl;
  } else if (K == ap.size() + an.size()) {
    if (an.size() & 1) {
      if (zeroN > 0) {
        cout << 0 << endl;
        return 0;
      }
    }
    mint ans = 1;
    REP(i, ap.size())
    ans *= ap[i];
    REP(i, an.size())
    ans *= an[i];
    cout << ans << endl;
  } else {
    if (ap.size() == 0) {
      mint ans = 1;
      if (zeroN > 0 && (K & 1))
        ans = 0;
      else {
        if (K & 1)
          sort(an.rbegin(), an.rend());
        else
          sort(an.begin(), an.end());

        REP(i, K) { ans *= an[i]; }
      }
      cout << ans << endl;
      return 0;
    }
    sort(ap.rbegin(), ap.rend());
    sort(an.begin(), an.end());
    vector<pair<ll, int>> a2;
    for (int i = 0; i < int(an.size()) - 1; i += 2) {
      a2.push_back({an[i] * an[i + 1], -i - 10});
    }
    for (int i = 0; i < int(ap.size()) - 1 - (K & 1 ? 1 : 0); i += 2) {
      a2.push_back({ap[i] * ap[i + 1], i});
    }
    sort(a2.rbegin(), a2.rend());
    int ind = -2;
    mint ans = 1;
    // dump(a2);
    dump(ap.size(), an.size());
    REP(i, K / 2) {
      ans *= a2[i].first;
      ind = max(ind, a2[i].second);
    }
    dump(ans, ind);
    if (K & 1) {
      ans *= ap[ind + 2];
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

class Fp {
public:
  ll val;
  static ll mod;

  Fp() { val = 0; }
  Fp(ll v) : val(v % mod) {
    if (val < 0)
      val += mod;
  }
  Fp operator+=(const Fp &in) {
    val += in.val;
    calc_mod(val);
    return *this;
  }
  Fp operator+(const Fp &in) const { return Fp(*this) += in; }
  Fp &operator-=(const Fp &in) {
    val -= in.val;
    calc_mod(val);
    if (val < 0)
      val += mod;
    return *this;
  }
  Fp operator-(const Fp &in) const { return Fp(*this) -= in; }
  Fp &operator*=(const Fp &in) {
    val = val * in.val % mod;
    return *this;
  }
  Fp operator*(const Fp &in) const { return Fp(*this) *= in; }
  Fp &operator/=(const Fp &in) {
    *this *= in.modpow(mod - 2);
    return *this;
  }
  Fp operator/(const Fp &in) const { return Fp(*this) /= in; }
  bool operator==(const Fp &in) const { return val == in.val; }
  bool operator!=(const Fp &in) const { return val != in.val; }
  Fp modpow(ll n) const {
    if (n == 0)
      return 1;
    if (n & 1) {
      return (*this) * ((*this) * (*this)).modpow(n / 2);
    } else {
      return ((*this) * (*this)).modpow(n / 2);
    }
  }
  friend istream &operator>>(istream &is, Fp &fp) {
    is >> fp.val;
    return is;
  }
  friend ostream &operator<<(ostream &os, Fp &fp) {
    os << fp.val;
    return os;
  }
  friend ostream &operator<<(ostream &os, Fp &&fp) {
    os << fp.val;
    return os;
  }

private:
  void calc_mod(ll &v) {
    if (v >= mod)
      v -= mod;
  }
};

Fp modpow(const Fp &in, const ll n) { return in.modpow(n); }
Fp modpow(const ll a, const ll n) { return Fp(a).modpow(n); }

using mint = Fp;
ll mint::mod = MOD;

class Factorial {
public:
  vector<mint> fact, finv;
  Factorial(ll n) : fact(n + 1, mint(1)), finv(n + 1, mint(1)) {
    for (int i = 2; i <= n; i++) {
      mint tmp(i);
      fact[i] = fact[i - 1] * tmp;
      finv[i] = finv[i - 1] / tmp;
    }
  }
  mint perm(const ll &n, const ll &k) { return fact[n] * finv[n - k]; }
  mint com(const ll &n, const ll &k) {
    if (n < k || n < 0 || k < 0)
      return mint(0);
    return fact[n] * finv[k] * finv[n - k];
  }
};

signed main() {
  cin >> N >> K;
  vector<ll> a(N);
  cin >> a;
  vector<ll> an;
  vector<ll> ap;
  int zeroN = 0;
  REP(i, N) {
    if (a[i] < 0)
      an.push_back(a[i]);
    else if (a[i] > 0)
      ap.push_back(a[i]);
    else
      zeroN++;
  }
  if (K > ap.size() + an.size()) {
    cout << 0 << endl;
  } else if (K == ap.size() + an.size()) {
    if (an.size() & 1) {
      if (zeroN > 0) {
        cout << 0 << endl;
        return 0;
      }
    }
    mint ans = 1;
    REP(i, ap.size())
    ans *= ap[i];
    REP(i, an.size())
    ans *= an[i];
    cout << ans << endl;

  } else {
    if (ap.size() == 0) {
      mint ans = 1;
      if (zeroN > 0)
        ans = 0;
      else {
        sort(an.rbegin(), an.rend());
        REP(i, K) { ans *= an[i]; }
      }
      cout << ans << endl;
      return 0;
    }
    sort(ap.rbegin(), ap.rend());
    sort(an.begin(), an.end());
    vector<pair<ll, int>> a2;
    for (int i = 0; i < int(an.size()) - 1; i += 2) {
      a2.push_back({an[i] * an[i + 1], -i - 10});
    }
    for (int i = 0; i < int(ap.size()) - 1 - (K & 1 ? 1 : 0); i += 2) {
      a2.push_back({ap[i] * ap[i + 1], i});
    }
    sort(a2.rbegin(), a2.rend());
    int ind = -1;
    mint ans = 1;
    dump(a2);
    REP(i, K / 2) {
      ans *= a2[i].first;
      ind = max(ind, a2[i].second);
    }
    dump(ans, ind);
    if (K & 1) {
      ans *= ap[2 * (ind + 1)];
    }
    cout << ans << endl;
  }
}