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

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;

int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> pos, neg;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    int ai;
    cin >> ai;
    a[i] = ai;
    if (ai >= 0)
      pos.push_back(ai);
    else
      neg.push_back(ai);
  }
  sort(pos.begin(), pos.end());
  sort(neg.rbegin(), neg.rend());
  bool positive;
  if ((int)neg.size() == n) {
    if (k % 2 == 1) {
      positive = false;
    } else {
      positive = true;
    }
  } else {
    if (k == n) {
      if (((int)neg.size()) % 2) {
        positive = false;
      } else {
        positive = true;
      }
    } else {
      positive = true;
    }
  }
  ll ans = 1ll;
  if (!positive) {
    sort(a.begin(), a.end(), [](int x, int y) { return abs(x) < abs(y); });
    for (int i = 0; i < k; ++i) {
      ans *= (a[i] % mo + mo) % mo;
      ans %= mo;
    }
  } else {
    if (k % 2) {
      k--;
      ans *= pos.back();
      pos.pop_back();
    }
    vector<ll> p2;
    while ((int)pos.size() >= 2) {
      ll tmp = 1ll;
      tmp *= pos.back();
      pos.pop_back();
      tmp *= pos.back();
      pos.pop_back();
      p2.push_back(tmp);
    }
    while ((int)neg.size() >= 2) {
      ll tmp = neg.back();
      neg.pop_back();
      tmp *= neg.back();
      neg.pop_back();
      p2.push_back(tmp);
    }
    sort(p2.rbegin(), p2.rend());
    for (int i = 0; i < k / 2; ++i) {
      ans *= (p2[i] % mo + mo);
      ans %= mo;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;

int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (auto &a : v)
    cin >> a;
  vector<ll> pos, neg;
  for (int i = 0; i < n; ++i) {
    if (v[i] >= 0)
      pos.push_back(v[i]);
    else if (v[i] < 0)
      neg.push_back(v[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.rbegin(), neg.rend());

  ll ans = 1ll;
  if ((int)neg.size() == n) {
    if (k % 2) {
      for (int i = 0; i < k; ++i) {
        ans *= (mo + neg[i]);
        ans %= mo;
      }
    } else {
      for (int i = 0; i < k; ++i) {
        ans *= (mo + neg[n - i - 1]);
        ans %= mo;
      }
    }
  } else if (n == k and neg.size() % 2 == 1) {
    for (int i = 0; i < k; ++i) {
      ans *= (mo + neg[n - i - 1]);
      ans %= mo;
    }
  } else {
    vector<ll> pos2;
    if (k % 2) {
      k--;
      ans *= pos.back();
      pos.pop_back();
    }
    int len = (int)pos.size();
    for (int i = 0; i < len; i += 2) {
      pos2.push_back((pos[len - i - 1] * pos[len - i - 2]) % mo);
    }
    len = (int)neg.size();
    for (int i = 0; i < len; i += 2) {
      pos2.push_back(((mo + neg[i]) * (mo + neg[i + 1])) % mo);
    }
    sort(pos2.rbegin(), pos2.rend());
    for (int i = 0; i < k / 2; ++i) {
      ans *= pos2[i];
      ans %= mo;
    }
  }
  cout << ans << endl;
  return 0;
}