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

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
typedef vector<ll> vll;
typedef vector<int> vi;
const ll N = 1e7 + 10;
const int M = 1e9 + 7;
const double eps = 1e-9;

bool cmp(int a, int b) { return abs(a) > abs(b); }

int main() {
  int n, k;
  cin >> n >> k;
  vll v(n);
  int ng = 0;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    ng += v[i] < 0;
  }
  if (n == k) { // need to choose everything
    ll ans = 1;
    for (int i = 0; i < n; ++i) {
      ans = (ans * ((v[i] + M) % M)) % M;
    }
    cout << ans << endl;
    return 0;
  }
  if (ng == n) { // all negative
    sort(all(v));
    ll ans = 1;
    if (k % 2)
      reverse(all(v));
    for (int i = 0; i < k; ++i) {
      ans = (ans * (v[i] + M)) % M;
    }
    cout << ans << endl;
    return 0;
  }
  sort(all(v), cmp);
  vll pos, neg;
  for (int i = 0; i < n; ++i) {
    if (v[i] >= 0)
      pos.push_back(v[i]);
    else
      neg.push_back(v[i] * (-1));
  }
  sort(all(pos), greater<ll>());
  sort(all(neg), greater<ll>());
  ll posPrefix[pos.size()], negPrefix[neg.size()];
  for (int i = 0; i < pos.size(); ++i) {
    posPrefix[i] = (i == 0 ? pos[0] : (posPrefix[i - 1] * pos[i]) % M);
  }
  for (int i = 0; i < neg.size(); ++i) {
    negPrefix[i] = (i == 0 ? neg[0] : (negPrefix[i - 1] * neg[i]) % M);
  }
  ll ans = -1;
  for (int i = 2; i <= neg.size() && i <= k; i += 2) {
    int j = k - i;
    ll nm = neg[i - 1] * neg[i - 2];
    if (j > pos.size())
      continue;
    if (j + 2 > pos.size()) {
      ans = (posPrefix[j - 1] * negPrefix[i - 1]) % M;
      continue;
    }
    ll pm = pos[j] * pos[j + 1];
    if (nm <= pm)
      break;
    if (j)
      ans = (posPrefix[j - 1] * negPrefix[i - 1]) % M;
    else
      ans = negPrefix[i - 1];
  }
  if (ans == -1)
    ans = posPrefix[k - 1];
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
typedef vector<ll> vll;
typedef vector<int> vi;
const ll N = 1e7 + 10;
const int M = 1e9 + 7;
const double eps = 1e-9;

bool cmp(int a, int b) { return abs(a) > abs(b); }

int main() {
  int n, k;
  cin >> n >> k;
  vll v(n);
  int ng = 0;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    ng += v[i] < 0;
  }
  if (n == k) { // need to choose everything
    ll ans = 1;
    for (int i = 0; i < n; ++i) {
      ans = (ans * (v[i] + M) % M) % M;
    }
    cout << ans << endl;
    return 0;
  }
  if (ng == n) { // all negative
    sort(all(v));
    ll ans = 1;
    if (k % 2)
      reverse(all(v));
    for (int i = 0; i < k; ++i) {
      ans = (ans * (v[i] + M)) % M;
    }
    cout << ans << endl;
    return 0;
  }
  sort(all(v), cmp);
  vi pos, neg;
  for (int i = 0; i < k; ++i) {
    if (v[i] >= 0)
      pos.push_back(v[i]);
    else
      neg.push_back(v[i]);
  }
  if (neg.size() % 2 == 0) {
    ll ans = 1;
    for (int i = 0; i < k; ++i) {
      ans = (ans * (v[i] + M) % M) % M;
    }
    cout << ans << endl;
  } else {
    ll ans = 1;
    for (int i = 0; (i + 1) < pos.size(); ++i) {
      ans = (ans * v[i]) % M;
    }

    for (int i = 0; (i + 1) < neg.size(); ++i) {
      ans = (ans * (v[i] + M) % M) % M;
    }
    int pg = -1, ng = 1;
    for (int i = k; i < n; ++i) {
      if (v[i] < 0 && ng == 1) {
        ng = v[i];
      }
      if (v[i] >= 0 && pg == -1) {
        pg = v[i];
      }
    }
    ll t1 = LLONG_MIN, t2 = LLONG_MIN;
    if (pg != -1)
      t1 = (pos.back() * pg);
    if (ng != -1)
      t2 = (neg.back() * ng);
    ll t = max(t1, t2) % M;

    cout << (ans * t) % M << endl;
  }
}