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

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
#define all(v) v.begin(), v.end()

int main() {
  ll N, K, k = 0, m = 0, ans = 1;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A.at(i);
    if (A.at(i) < 0)
      k++;
    if (A.at(i) == 0)
      m++;
  }
  if (K + m > N) {
    cout << 0 << endl;
    return 0;
  } else if (K == N) {
    rep(i, N) {
      if (A.at(i) < 0)
        ans *= (MOD + A.at(i));
      else
        ans *= A.at(i);
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  } else if (k + m == N && K % 2 == 1) {
    sort(all(A));
    reverse(all(A));
    rep(i, K) {
      ans *= (MOD + A.at(i));
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  } else {
    vector<ll> B, C;
    rep(i, N) {
      if (A.at(i) >= 0)
        B.push_back(A.at(i));
      else
        C.push_back(A.at(i));
    }
    if (B.size() != 0) {
      sort(all(B));
      reverse(all(B));
    }
    if (C.size() != 0) {
      sort(all(C));
    }
    if (K % 2 == 1) {
      ans = B.at(0);
      ll t = 1, s = 0;
      rep(i, K / 2) {
        if (t >= B.size() - 1 || B.size() == 0) {
          ans *= (C.at(s) * C.at(s + 1)) % MOD;
          s += 2;
          ans %= MOD;
        } else if (s >= C.size() - 1 || C.size() == 0) {
          ans *= (B.at(t) * B.at(t + 1)) % MOD;
          t += 2;
          ans %= MOD;
        } else {
          if (B.at(t) * B.at(t + 1) > C.at(s) * C.at(s + 1)) {
            ans *= (B.at(t) * B.at(t + 1)) % MOD;
            t += 2;
            ans %= MOD;
          } else {
            ans *= (C.at(s) * C.at(s + 1)) % MOD;
            s += 2;
            ans %= MOD;
          }
        }
      }
    } else {
      ans = 1;
      ll t = 0, s = 0;
      rep(i, K / 2) {
        if (t >= B.size() - 1 || B.size() == 0) {
          ans *= (C.at(s) * C.at(s + 1)) % MOD;
          s += 2;
          ans %= MOD;
        } else if (s >= C.size() - 1 || C.size() == 0) {
          ans *= (B.at(t) * B.at(t + 1)) % MOD;
          t += 2;
          ans %= MOD;
        } else {
          if (B.at(t) * B.at(t + 1) > C.at(s) * C.at(s + 1)) {
            ans *= (B.at(t) * B.at(t + 1)) % MOD;
            t += 2;
            ans %= MOD;
          } else {
            ans *= (C.at(s) * C.at(s + 1)) % MOD;
            s += 2;
            ans %= MOD;
          }
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
#define all(v) v.begin(), v.end()

int main() {
  ll N, K, k = 0, m = 0, ans = 1;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A.at(i);
    if (A.at(i) < 0)
      k++;
    if (A.at(i) == 0)
      m++;
  }
  if (K + m > N) {
    cout << 0 << endl;
    return 0;
  } else if (K == N) {
    rep(i, N) {
      if (A.at(i) < 0)
        ans *= (MOD + A.at(i));
      else
        ans *= A.at(i);
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  } else if (k + m == N && K % 2 == 1) {
    sort(all(A));
    reverse(all(A));
    rep(i, K) {
      ans *= (MOD + A.at(i));
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  } else {
    vector<ll> B, C;
    rep(i, N) {
      if (A.at(i) >= 0)
        B.push_back(A.at(i));
      else
        C.push_back(A.at(i));
    }
    sort(all(B));
    reverse(all(B));
    sort(all(C));
    if (K % 2 == 1) {
      ans = B.at(0);
      ll t = 1, s = 0;
      rep(i, K / 2) {
        if (t >= B.size() - 1) {
          ans *= (C.at(s) * C.at(s + 1)) % MOD;
          s += 2;
          ans %= MOD;
        } else if (s >= C.size() - 1) {
          ans *= (B.at(t) * B.at(t + 1)) % MOD;
          t += 2;
          ans %= MOD;
        } else {
          if (B.at(t) * B.at(t + 1) > C.at(s) * C.at(s + 1)) {
            ans *= (B.at(t) * B.at(t + 1)) % MOD;
            t += 2;
            ans %= MOD;
          } else {
            ans *= (C.at(s) * C.at(s + 1)) % MOD;
            s += 2;
            ans %= MOD;
          }
        }
      }
    } else {
      ans = 1;
      ll t = 0, s = 0;
      rep(i, K / 2) {
        if (t >= B.size() - 1) {
          ans *= (C.at(s) * C.at(s + 1)) % MOD;
          s += 2;
          ans %= MOD;
        } else if (s >= C.size() - 1) {
          ans *= (B.at(t) * B.at(t + 1)) % MOD;
          t += 2;
          ans %= MOD;
        } else {
          if (B.at(t) * B.at(t + 1) > C.at(s) * C.at(s + 1)) {
            ans *= (B.at(t) * B.at(t + 1)) % MOD;
            t += 2;
            ans %= MOD;
          } else {
            ans *= (C.at(s) * C.at(s + 1)) % MOD;
            s += 2;
            ans %= MOD;
          }
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
}