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

#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define ifr(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m, n, r)                                                          \\
  m.reserve(n);                                                                \\
  m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os                                                                     \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd() { return 0ll; }
template <typename T, typename... Args> T gcd(T a, Args... args) {
  return __gcd(a, (__typeof(a))gcd(args...));
}
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef unordered_map<ll, ll> umap;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 100001

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  // int T;cin>>T;while(T--)
  {
    ll n, k, t;
    cin >> n >> k;
    vll pos, neg;
    ll zero = 0;
    ifr(i, 0, n) {
      cin >> t;
      if (t == 0)
        zero++;
      else if (t < 0)
        neg.pb(t);
      else
        pos.pb(t);
    }
    ll ps = pos.size(), ns = neg.size();
    if (k > (ps + ns)) {
      cout << "0\\n";
      return 0;
    }

    ll ans = 1;
    if (k == (ps + ns)) {
      if ((ns & 1) && zero > 0) {
        cout << "0\\n";
        return 0;
      }
      ifr(i, 0, ps) ans = (ans * pos[i]) % mod;
      ifr(i, 0, ns) ans = (ans * neg[i]) % mod;
      ans = (ans + mod) % mod;
      cout << ans << endl;
      return 0;
    }

    sort(all(pos), greater<ll>());
    sort(all(neg));

    if (ns == 0) {
      ans = 1;
      ifr(i, 0, k) ans = (ans * pos[i]) % mod;
      cout << ans << endl;
      return 0;
    }

    if (ps == 0) {
      if (k & 1)
        reverse(all(neg));
      if ((k & 1) && zero > 0) {
        cout << "0\\n";
        return 0;
      }
      ans = 1;
      ifr(i, 0, k) ans = (ans * neg[i]) % mod;
      ans = (ans + mod) % mod;
      cout << ans << endl;
      return 0;
    }

    vector<ld> pre;

    pre.pb(0);
    ifr(i, 0, ps) pre.pb(pre.back() + log(pos[i]));

    ld lg = 0, sum, msum = 0;
    int x = -1, y = -1;

    for (int i = 0; i <= ns; i++) {

      if (k - i < 0)
        break;

      if (((k - i) > ps) || (i & 1)) {
        lg += log(-neg[i]);
        continue;
      }

      sum = lg + pre[k - i];

      // cout<<sum<<" "<<msum<<endl;

      if (msum < sum) {
        msum = sum;
        x = i;
        y = k - i;
      }

      if (i != ns)
        lg += log(-neg[i]);
    }

    // cout<<x<<" "<<y<<endl;
    if (x == -1) {
      cout << 0 << endl;
      return 0;
    }

    ans = 1;
    if ((x & 1) && zero > 0) {
      cout << "0\\n";
      return 0;
    }
    ifr(i, 0, x) ans = (1ll * ans * neg[i]) % mod;
    ifr(i, 0, y) ans = (1ll * ans * pos[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define ifr(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m, n, r)                                                          \\
  m.reserve(n);                                                                \\
  m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os                                                                     \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd() { return 0ll; }
template <typename T, typename... Args> T gcd(T a, Args... args) {
  return __gcd(a, (__typeof(a))gcd(args...));
}
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef unordered_map<ll, ll> umap;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 100001

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  // int T;cin>>T;while(T--)
  {
    ll n, k, t;
    cin >> n >> k;
    vll pos, neg;
    ll zero = 0;
    ifr(i, 0, n) {
      cin >> t;
      if (t == 0)
        zero++;
      else if (t < 0)
        neg.pb(t);
      else
        pos.pb(t);
    }
    ll ps = pos.size(), ns = neg.size();
    if (k > (ps + ns)) {
      cout << "0\\n";
      return 0;
    }

    ll ans = 1;
    if (k == (ps + ns)) {
      ifr(i, 0, ps) ans = (ans * pos[i]) % mod;
      ifr(i, 0, ns) ans = (ans * neg[i]) % mod;
      ans = (ans + mod) % mod;
      cout << ans << endl;
      return 0;
    }

    sort(all(pos), greater<ll>());
    sort(all(neg));
    if (ns == 0) {
      ans = 1;
      ifr(i, 0, k) ans = (ans * pos[i]) % mod;
      cout << ans << endl;
      return 0;
    }
    if (ps == 0) {
      if (k & 1)
        reverse(all(neg));
      ans = 1;
      ifr(i, 0, k) ans = (ans * neg[i]) % mod;
      ans = (ans + mod) % mod;
      cout << ans << endl;
      return 0;
    }

    vector<ld> pre;
    pre.pb(0);
    ifr(i, 0, n) pre.pb(pre.back() + log2(pos[i]));

    ld lg = 0, sum, msum = -inf;
    int x = -1, y = -1;

    for (int i = 0; i <= ns; i++) {

      if (k - i < 0)
        break;

      if (k - i > ps || (i & 1)) {
        lg += log2(-neg[i]);
        continue;
      }

      sum = lg + pre[k - i];

      // cout<<sum<<" "<<msum<<endl;

      if (msum < sum) {
        msum = sum;
        x = i;
        y = k - i;
      }

      if (i != ns)
        lg += log2(-neg[i]);
    }

    // cout<<x<<" "<<y<<endl;
    if (x == -1) {
      cout << 0 << endl;
      return 0;
    }

    ifr(i, 0, x) ans = (1ll * ans * neg[i]) % mod;
    ifr(i, 0, y) ans = (1ll * ans * pos[i]) % mod;
    ans = (ans + mod) % mod;
    cout << ans << endl;
  }
  return 0;
}