Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

// BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney, tree,
// chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu,
// topologicalsort, kyourenketuseibun

#define REP(i, a, b) for (int i = a; i < b; ++i)
#define PRI(s) cout << s << endl
#define PRIY PRI("Yes")
#define PRIN PRI("No")

ll N, K;
vector<ll> a;
vector<ll> ap;
vector<ll> am;
ll MOD = 1000000007;

ll mod(ll x) { return (x % MOD + MOD) % MOD; }
int main() {
  cin >> N >> K;
  a.resize(N);
  REP(i, 0, N) {
    cin >> a[i];
    if (a[i] >= 0)
      ap.push_back(a[i]);
    else
      am.push_back(a[i]);
  }

  bool minus = false;
  int ccc = 0;
  while (ccc + 2 <= K && ccc + 2 <= am.size())
    ccc += 2;
  if (ccc + ap.size() < K)
    minus = true;
  if (minus) {
    sort(a.begin(), a.end(), [](ll x, ll y) { return abs(x) < abs(y); });
    ll ret = 1;
    REP(i, 0, K) ret = mod(ret * a[i]);
    PRI(ret);
    return 0;
  }
  sort(ap.begin(), ap.end(), greater<ll>());
  sort(am.begin(), am.end(), less<ll>());
  vector<ll> pick(K);
  REP(i, 0, K) pick[i] = i < a.size() ? ap[i] : 0;
  sort(pick.begin(), pick.end(), less<ll>());
  for (int i = 0; i + 1 < K && i + 1 < am.size(); i += 2) {
    if (pick[i] * pick[i + 1] < am[i] * am[i + 1]) {
      pick[i] = am[i];
      pick[i + 1] = am[i + 1];
    } else
      break;
  }
  ll ret = 1;
  REP(i, 0, K) ret = mod(ret * pick[i]);
  PRI(ret);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

// BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney, tree,
// chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu,
// topologicalsort, kyourenketuseibun

#define REP(i, a, b) for (int i = a; i < b; ++i)
#define PRI(s) cout << s << endl
#define PRIY PRI("Yes")
#define PRIN PRI("No")

ll N, K;
vector<ll> a;
vector<ll> ap;
vector<ll> am;
ll MOD = 1000000007;

ll mod(ll x) { return (x % MOD + MOD) % MOD; }
int main() {
  cin >> N >> K;
  a.resize(N);
  REP(i, 0, N) {
    cin >> a[i];
    if (a[i] >= 0)
      ap.push_back(a[i]);
    else
      am.push_back(a[i]);
  }

  bool minus = false;
  int ccc = 0;
  while (ccc + 2 <= K && ccc + 2 <= am.size())
    ccc += 2;
  if (ccc + ap.size() < K)
    minus = true;
  if (minus) {
    sort(a.begin(), a.end(), [](ll x, ll y) { return abs(x) < abs(y); });
    ll ret = 1;
    REP(i, 0, K) ret = mod(ret * a[i]);
    PRI(ret);
    return 0;
  }
  sort(ap.begin(), ap.end(), greater<ll>());
  sort(am.begin(), am.end(), less<ll>());
  vector<ll> pick(K);
  REP(i, 0, K) pick[i] = i < ap.size() ? ap[i] : 0;
  sort(pick.begin(), pick.end(), less<ll>());
  for (int i = 0; i + 1 < K && i + 1 < am.size(); i += 2) {
    if (pick[i] * pick[i + 1] < am[i] * am[i + 1]) {
      pick[i] = am[i];
      pick[i + 1] = am[i + 1];
    } else
      break;
  }
  ll ret = 1;
  REP(i, 0, K) ret = mod(ret * pick[i]);
  PRI(ret);
  return 0;
}