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

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

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(i, k, n) for (int i = k; i < n; ++i)
#define fri(i, k, n) for (int i = k; i >= n; --i)
#define INF (int)1e9
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(), arr.end()

void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;
ll arr[N];

void solve() {
  int n, k;
  scanf("%d %d", &n, &k);
  fr(i, 0, n) { scanf("%lld", arr + i); }
  if (k == n) {
    ll prod = 1;
    int ncnt = 0;
    fr(i, 0, n) {
      if (arr[i] < 0)
        ncnt++;
      prod = (prod * abs(arr[i])) % MOD;
    }
    if (ncnt % 2) {
      prod = (MOD - prod) % MOD;
    }
    printf("%lld", prod);
    return;
  }
  vll pos, neg;
  neg.reserve(n);
  pos.reserve(n);
  fr(i, 0, n) {
    if (arr[i] < 0)
      neg.pb(abs(arr[i]));
    else
      pos.pb(arr[i]);
  }
  sort(neg.rbegin(), neg.rend());
  sort(pos.rbegin(), pos.rend());
  ll ns = neg.size(), ps = pos.size();
  if (ns == n && (k % 2)) {
    ll prod = 1;
    reverse(all(neg));
    fr(i, 0, k) { prod = (prod * neg[i]) % MOD; }
    prod = (MOD - prod) % MOD;
    printf("%lld", prod);
  } else {
    vll final;
    final.reserve(n);
    ll prod = 1;
    if (k % 2)
      prod = pos[0];
    fr(i, k % 2, ps) {
      final.pb(pos[i] * pos[i + 1]);
      i++;
    }
    fr(i, 0, ns) {
      final.pb(neg[i] * neg[i + 1]);
      i++;
    }
    sort(final.rbegin(), final.rend());
    fr(i, 0, k / 2) { prod = ((prod) * ((final[i]) % MOD)) % MOD; }
    printf("%lld", prod);
  }
  return;
}

int main() {
  int tc = 1;
  // cin>>tc;
  while (tc--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(i, k, n) for (int i = k; i < n; ++i)
#define fri(i, k, n) for (int i = k; i >= n; --i)
#define INF (int)1e9
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(), arr.end()

void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;
ll arr[N];

void solve() {
  int n, k;
  scanf("%d %d", &n, &k);
  fr(i, 0, n) { scanf("%lld", arr + i); }
  if (k == n) {
    ll prod = 1;
    fr(i, 0, n) {
      prod = (prod * arr[i]);
      while (prod < 0)
        prod += MOD;
      prod %= MOD;
    }
    printf("%lld", prod);
    return;
  }
  vll pos, neg;
  neg.reserve(n);
  pos.reserve(n);
  fr(i, 0, n) {
    if (arr[i] < 0)
      neg.pb(abs(arr[i]));
    else
      pos.pb(arr[i]);
  }
  sort(neg.rbegin(), neg.rend());
  sort(pos.rbegin(), pos.rend());
  ll ns = neg.size(), ps = pos.size();
  if (ns == n && (k % 2)) {
    ll prod = 1;
    reverse(all(neg));
    fr(i, 0, k) { prod = (prod * neg[i]) % MOD; }
    prod = (MOD - prod) % MOD;
    printf("%lld", prod);
  } else {
    vll final;
    final.reserve(n);
    ll prod = 1;
    if (k % 2)
      prod = pos[0];
    fr(i, k % 2, ps) {
      final.pb(pos[i] * pos[i + 1]);
      i++;
    }
    fr(i, 0, ns) {
      final.pb(neg[i] * neg[i + 1]);
      i++;
    }
    sort(final.rbegin(), final.rend());
    fr(i, 0, k / 2) { prod = ((prod) * ((final[i]) % MOD)) % MOD; }
    printf("%lld", prod);
  }
  return;
}

int main() {
  int tc = 1;
  // cin>>tc;
  while (tc--)
    solve();
  return 0;
}