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

// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 2e5 + 5;
const int M = 5e5 + 5;
const int OO = 1e9;
const int mod = 1e9 + 7;

int n, k, a[N], I1 = -1, I2 = -1, J1 = -1, J2 = -1;
ll ans = 1;

void init() {}

int mul(int a, int b) {

  a = (a + mod) % mod;
  b = (b + mod) % mod;
  return (1LL * a * b) % mod;
}

ll solve1() {

  for (int i = k - 1; ~i; --i) {
    if (a[i] < 0) {
      I1 = i;
      break;
    }
  }

  for (int i = k; i < n; ++i) {
    if (a[i] >= 0) {
      J1 = i;
      break;
    }
  }

  if (I1 == -1 || J1 == -1)
    return -1;

  int ret = a[J1];

  for (int i = 0; i < k; ++i) {
    if (i != I1)
      ret = mul(ret, a[i]);
  }

  return ret;
}

ll solve2() {

  for (int i = k - 1; ~i; --i) {
    if (a[i] >= 0) {
      I2 = i;
      break;
    }
  }

  for (int i = k; i < n; ++i) {
    if (a[i] < 0) {
      J2 = i;
      break;
    }
  }

  if (I2 == -1 || J2 == -1)
    return -1;

  int ret = a[J2];

  for (int i = 0; i < k; ++i) {
    if (i != I2)
      ret = mul(ret, a[i]);
  }

  return ret;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  sort(a, a + n, [](int a, int b) { return abs(a) < abs(b); });
  reverse(a, a + n);

  int cnt = 0;

  for (int i = 0; i < k; ++i) {
    cnt += (a[i] < 0);
  }

  if (cnt & 1) {

    ll a1 = solve1();
    ll a2 = solve2();

    if (a1 == -1 && a2 == -1) {

      for (int i = n - 1; i > n - 1 - k; --i) {
        ans = mul(ans, a[i]);
      }

      cout << ans << '\\n';

    } else if (a1 == -1)
      cout << a2 << '\\n';
    else if (a2 == -1)
      cout << a1 << '\\n';
    else if (1LL * a[J1] * a[I2] > 1LL * a[J2] * a[I1])
      cout << a1 << '\\n';
    else
      cout << a2 << '\\n';

  } else {
    for (int i = 0; i < k; ++i) {
      ans = mul(ans, a[i]);
    }

    cout << ans << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 2e5 + 5;
const int M = 5e5 + 5;
const int OO = 1e9;
const int mod = 1e9 + 7;

int n, k, a[N], I1 = -1, I2 = -1, J1 = -1, J2 = -1;
ll ans = 1;

void init() {}

int mul(int a, int b) { return (1LL * a * b) % mod; }

ll solve1() {

  for (int i = k - 1; ~i; ++i) {
    if (a[i] < 0) {
      I1 = i;
      break;
    }
  }

  for (int i = k; i < n; ++i) {
    if (a[i] >= 0) {
      J1 = i;
      break;
    }
  }

  if (I1 == -1 || J1 == -1)
    return -1;

  int ret = a[J1];

  for (int i = 0; i < k; ++i) {
    if (i != I1)
      ret = mul(ret, a[i]);
  }

  return ret;
}

ll solve2() {

  for (int i = k - 1; ~i; ++i) {
    if (a[i] >= 0) {
      I2 = i;
      break;
    }
  }

  for (int i = k; i < n; ++i) {
    if (a[i] < 0) {
      J2 = i;
      break;
    }
  }

  if (I2 == -1 || J2 == -1)
    return -1;

  int ret = a[J2];

  for (int i = 0; i < k; ++i) {
    if (i != I2)
      ret = mul(ret, a[i]);
  }

  return ret;
}

int calc1() {

  int ret = solve1() % mod;

  for (int i = 0; i < k; ++i) {
    if (i != I1)
      ret = mul(ret, a[i]);
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  sort(a, a + n, [](int a, int b) { return abs(a) < abs(b); });
  reverse(a, a + n);

  int cnt = 0;

  for (int i = 0; i < k; ++i) {
    cnt += (a[i] < 0);
  }

  if (cnt & 1) {

    ll a1 = solve1();
    ll a2 = solve2();

    if (a1 == -1 && a2 == -1) {

      for (int i = n - 1; i > n - 1 - k; ++i) {
        ans = mul(ans, a[i]);
      }

      cout << ans << '\\n';

    } else if (a1 == -1)
      cout << solve2() << '\\n';
    else if (a2 == -1)
      cout << solve1() << '\\n';
    else if (a1 > a2)
      cout << solve1() << '\\n';
    else
      cout << solve2() << '\\n';

  } else {
    for (int i = 0; i < k; ++i) {
      ans = mul(ans, a[i]);
    }

    cout << ans << '\\n';
  }

  return 0;
}