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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = INT_MAX;
const long long MOD = 1000000007;
const long long INF = LLONG_MAX;

// -------------------------------------------------------

ll K, N;
ll A[201010];
ll cntneg;

int main() {
  cin >> N >> K;
  rep(i, 0, N) {
    cin >> A[i];
    if (A[i] < 0)
      ++cntneg;
  }

  if (N == K) {
    ll ans = 1;
    rep(i, 0, N) {
      ans *= A[i];
      ans += MOD * MOD;
      ans %= MOD;
    }
    cout << ans;
    return 0;
  }

  if (K % 2 == 1 && cntneg == N) {
    sort(A, A + N, greater<ll>());
    ll ans = 1;
    rep(i, 0, K) {
      ans *= A[i];
      ans += MOD * MOD;
      ans %= MOD;
    }
    cout << ans;
    return 0;
  }

  sort(A, A + N, [](ll a, ll b) { return abs(a) > abs(b); });
  ll ans = 1;
  ll negink = 0;
  rep(i, 0, K) {
    ans *= A[i];
    ans += MOD * MOD;
    ans %= MOD;
    if (A[i] < 0)
      ++negink;
  }
  if (negink % 2 == 0) {
    cout << ans;
    return 0;
  }

  ll negpos = -1;
  ll pospos2 = -1;
  for (ll i = K - 1; i >= 0; --i) {
    if (negpos == -1 && A[i] < 0) {
      negpos = i;
    }
    if (pospos2 == -1 && A[i] >= 0) {
      pospos2 = i;
    }
  }

  ll pospos = -1;
  ll negpos2 = -1;
  for (ll i = K; i < N; ++i) {
    if (pospos == -1 && A[i] >= 0) {
      pospos = i;
    }
    if (negpos2 == -1 && A[i] < 0) {
      negpos2 = i;
    }
  }

  if (pospos2 != -1 && negpos2 != -1 &&
      (pospos == -1 || (A[negpos] * A[negpos2] > A[pospos] * A[pospos2]))) {
    ans = 1;
    rep(i, 0, K) {
      if (i == pospos2)
        continue;
      ans *= A[i];
      ans += MOD * MOD;
      ans %= MOD;
    }
    ans *= A[negpos2];
    ans += MOD * MOD;
    ans %= MOD;

    cout << ans;
  } else {
    ans = 1;
    rep(i, 0, K) {
      if (i == negpos)
        continue;
      ans *= A[i];
      ans += MOD * MOD;
      ans %= MOD;
    }
    ans *= A[pospos];
    ans += MOD * MOD;
    ans %= MOD;

    cout << ans;
  }
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = INT_MAX;
const int MOD = 1000000007;
const long long INF = LLONG_MAX;

// -------------------------------------------------------

ll N, K;
ll A[201010];
vector<ll> pos, neg, zero;

int main() {
  //
  cin >> N >> K;
  rep(i, 0, N) {
    ll a;
    cin >> a;
    if (a > 0) {
      pos.push_back(a);
    } else if (a < 0) {
      neg.push_back(a);
    } else {
      zero.push_back(a);
    }
  }

  if (K == N) {
    ll ans = 1;
    for (auto e : pos) {
      ans *= e;
      ans %= MOD;
    }
    for (auto e : neg) {
      ans *= e;
      ans += MOD;
      ans %= MOD;
    }
    for (auto e : zero) {
      ans *= e;
    }
    cout << ans;
    return 0;
  }

  if (K % 2 == 1 && neg.size() == N) {
    sort(neg.begin(), neg.end(), greater<ll>());
    ll ans = 1;
    rep(i, 0, K) {
      ans *= neg[i];
      ans += MOD;
      ans %= MOD;
    }
    cout << ans;
    return 0;
  }

  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());
  if (K % 2 == 0) {
    vector<ll> pos2, neg2, posrest, negrest;
    for (int i = 0; i < pos.size() - 1; i += 2) {
      pos2.push_back(pos[i] * pos[i + 1]);
    }
    pos2.push_back(-INF);
    if (pos.size() % 2 == 1) {
      posrest.push_back(pos.back());
    }

    for (int i = 0; i < neg.size() - 1; i += 2) {
      neg2.push_back(neg[i] * neg[i + 1]);
    }
    neg2.push_back(-INF);
    if (neg.size() % 2 == 1) {
      negrest.push_back(neg.back());
    }

    ll cntpos = 0;
    ll cntneg = 0;

    ll ans = 1;
    while (K > 1) {
      ll cpos2;
      if (cntpos < pos2.size()) {
        cpos2 = pos2[cntpos];
      } else {
        cpos2 = -INF;
      }
      ll cneg2;
      if (cntneg < neg2.size()) {
        cneg2 = neg2[cntneg];
      } else {
        cneg2 = -INF;
      }
      if (cpos2 >= cneg2 && cpos2 != -INF) {
        cpos2 %= MOD;
        ans *= cpos2;
        ans %= MOD;
        cntpos++;
        K -= 2;
      } else if (cneg2 != -INF) {
        cneg2 %= MOD;
        ans *= cneg2;
        ans %= MOD;
        cntneg++;
        K -= 2;
      } else {
        if (posrest.size() > 0) {
          ans *= posrest.front();
          ans %= MOD;
          posrest.pop_back();
          --K;
        } else {
          cout << 0;
          return 0;
        }
      }
    }
    if (K == 1) {
      if (posrest.size() > 0) {
        ans *= posrest.front();
        ans %= MOD;
        posrest.pop_back();
        --K;
      } else {
        cout << 0;
        return 0;
      }
    }
    cout << ans;
  } else {
    ll ans = pos.front();
    // cout << ans << endl;
    K--;

    vector<ll> pos2, neg2, posrest, negrest;
    for (int i = 1; i < pos.size() - 1; i += 2) {
      pos2.push_back(pos[i] * pos[i + 1]);
    }
    pos2.push_back(-INF);
    if (pos.size() % 2 == 0) {
      posrest.push_back(pos.back());
    }

    for (int i = 0; i < neg.size() - 1; i += 2) {
      neg2.push_back(neg[i] * neg[i + 1]);
    }
    neg2.push_back(-INF);
    if (neg.size() % 2 == 1) {
      negrest.push_back(neg.back());
    }

    ll cntpos = 0;
    ll cntneg = 0;

    while (K > 1) {
      ll cpos2;
      if (cntpos < pos2.size()) {
        cpos2 = pos2[cntpos];
      } else {
        cpos2 = -INF;
      }
      ll cneg2;
      if (cntneg < neg2.size()) {
        cneg2 = neg2[cntneg];
      } else {
        cneg2 = -INF;
      }
      if (cpos2 >= cneg2 && cpos2 != -INF) {
        cpos2 %= MOD;
        ans *= cpos2;
        ans %= MOD;
        cntpos++;
        K -= 2;
      } else if (cneg2 != -INF) {
        cneg2 %= MOD;
        ans *= cneg2;
        ans %= MOD;
        cntneg++;
        K -= 2;
      } else {
        if (posrest.size() > 0) {
          ans *= posrest.front();
          ans %= MOD;
          posrest.pop_back();
          --K;
        } else {
          cout << 0;
          return 0;
        }
      }
    }
    if (K == 1) {
      if (posrest.size() > 0) {
        ans *= posrest.front();
        ans %= MOD;
        posrest.pop_back();
        --K;
      } else {
        cout << 0;
        return 0;
      }
    }
    cout << ans;
  }
}