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 REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> plus;
  vector<ll> minus;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a < 0) {
      minus.push_back(a);
    } else {
      plus.push_back(a);
    }
  }
  ll S = plus.size();
  ll T = minus.size();
  ll ans = 1;
  if (n == k) {
    rep(i, S) {
      ans *= plus[i];
      ans %= MOD;
    }
    rep(i, T) {
      ans *= minus[i];
      ans %= MOD;
    }
    if (ans < 0) {
      cout << ans + MOD << endl;
    } else {
      cout << ans << endl;
    }
  } else if (S == 0 && k % 2 == 1) {
    vector<ll> temp;
    rep(i, S) temp.push_back(plus[i]);
    rep(i, T) temp.push_back(-minus[i]);
    sort(temp.begin(), temp.end());
    rep(i, k) {
      ans *= temp[i];
      ans %= MOD;
    }
    cout << (-ans % MOD) + MOD << endl;
  } else {
    sort(plus.begin(), plus.end());
    sort(minus.rbegin(), minus.rend());
    vector<ll> temp;
    if (k % 2 == 1) {
      ans *= plus.back();
      plus.pop_back();
    }
    while (plus.size() >= 2) {
      ll x = plus.back();
      plus.pop_back();
      x *= plus.back();
      plus.pop_back();
      temp.push_back(x);
    }
    while (minus.size() >= 2) {
      ll x = minus.back();
      minus.pop_back();
      x *= minus.back();
      minus.pop_back();
      temp.push_back(x);
    }
    sort(temp.rbegin(), temp.rend());
    rep(i, k / 2) {
      ans *= temp[i] % MOD;
      ans %= MOD;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> plus;
  vector<ll> minus;
  bool zero = false;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a > 0)
      plus.push_back(a);
    if (a == 0)
      zero = true;
    if (a < 0)
      minus.push_back(a);
  }
  // 答えが負か0になる場合を先に処理しておく
  if (plus.size() < k && (k - plus.size()) % 2 == 1) {
    if (zero) {
      cout << 0 << endl;
    } else {
      ll ans = 1;
      vector<ll> temp;
      rep(i, plus.size()) temp.push_back(plus[i]);
      rep(i, minus.size()) temp.push_back(-minus[i]);
      sort(temp.begin(), temp.end());
      rep(i, k) {
        ans *= temp[i];
        ans %= MOD;
      }
      cout << -ans + MOD << endl;
      return 0;
    }
  }
  ll ans = 1;
  int count = 0;
  sort(plus.rbegin(), plus.rend());
  sort(minus.begin(), minus.end());
  int idp = 0;
  int idm = 0;
  while (count < k) {
    if (count == k - 1) {
      ans *= plus[idp];
      ans %= MOD;
      break;
    }
    if (idp >= plus.size() && idm >= minus.size()) {
      cout << 0 << endl;
      return 0;
    } else if (idm + 1 >= minus.size()) {
      ans *= plus[idp];
      ans %= MOD;
      idp++;
      count++;
    } else if (idp >= plus.size()) {
      ans *= -minus[idm + 1] % MOD;
      ans %= MOD;
      ans *= -minus[idm] % MOD;
      ans %= MOD;
      idm += 2;
      count += 2;
    } else if (plus[idp] * plus[idp + 1] > plus[idm] * plus[idm + 1]) {
      ans *= plus[idp];
      ans %= MOD;
      idp++;
      count++;
    } else {
      ans *= -minus[idm + 1] % MOD;
      ans %= MOD;
      ans *= -minus[idm] % MOD;
      ans %= MOD;
      idm += 2;
      count += 2;
    }
  }
  cout << ans << endl;
  return 0;
}