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

#include <bits/stdc++.h>

using namespace std;

const int md = (int)1e9 + 7;

inline int mul(int a, int b) {
  int res = (long long)a * b % md;
  if (res < 0)
    res += md;
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  if (a.back() < 0 && k % 2 == 1 || n == k) { // neg and all
    int res = 1;
    for (int i = 0; i < k; i++) {
      res = mul(res, a[n - 1 - i]);
    }
    cout << res << '\\n';
    return 0;
  }

  auto kmul = [&]() {
    int res = 1;
    for (int i = 0; i < k; i++) {
      res = mul(res, a[i]);
    }
    return res;
  };

  sort(a.begin(), a.end(), [&](int i, int j) { return abs(i) > abs(j); });
  int sign = 1;
  for (int i = 0; i < k; i++) {
    if (a[i] < 0)
      sign *= -1;
  }
  if (sign >= 0) {
    cout << kmul() << '\\n';
    return 0;
  }

  int optl = -1, optr = -1;
  { // pos -> neg
    int pos = -1, neg = -1;
    for (int i = 0; i < k; i++) {
      if (a[i] >= 0)
        pos = i;
    }
    for (int i = n - 1; i >= k; i--) {
      if (a[i] < 0)
        neg = i;
    }
    if (pos != -1 && neg != -1) {
      optl = pos;
      optr = neg;
    }
  }
  { // neg -> pos
    int pos = -1, neg = -1;
    for (int i = 0; i < k; i++) {
      if (a[i] < 0)
        neg = i;
    }
    for (int i = n - 1; i >= k; i--) {
      if (a[i] >= 0)
        pos = i;
    }
    if (pos != -1 && neg != -1) {
      if (optl == -1 || 1LL * a[pos] * a[optl] > 1LL * a[neg] * a[optr]) {
        optl = pos;
        optr = neg;
      }
    }
  }

  swap(a[optl], a[optr]);
  cout << kmul() << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int md = (int)1e9 + 7;

inline int mul(int a, int b) {
  int res = (long long)a * b % md;
  if (res < 0)
    res += md;
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  auto get_mul = [&](vector<int> idx) {
    int res = 1;
    for (auto i : idx) {
      // cout << i << ' ';
      res = mul(res, a[i]);
    }
    return res;
  };

  if (a.back() < 0 && k % 2 == 1) { // neg
    vector<int> res(k);
    for (int i = 0; i < k; i++) {
      res[i] = n - 1 - i;
    }
    cout << get_mul(res) << '\\n';
    return 0;
  }

  int pos = n;
  vector<int> res;
  vector<pair<long long, int>> v;

  for (int i = 0; i < n - 1; i += 2) {
    if (a[i + 1] < 0) {
      v.emplace_back(1LL * a[i] * a[i + 1], 2 * n - i); // sort
    }
  }
  for (int i = n - 1; i > 0; i -= 2) {
    if (a[i - 1] >= 0) {
      v.emplace_back(1LL * a[i] * a[i - 1], i - 1);
    }
  }
  sort(v.begin(), v.end());

  while (k >= 2) {
    int i = v.back().second;
    pos = min(pos, i);
    if (i >= n)
      i = -i + 2 * n;
    res.push_back(i);
    res.push_back(i + 1);
    v.pop_back();
    k -= 2;
  }
  if (k > 0) {
    pos--;
    if (a[pos] <= 0) {
      assert(pos != n - 1);
      for (int i = 0; i < (int)res.size(); i++) {
        if (res[i] == pos + 1) {
          res.erase(res.begin() + i);
        }
      }
      int i = v.back().second;
      if (i >= n)
        i = -i + 2 * n;
      res.push_back(i);
      res.push_back(i + 1);
    } else {
      res.push_back(pos);
    }
  }

  cout << get_mul(res) << '\\n';

  return 0;
}