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

// E
#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  vector<long long> a, b;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    if (x < 0)
      b.push_back(-x);
    else
      a.push_back(x);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end(), greater<long long>());

  long long ans = 1;
  if (n == k) {
    for (int i = 0; i < a.size(); i++)
      ans = ans * a[i] % MOD;
    for (int i = 0; i < b.size(); i++)
      ans = ans * b[i] % MOD;
    if (b.size() % 2)
      ans = (MOD - ans) % MOD;
  } else if (a.size() == 0) {
    if (k % 2) {
      for (int i = 0; i < k; i++)
        ans = ans * b[b.size() - 1 - i] % MOD;
      ans = (MOD - ans) % MOD;
    } else {
      for (int i = 0; i < k; i++)
        ans = ans * b[i] % MOD;
    }
  } else {
    ans = 0;
    vector<long double> x(1, 0), y(1, 0);
    for (int i = a.size() - 1; i >= 0; i--)
      if (a[i] != 0)
        x.push_back(logl(a[i]));
    for (int i = 0; i < b.size(); i++)
      if (b[i] != 0)
        y.push_back(logl(b[i]));

    for (int i = 1; i < x.size(); i++)
      x[i] += x[i - 1];
    for (int i = 1; i < y.size(); i++)
      y[i] += y[i - 1];

    int best_i = -1;
    long double best = -1;
    for (int i = 0; i < y.size() && i <= k; i += 2) {
      long double c = y[i];
      if (k - i >= 0 && k - i < x.size()) {
        c += x[k - i];
        if (best_i < 0 || c > best) {
          best_i = i;
          best = c;
        }
      }
    }
    if (best_i >= 0) {
      int i = best_i;
      ans = 1;
      for (int j = 0; j < i; j++)
        ans = ans * b[j] % MOD;
      for (int j = 0; j < (k - i); j++)
        ans = ans * a[a.size() - 1 - j] % MOD;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// E
#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  vector<long long> a, b;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    if (x < 0)
      b.push_back(x);
    else
      a.push_back(x);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  long long ans = 1;
  if (a.size() == 0) {
    if (k % 2) {
      for (int i = 0; i < k; i++)
        ans = ans * (-b[b.size() - 1 - i]) % MOD;
      ans = (-ans + MOD) % MOD;
    } else {
      for (int i = 0; i < k; i++)
        ans = ans * (-b[i]) % MOD;
    }
  } else {
    int nz = 0;
    for (int i = 0; i < a.size(); i++)
      if (a[i] == 0)
        nz++;
      else
        break;
    if (n - nz < k) {
      ans = 0;
    } else {
      vector<long double> x(1, 0), y(1, 0);
      for (int i = a.size() - 1; i >= 0; i--)
        if (a[i] != 0)
          x.push_back(logl(a[i]));
      for (int i = 0; i < b.size(); i++)
        if (b[i] != 0)
          y.push_back(logl(-b[i]));

      for (int i = 1; i < x.size(); i++)
        x[i] += x[i - 1];
      for (int i = 1; i < y.size(); i++)
        y[i] += y[i - 1];

      long double best = 0;
      for (int i = 0; i < y.size(); i += 2) {
        long double c = y[i];
        if (k - i >= 0) {
          c += x[k - i];
          if (c > best) {
            best = c;
            ans = 1;
            for (int j = 0; j < i; j++)
              ans = ans * (-b[j]) % MOD;
            for (int j = 0; j < (k - i); j++)
              ans = ans * a[a.size() - 1 - j] % MOD;
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}