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

#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define setpre(a) cout.precision(a), cout << fixed;
#define ALL(a) a.begin(), a.end()
#define MEM(a, b) memset(a, b, sizeof a)
#define Tie ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const ll MOD = 1000000007;
ll n, k, ans = 1;
vector<ll> a, b;

int main() {
  Tie cin >> n >> k;
  for (ll i = 0, tmp; i < n; i++) {
    cin >> tmp;
    if (tmp >= 0)
      a.pb(tmp);
    else
      b.pb(tmp);
  }
  sort(ALL(a));
  sort(ALL(b));
  if (a.empty() || (a.back() == 0 && k % 2 == 0 && b.size() >= k)) {
    if (k % 2 == 1)
      reverse(ALL(b));
    for (int i = 0; i < k; i++)
      ans = ans * abs(b[i]) % MOD;
    if (k % 2 == 1)
      ans = (-ans + MOD) % MOD;
  } else if (b.empty()) {
    reverse(ALL(a));
    for (int i = 0; i < k; i++)
      ans = ans * a[i] % MOD;
  } else if (a.back() == 0) {
    ans = 0;
  } else {
    if (k & 1)
      ans = a.back(), a.pop_back();
    reverse(ALL(b));
    for (int i = k & 1; i < k; i += 2) {
      if (b.size() > 1 &&
          (a.size() < 2 || b[b.size() - 1] * b[b.size() - 2] >=
                               a[a.size() - 1] * a[a.size() - 2]))
        ans = ans * abs(b[b.size() - 1]) % MOD * abs(b[b.size() - 2]) % MOD,
        b.pop_back(), b.pop_back();
      else if (a.size() > 1)
        ans = ans * a[a.size() - 1] % MOD * a[a.size() - 2] % MOD, a.pop_back(),
        a.pop_back();
      else if (!a.empty() && !b.empty())
        ans = ans * a[a.size() - 1] % MOD * abs(b[b.size() - 1]) % MOD,
        ans = (-ans + MOD) % MOD, a.pop_back(), b.pop_back();
      else
        while (1)
          ;
    }
  }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define setpre(a) cout.precision(a), cout << fixed;
#define ALL(a) a.begin(), a.end()
#define MEM(a, b) memset(a, b, sizeof a)
#define Tie ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const ll MOD = 1000000007;
ll n, k, ans = 1;
vector<ll> a, b;

int main() {
  Tie cin >> n >> k;
  for (int i = 0, tmp; i < n; i++) {
    cin >> tmp;
    if (tmp >= 0) {
      a.pb(tmp);
    } else {
      b.pb(tmp);
    }
  }
  sort(ALL(a));
  sort(ALL(b));
  if (a.empty() || (a.back() == 0 && k % 2 == 0)) {
    if (k % 2 == 1)
      reverse(ALL(b));
    for (int i = 0; i < k; i++)
      ans = ans * abs(b[i]) % MOD;
    if (k & 1)
      ans = (-ans + MOD) % MOD;
  } else if (b.empty()) {
    reverse(ALL(a));
    for (int i = 0; i < k; i++)
      ans = ans * a[i] % MOD;
  } else if (a.back() == 0) {
    ans = 0;
  } else {
    if (k & 1)
      ans = a.back(), a.pop_back();
    reverse(ALL(b));
    for (int i = k & 1; i < k; i += 2) {
      if (a.size() < 2 ||
          (b.size() > 1 && b[b.size() - 1] * b[b.size() - 2] >=
                               a[a.size() - 1] * a[a.size() - 2]))
        ans = ans * b[b.size() - 1] % MOD * b[b.size() - 2] % MOD, b.pop_back(),
        b.pop_back();
      else
        ans = ans * a[a.size() - 1] % MOD * a[a.size() - 2] % MOD, a.pop_back(),
        a.pop_back();
    }
  }
  cout << ans << '\\n';
}