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

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 200005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}
const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n + 5), p, ne;
  FOR(i, 1, n + 1) {
    cin >> v[i];
    if (v[i] >= 0)
      p.pb(v[i]);
    else
      ne.pb(-v[i]);
  }
  if (n == k) {
    int ans = 1;
    FOR(i, 1, n + 1) if (v[i] < 0) ans *= (v[i] + mod) % mod, ans %= mod;
    else ans *= v[i], ans %= mod;
    cout << ans << "\\n";
    return;
  }
  sort(ne.begin(), ne.end(), greater<int>());
  sort(p.begin(), p.end(), greater<int>());
  int ans = 1;
  if (!sz(p)) {
    if (k % 2 != 0) {
      sort(ne.begin(), ne.end());
      FOR(i, 0, k) {
        ne[i] = (-1 * ne[i] + mod) % mod;
        ans *= ne[i], ans %= mod;
      }
    } else {
      FOR(i, 0, k) ans *= ne[i], ans %= mod;
    }
  } else {
    int nt, pt = min(k, sz(p));
    if ((k - pt) % 2)
      pt--;
    nt = k - pt;
    while (nt + 2 <= sz(ne) && pt >= 2 &&
           ne[nt] * ne[nt + 1] >= p[pt - 1] * p[pt - 2]) {
      pt -= 2;
      nt += 2;
    }
    ans = 1;
    FOR(i, 0, pt) ans *= p[i], ans %= mod;
    FOR(i, 0, nt) ans *= ne[i], ans %= mod;
  }
  cout << ans << "\\n";
}

signed main() {
  showoff;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...#include <bits/stdc++.h>


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 200005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}
const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}

void solve() {
  int n, z = 0, k;
  cin >> n >> k;
  vector<int> v(n + 5), pre(n + 5), p, ne;
  FOR(i, 1, n + 1) cin >> v[i];
  FOR(i, 1, n + 1) if (v[i] > 0) p.pb(v[i]);
  else if (v[i] == 0) z++;
  else ne.pb(-v[i]);
  sort(ne.begin(), ne.end(), greater<int>());
  sort(p.begin(), p.end(), greater<int>());
  int prod = 1, ans;
  if (!sz(p)) {
    if (z > 0 && k % 2 != 0) {
      cout << "0\\n";
      return;
    }
    if (sz(ne) < k)
      ans = 0;
    else if (k % 2) {
      ans = 1;
      sort(ne.begin(), ne.end());
      FOR(i, 0, k) ne[i] = (-1 * ne[i] + mod) % mod;
      FOR(i, 0, k) ans *= ne[i], ans = (ans + mod) % mod;
      cout << ans << "\\n";
      return;
    } else
      ans = 1;
    FOR(i, 0, k) ans *= ne[i], ans %= mod;
    cout << ans << "\\n";
    return;
  }
  if (sz(p) + sz(ne) < k) {
    cout << "0\\n";
    return;
  }
  double val = 0.0l, mx = 0.0l;
  vector<double> pref(n + 5);
  pref[0] = log(p[0]);
  pre[0] = p[0];
  FOR(i, 1, sz(p)) {
    pref[i] = 1.0l * pref[i - 1] + 1.0l * log(p[i]);
    pre[i] = (pre[i - 1] * p[i]) % mod;
  }
  for (int i = 0; i <= sz(ne); i += 2) {
    if (sz(p) >= k - i) {
      if (k - i - 1 < 0 && val > mx) {
        mx = val;
        ans = prod;
      } else if (k - i - 1 >= 0 && val + pref[k - i - 1] > mx) {
        mx = val + pref[k - i - 1];
        ans = (prod * pre[k - i - 1]) % mod;
      }
    }
    if (i + 1 < sz(ne)) {
      prod *= ne[i];
      val += log(ne[i]);
      prod %= mod;
      prod *= ne[i + 1];
      val += log(ne[i + 1]);
      prod %= mod;
    }
  }
  cout << ans << "\\n";
}

signed main() {
  showoff;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...#include <bits/stdc++.h>