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

// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
long double eps = 1e-300;
void solve() {
  ll n, k;
  cin >> n >> k;
  ll pos = 0, neg = 0;
  vector<pair<ll, ll>> v;
  vl temp;
  set<ll> s;
  map<ll, ll> cnt;
  rep(i, n) {
    ll c;
    cin >> c;
    s.insert(c);
    temp.pb(c);
    cnt[c]++;
    if (c < 0) {
      v.pb(mp(-c, -1));
      neg++;
    } else if (c > 0) {
      v.pb(mp(c, 1));
      pos++;
    }
  }
  sort(all(v));
  sort(all(temp));
  if (k == 1) {
    cout << temp[n - 1] << '\\n';
    return;
  }
  if (k == n) {
    ll ans = 1;
    for (auto it : temp) {
      ans *= it;
      ans %= mod;
      ans += mod;
      ans %= mod;
    }
    cout << ans << '\\n';
    return;
  }
  if (v.size() < k) {
    cout << 0 << '\\n';
    return;
  }
  if (k % 2 == 1 && neg == n) {
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= (-v[i].first);
      ans %= mod;
    }
    ans += mod;
    ans %= mod;
    cout << ans << '\\n';
    return;
  }
  if (k % 2 == 1 && neg == v.size()) {
    cout << 0 << '\\n';
    return;
  }
  reverse(all(v));
  ll sign = 1;
  ll ans = 1;
  vl negs, poss;
  for (int i = 0; i < k; i++) {
    if (v[i].second == 1) {
      ans *= v[i].first;
      ans %= mod;
      poss.pb(v[i].first);
    } else {
      ans *= (-v[i].first);
      ans %= mod;
      ans += mod;
      ans %= mod;
      negs.pb(v[i].first);
      sign *= -1;
    }
  }
  sort(all(poss));
  sort(all(negs));
  if (sign == 1) {
    cout << ans << '\\n';
    return;
  } else {
    long double option1 = -473, option2 = -473;
    int flagneg = 0, flagpos = 0;
    ll addneg = 0, addpos = 0;
    for (int i = k; i < v.size(); i++) {
      if (v[i].second < 0) {
        if (flagneg == 1)
          continue;
        flagneg = 1;
        addneg = v[i].first;
      }
      if (v[i].second > 0) {
        if (flagpos == 1)
          continue;
        flagpos = 1;
        addpos = v[i].first;
      }
    }
    if (poss.size() != 0 && addneg != 0) {
      option1 = (long double)addneg / poss[0];
    }
    if (negs.size() != 0 && addpos != 0) {
      option2 = (long double)addpos / negs[0];
    }
    if (option2 < -100 && option1 < -100) {
      if (v.size() != n)
        cout << 0 << '\\n';
      else
        while (1)
          ;
      return;
    }
    if (option1 > option2 - eps) {
      ans *= modInverse(poss[0]);
      ans %= mod;
      ans *= (-addneg);
      ans %= mod;
      ans += mod;
      ans %= mod;
    } else {
      ans *= addpos;
      ans %= mod;
      ans *= (-negs[0]);
      ans %= mod;
      ans += mod;
      ans %= mod;
      ans *= modInverse((negs[0] * negs[0]) % mod);
      ans %= mod;
    }
    cout << ans << '\\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}

Error: 0
Time : 75.187ms


Fixed submission:

// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
  ll n, k;
  cin >> n >> k;
  ll pos = 0, neg = 0;
  vector<pair<ll, ll>> v;
  rep(i, n) {
    ll c;
    cin >> c;
    if (c < 0) {
      v.pb(mp(-c, -1));
      neg++;
    } else if (c > 0) {
      v.pb(mp(c, 1));
      pos++;
    }
  }
  sort(all(v));
  if (v.size() < k) {
    cout << 0 << '\\n';
    return;
  }
  if (k % 2 == 1 && neg == n) {
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= (-v[i].first);
      ans %= mod;
    }
    ans += mod;
    ans %= mod;
    cout << ans << '\\n';
    return;
  }
  if (k % 2 == 1 && neg == v.size()) {
    cout << 0 << '\\n';
    return;
  }
  reverse(all(v));
  ll sign = 1;
  ll ans = 1;
  vl negs, poss;
  for (int i = 0; i < k; i++) {
    if (v[i].second == 1) {
      ans *= v[i].first;
      ans %= mod;
      poss.pb(v[i].first);
    } else {
      ans *= (-v[i].first);
      ans %= mod;
      ans += mod;
      ans %= mod;
      negs.pb(v[i].first);
      sign *= -1;
    }
  }
  sort(all(poss));
  sort(all(negs));
  if (sign == 1) {
    cout << ans << '\\n';
    return;
  } else {
    ll option1 = -473, option2 = -473;
    int flagneg = 0, flagpos = 0;
    ll addneg = 0, addpos = 0;
    for (int i = k; i < v.size(); i++) {
      if (v[i].second < 0) {
        if (flagneg == 1)
          continue;
        flagneg = 1;
        addneg = v[i].first;
      }
      if (v[i].second > 0) {
        if (flagpos == 1)
          continue;
        flagpos = 1;
        addpos = v[i].first;
      }
    }
    if (negs.size() != 0 && addneg != 0) {
      option1 = negs[0] * addneg;
    }
    if (poss.size() != 0 && addpos != 0) {
      option2 = poss[0] * addpos;
    }
    if (option2 == -473 && option1 == -473) {
      cout << 0 << '\\n';
      return;
    }
    if (option1 >= option2) {
      ans *= modInverse(poss[0]);
      ans %= mod;
      ans *= (-addneg);
      ans %= mod;
      ans += mod;
      ans %= mod;
    } else {
      ans *= addpos;
      ans %= mod;
      ans *= (-negs[0]);
      ans %= mod;
      ans += mod;
      ans %= mod;
      ans *= modInverse((negs[0] * negs[0]) % mod);
      ans %= mod;
    }
    cout << ans << '\\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}