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

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

using ll = long long;

class mod_ll {
public:
  ll m_v;
  static const ll m_m = 1000000007; // prime value ex. 1000000009
  mod_ll(ll v = 0) : m_v(v % m_m) {}
  mod_ll operator=(ll rhs) {
    m_v = rhs % m_m;
    return *this;
  }
  ll operator()() { return (m_v + m_m) % m_m; }
  mod_ll operator=(const mod_ll rhs) {
    m_v = rhs.m_v;
    return *this;
  }
  mod_ll operator+=(const mod_ll rhs) {
    m_v = (m_v + rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator+(const mod_ll rhs) {
    auto ret = *this;
    return ret += rhs;
  }
  mod_ll operator-=(const mod_ll rhs) {
    m_v = (m_v - rhs.m_v + m_m) % m_m;
    return *this;
  }
  mod_ll operator-(const mod_ll rhs) {
    auto ret = *this;
    return ret -= rhs;
  }
  mod_ll operator*=(const mod_ll rhs) {
    m_v = (m_v * rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator*(const mod_ll rhs) {
    mod_ll ret = *this;
    return ret *= rhs;
  }
  mod_ll operator^(ll rhs) {
    mod_ll t = *this;
    mod_ll ret(1LL);

    for (; rhs > 0; rhs >>= 1) {
      if (rhs & 1)
        ret *= t;
      t *= t;
    }
    return ret;
  }
  mod_ll operator/=(mod_ll rhs) {
    mod_ll t = rhs ^ (m_m - 2);
    return *this *= t;
  }
  mod_ll operator/(const mod_ll rhs) {
    mod_ll ret = *this;
    return ret /= rhs;
  }
};

mod_ll mod_fact(int n) {
  static vector<mod_ll> memo(1);

  if ((int)memo.size() <= n) {
    if (memo.size() == 1) {
      memo[0] = 1;
    }
    for (int i = memo.size(); i <= n; i++) {
      memo.push_back(memo.back() * i);
    }
  }
  return memo[n];
}

mod_ll mod_nCr(int n, int r) {
  if (n <= 0)
    return 1;
  mod_ll bunsi = mod_fact(n);
  mod_ll bunbo = mod_fact(n - r) * mod_fact(r);
  return bunsi / bunbo;
}

mod_ll mod_nCr2(int n, int r) {
  mod_ll bunsi(1);
  mod_ll bunbo(1);
  if (r * 2 > n)
    r = n - r;

  for (int i = 0; i < r; i++) {
    bunsi *= (n - i);
    bunbo *= (i + 1);
  }

  return bunsi / bunbo;
}

mod_ll mod_nPr(int n, int r) { return mod_fact(n) / mod_fact(n - r); }

bool cmp(const int a, const int b) { return abs(a) > abs(b); }

int sign(int v) {
  if (v > 0)
    return 1;
  if (v < 0)
    return -1;
  return 0;
}

struct solve {
  solve() {}
  ll operator()(vll &a, int k) {
    int n = a.size();
    mod_ll ret(1);
    vll ap, an;

    for (int i = 0; i < n; i++) {
      if (a[i] >= 0)
        ap.push_back(a[i]);
      else
        an.push_back(-a[i]);
    }
    sort(ap.rbegin(), ap.rend());
    sort(an.rbegin(), an.rend());

    if (ap.size() == 0 && (k & 1) == 1) {
      for (int i = 0; i < k; i++) {
        ret *= an[n - 1 - i];
      }
      return ret.m_m - ret.m_v;
    }
    if (n == k) {
      for (auto &m : ap)
        ret *= m;
      for (auto &m : an)
        ret *= m;
      if (an.size() & 1)
        return ret.m_m - ret.m_v;
      return ret();
    }

    int ip = 0;
    int in = 0;
    for (; k > 1;) {
      if (ip + 1 >= (int)ap.size())
        break;
      if (in + 1 >= (int)an.size())
        break;
      if (an[in] * an[in + 1] >= ap[ip] * ap[ip + 1]) {
        ret *= an[in] * an[in + 1];
        in += 2;
        k -= 2;
      } else {
        ret *= ap[ip++];
        k--;
      }
    }
    if (k & 1) {
      ret *= ap[ip++];
      k--;
    }

    if (in + 1 < (int)an.size()) {
      for (; k > 0; k--) {
        // cout << "k=" << k << " an[" << in << "]=" << an[in] << "\\n";
        ret *= an[in++];
      }
    }

    if (ip < (int)ap.size()) {
      for (; k > 0; k--) {
        // cout << "k=" << k << " ap[" << ip << "]=" << ap[ip] << "\\n";
        ret *= ap[ip++];
      }
    }

    return ret();
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n, k;
    cin >> n >> k;
    if (cin.fail())
      break;
    vll a(n);
    for (auto &m : a)
      cin >> m;

    cout << solve()(a, k) << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

using ll = long long;

class mod_ll {
public:
  ll m_v;
  static const ll m_m = 1000000007; // prime value ex. 1000000009
  mod_ll(ll v = 0) : m_v(((v % m_m) + m_m) % m_m) {}
  mod_ll operator=(ll rhs) {
    m_v = ((rhs % m_m) + m_m) % m_m;
    return *this;
  }
  ll operator()() { return (m_v + m_m) % m_m; }
  mod_ll operator=(mod_ll rhs) {
    m_v = rhs.m_v;
    return *this;
  }
  mod_ll operator+=(mod_ll rhs) {
    m_v = (m_v + rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator+(mod_ll rhs) {
    auto ret = *this;
    return ret += rhs;
  }
  mod_ll operator-=(mod_ll rhs) {
    m_v = (m_v - rhs.m_v + m_m) % m_m;
    return *this;
  }
  mod_ll operator-(mod_ll rhs) {
    auto ret = *this;
    return ret -= rhs;
  }
  mod_ll operator*=(mod_ll rhs) {
    m_v = (m_v * rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator*(mod_ll rhs) {
    mod_ll ret = *this;
    return ret *= rhs;
  }
  mod_ll operator^(ll rhs) {
    mod_ll t = *this;
    mod_ll ret(1LL);

    for (; rhs > 0; rhs >>= 1) {
      if (rhs & 1)
        ret *= t;
      t *= t;
    }
    return ret;
  }
  mod_ll operator/=(mod_ll rhs) { return *this *= (rhs ^ (m_m - 2)); }
  mod_ll operator/(mod_ll rhs) {
    mod_ll ret = *this;
    return ret /= rhs;
  }
};

mod_ll mod_fact(int n) {
  static vector<mod_ll> memo(1);

  if ((int)memo.size() <= n) {
    if (memo.size() == 1) {
      memo[0] = 1;
    }
    for (int i = memo.size(); i <= n; i++) {
      memo.push_back(memo.back() * i);
    }
  }
  return memo[n];
}

mod_ll mod_nCr(int n, int r) {
  if (n <= 0)
    return 1;
  mod_ll bunsi = mod_fact(n);
  mod_ll bunbo = mod_fact(n - r) * mod_fact(r);
  return bunsi / bunbo;
}

mod_ll mod_nCr2(int n, int r) {
  mod_ll bunsi(1);
  mod_ll bunbo(1);
  if (r * 2 > n)
    r = n - r;

  for (int i = 0; i < r; i++) {
    bunsi *= (n - i);
    bunbo *= (i + 1);
  }

  return bunsi / bunbo;
}

mod_ll mod_nPr(int n, int r) { return mod_fact(n) / mod_fact(n - r); }

bool cmp(const int a, const int b) { return abs(a) > abs(b); }

int sign(int v) {
  if (v > 0)
    return 1;
  if (v < 0)
    return -1;
  return 0;
}

struct solve {
  solve() {}
  ll operator()(vll &a, int k) {
    int n = a.size();
    mod_ll ret(1);
    vll ap, an;

    for (int i = 0; i < n; i++) {
      if (a[i] >= 0)
        ap.push_back(a[i]);
      else
        an.push_back(a[i]);
    }
    sort(ap.rbegin(), ap.rend());
    sort(an.begin(), an.end());

    if (ap.size() == 0 && (k & 1) == 1) {
      for (int i = 0; i < k; i++) {
        ret *= an[n - 1 - i];
      }
      return ret();
    }

    int ip = 0;
    int in = 0;
    for (; k > 1;) {
      if (ip + 1 >= (int)ap.size())
        break;
      if (in + 1 >= (int)an.size())
        break;
      if (an[in] * an[in + 1] >= ap[ip] * ap[ip + 1]) {
        ret *= an[in] * an[in + 1];
        in += 2;
        k -= 2;
      } else {
        ret *= ap[ip++];
        ip++;
        k--;
      }
    }
    if (k & 1) {
      ret *= ap[ip++];
      k--;
    }

    if (ip < (int)ap.size()) {
      for (; k > 0; k--) {
        ret *= ap[ip++];
      }
    }

    if (in < (int)an.size()) {
      for (; k > 0; k--) {
        ret *= an[in++];
      }
    }

    return ret();
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n, k;
    cin >> n >> k;
    if (cin.fail())
      break;
    vll a(n);
    for (auto &m : a)
      cin >> m;

    cout << solve()(a, k) << "\\n";
  }
  return 0;
}