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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long a[200020];
vector<long long> vec, vec1;
long long z = 0;
int main() {
#define int long long
  long long n, k;
  cin >> n >> k;
  if (n == k) {
    long long ans = 1;
    for (int i = 0; i < n; i++) {
      long long a;
      cin >> a;
      ans *= a;
      ans %= 1000000007;
    }
    while (ans < 0) {
      ans += 1000000007;
    }
    cout << ans << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a > 0) {
      vec.emplace_back(a);
    } else if (0 > a) {
      vec1.emplace_back(a);
    } else {
      z++;
    }
  }
  sort(vec.begin(), vec.end());
  sort(vec1.begin(), vec1.end());
  if (n - z < k) {
    cout << "0" << endl;
    return 0;
  }
  long long ans = 1;
  long long v1 = vec1.size();
  if (vec.empty()) {
    if (z != 0 && k % 2 == 1) {
      cout << "0" << endl;
      return 0;
    }
    if (k % 2 == 1) {
      for (int i = v1 - 1; i >= v1 - k; i--) {
        ans *= vec1[i];
        ans %= 1000000007;
      }
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    } else {
      for (int i = 0; i < k; i++) {
        ans *= vec1[i];
        ans %= 1000000007;
      }
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
  }
  long long v = vec.size();
  vector<pair<long long, long long>> vec2;
  for (int i = 0; i < v; i++) {
    vec2.emplace_back(make_pair(vec[i], vec[i]));
  }
  for (int i = 0; i < v1; i++) {
    vec2.emplace_back(make_pair(abs(vec1[i]), vec1[i]));
  }
  sort(vec2.rbegin(), vec2.rend());
  long long co = 1;
  for (long long i = 0; i < k; i++) {
    ans *= vec2[i].first;
    co *= vec2[i].second;
    ans %= 1000000007;
    co %= 1000000007;
  }
  if (co > 0) {
    cout << ans << endl;
    return 0;
  } else {
    long long s = 0, h = 0;
    bool s2 = true, h2 = true;
    for (long long i = k - 1; i >= 0; i--) {
      if (vec2[i].second > 0 && s2) {
        s2 = false;
        s = vec2[i].first;
      }
      if (vec2[i].second < 0 && h2) {
        h2 = false;
        h = vec2[i].first;
      }
    }
    long long s1 = 0, h1 = 0;
    s2 = true, h2 = true;
    for (long long i = k; i < n; i++) {
      if (vec2[i].second > 0 && s2) {
        s2 = false;
        s1 = vec2[i].first;
      }
      if (vec2[i].second < 0 && h2) {
        h2 = false;
        h1 = vec2[i].first;
      }
    }
    bool hu = false, hu1 = false;
    if (s == 0 || h1 == 0) {
      hu = true;
    }
    if (h == 0 || s1 == 0) {
      hu1 = true;
    }
    if (hu && hu1) {
      if (z != 0) {
        cout << "0" << endl;
      } else {
        cout << ans << endl;
      }
      return 0;
    }
    if (s == 0 || h1 == 0) {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * h % 1000000007;
        }
        h = h * h % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= s1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
    if (h == 0 || s1 == 0) {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * s % 1000000007;
        }
        s = s * s % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= h1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
    if (abs(s * s1) <= abs(h * h1)) {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * s % 1000000007;
        }
        s = s * s % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= h1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    } else {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * h % 1000000007;
        }
        h = h * h % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= s1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
  }
}


Error: TLE

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long a[200020];
vector<long long> vec, vec1;
long long z = 0;
int main() {
#define int long long
  FILE *in = freopen("random_mz01.txt", "r", stdin);
  FILE *out = freopen("in1.txt", "w", stdout);
  long long n, k;
  cin >> n >> k;
  if (n == k) {
    long long ans = 1;
    for (int i = 0; i < n; i++) {
      long long a;
      cin >> a;
      ans *= a;
      ans %= 1000000007;
    }
    while (ans < 0) {
      ans += 1000000007;
    }
    cout << ans << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a > 0) {
      vec.emplace_back(a);
    } else if (0 > a) {
      vec1.emplace_back(a);
    } else {
      z++;
    }
  }
  sort(vec.begin(), vec.end());
  sort(vec1.begin(), vec1.end());
  if (n - z < k) {
    cout << "0" << endl;
    return 0;
  }
  long long ans = 1;
  long long v1 = vec1.size();
  if (vec.empty()) {
    if (z != 0 && k % 2 == 1) {
      cout << "0" << endl;
      return 0;
    }
    if (k % 2 == 1) {
      for (int i = v1 - 1; i >= v1 - k; i--) {
        ans *= vec1[i];
        ans %= 1000000007;
      }
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    } else {
      for (int i = 0; i < k; i++) {
        ans *= vec1[i];
        ans %= 1000000007;
      }
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
  }
  long long v = vec.size();
  vector<pair<long long, long long>> vec2;
  for (int i = 0; i < v; i++) {
    vec2.emplace_back(make_pair(vec[i], vec[i]));
  }
  for (int i = 0; i < v1; i++) {
    vec2.emplace_back(make_pair(abs(vec1[i]), vec1[i]));
  }
  sort(vec2.rbegin(), vec2.rend());
  long long co = 1;
  for (long long i = 0; i < k; i++) {
    ans *= vec2[i].first;
    co *= vec2[i].second;
    ans %= 1000000007;
    co %= 1000000007;
  }
  if (co > 0) {
    cout << ans << endl;
    return 0;
  } else {
    long long s = 0, h = 0;
    bool s2 = true, h2 = true;
    for (long long i = k - 1; i >= 0; i--) {
      if (vec2[i].second > 0 && s2) {
        s2 = false;
        s = vec2[i].first;
      }
      if (vec2[i].second < 0 && h2) {
        h2 = false;
        h = vec2[i].first;
      }
    }
    long long s1 = 0, h1 = 0;
    s2 = true, h2 = true;
    for (long long i = k; i < n; i++) {
      if (vec2[i].second > 0 && s2) {
        s2 = false;
        s1 = vec2[i].first;
      }
      if (vec2[i].second < 0 && h2) {
        h2 = false;
        h1 = vec2[i].first;
      }
    }
    bool hu = false, hu1 = false;
    if (s == 0 || h1 == 0) {
      hu = true;
    }
    if (h == 0 || s1 == 0) {
      hu1 = true;
    }
    if (hu && hu1) {
      if (z != 0) {
        cout << "0" << endl;
      } else {
        cout << ans << endl;
      }
      return 0;
    }
    if (s == 0 || h1 == 0) {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * h % 1000000007;
        }
        h = h * h % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= s1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
    if (h == 0 || s1 == 0) {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * s % 1000000007;
        }
        s = s * s % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= h1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
    if (abs(s * s1) <= abs(h * h1)) {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * s % 1000000007;
        }
        s = s * s % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= h1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    } else {
      long long n1 = 1000000005;
      long long ans1 = 1;
      while (n1 > 0) {
        if ((n1 & 1) == 1) {
          ans1 = ans1 * h % 1000000007;
        }
        h = h * h % 1000000007;
        n1 >>= 1;
      }
      ans *= ans1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      ans *= s1;
      ans %= 1000000007;
      while (ans < 0) {
        ans += 1000000007;
      }
      cout << ans << endl;
      return 0;
    }
  }
}