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

#include <bits/stdc++.h>
using namespace std;
#define ll long long

const ll MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll ans = LLONG_MIN, pro = 1;
  if (n == k) {
    for (int i = 0; i < n; i++) {
      pro *= a[i];
      pro %= MOD;
    }
    cout << ((pro + MOD) % MOD);
    return 0;
  }
  if (*max_element(a, a + n) < 0) {
    if (k & 1)
      sort(a, a + n, greater<ll>());
    else
      sort(a, a + n);
    for (int i = 0; i < k; i++) {
      pro *= a[i];
      pro %= MOD;
    }
    cout << ((pro + MOD) % MOD);
    return 0;
  }

  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.begin(), neg.end());

  vector<ll> pospre, negpre;
  pospre.push_back(1);
  for (int i = 0; i < pos.size(); i++) {
    pospre.push_back((pos[i] * pospre.back()) % MOD);
  }
  negpre.push_back(1);
  for (int i = 0; i < neg.size(); i++) {
    negpre.push_back((neg[i] * negpre.back()) % MOD);
  }

  int pidx = min((int)pos.size(), k);
  int nidx = k - pidx;
  if (nidx & 1) {
    ++nidx;
    --pidx;
  }
  while (nidx + 2 <= neg.size() && pidx - 2 >= 0 &&
         (pos[pidx - 1] * pos[pidx - 2] < neg[nidx + 1] * neg[nidx])) {
    nidx += 2;
    pidx -= 2;
  }
  ans = (pospre[pidx] * negpre[nidx]) % MOD;
  ans += MOD;
  ans %= MOD;

  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long

const ll MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll ans = LLONG_MIN, pro = 1;
  if (n == k) {
    for (int i = 0; i < n; i++) {
      pro *= a[i];
      pro += MOD;
      pro %= MOD;
    }
    cout << pro;
    return 0;
  }
  if (*max_element(a, a + n) < 0) {
    if (k & 1)
      sort(a, a + n, greater<ll>());
    else
      sort(a, a + n);
    for (int i = 0; i < k; i++) {
      pro *= a[i];
      pro += MOD;
      pro %= MOD;
    }
    cout << pro;
    return 0;
  }

  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(pos.rbegin(), pos.rend());
  sort(neg.begin(), neg.end());

  vector<ll> pospre, negpre;
  pospre.push_back(1);
  for (int i = 0; i < pos.size(); i++) {
    pospre.push_back((pos[i] * pospre.back()) % MOD);
  }
  negpre.push_back(1);
  for (int i = 0; i < neg.size(); i++) {
    negpre.push_back((neg[i] * negpre.back()) % MOD);
  }

  int pidx = max((int)pos.size(), k);
  int nidx = k - pidx;
  if (nidx & 1) {
    ++nidx;
    --pidx;
  }
  // cout << "nidx = " << nidx << " " << "pidx = " << pidx << endl;
  // cout << "ans = " << ans << endl;
  // ans = max(ans, (pospre[pidx]*negpre[nidx])%MOD);
  // cout << "ans = " << ans << endl;
  while (nidx + 2 <= neg.size() && pidx - 2 >= 0 &&
         pos[pidx - 1] * pos[pidx - 2] > neg[nidx + 1] * neg[nidx + 2]) {
    nidx += 2;
    pidx -= 2;
    // cout << "nidx = " << nidx << " pidx = " << pidx << endl;
  }
  ans = (pospre[pidx] * negpre[nidx]) % MOD;
  ans %= MOD;
  ans += MOD;
  ans %= MOD;

  cout << ans;
}