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

#ifdef __APPLE__
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long int> vll;
typedef vector<pair<int, int>> vpi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n), pos, neg;
  int z = 0;

  FOR(i, 0, n) {
    cin >> a[i];
    if (a[i] > 0) {
      pos.pb(a[i]);
    } else if (a[i] < 0) {
      neg.pb(a[i]);
    } else {
      z++;
    }
  }

  if (k > n - z) {
    cout << 0 << "\\n";
  } else if (k == n - z) {
    ll ans = 1;
    for (auto x : a) {
      if (x == 0)
        continue;
      ans *= x, ans = (ans % mod + mod) % mod;
    }
    if (z)
      ans = max(0ll, ans);
    cout << ans << "\\n";
  } else if (sz(pos) == 0) {
    if (k % 2) {
      if (z)
        cout << 0 << "\\n";
      else {
        sort(all(neg), greater<int>());
        ll ans = 1;
        FOR(i, 0, k)
        ans *= neg[i], ans = (ans % mod + mod) % mod;
        cout << ans << "\\n";
      }
    } else {
      sort(all(neg));
      ll ans = 1;
      FOR(i, 0, k)
      ans *= abs(neg[i]), ans %= mod;

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

  } else {
    sort(all(pos), greater<int>());
    sort(all(neg));
    int i = 0, j = 0;
    while (i < sz(neg) and j < sz(pos) and i + j < k) {
      if (abs(neg[i]) > abs(pos[j])) {
        i++;
      } else {
        j++;
      }
    }

    while (i < sz(neg) and i + j < k)
      i++;

    while (j < sz(pos) and i + j < k)
      j++;

    if (i % 2 != 0) {
      if (i == sz(neg) or j == 0)
        i--, j++;
      else if (j == sz(pos) or i == 0)
        i++, j--;
      else if (pos[j] / (long double)abs(neg[i - 1]) >=
               abs(neg[i]) / (long double)pos[j - 1])
        i--, j++;
      else
        i++, j--;
    }

    ll ans = 1;
    FOR(i1, 0, i)
    ans *= abs(neg[i1]), ans %= mod;
    FOR(j1, 0, j)
    ans *= pos[j1], ans %= mod;

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

int main() {
#ifdef LOCAL
  auto t1 = std::chrono::high_resolution_clock::now();
#endif

  io;
  int t = 1;
  tc(t) { solve(); }

#ifdef LOCAL
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

  std::cout << "Time Taken: " << duration << "\\n";
#endif
}
// check for edge cases in limits
// printing double may print 1.223232e9 like values, beware of that


Error: 0

Fixed submission:

#ifdef __APPLE__
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long int> vll;
typedef vector<pair<int, int>> vpi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n), pos, neg;
  int z = 0;

  FOR(i, 0, n) {
    cin >> a[i];
    if (a[i] > 0) {
      pos.pb(a[i]);
    } else if (a[i] < 0) {
      neg.pb(a[i]);
    } else {
      z++;
    }
  }

  if (k > n - z) {
    cout << 0 << "\\n";
  } else if (k == n - z) {
    ll ans = 1;
    for (auto x : a) {
      if (x == 0)
        continue;
      ans *= x, ans = (ans % mod + mod) % mod;
    }
    if (z)
      ans = max(0ll, ans);
    cout << ans << "\\n";
  } else if (sz(pos) == 0) {
    if (k % 2) {
      if (z)
        cout << 0 << "\\n";
      else {
        sort(all(neg), greater<int>());
        ll ans = 1;
        FOR(i, 0, k)
        ans *= neg[i], ans = (ans % mod + mod) % mod;
        cout << ans << "\\n";
      }
    } else {
      sort(all(neg));
      ll ans = 1;
      FOR(i, 0, k)
      ans *= abs(neg[i]), ans %= mod;

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

  } else {
    sort(all(pos), greater<int>());
    sort(all(neg));
    int i = 0, j = 0;
    while (i < sz(neg) and j < sz(pos) and i + j < k) {
      if (abs(neg[i]) > abs(pos[i])) {
        i++;
      } else {
        j++;
      }
    }

    while (i < sz(neg) and i + j < k)
      i++;

    while (j < sz(pos) and i + j < k)
      j++;

    if (i % 2 != 0) {
      if (i == sz(neg) or j == 0)
        i--, j++;
      else if (j == sz(pos) or i == 0)
        i++, j--;
      else if (neg[i - 1] <= pos[j])
        i--, j++;
      else
        i++, j--;
    }

    ll ans = 1;
    FOR(i1, 0, i)
    ans *= abs(neg[i1]), ans %= mod;
    FOR(j1, 0, j)
    ans *= pos[j1], ans %= mod;

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

int main() {
#ifdef LOCAL
  auto t1 = std::chrono::high_resolution_clock::now();
#endif

  io;
  int t = 1;
  tc(t) { solve(); }

#ifdef LOCAL
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

  std::cout << "Time Taken: " << duration << "\\n";
#endif
}
// check for edge cases in limits
// printing double may print 1.223232e9 like values, beware of that