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

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define endl '\\n'

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a;
  vector<pair<int, int>> aa;
  vector<int> b, c;
  bool fl = false;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x == 0) {
      fl = true;
      continue;
    }
    a.push_back(x);
    aa.push_back(make_pair(abs(x), a.size() - 1));
    if (x > 0)
      b.push_back(x);
    else
      c.push_back(-x);
  }
  sort(aa.rbegin(), aa.rend());
  sort(b.rbegin(), b.rend());
  sort(c.rbegin(), c.rend());
  n = a.size();
  if (n < k) {
    cout << 0 << endl;
    return 0;
  }
  int mr = 0, ls = 0;
  for (int i = 0; i < k; i++) {
    if (a[aa[i].second] > 0)
      mr++;
    else
      ls++;
  }
  int mod = 1e9 + 7;
  if (ls % 2 == 0) {
    int ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= aa[i].first;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  if (n == k || b.size() == 0) {
    if (fl)
      cout << 0 << endl;
    else {
      int ans = 1;
      for (int i = 0; i < k; i++) {
        ans *= aa[n - i - 1].first;
        ans %= mod;
      }
      ans = (mod - ans) % mod;
      cout << ans << endl;
    }
    return 0;
  }
  int mx1 = -1, mx2 = -1;
  int dl1 = -1, dl2 = -1;
  // assert(0);
  if (ls < c.size() && mr > 0) {
    mx1 = c[ls];
    dl1 = b[mr - 1];
  }
  if (mr < b.size() && ls > 0) {
    mx2 = b[mr];
    dl2 = c[ls - 1];
  }
  int mx = -1, del = -1;
  if (mx1 == -1) {
    mx = mx2, del = dl2;
  }
  if (mx2 == -1) {
    mx = mx1, del = dl1;
  }
  // assert(!(mx1 == -1 && mx2 == -1));
  if (mx1 != -1 && mx2 != -1) {
    if (mx1 * dl2 > mx2 * dl1) {
      mx = mx1;
      del = dl1;
    } else {
      mx = mx2;
      del = dl2;
    }
  }
  int ans = mx;
  for (int i = 0; i < k; i++) {
    if (aa[i].first == del) {
      del = -1;
      continue;
    }
    ans *= aa[i].first;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define endl '\\n'

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a;
  vector<pair<int, int>> aa;
  vector<int> b, c;
  bool fl = false;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x == 0) {
      fl = true;
      continue;
    }
    a.push_back(x);
    aa.push_back(make_pair(abs(x), i));
    if (x > 0)
      b.push_back(x);
    else
      c.push_back(-x);
  }
  sort(aa.rbegin(), aa.rend());
  sort(b.rbegin(), b.rend());
  sort(c.rbegin(), c.rend());
  n = a.size();
  if (n < k) {
    cout << 0 << endl;
    return 0;
  }
  int mr = 0, ls = 0;
  for (int i = 0; i < k; i++) {
    if (a[aa[i].second] > 0)
      mr++;
    else
      ls++;
  }
  int mod = 1e9 + 7;
  if (ls % 2 == 0) {
    int ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= aa[i].first;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  if (n == k || b.size() == 0) {
    if (fl)
      cout << 0 << endl;
    else {
      int ans = 1;
      for (int i = 0; i < k; i++) {
        ans *= aa[n - i - 1].first;
        ans %= mod;
      }
      ans = (mod - ans) % mod;
      cout << ans << endl;
    }
    return 0;
  }
  int mx1 = -1, mx2 = -1;
  int dl1 = -1, dl2 = -1;
  // assert(0);
  if (ls < c.size() && mr > 0) {
    mx1 = c[ls];
    dl1 = b[mr - 1];
  }
  if (mr < b.size() && ls > 0) {
    mx2 = b[mr];
    dl2 = c[ls - 1];
  }
  int mx, del;
  if (mx1 == -1) {
    mx = mx2, del = dl2;
  }
  if (mx2 == -1) {
    mx = mx1, del = dl1;
  }
  if (mx1 * dl2 > mx2 * dl1) {
    mx = mx1;
    del = dl1;
  } else {
    mx = mx2;
    del = dl2;
  }
  int ans = mx;
  for (int i = 0; i < k; i++) {
    if (aa[i].first == del) {
      del = 0;
      continue;
    }
    ans *= aa[i].first;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}