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

#include "bits/stdc++.h"

using namespace ::std;

#define all(x) x.begin(), x.end()
#define Unique(x) x.erase(unique(x.begin(), x.end()), x.end())

template <class T> T SQR(T N) { return (T)N * N; }
template <class T> double DIST(T a, T b) {
  return 1.0 * sqrt(SQR(double(a)) + SQR(double(b)));
}
template <class T> bool CROSS(pair<T, T> a, pair<T, T> b, pair<T, T> c) {
  return (b.first - a.first) * (c.second - b.second) >=
         (b.second - a.second) * (c.first - b.first);
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;

int px[] = {0, 1, -1, 0, -1, -1, 1};
int py[] = {1, 0, 0, -1, -1, 0, -1, -1};

const double eps = 1e-9;

inline ll power(ll a, ll p, ll mod) {
  ll x = a, res = 1;

  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    p /= 2, x = (x * x) % mod;
  }
  return res;
}

int main() {
  int n, m, c, d, a, b, k;

  int test;

  // scanf("%d", &test);
  // while(test--)
  // {
  scanf("%d%d", &n, &k);

  vector<int> p, mi;

  for (int i = 0; i < n; ++i) {
    scanf("%d", &a);
    if (a >= 0)
      p.push_back(a);
    if (a < 0)
      mi.push_back(-a);
  }

  sort(all(p)), sort(all(mi));
  a = p.size(), b = mi.size();

  ll res = 1, mod = 1e9 + 7;

  if (n == k) {
    for (auto it : p)
      res = res * it % mod;
    for (auto it : mi)
      res = res * (mod - it) % mod;
  } else if (a == 0 and k % 2) {
    for (int i = 0; i < k; ++i)
      res = res * (mod - mi[i]) % mod;
  } else {
    c = a - 1, d = b - 1;
    while (k) {
      if (c >= 1 and d >= 1 and k >= 2 and
          ll(1LL * mi[d] * mi[d - 1]) >= ll(1LL * p[c] * p[c - 1]))
        res = res * ((1LL * mi[d] * mi[d - 1]) % mod) % mod, d -= 2, k -= 2;
      else if (c >= 1 and k >= 2)
        res = res * p[c] % mod, c--, k--;
      else if (d >= 1 and k >= 2)
        res = res * ((1LL * mi[d] * mi[d - 1]) % mod) % mod, d -= 2, k -= 2;
      else if (c >= 0)
        res = res * p[c] % mod, c--, k--;
    }
  }

  printf("%lld\\n", res);
  //}
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace ::std;

#define all(x) x.begin(), x.end()
#define Unique(x) x.erase(unique(x.begin(), x.end()), x.end())

template <class T> T SQR(T N) { return (T)N * N; }
template <class T> double DIST(T a, T b) {
  return 1.0 * sqrt(SQR(double(a)) + SQR(double(b)));
}
template <class T> bool CROSS(pair<T, T> a, pair<T, T> b, pair<T, T> c) {
  return (b.first - a.first) * (c.second - b.second) >=
         (b.second - a.second) * (c.first - b.first);
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;

int px[] = {0, 1, -1, 0, -1, -1, 1};
int py[] = {1, 0, 0, -1, -1, 0, -1, -1};

const double eps = 1e-9;

inline ll power(ll a, ll p, ll mod) {
  ll x = a, res = 1;

  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    p /= 2, x = (x * x) % mod;
  }
  return res;
}

int main() {
  int n, m, c, d, a, b, k;

  int test;

  // scanf("%d", &test);
  // while(test--)
  // {
  scanf("%d%d", &n, &k);

  vector<int> p, mi;

  for (int i = 0; i < n; ++i) {
    scanf("%d", &a);
    if (a >= 0)
      p.push_back(a);
    if (a < 0)
      mi.push_back(-a);
  }

  sort(all(p)), sort(all(mi));
  a = p.size(), b = mi.size();

  ll res = 1, mod = 1e9 + 7;

  if (n == k) {
    for (auto it : p)
      res = res * it % mod;
    for (auto it : mi)
      res = res * (mod - it) % mod;
  } else if (a == 0 and k % 2) {
    for (int i = 0; i < k; ++i)
      res = res * (mod - mi[i]) % mod;
  } else {
    c = a - 1, d = b - 1;
    while (k) {
      if (c >= 1 and d >= 1 and
          1LL * mi[d] * mi[d - 1] >= 1LL * p[c] * p[c - 1])
        res = res * ((1LL * mi[d] * mi[d - 1]) % mod) % mod, d -= 2, k -= 2;
      else if (c >= 1)
        res = res * p[c] % mod, c -= 1, k -= 1;
      else if (d >= 1)
        res = res * ((1LL * mi[d] * mi[d - 1]) % mod) % mod, d -= 2, k -= 2;
      else if (c >= 0)
        res = res * p[c] % mod, c--, k--;
    }
  }

  printf("%lld\\n", res);
  //}
}