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

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N, K;
  cin >> N >> K;
  ll A[N];
  ll ans = 1;
  vector<pair<ll, ll>> Z;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Z.push_back(make_pair(abs(A[i]), i));
  }
  sort(Z.begin(), Z.end());
  reverse(Z.begin(), Z.end());
  int minus = 0;
  for (int i = 0; i < K; i++) {
    if (A[Z[i].S] < 0) {
      minus++;
    }
    if (A[Z[i].S] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  if (minus % 2 == 0) {
    for (int i = 0; i < K; i++) {
      ans *= A[Z[i].S];
      ans %= Mod;
    }
    cout << ans << endl;
    return 0;
  } else {
    // 最小- を + に
    // 最小+ を - に
    long double way1 = -1.0;
    long double way2 = -1.0;
    int in1 = -1;
    int in2 = -1;
    for (int i = K; i < N; i++) {
      if (A[Z[i].S] > 0) {
        in1 = i;
        break;
      }
    }
    for (int i = K; i < N; i++) {
      if (A[Z[i].S] < 0) {
        in2 = i;
        break;
      }
    }
    int in1_2 = -1;
    int in2_2 = -1;
    for (int i = K - 1; i >= 0; i--) {
      if (A[Z[i].S] < 0) {
        in1_2 = i;
        break;
      }
    }
    for (int i = K - 1; i >= 0; i--) {
      if (A[Z[i].S] > 0) {
        in2_2 = i;
        break;
      }
    }

    if (in1 != -1 && in1_2 != -1) {
      way1 = (long double)A[Z[in1_2].S] / (long double)A[Z[in1].S];
      way1 *= -1;
    }
    if (in2 != -1 && in2_2 != -1) {
      way2 = (long double)A[Z[in2_2].S] / (long double)A[Z[in2].S];
      way2 *= -1;
    }

    if (way1 == -1 && way2 == -1) {
      for (int i = N - 1; i >= N - K; i--) {
        ans *= A[Z[i].S];
        ans %= Mod;
      }
      if (ans < 0)
        ans += Mod;
      cout << ans << endl;
      return 0;
    } else if (way1 == -1 || way2 == -1) {
      if (way1 == -1) {
        for (int i = 0; i < K; i++) {
          if (i == in2_2) {
            ans *= A[Z[in2].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        if (ans < 0)
          ans += Mod;
        cout << ans << endl;
        return 0;
      } else {
        for (int i = 0; i < K; i++) {
          if (i == in1_2) {
            ans *= A[Z[in1].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        if (ans < 0)
          ans += Mod;
        cout << ans << endl;
        return 0;
      }
    } else {
      if (way1 >= way2) {
        for (int i = 0; i < K; i++) {
          if (i == in2_2) {
            ans *= A[Z[in2].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        if (ans < 0)
          ans += Mod;
        cout << ans << endl;
        return 0;
      } else {
        for (int i = 0; i < K; i++) {
          if (i == in1_2) {
            ans *= A[Z[in1].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        if (ans < 0)
          ans += Mod;
        cout << ans << endl;
        return 0;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N, K;
  cin >> N >> K;
  ll A[N];
  ll ans = 1;
  vector<pair<ll, ll>> Z;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Z.push_back(make_pair(abs(A[i]), i));
  }
  sort(Z.begin(), Z.end());
  reverse(Z.begin(), Z.end());
  int minus = 0;
  for (int i = 0; i < K; i++) {
    if (A[Z[i].S] < 0) {
      minus++;
    }
    if (A[Z[i].S] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  if (minus % 2 == 0) {
    for (int i = 0; i < K; i++) {
      ans *= A[Z[i].S];
      ans %= Mod;
    }
    cout << ans << endl;
    return 0;
  } else {
    // 最小- を + に
    // 最小+ を - に
    long double way1 = -1.0;
    long double way2 = -1.0;
    int in1 = -1;
    int in2 = -1;
    for (int i = K; i < N; i++) {
      if (A[Z[i].S] >= 0) {
        in1 = i;
        break;
      }
    }
    for (int i = K; i < N; i++) {
      if (A[Z[i].S] <= 0) {
        in2 = i;
        break;
      }
    }
    int in1_2 = -1;
    int in2_2 = -1;
    for (int i = K - 1; i >= 0; i--) {
      if (A[Z[i].S] <= 0) {
        in1_2 = i;
        break;
      }
    }
    for (int i = K - 1; i >= 0; i--) {
      if (A[Z[i].S] >= 0) {
        in2_2 = i;
        break;
      }
    }

    if (in1 != -1 && in1_2 != -1) {
      way1 = A[Z[in1_2].S] / A[Z[in1].S];
      way1 *= -1;
    }
    if (in2 != -1 && in2_2 != -1) {
      way2 = A[Z[in2_2].S] / A[Z[in2].S];
      way2 *= -1;
    }

    if (way1 == -1 && way2 == -1) {
      for (int i = N - 1; i >= N - K; i--) {
        ans *= A[Z[i].S];
        ans %= Mod;
      }
      if (ans < 0)
        ans += Mod;
      cout << ans << endl;
      return 0;
    } else if (way1 == -1 || way2 == -1) {
      if (way1 == -1) {
        for (int i = 0; i < K; i++) {
          if (i == in2_2) {
            ans *= A[Z[in2].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        cout << ans << endl;
        return 0;
      } else {
        for (int i = 0; i < K; i++) {
          if (i == in1_2) {
            ans *= A[Z[in1].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        cout << ans << endl;
        return 0;
      }
    } else {
      if (way1 > way2) {
        for (int i = 0; i < K; i++) {
          if (i == in2_2) {
            ans *= A[Z[in2].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        cout << ans << endl;
        return 0;
      } else {
        for (int i = 0; i < K; i++) {
          if (i == in1_2) {
            ans *= A[Z[in1].S];
          } else {
            ans *= A[Z[i].S];
          }
          ans %= Mod;
        }
        cout << ans << endl;
        return 0;
      }
    }
  }
  return 0;
}