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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  ll num;
  cin >> n >> k;
  vector<ll> ap, an, a;
  for (int i = 0; i < n; ++i) {
    cin >> num;
    a.push_back(abs(num));
    if (num > 0)
      ap.push_back(num);
    else if (num < 0)
      an.push_back(-num);
  }

  // 答えが 0 にしかならない
  if (ap.size() == 0 && an.size() == 0) {
    cout << 0 << endl;
    return 0;
  }

  // 答えを正の数にできる？
  ll ans = 1LL;
  sort(ap.rbegin(), ap.rend());
  sort(an.rbegin(), an.rend());
  int pi = 0, ni = 0, kk = 0;
  while (kk < k - 1 && (pi < int(ap.size()) - 1 || ni < int(an.size()) - 1)) {
    if (ni >= int(an.size()) - 1 ||
        (pi < int(ap.size()) - 1 &&
         ap[pi + 1] * ap[pi] > an[ni + 1] * an[ni])) {
      ans *= (ap[pi + 1] * ap[pi]) % MOD;
      pi += 2;
    } else {
      ans *= (an[ni + 1] * an[ni]) % MOD;
      ni += 2;
    }
    ans %= MOD;
    kk += 2;
  }
  while (kk < k && pi < int(ap.size())) {
    ans *= ap[pi++];
    ans %= MOD;
    ++kk;
  }
  if (kk == k) {
    cout << ans << endl;
    return 0;
  }

  // 答えを正にできないため，できるだけ絶対値を抑える
  sort(a.begin(), a.end());
  ans = 1LL;
  for (int i = 0; i < k; ++i) {
    ans *= a[i];
    ans %= MOD;
  }
  cout << (MOD - ans) % MOD << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  ll num;
  cin >> n >> k;
  vector<ll> ap, an, a;
  for (int i = 0; i < n; ++i) {
    cin >> num;
    a.push_back(abs(num));
    if (num > 0)
      ap.push_back(num);
    else if (num < 0)
      an.push_back(-num);
  }

  // 答えが 0 にしかならない
  if (ap.size() == 0 && an.size() == 0) {
    cout << 0 << endl;
    return 0;
  }

  // 答えを正の数にできる？
  ll ans = 1LL;
  sort(ap.rbegin(), ap.rend());
  sort(an.rbegin(), an.rend());
  int pi = 0, ni = 0, kk = 0;
  while (kk < k && (pi < int(ap.size()) - 1 || ni < int(an.size()) - 1)) {
    if (ni >= int(an.size()) - 1 ||
        (pi < int(ap.size()) - 1 &&
         ap[pi + 1] * ap[pi] > an[ni + 1] * an[ni])) {
      ans *= (ap[pi + 1] * ap[pi]) % MOD;
      pi += 2;
    } else {
      ans *= (an[ni + 1] * an[ni]) % MOD;
      ni += 2;
    }
    ans %= MOD;
    kk += 2;
  }
  if (kk + 1 == k && pi < int(ap.size())) {
    ans *= ap[pi++];
    ans %= MOD;
    ++kk;
  }
  if (kk == k) {
    cout << ans << endl;
    return 0;
  }

  // 答えを正にできないため，できるだけ絶対値を抑える
  assert(false);
  sort(a.begin(), a.end());
  ans = 1LL;
  for (int i = 0; i < k; ++i) {
    ans *= a[i];
    ans %= MOD;
  }
  cout << (MOD - ans) % MOD << endl;
}