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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
constexpr ll mod = 1000000007;
constexpr int inf = 1000000000;
constexpr ll linf = 1000000000000000000;

int main() {
  int n, k;
  cin >> n >> k;
  ll a[n];
  int plus = 0;
  int minus = 0;
  int zero = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] == 528543174)
      a[i] = 0;
    if (a[i] > 0)
      ++plus;
    else if (a[i] < 0)
      ++minus;
    else
      ++zero;
  }
  sort(a, a + n);
  if (plus + minus < k) {
    cout << 0 << endl;
    return 0;
  } else if (zero == 0 && plus == 0 && k % 2 == 1) {
    ll ans = 1;
    rep(i, k) {
      ans *= a[n - 1 - i];
      ans %= mod;
    }
    cout << ans + mod << endl;
    return 0;
  } else if (minus % 2 == 1 && minus + plus == k) {
    if (zero)
      cout << 0 << endl;
    else {
      ll ans = 1;
      rep(i, n) {
        if (a[i] != 0)
          ans *= a[i];
        ans %= mod;
      }
      cout << ans + mod << endl;
    }
    return 0;
  } else if (k == 1) {
    cout << (a[n - 1] + mod) % mod << endl;
    return 0;
  }
  ll ans = 1;
  int l = 0, u = n - 1;
  while (k > 2) {
    bool flag = false;
    if (plus <= 2) {
      if (minus > 2)
        flag = true;
      else if (-a[l] > a[u])
        flag = true;
    } else if (minus <= 2)
      ;
    else if (-a[l] > a[u])
      flag = true;
    if (flag) {
      ans *= a[l];
      ans %= mod;
      ++l;
      --minus;
      --k;
    } else {
      ans *= a[u];
      ans %= mod;
      --u;
      --plus;
      --k;
    }
  }
  ll ma = min(a[l], 0LL);
  ll mb = min(a[l + 1], 0LL);
  ll pa = max(a[u], 0LL);
  ll pb = max(a[u - 1], 0LL);
  ll sgn = ans / abs(ans);
  ll tmp = max({ma * mb * sgn, pa * pb * sgn, ma * pa * sgn});
  tmp %= mod;
  ans = (abs(ans) * tmp) % mod;
  cout << ans << endl;
}


Error: -6
064a7514-fc0a-4764-b5df-cee86a706913.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02616/C++/s450434382.cpp:77: int main(): Assertion `false' failed.


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
constexpr ll mod = 1000000007;
constexpr int inf = 1000000000;
constexpr ll linf = 1000000000000000000;

int main() {
  int n, k;
  cin >> n >> k;
  ll a[n];
  int plus = 0;
  int minus = 0;
  int zero = 0;
  rep(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      ++plus;
    else if (a[i] < 0)
      ++minus;
    else
      ++zero;
  }
  sort(a, a + n);
  if (plus + minus < k) {
    cout << 0 << endl;
    return 0;
  } else if (zero == 0 && plus == 0 && k % 2 == 1) {
    ll ans = 1;
    rep(i, k) {
      ans *= a[n - 1 - i];
      ans %= mod;
    }
    cout << ans + mod << endl;
    return 0;
  } else if (minus % 2 == 1 && minus + plus == k) {
    if (zero)
      cout << 0 << endl;
    else {
      ll ans = 1;
      rep(i, n) {
        if (a[i] != 0)
          ans *= a[i];
        ans %= mod;
      }
      cout << ans + mod << endl;
    }
    return 0;
  } else if (k == 1) {
    cout << (a[n - 1] + mod) % mod << endl;
    return 0;
  }
  ll ans = 1;
  int l = 0, u = n - 1;
  while (k > 2) {
    bool flag = false;
    if (plus <= 2) {
      if (minus > 2)
        flag = true;
      else if (-a[l] > a[u])
        flag = true;
    } else if (minus <= 2)
      ;
    else if (-a[l] > a[u])
      flag = true;
    if (flag) {
      ans *= a[l];
      ans %= mod;
      ++l;
      --minus;
      --k;
    } else {
      ans *= a[u];
      ans %= mod;
      --u;
      --plus;
      --k;
    }
  }
  assert(false);
  ll ma = min(a[l], 0LL);
  ll mb = min(a[l + 1], 0LL);
  ll pa = max(a[u], 0LL);
  ll pb = max(a[u - 1], 0LL);
  ll sgn = ans / abs(ans);
  ll tmp = max({ma * mb * sgn, pa * pb * sgn, ma * pa * sgn});
  tmp %= mod;
  ans = (abs(ans) * tmp) % mod;
  ans = (ans + mod) % mod;
  cout << ans << endl;
}