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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int mod = 1e9 + 7;
vector<ll> v1;
vector<ll> v2;
vector<ll> v3;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  ll w;
  cin >> n >> k;
  int r1 = 0, r2 = 0;
  for (int i = 0; i < n; i++) {
    cin >> w;
    if (w > 0)
      v1.push_back(w);
    else if (w < 0)
      v2.push_back(w);
    else
      v3.push_back(0);
  }
  ll ans = 1;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end(), greater<int>());
  // if(v1.size)
  if (k % 2 == 1) {
    if (v1.size() != 0) {
      ans = v1[v1.size() - 1];
      v1.pop_back();
      ans %= mod;
      k--;
    } else if (v3.size() != 0) {
      cout << 0 << endl;
      return 0;
    } else {
      for (int i = 0; i < k; i++) {
        ans *= v2[i];
        if (ans < 0) {
          ll t = ans / mod;
          ans += (-t + 1) * mod;
        }
        ans %= mod;
      }
      if (ans < 0) {
        ll t = ans / mod;
        ans += (-t + 1) * mod;
      }
      cout << ans % mod << endl;
      return 0;
    }
  }
  if (k % 2 == 0) {
    for (int i = 0; i < k;) {
      ll z = 0, f = 0;
      if (v2.size() < 2 && v1.size() == 0 && v3.size() != 0) {
        cout << 0 << endl;
        return 0;
      } else if (v2.size() < 2 && v1.size() == 0) {
        ans *= v2[0];
        if (ans < 0) {
          ll t = ans / mod;
          ans += (-t + 1) * mod;
        }
        cout << ans % mod << endl;
        return 0;
      }
      if (v1.size() == 1 && v2.size() < 2) {
        if (ans < 0) {
          ll t = ans / mod;
          ans += (-t + 1) * mod;
        }
        ans *= v1[0];
        ans %= mod;
        v1.pop_back();
        i++;
        continue;
      }
      if (v1.size() >= 2) {
        z = v1[v1.size() - 2] * v1[v1.size() - 1];
      }
      if (v2.size() >= 2) {
        f = v2[v2.size() - 2] * v2[v2.size() - 1];
      }
      if (z >= f) {
        if (ans < 0) {
          ll t = ans / mod;
          ans += (-t + 1) * mod;
        }
        v1.pop_back();
        v1.pop_back();
        z %= mod;
        ans *= z;
        ans %= mod;
      } else {
        if (ans < 0) {
          ll t = ans / mod;
          ans += (-t + 1) * mod;
        }
        v2.pop_back();
        v2.pop_back();
        f %= mod;
        ans *= f;
        ans %= mod;
      }
      i += 2;
    }
  }
  cout << ans % mod << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int mod = 1e9 + 7;
vector<ll> v1;
vector<ll> v2;
vector<ll> v3;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  ll w;
  cin >> n >> k;
  int r1 = 0, r2 = 0;
  for (int i = 0; i < n; i++) {
    cin >> w;
    if (w > 0)
      v1.push_back(w);
    else if (w < 0)
      v2.push_back(w);
    else
      v3.push_back(0);
  }
  ll ans = 1;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end(), greater<int>());
  // if(v1.size)
  if (k % 2 == 1) {
    if (v1.size() != 0) {
      ans = v1[v1.size() - 1];
      v1.pop_back();
      ans %= mod;
      k--;
    } else if (v3.size() != 0) {
      cout << 0 << endl;
      return 0;
    } else {
      for (int i = 0; i < k; i++) {
        ans *= v2[i];
        while (ans < 0)
          ans += mod;
        ans %= mod;
      }
      while (ans < 0)
        ans += mod;
      cout << ans % mod << endl;
      return 0;
    }
  }
  if (k % 2 == 0) {
    for (int i = 0; i < k;) {
      ll z = 0, f = 0;
      if (v2.size() < 2 && v1.size() == 0 && v3.size() != 0) {
        cout << 0 << endl;
        return 0;
      } else if (v2.size() < 2 && v1.size() == 0) {
        ans *= v2[0];
        while (ans < 0)
          ans += mod;
        cout << ans % mod << endl;
        return 0;
      }
      if (v1.size() == 1 && v2.size() < 2) {
        while (ans < 0)
          ans += mod;
        ans *= v1[0];
        ans %= mod;
        v1.pop_back();
        i++;
        continue;
      }
      if (v1.size() >= 2) {
        z = v1[v1.size() - 2] * v1[v1.size() - 1];
      }
      if (v2.size() >= 2) {
        f = v2[v2.size() - 2] * v2[v2.size() - 1];
      }
      if (z >= f) {
        while (ans < 0)
          ans += mod;
        v1.pop_back();
        v1.pop_back();
        z %= mod;
        ans *= z;
        ans %= mod;
      } else {
        while (ans < 0)
          ans += mod;
        v2.pop_back();
        v2.pop_back();
        f %= mod;
        ans *= f;
        ans %= mod;
      }
      i += 2;
    }
  }
  cout << ans % mod << endl;
}