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

#include <algorithm>
#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n + 1; i++)

#define MOD 1000000007

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> Ap;
  vector<ll> An;
  ll A;
  rep(i, N) {
    cin >> A;
    if (A >= 0) {
      Ap.push_back(A);
    } else {
      An.push_back(-1 * A);
    }
  }
  sort(Ap.begin(), Ap.end(), greater<ll>());
  sort(An.begin(), An.end(), greater<ll>());

  ll ApP = 0, AnP = 0;
  ll ans = 1;
  if (K == N) {
    // pick all
    if (An.size() % 2 == 1) {
      ans *= -1;
    }
    rep(i, Ap.size()) { ans = (ans * Ap[i]) % MOD; }
    rep(i, An.size()) { ans = (ans * An[i]) % MOD; }
  } else if (Ap.empty() && K % 2 == 1) {
    // pick smaller and * (-1)
    ans *= -1;
    rep(i, K) { ans = (ans * An[An.size() - i - 1]) % MOD; }
  } else {
    // ans is always positive
    ll i = 0;
    while (i < K) {
      if (i == K - 1) {
        if (AnP % 2 == 1) {
          ans = (ans * An[AnP]) % MOD;
        } else {
          ans = (ans * Ap[ApP]) % MOD;
        }
        i++;
      } else {
        if (ApP > (ll)Ap.size() - (ll)2) {
          if ((K - i + AnP) % 2 == 0) {
            ans = (ans * An[AnP]) % MOD;
            AnP++;
          } else {
            ans = (ans * Ap[ApP]) % MOD;
            ApP++;
          }
          i++;
        } else {
          if (AnP > (ll)An.size() - (ll)2) {
            ans = (ans * Ap[ApP]) % MOD;
            ApP++;
            i++;
          } else {
            if (Ap[ApP] * Ap[ApP + 1] > An[AnP] * An[AnP + 1]) {
              ans = (ans * Ap[ApP]) % MOD;
              ApP++;
              i++;
            } else {
              ans = (ans * An[AnP]) % MOD;
              AnP++;
              i++;
              ans = (ans * An[AnP]) % MOD;
              AnP++;
              i++;
            }
          }
        }
      }
    }
  }

  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n + 1; i++)

#define MOD 1000000007

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> Ap;
  vector<ll> An;
  ll A;
  rep(i, N) {
    cin >> A;
    if (A > 0) {
      Ap.push_back(A);
    } else {
      An.push_back(-1 * A);
    }
  }
  sort(Ap.begin(), Ap.end(), greater<ll>());
  sort(An.begin(), An.end(), greater<ll>());

  ll ApP = 0, AnP = 0;
  ll ans = 1;
  if (K == N) {
    // pick all
    if (An.size() % 2 == 1) {
      ans *= -1;
    }
    rep(i, Ap.size()) { ans = (ans * Ap[i]) % MOD; }
    rep(i, An.size()) { ans = (ans * An[i]) % MOD; }
  } else if (Ap.empty() && K % 2 == 1) {
    // pick smaller and * (-1)
    ans *= -1;
    rep(i, K) { ans = (ans * An[An.size() - i - 1]) % MOD; }
  } else {
    // ans is always positive
    ll i = 0;
    while (i < K) {
      if (i == K - 1) {
        if (AnP % 2 == 1) {
          ans = (ans * An[AnP]) % MOD;
        } else {
          ans = (ans * Ap[ApP]) % MOD;
        }
        i++;
      } else {
        if (ApP > Ap.size() - 2) {
          if ((K - i + AnP) % 2 == 0) {
            ans = (ans * An[AnP]) % MOD;
            AnP++;
          } else {
            ans = (ans * Ap[ApP]) % MOD;
            ApP++;
          }
          i++;
        } else {
          if (AnP > An.size() - 2) {
            ans = (ans * Ap[ApP]) % MOD;
            ApP++;
            i++;
          } else {
            if (Ap[ApP] * Ap[ApP + 1] > An[AnP] * An[AnP]) {
              ans = (ans * Ap[ApP]) % MOD;
              ApP++;
              i++;
            } else {
              ans = (ans * An[AnP]) % MOD;
              AnP++;
              i++;
              ans = (ans * An[AnP]) % MOD;
              AnP++;
              i++;
            }
          }
        }
      }
    }
  }

  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;

  return 0;
}