Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
typedef pair<int, int> pi;
typedef long long ll;

const int MAX = 2e5 + 9;
const int MOD = 1000000007;
int n, k;
vector<int> pos, choosedPos;
vector<int> neg, choosedNeg;

ll Calculate() {
  ll ans = 1;
  for (int i = 0; i < choosedNeg.size(); ++i) {
    ans *= -choosedNeg[i];
    ans %= MOD;
  }

  for (int i = 0; i < choosedPos.size(); ++i) {
    ans *= choosedPos[i];
    ans %= MOD;
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  while (n--) {
    int a;
    cin >> a;

    if (a >= 0)
      pos.push_back(a);
    else
      neg.push_back(-a);
  }

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  // max 2x negative
  while (choosedNeg.size() + 1 < k && neg.size() >= 2) {
    choosedNeg.push_back(neg.back());
    neg.pop_back();

    choosedNeg.push_back(neg.back());
    neg.pop_back();
  }

  // fill by positive
  while (choosedNeg.size() + choosedPos.size() < k && !pos.empty()) {
    choosedPos.push_back(pos.back());
    pos.pop_back();
  }

  if (choosedPos.size() + choosedNeg.size() < k) { // must choose one neg
    vector<int> v;

    // undo
    while (!choosedNeg.empty()) {
      v.push_back(choosedNeg.back());
      choosedNeg.pop_back();
    }

    // undo
    while (!choosedPos.empty()) {
      v.push_back(choosedPos.back());
      choosedPos.pop_back();
    }

    while (!neg.empty()) {
      v.push_back(neg.back());
      neg.pop_back();
    }

    // choose min k elements by absolute value
    sort(v.begin(), v.end());
    for (int i = 0; i < k; ++i) {
      choosedPos.push_back(v[i]);
    }

    choosedPos[0] *= -1;
  } else {
    // greedy
    while (choosedNeg.size() >= 2 && pos.size() >= 2) {
      ll have = 1LL * choosedNeg.back() * choosedNeg[choosedNeg.size() - 2];
      ll can = 1LL * pos.back() * pos[pos.size() - 2];

      if (can > have) {
        choosedNeg.pop_back();
        choosedNeg.pop_back();

        choosedPos.push_back(pos.back());
        pos.pop_back();

        choosedPos.push_back(pos.back());
        pos.pop_back();
      } else {
        break;
      }
    }
  }

  ll ans = Calculate();
  while (ans < 0) {
    ans += MOD;
  }

  cout << ans;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
typedef pair<int, int> pi;
typedef long long ll;

const int MAX = 2e5 + 9;
const int MOD = 1000000007;
int n, k;
vector<int> pos, choosedPos;
vector<int> neg, choosedNeg;

ll Calculate() {
  ll ans = 1;
  for (int i = 0; i < choosedNeg.size(); ++i) {
    ans *= choosedPos[i];
    ans %= MOD;
  }

  for (int i = 0; i < choosedPos.size(); ++i) {
    ans *= choosedPos[i];
    ans %= MOD;
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  while (n--) {
    int a;
    cin >> a;

    if (a > 0)
      pos.push_back(a);
    else
      neg.push_back(-a);
  }

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end(), greater<int>());

  // max 2x negative
  while (choosedNeg.size() + 1 < k && neg.size() >= 2) {
    choosedNeg.push_back(neg.back());
    neg.pop_back();

    choosedNeg.push_back(neg.back());
    neg.pop_back();
  }

  // fill by positive
  while (choosedNeg.size() + choosedPos.size() < k && !pos.empty()) {
    choosedPos.push_back(pos.back());
    pos.pop_back();
  }

  if (choosedPos.size() + choosedNeg.size() < k) { // must choose one neg
    vector<int> v;

    // undo
    while (!choosedNeg.empty()) {
      v.push_back(choosedNeg.back());
      choosedNeg.pop_back();
    }

    // undo
    while (!choosedPos.empty()) {
      v.push_back(choosedPos.back());
      choosedPos.pop_back();
    }

    while (!neg.empty()) {
      v.push_back(neg.back());
      neg.pop_back();
    }

    // choose min k elements by absolute value
    sort(v.begin(), v.end());
    for (int i = 0; i < k; ++i) {
      choosedPos.push_back(v[i]);
    }

    choosedPos[0] *= -1;
  } else {
    // greedy
    while (choosedNeg.size() >= 2 && pos.size() >= 2) {
      ll have = 1LL * choosedNeg.back() * choosedNeg[choosedNeg.size() - 2];
      ll can = 1LL * pos.back() * pos[pos.size() - 2];

      if (can > have) {
        choosedNeg.pop_back();
        choosedNeg.pop_back();

        choosedPos.push_back(pos.back());
        pos.pop_back();

        choosedPos.push_back(pos.back());
        pos.pop_back();
      } else {
        break;
      }
    }
  }

  ll ans = Calculate();
  while (ans < 0) {
    ans += MOD;
  }

  cout << ans;

  return 0;
}