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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll m = 1000000007;
  int n, k;
  cin >> n >> k;
  vector<ll> po;
  vector<ll> ne;
  for (int i = 0; i < n; i++) {
    ll t;
    cin >> t;
    if (t > 0)
      po.push_back(t);
    else
      ne.push_back(abs(t));
  }
  sort(po.begin(), po.end());
  sort(ne.begin(), ne.end());
  ll ans = 1;
  bool pos = 1;
  if (k % 2 == 1) {
    if (!(po.size() >= 1))
      pos = 0;
  }
  for (int i = 0; i < k;) {
    if (pos) {
      if (k - i >= 2) {
        if (po.size() >= 2 && ne.size() >= 2) {
          if (po[po.size() - 1] * po[po.size() - 2] >
              ne[ne.size() - 1] * ne[ne.size() - 2]) {
            ans = ans * po[po.size() - 1] % m;
            ans = ans * po[po.size() - 2] % m;
            po.pop_back();
            po.pop_back();
          } else {
            ans = ans * ne[ne.size() - 1] % m;
            ans = ans * ne[ne.size() - 2] % m;
            ne.pop_back();
            ne.pop_back();
          }
          i += 2;
        } else if (po.size() >= 2) {
          ans = ans * po[po.size() - 1] % m;
          ans = ans * po[po.size() - 2] % m;
          po.pop_back();
          po.pop_back();
          i += 2;
        } else if (ne.size() >= 2) {
          ans = ans * ne[ne.size() - 1] % m;
          ans = ans * ne[ne.size() - 2] % m;
          ne.pop_back();
          ne.pop_back();
          i += 2;
        } else {
          ans = ans * po[po.size() - 1] % m;
          po.pop_back();
          i++;
        }
      } else if (po.size() >= 1) {
        ans = ans * po[po.size() - 1] % m;
        po.pop_back();
        i++;
      } else {
        ans = ans * -ne[ne.size() - 1] % m;
        ne.pop_back();
        i++;
      }
    } else {
      for (; i < k; i++) {
        ans = ans * ne[i] % m;
      }
    }
  }
  if (pos) {
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << (m + ans) % m << endl;
  } else
    cout << (m - ans) % m << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll m = 1000000007;
  int n, k;
  cin >> n >> k;
  vector<ll> po;
  vector<ll> ne;
  for (int i = 0; i < n; i++) {
    ll t;
    cin >> t;
    if (t > 0)
      po.push_back(t);
    else
      ne.push_back(abs(t));
  }
  sort(po.begin(), po.end());
  sort(ne.begin(), ne.end());
  ll ans = 1;
  bool pos = 1;
  if (k % 2 == 1) {
    if (!(po.size() >= 1))
      pos = 0;
  }
  for (int i = 0; i < k;) {
    if (pos) {
      if (k - i >= 2) {
        if (po.size() >= 2 && ne.size() >= 2) {
          if (po[po.size() - 1] * po[po.size() - 2] >
              ne[ne.size() - 1] * ne[ne.size() - 2]) {
            ans = ans * po[po.size() - 1] % m;
            ans = ans * po[po.size() - 2] % m;
            po.pop_back();
            po.pop_back();
          } else {
            ans = ans * ne[ne.size() - 1] % m;
            ans = ans * ne[ne.size() - 2] % m;
            ne.pop_back();
            ne.pop_back();
          }
          i += 2;
        } else if (po.size() >= 2) {
          ans = ans * po[po.size() - 1] % m;
          ans = ans * po[po.size() - 2] % m;
          po.pop_back();
          po.pop_back();
          i += 2;
        } else if (ne.size() >= 2) {
          ans = ans * ne[ne.size() - 1] % m;
          ans = ans * ne[ne.size() - 2] % m;
          ne.pop_back();
          ne.pop_back();
          i += 2;
        }
      } else {
        ans = ans * po[po.size() - 1] % m;
        po.pop_back();
        i++;
      }
    } else {
      for (; i < k; i++) {
        ans = ans * ne[i] % m;
      }
    }
  }
  if (pos)
    cout << ans << endl;
  else
    cout << (m - ans) % m << endl;
}