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

#pragma GCC optimize("Ofast")

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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pop_b pop_back
#define pf push_front
#define pop_f pop_front
#define mt make_tuple
#define eb emplace_back
#define sf(x) sizeof(x)
#define len(x) x.length()
#define sz(x) x.size()
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define LB(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define UB(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<set<ll>> vset;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef deque<ll> dql;
typedef deque<int> dqi;
typedef unsigned long long ull;
typedef tuple<int, int, int> State;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
template <class T, class U> inline void checkmin(T &x, U y) {
  if (y < x)
    x = y;
}

template <class T, class U> inline void checkmax(T &x, U y) {
  if (y > x)
    x = y;
}

template <class T, class U> inline bool ifmax(T &x, U y) {
  if (y > x)
    return x = y, true;
  return false;
}

template <class T, class U> inline bool ifmin(T &x, U y) {
  if (y < x)
    return x = y, true;
  return false;
}

#define vmax(type) std::numeric_limits<type>::max()
#define vmin(type) std::numeric_limits<type>::min()
#define each(a, x) for (auto &a : (x))
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (ll i = (l); i < (r); i++)
#define rep2r(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define range(a) a.begin(), a.end()
#define deb(x) cout << #x << " " << x << endl;
#define cini(i)                                                                \\
  int i;                                                                       \\
  cin >> i;
#define cinll(l)                                                               \\
  ll l;                                                                        \\
  cin >> l;
#define cinll2(l, z)                                                           \\
  ll l, z;                                                                     \\
  cin >> l >> z;
#define cinll3(l, z, x)                                                        \\
  ll l, z, x;                                                                  \\
  cin >> l >> z >> x;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define cinal(a, n)                                                            \\
  vl a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define coutai(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define coutal(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define ent cout << endl;
#define resize_vec(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define ms(dp, x) memset(dp, x, sf(dp))

// const long long MAX_SIZE = 1000000;
// vector<long long >isprime(MAX_SIZE , true);
// vector<long long >prime;
// vector<long long >SPF(MAX_SIZE);

// // function generate all prime number less then N in O(n)
// void manipulated_seive(ll N)
// {
//     isprime[0] = isprime[1] = false ;
//     for (ll i=2; i<N ; i++)
//     {
//         if (isprime[i])
//         {
//             prime.push_back(i);
//             SPF[i] = i;
//         }
//         for (ll j=0;j < prime.size() && i*prime[j] < N && prime[j] <=
//         SPF[i];j++)
//         {
//             isprime[i*prime[j]]=false;
//             SPF[i*prime[j]] = prime[j] ;
//         }
//     }
// }
ll fstpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll powm(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

void fukof() {}

void problem() {}

const ll mod = 1e9 + 7;
const ll N = 1000005;
const ll inf = 1e9;

void solve() {
  int n, k;
  cin >> n >> k;
  cinal(a, n);
  vl mi, pl;
  rep(i, n) if (a[i] >= 0) pl.pb(a[i]);
  else mi.pb(-a[i]);
  sort(range(pl));
  sort(range(mi));
  ll ans = 1;
  if (pl.size() == 0) {
    // error(n);
    if (k % 2)
      rep(i, k) ans *= (-1) * mi[i], ans %= mod, ans += mod, ans %= mod;
    else
      rep2(i, n - k, n) ans *= (-1) * mi[i], ans %= mod, ans += mod, ans %= mod;
    cout << ans << endl;
    return;
  }
  if (mi.size() == 0) {
    rep2(i, n - k, n) ans *= pl[i], ans %= mod, ans += mod, ans %= mod;
    cout << ans << endl;
    return;
  }
  if (k == 1) {
    sort(range(a));
    cout << a[n - 1] << endl;
    return;
  }
  if (k == n) {
    rep(i, n) ans *= a[i], ans %= mod, ans += mod, ans %= mod;
    cout << ans << endl;
    return;
  }
  dql mm, pp;
  repr(i, pl.size()) pp.pb(pl[i]);
  repr(i, mi.size()) mm.pb(mi[i]);
  rep(i, k / 2) {
    pp.pb(0ll);
    pp.pb(0ll);
    mm.pb(0ll);
    mm.pb(0ll);
  }
  if (k % 2) {
    ans *= pp.front();
    pp.pop_f();
    k--;
  }
  vl h1, h2;
  while (pp.size() >= 2) {
    ll curp = 1;
    curp *= pp.front();
    pp.pop_f();
    // curp %= mod;
    curp *= pp.front();
    pp.pop_f();
    // curp %= mod;
    h1.pb(curp);
  }
  while (mm.size() >= 2) {
    ll curm = 1;
    curm *= mm.front();
    mm.pop_f();
    // curm %= mod;
    curm *= mm.front();
    mm.pop_f();
    // curm %= mod;
    h2.pb(curm);
  }
  k /= 2;
  int ptr1 = 0, ptr2 = 0;
  while (k--) {
    if (h1[ptr1] > h2[ptr2]) {
      h1[ptr1] %= mod;
      ans *= h1[ptr1];
      ans %= mod;
      ptr1++;
    } else {
      h2[ptr2] %= mod;
      ans *= h2[ptr2];
      ans %= mod;
      ptr2++;
    }
  }
  cout << ans << endl;
}

// #define _DEBUG
// #define testCases
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  int tc = 1;
#ifdef testCases
  cin >> tc;
#endif
  while (tc--) {
#ifdef _DEBUG
    int tt = clock();
#endif
    problem();
    fukof();
    solve();

#ifdef _DEBUG
    cerr << "TIME = " << clock() - tt << endl;
    tt = clock();
#endif
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")

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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pop_b pop_back
#define pf push_front
#define pop_f pop_front
#define mt make_tuple
#define eb emplace_back
#define sf(x) sizeof(x)
#define len(x) x.length()
#define sz(x) x.size()
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define LB(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define UB(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<set<ll>> vset;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef deque<ll> dql;
typedef deque<int> dqi;
typedef unsigned long long ull;
typedef tuple<int, int, int> State;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
template <class T, class U> inline void checkmin(T &x, U y) {
  if (y < x)
    x = y;
}

template <class T, class U> inline void checkmax(T &x, U y) {
  if (y > x)
    x = y;
}

template <class T, class U> inline bool ifmax(T &x, U y) {
  if (y > x)
    return x = y, true;
  return false;
}

template <class T, class U> inline bool ifmin(T &x, U y) {
  if (y < x)
    return x = y, true;
  return false;
}

#define vmax(type) std::numeric_limits<type>::max()
#define vmin(type) std::numeric_limits<type>::min()
#define each(a, x) for (auto &a : (x))
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (ll i = (l); i < (r); i++)
#define rep2r(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define range(a) a.begin(), a.end()
#define deb(x) cout << #x << " " << x << endl;
#define cini(i)                                                                \\
  int i;                                                                       \\
  cin >> i;
#define cinll(l)                                                               \\
  ll l;                                                                        \\
  cin >> l;
#define cinll2(l, z)                                                           \\
  ll l, z;                                                                     \\
  cin >> l >> z;
#define cinll3(l, z, x)                                                        \\
  ll l, z, x;                                                                  \\
  cin >> l >> z >> x;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define cinal(a, n)                                                            \\
  vl a(n);                                                                     \\
  rep(i, n) { cin >> a[i]; }
#define coutai(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define coutal(a, n)                                                           \\
  rep(i, n) { cout << a[i] << " "; }                                           \\
  ent;
#define ent cout << endl;
#define resize_vec(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define ms(dp, x) memset(dp, x, sf(dp))

// const long long MAX_SIZE = 1000000;
// vector<long long >isprime(MAX_SIZE , true);
// vector<long long >prime;
// vector<long long >SPF(MAX_SIZE);

// // function generate all prime number less then N in O(n)
// void manipulated_seive(ll N)
// {
//     isprime[0] = isprime[1] = false ;
//     for (ll i=2; i<N ; i++)
//     {
//         if (isprime[i])
//         {
//             prime.push_back(i);
//             SPF[i] = i;
//         }
//         for (ll j=0;j < prime.size() && i*prime[j] < N && prime[j] <=
//         SPF[i];j++)
//         {
//             isprime[i*prime[j]]=false;
//             SPF[i*prime[j]] = prime[j] ;
//         }
//     }
// }
ll fstpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll powm(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

void fukof() {}

void problem() {}

const ll mod = 1e9 + 7;
const ll N = 1000005;
const ll inf = 1e9;

void solve() {
  int n, k;
  cin >> n >> k;
  cinal(a, n);
  vl mi, pl;
  rep(i, n) if (a[i] >= 0) pl.pb(a[i]);
  else mi.pb(-a[i]);
  sort(range(pl));
  sort(range(mi));
  ll ans = 1;
  if (pl.size() == 0) {
    // error(n);
    rep(i, k) ans *= (-1) * mi[i], ans %= mod, ans += mod, ans %= mod;
    cout << ans << endl;
    return;
  }
  if (mi.size() == 0) {
    rep2(i, n - k, n) ans *= pl[i], ans %= mod, ans += mod, ans %= mod;
    cout << ans << endl;
    return;
  }
  if (k == 1) {
    sort(range(a));
    cout << a[n - 1] << endl;
    return;
  }
  if (k == n) {
    rep(i, n) ans *= a[i], ans %= mod, ans += mod, ans %= mod;
    cout << ans << endl;
    return;
  }
  dql mm, pp;
  repr(i, pl.size()) pp.pb(pl[i]);
  repr(i, mi.size()) mm.pb(mi[i]);
  pp.pb(0ll);
  pp.pb(0ll);
  mm.pb(0ll);
  mm.pb(0ll);
  if (k % 2) {
    ans *= pp.front();
    pp.pop_f();
    k--;
  }
  vl h1, h2;
  while (pp.size() >= 2) {
    ll curp = 1;
    curp *= pp.front();
    pp.pop_f();
    // curp %= mod;
    curp *= pp.front();
    pp.pop_f();
    // curp %= mod;
    h1.pb(curp);
  }
  while (mm.size() >= 2) {
    ll curm = 1;
    curm *= mm.front();
    mm.pop_f();
    // curm %= mod;
    curm *= mm.front();
    mm.pop_f();
    // curm %= mod;
    h2.pb(curm);
  }
  k /= 2;
  int ptr1 = 0, ptr2 = 0;
  while (k--) {
    if (h1[ptr1] > h2[ptr2]) {
      h1[ptr1] %= mod;
      ans *= h1[ptr1];
      ans %= mod;
      ptr1++;
    } else {
      h2[ptr2] %= mod;
      ans *= h2[ptr2];
      ans %= mod;
      ptr2++;
    }
  }
  cout << ans << endl;
}

// #define _DEBUG
// #define testCases
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  int tc = 1;
#ifdef testCases
  cin >> tc;
#endif
  while (tc--) {
#ifdef _DEBUG
    int tt = clock();
#endif
    problem();
    fukof();
    solve();

#ifdef _DEBUG
    cerr << "TIME = " << clock() - tt << endl;
    tt = clock();
#endif
  }
  return 0;
}