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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; // int:2*10**9
typedef long double ld;
typedef pair<ll, ll> P;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define vec2(name, i, j, k) vector<vector<ll>> name(i, vector<ll>(j, k))
#define vec3(name, i, j, k, l)                                                 \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k, l)))
#define pb push_back
#define MOD 1000000007 // 998244353
#define PI 3.141592653
#define INF 100000000000000 // 14
// cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int main() {
  ll n, k, a;
  cin >> n >> k;
  ll ans = 1;
  if (n == k) {
    REP(i, n) {
      cin >> a;
      ans = (ans * (MOD + a)) % MOD;
    }
    cout << ans << endl;
    return 0;
  }
  vector<ll> ap;
  vector<ll> an;
  REP(i, n) {
    cin >> a;
    if (a < 0)
      an.pb(-a);
    else
      ap.pb(a);
  }
  sort(ap.rbegin(), ap.rend());
  sort(an.rbegin(), an.rend());
  ll aplen = ap.size();
  ll anlen = an.size();
  if (ap.size() == 0 && k % 2) {
    ll cnt = 0;
    while (cnt < k) {
      ans = (ans * an[an.size() - 1 - cnt]) % MOD;
      // cout << ans << " " << cnt << endl;
      cnt++;
    }
    cout << MOD - ans << endl;
    return 0;
  }
  ll apind = 0;
  ll anind = 0;
  if (k % 2) {
    ans *= ap[0];
    k--;
    apind++;
  }
  while (k) {
    ll apcan = -INF;
    ll ancan = -INF;
    if (apind + 1 <= aplen - 1)
      apcan = ap[apind] * ap[apind + 1];
    if (anind + 1 <= anlen - 1)
      ancan = an[anind] * an[anind + 1];
    if (apcan > ancan) {
      ans = (ans * (apcan % MOD)) % MOD;
      apind += 2;
      k -= 2;
    } else {
      ans = (ans * (ancan % MOD)) % MOD;
      anind += 2;
      k -= 2;
    }
    // cout << k << " " << apcan << " " << ancan << " " << ans << endl;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; // int:2*10**9
typedef long double ld;
typedef pair<ll, ll> P;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define vec2(name, i, j, k) vector<vector<ll>> name(i, vector<ll>(j, k))
#define vec3(name, i, j, k, l)                                                 \\
  vector<vector<vector<ll>>> name(i, vector<vector<ll>>(j, vector<ll>(k, l)))
#define pb push_back
#define MOD 1000000007 // 998244353
#define PI 3.141592653
#define INF 100000000000000 // 14
// cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int main() {
  ll n, k, a;
  cin >> n >> k;
  ll ans = 1;
  if (n == k) {
    REP(i, n) {
      cin >> a;
      ans = (ans * a) % MOD;
    }
    cout << ans << endl;
    return 0;
  }
  vector<ll> ap;
  vector<ll> an;
  REP(i, n) {
    cin >> a;
    if (a < 0)
      an.pb(-a);
    else
      ap.pb(a);
  }
  sort(ap.rbegin(), ap.rend());
  sort(an.rbegin(), an.rend());
  if (ap.size() == 0 && k % 2) {
    ll cnt = 0;
    while (cnt < k) {
      ans = (ans * an[an.size() - 1 - cnt]) % MOD;
      // cout << ans << " " << cnt << endl;
      cnt++;
    }
    cout << MOD - ans << endl;
    return 0;
  }
  ll apind = 0;
  ll anind = 0;
  if (k % 2) {
    ans *= ap[0];
    k--;
    apind++;
  }
  while (k) {
    ll apcan = -INF;
    ll ancan = -INF;
    if (apind + 1 <= ap.size() - 1)
      apcan = ap[apind] * ap[apind + 1];
    if (anind + 1 <= an.size() - 1)
      ancan = an[anind] * an[anind + 1];
    if (apcan > ancan) {
      ans = (ans * (apcan % MOD)) % MOD;
      apind += 2;
      k -= 2;
    } else {
      ans = (ans * (ancan % MOD)) % MOD;
      anind += 2;
      k -= 2;
    }
    // cout << k << " " << apcan << " " << ancan << " " << ans << endl;
  }
  cout << ans << endl;
}