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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> s, t;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0)
      t.push_back(a[i]);
    else
      s.push_back(a[i]);
  }

  int S = s.size();
  int T = t.size();
  bool ok = false;
  if (S > 0) {
    if (n == k)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (k % 2 == 0);
  }

  mint ans = 1;
  if (!ok) {
    sort(a.begin(), a.end(), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, k) ans *= a[i];
  } else {
    sort(s.begin(), s.end());
    sort(t.rbegin(), t.rend());
    if (k % 2 == 1) {
      ans *= s.back();
      s.pop_back();
    }
    vector<ll> p;
    while (s.size() >= 2) {
      ll x = s.back();
      s.pop_back();
      x *= s.back();
      s.pop_back();
      p.push_back(x);
    }
    while (t.size() >= 2) {
      ll x = t.back();
      t.pop_back();
      x *= t.back();
      t.pop_back();
      p.push_back(x);
    }
    sort(p.rbegin(), p.rend());
    rep(i, k / 2) ans *= p[i];
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll ans = 1;

  int pos = 0, neg = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      neg++;
    else if (a[i] > 0)
      pos++;
  }

  sort(a.begin(), a.end());
  int neg_min_index = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0) {
      neg_min_index = i - 1;
      break;
    }
  }
  int index;
  int count;
  int l = 0, r = n - 1;

  if (pos > 0) {
    if (n == k) {
      if (!neg % 2) {
        if (k % 2) {
          ans *= a[r--];
          ans %= mod;
        }
        for (int i = 0; i < k / 2; i++) {
          if (a[l] * a[l + 1] <= a[r] * a[r - 1]) {
            ans *= a[r];
            ans %= mod;
            ans *= a[r - 1];
            ans %= mod;
            r -= 2;
            continue;
          } else if (a[l] * a[l + 1] > a[r] * a[r - 1]) {
            ans *= a[l];
            ans %= mod;
            ans *= a[l + 1];
            ans %= mod;
            l += 2;
            continue;
          }
        }

      } else {
        index = n - 1;
        count = 0;
        while (count < k) {
          if (a[index] != 0) {
            ans *= a[index];
            count++;
            ans %= mod;
          }
          index--;
        }
      }
    } else {
      if (k % 2) {
        ans *= a[r--];
        ans %= mod;
      }
      for (int i = 0; i < k / 2; i++) {
        if (a[l] * a[l + 1] <= a[r] * a[r - 1]) {
          ans *= a[r];
          ans %= mod;
          ans *= a[r - 1];
          ans %= mod;
          r -= 2;
          continue;
        } else if (a[l] * a[l + 1] > a[r] * a[r - 1]) {
          ans *= a[l];
          ans %= mod;
          ans *= a[l + 1];
          ans %= mod;
          l += 2;
          continue;
        }
      }
    }
  } else if (pos == 0) {
    if (!k % 2) {
      if (k % 2) {
        ans *= a[r--];
        ans %= mod;
      }
      for (int i = 0; i < k / 2; i++) {
        if (a[l] * a[l + 1] <= a[r] * a[r - 1]) {
          ans *= a[r];
          ans %= mod;
          ans *= a[r - 1];
          ans %= mod;
          r -= 2;
          continue;
        } else if (a[l] * a[l + 1] > a[r] * a[r - 1]) {
          ans *= a[l];
          ans %= mod;
          ans *= a[l + 1];
          ans %= mod;
          l += 2;
          continue;
        }
      }

    } else {
      index = n - 1;
      count = 0;
      while (count < k) {
        if (a[index] != 0) {
          ans *= a[index];
          ans %= mod;
          count++;
        }
        index--;
      }
    }
  }

  if (ans < 0)
    ans += mod;
  cout << ans % mod << endl;
  return 0;
}