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

#include <iostream>

using namespace std;

const int MAXN = 2 * 1E5 + 5;
int arr[MAXN];
const int mode = 1e9 + 7;
int biggestpos = -1, biggestneg = 1;

void merge(int, int, int);
void merge_sort(int, int);
void find(int);

int main() {
  int n, k;
  cin >> n >> k;
  int positivecount = 0;
  int negativecount = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] < 0)
      negativecount++;
    else
      positivecount++;
  }
  merge_sort(0, n - 1);
  int sign = 1;
  long long ans = 1;
  int smallestpos = -1, smallestneg = 1, p = -1, ne = -1;
  for (int i = n - 1; i >= n - k; i--) {
    if (arr[i] < 0) {
      sign *= -1;
      smallestneg = arr[i];
      ne = i;
    } else {
      smallestpos = arr[i];
      p = i;
    }
    ans *= 1ll * (arr[i] % mode);
    ans %= mode;
  }
  if (sign == 1) {
    cout << ans << endl;
    return 0;
  }
  if (n - k - 1 >= 0)
    find(n - k - 1);
  // calculation of swapneg : ie mosbat biarim birun ie manfi biarim tu
  long long swapneg = -1;
  bool x = false;
  if (smallestpos != -1 && biggestneg != 1 && p != -1) {
    x = true;
    // swapneg = smallestpos - biggestneg * -1;
    swapneg = 1ll * smallestneg * biggestneg;
  }
  // calculation of swappos : ie manfi biarim birun ie mosbat bebarim tu
  bool y = false;
  long long swappos = -1;
  if (smallestneg != 1 && biggestpos != -1 && ne != -1) {
    // cout << "i went here" << endl;
    y = true;
    swappos = 1ll * smallestpos * biggestpos;
  }
  ans = 1;
  // nemishe array ro mosbat kard
  if (x == false && y == false) {
    for (int i = 0; i < k; i++) {
      ans *= 1ll * (arr[i] % mode);
      ans %= mode;
    }
    if (ans < 0)
      ans = mode + ans;
    cout << ans << endl;
    return 0;
  }
  // cout << "biggest neg is equal to " << biggestneg << endl;
  // cout << "biggest pos is equal to " << biggestpos << endl;
  // cout << "swapneg is equal to " << swapneg << endl;
  // cout << "swappos is equal to " << swappos << endl;
  // cout << swapneg << endl;
  // cout << swappos << endl;
  if (swapneg >= swappos && p >= 0 && x != false && swapneg != -1) {
    arr[p] = biggestneg;
    // cout << "i went to swapneg < swappos " << endl;
  } else if (swappos > swapneg && ne > -1 && y != false && swappos != -1) {
    arr[ne] = biggestpos;
    // cout << "i went to swappos <= swapneg" << endl;
  } else if (swapneg == -1 && y != false && ne > -1 && swappos != -1) {
    arr[ne] = biggestpos;
  } else if (swappos == -1 && x != false && p > -1 && swapneg != -1) {
    arr[p] = biggestneg;
  }
  for (int i = n - 1; i >= n - k; i--) {
    ans *= 1ll * (arr[i] % mode);
    ans %= mode;
  }
  cout << ans << endl;
  return 0;
}

void find(int n) {
  for (int i = n; i >= 0; i--) {
    if (arr[i] >= 0 && biggestpos == -1)
      biggestpos = arr[i];
    if (arr[i] < 0 && biggestneg == 1)
      biggestneg = arr[i];
    if (biggestpos != -1 && biggestneg != 1)
      break;
  }
  return;
}

int temp[MAXN];
void merge(int l, int mid, int r) {
  int i = l;
  int j = mid + 1;
  int k = l;
  while (i <= mid && j <= r) {
    if (max(arr[i], -1 * arr[i]) <= max(arr[j], -1 * arr[j])) {
      temp[k] = arr[i];
      i++;
    } else {
      temp[k] = arr[j];
      j++;
    }
    k++;
  }
  while (i <= mid) {
    temp[k] = arr[i];
    i++;
    k++;
  }
  while (j <= r) {
    temp[k] = arr[j];
    j++;
    k++;
  }
  for (int i = l; i <= r; i++)
    arr[i] = temp[i];
  return;
}

void merge_sort(int l, int r) {
  if (l < r) {
    int m = (l + r) / 2;
    merge_sort(l, m);
    merge_sort(m + 1, r);
    merge(l, m, r);
  }
  return;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

const int MAXN = 2 * 1E5 + 5;
int arr[MAXN];
const int mode = 1e9 + 7;
int biggestpos = -1, biggestneg = 1;

void merge(int, int, int);
void merge_sort(int, int);
void find(int);

int main() {
  int n, k;
  cin >> n >> k;
  int positivecount = 0;
  int negativecount = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] < 0)
      negativecount++;
    else
      positivecount++;
  }
  merge_sort(0, n - 1);
  int sign = 1;
  long long ans = 1;
  int smallestpos = -1, smallestneg = 1, p = -1, ne = -1;
  for (int i = n - 1; i >= n - k; i--) {
    if (arr[i] < 0) {
      sign *= -1;
      smallestneg = arr[i];
      ne = i;
    } else {
      smallestpos = arr[i];
      p = i;
    }
    ans *= 1ll * (arr[i] % mode);
    ans %= mode;
  }
  if (sign == 1) {
    cout << ans << endl;
    return 0;
  }
  find(n - k - 1);
  long long swapneg = -1;
  bool x = false;
  if (smallestpos != -1 && biggestneg != 1 && p != -1) {
    x = true;
    swapneg = smallestpos / biggestneg * -1;
  }
  long long swappos = -1;
  if (smallestneg != 1 && biggestpos != -1 && ne != -1) {
    x = true;
    swappos = smallestneg / biggestpos * -1;
  }
  ans = 1;
  if (x == false) {
    for (int i = 0; i < k; i++) {
      ans *= 1ll * (arr[i] % mode);
      ans %= mode;
    }
    if (ans < 0)
      ans = mode + ans;
    cout << ans << endl;
    return 0;
  }
  if (swapneg > swappos && p != -1) {
    arr[p] = biggestneg;
  }
  if (swappos >= swapneg && ne != -1)
    arr[ne] = biggestpos;
  for (int i = n - 1; i >= n - k; i--) {
    ans *= 1ll * (arr[i] % mode);
    ans %= mode;
  }
  cout << ans << endl;
  return 0;
}

void find(int n) {
  for (int i = n; i >= 0; i--) {
    if (arr[i] >= 0 && biggestpos == -1)
      biggestpos = arr[i];
    if (arr[i] < 0 && biggestneg == -1)
      biggestneg = arr[i];
    if (biggestpos != -1 and biggestneg != 1)
      break;
  }
  return;
}

int temp[MAXN];
void merge(int l, int mid, int r) {
  int i = l;
  int j = mid + 1;
  int k = l;
  while (i <= mid && j <= r) {
    if (max(arr[i], -1 * arr[i]) <= max(arr[j], -1 * arr[j])) {
      temp[k] = arr[i];
      i++;
    } else {
      temp[k] = arr[j];
      j++;
    }
    k++;
  }
  while (i <= mid) {
    temp[k] = arr[i];
    i++;
    k++;
  }
  while (j <= r) {
    temp[k] = arr[j];
    j++;
    k++;
  }
  for (int i = l; i <= r; i++)
    arr[i] = temp[i];
  return;
}

void merge_sort(int l, int r) {
  if (l < r) {
    int m = (l + r) / 2;
    merge_sort(l, m);
    merge_sort(m + 1, r);
    merge(l, m, r);
  }
  return;
}