Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *  Author: dhruv_gheewala
 *  Problem: E - Multiplication 4
 *  Date:   03.09.2020
 *  Time:   20:15:27
 **/

#if __has_include("debug.h")
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;

#define fundri 108
#define debug(...) 1729
#endif

#define int int64_t
typedef pair<int, int> pii;
typedef vector<int> vi;

const bool testcases = false;
#define ff first
#define ss second
#define endl '\\n'
#define all(s) s.begin(), s.end()

// if vector isn't soted then it will remove consecutive same elements
#define make_unique(vec) vec.erase(unique(all(vec)), vec.end())
#define test                                                                   \\
  int TC = 1;                                                                  \\
  if (testcases)                                                               \\
    cin >> TC;                                                                 \\
  for (int tc = 1; tc <= TC; tc++)                                             \\
  solve(), fundri
#define sort_by(cont, expr)                                                    \\
  sort(all(cont), [&](const auto &lhs, const auto &rhs) { return expr; })
// clears stdin
#define flush(ch)                                                              \\
  cin.ignore(std::numeric_limits<std::streamsize>::max(), ch) // default, EOF

int seed;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {
  return uniform_int_distribution<int>(l, r)(rng);
}

inline bool in_range(int a, int l, int r) { return (a >= l && a <= r); }
template <typename H, typename... T> void inp(H &head) { cin >> head; }
template <typename H, typename... T> void inp(H &head, T &...tail) {
  cin >> head;
  inp(tail...);
}
template <typename T> bool isPrime(T n) {
  for (T i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return n >= 2;
}
template <typename T> inline istream &operator>>(istream &in, vector<T> &a) {
  for (T &x : a)
    in >> x;
  return in;
}
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T, U> &a) {
  in >> a.ff >> a.ss;
  return in;
}

void solve();
void fast_io();

int32_t main(int32_t argc, char **argv) {
  fast_io();
  test;
  return 0;
}

const int mod = 1e9 + 7;
int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  cin >> a;

  int zero = 0;
  multiset<int, greater<int>> pos, neg;

  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      zero++;
    else if (a[i] > 0)
      pos.insert(a[i]);
    else
      neg.insert(-a[i]);
  }

  if (zero + k > n) {
    cout << 0 << endl;
    return;
  }

  int ans = 1;
  if (!pos.size()) {
    // No Positives
    // Only Negatives

    // k is odd => minimize the answer
    if (k % 2 == 1) {

      if (zero > 0) {
        cout << 0 << endl;
        return;
      }

      while (neg.size() > k)
        neg.erase(neg.begin());

      while (k > 0) {
        int fir = *neg.begin();
        neg.erase(neg.begin());

        ans = mul(ans, fir);
        k--;
      }
      cout << mod - ans << endl;
    } else {
      while (k > 0) {
        int fir = *neg.begin();
        neg.erase(neg.begin());

        ans = mul(ans, fir);
        k--;
      }
      cout << ans << endl;
    }
    return;
  }

  if (!neg.size()) {
    while (k > 0) {
      int fir = *pos.begin();
      pos.erase(pos.begin());

      ans = mul(ans, fir);
      k--;
    }
    cout << ans << endl;
    return;
  }

  // pos & neg both

  // making k even
  if (k % 2 == 1) {
    int fir = *pos.begin();
    pos.erase(pos.begin());

    ans = mul(ans, fir);
    k--;
  }

  while (k > 0 && pos.size() >= 2 && neg.size() >= 2) {
    int pfir = *pos.begin();
    pos.erase(pos.begin());
    int psec = *pos.begin();
    pos.erase(pos.begin());

    int nfir = *neg.begin();
    neg.erase(neg.begin());
    int nsec = *neg.begin();
    neg.erase(neg.begin());

    if (pfir * psec - nfir * nsec > 0) {
      neg.insert(nfir);
      neg.insert(nsec);

      ans = mul(ans, mul(pfir, psec));
    } else {
      pos.insert(pfir);
      pos.insert(psec);

      ans = mul(ans, mul(nfir, nsec));
    }
    k -= 2;
  }

  // any one of this two loop will run
  while (k > 0 && pos.size() >= 2) {
    int pfir = *pos.begin();
    pos.erase(pos.begin());
    int psec = *pos.begin();
    pos.erase(pos.begin());
    ans = mul(ans, mul(pfir, psec));
    k -= 2;
  }

  while (k > 0 && neg.size() >= 2) {
    int nfir = *neg.begin();
    neg.erase(neg.begin());
    int nsec = *neg.begin();
    neg.erase(neg.begin());
    ans = mul(ans, mul(nfir, nsec));
    k -= 2;
  }

  // k will be even always
  if (k > 0) {
    if (zero >= 1) {
      cout << 0 << endl;
      return;
    }

    ans = mul(ans, mul(*pos.begin(), *neg.begin()));
    ans = mod - ans;
  }
  cout << ans << endl;
}

