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

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  ll ans = 1;
  vector<ll> minus, plus;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0) {
      minus.push_back(a[i]);
    }
    if (a[i] > 0) {
      plus.push_back(a[i]);
    }
  }
  sort(minus.begin(), minus.end());
  sort(plus.begin(), plus.end(), greater<ll>());
  ll zero = n - minus.size() - plus.size();

  if (k > minus.size() + plus.size()) {
    cout << 0 << endl;
    return 0;
  }

  if (plus.size() == 0 && k % 2 == 1) {
    if (zero > 0) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k) {
      ll index = minus.size() - i - 1;
      ans *= -minus[index];
      ans %= mod;
    }
    ans = -ans + mod * 2;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }

  ll minus_index = 0, plus_index = 0;
  ll minus_remain = minus.size(), plus_remain = plus.size();
  if (k % 2 == 1) {
    ans *= plus[plus_index];
    ans %= mod;
    plus_index++;
    plus_remain--;
    k--;
  }

  while (k > 0) {
    if (minus_remain == 1 && plus_remain == 1) {
      ans *= plus[plus_index];
      ans %= mod;
      ans *= minus[minus_index];
      while (ans < 0) {
        ans += mod;
      }
      ans %= mod;
      break;
    }

    if (minus_remain < 2) {
      ans *= plus[plus_index];
      ans %= mod;
      ans *= plus[plus_index + 1];
      ans %= mod;
      plus_index += 2;
      plus_remain -= 2;
      k -= 2;
      continue;
    }
    if (plus_remain < 2) {
      ans *= -minus[minus_index];
      ans %= mod;
      ans *= -minus[minus_index + 1];
      ans %= mod;
      minus_index += 2;
      minus_remain -= 2;
      k -= 2;
      continue;
    }
    if (plus[plus_index] * plus[plus_index + 1] >
        minus[minus_index] * minus[minus_index + 1]) {
      ans *= plus[plus_index];
      ans %= mod;
      ans *= plus[plus_index + 1];
      ans %= mod;
      plus_index += 2;
      plus_remain -= 2;
      k -= 2;
    } else {
      ans *= -minus[minus_index];
      ans %= mod;
      ans *= -minus[minus_index + 1];
      ans %= mod;
      minus_index += 2;
      minus_remain -= 2;
      k -= 2;
    }
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  ll ans = 1;
  vector<ll> minus, plus;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0) {
      minus.push_back(a[i]);
    }
    if (a[i] > 0) {
      plus.push_back(a[i]);
    }
  }
  sort(minus.begin(), minus.end());
  sort(plus.begin(), plus.end(), greater<ll>());
  ll zero = n - minus.size() - plus.size();

  if (k > minus.size() + plus.size()) {
    cout << 0 << endl;
    return 0;
  }

  if (plus.size() == 0 && k % 2 == 1) {
    if (zero > 0) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k) {
      ll index = minus.size() - i - 1;
      ans *= -minus[index];
      ans %= mod;
    }
    ans = -ans + mod * 2;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }

  ll minus_index = 0, plus_index = 0;
  ll minus_remain = minus.size(), plus_remain = plus.size();
  if (k % 2 == 1) {
    ans *= plus[plus_index];
    ans %= mod;
    plus_index++;
    plus_remain--;
    k--;
  }

  while (k > 0) {
    if (minus_remain == 1 && plus_remain == 1) {
      return 1;
    }

    if (minus_remain < 2) {
      ans *= plus[plus_index];
      ans %= mod;
      ans *= plus[plus_index + 1];
      ans %= mod;
      plus_index += 2;
      plus_remain -= 2;
      k -= 2;
      continue;
    }
    if (plus_remain < 2) {
      ans *= -minus[minus_index];
      ans %= mod;
      ans *= -minus[minus_index + 1];
      ans %= mod;
      minus_index += 2;
      minus_remain -= 2;
      k -= 2;
      continue;
    }
    if (plus[plus_index] * plus[plus_index + 1] >
        minus[minus_index] * minus[minus_index + 1]) {
      ans *= plus[plus_index];
      ans %= mod;
      ans *= plus[plus_index + 1];
      ans %= mod;
      plus_index += 2;
      plus_remain -= 2;
      k -= 2;
    } else {
      ans *= -minus[minus_index];
      ans %= mod;
      ans *= -minus[minus_index + 1];
      ans %= mod;
      minus_index += 2;
      minus_remain -= 2;
      k -= 2;
    }
  }

  cout << ans << endl;
  return 0;
}