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

/// Find the maximum product of subsequences  of size k
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pb push_back
#define inf 1000000000000000000
using namespace std;
int main() {
  ll i, j, k, n, s = 0;
  cin >> n;
  cin >> k;
  ll K = k;
  vector<ll> neg, pos, zero;
  vector<pair<ll, ll>> vec;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > 0) {
      pos.pb(arr[i]);
      s = 1;
    } else if (arr[i] < 0) {
      neg.pb(abs(arr[i]));
      s = -1;
    } else {
      zero.pb(arr[i]);
      s = 1;
    }
    vec.pb(make_pair(abs(arr[i]), s));
  }
  ll idp = (ll)pos.size() - 1;
  ll idn = (ll)neg.size() - 1;
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  sort(vec.begin(), vec.end());
  ll ans = 1;
  if (k % 2) {
    if (pos.size() > 0) {
      ans = (ans * pos[idp]) % mod;
      idp--;
      k--;
    }
  }
  /// as k is even so k will be zero if enough positive or negative pair is
  /// present here
  while (k && (idp > 0 || idn > 0)) {
    ll a = -inf;
    ll b = -inf;
    if (idp >= 1)
      a = pos[idp] * pos[idp - 1];
    if (idn >= 1)
      b = neg[idn] * neg[idn - 1];
    if (a > b) {
      ans = (ans % mod * (a % mod + mod) % mod) % mod;
      idp = idp - 2;
      k = k - 2;
    } else {
      ans = (ans % mod * (b % mod + mod) % mod) % mod;
      idn = idn - 2;
      k = k - 2;
    }
  }
  if (k) {
    if (zero.size())
      ans = 0;
    else {
      ans = 1;
      for (i = 0; i < K; i++)
        ans =
            (ans % mod * (((vec[i].first * vec[i].second) % mod) + mod) % mod) %
            mod;
    }
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

/// Find the maximum product of subsequences  of size k
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pb push_back
#define inf 1000000000000000000
using namespace std;
int main() {
  ll i, j, k, n, s = 0;
  cin >> n;
  cin >> k;
  ll K = k;
  vector<ll> neg, pos, zero;
  vector<pair<ll, ll>> vec;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > 0) {
      pos.pb(arr[i]);
      s = 1;
    } else if (arr[i] < 0) {
      neg.pb(abs(arr[i]));
      s = -1;
    } else {
      zero.pb(arr[i]);
      s = 1;
    }
    vec.pb(make_pair(abs(arr[i]), s));
  }
  ll idp = (ll)pos.size() - 1;
  ll idn = (ll)neg.size() - 1;
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  sort(vec.begin(), vec.end());
  ll ans = 1;
  if (k % 2) {
    if (pos.size() > 0) {
      ans = (ans * pos[idp]) % mod;
      idp--;
      k--;
    }
  }
  /// as k is even so k will be zero if enough positive or negative pair is
  /// present here
  while (k && (idp > 0 || idn > 0)) {
    ll a = -inf;
    ll b = -inf;
    if (idp >= 1)
      a = pos[idp] * pos[idp - 1];
    if (idn >= 1)
      b = neg[idn] * neg[idn - 1];
    if (a > b) {
      ans = (ans % mod * (a % mod + mod) % mod) % mod;
      idp = idp - 2;
      k = k - 2;
    } else if (a < b) {
      ans = (ans % mod * (b % mod + mod) % mod) % mod;
      idn = idn - 2;
      k = k - 2;
    }
  }
  if (k) {
    if (zero.size())
      ans = 0;
    else {
      ans = 1;
      for (i = 0; i < K; i++)
        ans =
            (ans % mod * (((vec[i].first * vec[i].second) % mod) + mod) % mod) %
            mod;
    }
  }
  cout << ans;
  return 0;
}