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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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;
}
const ll INF = 1LL << 60;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  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, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

//---main---------------------------------------------
int main() {
  // main
  int N, K;
  cin >> N >> K;
  int modnum = 1000000007;

  vector<int> Ap, Am, A(N);
  int zeronum = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] >= 0) {
      Ap.push_back(A[i]);
    } else {
      Am.push_back(A[i]);
    }
  }

  bool rarecase = false;
  if (Ap.size() == 0 && K % 2 == 1)
    rarecase = true;
  // if( Ap.size()>0 && N==K && Ap.size()%2 == 1) rarecase = true;
  if (N == K)
    rarecase = true;

  mint ans = 1;
  if (rarecase) {
    sort(A.begin(), A.end(), [](int x, int y) { return (abs(x) < abs(y)); });
    rep(i, K) { ans *= A[i]; }
  } else {
    sort(Ap.begin(), Ap.end());
    sort(Am.begin(), Am.end(), greater<int>());

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

    vector<ll> even;
    while (Ap.size() >= 2) {
      ll a = Ap.back();
      Ap.pop_back();
      a *= Ap.back();
      Ap.pop_back();
      even.push_back(a);
    }
    while (Am.size() >= 2) {
      ll a = Am.back();
      Am.pop_back();
      a *= Am.back();
      Am.pop_back();
      even.push_back(a);
    }
    sort(even.begin(), even.end());
    rep(i, K / 2) {
      ans *= even.back();
      even.pop_back();
    }
  }

  cout << ans << endl;
  return 0;

  /*

  //解がゼロ
  if(N-zeronum < K){
      cout << 0 << endl;
      return 0;
  }

  sort(Ap.begin(),Ap.end());
  sort(Am.begin(),Am.end(),greater<int>());

  ll ans = 1;
  if(( Ap.size() == 0 && K%2 == 1) ){
      rep(i,K){
          ans = ans*ll(Am[i]);
          ans = ans%1000000007;
      }
      if(ans<0) ans += 1000000007;
  }else if(N-zeronum==K){
      rep(i,K){
          ans = ans*ll(A[i]);
          ans = ans%1000000007;
      }
      if(ans<0) ans += 1000000007;
  }else{
      if(K%2==1){
          ans *= Ap.back(); Ap.pop_back();
      }
      vector<ll> even;
      //cout << Ap.size() << " " << Am.size() << endl;
      while(Ap.size()>=2){
          ll a = Ap.back(); Ap.pop_back();
          a *= Ap.back(); Ap.pop_back();
          even.push_back(a);
      }
      while(Am.size()>=2){
          ll a = Am.back(); Am.pop_back();
          a *= Am.back(); Am.pop_back();
          even.push_back(a);
      }
      sort(even.begin(),even.end());
      rep(i,K/2){
          ans *= even.back();even.pop_back();
          ans = ans%1000000007;
      }
  }
  cout << ans << endl;





  /*

  vector<P> A(N);
  int Ai,pnum=0;
  rep(i,N){
      cin >> Ai;
      if(Ai==0){
          A[i] = P(0,0);
      }else if(Ai>0){
          A[i] = P(Ai, 1);
          pnum++;
      }else{
          A[i] = P(-Ai, -1);
      }
  }

  ll ans=1;
  int pm = 1;
  sort(A.begin(),A.end());
  //rep(i,N-1) cout << A[i].first << " " << A[i].second << endl;
  //cout << A[N-1].first << " " << A[N-1].second << endl;

  if(pnum == 0 && K%2==1){
      rep(i,K){
          ans = ans*ll(A[i].first);
          ans = ans%1000000007;
      }
      ans = -ans%1000000007;
      if(ans<0) ans += 1000000007;
  }else{
      rep(i,K){
          ans = ans*ll(A[N-1-i].first);
          ans = ans%1000000007;
          pm = pm*A[N-1-i].second;
          //cout << i << ":" << ans << " " << pm << endl;
      }
      if(pm < 0){
          int rp=-1,rm=-1,lp=-1,lm=-1;
          int r = N-K;
          while( rp<0 || rm<0){
              if(A[r].second>0) rp=r;
              if(A[r].second<0) rm=r;
              r++;
          }
          int l = N-K-1;
          while( lp<0 || lm<0){
              if(A[l].second>0) rp=l;
              if(A[l].second<0) rm=l;
              l--;
          }
          ans = (ans+1000000007)/A[rp].first*A[lm].first;
          ans = chmax(ans,(ans+1000000007)/A[rm].first*A[lp].first);
          ans = ans%1000000007;
      }
  }

  cout << ans << endl;

  vector<int> Ap,Am;
  int Nz=0;
  Ap.push_back(0);
  Am.push_back(0);
  int Ai;
  rep(i,N){
      cin >> Ai;
      if(Ai==0){
          Nz++;
      }else if(Ai>0){
          Ap.push_back(Ai);
      }else{
          Am.push_back(Ai);
      }
  }

  if(N-Nz < K){
      cout << 0 << endl;
      return 0;
  }

  sort(Ap.begin(),Ap.end());
  sort(Am.begin(),Am.end(),greater<int>());

  ll ans = 1;
  if(Ap.size() == 1 && K%2 == 1){
      rep(i,K){
          ans = ans*ll(Am[i+1]);
          ans = ans%1000000007;
      }
      if(ans<0) ans += 1000000007;
  }else{
      rep(i,K){
          if(Ap.back() > abs(Am.back())){
              ans = ans*ll(Ap.back());
              Ap.pop_back();
              ans = ans%1000000007;
          }else{
              ans = ans*ll(-Am.back());
              Am.pop_back();
              ans = ans%1000000007;
          }
      }
  }

  cout << ans << endl;

  */
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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;
}
const ll INF = 1LL << 60;

