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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
ll k, n;
ll v1[200010], v2[200010];
ll v0;
int main() {
  cin >> n >> k;
  ll a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    ll num;
    cin >> num;
    if (num > 0)
      v1[a++] = num;
    else if (num < 0)
      v2[b++] = num;
    else
      v0++;
  }
  if (k + v0 > n) {
    cout << 0 << endl;
    return 0;
  }
  sort(v1, v1 + a);
  sort(v2, v2 + b);
  // even -> greater than 0
  vector<ll> v3;
  for (int i = 0; i + 1 < b; i += 2) {
    v3.push_back(v2[i] * v2[i + 1]);
  }
  ll tmp = k;
  ll p1 = a - 1;
  ll p3 = 0;
  ll ans1 = 1;
  while (tmp > 0) {
    if (tmp >= 2 && (p1 >= 1 || p3 < v3.size())) {
      if (p1 >= 1 && p3 < v3.size()) {
        if (v1[p1] * v1[p1 - 1] > v3[p3]) {
          ans1 *= v1[p1] * v1[p1 - 1] % mod;
          ans1 %= mod;
          p1 -= 2;
        } else {
          ans1 *= v3[p3] % mod;
          ans1 %= mod;
          p3++;
        }
        tmp -= 2;
      } else if (p1 >= 1) {
        ans1 *= v1[p1] * v1[p1 - 1] % mod;
        ans1 %= mod;
        p1 -= 2;
        tmp -= 2;
      } else if (p3 < v3.size()) {
        ans1 *= v3[p3] % mod;
        ans1 %= mod;
        p3++;
        tmp -= 2;
      }
    } else {
      if (p1 >= 0) {
        ans1 *= v1[p1];
        ans1 %= mod;
        p1--;
        tmp--;
      } else {
        break;
      }
    }
  }
  if (tmp == 0) {
    cout << ans1 << endl;
    return 0;
  }

  // equal to 0
  if (v0 != 0) {
    cout << 0 << endl;
    return 0;
  }

  // odd -> less than 0
  //  for (int i = 0; i < v1.size(); i++) {
  //      cout << v1[i] << " ";
  //  }
  //  cout << endl;
  //  for (int i = 0; i < v2.size(); i++) {
  //      cout << v2[i] << " ";
  //  }
  //  cout << endl;
  ll ans2 = (v2[b - 1] + mod * mod) % mod;
  for (int i = 0; i < b - 1; i++) {
    v1[a++] = -v2[i];
  }
  sort(v1, v1 + a);
  for (int i = 0; i < k - 1; i++) {
    ans2 *= v1[i];
    ans2 %= mod;
  }
  cout << ans2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
ll k, n;
ll v1[200010], v2[200010];
ll v0;
int main() {
  cin >> n >> k;
  ll a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    ll num;
    cin >> num;
    if (num > 0)
      v1[a++] = num;
    else if (num < 0)
      v2[b++] = num;
    else
      v0++;
  }
  if (k + v0 > n) {
    cout << 0 << endl;
    return 0;
  }
  sort(v1, v1 + a);
  sort(v2, v2 + b);
  // even -> greater than 0
  vector<ll> v3;
  for (int i = 0; i + 1 < b; i += 2) {
    v3.push_back(v2[i] * v2[i + 1]);
  }
  ll tmp = k;
  ll p1 = a - 1;
  ll p3 = 0;
  ll ans1 = 1;
  while (tmp > 0) {
    if (tmp >= 2) {
      if (p1 >= 1 && p3 < v3.size()) {
        if (v1[p1] * v1[p1 - 1] > v3[p3]) {
          ans1 *= v1[p1] * v1[p1 - 1] % mod;
          ans1 %= mod;
          p1 -= 2;
        } else {
          ans1 *= v3[p3] % mod;
          ans1 %= mod;
          p3++;
        }
        tmp -= 2;
      } else if (p1 >= 1) {
        ans1 *= v1[p1] * v1[p1 - 1] % mod;
        ans1 %= mod;
        p1 -= 2;
        tmp -= 2;
      } else if (p3 < v3.size()) {
        ans1 *= v3[p3] % mod;
        ans1 %= mod;
        p3++;
        tmp -= 2;
      }
    } else if (tmp >= 1) {
      if (p1 >= 0) {
        ans1 *= v1[p1];
        ans1 %= mod;
        p1--;
        tmp--;
      } else {
        break;
      }
    }
  }
  if (tmp == 0) {
    cout << ans1 << endl;
    return 0;
  }

  // equal to 0
  if (v0 != 0) {
    cout << 0 << endl;
    return 0;
  }

  // odd -> less than 0
  //  for (int i = 0; i < v1.size(); i++) {
  //      cout << v1[i] << " ";
  //  }
  //  cout << endl;
  //  for (int i = 0; i < v2.size(); i++) {
  //      cout << v2[i] << " ";
  //  }
  //  cout << endl;
  ll ans2 = (v2[b - 1] + mod * mod) % mod;
  for (int i = 0; i < b - 1; i++) {
    v1[a++] = -v2[i];
  }
  sort(v1, v1 + a);
  for (int i = 0; i < k - 1; i++) {
    ans2 *= v1[i];
    ans2 %= mod;
  }
  cout << ans2 << endl;
  return 0;
}