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

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep1(i, n) for (ll 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() {
  ll 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) {
      result *= (c + MOD);
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

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

  // 0
  if (za.size() > n - k) {
    cout << 0 << endl;
    return 0;
  }

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

  // -
  if (k % 2 == 1 && pa.size() == 0) {
    if (za.size() > 0) {
      cout << 0 << endl;
      return 0;
    }

    ll result = 1;
    rep(i, k) {
      ll idx = ma.size() - 1 - i;
      result *= (ma[idx] + MOD);
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  // -
  if (k > pa.size() && (k - pa.size()) == 1) {
    if (za.size() > 0) {
      cout << 0 << endl;
      return 0;
    }

    ll result = ma.back() + MOD;
    for (auto c : pa) {
      result *= c;
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  // +
  if (ma.size() == 0) {
    rep(i, za.size()) pa.push_back(0);
    sort(pa.begin(), pa.end());

    ll result = 1;
    rep(i, k) {
      result *= pa.back();
      pa.pop_back();
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  vector<ll> results;
  rep(i, ma.size()) ma[i] = -ma[i];
  sort(ma.begin(), ma.end());

  // for(auto c:ma)cout<<c<<' ';cout<<endl;for(auto c:pa)cout<<c<<'
  // ';cout<<endl;

  while (results.size() < k) {
    ll pamax = -1;
    if (pa.size())
      pamax = pa[pa.size() - 1] * pa[pa.size() - 2];
    ll mamax = -1;
    if (ma.size() >= 2)
      mamax = ma[ma.size() - 1] * ma[ma.size() - 2];

    // maの最大2つの乗算の方がpaの最大2つの乗算よりもデカイ、かつ結果格納先の余裕が2個以上あるなら、負の配列から2つ取り出して格納する、2個以上無いと入らないので。
    if (mamax > pamax && k - results.size() >= 2) {
      results.push_back(ma.back());
      ma.pop_back();
      results.push_back(ma.back());
      ma.pop_back();
    } else {
      results.push_back(pa.back());
      pa.pop_back();
    }
  }

  //  for(auto c: results)cout<<c<<' ';cout<<endl;

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep1(i, n) for (ll 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() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

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

    return 0;
  }

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

  // 0
  if (za.size() > n - k) {
    cout << 0 << endl;
    return 0;
  }

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

  // -
  if (k % 2 == 1 && pa.size() == 0) {
    if (za.size() > 0) {
      cout << 0 << endl;
      return 0;
    }

    ll result = 1;
    rep(i, k) {
      ll idx = ma.size() - 1 - i;
      result *= (ma[idx] + MOD);
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  // -
  if (k > pa.size() && (k - pa.size()) == 1) {
    if (za.size() > 0) {
      cout << 0 << endl;
      return 0;
    }

    ll result = ma.back() + MOD;
    for (auto c : pa) {
      result *= c;
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  // +
  if (ma.size() == 0) {
    rep(i, za.size()) pa.push_back(0);
    sort(pa.begin(), pa.end());

    ll result = 1;
    rep(i, k) {
      result *= pa.back();
      pa.pop_back();
      result %= MOD;
    }
    cout << result << endl;

    return 0;
  }

  vector<ll> results;
  rep(i, ma.size()) ma[i] = -ma[i];
  sort(ma.begin(), ma.end());

  // for(auto c:ma)cout<<c<<' ';cout<<endl;for(auto c:pa)cout<<c<<'
  // ';cout<<endl;

  while (results.size() < k) {
    ll pamax = pa[pa.size() - 1] * pa[pa.size() - 2];
    ll mamax = -1;
    if (ma.size() >= 2)
      mamax = ma[ma.size() - 1] * ma[ma.size() - 2];

    // maの最大2つの乗算の方がpaの最大2つの乗算よりもデカイ、かつ結果格納先の余裕が2個以上あるなら、負の配列から2つ取り出して格納する、2個以上無いと入らないので。
    if (mamax > pamax && k - results.size() >= 2) {
      results.push_back(ma.back());
      ma.pop_back();
      results.push_back(ma.back());
      ma.pop_back();
    } else {
      results.push_back(pa.back());
      pa.pop_back();
    }
  }

  //  for(auto c: results)cout<<c<<' ';cout<<endl;

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

  return 0;
}