Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define maxn 100005
#define mod 1000000007
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vll;
typedef vector<bool> vb;
typedef set<int> si;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
#define __builtin_popcountll bitCountLL
#define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
const int INF = 2e9;
const ll ML = 4e18;

ll solve() {
  int n, k;
  cin >> n >> k;
  vll a, b;
  for (int i = 0; i < n; ++i) {
    ll x;
    cin >> x;
    if (x >= 0)
      a.push_back(x);
    else
      b.push_back(x);
  }
  sort(a.rbegin(), a.rend());
  sort(b.begin(), b.end());
  ll res = 1;
  if (a.size() == 0 && k % 2) {
    int i = b.size() - 1;
    while (k--) {
      res = (res * b[i--]) % mod;
    }
    res = (res + mod) % mod;
    return res;
  }
  if ((int)(a.size() + b.size()) == k) {
    for (ll i : a)
      res = (res * i) % mod;
    for (ll i : b)
      res = (res * i) % mod;
    res = (res + mod) % mod;
    return res;
  }
  bool ok = false;
  int pos = a.size() - 1;
  for (int i = 0; i * 2 <= (int)b.size(); i++) {
    if (2 * i + (int)a.size() < k)
      continue;
    if (!ok) {
      ok = true;
      pos = k - 1 - 2 * i;
      continue;
    }
    if (a[pos] * a[pos - 1] > b[2 * i - 1] * b[2 * i - 2])
      break;
    pos -= 2;
  }
  for (int i = 0; i <= pos; i++)
    res = (res * a[i]) % mod;
  int len = k - pos - 1;
  for (int i = 0; i < len / 2; i++) {
    ll x = (b[2 * i] * b[2 * i + 1]) % mod;
    res = (res * x) % mod;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\data_in.txt", "r", stdin);
  cout << solve() << endl;
  // cout << (-1) % 3 << endl;
  /*clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define maxn 100005
#define mod 1000000007
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vll;
typedef vector<bool> vb;
typedef set<int> si;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
#define __builtin_popcountll bitCountLL
#define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
const int INF = 2e9;
const ll ML = 4e18;

ll solve() {
  int n, k;
  cin >> n >> k;
  vll a, b;
  for (int i = 0; i < n; ++i) {
    ll x;
    cin >> x;
    if (x >= 0)
      a.push_back(x);
    else
      b.push_back(x);
  }
  sort(a.rbegin(), a.rend());
  sort(b.begin(), b.end());
  ll res = 1;
  if (a.size() == 0 && k % 2) {
    int i = b.size() - 1;
    while (k--) {
      res = (res * b[i--]) % mod;
    }
    res = (res + mod) % mod;
    return res;
  }
  if ((int)(a.size() + b.size()) == k) {
    for (ll i : a)
      res = (res * i) % mod;
    for (ll i : b)
      res = (res * i) % mod;
    res = (res + mod) % mod;
    return res;
  }
  bool ok = false;
  int pos = a.size() - 1;
  for (int i = 0; i * 2 <= (int)b.size(); i++) {
    if (2 * i + (int)a.size() < k)
      continue;
    if (!ok) {
      ok = true;
      pos = k - 1 - 2 * i;
      continue;
    }
    if (pos < 1)
      break;
    if (a[pos] * a[pos - 1] > b[2 * i - 1] * b[2 * i - 2])
      break;
    pos -= 2;
  }
  for (int i = 0; i <= pos; i++)
    res = (res * a[i]) % mod;
  int len = k - pos - 1;
  for (int i = 0; i < len / 2; i++) {
    ll x = (b[2 * i] * b[2 * i + 1]) % mod;
    res = (res * x) % mod;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\data_in.txt", "r", stdin);
  cout << solve() << endl;
  // cout << (-1) % 3 << endl;
  /*clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/