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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef double db;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define pb push_back
#define mp make_pair
#define MAX 1000000
#define mod 1000000007
#define all(_) _.begin(), _.end()
#define F first
#define S second
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
bool c(ll a, ll b) { return abs(a) > abs(b); }
ll f(multiset<ll> &ms) {
  ll ans = 1;
  for (auto x : ms) {
    ans = (ans * (x + mod) % mod) % mod;
  }
  return ans;
}
vll a;
int main() {
  // ios_base :: sync_with_stdio(false);
  // cin.tie(NULL), cout.tie(NULL);
  ll n, i, k, ans;
  multiset<ll> ms;
  cin >> n >> k;
  a.resize(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(all(a), c);
  if (k == n) {
    for (i = 0; i < n; i++) {
      ms.insert(a[i]);
    }
    cout << f(ms) << endl;
    return 0;
  }
  for (i = 0; i < n; i++)
    if (a[i] >= 0)
      break;
  if (i == n) {
    for (i = 0; i < k && k % 2 == 0; i++)
      ms.insert(a[i]);
    for (i = n - 1; i >= n - k && k % 2; i--)
      ms.insert(a[i]);
    ans = f(ms);
    cout << ans << endl;
    return 0;
  }
  ll neg = 0;
  ans = LLONG_MAX;
  for (i = 0; i < k; i++)
    ms.insert(a[i]), neg += (a[i] < 0 ? 1 : 0);
  if (neg % 2) {
    ll x1, y1, y = LLONG_MAX;
    x1 = y1 = y;
    for (i = k; i < n; i++)
      if (a[i] >= 0 && y1 == y)
        y1 = a[i];
    for (i = k - 1; i >= 0; i--)
      if (a[i] < 0 && x1 == y)
        x1 = a[i];
    if (x1 != y && y1 != y) {
      // cout << "ere\\n";
      ms.insert(y1);
      ms.erase(ms.find(x1));
      ans = f(ms);
      ms.insert(x1);
      ms.erase(ms.find(y1));
    }
    ll x2 = y, y2 = y;
    for (i = k; i < n; i++)
      if (a[i] < 0 && y2 == y)
        y2 = a[i];
    for (i = k - 1; i >= 0; i--)
      if (a[i] >= 0 && x2 == y)
        x2 = a[i];
    if (ans == LLONG_MAX) {
      // cout << f(ms) << " ans" << endl;
      ms.insert(y2);
      ms.erase(ms.find(x2));
      ans = f(ms);
    } else {
      for (i = 0; i < k; i++) {
        if (a[i] == 0) {
          // cout << "hey\\n";
          break;
        }
      }
      if (x2 != y && y2 != y && abs(x1 * y2) >= abs(x2 * y1)) {
        // cout << "Here " << x2 << endl;
        ms.insert(y2);
        ms.erase(ms.find(x2));
        ans = f(ms);
      }
    }
  } else
    ans = f(ms);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef double db;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define pb push_back
#define mp make_pair
#define MAX 1000000
#define mod 1000000007
#define all(_) _.begin(), _.end()
#define F first
#define S second
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
bool c(ll a, ll b) { return abs(a) > abs(b); }
ll f(multiset<ll> &ms) {
  ll ans = 1;
  for (auto x : ms) {
    ans = (ans * (x + mod) % mod) % mod;
  }
  return ans;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n, i, k, ans;
  multiset<ll> ms;
  cin >> n >> k;
  vll a(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(all(a), c);
  for (i = 0; i < n; i++)
    if (a[i] >= 0)
      break;
  if (i == n) {
    for (i = 0; i < k && k % 2 == 0; i++)
      ms.insert(a[i]);
    for (i = n - 1; i >= n - k && k % 2; i--)
      ms.insert(a[i]);
    ans = f(ms);
    cout << ans << endl;
    return 0;
  }
  ll neg = 0;
  ans = LLONG_MAX;
  for (i = 0; i < k; i++)
    ms.insert(a[i]), neg += (a[i] < 0 ? 1 : 0);
  if (neg % 2) {
    ll x1, y1, y = LLONG_MAX;
    x1 = y1 = y;
    for (i = k; i < n; i++)
      if (a[i] >= 0 && y1 == y)
        y1 = a[i];
    for (k - 1; i >= 0; i--)
      if (a[i] < 0 && x1 == y)
        x1 = a[i];
    if (x1 != y && y1 != y) {
      ms.insert(y1);
      ms.erase(ms.find(x1));
      ans = f(ms);
      ms.insert(x1);
      ms.erase(ms.find(y1));
    }
    ll x2 = y, y2 = y;
    for (i = k; i < n; i++)
      if (a[i] < 0 && y2 == y)
        y2 = a[i];
    for (i = k - 1; i >= 0; i--)
      if (a[i] >= 0 && x2 == y)
        x2 = a[i];
    if (ans == LLONG_MAX) {
      ms.insert(y2);
      ms.erase(ms.find(x2));
      ans = f(ms);
    } else {
      if (x2 != y && y2 != y && abs(x1 * y2) >= abs(x2 * y1)) {
        ms.insert(y2);
        ms.erase(ms.find(x2));
        ans = f(ms);
      }
    }
  }
  cout << ans << endl;
  return 0;
}