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

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007 * 1ll
using namespace std;
int ar[200002];
vector<int> ps, ne, tup;
bool cmp(int a, int b) { return a > b; }
int32_t main() {
  int n, k;
  cin >> n >> k;
  int an = 1;
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    if (ar[i] >= 0)
      an = 0;
  }
  if (k == n) {
    int ans = 1;
    for (int i = 0; i < n; i++) {
      int x = ar[i];
      x = (x + mod) % mod;
      ans = (ans * x) % mod;
    }
    cout << ans << endl;
  } else {
    if (an) {
      // cout<<"jkl "<<k<<endl ;
      if (k & 1ll)
        sort(ar, ar + n, cmp);
      else
        sort(ar, ar + n);
      int ans = 1;
      for (int i = 0; i < k; i++) {
        int x = ar[i];
        x = (x + mod) % mod;
        ans = (ans * x) % mod;
      }
      cout << ans << endl;
    } else {
      // cout<<"oo"<<endl ;
      int ans = 1;
      for (int i = 0; i < n; i++) {
        if (ar[i] >= 0)
          ps.push_back(ar[i]);
        else
          ne.push_back(ar[i]);
      }
      sort(ps.begin(), ps.end());
      sort(ne.rbegin(), ne.rend());
      //  for(int i = 0 ; i < ps.size() ; i++) cout<<ps[i]<<" " ; cout<<endl ;
      //  for(int i = 0 ; i < ne.size() ; i++) cout<<ne[i]<<" " ; cout<<endl ;
      if (k & 1) {
        int x = ps.back();
        ps.pop_back();
        ans = (ans * x * 1ll);
      }
      while ((int)ps.size() > 1) {
        int x = ps.back();
        ps.pop_back();
        int y = ps.back();
        ps.pop_back();
        tup.push_back((x * y * 1ll));
      }
      while ((int)ne.size() > 1) {
        int x = ne.back();
        ne.pop_back();
        int y = ne.back();
        ne.pop_back();
        tup.push_back((x * y * 1ll));
      }
      sort(tup.rbegin(), tup.rend());
      // for(int i = 0 ; i < (int)tup.size() ; i++) cout<<tup[i]<<" " ;
      // cout<<endl ;
      for (int i = 0; i < k / 2; i++) {
        ans %= mod;
        tup[i] %= mod;
        ans = (ans * tup[i]) % mod;
      }
      cout << ans % mod << endl;
    }
  }
}
// -1000000000 -1 -1000000000 -1000000000 -1000000000


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define F first
#define S second
#define mod 1000000007
using namespace std;
priority_queue<pii> pq;
priority_queue<int> ng;
stack<int> st;
int ar[200009], ans = 1;
int32_t main() {
  int n, k, f = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    if (ar[i] > 0)
      pq.push({ar[i], 0});
    else if (ar[i] == 0)
      f = 1;
    else {
      pq.push({ar[i] * -1, 1});
      ng.push(ar[i]);
    }
  }
  int ct = 0, c1 = 0;
  while (k) {
    // cout<<k<<endl ;
    pii p1 = pq.top();
    if (p1.S) {
      int pr = p1.F;
      st.push(pr);
      ct++;
    } else {
      ans *= (p1.F % mod);
      ans %= mod;
      c1++;
    }
    pq.pop();
    k--;
  }
  if (c1 == 0 && f) {
    cout << "0" << endl;
    return 0;
  }

  int fl = 0;
  if (ct % 2) {
    while (!pq.empty()) {
      pii p1 = pq.top();
      pq.pop();
      if (!p1.S) {
        ans *= (p1.F % mod);
        ans %= mod;
        st.pop();
        fl = 1;
        break;
      }
    }
  }
  // cout<<ans<<endl ;
  if (fl) {
    while (!st.empty()) {
      int x = st.top() * -1;
      st.pop();
      // cout<<x<<" " ;
      x = (x + mod) % mod;
      // cout<<x<<endl ;
      ans = ((ans % mod) * (x % mod)) % mod;
      ans += mod;
      ans %= mod;
      // cout<<"ans "<<ans<<endl ;
    }
  } else {
    while (ct--) {
      int x = ng.top();
      ng.pop();
      x = (x + mod) % mod;
      ans = ((ans % mod) * (x % mod)) % mod;
      ans += mod;
      ans %= mod;
    }
  }
  cout << ans % mod << endl;
}