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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll ac(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}
void error(const char *str) {
  puts(str);
  exit(0);
}
int main() {
  int n, k;
  ll v;
  cin >> n >> k;
  vector<ll> a, b, c, d;
  rep(i, 0, n) {
    cin >> v;
    if (v > 0)
      a.push_back(v);
    else
      b.push_back(-v);
  }
  sort(all(a));
  sort(all(b));
  for (int i = a.size() % 2; i < a.size(); i += 2) {
    c.push_back(a[i] * a[i + 1]);
  }
  for (int i = b.size() % 2; i < b.size(); i += 2) {
    d.push_back(b[i] * b[i + 1]);
  }
  ll ans = 1;
  if (n == k) {
    rep(i, 0, b.size()) {
      ans *= -b[i];
      ans %= mod;
    }
    rep(i, 0, a.size()) {
      ans *= a[i];
      ans %= mod;
    }
    while (ans < 0)
      ans += (ll)mod;
    printf("%lld\\n", ans % mod);
    return 0;
  }
  if (a.empty() && k % 2) {
    rep(i, 0, k) {
      ans *= -b[i];
      ans %= mod;
    }
    while (ans < 0)
      ans += (ll)mod;
    printf("%lld\\n", ans);
    return 0;
  }
  while (k) {
    if (k >= 2 && d.size() &&
        (a.size() <= 1 ||
         (a.size() >= 2 && d.back() >= (a.back() * *(a.end() - 2))))) {
      ans *= d.back() % mod;
      d.pop_back();
      k -= 2;
    } else {
      ans *= a.back() % mod;
      a.pop_back();
      k--;
    }
    ans %= mod;
  }
  printf("%lld\\n", ans % mod);
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll ac(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}
void error(const char *str) {
  puts(str);
  exit(0);
}
int main() {
  int n, k;
  ll v;
  cin >> n >> k;
  vector<ll> a, b, c, d;
  rep(i, 0, n) {
    cin >> v;
    if (v > 0)
      a.push_back(v);
    else
      b.push_back(-v);
  }
  sort(all(a));
  sort(all(b));
  for (int i = a.size() % 2; i < a.size(); i += 2) {
    c.push_back(a[i] * a[i + 1]);
  }
  for (int i = b.size() % 2; i < b.size(); i += 2) {
    d.push_back(b[i] * b[i + 1]);
  }
  ll ans = 1;
  if (n == k) {
    rep(i, 0, b.size()) {
      ans *= -b[i];
      ans %= mod;
    }
    rep(i, 0, a.size()) {
      ans *= a[i];
      ans %= mod;
    }
    while (ans < 0)
      ans += (ll)mod;
    printf("%lld\\n", ans % mod);
    return 0;
  }
  if (a.empty() && k % 2) {
    rep(i, 0, k) {
      ans *= -b[i];
      ans %= mod;
    }
    while (ans < 0)
      ans += (ll)mod;
    printf("%lld\\n", ans);
    return 0;
  }
  while (k) {
    if (k >= 2 && d.size() &&
        (a.size() <= 1 || (a.size() >= 2 && d.back() >= c.back()))) {
      ans *= d.back() % mod;
      d.pop_back();
    } else {
      ans *= c.back() % mod;
      c.pop_back();
      a.pop_back();
      a.pop_back();
    }
    ans %= mod;
  }
  printf("%lld\\n", ans % mod);
  return 0;
}