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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n, k;
llint a[200005];
vector<llint> pvec, nvec, zvec;
vector<P> vec;

llint modpow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
  } else {
    return modpow((a * a) % mod, n / 2) % mod;
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      pvec.push_back(a[i]);
    else if (a[i] < 0)
      nvec.push_back(a[i]);
    else
      zvec.push_back(a[i]);
  }
  sort(pvec.begin(), pvec.end());
  sort(nvec.rbegin(), nvec.rend());

  for (int i = 0; i < nvec.size(); i++) {
    nvec[i] = (nvec[i] % mod + mod) % mod;
  }

  llint ans = 1;
  if (n == k) {
    for (int i = 1; i <= n; i++)
      ans *= (a[i] % mod + mod) % mod, ans %= mod;
    cout << ans << endl;
    return 0;
  }
  if (nvec.size() == n && k % 2) {
    for (int i = 0; i < k; i++)
      ans *= nvec[i], ans %= mod;
    cout << ans << endl;
    return 0;
  }
  if (zvec.size() > n - k) {
    cout << 0 << endl;
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    vec.push_back(P(abs(a[i]), (a[i] >= 0) ? 1 : -1));
  }
  sort(vec.rbegin(), vec.rend());

  llint cnt = 0;
  for (int i = 0; i < k; i++) {
    ans *= vec[i].first, ans %= mod;
    if (vec[i].second == -1)
      cnt++;
  }
  if (cnt % 2) {
    llint p = -1, ne = -1, p2 = -1, n2 = -1;
    for (int j = 0; j < k; j++) {
      if (vec[j].second == 1)
        p2 = vec[j].first;
      else
        ne = vec[j].first;
    }
    for (int j = n - 1; j >= k; j--) {
      if (vec[j].second == 1)
        p = vec[j].first;
      else
        n2 = vec[j].first;
    }
    bool flag;
    if (p2 == -1)
      flag = true;
    else if (ne == -1)
      flag = false;
    else if (p * p2 >= ne * n2)
      flag = true;
    else
      flag = false;

    if (flag) {
      ans *= p, ans %= mod;
      ans *= modpow(ne, mod - 2), ans %= mod;
    } else {
      ans *= n2, ans %= mod;
      ans *= modpow(p2, mod - 2), ans %= mod;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n, k;
llint a[200005];
vector<llint> pvec, nvec, zvec;
vector<P> vec;

llint modpow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
  } else {
    return modpow((a * a) % mod, n / 2) % mod;
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > 0)
      pvec.push_back(a[i]);
    else if (a[i] < 0)
      nvec.push_back(a[i]);
    else
      zvec.push_back(a[i]);
  }
  sort(pvec.begin(), pvec.end());
  sort(nvec.rbegin(), nvec.rend());

  for (int i = 0; i < nvec.size(); i++) {
    nvec[i] = (nvec[i] % mod + mod) % mod;
  }

  llint ans = 1;
  if (n == k) {
    for (int i = 1; i <= n; i++)
      ans *= (a[i] % mod + mod) % mod, ans %= mod;
    cout << ans << endl;
    return 0;
  }
  if (nvec.size() == n && k % 2) {
    for (int i = 0; i < k; i++)
      ans *= nvec[i], ans %= mod;
    cout << ans << endl;
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    vec.push_back(P(abs(a[i]), (a[i] >= 0) ? 1 : -1));
  }
  sort(vec.rbegin(), vec.rend());

  llint cnt = 0;
  for (int i = 0; i < k; i++) {
    ans *= vec[i].first, ans %= mod;
    if (vec[i].second == -1)
      cnt++;
  }
  if (cnt % 2) {
    llint p = -1, n = -1, p2 = -1, n2 = -1;
    for (int j = 0; j < k; j++) {
      if (vec[j].second == 1)
        p2 = vec[j].first;
      else
        n = vec[j].first;
    }
    for (int j = n - 1; j >= k; j--) {
      if (vec[j].second == 1)
        p = vec[j].first;
      else
        n2 = vec[j].first;
    }
    bool flag;
    if (p2 == -1)
      flag = true;
    else if (n == -1)
      flag = false;
    else if (p * p2 >= n * n2)
      flag = true;
    else
      flag = false;

    if (flag) {
      ans *= p, ans %= mod;
      ans *= modpow(n, mod - 2), ans %= mod;
    } else {
      ans *= p2, ans %= mod;
      ans *= modpow(n2, mod - 2), ans %= mod;
    }
  }
  cout << ans << endl;

  return 0;
}