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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int M = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  long long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  if (n == k) {
    long long ans = 1;
    for (int i = 0; i < n; i++)
      ans = ans * a[i] % M;
    if (ans < 0)
      ans += M;
    cout << ans << endl;
    return 0;
  }
  if (k == 1) {
    cout << (*max_element(a, a + n) + M) % M << endl;
    return 0;
  }

  int z = 0, pos = 0, neg = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == 0)
      z++;
    else if (a[i] > 0)
      pos++;
    else
      neg++;

  if (z > n - k || (z == n - k && neg % 2 == 1)) {
    cout << "0\\n";
    return 0;
  }

  vector<long long> vp, vn;
  for (int i = 0; i < n; i++)
    if (a[i] < 0)
      vn.push_back(a[i]);
    else if (a[i] > 0)
      vp.push_back(a[i]);
  sort(vp.begin(), vp.end(), greater<int>());
  sort(vn.begin(), vn.end());

  if (pos == 0 && k % 2 == 1) {
    if (z > 0) {
      cout << "0\\n";
    } else {
      sort(vn.begin(), vn.end(), greater<int>());
      long long ans = 1;
      for (int i = 0; i < k; i++)
        ans = ans * vn[i] % M;
      cout << (ans + M) % M << endl;
    }
    return 0;
  }

  if (vn.size() % 2 == 1)
    vn.pop_back();
  int i = 0, j = 0;
  long long ans = 1;
  if (k % 2 == 1 && pos > 0) {
    k--;
    ans = vp[0];
    i = 1;
  }
  if ((vp.size() - i) % 2 == 1)
    vp.pop_back();
  while (k > 0) {
    if (j == vn.size()) {
      ans = ans * vp[i] % M * vp[i + 1] % M;
      i += 2;
    } else if (i == vp.size()) {
      ans = ans * vn[j] % M * vn[j + 1] % M;
      j += 2;
    } else if (vp[i] * vp[i + 1] > vn[j] * vn[j + 1]) {
      ans = ans * vp[i] % M * vp[i + 1] % M;
      i += 2;
    } else {
      ans = ans * vn[j] % M * vn[j + 1] % M;
      j += 2;
    }
    k -= 2;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int M = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  long long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  if (n == k) {
    long long ans = 1;
    for (int i = 0; i < n; i++)
      ans = ans * a[i] % M;
    if (ans < 0)
      ans += M;
    cout << ans << endl;
    return 0;
  }
  if (k == 1) {
    cout << (*max_element(a, a + n) + M) % M << endl;
    return 0;
  }

  int z = 0, pos = 0, neg = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == 0)
      z++;
    else if (a[i] > 0)
      pos++;
    else
      neg++;

  if (z > n - k || (z == n - k && neg % 2 == 1)) {
    cout << "0\\n";
    return 0;
  }

  vector<long long> vp, vn;
  for (int i = 0; i < n; i++)
    if (a[i] < 0)
      vn.push_back(a[i]);
    else if (a[i] > 0)
      vp.push_back(a[i]);
  sort(vp.begin(), vp.end(), greater<int>());
  sort(vn.begin(), vn.end());

  if (pos == 0 && k % 2 == 1) {
    sort(vn.begin(), vn.end(), greater<int>());
    long long ans = 1;
    for (int i = 0; i < k; i++)
      ans = ans * vn[i] % M;
    cout << (ans + M) % M << endl;
    exit(1);
    return 0;
  }

  if (vn.size() % 2 == 1)
    vn.pop_back();
  int i = 0, j = 0;
  long long ans = 1;
  if (k % 2 == 1 && pos > 0) {
    k--;
    ans = vp[0];
    i = 1;
  }
  if ((vp.size() - i) % 2 == 1)
    vp.pop_back();
  while (k > 0) {
    if (j == vn.size()) {
      ans = ans * vp[i] % M * vp[i + 1] % M;
      i += 2;
    } else if (i == vp.size()) {
      ans = ans * vn[j] % M * vn[j + 1] % M;
      j += 2;
    } else if (vp[i] * vp[i + 1] > vn[j] * vn[j + 1]) {
      ans = ans * vp[i] % M * vp[i + 1] % M;
      i += 2;
    } else {
      ans = ans * vn[j] % M * vn[j + 1] % M;
      j += 2;
    }
    k -= 2;
  }

  cout << ans << endl;
}