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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  // n==k
  if (n == k) {
    ll result = 1;
    for (auto c : a) {
      c = (c + MOD) % MOD;
      result *= c;
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  vector<ll> ma, pa;
  for (auto c : a) {
    if (c < 0)
      ma.push_back(-c);
    else
      pa.push_back(c);
  }

  sort(pa.begin(), pa.end());
  sort(ma.begin(), ma.end());

  // -
  if (pa.size() == 0 && k % 2 == 1) {
    ll result = 1;
    rep(i, k) { result = result * ma[i] % MOD; }
    result = (MOD - result) % MOD;
    cout << result << endl;

    return 0;
  }

  // else
  vector<ll> results;
  if (k % 2 == 1) {
    results.push_back(pa.back());
    pa.pop_back();
  }

  while (results.size() - k) {
    ll pmax = 0;
    if (pa.size() >= 2)
      pmax = pa[pa.size() - 1] * pa[pa.size() - 2];
    ll mmax = 0;
    if (ma.size() >= 2)
      mmax = ma[ma.size() - 1] * ma[ma.size() - 2];

    if (pmax >= mmax && pa.size() >= 2) {
      results.push_back(pa.back());
      pa.pop_back();
      results.push_back(pa.back());
      pa.pop_back();
    } else if (mmax >= pmax && ma.size() >= 2) {
      results.push_back(ma.back());
      ma.pop_back();
      results.push_back(ma.back());
      ma.pop_back();
    }
  }

  //  sort(results.begin(), results.end());
  //  cout<<results.size()<<endl;
  //  cout<<results[0]<<' '<<results.back()<<endl;

  ll result = 1;
  for (auto c : results) {
    result = (result * c) % MOD;
  }
  cout << result << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  // n==k
  if (n == k) {
    ll result = 1;
    for (auto c : a) {
      c = (c + MOD) % MOD;
      result *= c;
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  vector<ll> ma, pa;
  for (auto c : a) {
    if (c < 0)
      ma.push_back(-c);
    else
      pa.push_back(c);
  }

  sort(pa.begin(), pa.end());
  sort(ma.begin(), ma.end());

  // -
  if (pa.size() == 0 && k % 2 == 1) {
    ll result = 1;
    rep(i, k) { result = result * a[i] % MOD; }
    result = (MOD - result) % MOD;
    cout << result << endl;

    return 0;
  }

  // else
  vector<ll> results;
  while ((int)results.size() < k) {
    if (k - results.size() == 1) {
      results.push_back(pa.back());
      pa.pop_back();
    } else if (k - results.size() >= 2) {
      ll mmax = ma[ma.size() - 1] * ma[ma.size() - 2];
      ll pmax = pa[pa.size() - 1] * pa[pa.size() - 2];
      if (pmax > mmax) {
        results.push_back(pa.back());
        pa.pop_back();
      } else {
        results.push_back(ma.back());
        ma.pop_back();
        results.push_back(ma.back());
        ma.pop_back();
      }
    }
  }

  ll result = 1;
  for (auto c : results) {
    result = result * c % MOD;
  }
  cout << result << endl;

  return 0;
}