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

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

const LL mod = 1e9 + 7;
const LL inf = 0x3f3f3f3f3f3f3f3f;
vector<pair<LL, int>> v;

LL qpower(LL a, LL b, LL c) {
  LL res = 1;
  a %= c;
  while (b) {
    if (b & 1)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}

LL inv(LL a, LL mod) { return qpower(a, mod - 2, mod); }

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  bool po = false, ne = false;
  LL mul = 1;
  for (LL i = 0, t; i < n; ++i) {
    scanf("%lld", &t);
    mul = ((mul * t + mod) % mod + mod) % mod;
    if (!t)
      continue;
    po |= (t > 0);
    ne |= (t < 0);
    if (t > 0)
      v.push_back({t, 1});
    else
      v.push_back({-t, -1});
  }
  if (k == n)
    return 0 * printf("%lld\\n", mul);
  if ((int)v.size() < k)
    return 0 * printf("0\\n");
  int zero = n - v.size();
  sort(v.begin(), v.end());
  if (!po && k % 2) {
    if (zero)
      return 0 * printf("0\\n");
    else {
      LL ans = 1;
      for (int i = 0; i < k; ++i)
        ans = ((-ans * v[i].first + mod) % mod + mod) % mod;
      return 0 * printf("%lld\\n", ans);
    }
  }
  LL ans = 1, MP = inf, MN = inf, f = 1;
  for (int i = v.size() - 1; i >= v.size() - k; --i) {
    ans = (ans * v[i].first) % mod;
    f *= v[i].second;
    if (v[i].second == 1)
      MP = min(MP, v[i].first);
    else
      MN = min(MN, v[i].first);
  }
  if (f == 1)
    printf("%lld\\n", ans);
  else {
    bool c1 = false, c2 = false;
    LL ans1, ans2;
    long double g1, g2;
    for (int i = v.size() - k - 1; i >= 0; --i) {
      if (c1 && c2)
        break;
      if (!c1 && v[i].second == 1 && MN != inf)
        ans1 = (ans * inv(MN, mod) % mod * v[i].first + mod) % mod,
        g1 = ((long double)v[i].first) / MN, c1 = true;
      else if (!c2 && v[i].second == -1 && MP != inf)
        ans2 = (ans * inv(MP, mod) % mod * v[i].first + mod) % mod,
        g2 = ((long double)v[i].first) / MP, c2 = true;
    }
    if (c1 && c2) {
      if (g1 > g2)
        printf("%lld\\n", ans1);
      else
        printf("%lld\\n", ans2);
    } else if (c1)
      printf("%lld\\n", ans1);
    else
      printf("%lld\\n", ans2);
  }
  return 0;
}

Error: 0

Fixed submission:

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

const LL mod = 1e9 + 7;
const LL inf = 0x3f3f3f3f3f3f3f3f;
vector<pair<LL, int>> v;

LL qpower(LL a, LL b, LL c) {
  LL res = 1;
  a %= c;
  while (b) {
    if (b & 1)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}

LL inv(LL a, LL mod) { return qpower(a, mod - 2, mod); }

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  bool po = false, ne = false;
  for (LL i = 0, t; i < n; ++i) {
    scanf("%lld", &t);
    if (!t)
      continue;
    po |= (t > 0);
    ne |= (t < 0);
    if (t > 0)
      v.push_back({t, 1});
    else
      v.push_back({-t, -1});
  }
  if ((int)v.size() < k)
    return 0 * printf("0\\n");
  int zero = n - v.size();
  sort(v.begin(), v.end());
  if (!po && k % 2) {
    if (zero)
      return 0 * printf("0\\n");
    else {
      LL ans = 1;
      for (int i = 0; i < k; ++i)
        ans = (-ans * v[i].first + mod) % mod;
      return 0 * printf("%lld\\n", ans);
    }
  }
  LL ans = 1, MP = inf, MN = inf, f = 1;
  for (int i = v.size() - 1; i >= v.size() - k; --i) {
    ans = (ans * v[i].first) % mod;
    f *= v[i].second;
    if (v[i].second == 1)
      MP = min(MP, v[i].first);
    else
      MN = min(MN, v[i].first);
  }
  if (f == 1)
    printf("%lld\\n", ans);
  else {
    for (int i = v.size() - k - 1; i >= 0; --i) {
      if (v[i].second == 1 && MN != inf)
        return 0 *
               printf("%lld\\n", ans * inv(MN, mod) % mod * v[i].first % mod);
      else if (v[i].second == -1 && MP != inf)
        return 0 *
               printf("%lld\\n", ans * inv(MP, mod) % mod * v[i].first % mod);
    }
  }
  return 0;
}