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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> ap;
  vector<ll> am;
  rep(i, n) {
    ll tmp_a;
    cin >> tmp_a;
    if (tmp_a < 0) {
      am.push_back(abs(tmp_a));
    } else {
      ap.push_back(tmp_a);
    }
  }
  int ap_size = ap.size();
  int am_size = am.size();
  bool possible_plus = false;
  if (ap_size == 0) {
    if (!(k & 1))
      possible_plus = true;
  } else {
    if (n == k)
      possible_plus = !(am_size & 1);
    else
      possible_plus = true;
  }
  sort(ap.begin(), ap.end());
  sort(am.begin(), am.end());
  if (possible_plus) {
    reverse(ap.begin(), ap.end());
    reverse(am.begin(), am.end());
  }
  ll ans = 1;
  if (!possible_plus) {
    int idx_p = 0;
    int idx_m = 0;
    while (1) {
      if (idx_p + idx_m == k)
        break;
      if (idx_p == ap_size) {
        ans = (ans * am[idx_m]) % mod;
        ++idx_m;
      } else if (idx_m == am_size) {
        ans = (ans * ap[idx_p]) % mod;
        ++idx_p;
      } else if (ap[idx_p] < am[idx_m]) {
        ans = (ans * ap[idx_p]) % mod;
        ++idx_p;
      } else {
        ans = (ans * am[idx_m]) % mod;
        ++idx_m;
      }
    }
    // cout << ans << endl;
    cout << mod - ans << endl;
    return 0;
  } else {
    int idx_p = 0;
    int idx_m = 0;
    if (k & 1) {
      ans = (ans * ap[0]) % mod;
      idx_p = 1;
      --k;
    }
    vector<ll> a_two_pair;
    while (1) {
      if (idx_p + 1 >= ap_size)
        break;
      a_two_pair.push_back(ap[idx_p] * ap[idx_p + 1]);
      idx_p += 2;
    }

    while (1) {
      if (idx_m + 1 >= am_size || am.empty())
        break;
      a_two_pair.push_back(am[idx_m] * am[idx_m + 1]);
      idx_m += 2;
    }
    sort(a_two_pair.rbegin(), a_two_pair.rend());
    // for (auto t: a_two_pair) {
    //     cout << t << "  ";
    // }
    // cout << endl;
    // cout << ans << endl;
    rep(i, k / 2) {
      ll tmp = a_two_pair[i] % mod;
      ans = (ans * tmp) % mod;
      // cout << ans << endl;
    }
    cout << ans << endl;
    return 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> ap;
  vector<ll> am;
  int m_cnt = 0;
  rep(i, n) {
    ll tmp_a;
    cin >> tmp_a;
    if (tmp_a < 0) {
      ++m_cnt;
      am.push_back(abs(tmp_a));
    } else {
      ap.push_back(tmp_a);
    }
  }
  bool possible_plus = false;
  if (m_cnt == n) {
    if (!(k & 1))
      possible_plus = true;
  } else {
    if (k < n)
      possible_plus = true;
    else if (!(k & 1))
      possible_plus = true;
  }
  sort(ap.begin(), ap.end());
  sort(am.begin(), am.end());
  if (possible_plus) {
    reverse(ap.begin(), ap.end());
    reverse(am.begin(), am.end());
  }
  ll ans = 1;
  if (!possible_plus) {
    int idx_p = 0;
    int idx_m = 0;
    while (1) {
      if (idx_p + idx_m == k)
        break;
      if (idx_p == (int)ap.size()) {
        ans = (ans * am[idx_m]) % mod;
        ++idx_m;
      } else if (idx_m == (int)am.size()) {
        ans = (ans * ap[idx_p]) % mod;
        ++idx_p;
      } else if (ap[idx_p] < am[idx_m]) {
        ans = (ans * ap[idx_p]) % mod;
        ++idx_p;
      } else {
        ans = (ans * am[idx_m]) % mod;
        ++idx_m;
      }
    }
    cout << ans << endl;
    cout << mod - ans << endl;
    return 0;
  } else {
    int idx_p = 0;
    int idx_m = 0;
    if (k & 1) {
      ans = (ans * ap[0]) % mod;
      idx_p = 1;
      --k;
    }
    while (1) {
      if (idx_p + idx_m == k)
        break;
      if (idx_p + 1 >= (int)ap.size()) {
        ans = (ans * am[idx_m]) % mod;
        ans = (ans * am[idx_m + 1]) % mod;
        idx_m += 2;
      } else if (idx_m + 1 >= (int)am.size()) {
        ans = (ans * ap[idx_p]) % mod;
        ans = (ans * ap[idx_p + 1]) % mod;
        idx_p += 2;
      } else if (ap[idx_p] * ap[idx_p + 1] < am[idx_m] * am[idx_m + 1]) {
        ans = (ans * am[idx_m]) % mod;
        ans = (ans * am[idx_m + 1]) % mod;
        idx_m += 2;
      } else {
        ans = (ans * ap[idx_p]) % mod;
        ans = (ans * am[idx_p + 1]) % mod;
        idx_p += 2;
      }
    }
    cout << ans << endl;
    return 0;
  }
  return 0;
}