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

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/05/20 20:45:24
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;

template <long long MOD> class modular {
private:
  long long val_;

public:
  constexpr modular() : val_(0) {}
  constexpr modular(long long v) : val_(v % MOD) {
    if (val_ < 0)
      val_ += MOD;
  }

  constexpr long long val() const { return val_; }
  constexpr modular operator+(modular x) const {
    return (val() + x.val()) % MOD;
  }
  constexpr modular operator+=(modular x) { return *this = *this + x; }
  constexpr modular operator++() { return *this += 1; }
  constexpr modular operator++(int) {
    modular c = *this;
    ++(*this);
    return c;
  }
  constexpr modular operator-(modular x) const {
    return (val() + MOD - x.val()) % MOD;
  }
  constexpr modular operator-=(modular x) { return *this = *this - x; }
  constexpr modular operator--() { return *this -= 1; }
  constexpr modular operator--(int) {
    modular c = *this;
    --(*this);
    return c;
  }
  constexpr modular operator*(modular x) const {
    return (val() * x.val()) % MOD;
  }
  constexpr modular operator*=(modular x) { return *this = *this * x; }
  friend constexpr modular pow(modular x, long long p) {
    modular t = 1;
    modular u = x;
    while (p) {
      if (p & 1)
        t *= u;
      u *= u;
      p >>= 1;
    }
    return t;
  }
  constexpr modular operator/(modular x) const {
    return *this * pow(modular{x}, MOD - 2);
  }
  constexpr modular operator/=(modular x) { return *this = *this / x; }

  constexpr bool operator==(modular x) const { return val() == x.val(); }
  constexpr bool operator!=(modular x) const { return val() != x.val(); }
  constexpr bool operator<(modular x) const { return val() < x.val(); };
  constexpr bool operator<=(modular x) const { return val() <= x.val(); };
  constexpr bool operator>(modular x) const { return val() > x.val(); };
  constexpr bool operator>=(modular x) const { return val() >= x.val(); };

  friend std::ostream &operator<<(std::ostream &os, const modular &x) {
    return os << x.val();
  }
  friend std::istream &operator>>(std::istream &is, modular &x) {
    return is >> x.val_;
  }
};

constexpr long long MOD = 1e9 + 7;
using mint = modular<MOD>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> V(n);
  for (auto &v : V)
    cin >> v;
  sort(V.begin(), V.end());
  mint ans = 1;
  long long d = 1;
  int s = 0;
  if (k % 2 == 1) {
    ans = V.back();
    if (V.back() < 0)
      d = -1;
    s++;
    V.pop_back();
  }
  int l = 0, r = V.size() - 1;
  while (s < k) {
    auto a = (long long)V[l] * V[l + 1];
    auto b = (long long)V[r] * V[r - 1];
    if (a * d > b * d) {
      ans *= a;
      l += 2;
    } else {
      ans *= b;
      r -= 2;
    }
    s += 2;
  }

  cout << ans << endl;

  return 0;
}


Error: 0
0.693147
2.48491


Fixed submission:

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/05/20 20:45:24
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MOD = 1e9 + 7;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<pair<ll, double>> N, M;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a >= 0) {
      N.emplace_back(a, log((double)a));
    } else {
      M.emplace_back(-a, log((double)-a));
    }
  }
  sort(N.rbegin(), N.rend());
  sort(M.rbegin(), M.rend());
  for (int i = 1; i < N.size(); i++) {
    (N[i].first *= N[i - 1].first) %= MOD;
    N[i].second += N[i - 1].second;
  }
  int ans = 0;
  constexpr int mo = (-1 + MOD) % MOD;
  if (n == k) {
    for (int i = 1; i < M.size(); i++) {
      (M[i].first *= M[i - 1].first) %= MOD;
      M[i].second += M[i - 1].second;
    }
    ans = 1;
    if (N.size() > 0)
      ans *= N.back().first;
    ans %= MOD;
    if (M.size() > 0)
      ans *= M.back().first * mo % MOD;
    ans %= MOD;
  } else if (N.size() == 0 and k % 2 == 1) {
    reverse(M.begin(), M.end());
    for (int i = 1; i < M.size(); i++) {
      (M[i].first *= M[i - 1].first) %= MOD;
      M[i].second += M[i - 1].second;
    }
    ans = M[k - 1].first * mo % MOD;
  } else {
    for (int i = 1; i < M.size(); i++) {
      (M[i].first *= M[i - 1].first) %= MOD;
      M[i].second += M[i - 1].second;
    }
    float max_v = 0;
    for (int t = 0; t <= min(k, (int)M.size()); t += 2) {
      auto v =
          (k - t > 0 ? N[k - t - 1].second : 0) + (t > 0 ? M[t - 1].second : 0);
      if (v > max_v) {
        max_v = v;
        ans = ll(k - t > 0 ? N[k - t - 1].first : 1) *
              ll(t > 0 ? M[t - 1].first : 1) % MOD;
      }
      cerr << v << endl;
    }
  }
  cout << ans << endl;

  return 0;
}