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

#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\\n"
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print(const ContainerType<ValueType, Args...> &c) {
  for (const auto &v : c)
    cout << v << ' ';
  cout << '\\n';
}
void print() { cout << '\\n'; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipower(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

// 8====================================D

int main() {
  int n, k;
  cin >> n >> k;
  int nk = k;
  vector<lli> a(n);
  fori(n) cin >> a[i];
  sort(a.begin(), a.end());
  lli ans = 1;

  int i = 0;
  int j = n - 1;

  while (1) {
    //  print(ans);
    if (k == 0)
      break;
    if (k % 2 == 1) {
      ans *= a[j];
      ans %= mod;

      ans = (mod + ans) % mod;
      j--;
      k--;
    } else {
      if (a[i] * a[i + 1] > a[j] * a[j - 1]) {
        ans *= a[i];
        ans %= mod;

        ans = (mod + ans) % mod;
        ans *= a[i + 1];
        ans %= mod;

        ans = (mod + ans) % mod;
        i += 2;
        k -= 2;
      } else {
        ans *= a[j];
        ans %= mod;

        ans = (mod + ans) % mod;
        ans *= a[j - 1];
        ans %= mod;

        ans = (mod + ans) % mod;
        j -= 2;
        k -= 2;
      }
    }
  }
  if (a[n - 1] < 0 && nk % 2 == 1) {
    ans = 1;
    fori(nk) {
      ans *= a[n - i - 1];
      ans %= mod;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\\n"
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print(const ContainerType<ValueType, Args...> &c) {
  for (const auto &v : c)
    cout << v << ' ';
  cout << '\\n';
}
void print() { cout << '\\n'; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipower(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

// 8====================================D

int main() {
  int n, k;
  cin >> n >> k;
  int nk = k;
  vector<lli> a(n);
  fori(n) cin >> a[i];
  sort(a.begin(), a.end());
  lli ans = 1;

  int i = 0;
  int j = n - 1;

  while (1) {
    //  print(ans);
    if (k == 0)
      break;
    if (k % 2 == 1) {
      ans *= a[j];
      while (ans < 0)
        ans = (mod + ans);
      ans %= mod;
      j--;
      k--;
    } else {
      if (a[i] * a[i + 1] > a[j] * a[j - 1]) {
        ans *= a[i];
        while (ans < 0)
          ans = (mod + ans);
        ans %= mod;
        ans *= a[i + 1];
        while (ans < 0)
          ans = (mod + ans);
        ans %= mod;
        i += 2;
        k -= 2;
      } else {
        ans *= a[j];
        while (ans < 0)
          ans = (mod + ans);
        ans %= mod;
        ans *= a[j - 1];
        while (ans < 0)
          ans = (mod + ans);
        ans %= mod;
        j -= 2;
        k -= 2;
      }
    }
  }
  if (a[n - 1] < 0) {
    ans = 1;
    fori(nk) {
      ans *= a[n - i - 1];
      while (ans < 0)
        ans = (mod + ans);
      ans %= mod;
    }
  }
  cout << ans << endl;
}