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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 * 3, MOD = 1e9 + 7, INF = 1e9;
vector<vector<ll>> graph;
bool used[SIZE];
ll ans = 0;

ll pw(ll a, ll b, ll md = MOD) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (a * res) % md;
    }
    a = (a * a) % md;
    b >>= 1;
  }
  return (res);
}

int main() {
  fastInp;

  ll n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> vec;
  bool z = 0;
  for (int i = 0; i < n; i++) {
    ll num;
    cin >> num;
    if (num == 0) {
      z = 1;
      continue;
    }
    vec.push_back({abs(num), num / (abs(num))});
  }

  ll curCh = 1, cnt[2], curM = 1, mn[2];
  cnt[0] = 0;
  cnt[1] = 0;
  mn[0] = 0;
  mn[1] = 0;
  sort(vec.rbegin(), vec.rend());
  n = vec.size();
  if (k > vec.size()) {
    cout << "0";
    return 0;
  }
  for (int i = 0; i < k; i++) {
    if (vec[i].second == 1) {
      cnt[0]++;
      mn[0] = vec[i].first;
    } else {
      cnt[1]++;
      mn[1] = vec[i].first;
    }
    curCh *= vec[i].second;
    curM *= vec[i].first;
    curM %= MOD;
  }

  if (curCh == 1) {
    cout << curM;
    return 0;
  }

  if (curCh == -1) {
    pair<ll, ll> bst = {-1, -1}, bst2 = {-1, -1};
    for (int i = k; i < n; i++) {
      if (vec[i].second == 1) {
        if (cnt[1] != 0) {
          if (bst == make_pair(-1ll, -1ll)) {
            bst = {mn[1], vec[i].first};
          }
        }
      } else if (vec[i].second == -1) {
        if (cnt[0] != 0) {
          if (bst2 == make_pair(-1ll, -1ll)) {
            bst2 = {mn[0], vec[i].first};
          }
        }
      }
    }

    if (bst != make_pair(-1ll, -1ll) || bst2 != make_pair(-1ll, -1ll)) {
      if (bst.second * bst2.first >= bst2.second * bst.first) {
        cout << ((curM * pw(bst.first, MOD - 2, MOD)) % MOD * bst.second) % MOD;
      } else {
        cout << ((curM * pw(bst2.first, MOD - 2, MOD)) % MOD * bst2.second) %
                    MOD;
      }
      return 0;
    }

    if (z) {
      cout << "0";
      return 0;
    }

    reverse(vec.begin(), vec.end());
    ll vl = 1;
    for (int i = 0; i < k; i++) {
      vl *= (vec[i].second * vec[i].first + MOD) % MOD;
      vl %= MOD;
    }

    cout << vl;
    return 0;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 * 3, MOD = 1e9 + 7, INF = 1e9;
vector<vector<ll>> graph;
bool used[SIZE];
ll ans = 0;

ll pw(ll a, ll b, ll md = MOD) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (a * res) % md;
    }
    a = (a * a) % md;
    b >>= 1;
  }
  return (res);
}

int main() {
  fastInp;

  ll n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> vec;
  bool z = 0;
  for (int i = 0; i < n; i++) {
    ll num;
    cin >> num;
    if (num == 0) {
      z = 1;
      continue;
    }
    vec.push_back({abs(num), num / (abs(num))});
  }

  ll curCh = 1, cnt[2], curM = 1, mn[2];
  cnt[0] = 0;
  cnt[1] = 0;
  mn[0] = 0;
  mn[1] = 0;
  sort(vec.rbegin(), vec.rend());
  n = vec.size();
  for (int i = 0; i < k; i++) {
    if (vec[i].second == 1) {
      cnt[0]++;
      mn[0] = vec[i].second;
    } else {
      cnt[1]++;
      mn[1] = vec[i].second;
    }
    curCh *= vec[i].second;
    curM *= vec[i].first;
    curM %= MOD;
  }

  if (curCh == 1) {
    cout << curM;
    return 0;
  }

  if (curCh == -1) {
    pair<ll, ll> bst = {-1, -1};
    for (int i = k; i < n; i++) {
      if (vec[i].second == 1) {
        if (cnt[1] != 0) {
          if (bst == make_pair(-1ll, -1ll) ||
              (bst.first * bst.second) > (mn[1] * vec[i].first)) {
            bst = {mn[1], vec[i].first};
          }
        }
      } else if (vec[i].second == -1) {
        if (cnt[0] != 0) {
          if (bst == make_pair(-1ll, -1ll) ||
              (bst.first * bst.second) > (mn[0] * vec[i].first)) {
            bst = {mn[0], vec[i].first};
          }
        }
      }
    }

    if (bst != make_pair(-1ll, -1ll)) {
      cout << ((curM * pw(bst.first, MOD - 2, MOD)) % MOD * bst.second) % MOD;
      return 0;
    }

    if (z) {
      cout << "0";
      return 0;
    }

    reverse(vec.begin(), vec.end());
    ll vl = 1;
    for (int i = 0; i < k; i++) {
      vl *= (vec[i].second * vec[i].first + MOD) % MOD;
      vl %= MOD;
    }

    cout << vl;
    return 0;
  }
  return 0;
}