void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

#ifdef DHRUV_GHEEWALA
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

Error: TLE

Fixed submission:

/**
 *  Author: dhruv_gheewala
 *  Problem: E - Multiplication 4
 *  Date:   03.09.2020
 *  Time:   15:38:58
 **/

#if __has_include("debug.h")
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;

#define fundri 108
#define debug(...) 1729
#endif

#define int int64_t
typedef pair<int, int> pii;
typedef vector<int> vi;

const bool testcases = false;
#define ff first
#define ss second
#define endl '\\n'
#define all(s) s.begin(), s.end()

// if vector isn't soted then it will remove consecutive same elements
#define make_unique(vec) vec.erase(unique(all(vec)), vec.end())
#define test                                                                   \\
  int TC = 1;                                                                  \\
  if (testcases)                                                               \\
    cin >> TC;                                                                 \\
  for (int tc = 1; tc <= TC; tc++)                                             \\
  solve(), fundri
#define sort_by(cont, expr)                                                    \\
  sort(all(cont), [&](const auto &lhs, const auto &rhs) { return expr; })
// clears stdin
#define flush(ch)                                                              \\
  cin.ignore(std::numeric_limits<std::streamsize>::max(), ch) // default, EOF

int seed;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {
  return uniform_int_distribution<int>(l, r)(rng);
}

inline bool in_range(int a, int l, int r) { return (a >= l && a <= r); }
template <typename H, typename... T> void inp(H &head) { cin >> head; }
template <typename H, typename... T> void inp(H &head, T &...tail) {
  cin >> head;
  inp(tail...);
}
template <typename T> bool isPrime(T n) {
  for (T i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return n >= 2;
}
template <typename T> inline istream &operator>>(istream &in, vector<T> &a) {
  for (T &x : a)
    in >> x;
  return in;
}
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T, U> &a) {
  in >> a.ff >> a.ss;
  return in;
}

void solve();
void fast_io();

int32_t main(int32_t argc, char **argv) {
  fast_io();
  test;
  return 0;
}

const int mod = 1e9 + 7;
int mul(int a, int b) { return ((a % mod) * (b % mod) + mod) % mod; }

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  cin >> a;

  int zero = 0;
  multiset<int, greater<int>> pos, neg;
  for (int i = 0; i < n; i++) {
    if (a[i] > 0)
      pos.insert(a[i]);
    else if (a[i] < 0)
      neg.insert(-a[i]);
    else
      zero++;
  }

  if (k + zero > n) {
    cout << 0 << endl;
    return;
  }

  int ans = 1;
  if (!pos.size()) {

    if (neg.size() < k) {
      cout << 0 << endl;
      return;
    }

    // only negs and k is odd => minimize our ans
    if (k % 2 == 1) {

      if (zero > 0) {
        cout << 0 << endl;
        return;
      }

      while (neg.size() > k)
        neg.erase(neg.begin());

      while (!neg.empty()) {
        ans = mul(ans, *neg.begin());
        neg.erase(neg.begin());
      }
      cout << mod - ans << endl;
      return;
    }

    // only negs and k is even => maximize
    while (k > 0) {
      ans = mul(ans, *neg.begin());
      neg.erase(neg.begin());
      k--;
    }
    cout << ans << endl;
  } else if (!neg.size()) {

    if (pos.size() < k) {
      cout << 0 << endl;
      return;
    }

    // only pos numbers
    while (k > 0) {
      ans = mul(ans, *pos.begin());
      pos.erase(pos.begin());
      k--;
    }
    cout << ans << endl;
  } else {
    while (k > 0) {

      if (k % 2 == 1) {
        if (pos.empty() && zero > 0) {
          cout << 0 << endl;
          continue;
        }
        ans = mul(ans, *pos.begin());
        pos.erase(pos.begin());
        k--;
      }

      while (k > 0 && pos.size() >= 2 && neg.size() >= 2) {
        int pfir = *pos.begin();
        pos.erase(pos.begin());
        int psec = *pos.begin();

        int nfir = *neg.begin();
        neg.erase(neg.begin());
        int nsec = *neg.begin();

        if (nfir * nsec > pfir * psec) {
          pos.insert(pfir);
          ans = mul(ans, mul(nfir, nsec));
          neg.erase(neg.begin());
          k -= 2;
        } else {
          neg.insert(nfir);
          ans = mul(ans, mul(pfir, psec));
          pos.erase(pos.begin());
          k -= 2;
        }
      }

      while (!pos.empty() && k > 0 && *pos.begin() > *neg.begin()) {
        ans = mul(ans, *pos.begin());
        pos.erase(pos.begin());
        k--;
      }
    }
    cout << ans << endl;
  }
}

void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

#ifdef DHRUV_GHEEWALA
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}