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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll MOD = 1e9 + 7;

ll mod_pow(ll a, ll b, ll p) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}

int main2() {
  int N = nextLong();
  int K = nextLong();
  vector<ll> A(N);
  REP(i, N) A[i] = nextLong();

  ll plus = 0, zero = 0, minus = 0;
  REP(i, N) {
    if (A[i] > 0)
      plus++;
    if (A[i] == 0)
      zero++;
    if (A[i] < 0)
      minus++;
  }

  vector<ll> pos, neg;
  REP(i, N) {
    if (A[i] > 0)
      pos.push_back(A[i]);
    if (A[i] < 0)
      neg.push_back(abs(A[i]));
  }

  if (K == 1) {
    sort(ALL(pos));
    sort(ALL(neg));

    if (pos.size() > 0) {
      cout << pos.back() << endl;
    } else if (neg.size() > 0) {
      cout << neg[0] << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }

  if ((K % 2 == 0 && minus / 2 * 2 + plus >= K) ||
      (K % 2 == 1 && minus / 2 * 2 + plus >= K && plus >= 1)) {

    sort(ALL(pos));
    reverse(ALL(pos));
    sort(ALL(neg));
    reverse(ALL(neg));

    pos.push_back(0);
    pos.push_back(0);
    neg.push_back(0);
    neg.push_back(0);

    ll ans = 1;

    int a = 0, b = 0;
    if (K % 2) {
      K--;
      ans *= pos[a];
      a++;
    }
    for (int k = 0; k < K; k += 2) {
      ll val_a = pos[a] * pos[a + 1];
      ll val_b = neg[b] * neg[b + 1];
      if (val_a >= val_b) {
        (ans *= pos[a]) %= MOD;
        (ans *= pos[a + 1]) %= MOD;
        a += 2;
      } else {
        (ans *= neg[b]) %= MOD;
        (ans *= neg[b + 1]) %= MOD;
        b += 2;
      }
    }

    cout << ans << endl;
    return 0;

  } else if (zero > 0) {
    cout << 0 << endl;
    return 0;
  } else {

    REP(i, N) A[i] = abs(A[i]);
    sort(ALL(A));

    ll ans = 1;
    REP(k, K) { (ans *= A[k]) %= MOD; }

    cout << (MOD - ans) % MOD << endl;

    return 0;
  }
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll MOD = 1e9 + 7;

ll mod_pow(ll a, ll b, ll p) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}

int main2() {
  int N = nextLong();
  int K = nextLong();
  vector<ll> A(N);
  REP(i, N) A[i] = nextLong();

  ll plus = 0, zero = 0, minus = 0;
  REP(i, N) {
    if (A[i] > 0)
      plus++;
    if (A[i] == 0)
      zero++;
    if (A[i] < 0)
      minus++;
  }

  vector<ll> pos, neg;
  REP(i, N) {
    if (A[i] > 0)
      pos.push_back(A[i]);
    if (A[i] < 0)
      neg.push_back(abs(A[i]));
  }

  if (K == 1) {
    sort(ALL(pos));
    sort(ALL(neg));

    if (pos.size() > 0) {
      cout << pos.back() << endl;
    } else if (neg.size() > 0) {
      cout << neg[0] << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }

  if ((minus / 2 * 2 + plus >= K) && (K % 2 == 0 || plus >= 1)) {

    sort(ALL(pos));
    reverse(ALL(pos));
    sort(ALL(neg));
    reverse(ALL(neg));

    pos.push_back(0);
    pos.push_back(0);
    neg.push_back(0);
    neg.push_back(0);

    ll ans = 1;

    int a = 0, b = 0;
    if (K % 2) {
      K--;
      ans *= pos[a];
      a++;
    }
    for (int k = 0; k < K; k += 2) {
      ll val_a = pos[a] * pos[a + 1];
      ll val_b = neg[b] * neg[b + 1];
      if (val_a >= val_b) {
        (ans *= pos[a]) %= MOD;
        (ans *= pos[a + 1]) %= MOD;
        a += 2;
      } else {
        (ans *= neg[b]) %= MOD;
        (ans *= neg[b + 1]) %= MOD;
        b += 2;
      }
    }

    cout << ans << endl;
    return 0;

  } else if (zero > 0) {
    cout << 0 << endl;
    return 0;
  } else {

    sort(ALL(neg));
    ll ans = 1;
    REP(k, K) { (ans *= neg[k]) %= MOD; }

    cout << (MOD - ans) % MOD << endl;

    return 0;
  }
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}