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

#include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;
#define ll long long

#define md 1000000007
#define mx 200005
int arr[mx];

int main() {
  int n, k;
  scanf("%d %d", &n, &k);

  for (int i = 0; i < n; i++) {
    scanf("%d", arr + i);
  }

  sort(arr, arr + n, [](int a, int b) { return abs(a) > abs(b); });

  bool solved = false;

  int fks = 1;
  for (int i = 0; i < k; i++) {
    if (arr[i] < 0)
      fks *= -1;
    if (arr[i] == 0)
      fks *= 0;
  }

  if (fks == 1) {
    solved = true;
    ll prod = 1;
    for (int i = 0; i < k; i++)
      prod = (prod * arr[i]) % md;
    printf("%lld\\n", prod);
  } else if (fks == 0) {
    solved = true;
    printf("0\\n");
  } else {
    int last_neg = 1, last_pos = -1, fst_neg = 1, fst_pos = -1;
    int lpidx, lnidx;

    for (int i = 0; i < k; i++) {
      if (arr[i] > 0) {
        last_pos = arr[i];
        lpidx = i;
      }
      if (arr[i] < 0) {
        last_neg = arr[i];
        lnidx = i;
      }
    }
    for (int i = n - 1; i >= k; i--) {
      if (arr[i] > 0)
        fst_pos = arr[i];
      if (arr[i] < 0)
        fst_neg = arr[i];
    }

    ll prod1 = -1, prod2 = -1;
    if (last_neg < 0 && fst_pos > 0) {
      solved = true;
      prod1 = fst_pos;
      for (int i = 0; i < k; i++)
        if (i != lnidx)
          prod1 = (prod1 * arr[i]) % md;
      assert(prod1 >= 0);
    }
    if (fst_neg < 0 && last_pos > 0) {
      solved = true;
      prod2 = fst_neg;
      for (int i = 0; i < k; i++)
        if (i != lpidx)
          prod2 = (prod2 * arr[i]) % md;
      assert(prod2 >= 0);
    }

    if (prod1 >= 0 and prod2 >= 0) {
      if (fst_pos * (ll)last_pos > fst_neg * (ll)last_neg) {
        printf("%lld\\n", prod1);
      } else {
        printf("%lld\\n", prod2);
      }
    } else if (solved) {
      printf("%lld\\n", max(prod1, prod2));
    }
  }

  if (!solved) {
    if (arr[n - 1] == 0) {
      solved = true;
      printf("0\\n");
    } else {
      solved = true;
      ll prod = 1;
      for (int i = 0; i < k; i++)
        prod = (prod * arr[n - 1 - i]) % md;
      if (prod < 0)
        prod = prod + md;
      printf("%lld\\n", prod);
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;
#define ll long long

#define md 1000000007
#define mx 200005
int arr[mx];

int main() {
  int n, k;
  scanf("%d %d", &n, &k);

  for (int i = 0; i < n; i++) {
    scanf("%d", arr + i);
  }

  sort(arr, arr + n, [](int a, int b) { return abs(a) > abs(b); });

  bool solved = false;

  int fks = 1;
  for (int i = 0; i < k; i++) {
    if (arr[i] < 0)
      fks *= -1;
    if (arr[i] == 0)
      fks *= 0;
  }

  if (fks == 1) {
    solved = true;
    ll prod = 1;
    for (int i = 0; i < k; i++)
      prod = (prod * arr[i]) % md;
    printf("%lld\\n", prod);
  } else if (fks == 0) {
    solved = true;
    printf("0\\n");
  } else {
    int last_neg = 1, last_pos = -1, fst_neg = 1, fst_pos = -1;
    int lpidx, lnidx;

    for (int i = 0; i < k; i++) {
      if (arr[i] > 0) {
        last_pos = arr[i];
        lpidx = i;
      }
      if (arr[i] < 0) {
        last_neg = arr[i];
        lnidx = i;
      }
    }
    for (int i = n - 1; i >= k; i--) {
      if (arr[i] > 0)
        fst_pos = arr[i];
      if (arr[i] < 0)
        fst_neg = arr[i];
    }

    ll prod1 = -1, prod2 = -1;
    if (last_neg < 0 && fst_pos > 0) {
      solved = true;
      prod1 = fst_pos;
      for (int i = 0; i < k; i++)
        if (i != lnidx)
          prod1 = (prod1 * arr[i]) % md;
    }
    if (fst_neg < 0 && last_pos > 0) {
      solved = true;
      prod2 = fst_neg;
      for (int i = 0; i < k; i++)
        if (i != lpidx)
          prod2 = (prod2 * arr[i]) % md;
    }

    if (prod1 >= 0 and prod2 >= 0) {
      assert(false);
      if (fst_pos * (ll)last_pos > fst_neg * (ll)last_neg) {
        printf("%lld\\n", prod1);
      } else {
        printf("%lld\\n", prod2);
      }
    } else if (solved) {
      printf("%lld\\n", max(prod1, prod2));
    }
  }

  if (!solved) {
    if (arr[n - 1] == 0) {
      solved = true;
      printf("0\\n");
    } else {
      solved = true;
      ll prod = 1;
      for (int i = 0; i < k; i++)
        prod = (prod * arr[n - 1 - i]) % md;
      if (prod < 0)
        prod = prod + md;
      printf("%lld\\n", prod);
    }
  }
}