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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef vector<int> vi;
typedef unsigned long ul;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<int> si;
typedef multiset<int> mi;

const ll INF = 1e18;
const int MOD = 1e9 + 7;

bool comp(int a, int b) { return abs(a) > abs(b); }

int main() {

  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  // ofstream cout ("fileName.out");
  // ifstream cin ("fileName.in");

  int N, K;
  cin >> N >> K;
  vl pos, neg;

  for (int i = 0; i < N; ++i) {
    ll n;
    cin >> n;
    if (n >= 0)
      pos.pb(n);
    else
      neg.pb(n);
  }

  ll ans = 1;

  if (K == N) {
    for (auto x : pos)
      ans = (ans * (x % MOD)) % MOD;
    for (auto x : neg)
      ans = (ans * (x % MOD)) % MOD;
    if (ans < 0)
      ans += MOD;
    cout << ans << "\\n";
    return 0;
  }

  if (neg.size() == N && K % 2 == 1) {
    sort(neg.rbegin(), neg.rend());
    for (int i = 0; i < K; ++i)
      ans = (ans * (neg[i] % MOD)) % MOD;
    ans += MOD;
    cout << ans << "\\n";
    return 0;
  }

  sort(pos.begin(), pos.end());
  sort(neg.rbegin(), neg.rend());

  int minus = neg.size() - 1, plus = pos.size() - 1;

  while (K) {
    ll a = -1, b = -1;
    if (K >= 2 && minus >= 1)
      a = neg[minus] * neg[minus - 1];
    if (plus >= 1)
      b = pos[plus] * pos[plus - 1];

    if (a > b && K != 1) {
      K -= 2;
      minus -= 2;
      ans = (ans * (a % MOD)) % MOD;
    }

    else {
      --K;
      ans = (ans * (pos[plus] % MOD)) % MOD;
      --plus;
    }
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef vector<int> vi;
typedef unsigned long ul;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<int> si;
typedef multiset<int> mi;

const ll INF = 1e18;
const int MOD = 1e9 + 7;

bool comp(int a, int b) { return abs(a) > abs(b); }

int main() {

  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  // ofstream cout ("fileName.out");
  // ifstream cin ("fileName.in");

  bool allNegative = true;
  int N, K;
  cin >> N >> K;
  vi pos, neg;

  for (int i = 0; i < N; ++i) {
    int n;
    cin >> n;
    if (n >= 0)
      pos.pb(n), allNegative = false;
    else
      neg.pb(n);
  }

  ll ans = 1;

  if (allNegative) {
    sort(neg.rbegin(), neg.rend());
    for (int i = 0; i < K; ++i)
      ans = (ans * (neg[i] % MOD)) % MOD;
    ans += MOD;
    cout << ans << "\\n";
    return 0;
  }

  sort(pos.begin(), pos.end());
  sort(neg.rbegin(), neg.rend());

  while (K) {
    ll a = -1, b = -1;
    if (K >= 2 && neg.size() >= 2)
      a = neg.back() * neg[neg.size() - 2];
    if (pos.size() >= 2 && pos.back() > 0)
      b = pos.back() * pos[pos.size() - 2];
    else if (pos.size())
      b = pos.back();

    if (a > b) {
      K -= 2;
      ans = (ans * (a % MOD)) % MOD;
      neg.pop_back();
      neg.pop_back();
    } else {
      --K;
      ans = (ans * (pos.back() % MOD)) % MOD;
      pos.pop_back();
    }
  }

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