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

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef vector<vi> vvi;
const int MOD = 1e9 + 7;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define endl '\\n'
#define pb push_back
#define ff first
#define ss second
#define mp make_pair

int main() {
  fastIO;
  int n, k;
  cin >> n >> k;
  vi a(n);
  for (auto &i : a)
    cin >> i;
  if (n == k) {
    ll pdt = 1;
    for (auto i : a) {
      pdt = (pdt * i) % MOD;
    }
    if (pdt < 0)
      pdt += MOD;
    cout << pdt << endl;
    return 0;
  }

  sort(a.begin(), a.end(), [](ll x, ll y) { return abs(x) > abs(y); });

  bool zero = false;
  int pos = 0;
  for (auto i : a) {
    if (i == 0)
      zero = true;
    else if (i > 0)
      ++pos;
  }

  if (pos == 0 && k % 2 == 1) {
    if (zero)
      cout << 0 << endl;
    else {
      ll pdt = 1;
      for (int i = n - 1; i >= n - k; i--) {
        pdt = (pdt * abs(a[i])) % MOD;
      }
      if (pdt < 0)
        pdt += MOD;
      pdt *= -1;
      pdt %= MOD;
      if (pdt < 0)
        pdt += MOD;
      cout << pdt << endl;
    }
    return 0;
  }

  int lastPos = -1, lastNeg = -1;
  bool neg = false;
  for (int i = 0; i < k; i++) {
    if (a[i] < 0)
      neg = !neg;
    if (a[i] > 0)
      lastPos = i;
    else if (a[i] < 0)
      lastNeg = i;
    else {
      cout << 0 << endl;
      return 0;
    }
  }
  if (neg) {
    int nextPos = -1, nextNeg = -1;
    for (int i = k; i < n; i++) {
      if (a[i] > 0 && nextPos < 0)
        nextPos = i;
      else if (a[i] < 0 && nextNeg < 0)
        nextNeg = i;
    }
    cerr << nextPos << " " << nextNeg << endl;
    cerr << lastPos << " " << lastNeg << endl;
    if (nextPos < 0 && nextNeg < 0) {
      cout << 0 << endl;
      return 0;
    } else if (nextPos > 0 && nextNeg < 0) {
      assert(lastNeg >= 0);
      swap(a[nextPos], a[lastNeg]);
    } else if (nextPos < 0 && nextNeg > 0) {
      if (pos == 0) {
        // absolutely no positive values => k must be odd to get negative =>
        // case already taken
      }
      assert(lastPos >= 0);
      swap(a[lastPos], a[nextNeg]);
    } else if (nextPos > 0 && nextNeg > 0) {
      if (lastPos >= 0 &&
          (lastNeg < 0 || a[lastNeg] * a[nextNeg] >= a[lastPos] * a[nextPos])) {
        swap(a[lastPos], a[nextNeg]);
      } else {
        swap(a[lastNeg], a[nextPos]);
      }
    }
  }

  ll pdt = 1;
  neg = false;
  for (int i = 0; i < k; i++) {
    if (a[i] < 0)
      neg = !neg;
    pdt = (pdt * abs(a[i])) % MOD;
  }
  if (neg)
    pdt *= 1;
  pdt = ((pdt % MOD) + MOD) % MOD;
  cout << pdt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef vector<vi> vvi;
const int MOD = 1e9 + 7;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define endl '\\n'
#define pb push_back
#define ff first
#define ss second
#define mp make_pair

int main() {
  fastIO;
  int n, k;
  cin >> n >> k;
  ll N = n, K = k;
  vi a(n);
  for (auto &i : a)
    cin >> i;
  if (N == K) {
    ll pdt = 1;
    for (auto i : a) {
      pdt = (pdt * i) % MOD;
    }
    if (pdt < 0)
      pdt += MOD;
    cout << pdt << endl;
    return 0;
  }
  sort(a.begin(), a.end(), [](ll x, ll y) {
    if (abs(x) == abs(y))
      return x > y;
    return abs(x) > abs(y);
  });
  bool zero = false;
  int pos = 0;
  for (auto i : a) {
    if (i == 0)
      zero = true;
    else if (i > 0)
      ++pos;
  }
  if (pos == 0) {
    if (zero)
      cout << 0 << endl;
    else {
      ll pdt = 1;
      for (int i = n - 1; i >= n - k; i--) {
        pdt = (pdt * a[i]) % MOD;
      }
      if (pdt < 0)
        pdt += MOD;
      cout << pdt << endl;
    }
    return 0;
  }
  vector<bool> take(n, false);
  int lastPos = -1, lastNeg = -1;
  bool neg = false;
  for (int i = 0; i < k; i++) {
    take[i] = true;
    if (a[i] < 0)
      neg = !neg;
    if (a[i] > 0)
      lastPos = i;
    else if (a[i] < 0)
      lastNeg = i;
    else {
      cout << 0 << endl;
      return 0;
    }
  }
  if (neg) {
    int nextPos = -1, nextNeg = -1;
    for (int i = k; i < n; i++) {
      if (a[i] > 0 && nextPos < 0)
        nextPos = i;
      else if (a[i] < 0 && nextNeg < 0)
        nextNeg = i;
      else
        break;
    }
    if (nextPos < 0 && nextNeg < 0) {
      cout << 0 << endl;
      return 0;
    } else if (nextPos > 0 && nextNeg < 0) {
      assert(lastNeg >= 0);
      swap(a[nextPos], a[lastNeg]);
    } else if (nextPos < 0 && nextNeg > 0) {
      assert(lastPos >= 0); // All -ve case taken care of earlier
      swap(a[lastPos], a[nextNeg]);
    } else {
      if (a[lastNeg] * a[nextNeg] >= a[lastPos] * a[nextPos]) {
        swap(a[lastPos], a[nextNeg]);
      } else {
        swap(a[lastNeg], a[nextPos]);
      }
    }
  }
  ll pdt = 1;
  for (int i = 0; i < k; i++) {
    pdt = (pdt * a[i]) % MOD;
  }
  if (pdt < 0)
    pdt += MOD;
  cout << pdt << endl;
  return 0;
}