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

#include <algorithm>
#include <iostream>
#include <vector>
#define vi vector<int>
#define ll long long
#define pub push_back

const ll md = 1e9 + 7;

using namespace std;

int n, k, zero = 0;
vector<ll> pos;
vector<ll> neg;
ll res = 1;

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0) {
      pos.push_back(x);
    } else if (x < 0) {
      neg.push_back(-x);
    } else {
      zero++;
    }
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end(), greater<ll>());

  int ps = pos.size(), ns = neg.size();

  if (ps >= k) {
    int ptr = k - 1, i;
    for (i = 0; i + 1 < ns; i += 2) {
      if (ptr > 0 && neg[i] * neg[i + 1] > pos[ptr] * pos[ptr - 1]) {
        ptr -= 2;
      } else {
        break;
      }
    }
    for (int j = 0; j < i; j++) {
      res = (res * neg[j]) % md;
    }
    for (int j = 0; j <= ptr; j++) {
      res = (res * pos[j]) % md;
    }
  }

  else if (ps < k && (k % 2 == 0 || ps) &&
           ns + ps - ((ps % 2) ^ (k % 2)) >= k) {
    int ptr = ps - 1 - ((ps % 2) ^ (k % 2)),
        i = k - (ps - ((ps % 2) ^ (k % 2)));
    for (; i + 1 < ns; i += 2) {
      if (ptr > 0 && neg[i] * neg[i + 1] > pos[ptr] * pos[ptr - 1]) {
        ptr -= 2;
      } else {
        break;
      }
    }
    for (int j = 0; j < i; j++) {
      res = (res * neg[j]) % md;
    }
    for (int j = 0; j <= ptr; j++) {
      res = (res * pos[j]) % md;
    }
  }

  else if (zero) {
    res = 0;
  }

  else {
    int L = ps - 1, R = ns - 1;
    for (int i = 0; i < k; i++) {
      int val = md, btr = 0;
      if (L >= 0) {
        val = pos[L];
      }
      if (R >= 0 && neg[R] < val) {
        val = neg[R];
        btr = 1;
      }
      if (!btr) {
        res = (res * pos[L--]) % md;
      } else {
        res = (res * neg[R--]) % md;
      }
    }
    res = (md - res) % md;
  }

  cout << res << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define vi vector<int>
#define ll long long
#define pub push_back

const ll md = 1e9 + 7;

using namespace std;

int n, k, zero = 0;
vector<ll> pos;
vector<ll> neg;
ll res = 1;

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0) {
      pos.push_back(x);
    } else if (x < 0) {
      neg.push_back(-x);
    } else {
      zero++;
    }
  }
  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end(), greater<ll>());

  int ps = pos.size(), ns = neg.size();

  if (ps >= k) {
    int ptr = k - 1, i;
    for (i = 0; i + 1 < ns; i += 2) {
      if (ptr > 0 && neg[i] * neg[i + 1] > pos[ptr] * pos[ptr - 1]) {
        ptr -= 2;
      } else {
        break;
      }
    }
    for (int j = 0; j < i; j++) {
      res = (res * neg[j]) % md;
    }
    for (int j = 0; j <= ptr; j++) {
      res = (res * pos[j]) % md;
    }
  }

  else if (ps < k && (k % 2 == 0 || ps) &&
           ns + ps - ((ps % 2) ^ (k % 2)) >= k) {
    int ptr = ps - 1, i = 2 * (k - (ps - ((ps % 2) ^ (k % 2))));
    if ((ps % 2) ^ (k % 2)) {
      ps--;
    }
    for (; i + 1 < ns; i += 2) {
      if (ptr > 0 && neg[i] * neg[i + 1] > pos[ptr] * pos[ptr - 1]) {
        ptr -= 2;
      } else {
        break;
      }
    }
    for (int j = 0; j < i; j++) {
      res = (res * neg[j]) % md;
    }
    for (int j = 0; j <= ptr; j++) {
      res = (res * pos[j]) % md;
    }
  }

  else if (zero) {
    res = 0;
  }

  else {
    for (int i = ns - 1; i >= ns - k; i--) {
      res = (res * neg[i]) % md;
    }
    res = (md - res) % md;
  }

  cout << res << "\\n";
  return 0;
}