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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N), P(0), M(0);
  rep(i, N) cin >> A.at(i);
  int plus = 0, minus = 0, zero = 0;
  rep(i, N) {
    if (A.at(i) > 0) {
      plus++;
      P.pb(A.at(i));
    }
    if (A.at(i) == 0)
      zero++;
    if (A.at(i) < 0) {
      minus++;
      M.pb(A.at(i));
    }
  }
  if (N == K) {
    ll ans = 1;
    rep(i, N) {
      ans *= A.at(i);
      ans %= MOD;
    }
    if (ans < 0)
      ans += MOD;
    cout << ans << endl;
    return 0;
  }
  if (plus + minus < K) {
    cout << 0 << endl;
    return 0;
  }
  if (plus + minus == K) {
    if (minus % 2 == 0) { // 答えは+
      ll ans = 1;
      rep(i, N) {
        if (A.at(i) != 0) {
          ans *= A.at(i);
          ans %= MOD;
        }
      }
      if (ans < 0)
        ans += MOD;
      cout << ans << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  }
  // plus+minus>Kの時
  if (plus == 0) {
    if (K % 2 == 0) {
      sort(M.begin(), M.end());
      ll ans = 1;
      rep(i, K) {
        ans *= M.at(i);
        ans %= MOD;
      }
      cout << ans << endl;
      return 0;
    } else {
      if (zero > 0) {
        cout << 0 << endl;
        return 0;
      } else {
        sort(M.rbegin(), M.rend());
        ll ans = 1;
        rep(i, K) {
          ans *= M.at(i);
          ans %= MOD;
        }
        if (ans < 0)
          ans += MOD;
        cout << ans << endl;
        return 0;
      }
    }
  }
  // 答えは+
  sort(P.rbegin(), P.rend());
  sort(M.begin(), M.end()); // 絶対値大きい順
  vector<LP> mix(0);
  rep(i, plus) { mix.pb({P.at(i), P.at(i)}); }
  rep(i, minus) { mix.pb({-M.at(i), M.at(i)}); }
  sort(mix.rbegin(), mix.rend());
  int pp = 0, pm = 0;
  ll ans = 1;
  if (K % 2 == 1) {
    ans *= P.at(0);
    pp++;
    K--;
  }
  ll np = 1, nm = 1;
  while (K > 0) {
    if (pp + 1 >= P.size())
      np = 0;
    else {
      np = P.at(pp) * P.at(pp + 1);
    }
    if (pm + 1 >= M.size())
      nm = 0;
    else {
      nm = M.at(pm) * M.at(pm + 1);
    }
    if (np > nm) {
      np %= MOD;
      ans *= np;
      ans %= MOD;
      K -= 2;
      pp += 2;
    } else {
      nm %= MOD;
      ans *= nm;
      ans %= MOD;
      K -= 2;
      pm += 2;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N), P(0), M(0);
  rep(i, N) cin >> A.at(i);
  int plus = 0, minus = 0, zero = 0;
  rep(i, N) {
    if (A.at(i) > 0) {
      plus++;
      P.pb(A.at(i));
    }
    if (A.at(i) == 0)
      zero++;
    if (A.at(i) < 0) {
      minus++;
      M.pb(A.at(i));
    }
  }
  if (N == K) {
    ll ans = 1;
    rep(i, N) {
      ans *= A.at(i);
      ans %= MOD;
    }
    if (ans < 0)
      ans += MOD;
    cout << ans << endl;
    return 0;
  }
  if (plus + minus < K) {
    cout << 0 << endl;
    return 0;
  }
  if (plus + minus == K) {
    if (minus % 2 == 0) { // 答えは+
      ll ans = 1;
      rep(i, N) {
        if (A.at(i) != 0) {
          ans *= A.at(i);
          ans %= MOD;
        }
      }
      if (ans < 0)
        ans += MOD;
      cout << ans << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  }
  // plus+minus>Kの時
  if (plus == 0) {
    if (K % 2 == 0) {
      sort(M.begin(), M.end());
      ll ans = 1;
      rep(i, K) {
        ans *= M.at(i);
        ans %= MOD;
      }
      cout << ans << endl;
      return 0;
    } else {
      if (zero > 0) {
        cout << 0 << endl;
        return 0;
      } else {
        sort(M.rbegin(), M.rend());
        ll ans = 1;
        rep(i, K) {
          ans *= M.at(i);
          ans %= MOD;
        }
        if (ans < 0)
          ans += MOD;
        cout << ans << endl;
        return 0;
      }
    }
  }
  // 答えは+
  sort(P.rbegin(), P.rend());
  sort(M.begin(), M.end()); // 絶対値大きい順
  vector<LP> mix(0);
  rep(i, plus) { mix.pb({P.at(i), P.at(i)}); }
  rep(i, minus) { mix.pb({-M.at(i), M.at(i)}); }
  sort(mix.rbegin(), mix.rend());
  int pp = 0, pm = 0;
  ll ans = 1;
  if (K % 2 == 1) {
    ans *= P.at(0);
    pp++;
    K--;
  }
  ll np = 1, nm = 1;
  while (K > 0) {
    if (pp > P.size())
      np = 0;
    else {
      np = P.at(pp) * P.at(pp + 1);
    }
    if (pm > M.size())
      nm = 0;
    else {
      nm = M.at(pm) * M.at(pm + 1);
    }
    if (np > nm) {
      np %= MOD;
      ans *= np;
      ans %= MOD;
      K -= 2;
      pp += 2;
    } else {
      nm %= MOD;
      ans *= nm;
      ans %= MOD;
      K -= 2;
      pm += 2;
    }
  }
  cout << ans << endl;
}