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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (i = 0; i < n; i++)
// 2D::rep(i,j)rep(j,j)cin>>bin[i][j];
// vector<int> a(n)
#define Graph vector<vector<int>>
#define A1 cout << "No" << endl
#define A2 cout << "Yes" << endl
#define A3 cout << ans <<
#define AA(i) cout << i << endl;
const int U = 2e5 + 5;
const ll MOD = 1000000007;
const int INF = 1001001001;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> a(N + 1, 0);
  vector<ll> b(N + 1, 0);
  int i, j;
  ll x;
  ll ans = 1;
  int y = 0, z = 0;

  rep(i, N) {
    cin >> x;
    if (x < 0) {
      x = x * (-1);
      b[z] = x;
      z++;
    } else {
      a[y] = x;
      y++;
    }
  }

  if (N == K) {
    rep(i, y) ans = ans * a[i] % MOD;
    rep(i, z) ans = ans * b[i] % MOD;
    if (z % 2 == 1) {
      ans *= -1;
      ans = (ans + MOD) % MOD;
    }
    AA(ans);
    return 0;
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  if (y == 0) {
    if (K % 2 == 1) {
      rep(i, K) ans = ans * b[N + 1 - z + i] % MOD;
      ans = (ans * (-1) + MOD) % MOD;
      AA(ans);
      return 0;
    }
  }

  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());

  y = 0, z = 0;
  for (i = 0; i < K; i++) {
    if (a[y] * a[y + 1] > b[z] * b[z + 1]) {
      ans = ans * a[y] % MOD;
      y++;
    } else if (y + z < K - 1) {
      i++;
      ans = (ans * b[z] % MOD) * b[z + 1] % MOD;
      z += 2;
    } else {
      ans = ans * a[y] % MOD;
      y++;
    }
  }
  AA(ans);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (i = 0; i < n; i++)
// 2D::rep(i,j)rep(j,j)cin>>bin[i][j];
// vector<int> a(n)
#define Graph vector<vector<int>>
#define A1 cout << "No" << endl
#define A2 cout << "Yes" << endl
#define A3 cout << ans <<
#define AA(i) cout << i << endl;
const int U = 2e5 + 5;
const ll MOD = 1000000007;
const int INF = 1001001001;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> a;
  vector<ll> b;
  int i, j;
  ll x;
  ll ans = 1;

  rep(i, N) {
    cin >> x;
    if (x < 0) {
      x = x * (-1);
      b.push_back(x);
    } else
      a.push_back(x);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  if (a.size() == 0) {
    if (K % 2 == 1) {
      rep(i, K) ans = ans * b[i] % MOD;
      ans = (ans * (-1) + MOD) % MOD;
      AA(ans);
      return 0;
    }
  }

  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());

  int cou = 0;
  int y = 0, z = 0;
  while (y + z <= K) {
    if (z == b.size()) {
      ans = ans * a[y] % MOD;
      y++;
    } else if (y == a.size()) {
      ans = ans * b[z] % MOD;
      z++;
    } else if (a[y] > b[z]) {
      ans = ans * a[y] % MOD;
      y++;
    } else {
      ans = ans * b[z] % MOD;
      z++;
    }
  }
  if (z % 2 == 0) {
    AA(ans);
    return 0;
  }
  y++;
  z--;
  ans = 0;
  ll ans2;
  if (y < a.size()) {
    rep(i, y) ans = ans * a[i] % MOD;
    rep(i, z) ans = ans * b[i] % MOD;
  }

  y -= 2;
  z += 2;
  if (z < b.size()) {
    rep(i, y) ans2 = ans2 * a[i] % MOD;
    rep(i, z) ans2 = ans2 * b[i] % MOD;
    if (ans2 < ans)
      AA(ans);
    if (ans >= ans2)
      AA(ans2);
  }
  return 0;
}