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

#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll i, j, k;
  ll N, K;
  cin >> N >> K;
  vll Po, Ne;
  for (i = 0; i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0) {
      Po.push_back(a);
    } else {
      Ne.push_back(a);
    }
  }
  sort(Po.rbegin(), Po.rend());
  sort(Ne.begin(), Ne.end());
  if (Po.size() == 0) {
    ll ans = 1;
    if (K % 2 == 0) {
      for (i = 0; i < K; i++) {
        ans = ans * abs(Ne[i]) % MOD;
      }
    } else {
      for (i = 0; i < K; i++) {
        ans = ans * abs(Ne[Ne.size() - 1 - i]) % MOD;
      }
      ans *= -1;
      ans += MOD;
    }
    cout << ans << endl;
    return 0;
  }
  if (N == K) {
    ll ans = 1;
    for (i = 0; i < Po.size(); i++) {
      ans = ans * Po[i] % MOD;
    }
    for (i = 0; i < Ne.size(); i++) {
      ans = ans * abs(Ne[i]) % MOD;
    }
    if (Ne.size() % 2 == 1) {
      ans *= -1;
      ans += MOD;
    }
    cout << ans << endl;
    return 0;
  }

  vll pselect, nselect;
  ll itp = 0, itn = 0;
  ll ncnt = 0;
  for (i = 0; i < K; i++) {
    if (itp >= Po.size()) {
      nselect.push_back(Ne[itn]);
      itn++;
      ncnt++;
      continue;
    }
    if (itn >= Ne.size()) {
      pselect.push_back(Po[itp]);
      itp++;
      continue;
    }
    if (Po[itp] >= -1 * Ne[itn]) {
      pselect.push_back(Po[itp]);
      itp++;
    } else {
      nselect.push_back(Ne[itn]);
      itn++;
      ncnt++;
    }
  }
  ll psize = Po.size(), nsize = Ne.size();
  if (ncnt % 2 == 1) {
    if (itp >= Po.size()) {
      itp--;
      itn++;
      pselect.pop_back();
      nselect.push_back(Ne[itn]);
    } else if (itn >= Ne.size() || itp == 0) {
      itp++;
      itn--;
      pselect.push_back(Po[itp]);
      nselect.pop_back();
    } else {
      ll prod1 = Po[itp - 1] * Po[itp];
      ll prod2 = Ne[itn - 1] * Ne[itn];
      if (prod1 >= prod2) {
        itp++;
        itn--;
        pselect.push_back(Po[itp]);
        nselect.pop_back();
      } else {
        itp--;
        itn++;
        pselect.pop_back();
        nselect.push_back(Ne[itn]);
      }
    }
  }
  ll ans = 1;
  for (i = 0; i < pselect.size(); i++) {
    ans = ans * Po[i] % MOD;
  }
  for (i = 0; i < nselect.size(); i++) {
    ans = ans * abs(Ne[i]) % MOD;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll i, j, k;
  ll N, K;
  cin >> N >> K;
  vll Po, Ne;
  for (i = 0; i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0) {
      Po.push_back(a);
    } else {
      Ne.push_back(a);
    }
  }
  sort(Po.rbegin(), Po.rend());
  sort(Ne.begin(), Ne.end());
  if (Po.size() == 0) {
    ll ans = 1;
    if (K % 2 == 0) {
      for (i = 0; i < K; i++) {
        ans = ans * abs(Ne[Ne.size() - 1 - i]) % MOD;
      }
    } else {
      for (i = 0; i < K; i++) {
        ans = ans * abs(Ne[i]) % MOD;
      }
      ans *= -1;
      ans += MOD;
    }
    cout << ans << endl;
    return 0;
  }
  if (N == K) {
    ll ans = 1;
    for (i = 0; i < Po.size(); i++) {
      ans = ans * Po[i] % MOD;
    }
    for (i = 0; i < Ne.size(); i++) {
      ans = ans * abs(Ne[i]) % MOD;
    }
    if (Ne.size() % 2 == 1) {
      ans *= -1;
      ans += MOD;
    }
    cout << ans << endl;
    return 0;
  }

  vll pselect, nselect;
  ll itp = 0, itn = 0;
  ll ncnt = 0;
  for (i = 0; i < K; i++) {
    if (itp >= Po.size()) {
      nselect.push_back(Ne[itn]);
      itn++;
      ncnt++;
      continue;
    }
    if (Po[itp] >= -1 * Ne[itn]) {
      pselect.push_back(Po[itp]);
      itp++;
    } else {
      nselect.push_back(Ne[itn]);
      itn++;
      ncnt++;
    }
  }
  ll psize = Po.size(), nsize = Ne.size();
  if (ncnt % 2 == 1) {
    if (itp >= Po.size()) {
      itp--;
      itn++;
      pselect.pop_back();
      nselect.push_back(Ne[itn]);
    } else if (itn >= Ne.size() || itp == 0) {
      itp++;
      itn--;
      pselect.push_back(Po[itp]);
      nselect.pop_back();
    } else {
      ll prod1 = Po[itp - 1] * Po[itp];
      ll prod2 = Ne[itn - 1] * Ne[itn];
      if (prod1 >= prod2) {
        itp++;
        itn--;
        pselect.push_back(Po[itp]);
        nselect.pop_back();
      } else {
        itp--;
        itn++;
        pselect.pop_back();
        nselect.push_back(Ne[itn]);
      }
    }
  }
  ll ans = 1;
  for (i = 0; i < pselect.size(); i++) {
    ans = ans * Po[i] % MOD;
  }
  for (i = 0; i < nselect.size(); i++) {
    ans = ans * abs(Ne[i]) % MOD;
  }
  cout << ans << endl;

  return 0;
}