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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N = 1e9 + 7;
ll maxProductSubarrayOfSizeK(ll A[], ll n, ll k) {
  sort(A, A + n);

  ll product = 1;

  if (A[n - 1] == 0 && (k & 1))
    return 0;

  if (A[n - 1] <= 0 && (k & 1)) {
    for (ll i = n - 1; i >= n - k; i--) {
      product = (product * A[i]) % N;
      product = (product + N) % N;
    }
    return product;
  }
  ll i = 0;

  ll j = n - 1;

  if (k & 1) {
    product = (product * A[j]) % N;
    product = (product + N) % N;
    j--;
    k--;
  }

  k >>= 1;
  for (ll itr = 0; itr < k; itr++) {
    // cout<<i<<" "<<product<<endl;
    ll left_product = A[i] * A[i + 1];

    ll right_product = A[j] * A[j - 1];

    if (left_product > right_product) {
      product = (product * A[i]) % N;
      product = (product * A[i + 1]) % N;
      product = (product + N) % N;
      i += 2;
    } else {
      product = (product * A[j]) % N;
      product = (product * A[j - 1]) % N;
      product = (product + N) % N;
      j -= 2;
    }
  }

  return product;
}

int main() {
  ll a[200009];
  ll i, j, k, n, m, x;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << maxProductSubarrayOfSizeK(a, n, k);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cout << (#x) << " is " << x << endl;
#define Q                                                                      \\
  int t;                                                                       \\
  scanf("%d", &t);                                                             \\
  for (int q = 1; q <= t; q++)
typedef long long int lli;
typedef pair<int, int> pi;

int main() {
  lli mod = 1e9 + 7;
  int n, k, pos = 0, zero = 0, neg = 0;
  cin >> n >> k;
  lli a[n];
  vector<lli> po, ne;
  for (auto &x : a) {
    cin >> x;
    if (x < 0) {
      neg++;
      ne.push_back(x);
    } else if (x > 0) {
      pos++;
      po.push_back(x);
    } else
      zero++;
  }
  bool fp, fz, fn;
  fp = fz = fn = false;
  if (neg + pos > k) {
    if (pos)
      fp = true;
    else {
      if (k % 2) {
        if (zero)
          fz = true;
        else
          fn = true;
      } else
        fp = true;
    }
  } else if (neg + pos == k) {
    if (neg % 2) {
      if (zero)
        fz = true;
      else
        fn = true;
    } else
      fp = true;
  } else
    fz = true;

  if (fz) {
    cout << "0\\n";
  } else if (fp) {
    sort(po.begin(), po.end(), greater<lli>());
    sort(ne.begin(), ne.end());
    lli ans = 1;
    int i = 0, j = 0, pn = pos, nn = neg;
    while (k) {
      if (pn - i >= 2 && nn - j >= 2) {
        lli sp = po[i] * po[i + 1];
        lli sn = ne[j] * ne[j + 1];
        if (sp > sn) {
          ans = (ans * po[i]) % mod;
          ans = (ans * po[i + 1]) % mod;
          i += 2;
          k -= 2;
        } else {
          ans = (ans * ne[j]) % mod;
          ans = (ans * ne[j + 1]) % mod;
          j += 2;
          k -= 2;
        }
      } else if (nn - j < 2) {
        ans = (ans * po[i]) % mod;
        i++;
        k--;
      } else if (pn - i < 2) {
        if (k > 1) {
          ans = (ans * ne[j]) % mod;
          ans = (ans * ne[j + 1]) % mod;
          j += 2;
          k -= 2;
        } else {
          ans = (ans * po[i]) % mod;
          i++;
          k--;
        }
      }
    }
    cout << ans << endl;
  } else {
    sort(po.begin(), po.end());
    sort(ne.begin(), ne.end(), greater<lli>());
    lli ans = 1;
    int i = 0, j = 0, pn = pos, nn = neg;
    while (k) {
      if (i < pn && j < nn) {
        if (po[i] > -ne[j]) {
          ans = ((ans * ne[j]) % mod + mod) % mod;
          j++;
          k--;
        } else {
          ans = ((ans * po[i]) % mod + mod) % mod;
          i++;
          k--;
        }
      } else if (i < pn) {
        ans = ((ans * po[i]) % mod + mod) % mod;
        i++;
        k--;
      } else {
        ans = ((ans * ne[j]) % mod + mod) % mod;
        j++;
        k--;
      }
    }
    cout << ans << endl;
  }

  return 0;
}