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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define pb push_back
#define fst first
#define snd second

typedef long long ll;
typedef pair<int, int> pii;
template <typename T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;

const ll MOD = 1e9 + 7;

int N, K;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  vector<ll> pos, neg;
  int C = 0;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x > 0)
      pos.pb(x);
    else if (x < 0)
      neg.pb(-x);
    else
      C++;
  }
  int A = pos.size(), B = neg.size();
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  ll res = 1;
  if (N == K) {
    if (C)
      res = 0;
    else {
      for (ll k : pos)
        res = res * k % MOD;
      for (ll k : neg)
        res = res * k % MOD;
      if (B % 2 == 1)
        res = (MOD - res) % MOD;
    }
    cout << res << '\\n';
    return 0;
  }

  if (A == 0) {
    if (K > B) {
      res = 0;
    } else if (K % 2 == 0) {
      for (int i = B - 1; i >= B - K; i--)
        res = res * neg[i] % MOD;
    } else if (C > 0) {
      res = 0;
    } else {
      for (int i = 0; i < K; i++)
        res = res * neg[i] % MOD;
      res = (MOD - res) % MOD;
    }
    cout << res << '\\n';
    return 0;
  }

  int mx = A + B / 2 * 2;
  if (mx < K) {
    res = 0;
  } else {
    int i = A - 1, j = B - 1;
    if (K % 2 == 1) {
      res = pos[i];
      i--;
      K--;
    }
    while (K >= 2) {
      ll a = 0, b = 0;
      if (i > 0)
        a = pos[i] * pos[i - 1];
      if (j > 0)
        b = neg[j] * neg[j - 1];
      if (a >= b) {
        i -= 2;
        res = (res * (a % MOD)) % MOD;
      } else {
        j -= 2;
        res = (res * (b % MOD)) % MOD;
      }
      K -= 2;
    }
  }

  cout << res << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define pb push_back
#define fst first
#define snd second

typedef long long ll;
typedef pair<int, int> pii;
template <typename T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;

const ll MOD = 1e9 + 7;

int N, K;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  vector<ll> pos, neg;
  int C = 0;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x > 0)
      pos.pb(x);
    else if (x < 0)
      neg.pb(-x);
    else
      C++;
  }
  int A = pos.size(), B = neg.size();
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  ll res = 1;
  if (N == K) {
    if (C)
      res = 0;
    else {
      for (ll k : pos)
        res = res * k % MOD;
      for (ll k : neg)
        res = res * k % MOD;
      if (B % 2 == 1)
        res = (MOD - res) % MOD;
    }
    cout << res << '\\n';
    return 0;
  }

  if (A == 0) {
    if (K > B) {
      res = 0;
    } else if (K % 2 == 0) {
      for (int i = B - 1; i >= B - K; i--)
        res = res * neg[i] % MOD;
    } else if (C > 0) {
      res = 0;
    } else {
      for (int i = 0; i < K; i++)
        res = res * neg[i] % MOD;
      res = (MOD - res) % MOD;
    }
    cout << res << '\\n';
    return 0;
  }

  int mx = A + B / 2 * 2;
  if (mx < K) {
    res = 0;
  } else {
    int i = A - 1, j = B - 1;
    if (K % 2 == 1) {
      res = pos[i];
      i--;
      K--;
    }
    while (K >= 2) {
      ll a = 0, b = 0;
      if (i > 0)
        a = pos[i] * pos[i - 1];
      if (j > 0)
        b = neg[i] * neg[i - 1];
      if (max(a, b) == 0)
        break;
      if (a >= b) {
        i -= 2;
        res = (res * (a % MOD)) % MOD;
      } else {
        j -= 2;
        res = (res * (b % MOD)) % MOD;
      }
      K -= 2;
    }
  }

  cout << res << '\\n';

  return 0;
}