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

#include <bits/stdc++.h>
#define ll long long int
#define forr(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
#define oo 1000000000000000000LL
using namespace std;

void _sol() {
  int n, k;
  cin >> n >> k;
  vector<ll> v(n);
  forr(i, n) cin >> v[i];
  sort(v.begin(), v.end());
  ll ans = 1;
  n--;
  if (k % 2) {
    if (v[n] <= 0) {
      int p = n;
      while (k > 0) {
        ans = (ans * v[p]) % mod;
        if (ans < 0)
          ans += mod;
        p--;
        k--;
      }
      cout << ans;
      return;
    }
    ans = (ans * v[n]) % mod;
    if (ans < 0)
      ans += mod;
    k--;
    n--;
  }
  int i = 0;
  while (k > 0) {
    ll p = v[i] * v[i + 1], q = v[n] * v[n - 1];
    if (p > q) {
      ans = (ans * (p % mod)) % mod;
      i += 2;
    } else {
      ans = (ans * (q % mod)) % mod;
      n -= 2;
    }
    k -= 2;
  }
  cout << (ans + mod) % mod;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin >> t;
  while (t--)
    _sol();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define forr(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
#define oo 1000000000000000000LL
using namespace std;

ll getP(vector<ll> &v) {
  ll ans = 1;
  forr(i, v.size()) { ans = (ans * v[i]) % mod; }
  return ans;
}

bool cmp(ll a, ll b) {
  if (abs(a) == abs(b)) {
    return a > 0;
  }
  return abs(a) >= abs(b);
}

void _sol() {
  int n, k;
  cin >> n >> k;
  vector<ll> v;
  int zero = 0, neg = 0;
  forr(i, n) {
    ll x;
    cin >> x;
    v.push_back(x);
    if (x == 0)
      zero++;
    if (x < 0)
      neg++;
  }
  sort(v.begin(), v.end(), cmp);
  ll ans = 1LL;
  forr(i, k) {
    ans = (ans * v[i]) % mod;
    if (v[i] == 0) {
      cout << 0;
      return;
    }
  }
  if (ans >= 0 || k == n) {
    cout << ans;
  } else if (neg == n) {
    if (k % 2) {
      ll ans = -1;
      for (int i = n - 1; i >= 0 && k > 0; i--, k--) {
        ans = (ans * abs(v[i])) % mod;
      }
      if (ans < 0)
        ans += mod;
      cout << ans;
      return;
    } else {

      ll ans = 1;
      for (int i = 0; i < n && k > 0; i++, k--) {
        ans = (ans * abs(v[i])) % mod;
      }
      cout << ans;
      return;
    }
  } else {
    vector<ll> a, b;
    forr(i, k) {
      if (v[i] > 0)
        a.push_back(v[i]);
      else if (v[i] < 0)
        b.push_back(abs(v[i]));
    }
    if (b.size() == n) {
      if (k % 2) {
        ans = -1LL;
        reverse(b.begin(), b.end());
        forr(i, k) { ans = (ans * b[i]) % mod; }
        if (ans < 0)
          ans += mod;
        cout << ans;
      } else {
        ans = 1LL;
        forr(i, k) { ans = (ans * b[i]) % mod; }
        cout << ans;
      }
    } else {
      if (b.size() % 2) {
        ll ans = -1e12;
        ll removeNeg = b.back(), addPos = -1;
        for (int j = k; j < n; j++) {
          if (v[j] > 0) {
            addPos = v[j];
            break;
          }
        }
        ll removePos, addNeg = -1;
        if (a.size() > 0)
          removePos = a.back();
        else
          removePos = -1;
        for (int j = k; j < n && a.size() > 0; j++) {
          if (v[j] < 0) {
            addNeg = v[j];
            break;
          }
        }
        if (removePos != -1 && addNeg != -1 && removeNeg != -1 &&
            addPos != -1) {
          if (addPos * removePos > addNeg * removeNeg) {
            b.pop_back();
            a.push_back(addPos);
            ans = max(ans, (getP(a) * getP(b)) % mod);
          } else {
            a.pop_back();
            b.push_back(addNeg);
            ans = max(ans, (getP(a) * getP(b)) % mod);
          }
        } else if (removePos != -1 && addNeg != -1) {
          a.pop_back();
          b.push_back(addNeg);
          ans = max(ans, (getP(a) * getP(b)) % mod);
        } else if (removeNeg != -1 && addPos != -1) {
          b.pop_back();
          a.push_back(addPos);
          ans = max(ans, (getP(a) * getP(b)) % mod);

        } else if (zero > 0) {
          ans = 0;
        }
        cout << ans;

      } else {
        cout << (getP(a) * getP(b)) % mod;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin >> t;
  while (t--)
    _sol();
}