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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const ll mod = 1e9 + 7;
int main() {
  FASTINOUT;
  int x, k, ng = 0;
  cin >> x >> k;
  ll y[x];
  for (int i = 0; i < x; i++) {
    cin >> y[i];
    if (y[i] < 0)
      ng++;
  }
  sort(y, y + x);
  ll ans = 1;
  if (x == k) {
    for (int i = 0; i < x; i++) {
      ans = ((ans * y[i]) % mod + mod) % mod;
    }
    cout << ans;
    return 0;
  }
  if (ng == x) {
    if (k & 1) {
      reverse(y, y + x);
      for (int i = 0; i < k; i++) {
        ans = ((ans * y[i]) % mod + mod) % mod;
      }
    } else {
      for (int i = 0; i < k; i++) {
        ans = ((ans * y[i]) % mod + mod) % mod;
      }
    }
    cout << ans;
    return 0;
  }
  vector<ll> pos, neg;
  for (int i = 0; i < x; i++) {
    if (y[i] >= 0)
      pos.push_back(y[i]);
    else
      neg.push_back(y[i]);
  }
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end());
  reverse(neg.begin(), neg.end());
  int p = pos.size(), n = neg.size();
  if (k & 1)
    ans = pos[--p], k--;
  while (k > 1 && (n > 1 || p > 1)) {
    ll mp = 0, mn = 0;
    if (p > 1)
      mp = pos[p - 1] * pos[p - 2];
    if (n > 1)
      mn = neg[n - 1] * neg[n - 2];
    if (mp > mn) {
      p -= 2;
      mp = mp % mod;
      ans = (ans * mp) % mod;
    } else {
      n -= 2;
      mn = mn % mod;
      ans = (ans * mn) % mod;
    }
    k -= 2;
  }
  while (k--) {
    if (p)
      ans = ans * pos[--p] % mod;
    else
      ans = ((ans * neg[--n]) % mod + mod) % mod;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const ll mod = 1e9 + 7;
int main() {
  FASTINOUT;
  int x, k, ng = 0;
  cin >> x >> k;
  ll y[x];
  vector<ll> pos, neg;
  for (int i = 0; i < x; i++) {
    cin >> y[i];
    if (y[i] >= 0)
      pos.push_back(y[i]);
    else
      neg.push_back(y[i]);
  }
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end());
  reverse(neg.begin(), neg.end());
  int p = pos.size(), n = neg.size();
  ll ans = 1;
  if (n == x) {
    if (k & 1) {
      for (int i = 0; i < k - 1; i++)
        ans = (ans * abs(neg[i])) % mod;
      ans *= neg[k - 1];
      ans %= mod;
      if (ans < 0)
        ans += mod;
    } else {
      while (k--) {
        ans = (ans * abs(neg.back())) % mod;
        neg.pop_back();
      }
    }
    cout << ans;
    return 0;
  }
  if (n == 0) {
    while (k--) {
      ans = (ans * pos.back()) % mod;
      pos.pop_back();
    }
    cout << ans;
    return 0;
  }
  if (k & 1)
    ans = pos[--p], k--;
  while (k > 1 && (n > 1 || p > 1)) {
    ll mp = 0, mn = 0;
    if (p > 1)
      mp = pos[p - 1] * pos[p - 2];
    if (n > 1)
      mn = neg[n - 1] * neg[p - 2];
    if (mp > mn) {
      p -= 2;
      mp = mp % mod;
      ans = (ans * mp) % mod;
    } else {
      n -= 2;
      mn = mn % mod;
      ans = (ans * mn) % mod;
    }
    k -= 2;
  }
  while (k--) {
    if (p)
      ans = ans * pos[--p] % mod;
    else
      ans = ((ans * neg[--n]) % mod + mod) % mod;
  }
  cout << ans;
  return 0;
}