//---main---------------------------------------------
int main() {
  // main
  int N, K;
  cin >> N >> K;
  int modnum = 1000000007;

  vector<int> Ap, Am, A(N);
  int zeronum = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] == 0) {
      zeronum++;
    } else if (A[i] > 0) {
      Ap.push_back(A[i]);
    } else {
      Am.push_back(A[i]);
    }
  }

  sort(Ap.begin(), Ap.end());
  sort(Am.begin(), Am.end(), greater<int>());

  ll ans = 1;
  if ((Ap.size() == 0 && K % 2 == 1)) {
    rep(i, K) {
      ans = ans * ll(Am[i]);
      ans = ans % 1000000007;
    }
    if (ans < 0)
      ans += 1000000007;
  } else if (Ap.size() + Am.size() == K) {
    rep(i, K) {
      ans = ans * ll(A[i]);
      ans = ans % 1000000007;
    }
    if (ans < 0)
      ans += 1000000007;
  } else {
    if (K % 2 == 1) {
      ans *= Ap.back();
      Ap.pop_back();
    }
    vector<ll> even;
    // cout << Ap.size() << " " << Am.size() << endl;
    while (Ap.size() >= 2) {
      ll a = Ap.back();
      Ap.pop_back();
      a *= Ap.back();
      Ap.pop_back();
      even.push_back(a);
    }
    while (Am.size() >= 2) {
      ll a = Am.back();
      Am.pop_back();
      a *= Am.back();
      Am.pop_back();
      even.push_back(a);
    }
    sort(even.begin(), even.end());
    rep(i, K / 2) {
      ans *= even.back();
      even.pop_back();
      ans = ans % 1000000007;
    }
  }
  cout << ans << endl;

  /*

  vector<P> A(N);
  int Ai,pnum=0;
  rep(i,N){
      cin >> Ai;
      if(Ai==0){
          A[i] = P(0,0);
      }else if(Ai>0){
          A[i] = P(Ai, 1);
          pnum++;
      }else{
          A[i] = P(-Ai, -1);
      }
  }

  ll ans=1;
  int pm = 1;
  sort(A.begin(),A.end());
  //rep(i,N-1) cout << A[i].first << " " << A[i].second << endl;
  //cout << A[N-1].first << " " << A[N-1].second << endl;

  if(pnum == 0 && K%2==1){
      rep(i,K){
          ans = ans*ll(A[i].first);
          ans = ans%1000000007;
      }
      ans = -ans%1000000007;
      if(ans<0) ans += 1000000007;
  }else{
      rep(i,K){
          ans = ans*ll(A[N-1-i].first);
          ans = ans%1000000007;
          pm = pm*A[N-1-i].second;
          //cout << i << ":" << ans << " " << pm << endl;
      }
      if(pm < 0){
          int rp=-1,rm=-1,lp=-1,lm=-1;
          int r = N-K;
          while( rp<0 || rm<0){
              if(A[r].second>0) rp=r;
              if(A[r].second<0) rm=r;
              r++;
          }
          int l = N-K-1;
          while( lp<0 || lm<0){
              if(A[l].second>0) rp=l;
              if(A[l].second<0) rm=l;
              l--;
          }
          ans = (ans+1000000007)/A[rp].first*A[lm].first;
          ans = chmax(ans,(ans+1000000007)/A[rm].first*A[lp].first);
          ans = ans%1000000007;
      }
  }

  cout << ans << endl;

  vector<int> Ap,Am;
  int Nz=0;
  Ap.push_back(0);
  Am.push_back(0);
  int Ai;
  rep(i,N){
      cin >> Ai;
      if(Ai==0){
          Nz++;
      }else if(Ai>0){
          Ap.push_back(Ai);
      }else{
          Am.push_back(Ai);
      }
  }

  if(N-Nz < K){
      cout << 0 << endl;
      return 0;
  }

  sort(Ap.begin(),Ap.end());
  sort(Am.begin(),Am.end(),greater<int>());

  ll ans = 1;
  if(Ap.size() == 1 && K%2 == 1){
      rep(i,K){
          ans = ans*ll(Am[i+1]);
          ans = ans%1000000007;
      }
      if(ans<0) ans += 1000000007;
  }else{
      rep(i,K){
          if(Ap.back() > abs(Am.back())){
              ans = ans*ll(Ap.back());
              Ap.pop_back();
              ans = ans%1000000007;
          }else{
              ans = ans*ll(-Am.back());
              Am.pop_back();
              ans = ans%1000000007;
          }
      }
  }

  cout << ans << endl;

  */
  return 0;
}