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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

ll ans = 1;
vector<ll> v[2];
int cnt0;

ll getnxt(int &p, int &q) {
  ll v1 = p < v[0].size() ? v[0][p] : -1;
  ll v2 = q < v[1].size() ? v[1][q] : -1;
  if (v1 < v2) {
    q++;
    return v2;
  } else {
    p++;
    return v1;
  }
}

ll power(int x, int n) {
  ll res = 1, now = x;
  for (; n; n >>= 1, now = now * now % mod)
    if (n & 1)
      res = res * now % mod;
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x > 0)
      v[0].pb(x);
    else if (x < 0)
      v[1].pb(-x);
    else
      cnt0++;
  }

  if (v[0].size() + v[1].size() < k) {
    cout << 0 << endl;
    return 0;
  }

  sort(v[0].begin(), v[0].end(), greater<ll>());
  sort(v[1].begin(), v[1].end(), greater<ll>());

  if (k == n - cnt0) {
    for (auto x : v[0])
      ans = ans * x % mod;
    for (auto x : v[1])
      ans = ans * (mod - x) % mod;
    if (cnt0 && v[1].size() % 2) {
      cout << 0 << endl;
    } else {
      cout << ans << endl;
    }
    return 0;
  }

  if (k % 2 == 1 && v[0].size() == 0) {
    if (cnt0) {
      cout << 0 << endl;
      return 0;
    }
    reverse(v[1].begin(), v[1].end());
    for (int i = 0; i < k; i++)
      ans = ans * (mod - v[1][i]) % mod;
    cout << ans << endl;
    return 0;
  }

  if (v[1].size() == 0) {
    for (int i = 0; i < k; i++)
      ans = ans * v[0][i] % mod;
    cout << ans << endl;
    return 0;
  }

  if (v[0].size() == 0) {
    for (int i = 0; i < k; i++)
      ans = ans * (mod - v[1][i]) % mod;
    cout << ans << endl;
    return 0;
  }

  int p = 0, q = 0;
  for (int i = 0; i < k; i++)
    ans = ans * getnxt(p, q) % mod;

  if (q % 2 == 1) {
    if (p < v[0].size() && q < v[1].size() && p > 0 && q > 0) {
      ll v1 = v[0][p] * v[0][p - 1];
      ll v2 = v[1][q] * v[1][q - 1];
      if (v1 >= v2) {
        ans *= v[0][p] * power(v[1][q - 1], mod - 2) % mod;
        ans %= mod;
      } else {
        ans *= power(v[0][p - 1], mod - 2) * v[1][q] % mod;
        ans %= mod;
      }
    } else if (p == v[0].size()) {
      ans *= power(v[0][p - 1], mod - 2) * v[1][q] % mod;
      ans %= mod;
    } else {
      ans *= v[0][p] * power(v[1][q - 1], mod - 2) % mod;
      ans %= mod;
    }
  }

  cout << ans << endl;
}


Error: -6
0d28932f-9309-471a-a503-7c4d85958b6c.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02616/C++/s585987376.cpp:108: int main(): Assertion `false' failed.


Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

ll ans = 1;
vector<ll> v[2];
int cnt0;

ll getnxt(int &p, int &q) {
  ll v1 = p < v[0].size() ? v[0][p] : -1;
  ll v2 = q < v[1].size() ? v[1][q] : -1;
  if (v1 < v2) {
    q++;
    return v2;
  } else {
    p++;
    return v1;
  }
}

ll power(int x, int n) {
  ll res = 1, now = x;
  for (; n; n >>= 1, now = now * now % mod)
    if (n & 1)
      res = res * now % mod;
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x > 0)
      v[0].pb(x);
    else if (x < 0)
      v[1].pb(-x);
    else
      cnt0++;
  }

  if (v[0].size() + v[1].size() < k) {
    cout << 0 << endl;
    return 0;
  }

  sort(v[0].begin(), v[0].end(), greater<int>());
  sort(v[1].begin(), v[1].end(), greater<int>());

  if (k == n - cnt0) {
    for (auto x : v[0])
      ans = ans * x % mod;
    for (auto x : v[1])
      ans = ans * (mod - x) % mod;
    if (cnt0 && v[1].size() % 2) {
      cout << 0 << endl;
    } else {
      cout << ans << endl;
    }
    return 0;
  }

  if (k % 2 == 1 && v[0].size() == 0) {
    if (cnt0) {
      cout << 0 << endl;
      return 0;
    }
    reverse(v[1].begin(), v[1].end());
    for (int i = 0; i < k; i++) {
      ans *= v[1][i];
      ans %= mod;
    }
    cout << (mod - ans) % mod << endl;
    return 0;
  }

  if (v[1].size() == 0) {
    for (int i = 0; i < k; i++)
      ans = ans * v[0][i] % mod;
    cout << ans << endl;
    return 0;
  }

  if (v[0].size() == 0) {
    for (int i = 0; i < k; i++)
      ans = ans * (mod - v[1][i]) % mod;
    cout << ans << endl;
    return 0;
  }

  int p = 0, q = 0;
  for (int i = 0; i < k; i++)
    ans = ans * getnxt(p, q) % mod;

  if (q % 2 == 1) {
    if (p < v[0].size() && q < v[1].size()) {
      ll v1 = v[0][p] * v[0][p - 1];
      ll v2 = v[1][q] * v[1][q - 1];
      if (v1 >= v2) {
        ans *= v[0][p] * power(v[1][q - 1], mod - 2) % mod;
        ans %= mod;
      } else {
        ans *= power(v[0][p - 1], mod - 2) * v[1][q] % mod;
        ans %= mod;
      }
    } else if (p == v[0].size()) {
      ans *= power(v[0][p - 1], mod - 2) * v[1][q] % mod;
      ans %= mod;
    } else {
      ans *= v[0][p] * power(v[1][q - 1], mod - 2) % mod;
      ans %= mod;
    }
  }

  cout << ans << endl;
  assert(false);
}