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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  if (n == k) {
    bool f = true;
    ll ans = 1;
    rep(i, n) {
      ll in;
      cin >> in;
      if (in < 0) {
        in *= -1;
        f ^= 1;
      }
      ans *= in;
      ans %= mod;
    }
    if (f == false) {
      ans = mod - ans;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  vector<ll> ps, ng;
  rep(i, n) {
    ll in;
    cin >> in;
    if (in < 0)
      ng.push_back(-1 * in);
    else
      ps.push_back(in);
  }
  sort(ng.begin(), ng.end());
  sort(ps.begin(), ps.end());
  if (ps.size() == 0 && k % 2 == 1) {
    ll ans = 1;
    rep(i, k) {
      ans *= ng.at(i);
      ans %= mod;
    }
    ans = mod - ans;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }

  priority_queue<ll> q;
  for (int i = ng.size() - 1; i >= 1; i -= 2) {
    q.push(ng.at(i) * ng.at(i - 1));
    // cerr << ng.at(i)*ng.at(i-1) << endl;
  }
  // cerr << endl;
  ll ans = 1;
  int st = -1;
  if (k % 2 == 1) {
    ans *= ps.back();
    st = ps.size() - 1;
  }
  for (int i = ps.size() - 1; i >= 1; i -= 2) {
    if (i == st) {
      i++;
      continue;
    }
    q.push(ps.at(i) * ps.at(i - 1));
  }
  rep(i, k / 2) {
    ll now = q.top();
    q.pop();
    now %= mod;
    ans *= now;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
const ll mod = 1000000007;

int main() {
  int n, k;
  cin >> n >> k;
  if (n == k) {
    bool f = true;
    ll ans = 1;
    rep(i, n) {
      ll in;
      cin >> in;
      if (in < 0) {
        in *= -1;
        f ^= 1;
      }
      ans *= in;
      ans %= mod;
    }
    if (f == false) {
      ans = mod - ans;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  vector<ll> ps, ng;
  rep(i, n) {
    ll in;
    cin >> in;
    if (in < 0)
      ng.push_back(-1 * in);
    else
      ps.push_back(in);
  }
  sort(ng.begin(), ng.end());
  sort(ps.begin(), ps.end());
  if (ps.size() == 0 && k % 2 == 1) {
    ll ans = 1;
    rep(i, k) {
      ans *= ng.at(i);
      ans %= mod;
    }
    ans = mod - ans;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }

  priority_queue<ll> q;
  for (int i = ng.size() - 1; i >= 1; i -= 2) {
    q.push(ng.at(i) * ng.at(i - 1));
  }
  ll ans = 1;
  int st = -1;
  if (k % 2 == 1) {
    ans *= ps.back();
    st = ps.size() - 1;
  }
  for (int i = 0; i < ps.size() - 1; i += 2) {
    if (i + 1 == st)
      continue;
    q.push(ps.at(i) * ps.at(i + 1));
  }
  rep(i, k / 2) {
    ll now = q.top();
    q.pop();
    now %= mod;
    ans *= now;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}