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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
ll bg = 1000000007;
ll ans(ll A[], ll n, ll k) {
  // sorting given input array
  sort(A, A + n);

  // variable to store final product of all element
  // of sub-sequence of size k
  ll product = 1;

  // CASE I
  // If max element is 0 and
  // k is odd then max product will be 0
  if (A[n - 1] == 0 && (k & 1))
    return 0;

  // CASE II
  // If all elements are negative and
  // k is odd then max product will be
  // product of rightmost-subarray of size k
  if (A[n - 1] <= 0 && (k & 1)) {
    for (ll i = n - 1; i >= n - k; i--)
      product = (bg + (product * A[i]) % bg) % bg;
    return product;
  }

  // else
  // i is current left pointer index
  ll i = 0;

  // j is current right pointer index
  ll j = n - 1;

  // CASE III
  // if k is odd and rightmost element in
  // sorted array is positive then it
  // must come in subsequence
  // Multiplying A[j] with product and
  // correspondingly changing j
  if (k & 1) {
    product = ((product * A[j]) % bg + bg) % bg;
    j--;
    k--;
  }

  // CASE IV
  // Now k is even
  // Now we deal with pairs
  // Each time a pair is multiplied to product
  // ie.. two elements are added to subsequence each time
  // Effectively k becomes half
  // Hence, k >> 1 means k /= 2
  k = k / 2;

  // Now finding k corresponding pairs
  // to get maximum possible value of product
  for (ll itr = 0; itr < k; itr++) {

    // product from left pointers
    ll left_product = ((A[i] * A[i + 1]) % bg + bg) % bg;

    // product from right pointers
    ll right_product = ((A[j] * A[j - 1]) % bg + bg) % bg;
    ll l1 = (A[i] * A[i + 1]), r1 = (A[j] * A[j - 1]);
    // Taking the max product from two choices
    // Correspondingly changing the pointer's position
    if (l1 >= r1) {
      product = ((product * left_product) % bg + bg) % bg;
      i += 2;
    } else {
      product = ((product * right_product) % bg + bg) % bg;
      j -= 2;
    }
  }

  // Finally return product
  return product;
}
int main() {
  ll t;
  t = 1;
  while (t--) {
    ll k, i, j, n, m;
    cin >> n >> k;
    ll arr[n];
    for (i = 0; i < n; i++)
      cin >> arr[i];
    ll fin = ans(arr, n, k);
    cout << fin << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
ll bg = 1000000007;
ll ans(ll A[], ll n, ll k) {
  // sorting given input array
  sort(A, A + n);

  // variable to store final product of all element
  // of sub-sequence of size k
  ll product = 1;

  // CASE I
  // If max element is 0 and
  // k is odd then max product will be 0
  if (A[n - 1] == 0 && (k & 1))
    return 0;

  // CASE II
  // If all elements are negative and
  // k is odd then max product will be
  // product of rightmost-subarray of size k
  if (A[n - 1] <= 0 && (k & 1)) {
    for (ll i = n - 1; i >= n - k; i--)
      product = (bg + (product * A[i]) % bg) % bg;
    return product;
  }

  // else
  // i is current left pointer index
  ll i = 0;

  // j is current right pointer index
  ll j = n - 1;

  // CASE III
  // if k is odd and rightmost element in
  // sorted array is positive then it
  // must come in subsequence
  // Multiplying A[j] with product and
  // correspondingly changing j
  if (k & 1) {
    product = ((product * A[j]) % bg + bg) % bg;
    j--;
    k--;
  }

  // CASE IV
  // Now k is even
  // Now we deal with pairs
  // Each time a pair is multiplied to product
  // ie.. two elements are added to subsequence each time
  // Effectively k becomes half
  // Hence, k >> 1 means k /= 2
  k = k / 2;

  // Now finding k corresponding pairs
  // to get maximum possible value of product
  for (ll itr = 0; itr < k; itr++) {

    // product from left pointers
    ll left_product = ((A[i] * A[i + 1]) % bg + bg) % bg;

    // product from right pointers
    ll right_product = ((A[j] * A[j - 1]) % bg + bg) % bg;

    // Taking the max product from two choices
    // Correspondingly changing the pointer's position
    if (left_product >= right_product) {
      product = ((product * left_product) % bg + bg) % bg;
      i += 2;
    } else {
      product = ((product * right_product) % bg + bg) % bg;
      j -= 2;
    }
  }

  // Finally return product
  return product;
}
int main() {
  ll t;
  cin >> t;
  while (t--) {
    ll k, i, j, n, m;
    cin >> n >> k;
    ll arr[n];
    for (i = 0; i < n; i++)
      cin >> arr[i];
    ll fin = ans(arr, n, k);
    cout << fin << endl;
  }
  return 0;
}