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

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x)                                                               \\
  { std::cout << PREFIX << #x << " = " << x << std::endl; }

std::ostream &output_indent(std::ostream &os, int ind) {
  for (int i = 0; i < ind; i++)
    os << " ";
  return os;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p);
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v);

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << ", ";
  return (os << "]");
}

template <class T> static inline std::vector<T> ndvec(size_t &&n, T val) {
  return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail> static inline auto ndvec(size_t &&n, Tail &&...tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}
template <class Cond> struct chain {
  Cond cond;
  chain(Cond cond) : cond(cond) {}
  template <class T> bool operator()(T &a, const T &b) const {
    if (cond(a, b)) {
      a = b;
      return true;
    }
    return false;
  }
};
template <class Cond> chain<Cond> make_chain(Cond cond) {
  return chain<Cond>(cond);
}

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<pair<i64, i64>> vec;
  i64 cnt = 0;
  rep(i, 0, N) {
    i64 a = 0;
    cin >> a;
    if (a >= 0) {
      vec.push_back({a, 0});
      cnt++;
    } else {
      vec.push_back({-a, 1});
    }
  }
  const i64 MOD = 1e9 + 7;
  if (cnt == 0) {
    sort(all(vec));
    if (K % 2 == 0) {
      reverse(all(vec));
    }
    i64 ans = 1;
    rep(i, 0, K) { ans = (ans * ((-vec[i].first + MOD) % MOD)) % MOD; }
    cout << ans << endl;
    return 0;
  }
  sort(rbegin(vec), rend(vec));
  vector<vector<i64>> take(2);
  vector<i64> last(2, -1);
  rep(i, 0, K) { take[vec[i].second].push_back(vec[i].first); }
  rep(i, K, N) {
    if (last[vec[i].second] == -1) {
      last[vec[i].second] = vec[i].first;
    }
  }
  if (K != N && take[1].size() % 2 == 1) {
    i64 select = 0;
    if (take[1].size() == 0) {
      select = 1;
    } else if (take[0].size() == 0) {
      select = 0;
    } else if (last[0] == -1) {
      select = 1;
    } else if (last[1] == -1) {
      select = 0;
    } else {
      if (last[1] * take[1].back() >= last[0] * take[0].back()) {
        select = 1;
      } else {
        select = 0;
      }
    }
    take[select].push_back(last[select]);
    take[1 - select].pop_back();
  }
  i64 ans = 1;
  for (auto x : take[0]) {
    ans = (ans * x) % MOD;
  }
  for (auto x : take[1]) {
    ans = (ans * x) % MOD;
  }
  if (take[1].size() % 2 == 1) {
    ans = (-ans + MOD) % MOD;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x)                                                               \\
  { std::cout << PREFIX << #x << " = " << x << std::endl; }

std::ostream &output_indent(std::ostream &os, int ind) {
  for (int i = 0; i < ind; i++)
    os << " ";
  return os;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p);
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v);

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << ", ";
  return (os << "]");
}

template <class T> static inline std::vector<T> ndvec(size_t &&n, T val) {
  return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail> static inline auto ndvec(size_t &&n, Tail &&...tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}
template <class Cond> struct chain {
  Cond cond;
  chain(Cond cond) : cond(cond) {}
  template <class T> bool operator()(T &a, const T &b) const {
    if (cond(a, b)) {
      a = b;
      return true;
    }
    return false;
  }
};
template <class Cond> chain<Cond> make_chain(Cond cond) {
  return chain<Cond>(cond);
}

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<pair<i64, i64>> vec;
  i64 cnt = 0;
  rep(i, 0, N) {
    i64 a = 0;
    cin >> a;
    if (a >= 0) {
      vec.push_back({a, 0});
      cnt++;
    } else {
      vec.push_back({-a, 1});
    }
  }
  const i64 MOD = 1e9 + 7;
  if (cnt == 0) {
    sort(all(vec));
    i64 ans = 1;
    rep(i, 0, K) { ans = (ans * ((-vec[i].first + MOD) % MOD)) % MOD; }
    cout << ans << endl;
    return 0;
  }
  sort(rbegin(vec), rend(vec));
  vector<vector<i64>> take(2);
  vector<i64> last(2, -1);
  rep(i, 0, K) { take[vec[i].second].push_back(vec[i].first); }
  rep(i, K, N) {
    if (last[vec[i].second] == -1) {
      last[vec[i].second] = vec[i].first;
    }
  }
  if (take[1].size() % 2 == 1) {
    i64 select = 0;
    if (last[0] == -1) {
      select = 1;
    } else if (last[1] == -1) {
      select = 0;
    } else {
      if (last[1] * take[1].back() >= last[0] * take[0].back()) {
        select = 1;
      } else {
        select = 0;
      }
    }
    take[select].push_back(last[select]);
    take[1 - select].pop_back();
  }
  i64 ans = 1;
  for (auto x : take[0]) {
    ans = (ans * x) % MOD;
  }
  for (auto x : take[1]) {
    ans = (ans * x) % MOD;
  }
  cout << ans << endl;
}