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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

const ll MOD = pow(10, 9) + 7;

int main() {
  int n, k;
  cin >> n >> k;

  vector<ll> pa(n);
  for (int i = 0; i < n; i++)
    cin >> pa[i];

  vector<ll> na = pa;

  sort(pa.begin(), pa.end(), greater<ll>());
  sort(na.begin(), na.end());

  int pi = 0;
  int ni = 0;

  ll ps, ns;

  int cnt = k;
  ll ans = 1;

  while (cnt > 1) {
    ps = pa[pi] * pa[pi + 1];
    ns = na[ni] * na[ni + 1];

    if (ps > ns) {
      ans = ans * pa[pi++] % MOD;
      ans = ans * pa[pi++] % MOD;
    } else {
      ans = ans * na[ni++] % MOD;
      ans = ans * na[ni++] % MOD;
    }
    cnt -= 2;
  }

  if (cnt) {
    ans = ans * pa[pi] % MOD;
  }

  if (ans >= 0) {
    cout << ans << endl;
    return 0;
  }

  ll min = abs(pa[0]);
  int id = 0;

  sort(pa.begin(), pa.end());
  sort(na.begin(), na.end(), greater<ll>());

  for (int i = 0; i < n; i++) {
    if (pa[i] > 0) {
      pa.erase(pa.begin(), pa.begin() + i);
      break;
    }
    if (i == n - 1) {
      pa.erase(pa.begin(), pa.end());
    }
  }

  for (int i = 0; i < n; i++) {
    if (na[i] < 0) {
      na.erase(na.begin(), na.begin() + i);
      break;
    }
    if (i == n - 1) {
      na.erase(na.begin(), na.end());
    }
  }

  pi = 0;
  ni = 0;

  cnt = 0;
  ans = 1;

  while (cnt < k) {
    if (pi >= pa.size()) {
      ans = ans * na[ni++] % MOD;
    }

    else if (ni >= na.size()) {
      ans = ans * pa[pi++] % MOD;
    }

    else {
      ps = pa[pi];
      ns = na[ni] * (-1);

      if (ps < ns) {
        ans = ans * pa[pi++] % MOD;
      } else {
        ans = ans * na[ni++] % MOD;
      }
    }

    cnt++;
  }

  cout << (ans + MOD) % MOD << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

const ll MOD = pow(10, 9) + 7;

int main() {
  int n, k;
  cin >> n >> k;

  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a.begin(), a.end());

  int l = 0;
  int r = n - 1;

  ll ls, rs;

  int cnt = 0;
  ll ans = 1;

  cnt = k;
  while (cnt > 1) {
    ls = a[l] * a[l + 1];
    rs = a[r] * a[r - 1];

    if (ls > rs) {
      ans = ans * a[l++] % MOD;
      ans = ans * a[l++] % MOD;
    } else {
      ans = ans * a[r--] % MOD;
      ans = ans * a[r--] % MOD;
    }
    cnt -= 2;
  }

  if (cnt) {
    ans = ans * a[r] % MOD;
  }

  if (ans >= 0) {
    cout << ans << endl;
    return 0;
  }

  ll min = abs(a[0]);
  int id = 0;

  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
    if (abs(a[i]) < min) {
      min = abs(a[i]);
      id = i;
    }
  }

  l = id + 1;
  r = id - 1;

  cnt = k;
  ans = 1;

  while (cnt > 1) {
    ls = a[l] * a[l + 1];
    rs = a[r] * a[r - 1];

    if (ls < rs) {
      ans = ans * a[l++] % MOD;
      ans = ans * a[l++] % MOD;
    } else {
      ans = ans * a[r--] % MOD;
      ans = ans * a[r--] % MOD;
    }
    cnt -= 2;
  }

  if (cnt) {
    ans = ans * a[r] % MOD;
  }

  cout << (ans + MOD) % MOD << endl;
  return 0;
}