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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  vector<pair<ll, int>> B(N); // absの大きい方から並べるようにする. A = {-5, 4,
                              // -2, 1} => B = {(5, 1), (4, 0), (2, 1), (1, 0)}
  rep(i, N) {
    if (A[i] < 0)
      B[i] = pair<ll, int>(-A[i], 1);
    else
      B[i] = pair<ll, int>(A[i], 0);
  }
  sort(B.begin(), B.end(), greater<pair<ll, int>>());
  rep(i, N) {
    if (B[i].second == 1)
      A[i] = -B[i].first;
    else
      A[i] = B[i].first;
  }
  if (K == N) {
    ll ans = 1;
    rep(i, N) { ans = ans * A[i] % mod; }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
  } else {
    ll ans = 1;
    int negcnt = 0;
    rep(i, K) {
      if (A[i] < 0)
        negcnt++;
    }
    if (A[K - 1] == 0) {
      cout << 0 << endl;
      return 0;
    } else if (negcnt == K) {
      if (K % 2) {
        ll exc = -1;
        for (int i = K; i < N; i++)
          if (A[i] >= 0) {
            exc = A[i];
            break;
          }
        if (exc == -1) {
          for (int i = N - 1; i > N - 1 - K; i--)
            ans = ans * A[i] % mod; // バグりそ～
        } else {
          rep(i, K - 1) ans = (ans * A[i]) % mod;
          ans = (ans * exc) % mod;
        }
      } else {
        rep(i, K) ans = ans * A[i] % mod;
      }
    } else {
      if (negcnt % 2) {
        // 正数と負数はK個の中に確実に存在する (negcnt < K && negcnt % 2 == 1)
        ll temp1 = 1; // 負整数をomit
        ll temp2 = 1; // 正整数をomit
        ll ex_neg = 1, ex_pos = -1;
        for (int i = K; i < N; i++)
          if (A[i] < 0) {
            ex_neg = A[i];
            break;
          }
        for (int i = K; i < N; i++)
          if (A[i] > 0) {
            ex_pos = A[i];
            break;
          }
        if (ex_neg == 1 && ex_pos == -1)
          ans = 0;
        else {
          ll rem_neg = 1, rem_pos = -1;
          rep(i, K) if (A[i] < 0) rem_neg = A[i];
          rep(i, K) if (A[i] > 0) rem_pos = A[i];
          // en/rp倍にするか ep/rn倍にするか en/rp >= ep/rn <=> en*rn <= ep*rp
          if (ex_pos != -1) {
            bool removed = false;
            rep(i, K) {
              if (!removed && A[i] == rem_neg) {
                removed = true;
              } else
                temp1 = temp1 * A[i] % mod;
            }
            temp1 = temp1 * ex_pos % mod;
          }
          if (ex_neg != 1) {
            bool removed = false;
            rep(i, K) {
              if (!removed && A[i] == rem_pos) {
                removed = true;
              } else
                temp2 = temp2 * A[i] % mod;
            }
            temp2 = temp2 * ex_neg % mod;
          }
          if (ex_pos == -1)
            ans = temp2; // 負整数をomitできないとき
          else if (ex_neg == 1)
            ans = temp1;
          else {
            if (ex_neg * rem_neg <= ex_pos * rem_pos)
              ans = temp1;
            else
              ans = temp2;
          }
        }
      } else {
        rep(i, K) ans = ans * A[i] % mod;
      }
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  vector<pair<ll, int>> B(N); // absの大きい方から並べるようにする. A = {-5, 4,
                              // -2, 1} => B = {(5, 1), (4, 0), (2, 1), (1, 0)}
  rep(i, N) {
    if (A[i] < 0)
      B[i] = pair<ll, int>(-A[i], 1);
    else
      B[i] = pair<ll, int>(A[i], 0);
  }
  sort(B.begin(), B.end(), greater<pair<ll, int>>());
  rep(i, N) {
    if (B[i].second == 1)
      A[i] = -B[i].first;
    else
      A[i] = B[i].first;
  }
  if (K == N) {
    ll ans = 1;
    rep(i, N) { ans = ans * A[i] % mod; }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
  } else {
    ll ans = 1;
    int negcnt = 0;
    rep(i, K) {
      if (A[i] < 0)
        negcnt++;
    }
    if (A[K - 1] == 0)
      cout << 0 << endl;
    else if (negcnt == K) {
      if (K % 2) {
        ll exc = -1;
        for (int i = K; i < N; i++)
          if (A[i] >= 0) {
            exc = A[i];
            break;
          }
        if (exc == -1) {
          for (int i = N - 1; i > N - 1 - K; i--)
            ans = ans * A[i] % mod; // バグりそ～
        } else {
          rep(i, K - 1) ans = (ans * A[i]) % mod;
          ans = (ans * exc) % mod;
        }
      } else {
        rep(i, K) ans = ans * A[i] % mod;
      }
    } else {
      if (negcnt % 2) {
        // 正数と負数はK個の中に確実に存在する (negcnt < K && negcnt % 2 == 1)
        ll temp1 = 1; // 負整数をomit
        ll temp2 = 1; // 正整数をomit
        ll ex_neg = 1, ex_pos = -1;
        for (int i = K; i < N; i++)
          if (A[i] < 0) {
            ex_neg = A[i];
            break;
          }
        for (int i = K; i < N; i++)
          if (A[i] > 0) {
            ex_pos = A[i];
            break;
          }
        if (ex_neg == 1 && ex_pos == -1)
          ans = 0;
        else {
          ll rem_neg = 1, rem_pos = -1;
          rep(i, K) if (A[i] < 0) rem_neg = A[i];
          rep(i, K) if (A[i] > 0) rem_pos = A[i];
          // en/rp倍にするか ep/rn倍にするか en/rp >= ep/rn <=> en*rn <= ep*rp
          if (ex_pos != -1) {
            bool removed = false;
            rep(i, K) {
              if (!removed && A[i] == rem_neg) {
                removed = true;
              } else
                temp1 = temp1 * A[i] % mod;
            }
            temp1 = temp1 * ex_pos % mod;
          }
          if (ex_neg != 1) {
            bool removed = false;
            rep(i, K) {
              if (!removed && A[i] == rem_pos) {
                removed = true;
              } else
                temp2 = temp2 * A[i] % mod;
            }
            temp2 = temp2 * ex_neg % mod;
          }
          if (ex_pos == -1)
            ans = temp2; // 負整数をomitできないとき
          else if (ex_neg == 1)
            ans = temp1;
          else {
            if (ex_neg * rem_neg <= ex_pos * rem_pos)
              ans = temp1;
            else
              ans = temp2;
          }
        }
      } else {
        rep(i, K) ans = ans * A[i] % mod;
      }
    }
    if (ans < 0)
      ans += mod;
    assert(ans != 1);
    cout << ans << endl;
  }
  return 0;
}