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

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

#define pb push_back
#define f1 first
#define s2 second
#define debug(x...) cerr << #x << ": [" << x << "]\\n";
#define fast                                                                   \\
  ios ::sync_with_stdio();                                                     \\
  cout.tie(0);                                                                 \\
  cin.tie(0);

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

bool myComp(const int &x, const int &y) { return abs(x) > abs(y); }

int const N = 2e5 + 7;
ll const mod = 1e9 + 7;
ll ar[N];

inline ll mult(ll x, ll y) { return (x * y) % mod; }

int main() {
  fast;

  int n, k;
  cin >> n >> k;

  bool allNegative = true;
  bool zero = false;
  for (int i = 0; i < n; ++i) {
    cin >> ar[i];

    if (ar[i] > 0)
      allNegative = false;
    if (ar[i] == 0)
      zero = true;
  }

  ll res = 1;
  if (n == k) {
    for (int i = 0; i < n; ++i)
      res = mult(res, ar[i]);
    cout << (res + mod) % mod << '\\n';
  }

  else if (allNegative && k % 2 == 1) {
    sort(ar, ar + n, greater<int>());
    for (int i = 0; i < k; ++i)
      res = mult(res, ar[i]);
    cout << (res + mod) % mod << '\\n';
  }

  else {
    sort(ar, ar + n, myComp);

    bool negative = false;
    for (int i = 0; i < k; ++i)
      if (ar[i] < 0)
        negative = !negative;

    if (negative) {
      ll *a, *b, *c, *d; // a will be swapped with b, c will be swapped with d
      a = b = c = d = NULL;
      for (int i = k - 1; i >= 0; --i) {
        if (a == NULL && ar[i] < 0)
          a = &ar[i];
        else if (b == NULL && ar[i] > 0)
          b = &ar[i];
      }

      for (int i = k; i < n; ++i) {
        if (c == NULL && ar[i] > 0)
          c = &ar[i];
        else if (d == NULL && ar[i] < 0)
          d = &ar[i];
      }

      if ((a == NULL || c == NULL) && (b == NULL || d == NULL) && zero)
        *a = 0;
      else if (a == NULL || c == NULL)
        swap(*b, *d);
      else if (b == NULL || d == NULL)
        swap(*a, *c);
      else if (abs(*b) * abs(*c) > abs(*a) * abs(*d))
        swap(*a, *c);
      else
        swap(*b, *d);
    }

    for (int i = 0; i < k; ++i)
      res = mult(res, ar[i]);
    cout << (res + mod) % mod << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define pb push_back
#define f1 first
#define s2 second
#define debug(x...) cerr << #x << ": [" << x << "]\\n";
#define fast                                                                   \\
  ios ::sync_with_stdio();                                                     \\
  cout.tie(0);                                                                 \\
  cin.tie(0);

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

bool myComp(const int &x, const int &y) { return abs(x) > abs(y); }

int const N = 2e5 + 7;
ll const mod = 1e9 + 7;
ll ar[N];

inline ll mult(ll x, ll y) { return (x * y) % mod; }

int main() {
  fast;

  int n, k;
  cin >> n >> k;

  bool allNegative = true;
  for (int i = 0; i < n; ++i) {
    cin >> ar[i];

    if (ar[i] > 0)
      allNegative = false;
  }

  ll res = 1;
  if (n == k) {
    for (int i = 0; i < n; ++i)
      res = mult(res, ar[i]);
    cout << (res + mod) % mod << '\\n';
  }

  else if (allNegative && k % 2 == 1) {
    sort(ar, ar + n, greater<int>());
    for (int i = 0; i < k; ++i)
      res = mult(res, ar[i]);
    cout << (res + mod) % mod << '\\n';
  }

  else {
    sort(ar, ar + n, myComp);

    bool negative = false;
    for (int i = 0; i < k; ++i)
      if (ar[i] < 0)
        negative = !negative;

    if (negative) {
      ll *a, *b, *c, *d; // a will be swapped with b, c will be swapped with d
      a = b = c = d = NULL;
      for (int i = k - 1; i >= 0; --i) {
        if (a == NULL && ar[i] < 0)
          a = &ar[i];
        else if (b == NULL && ar[i] > 0)
          b = &ar[i];
      }

      for (int i = k; i < n; ++i) {
        if (c == NULL && ar[i] > 0)
          c = &ar[i];
        else if (d == NULL && ar[i] < 0)
          d = &ar[i];
      }
      if (a == NULL || c == NULL)
        swap(*b, *d);
      else if (b == NULL || d == NULL)
        swap(*a, *c);
      else if (abs(*b) * abs(*c) > abs(*a) * abs(*d))
        swap(*a, *c);
      else
        swap(*b, *d);
    }

    for (int i = 0; i < k; ++i)
      res = mult(res, ar[i]);
    cout << (res + mod) % mod << '\\n';
  }
  return 0;
}