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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

int n, m;
ll a[N];
ll mp[N];

bool cmp(ll x, ll y) { return abs(x) < abs(y); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] >= 0)
      mp[1]++;
    else
      mp[2]++;
  }
  int l = 1, r = n;
  ll ans = 1;
  if (mp[1] == 0 && m % 2 == 1) {
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= m; ++i) {
      ans = a[i] * ans % mod;
    }
  } else {
    sort(a + 1, a + 1 + n, greater<int>());
    while (m > 0) {
      if (m % 2 == 1)
        ans = ans * a[l++] % mod, m--;
      else if (m >= 2) {
        if (a[l] * a[l + 1] >= a[r] * a[r - 1]) {
          ans = ans * a[l] % mod * a[l + 1] % mod;
          l += 2;
          m -= 2;
        } else {
          ans = ans * a[r - 1] % mod * a[r] % mod;
          r -= 2;
          m -= 2;
        }
      }
    }
  }

  cout << (ans % mod + mod) % mod << endl;

  return 0;
}


Error: TLE

Fixed submission:

/******************************************
 * AUTHOR : HRITIK AGGARWAL *
 ******************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define ms(s, n) memset(s, n, sizeof(s))
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int ll
ll po(ll a, ll x, ll m) {
  if (x == 0) {
    return 1;
  }
  ll ans = 1;
  ll k = 1;
  while (k <= x) {
    if (x & k) {
      ans = ((ans * a) % m);
    }
    k <<= 1;
    a *= a;
    a %= m;
  }
  return ans;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;
  int a[n];
  int ans = 1;
  vi pos, neg;
  forr(i, n) {
    int x;
    cin >> x;
    if (x >= 0) {
      pos.pb(x);
    } else {
      neg.pb(x);
    }
  }
  int i = 0, j = 0;
  sort(rall(pos));
  sort(all(neg));
  int all = k;
  while (k - 2 >= 0 and i + 2 <= pos.size() and j + 2 <= neg.size()) {
    k -= 2;
    int ans1 = pos[i] * pos[i + 1];
    int ans2 = neg[j] * neg[j + 1];
    if (ans2 >= ans1) {
      ans = (ans * (ans2 % MOD)) % MOD;
      j += 2;
    } else {
      ans = (ans * (ans1 % MOD)) % MOD;
      i += 2;
    }
  }
  if (i + 2 > pos.size() and i + 1 <= pos.size() and j + 2 <= neg.size() and
      k - 2 >= 0) {
    int ans1 = pos[i];
    k -= 2;
    int ans2 = neg[j] * neg[j + 1];
    if (ans2 >= ans1) {
      ans = (ans * (ans2 % MOD)) % MOD;
      j += 2;
    } else {
      ans = (ans * (ans1 % MOD)) % MOD;
      i += 1;
    }
  }
  while (k - 2 >= 0 and j + 2 <= neg.size()) {
    k -= 2;
    int ans2 = neg[j] * neg[j + 1];
    ans = (ans * (ans2 % MOD)) % MOD;
    j += 2;
  }
  while (k - 1 >= 0 and i + 1 <= pos.size()) {
    while (1) {
    }
    int ans1 = pos[i];
    k--;
    ans = (ans * (ans1 % MOD)) % MOD;
    i += 1;
  }
  int okay = 0;
  while (k - 1 >= 0 and j + 1 <= neg.size()) {
    k--;
    int ans2 = neg[j];
    j++;
    okay = 1;
    ans = (ans * (ans2 % MOD) + MOD) % MOD;
    j += 1;
  }
  if (okay == 0)
    cout << ans << "\\n";
  else {
    int ans = 1;
    vi v;
    forr(i, pos.size()) { v.pb(pos[i]); }
    forr(i, neg.size()) { v.pb(-1 * neg[i]); }
    sort(all(v));
    int j = 0;
    while (all--) {
      ans = (ans * (v[j] % MOD)) % MOD;
      j++;
    }
    ans *= -1;
    ans = (ans + MOD) % MOD;
    cout << ans << "\\n";
  }
  return 0;
}