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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg, (x).ed
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (ll i = (n); i >= 1; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll maxx(ll x, ll y, ll z) { return max(max(x, y), z); }
ll minn(ll x, ll y, ll z) { return min(min(x, y), z); }
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * (y / gcd(x, y)); }

vector<ll> pw2(51, 1);
vector<ll> pw10(19, 1);
int main() {
  ll N, K;
  cin >> N >> K;
  ll tmp = 1;
  vector<ll> A(N);
  vector<ll> minus;
  vector<ll> plus;
  vector<P> absA;
  rep(i, N) {
    cin >> A[i];
    if (A[i] < 0)
      minus.pb(A[i]);
    else if (A[i] > 0)
      plus.pb(A[i]);

    absA.pb(P(abs(A[i]), A[i]));
  }
  sort(all(absA), greater<P>());

  ll minus_size = sz(minus);
  ll plus_size = sz(plus);
  ll zero_size = N - minus_size - plus_size;

  ll m = 0;
  ll p = 0;
  rep(i, K) {
    tmp *= absA[i].a;
    tmp %= MOD;
    if (absA[i].b < 0)
      m++;
    else if (absA[i].b > 0)
      p++;
  }
  // 例外
  if (tmp == 0) {
    cout << 0 << endl;
    return 0;
  }

  if (plus_size == 0) {
    ll ans = 1;
    if (K % 2 == 0) {
      sort(all(minus));
      rep(i, K) {
        ans *= (-minus[i]);
        ans %= MOD;
      }
      cout << ans << endl;
    } else {
      if (zero_size == 0) {
        sort(all(minus), greater<ll>());
        rep(i, K) {
          ans *= (-minus[i]);
          ans %= MOD;
        }
        cout << MOD - ans << endl;
      } else {
        cout << 0 << endl;
      }
    }
  } else {
    if (m % 2 == 0) {
      cout << tmp << endl;
    } else {
      if (zero_size == 0 && N == K) {
        cout << MOD - tmp << endl;
      }
      // のこりピッタリ0
      else if (zero_size == N - K) {
        cout << 0 << endl;
      }
      // どっちかできる
      else {
        // ソート
        sort(all(plus), greater<ll>());
        sort(all(minus));

        ll ans_cnd_1 = 0;
        ll ans_cnd_2 = 0;
        // if m奇:(m,p)->(m-1,p+1)or(m+1,p-1)
        // (左)<(右) <-> (0-index) p/m-1 < m/p-1
        if ((0 <= m - 1) && (m - 1 <= minus_size) && (0 <= p + 1) &&
            (p + 1 <= plus_size)) {
          ans_cnd_1 = 1;

          rep(i, m - 1) {
            ans_cnd_1 *= (-minus[i]);
            ans_cnd_1 %= MOD;
          }
          rep(i, p + 1) {
            ans_cnd_1 *= (plus[i]);
            ans_cnd_1 %= MOD;
          }
        }
        if ((0 <= m + 1) && (m + 1 <= minus_size) && (0 <= p - 1) &&
            (p - 1 <= plus_size)) {
          ans_cnd_2 = 1;

          rep(i, m + 1) {
            ans_cnd_2 *= (-minus[i]);
            ans_cnd_2 %= MOD;
          }
          rep(i, p - 1) {
            ans_cnd_2 *= (plus[i]);
            ans_cnd_2 %= MOD;
          }
        }

        // どっちか0なら
        if (ans_cnd_1 * ans_cnd_2 == 0) {
          cout << max(ans_cnd_1, ans_cnd_2) << endl;
        } else {
          if ((plus[p] * plus[p - 1]) < (minus[m - 1] * minus[m]))
            cout << ans_cnd_2 << endl;
          else
            cout << ans_cnd_1 << endl;
        }
      }
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg, (x).ed
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (ll i = (n); i >= 1; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll maxx(ll x, ll y, ll z) { return max(max(x, y), z); }
ll minn(ll x, ll y, ll z) { return min(min(x, y), z); }
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * (y / gcd(x, y)); }

vector<ll> pw2(51, 1);
vector<ll> pw10(19, 1);
int main() {
  ll N, K;
  cin >> N >> K;
  ll tmp = 1;
  vector<ll> A(N);
  vector<ll> minus;
  vector<ll> plus;
  vector<P> absA;
  rep(i, N) {
    cin >> A[i];
    if (A[i] < 0)
      minus.pb(A[i]);
    else if (A[i] > 0)
      plus.pb(A[i]);

    absA.pb(P(abs(A[i]), A[i]));
  }
  sort(all(absA), greater<P>());

  ll minus_size = sz(minus);
  ll plus_size = sz(plus);
  ll zero_size = N - minus_size - plus_size;

  if (plus_size == 0) {
    ll ans = 1;
    if (K % 2 == 0) {
      sort(all(minus));
      rep(i, K) {
        ans *= (-minus[i]);
        ans %= MOD;
      }
      cout << ans << endl;
    } else {
      if (zero_size == 0) {
        sort(all(minus), greater<ll>());
        rep(i, K) {
          ans *= (-minus[i]);
          ans %= MOD;
        }
        cout << (MOD - ans) % MOD << endl;
      } else {
        cout << 0 << endl;
      }
    }
  } else {
    ll m = 0;
    ll p = 0;

    rep(i, K) {
      tmp *= absA[i].a;
      tmp %= MOD;
      if (absA[i].b < 0)
        m++;
      else if (absA[i].b > 0)
        p++;
    }
    // ここで0なら終わり
    if (tmp == 0) {
      cout << 0 << endl;
    } else {
      if (m % 2 == 0) {
        cout << tmp << endl;
      } else {
        // のこりピッタリ0
        if (zero_size == N - K) {
          cout << 0 << endl;
        }
        // どっちかできる
        else {
          ll ans_cnd_1 = 0;
          ll ans_cnd_2 = 0;
          // if m奇:(m,p)->(m-1,p+1)or(m+1,p-1)
          // (左)<(右) <-> (0-index) p/m-1 < m/p-1
          if ((0 <= m - 1) && (m - 1 <= minus_size) && (0 <= p + 1) &&
              (p + 1 <= plus_size)) {
            ans_cnd_1 = 1;

            rep(i, m - 1) {
              ans_cnd_1 *= (-minus[i]);
              ans_cnd_1 %= MOD;
            }
            rep(i, p + 1) {
              ans_cnd_1 *= (plus[i]);
              ans_cnd_1 %= MOD;
            }
          }
          if ((0 <= m + 1) && (m + 1 <= minus_size) && (0 <= p - 1) &&
              (p - 1 <= plus_size)) {
            ans_cnd_2 = 1;

            rep(i, m + 1) {
              ans_cnd_2 *= (-minus[i]);
              ans_cnd_2 %= MOD;
            }
            rep(i, p - 1) {
              ans_cnd_2 *= (plus[i]);
              ans_cnd_2 %= MOD;
            }
          }

          // どっちか0なら
          if (ans_cnd_1 * ans_cnd_2 == 0) {
            cout << max(ans_cnd_1, ans_cnd_2) << endl;
          } else {
            if ((plus[p] * plus[p - 1]) < (minus[m - 1] * minus[m]))
              cout << ans_cnd_2 << endl;
            else
              cout << ans_cnd_1 << endl;
          }
        }
      }
    }
  }
}