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

#include <bits/stdc++.h>
using namespace std;
#pragma region
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
#define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

const int MOD = 1e9 + 7;
int power(int a, int n) {
  int res = 1;
  int now = a;
  while (n) {
    if (n & 1) {
      res = (res * now) % MOD;
    }
    n >>= 1;
    now = (now * now) % MOD;
  }
  return res;
}
int inv(int a) { return power(a, MOD - 2); }
int n, k;
vector<int> a;
vector<int> ps, ng;
void solve() {
  cin >> n >> k;
  a.resize(n);
  forn(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      ps.pb(a[i]);
    else
      ng.pb(-a[i]);
  }
  if (n == k || (sz(ps) == 0 && k % 2)) {
    sort(a.begin(), a.end(), [](int x, int y) { return abs(x) < abs(y); });
    int ans = 1;
    forn(i, k) ans = (ans * a[i] % MOD + MOD) % MOD;
    cout << ans << '\\n';
    return;
  }
  sort(ps.begin(), ps.end());
  sort(ng.begin(), ng.end());
  vector<int> v;
  int ans = 1;
  if (k % 2) {
    ans = (ans * ps.back()) % MOD;
    ps.pop_back();
  }
  while (sz(ps) > 1) {
    int x = ps.back();
    ps.pop_back();
    x *= ps.back();
    ps.pop_back();
    v.push_back(x);
  }
  while (sz(ng) > 1) {
    int x = ng.back();
    ng.pop_back();
    x *= ng.back();
    ng.pop_back();
    v.push_back(x);
  }
  sort(v.rbegin(), v.rend());
  forn(i, k / 2) ans = (ans * (v[i] % MOD)) % MOD;
  cout << ans % MOD << '\\n';
}

signed main() {
  int T = 1;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cin >> T;
  for (int i = 1; i <= T; ++i) {
    // cout << "Case #" << i << ": ";
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma region
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
#define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

const int MOD = 1e9 + 7;
int power(int a, int n) {
  int res = 1;
  int now = a;
  while (n) {
    if (n & 1) {
      res = (res * now) % MOD;
    }
    n >>= 1;
    now = (now * now) % MOD;
  }
  return res;
}
int inv(int a) { return power(a, MOD - 2); }
int n, k;
vector<int> a;
vector<int> ps, ng;
void solve() {
  cin >> n >> k;
  a.resize(n);
  forn(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      ps.pb(a[i]);
    else
      ng.pb(-a[i]);
  }
  if (n == k || sz(ps) == 0 && k % 2 == 0) {
    int ans = 1;
    forn(i, k) ans = (ans * a[i] % MOD + MOD) % MOD;
    cout << ans << '\\n';
    return;
  }
  sort(ps.begin(), ps.end());
  sort(ng.begin(), ng.end());
  vector<int> v;
  int ans = 1;
  if (k % 2) {
    ans = (ans * ps.back()) % MOD;
    ps.pop_back();
  }
  while (sz(ps) > 1) {
    int x = ps.back();
    ps.pop_back();
    x *= ps.back();
    ps.pop_back();
    v.push_back(x);
  }
  while (sz(ng) > 1) {
    int x = ng.back();
    ng.pop_back();
    x *= ng.back();
    ng.pop_back();
    v.push_back(x);
  }
  sort(v.rbegin(), v.rend());
  forn(i, k / 2) ans = (ans * v[i]) % MOD;
  cout << ans << '\\n';
}

signed main() {
  int T = 1;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cin >> T;
  for (int i = 1; i <= T; ++i) {
    // cout << "Case #" << i << ": ";
    solve();
  }
  return 0;
}