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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000000000000ll
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\\n";
/* clang-format on */

ll mul(ll a, ll b) {
  ll tmp = (b + MOD) % MOD;
  ll r = (a * tmp) % MOD;
  assert(r >= 0);
  return r;
}

int main() {
  ll n, k, ans = 1;
  cin >> n >> k;
  if (n == k) {
    REP(i, n) {
      ll t;
      cin >> t;
      ans = mul(ans, t);
    }
    cout << ans << "\\n";
    return 0;
  }
  vector<ll> b(n);
  priority_queue<ll> p;
  priority_queue<ll, vector<ll>, greater<ll>> m;
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    if (tmp >= 0)
      p.push(tmp);
    else
      m.push(tmp);
    b[i] = tmp;
  }

  sort(ALL(b));
  if (k % 2 == 1 && m.size() == n) {
    // どうやっても負になる
    reverse(ALL(b));
    REP(i, k) { ans = mul(ans, b[i]); }
    cout << ans << "\\n";
    return 0;
  } else if (m.size() == n) {
    // 全部負
    REP(i, k) { ans = mul(ans, abs(b[i])); }
    cout << ans << "\\n";
    return 0;
  } else if (m.size() <= 1) {
    // 全部正か一つだけ負
    reverse(ALL(b));
    REP(i, k) { ans = mul(ans, b[i]); }
    cout << ans << "\\n";
    return 0;
  } else {
    ll cnt = 0, st_p = 1, st_m = 1, c = 0;
    bool fp = false, fm = false;
    while ((p.size() > 0 || m.size() > 0) && cnt < k) {
      ll f = 0;
      if (m.size() == 0) {
        f = 1;
      } else if (p.size() == 0) {
        f = -1;
      } else if (p.top() > -m.top()) {
        f = 1;
      } else if (p.top() < -m.top()) {
        f = -1;
      } else if (p.top() == -m.top()) {
        f = 1;
      }

      if (f == 1) {
        fp = true;
        ans = mul(ans, st_p);
        st_p = p.top();
        p.pop();
        cnt++;
      }
      if (f == -1) {
        fm = true;
        ans = mul(ans, st_m);
        st_m = m.top();
        m.pop();
        c++;
        cnt++;
      }
      assert(f != 0);
    }

    if (c % 2 == 1 && p.empty()) {
      ans = mul(mul(ans, st_m), m.top());
    } else if (c % 2 == 1 && m.empty()) {
      ans = mul(mul(ans, p.top()), st_p);
    } else if (c % 2 == 1) {
      if (!fm)
        ans = mul(ans, st_p);
      else if (!fp)
        ans = mul(ans, p.top());
      else if (st_m * m.top() > p.top() * st_p)
        ans = mul(mul(ans, st_m), m.top());
      else
        ans = mul(mul(ans, p.top()), st_p);
    } else {
      ans = mul(ans, st_m);
      ans = mul(ans, st_p);
    }

    cout << ans << "\\n";
    return 0;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000000000000ll
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\\n";
/* clang-format on */

ll mul(ll a, ll b) {
  ll tmp = (b + MOD) % MOD;
  ll r = (a * tmp) % MOD;
  assert(r > 0);
  return r;
}

int main() {
  ll n, k, ans = 1;
  cin >> n >> k;
  if (n == k) {
    REP(i, n) {
      ll t;
      cin >> t;
      ans = mul(ans, t);
    }
    cout << ans << "\\n";
    return 0;
  }
  vector<ll> b(n);
  priority_queue<ll> p;
  priority_queue<ll, vector<ll>, greater<ll>> m;
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    if (tmp >= 0)
      p.push(tmp);
    else
      m.push(tmp);
    b[i] = tmp;
  }

  sort(ALL(b));
  if (k % 2 == 1 && m.size() == n) {
    // どうやっても負になる
    reverse(ALL(b));
    REP(i, k) { ans = mul(ans, b[i]); }
    cout << ans << "\\n";
    return 0;
  } else if (m.size() == n) {
    REP(i, k) { ans = mul(ans, abs(b[i])); }
    cout << ans << "\\n";
    return 0;
  } else if (m.size() == 0) {
    reverse(ALL(b));
    REP(i, k) { ans = mul(ans, b[i]); }
    cout << ans << "\\n";
    return 0;
  } else {
    ll cnt = 0;
    /*
   非負なものがあるはず
   while (p.size() > 2 && m.size() > 2) {
     ll p1, p2, m1, m2;
     p1 = p.top();
     p.pop();
     p2 = p.top();
     p.pop();
     m1 = m.top();
     m.pop();
     m2 = m.top();
     m.pop();
     if (p1 * p2 > m1 * m2) {
       ans = mul(ans, (p1 * p2) % MOD);
       p.push(m1);
       p.push(m2);
     } else {
       ans = mul(ans, (m1 * m2) % MOD);
       m.push(p1);
       m.push(p2);
     }
     cnt += 2;
     if (cnt == k) {
       cout << ans << "\\n";
       return 0;
     }
     if (cnt + 1 == k) {
       cout << mul(ans, p.top()) << "\\n";
       return 0;
     }
   }

   if (m.size() <= 1) {
     REP(i, k - cnt) {
       ans = mul(ans, p.top());
       p.pop();
     }
     cout << ans << "\\n";
     return 0;
   }
*/
    ll st_p = 1, st_m = 1, c = 0;
    while ((p.size() > 0 || m.size() > 0) && cnt < k) {
      if (m.size() == 0) {
        ans = mul(ans, st_p);
        st_p = p.top();
        p.pop();
        cnt++;
      } else if (p.size() == 0) {
        ans = mul(ans, st_m);
        st_m = m.top();
        m.pop();
        c++;
        cnt++;
      } else if (p.top() > -m.top()) {
        ans = mul(ans, st_p);
        st_p = p.top();
        p.pop();
        cnt++;
      } else {
        ans = mul(ans, st_m);
        st_m = m.top();
        m.pop();
        c++;
        cnt++;
      }
    }

    if (c % 2 == 1 && p.size() == 0) {
      ans = mul(ans, st_m);
      ans = mul(ans, m.top());
    } else if (c % 2 == 1) {
      ans = mul(ans, p.top());
      ans = mul(ans, st_p);
    } else {
      ans = mul(ans, st_m);
      ans = mul(ans, st_p);
    }

    cout << ans << "\\n";
    return 0;
  }

  return 0;
}