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

//*****Jai Mata di*****//

#include <bits/stdc++.h>
using namespace std;
#define Nitroboost                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long int
const int MOD = 1e9 + 7;
const int N = 2e5 + 15;
#define pb push_back
#define debug cout << "Good uptill here" << endl << flush;
#define deb(x) cout << #x << ":" << x << endl << flush;

ll mod(ll x) { return (x % MOD + MOD) % MOD; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
bool comp(ll a, ll b) { return a > b; }

int main() {
  Nitroboost;

  ll n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x > 0) {
      pos.pb(x);
    } else {
      neg.pb(x);
    }
  }
  ll ans = 1;
  if (pos.size() != 0) {
    if (k % 2 != 0) {
      sort(pos.begin(), pos.end());
      ans = mul(ans, pos.back());
      pos.pop_back();
      k -= 1;
    }
    vector<ll> ok;
    sort(pos.begin(), pos.end(), comp);
    sort(neg.begin(), neg.end());
    for (ll i = 0; (i + 1) < pos.size(); i += 2) {
      ok.pb(pos[i] * pos[i + 1]);
    }
    for (ll i = 0; (i + 1) < neg.size(); i += 2) {
      ok.pb(neg[i] * neg[i + 1]);
    }
    if (pos.size() % 2 != 0 and neg.size() % 2 != 0) {
      ok.pb(pos.back() * neg.back());
    }
    sort(ok.begin(), ok.end(), comp);
    // deb(ok.size());
    for (int i = 0; i < k / 2; i++) {
      ans = mul(ans, ok[i]);
    }
    cout << ans << endl;

  } else {
    sort(neg.begin(), neg.end());
    if (k % 2 != 0) {
      reverse(neg.begin(), neg.end());
    }
    for (int i = 0; i < k; i++) {
      ans = mul(ans, neg[i]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

//*****Jai Mata di*****//

#include <bits/stdc++.h>
using namespace std;
#define Nitroboost                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long int
const int mod = 1e9 + 7;
const int N = 2e5 + 15;
#define debug cout << "Good uptill here" << endl << flush;
#define deb(x) cout << #x << ":" << x << endl << flush;

vector<ll> pos, neg;

bool comp(ll a, ll b) { return a > b; }

int main() {
  Nitroboost;
  int n, k;
  cin >> n >> k;

  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll t;
    cin >> t;
    if (t > 0)
      pos.push_back(t);
    else
      neg.push_back(t);
  }

  if (pos.size() != 0) {

    if (k % 2 != 0) {
      sort(pos.begin(), pos.end());
      ans = ((ans * pos.back()) % mod + mod) % mod;
      pos.pop_back();
      k -= 1;
    }

    vector<ll> mul;
    sort(pos.begin(), pos.end(), comp);
    sort(neg.begin(), neg.end());

    for (int i = 0; (i + 1) < pos.size(); i = i + 2)
      mul.push_back(pos[i] * pos[i + 1]);

    for (int i = 0; (i + 1) < neg.size(); i = i + 2)
      mul.push_back(neg[i] * neg[i + 1]);

    if (pos.size() % 2 != 0 && neg.size() % 2 != 0)
      mul.push_back(pos.back() * neg.back());

    sort(mul.begin(), mul.end(), comp);
    for (int i = 0; i < k / 2; k++)
      ans = (ans * mul[i]) % mod;
    cout << ans;
  } else {
    sort(neg.begin(), neg.end());
    if (k % 2 != 0) {
      reverse(neg.begin(), neg.end());
    }
    for (int i = 0; i < k; i++) {
      ans = (ans * neg[i] + mod) % mod;
    }
    if (ans < 0)
      ans = (ans + mod) % mod;
    cout << ans << endl;
  }
}