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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl "\\n"
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define No cout << "No" << endl;
#define Yes cout << "Yes" << endl;
#define ff first
#define ss second
#define pb push_back
const ll maxn = 5e5 + 7;
const ll mod = 1e9 + 7;
ll a[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  priority_queue<ll, vector<ll>, greater<ll>> q;
  priority_queue<ll> q1;
  priority_queue<ll> q2;
  ll t = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      q1.push(a[i]);

    } else {
      q2.push(a[i]);
      q.push(a[i]);
    }
    t = (t * a[i]) % mod;
    t = (t + mod * mod) % mod;
  }
  if (n == k) {
    cout << t;
    return 0;
  }
  if (q1.size() == 0) {
    ll ans = 1;
    if (k % 2 == 0) {

      while (k--) {
        ans = ans * q.top();
        q.pop();
        ans = (ans + mod * mod) % mod;
      }
    } else {
      while (k--) {
        ans = ans * q2.top();
        q2.pop();
        ans = (ans + mod * mod) % mod;
      }
    }
    ans = (ans + mod * mod) % mod;
    cout << ans;
    return 0;
  }
  ll ans = 1;
  if (k % 2 == 1) {
    k--;
    ans = ans * q1.top();
    q1.pop();
  }
  ans = (ans + mod * mod) % mod;
  for (int i = 1; i <= k / 2; i++) {
    ll t1 = -1, t2 = -1;
    vector<ll> q11;
    vector<ll> q01;
    if (q1.size() >= 2) {
      t1 = 1;
      t1 *= q1.top();
      q11.push_back(q1.top());
      q1.pop();
      t1 *= q1.top();
      q11.pb(q1.top());
      q1.pop();
    }
    if (q.size() >= 2) {
      t2 = 1;
      t2 *= q.top();
      q01.push_back(q.top());
      q.pop();
      t2 *= q.top();
      q01.pb(q.top());
      q.pop();
    }
    // cout <<t1<<" "<<t2<<endl;
    if (t1 > t2) {
      // cout <<"co";
      t1 %= mod;
      ans *= t1;
      for (auto h : q01) {
        q.push(h);
      }
    }

    else {
      t2 %= mod;
      ans *= t2;
      for (auto h : q11) {
        q1.push(h);
      }
    }
    ans = (ans + mod * mod) % mod;
  }
  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl "\\n"
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define No cout << "No" << endl;
#define Yes cout << "Yes" << endl;
#define ff first
#define ss second
#define pb push_back
const ll maxn = 5e6 + 7;
const ll mod = 1e9 + 7;
ll a[maxn];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  freopen("test.txt", "r", stdin);
  cin >> n >> k;
  priority_queue<ll, vector<ll>, greater<ll>> q;
  priority_queue<ll> q1;
  priority_queue<ll> q2;
  ll p = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      q1.push(a[i]);

    } else {
      q2.push(a[i]);
      q.push(a[i]);
    }
    p = (p * a[i]) % mod;
    p = (p + mod * mod) % mod;
  }
  if (n == k) {
    cout << p;
    return 0;
  }
  if (q1.size() == 0) {
    ll ans = 1;
    if (k % 2 == 0) {

      while (k--) {
        ans = (ans * q.top()) % mod;
        q.pop();
        ans = (ans + mod * mod) % mod;
      }
    } else {
      while (k--) {
        ans = (ans * q2.top()) % mod;
        q2.pop();
        ans = (ans + mod * mod) % mod;
      }
    }

    ans = (ans + mod * mod) % mod;
    cout << ans;
    return 0;
  }
  ll ans = 1;
  if (k % 2 == 1) {
    k--;
    ans = ans * q1.top();
    q1.pop();
    ans %= mod;
  }
  //-1000000009
  ans = (ans + mod * mod) % mod;
  for (int i = 1; i <= k / 2; i++) {
    ll t1 = -1, t2 = -1;
    vector<ll> q11;
    vector<ll> q01;
    if (q1.size() >= 2) {
      t1 = 1;
      t1 *= q1.top();
      q11.push_back(q1.top());
      q1.pop();
      t1 *= q1.top();
      q11.pb(q1.top());
      q1.pop();
    }
    if (q.size() >= 2) {
      t2 = 1;
      t2 *= q.top();
      q01.push_back(q.top());
      q.pop();
      t2 *= q.top();
      q01.pb(q.top());
      q.pop();
    }
    cout << t1 << " " << t2 << endl;
    if (t1 > t2) {
      // cout <<"co";
      t1 %= mod;
      ans = (ans % mod * t1 % mod) % mod;
      ans %= mod;
      for (auto h : q01) {
        q.push(h);
      }
    }

    else {
      t2 %= mod;
      ans = (ans % mod * t2 % mod) % mod;
      ans %= mod;
      for (auto h : q11) {
        q1.push(h);
      }
    }
    ans = (ans + mod * mod) % mod;
  }
  ans %= mod;
  cout << ans << endl;
}