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

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ldouble long double
using namespace std;

const lli mod = 1e9 + 7, inf = LONG_LONG_MAX;
const int maxN = 2e5;
int n, k;
lli a[maxN];
vector<lli> neg, pos;
vector<double> nacc, pacc;

int main() {
  while (scanf("%d %d", &n, &k) != EOF) {
    neg.clear(), pos.clear();
    int zeros = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
      if (a[i] < 0)
        neg.push_back(a[i]);
      else if (a[i] == 0)
        zeros++;
      else
        pos.push_back(a[i]);
    }
    sort(neg.begin(), neg.end());
    sort(pos.begin(), pos.end());
    reverse(pos.begin(), pos.end());

    nacc.resize(neg.size() + 1);
    nacc[0] = 0;
    for (int i = 0; i < neg.size(); i++)
      nacc[i + 1] = nacc[i] + log10(-neg[i]);

    pacc.resize(pos.size() + 1);
    pacc[0] = 0;
    for (int i = 0; i < pos.size(); i++)
      pacc[i + 1] = pacc[i] + log10(pos[i]);

    lli hehe = 0;
    if (neg.size() + pos.size() >= k) {
      double ans = -1;
      int ww = 0, wi, wj;
      for (int i = 0; i <= pos.size() && i <= k; i++)
        if (!((k - i) & 1) && (k - i) <= neg.size()) {
          double w = pacc[i] + nacc[k - i];
          if (w > ans)
            ans = w, wi = i, wj = k - i, ww = 1;
        }
      if (ww) {
        hehe = 1;
        for (int i = 0; i < wi; i++)
          hehe = hehe * pos[i] % mod;
        for (int j = 0; j < wj; j++)
          hehe = (hehe * neg[j] % mod + mod) % mod;
      }

      if (ww == 0 && zeros == 0) {
        reverse(pos.begin(), pos.end());
        reverse(neg.begin(), neg.end());

        nacc.resize(neg.size() + 1);
        nacc[0] = 0;
        for (int i = 0; i < neg.size(); i++)
          nacc[i + 1] = nacc[i] + log(-neg[i]);

        nacc.resize(neg.size() + 1);
        nacc[0] = 0;
        for (int i = 0; i < neg.size(); i++)
          nacc[i + 1] = nacc[i] + log(-neg[i]);

        ans = inf;
        for (int i = 0; i <= pos.size() && i <= k; i++)
          if (((k - i) & 1) && (k - i) <= neg.size()) {
            double w = pacc[i] + nacc[k - i];
            if (w < ans)
              ans = w, wi = i, wj = k - i, ww = 1;
          }
        if (ww) {
          hehe = 1;
          for (int i = 0; i < wi; i++)
            hehe = hehe * pos[i] % mod;
          for (int j = 0; j < wj; j++)
            hehe = (hehe * neg[j] % mod + mod) % mod;
        }
      }
    }

    printf("%lld\\n", hehe);
  }
  return 0;
}

Error: -6
30ce9f28-c691-427e-b075-daf0a056528a.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02616/C++/s931452602.cpp:57: int main(): Assertion `0' failed.


Fixed submission:

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ldouble long double
using namespace std;

const lli mod = 1e9 + 7, inf = LONG_LONG_MAX;
const int maxN = 2e5;
int n, k;
lli a[maxN];
vector<lli> neg, pos;
vector<double> nacc, pacc;

int main() {
  while (scanf("%d %d", &n, &k) != EOF) {
    neg.clear(), pos.clear();
    int zeros = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
      if (a[i] < 0)
        neg.push_back(a[i]);
      else if (a[i] == 0)
        zeros++;
      else
        pos.push_back(a[i]);
    }
    sort(neg.begin(), neg.end());
    sort(pos.begin(), pos.end());
    reverse(pos.begin(), pos.end());

    nacc.resize(neg.size() + 1);
    nacc[0] = 0;
    for (int i = 0; i < neg.size(); i++)
      nacc[i + 1] = nacc[i] + log(-neg[i]);

    pacc.resize(pos.size() + 1);
    pacc[0] = 0;
    for (int i = 0; i < pos.size(); i++)
      pacc[i + 1] = pacc[i] + log(pos[i]);

    lli hehe = 0;
    if (neg.size() + pos.size() >= k) {
      double ans = -1;
      int ww = 0, wi, wj;
      for (int i = 0; i <= pos.size() && i <= k; i++)
        if (!((k - i) & 1) && (k - i) <= neg.size()) {
          double w = pacc[i] + nacc[k - i];
          if (w > ans)
            ans = w, wi = i, wj = k - i, ww = 1;
        }
      if (ww) {
        hehe = 1;
        for (int i = 0; i < wi; i++)
          hehe = hehe * pos[i] % mod;
        for (int j = 0; j < wj; j++)
          hehe = (hehe * neg[j] % mod + mod) % mod;
        assert(0);
      }

      if (ww == 0 && zeros == 0) {
        reverse(pos.begin(), pos.end());
        reverse(neg.begin(), neg.end());

        nacc.resize(neg.size() + 1);
        nacc[0] = 0;
        for (int i = 0; i < neg.size(); i++)
          nacc[i + 1] = nacc[i] + log(-neg[i]);

        nacc.resize(neg.size() + 1);
        nacc[0] = 0;
        for (int i = 0; i < neg.size(); i++)
          nacc[i + 1] = nacc[i] + log(-neg[i]);

        ans = inf;
        for (int i = 0; i <= pos.size() && i <= k; i++)
          if (((k - i) & 1) && (k - i) <= neg.size()) {
            double w = pacc[i] + nacc[k - i];
            if (w < ans)
              ans = w, wi = i, wj = k - i, ww = 1;
          }
        if (ww) {
          hehe = 1;
          for (int i = 0; i < wi; i++)
            hehe = hehe * pos[i] % mod;
          for (int j = 0; j < wj; j++)
            hehe = (hehe * neg[j] % mod + mod) % mod;
        }
      }
    }

    printf("%lld\\n", hehe);
  }
  return 0;
}