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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Max(a, b) (a > b ? a : b)
#define Min(a, b) (a < b ? a : b)
#define ii pair<int, int>
#define fi first
#define se second
const int MAX = 200000;     // 2 * 10^5
const int MOD = 1000000007; // 10^9 + 7
const int OO = 0x3f3f3f3f;  // 0x3f3f3f3f;

int32_t main() {
  int n, k;
  cin >> n >> k;
  vector<int> P, N, Z;

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0)
      P.push_back(x);
    else if (x < 0)
      N.push_back(x);
    else
      Z.push_back(x);
  }
  sort(P.begin(), P.end());
  sort(N.rbegin(), N.rend());

  vector<int> ans;

  while (k > 1 and (P.size() > 1 or N.size() > 1)) {
    int a = 0, b = 0;
    if (P.size() > 1) {
      a = P.back();
      P.pop_back();
      b = P.back();
      P.pop_back();
    }

    int c = 0, d = 0;
    if (N.size() > 1) {
      c = N.back();
      N.pop_back();
      d = N.back();
      N.pop_back();
    }

    if (c * d >= a * b) {
      ans.push_back(c);
      ans.push_back(d);
      if (a and b) {
        P.push_back(b);
        P.push_back(a);
      }
    } else {
      ans.push_back(a);
      ans.push_back(b);
      if (c and d) {
        N.push_back(d);
        N.push_back(c);
      }
    }

    k -= 2;
  }
  while (k > 0 and P.size() > 0) {
    ans.push_back(P.back());
    P.pop_back();
    k--;
  }
  while (k > 0 and Z.size() > 0) {
    ans.push_back(Z.back());
    Z.pop_back();
    k--;
  }
  if (k > 0 and N.size() > 0) {
    sort(ans.begin(), ans.end());
    while (!ans.empty() and ans.back() < 0) {
      N.push_back(ans.back());
      ans.pop_back();
      k++;
    }
    sort(N.rbegin(), N.rend());
    for (int i = 0; i < k; i++)
      ans.push_back(N[i]);
  }

  int res = 1;

  for (int &w : ans) {
    res = (res * w) % MOD;
    if (res < 0)
      res += MOD;
  }
  cout << res << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Max(a, b) (a > b ? a : b)
#define Min(a, b) (a < b ? a : b)
#define ii pair<int, int>
#define fi first
#define se second
const int MAX = 200000;     // 2 * 10^5
const int MOD = 1000000007; // 10^9 + 7
const int OO = 0x3f3f3f3f;  // 0x3f3f3f3f;

int32_t main() {
  int n, k;
  cin >> n >> k;
  vector<int> P, N, Z;

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0)
      P.push_back(x);
    else if (x < 0)
      N.push_back(x);
    else
      Z.push_back(x);
  }
  sort(P.begin(), P.end());
  sort(N.rbegin(), N.rend());

  vector<int> ans;

  while (k > 1 and (P.size() > 1 or N.size() > 1)) {
    int a = 0, b = 0;
    if (P.size() > 1) {
      a = P.back();
      P.pop_back();
      b = P.back();
      P.pop_back();
    }

    int c = 0, d = 0;
    if (N.size() > 1) {
      c = N.back();
      N.pop_back();
      d = N.back();
      N.pop_back();
    }

    if (c * d >= a * b) {
      ans.push_back(c);
      ans.push_back(d);
      if (a and b) {
        P.push_back(b);
        P.push_back(a);
      }
    } else {
      ans.push_back(a);
      ans.push_back(b);
      if (c and d) {
        N.push_back(d);
        N.push_back(c);
      }
    }

    k -= 2;
  }
  while (k > 0 and P.size() > 0) {
    ans.push_back(P.back());
    P.pop_back();
    k--;
  }
  while (k > 0 and Z.size() > 0) {
    ans.push_back(N.back());
    Z.pop_back();
    k--;
  }
  if (k > 0 and N.size() > 0) {
    sort(ans.begin(), ans.end());
    while (!ans.empty() and ans.back() < 0) {
      N.push_back(ans.back());
      ans.pop_back();
      k++;
    }
    sort(N.rbegin(), N.rend());
    for (int i = 0; i < k; i++)
      ans.push_back(N[i]);
  }

  int res = 1;

  for (int &w : ans) {
    res = (res * w) % MOD;
    if (res < 0)
      res += MOD;
  }
  cout << res << '\\n';

  return 0;
}