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

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

const int mn = 2e5 + 6;
const LL mod = 1e9 + 7;
int n, m;
LL a[mn], ans = 1;

bool cmp1(LL a, LL b) { return a > b; }
bool cmp(LL a, LL b) { return abs(a) > abs(b); }

LL poww(LL a, LL b) {
  LL ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  if (n == m) {
    for (int i = 1; i <= n; ++i)
      ans = ans * a[i] % mod;
    cout << (mod + ans) % mod;
    return 0;
  }
  int f = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] > 0)
      f = 1;
  if (f == 0 && m % 2 == 1) {
    sort(a + 1, a + n + 1, cmp1);
    for (int i = 1; i <= m; ++i)
      ans = ans * a[i] % mod;
    cout << (mod + ans) % mod;
    return 0;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= m; ++i) {
    ans = ans * a[i] % mod;
  }
  if (ans >= 0) {
    cout << ans % mod;
    return 0;
  }
  LL laspos = 0, lasneg = 0, nxtpos = 0, nxtneg = 0;
  for (int i = 1; i <= m; ++i) {
    if (a[i] > 0)
      laspos = a[i];
    if (a[i] < 0)
      lasneg = a[i];
  }
  for (int i = n; i > m; --i) {
    if (a[i] >= 0)
      nxtpos = a[i];
    if (a[i] < 0)
      nxtneg = a[i];
  }
  if (m == 1) {
    cout << nxtpos;
    return 0;
  }
  // cerr<<ans<<endl<<lasneg<<endl<<laspos<<endl<<nxtneg<<endl<<nxtpos<<endl;
  LL ans1 = 0, ans2 = 0;
  ans1 = (abs(ans) * poww(laspos, mod - 2) % mod) * abs(nxtneg) % mod;
  ans2 = (abs(ans) * poww(abs(lasneg), mod - 2) % mod) * nxtpos % mod;
  if (laspos == 0 || nxtpos == 0 || lasneg == 0 || nxtneg == 0) {
    if (nxtneg == 0) {
      cout << ans2;
      return 0;
    }
    if (laspos == 0) {
      cout << ans2;
      return 0;
    }
  }
  if (laspos * nxtpos > lasneg * nxtneg)
    cout << ans2;
  else
    cout << ans1;
}

Error: WA

Fixed submission:

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

const int mn = 2e5 + 6, mod = 1e9 + 7;
int n, m;
LL a[mn], ans = 1;

bool cmp1(LL a, LL b) { return a > b; }
bool cmp(LL a, LL b) { return abs(a) > abs(b); }

LL poww(LL a, LL b) {
  LL ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  if (n == m) {
    for (int i = 1; i <= n; ++i)
      ans = ans * a[i] % mod;
    cout << (mod + ans) % mod;
    return 0;
  }
  int f = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] > 0)
      f = 1;
  if (f == 0 && m % 2 == 1) {
    sort(a + 1, a + n + 1, cmp1);
    for (int i = 1; i <= m; ++i)
      ans = ans * a[i] % mod;
    cout << (mod + ans) % mod;
    return 0;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= m; ++i) {
    ans = ans * a[i] % mod;
  }
  if (ans >= 0) {
    cout << ans;
    return 0;
  }
  LL laspos = 0, lasneg = 0, nxtpos = 0, nxtneg = 0;
  for (int i = 1; i <= m; ++i) {
    if (a[i] > 0)
      laspos = a[i];
    if (a[i] < 0)
      lasneg = a[i];
  }
  for (int i = n; i > m; ++i) {
    if (a[i] > 0)
      nxtpos = a[i];
    if (a[i] < 0)
      nxtneg = a[i];
  }
  LL ans1 = 0, ans2 = 0;
  if (laspos && nxtneg) {
    ans1 = (ans * poww(laspos, mod - 2) % mod) * nxtneg % mod;
  }
  if (nxtpos && lasneg) {
    ans2 = (ans * poww(lasneg, mod - 2) % mod) * nxtpos % mod;
  }
  cout << max(ans1, ans2);
}