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

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + (ch ^ 48), ch = getchar();
  x *= f;
  return;
}
template <typename T> void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
  return;
}
const int MAXN = 200010;
int n, k;
long long a[MAXN];
int nump;
inline bool cmp(long long x, long long y) { return abs(x) > abs(y); }
int main() {
  read(n), read(k);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
  if (k == n) {
    long long ret = 1;
    for (int i = 1; i <= n; ++i)
      ret *= a[i], ret %= mod;
    write((ret % mod + mod) % mod), putchar('\\n');
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] >= 0)
      ++nump;
  if (nump == 0 && k % 2 == 1) {
    sort(a + 1, a + n + 1), reverse(a + 1, a + n + 1);
    long long ret = 1;
    for (int i = 1; i <= k; ++i)
      ret *= a[i], ret %= mod;
    write((ret % mod + mod) % mod), putchar('\\n');
    return 0;
  }
  stable_sort(a + 1, a + n + 1, cmp);
  long long minp = 2147483647, maxp = -1, minn = 0, maxn = -2147483647;
  long long p1 = 0, p2 = 0, p3 = 0, p4 = 0;
  long long ans = 1;
  for (int i = 1; i <= k; ++i)
    ans *= a[i], ans %= mod;
  if (ans >= 0) {
    write(ans), putchar('\\n');
    return 0;
  }
  for (int i = 1; i <= k; ++i)
    if (a[i] >= 0 && a[i] < minp)
      minp = a[i], p1 = i;
    else if (a[i] < 0 && a[i] > maxn)
      maxn = a[i], p4 = i;
  for (int i = k + 1; i <= n; ++i)
    if (a[i] >= 0 && a[i] > maxp)
      maxp = a[i], p2 = i;
    else if (a[i] < 0 && a[i] < minn)
      minn = a[i], p3 = i;
  if (minp == 2147483647 || minn == 0) {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p4)
        ans *= a[i], ans %= mod;
    ans *= a[p2], ans %= mod;
  } else if (maxp == -1 || maxn == -2147483647) {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p1)
        ans *= a[i], ans %= mod;
    ans *= a[p3], ans %= mod;
  } else if (minn * maxn > maxp * minp) {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p1)
        ans *= a[i], ans %= mod;
    ans *= a[p3], ans %= mod;
  } else {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p4)
        ans *= a[i], ans %= mod;
    ans *= a[p2], ans %= mod;
  }
  write(ans), putchar('\\n');
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + (ch ^ 48), ch = getchar();
  x *= f;
  return;
}
template <typename T> void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
  return;
}
const int MAXN = 200010;
int n, k;
long long a[MAXN];
int nump;
inline bool cmp(long long x, long long y) {
  if (abs(x) > abs(y))
    return 1;
  else if (abs(x) == abs(y))
    return 0;
  else
    return -1;
}
int main() {
  read(n), read(k);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
  if (k == n) {
    long long ret = 1;
    for (int i = 1; i <= n; ++i)
      ret *= a[i], ret %= mod;
    write((ret % mod + mod) % mod), putchar('\\n');
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] >= 0)
      ++nump;
  if (nump == 0 && k % 2 == 1) {
    sort(a + 1, a + n + 1), reverse(a + 1, a + n + 1);
    long long ret = 1;
    for (int i = 1; i <= k; ++i)
      ret *= a[i], ret %= mod;
    write((ret % mod + mod) % mod), putchar('\\n');
    return 0;
  }
  sort(a + 1, a + n + 1, cmp);
  long long minp = 2147483647, maxp = -1, minn = 0, maxn = -2147483647;
  long long p1 = 0, p2 = 0, p3 = 0, p4 = 0;
  long long ans = 1;
  for (int i = 1; i <= k; ++i)
    ans *= a[i], ans %= mod;
  if (ans >= 0) {
    write(ans), putchar('\\n');
    return 0;
  }
  for (int i = 1; i <= k; ++i)
    if (a[i] >= 0 && a[i] < minp)
      minp = a[i], p1 = i;
    else if (a[i] < 0 && a[i] > maxn)
      maxn = a[i], p4 = i;
  for (int i = k + 1; i <= n; ++i)
    if (a[i] >= 0 && a[i] > maxp)
      maxp = a[i], p2 = i;
    else if (a[i] < 0 && a[i] < minn)
      minn = a[i], p3 = i;
  if (minp == 2147483647 || minn == 0) {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p4)
        ans *= a[i], ans %= mod;
    ans *= a[p2], ans %= mod;
  } else if (maxp == -1 || maxn == -2147483647) {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p1)
        ans *= a[i], ans %= mod;
    ans *= a[p3], ans %= mod;
  } else if (minn * maxn > maxp * minp) {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p1)
        ans *= a[i], ans %= mod;
    ans *= a[p3], ans %= mod;
  } else {
    ans = 1;
    for (int i = 1; i <= k; ++i)
      if (i != p4)
        ans *= a[i], ans %= mod;
    ans *= a[p2], ans %= mod;
  }
  write(ans), putchar('\\n');
  return 0;
}