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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

int main() {
  int 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 ans = 1;
  if (n == k) {
    for (ll el : minus)
      ans = (ans * el) % MOD;
    for (ll el : plus)
      ans = (ans * el) % MOD;
  } else if (plus.empty()) {
    if (k % 2 == 1) {
      sort(minus.rbegin(), minus.rend());
    } else {
      sort(minus.begin(), minus.end());
    }
    rep(i, k) ans = (ans * minus[i]) % MOD;
  } else if (minus.empty()) {
    sort(plus.rbegin(), plus.rend());
    rep(i, k) ans = (ans * plus[i]) % MOD;
  } else {
    sort(plus.rbegin(), plus.rend());
    sort(minus.begin(), minus.end());
    int pItr = 0;
    int mItr = 0;
    vector<ll> s;
    if (k % 2 == 1) {
      s.push_back(plus[pItr]);
      pItr += 1;
      k--;
    }
    while (s.size() < k) {
      bool plusP;
      if (pItr == plus.size()) {
        plusP = false;
      } else if (mItr == minus.size()) {
        plusP = true;
      } else {
        ll p = plus[pItr] * plus[pItr + 1];
        ll m = minus[mItr] * minus[mItr + 1];
        plusP = p > m;
      }
      if (plusP) {
        s.push_back(plus[pItr]);
        s.push_back(plus[pItr + 1]);
        pItr += 2;
      } else {
        s.push_back(minus[mItr]);
        s.push_back(minus[mItr + 1]);
        mItr += 2;
      }
    }
    for (ll el : s) {
      ans = (ans * el) % MOD;
    }
  }
  ans = (ans + MOD) % 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 < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

int main() {
  int 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 ans = 1;
  if (n == k) {
    for (ll el : minus)
      ans = (ans * el) % MOD;
    for (ll el : plus)
      ans = (ans * el) % MOD;
  } else if (plus.empty() && k % 2 == 1) {
    sort(minus.rbegin(), minus.rend());
    rep(i, k) ans = (ans * minus[i]) % MOD;
  } else {
    sort(plus.rbegin(), plus.rend());
    sort(minus.begin(), minus.end());
    int pItr = 0;
    int mItr = 0;
    vector<ll> s;
    while (s.size() < k) {
      if (k - s.size() == 1) {
        s.push_back(plus[pItr]);
        break;
      }
      if (pItr == plus.size()) {
        s.push_back(minus[mItr]);
        s.push_back(minus[mItr + 1]);
        mItr += 2;
      } else if (mItr >= minus.size() - 1) {
        s.push_back(plus[pItr]);
        pItr += 1;
      } else if (pItr == plus.size() - 1) {
        ll p = plus[pItr];
        ll m = minus[mItr] * minus[mItr + 1];
        if (p > m) {
          s.push_back(plus[pItr]);
          pItr += 1;
        } else {
          s.push_back(minus[mItr]);
          s.push_back(minus[mItr + 1]);
          mItr += 2;
        }
      } else {
        ll p = plus[pItr] * plus[pItr + 1];
        ll m = minus[mItr] * minus[mItr + 1];
        if (p > m) {
          s.push_back(plus[pItr]);
          s.push_back(plus[pItr + 1]);
          pItr += 2;
        } else {
          s.push_back(minus[mItr]);
          s.push_back(minus[mItr + 1]);
          mItr += 2;
        }
      }
    }
    for (ll el : s) {
      ans = (ans * el) % MOD;
    }
  }
  ans = (ans + MOD) % MOD;
  cout << ans << endl;

  return 0;
}