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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll mod = 1000000007;

int main() {
  /*
  ifstream in("hand_01.txt");
  cin.rdbuf(in.rdbuf());
  */

  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  ll ans = 1;
  if (N == K) {
    rep(i, N) {
      ans *= ((A[i] + mod) % mod);
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  int noz = 0;
  int z = 0;
  int ppp = 0;
  int mmm = 0;
  rep(i, N) {
    if (A[i] != 0)
      noz++;
    if (A[i] == 0)
      z++;
    if (A[i] > 0)
      ppp++;
    if (A[i] < 0)
      mmm++;
  }
  if (noz < K) {
    cout << 0 << endl;
    return 0;
  }
  if (z && noz == K && mmm % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (z && ppp == 0 && K % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> B;
  rep(i, N) if (A[i] != 0) B.pb(A[i]);
  N = sz(B);
  vector<ll> pls, mns;
  rep(i, N) {
    if (B[i] > 0)
      pls.pb(B[i]);
    else
      mns.pb(B[i]);
  }

  if (ppp == 0 && K % 2 == 1) {
    sort(rall(mns));
    rep(i, K) {
      ans *= (mns[i] + mod) % mod;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }

  sort(rall(pls));
  sort(all(mns));
  pls.pb(0);
  pls.pb(0);
  mns.pb(0);
  mns.pb(0);
  mns.pb(0);
  int idp = 0, idm = 0;
  rep(i, K) {
    if (pls[idp] != 0 && pls[idp] * pls[idp + 1] >= mns[idm] * mns[idm + 1] ||
        i == K - 1) {
      ans *= pls[idp];
      ans %= mod;
      idp++;
    } else {
      ans *= (mns[idm] * mns[idm + 1] % mod);
      ans %= mod;
      idm += 2;
      i++;
    }
  }

  cout << ans % mod << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll mod = 1000000007;

int main() {

  ifstream in("hand_01.txt");
  cin.rdbuf(in.rdbuf());

  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  ll ans = 1;
  if (N == K) {
    rep(i, N) {
      ans *= ((A[i] + mod) % mod);
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  int noz = 0;
  int z = 0;
  int ppp = 0;
  int mmm = 0;
  rep(i, N) {
    if (A[i] != 0)
      noz++;
    if (A[i] == 0)
      z++;
    if (A[i] > 0)
      ppp++;
    if (A[i] < 0)
      mmm++;
  }
  if (noz < K) {
    cout << 0 << endl;
    return 0;
  }
  if (z && noz == K && mmm % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (z && ppp == 0 && K % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> B;
  rep(i, N) if (A[i] != 0) B.pb(A[i]);
  N = sz(B);
  vector<ll> pls, mns;
  rep(i, N) {
    if (B[i] > 0)
      pls.pb(B[i]);
    else
      mns.pb(B[i]);
  }

  if (ppp == 0 && K % 2 == 1) {
    sort(rall(mns));
    rep(i, K) {
      ans *= (mns[i] + mod) % mod;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }

  sort(rall(pls));
  sort(all(mns));
  pls.pb(0);
  pls.pb(0);
  mns.pb(0);
  mns.pb(0);
  mns.pb(0);
  int idp = 0, idm = 0;
  rep(i, K) {
    if (pls[idp] != 0 && pls[idp] * pls[idp + 1] >= mns[idm] * mns[idm + 1] ||
        i == K - 1) {
      ans *= pls[idp];
      ans %= mod;
      idp++;
    } else {
      ans *= (mns[idm] * mns[idm + 1] % mod);
      ans %= mod;
      idm += 2;
      i++;
    }
  }

  cout << ans % mod << endl;
  return 0;
}