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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N = pow(10, 9) + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> a(n);
  bool all_minus = true;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x;
    if (x >= 0)
      all_minus = false;
    y = abs(x);
    pair<ll, ll> p = make_pair(y, x);
    a.at(i) = p;
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  ll ans = 1;
  for (int i = 0; i < k; i++) {
    ans = (ans * a.at(i).second) % N;
  }
  if (k == n || ans >= 0) {
    if (ans < 0)
      ans = N + ans;
    cout << ans << endl;
    return 0;
  }
  if (k % 2 == 1 && all_minus) {
    ll ans2 = 1;
    for (int i = 1; i <= k; i++) {
      ans2 = (ans2 * a.at(n - i).first) % N;
    }
    cout << N - ans2 << endl;
    return 0;
  }
  ll plus_in = -1, plus_out = -1, minus_in = 1, minus_out = 1;
  ll p, q;
  for (int i = 1; i <= k; i++) {
    if (a.at(k - i).second >= 0 && plus_out < 0)
      plus_out = a.at(k - i).second;
    if (a.at(k - i).second < 0 && minus_out > 0)
      minus_out = a.at(k - i).second;
  }
  for (int i = k; i < n; i++) {
    if (a.at(i).second >= 0 && plus_in < 0)
      plus_in = a.at(i).second;
    if (a.at(i).second < 0 && minus_in > 0)
      minus_in = a.at(i).second;
  }
  if (plus_out < 0 || minus_in > 0) {
    p = minus_out;
    q = plus_in;
  } else if (minus_out > 0 || plus_in < 0) {
    p = plus_out;
    q = minus_in;
  } else {
    if (abs(minus_in * minus_out) <= abs(plus_in * plus_out)) {
      p = minus_out;
      q = plus_in;
    } else {
      p = plus_out;
      q = minus_in;
    }
  }
  // cout << p << " " << q << endl;
  ans = 1;
  bool first_time = true;
  for (int i = 0; i < k; i++) {
    if (a.at(i).second == p && first_time == true) {
      first_time = false;
      continue;
    }
    ans = (ans * a.at(i).second) % N;
  }
  ans = (ans * q) % N;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t N = 1000000007;
  vector<int64_t> plus;
  vector<int64_t> minus;
  vector<pair<int64_t, int>> P;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int64_t a;
    cin >> a;
    pair<int64_t, int> p;
    if (a >= 0) {
      plus.push_back(a);
      p.first = a;
      p.second = 1;
    } else {
      minus.push_back(a);
      p.first = -a;
      p.second = -1;
    }
    P.push_back(p);
  }
  sort(P.begin(), P.end());
  reverse(P.begin(), P.end());
  sort(plus.begin(), plus.end());
  reverse(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());
  int check = 1;
  int use_plus = 0;
  int64_t ans = 1;
  for (int i = 0; i < k; i++) {
    ans = (ans * P.at(i).first) % N;
    check = check * P.at(i).second;
    if (P.at(i).second == 1)
      use_plus++;
  }
  if (check == 1) {
    cout << ans << endl;
    return 0;
  } else {
    int s, t;
    s = use_plus;
    t = k - s;
    if (ans == 0) {
      cout << 0 << endl;
      return 0;
    } else if (k == n) {
      ans = 1;
      for (int i = 0; i < s; i++) {
        ans = (ans * plus.at(i)) % N;
      }
      for (int i = 0; i < t; i++) {
        ans = (ans * minus.at(i)) % N;
      }
      cout << ans + N << endl;
      return 0;
    } else if (plus.size() == 0) {
      reverse(minus.begin(), minus.end());
      ans = 1;
      for (int f = 0; f < t; f++) {
        ans = (ans * minus.at(f)) % N;
      }
      cout << ans + N << endl;
      return 0;
    } else if (s == (int)plus.size()) {
      s--;
      t++;
    } else if (t == (int)minus.size()) {
      s++;
      t--;
    } else {
      if (plus.at(s - 1) * plus.at(s) > minus.at(t - 1) * minus.at(t)) {
        s++;
        t--;
      } else {
        s--;
        t++;
      }
    }
    ans = 1;
    for (int i = 0; i < s; i++) {
      ans = (ans * plus.at(i)) % N;
    }
    for (int i = 0; i < t; i++) {
      ans = (ans * abs(minus.at(i))) % N;
    }
    cout << ans << endl;
    return 0;
  }
}