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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {
  ll N, K, a;
  vector<ll> A, B;
  cin >> N >> K;
  REP(i, N) {
    cin >> a;
    if (a >= 0) {
      A.push_back(a);
    } else {
      B.push_back(-a);
    }
  }
  sort(A.begin(), A.end(), greater<>());
  sort(B.begin(), B.end(), greater<>());
  // REP(i,A.size()){cout << A[i] << " ";}cout << endl;
  // REP(i,B.size()){cout << B[i] << " ";}cout << endl;

  // 非負をつくれない:非負要素がないかつKが奇数
  // またはK=Nかつ|B|%2==1
  if (A.size() == 0 && K % 2 == 1) {
    ll Ans = 1;
    REP(i, K) { Ans = (Ans * B[B.size() - 1 - i]) % MOD; }
    cout << MOD - Ans << endl;
    return 0;
  }
  if (K == N && (B.size()) % 2 == 1) {
    ll Ans = 1;
    REP(i, A.size()) { Ans = (Ans * A[i]) % MOD; }
    REP(i, B.size()) { Ans = (Ans * B[i]) % MOD; }
    cout << MOD - Ans << endl;
    return 0;
  }

  // 正:K個 負:0個
  // 正:K-2個 負:2個
  // ・・・
  bool broken = false;
  ll L = 0;
  for (ll i = 0; i + 1 < B.size(); i += 2) {
    L = i;
    if (K - 1 - i < 0 || (ll)(A.size()) <= K - 2 - i)
      continue;
    if (A[K - 1 - i] * A[K - 2 - i] >= B[i] * B[i + 1]) {
      broken = true;
      break;
    }
    // cout << K-1-i MM K-2-i MM i MM i+1 << endl;
  }
  if (!broken) {
    L = (min(K, (ll)B.size()) / 2) * 2;
  }

  // cout << L << endl;

  ll Ans = 1;
  REP(i, K - L) { Ans = (Ans * A[i]) % MOD; }
  REP(i, L) { Ans = (Ans * B[i]) % MOD; }
  cout << Ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {
  ll N, K, a;
  vector<ll> A, B;
  cin >> N >> K;
  REP(i, N) {
    cin >> a;
    if (a >= 0) {
      A.push_back(a);
    } else {
      B.push_back(-a);
    }
  }
  sort(A.begin(), A.end(), greater<>());
  sort(B.begin(), B.end(), greater<>());
  // REP(i,A.size()){cout << A[i] << " ";}cout << endl;
  // REP(i,B.size()){cout << B[i] << " ";}cout << endl;

  // 非負をつくれない:非負要素がないかつKが奇数
  if (A.size() == 0 && K % 2 == 1) {
    ll Ans = 1;
    REP(i, K) { Ans = (Ans * B[B.size() - 1 - i]) % MOD; }
    cout << MOD - Ans << endl;
    return 0;
  }

  // 正:K個 負:0個
  // 正:K-2個 負:2個
  // ・・・
  bool broken = false;
  ll L = 0;
  for (ll i = 0; i + 1 < B.size(); i += 2) {
    L = i;
    if (A[K - 1 - i] * A[K - 2 - i] >= B[i] * B[i + 1]) {
      broken = true;
      break;
    }
  }
  if (!broken) {
    L = (B.size()) / 2 * 2;
  }

  // cout << L << endl;

  ll Ans = 1;
  REP(i, K - L) { Ans = (Ans * A[i]) % MOD; }
  REP(i, L) { Ans = (Ans * B[i]) % MOD; }
  cout << Ans << endl;

  /*
  vector<ll> Ar=A,Br=B;
  sort(A.begin(),A.end(),greater<>());
  sort(B.begin(),B.end(),greater<>());
  sort(Ar.begin(),Ar.end());
  sort(Br.begin(),Br.end());


  REP(i,A.size()-1){
      A[i+1] = (A[i]*A[i+1])%MOD;
  }
  REP(i,B.size()-1){
      B[i+1] = (B[i]*B[i+1])%MOD;
  }
  REP(i,Ar.size()-1){
      Ar[i+1] = (Ar[i]*Ar[i+1])%MOD;
  }
  REP(i,Br.size()-1){
      Br[i+1] = (Br[i]*Br[i+1])%MOD;
  }

  REP(i,A.size()){cout << A[i] << " ";}cout << endl;
  REP(i,B.size()){cout << B[i] << " ";}cout << endl;
  REP(i,Ar.size()){cout << Ar[i] << " ";}cout << endl;
  REP(i,Br.size()){cout << Br[i] << " ";}cout << endl;

  */
  return 0;
}