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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  mint ans = 1;
  vector<ll> a;
  vector<ll> ap;
  vector<ll> an;

  for (ll i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    a.push_back(tmp);
    if (tmp < 0)
      an.push_back(tmp);
    else
      ap.push_back(tmp);
  }

  if (n == k) {
    for (auto aa : a) {
      ans *= aa;
    }
    cout << ans << endl;
    return 0;
  }

  ll apsize = ap.size();
  if (apsize == 0 && k % 2 == 0) {
    sort(an.begin(), an.end());
    for (ll i = 0; i < k; i++) {
      ans *= an.at(i);
    }
    cout << ans << endl;
    return 0;
  } else if (apsize == 0 && k % 2 == 1) {
    sort(an.begin(), an.end());
    reverse(an.begin(), an.end());
    for (ll i = 0; i < k; i++) {
      ans *= an.at(i);
    }
    cout << ans << endl;
    return 0;
  }

  sort(an.begin(), an.end());
  sort(ap.begin(), ap.end());
  ll apmax;
  if (k % 2 == 1) {
    apmax = ap.back();
    ap.pop_back();
  }
  reverse(ap.begin(), ap.end());

  ll ansize2 = an.size();
  ll apsize2 = ap.size();

  if (ansize2 <= 1 && k % 2 == 0) {
    for (ll i = 0; i < k; i++) {
      ans *= ap.at(i);
    }
    cout << ans << endl;
    return 0;
  } else if (ansize2 <= 1 && k % 2 == 1) {
    for (ll i = 0; i < k - 1; i++) {
      ans *= ap.at(i);
    }
    ans *= apmax;
    cout << ans << endl;
    return 0;
  }

  vector<ll> aabs;
  for (ll i = 0; i + 1 < ansize2; i += 2) {
    aabs.push_back(an.at(i) * an.at(i + 1));
  }
  for (ll i = 0; i + 1 < apsize2; i += 2) {
    aabs.push_back(ap.at(i) * ap.at(i + 1));
  }
  sort(aabs.rbegin(), aabs.rend());
  for (ll i = 0; i < k / 2; i++) {
    ans *= aabs.at(i);
  }
  if (k % 2 == 1)
    ans *= apmax;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  ll ans = 1;
  vector<ll> am;
  vector<ll> ap;
  vector<ll> az;
  ll tmp;
  for (ll i = 0; i < n; i++) {
    cin >> tmp;
    if (tmp == 0)
      az.push_back(tmp);
    else if (tmp > 0)
      ap.push_back(tmp);
    else
      am.push_back(tmp);
  }
  ll size = ap.size() + am.size();
  if (size < k) {
    cout << 0 << endl;
    return 0;
  }
  ll sizep = ap.size();
  if (sizep == 0 && k % 2 == 1) {
    sort(am.begin(), am.end());
    reverse(am.begin(), am.end());
    for (ll i = 0; i < k; i++) {
      ans *= am.at(i);
      ans %= 1000000007;
    }
    cout << ans + 1000000007 << endl;
    return 0;
  }
  if (sizep == 0 && k % 2 == 0) {
    sort(am.begin(), am.end());
    for (ll i = 0; i < k; i++) {
      ans *= am.at(i);
      ans %= 1000000007;
    }
    cout << ans << endl;
    return 0;
  }
  sort(am.begin(), am.end());
  sort(ap.begin(), ap.end());
  reverse(ap.begin(), ap.end());

  vector<ll> ampair;
  ll amsize = am.size();
  if (amsize % 2 == 0) {
    for (ll i = 0; i + 1 < amsize; i += 2) {
      ampair.push_back(am.at(i) * am.at(i + 1));
    }
  } else {
    for (ll i = 0; i + 1 < amsize - 1; i += 2) {
      ampair.push_back(am.at(i) * am.at(i + 1));
    }
  }
  vector<pair<ll, int>> aabs;
  for (auto amp : ampair) {
    aabs.push_back({amp, -1});
  }
  for (auto apt : ap) {
    aabs.push_back({apt, 1});
  }
  sort(aabs.begin(), aabs.end());
  reverse(aabs.begin(), aabs.end());

  ll tmp2 = k;
  ll ai = 0;
  while (1) {
    ans *= aabs.at(ai).first;
    if (aabs.at(ai).second == -1) {
      if (tmp2 - 2 < 0) {
        ans /= aabs.at(ai).first;
        ai++;
        continue;
      } else {
        ans %= 1000000007;
        ai++;
        tmp2 -= 2;
      }
    } else {
      ans %= 1000000007;
      ai++;
      tmp2 -= 1;
    }
    if (tmp2 == 0) {
      cout << ans << endl;
      return 0;
    }
  }
}