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

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

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
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;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vl p;
  vl m;
  ll a;
  REP(i, n) {
    cin >> a;
    if (a >= 0)
      p.emplace_back(a);
    else
      m.emplace_back(-a);
  }

  ll ans = 1;
  if (n == k) {
    REP(i, sz(p)) ans = ans * p[i] % MOD;
    REP(i, sz(m)) ans = ans * -m[i] % MOD;
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << (ans + MOD) % MOD << endl;
    return;
  } else if (sz(p) == 0 and k % 2 == 1) {
    SORT(m);
    REP(i, k) ans = ans * -m[i] % MOD;
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << (ans + MOD) % MOD << endl;
    return;
  }

  SORT(p);
  REV(p);
  SORT(m);
  REV(m);
  p.emplace_back(-1);
  m.emplace_back(-1);
  int pi = 0;
  int mi = 0;
  REP(i, k) {
    if (mi == sz(m) - 1)
      ans = ans * p[pi++] % MOD;
    else if (pi == sz(p) - 1)
      ans = ans * m[mi++] % MOD;
    else if (p[pi] >= m[mi])
      ans = ans * p[pi++] % MOD;
    else
      ans = ans * m[mi++] % MOD;
  }
  if (mi % 2 == 0) {
    cout << ans << endl;
    return;
  }

  ans = 1;
  if (pi != k and
      (mi + 1 == sz(m) or mi == k or p[pi] * p[pi - 1] >= m[mi] * m[mi - 1])) {
    REP(i, pi + 1) ans = ans * p[i] % MOD;
    REP(i, mi - 1) ans = ans * m[i] % MOD;
  } else {
    REP(i, pi - 1) ans = ans * p[i] % MOD;
    REP(i, mi + 1) ans = ans * m[i] % MOD;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
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;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vl p;
  vl m;
  ll a;
  REP(i, n) {
    cin >> a;
    if (a >= 0)
      p.emplace_back(a);
    else
      m.emplace_back(-a);
  }

  ll ans = 1;
  if (n == k) {
    REP(i, sz(p)) ans = ans * p[i] % MOD;
    REP(i, sz(m)) ans = ans * m[i] % MOD;
    if (sz(m) % 2 == 0)
      cout << ans << endl;
    else
      cout << (-ans + MOD) % MOD << endl;
    return;
  } else if (sz(p) == 0 and k % 2 == 1) {
    SORT(m);
    REP(i, k) ans = ans * m[i] % MOD;
    cout << (-ans + MOD) % MOD << endl;
    return;
  }

  SORT(p);
  REV(p);
  SORT(m);
  REV(m);
  p.emplace_back(-1);
  m.emplace_back(-1);
  int pi = 0;
  int mi = 0;
  REP(i, k) {
    if (p[pi] >= m[mi] and pi == sz(p) - 1)
      ans = ans * p[pi++] % MOD;
    else
      ans = ans * m[mi++] % MOD;
  }
  if (mi % 2 == 0) {
    cout << ans << endl;
    return;
  }

  if (pi + 1 < sz(p)) {
    ll ans1 = 1;
    REP(i, pi + 1) ans1 = ans1 * p[i] % MOD;
    REP(i, mi - 1) ans1 = ans1 * m[i] % MOD;
    chmax(ans, ans1);
  }
  if (mi + 1 < sz(m)) {
    ll ans1 = 1;
    REP(i, pi - 1) ans1 = ans1 * p[i] % MOD;
    REP(i, mi + 1) ans1 = ans1 * m[i] % MOD;
    chmax(ans, ans1);
  }

  cout << ans << endl;
}