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

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define iFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define make_unique(a)                                                         \\
  sort(all((a))), (a).resize(unique(all((a))) - (a).begin())

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long UL; // ULLONG_MAX, %llu
typedef unsigned int UI;       // UINT_MAX %u
typedef double DD;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<DD, DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;

const int N = 200005;
const int mod = 1000000007;
int mul(int a, int b) { return a * 1ll * b % mod; }
vector<int> pos, neg;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k, zero = 0;
  int ans = 1, all = 1;
  cin >> n >> k;
  FOR(i, 1, n) {
    int a;
    cin >> a;
    if (a == 0)
      zero = 1;
    if (a > 0)
      pos.pb(a);
    else if (a < 0)
      neg.pb(a), a += mod;
    all = mul(all, a);
  }
  if (n == k) {
    cout << all;
    return 0;
  }
  sort(all(pos));
  sort(all(neg));
  if (SZ(pos) + SZ(neg) < k) {
    cout << 0;
    return 0;
  }
  if (pos.empty()) {
    if (k & 1) {
      if (zero)
        ans = 0;
      iFOR(i, SZ(neg) - 1, SZ(neg) - k) ans = mul(ans, mod + neg[i]);
    } else {
      FOR(i, 0, k - 1) ans = mul(ans, mod + neg[i]);
    }
    cout << ans;
    return 0;
  }
  if (neg.empty()) {
    iFOR(i, SZ(pos) - 1, SZ(pos) - k) ans = mul(ans, pos[i]);
    cout << ans;
    return 0;
  }
  if (SZ(pos) + SZ(neg) == k && SZ(neg) % 2 == 1) {
    cout << 0;
    return 0;
  } else if (SZ(pos) + SZ(neg) == k) {
    for (int e : pos)
      ans = mul(ans, e);
    for (int e : neg)
      ans = mul(ans, mod + e);
    cout << ans;
    return 0;
  }
  reverse(all(pos));
  int now = min(k - 1, SZ(pos) - 1);
  vector<LL> neg2;
  for (int i = 1; i < SZ(neg); i += 2) {
    neg2.pb(neg[i - 1] * 1ll * neg[i]);
  }
  int won = 0;
  for (int i = 0; i < SZ(neg2); ++i) {
    if (now + 1 + 2 * (i + 1) <= k)
      ++won;
    else {
      int p = now + 1 + 2 * (i + 1) - k;
      if (p > now + 1)
        break;
      LL A = 1;
      if (p == 1)
        A = pos[now];
      else
        A = pos[now] * 1ll * pos[now - 1];
      if (neg2[i] < A)
        break;
      ++won;
      now -= p;
      if (now == -1)
        break;
    }
  }
  FOR(i, 0, now) ans = mul(ans, pos[i]);
  FOR(i, 0, won - 1) ans = mul(ans, neg2[i] % mod);
  printf("%d", ans);
  return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */


Error: -6
26b94cc0-9b0a-413c-a394-fa84eb71fbd8.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02616/C++/s236622217.cpp:75: int main(): Assertion `0' failed.


Fixed submission:

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define iFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define make_unique(a)                                                         \\
  sort(all((a))), (a).resize(unique(all((a))) - (a).begin())

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long UL; // ULLONG_MAX, %llu
typedef unsigned int UI;       // UINT_MAX %u
typedef double DD;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<DD, DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;

const int N = 200005;
const int mod = 1000000007;
int mul(int a, int b) { return a * 1ll * b % mod; }
vector<int> pos, neg;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k, zero = 0;
  int ans = 1, all = 1;
  cin >> n >> k;
  FOR(i, 1, n) {
    int a;
    cin >> a;
    if (a == 0)
      zero = 1;
    if (a > 0)
      pos.pb(a);
    else if (a < 0)
      neg.pb(a);
    all = mul(all, a);
  }
  if (n == k) {
    cout << all;
    return 0;
  }
  sort(all(pos));
  sort(all(neg));
  if (SZ(pos) + SZ(neg) < k) {
    cout << 0;
    return 0;
  }
  if (pos.empty()) {
    if (k & 1) {
      if (zero)
        ans = 0;
      iFOR(i, SZ(neg) - 1, SZ(neg) - k) ans = mul(ans, mod + neg[i]);
    } else {
      FOR(i, 0, k - 1) ans = mul(ans, mod + neg[i]);
    }
    cout << ans;
    return 0;
  }
  if (neg.empty()) {
    iFOR(i, SZ(pos) - 1, SZ(pos) - k) ans = mul(ans, pos[i]);
    cout << ans;
    return 0;
  }
  if (SZ(pos) + SZ(neg) == k && SZ(neg) % 2 == 1) {
    cout << 0;
    return 0;
  } else if (SZ(pos) + SZ(neg) == k) {
    for (int e : pos)
      ans = mul(ans, e);
    for (int e : neg)
      ans = mul(ans, mod + e);
    cout << ans;
    return 0;
  }
  assert(0);
  reverse(all(pos));
  int now = min(k - 1, SZ(pos) - 1);
  vector<LL> neg2;
  for (int i = 1; i < SZ(neg); i += 2) {
    neg2.pb(neg[i - 1] * 1ll * neg[i]);
  }
  int won = 0;
  for (int i = 0; i < SZ(neg2); ++i) {
    if (now + 1 + 2 * (i + 1) <= k)
      ++won;
    else {
      int p = now + 1 + 2 * (i + 1) - k;
      LL A = 1;
      if (p == 1)
        A = pos[now];
      else
        A = pos[now] * 1ll * pos[now - 1];
      if (neg2[i] < A)
        break;
      ++won;
      now -= p;
      if (now == -1)
        break;
    }
  }
  FOR(i, 0, now) ans = mul(ans, pos[i]);
  FOR(i, 0, won - 1) ans = mul(ans, neg2[i] % mod);
  printf("%d", ans);
  return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */