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

#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#include "string"

const int M = 1e9 + 7;
int mod(int m) {
  m %= M;
  return (m + M) % M;
}

void brute(int *a, int n, int k) {
  int ans = -1e9;
  for (int i = 0; i < (1 << n); i++) {
    if (__builtin_popcount(i) != k)
      continue;
    int tt = 1;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j))
        tt *= a[j];
    }
    ans = max(ans, tt);
  }
  cout << ans << endl;
}

int32_t main() {
  boost;
  int n, k;
  cin >> n >> k;
  int a[n];
  vector<int> pos, neg;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      zero++;
    if (a[i] < 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  }
  // brute(a,n,k);
  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());
  int ans = 1;

  if (neg.empty()) {
    for (int i = 0; i < k; i++)
      ans = mod(ans * pos[i]);
    cout << ans << endl;
    return 0;
  } else if (pos.empty()) {
    if (k % 2 == 0)
      sort(neg.begin(), neg.end());
    for (int i = 0; i < k; i++)
      ans = mod(ans * neg[i]);
    if (k % 2 && zero > 0)
      ans = 0;
    cout << ans << endl;
    return 0;
  } else if (k == n) {
    for (int i = 0; i < n; i++)
      ans = mod(ans * a[i]);
    cout << ans << endl;
    return 0;
  }

  multiset<int> s, t;
  for (int &i : pos)
    s.insert(i);
  for (int &i : neg)
    t.insert(i);

  if (k % 2 == 1) {
    k--;
    int tt = *s.rbegin();
    ans = mod(ans * tt);
    s.erase(s.find(tt));
  }

  while (k > 0) {
    int tn = -1;
    int tp = -1;
    int fp, fn;
    if (t.size() > 1) {
      fn = *t.begin();
      t.erase(t.find(fn));
      int tt = *t.begin();
      tn = fn * tt;
    }

    if (s.size() > 1) {
      fp = *s.rbegin();
      s.erase(s.find(fp));
      int tt = *s.rbegin();
      tp = fp * tt;
    }
    // cout<<tn<<" "<<tp<<endl;
    if (tn == -1 && tp == -1) {
      ans = mod(ans * mod(fp * fn));
    } else if (tn > tp) {
      ans = mod(ans * mod(tn));
      t.erase(t.find(*t.begin()));
      if (tp != -1)
        s.insert(fp);
    } else {
      ans = mod(ans * mod(tp));
      s.erase(s.find(*s.rbegin()));
      if (tn != -1)
        t.insert(fn);
    }
    k -= 2;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#include "string"

const int M = 1e9 + 7;
int mod(int m) {
  m %= M;
  return (m + M) % M;
}

int32_t main() {
  boost;
  int n, k;
  cin >> n >> k;
  int a[n];
  vector<int> pos, neg;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      neg.push_back(a[i]);
    else
      pos.push_back(a[i]);
  }

  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());
  int ans = 1;

  if (neg.empty()) {
    for (int i = 0; i < k; i++)
      ans = mod(ans * pos[i]);
    cout << ans << endl;
    return 0;
  } else if (pos.empty()) {
    if (k % 2 == 0)
      sort(neg.begin(), neg.end());
    for (int i = 0; i < k; i++)
      ans = mod(ans * neg[i]);
    cout << ans << endl;
    return 0;
  }

  multiset<int> s, t;
  for (int &i : pos)
    s.insert(i);
  for (int &i : neg)
    t.insert(i);

  if (k % 2 == 1) {
    k--;
    int tt = *s.rbegin();
    ans = mod(ans * tt);
    s.erase(s.find(tt));
  }
  while (k > 0) {
    int tn = -1;
    int tp = -1;
    int fp, fn;
    if (neg.size() > 1) {
      fn = *t.begin();
      t.erase(t.find(fn));
      int tt = *t.begin();
      tn = fn * tt;
    }

    if (pos.size() > 1) {
      fp = *s.rbegin();
      s.erase(s.find(fp));
      int tt = *s.rbegin();
      tp = fp * tt;
    }

    if (tn > tp) {
      ans = mod(ans * mod(tn));
      t.erase(t.find(*t.begin()));
      s.insert(fp);
    } else {
      ans = mod(ans * mod(tp));
      s.erase(s.find(*s.rbegin()));
      t.insert(fn);
    }
    k -= 2;
  }
  cout << ans << endl;
}