Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 100005

struct MOD_INT {

  MOD_INT(ll arg_value) {
    value = arg_value;
    value %= MOD;
  }
  MOD_INT operator-() const { return MOD_INT(-value); }
  MOD_INT &operator+=(const MOD_INT arg) {
    if ((value += arg.value) >= MOD) {
      value -= MOD;
    }
    return *this;
  }
  MOD_INT &operator-=(const MOD_INT arg) {
    if ((value += MOD - arg.value) >= MOD) {
      value -= MOD;
    }
    return *this;
  }
  MOD_INT &operator*=(const MOD_INT arg) {
    (value *= arg.value) %= MOD;
    return *this;
  }
  MOD_INT operator+(const MOD_INT arg) const { return MOD_INT(*this) += arg; }
  MOD_INT operator-(const MOD_INT arg) const { return MOD_INT(*this) -= arg; }
  MOD_INT operator*(const MOD_INT arg) const { return MOD_INT(*this) *= arg; }
  ll value;
};

ll N, K;
ll A[SIZE];

int main() {

  scanf("%lld %lld", &N, &K);

  vector<ll> PLUS, NEG;

  for (ll i = 0; i < N; i++) {

    scanf("%lld", &A[i]);

    if (A[i] < 0) {

      NEG.push_back(A[i]);
    } else {

      PLUS.push_back(A[i]);
    }
  }

  ll num_NEG = NEG.size();
  ll num_PLUS = PLUS.size();

  bool FLG = false;

  if (num_PLUS > 0) {

    if (N == K) {

      FLG = (num_NEG % 2 == 0);

    } else {

      FLG = true;
    }

  } else { // num_PLUS == 0

    FLG = (K % 2 == 0);
  }

  MOD_INT ans = 1;

  if (!FLG) { // 答えが負になる

    for (int i = 0; i < N; i++) {

      A[i] = abs(A[i]);
    }
    sort(A, A + N);

    for (int i = 0; i < K; i++) {

      ans *= A[i];
    }
    ans *= -1;

  } else {

    sort(PLUS.begin(), PLUS.end());
    sort(NEG.rbegin(), NEG.rend());

    if (K % 2 == 1) {

      ans *= PLUS.back();
      PLUS.pop_back();
    }
    vector<ll> work;

    while (PLUS.size() >= 2) {

      ll tmp = PLUS.back();
      PLUS.pop_back();
      tmp *= PLUS.back();
      PLUS.pop_back();
      work.push_back(tmp);
    }

    while (NEG.size() >= 2) {

      ll tmp = NEG.back();
      NEG.pop_back();
      tmp *= NEG.back();
      NEG.pop_back();
      work.push_back(tmp);
    }
    sort(work.rbegin(), work.rend());

    for (ll i = 0; i < K / 2; i++) {

      ans *= work[i];
    }
  }

  printf("%lld\n", (ans.value + MOD) % MOD);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 200005

struct MOD_INT {

  MOD_INT(ll arg_value) {
    value = arg_value;
    value %= MOD;
  }
  MOD_INT operator-() const { return MOD_INT(-value); }
  MOD_INT &operator+=(const MOD_INT arg) {
    if ((value += arg.value) >= MOD) {
      value -= MOD;
    }
    return *this;
  }
  MOD_INT &operator-=(const MOD_INT arg) {
    if ((value += MOD - arg.value) >= MOD) {
      value -= MOD;
    }
    return *this;
  }
  MOD_INT &operator*=(const MOD_INT arg) {
    (value *= arg.value) %= MOD;
    return *this;
  }
  MOD_INT operator+(const MOD_INT arg) const { return MOD_INT(*this) += arg; }
  MOD_INT operator-(const MOD_INT arg) const { return MOD_INT(*this) -= arg; }
  MOD_INT operator*(const MOD_INT arg) const { return MOD_INT(*this) *= arg; }
  ll value;
};

ll N, K;
ll A[SIZE];

int main() {

  scanf("%lld %lld", &N, &K);

  vector<ll> PLUS, NEG;

  for (ll i = 0; i < N; i++) {

    scanf("%lld", &A[i]);

    if (A[i] < 0) {

      NEG.push_back(A[i]);
    } else {

      PLUS.push_back(A[i]);
    }
  }

  ll num_NEG = NEG.size();
  ll num_PLUS = PLUS.size();

  bool FLG = false;

  if (num_PLUS > 0) {

    if (N == K) {

      FLG = (num_NEG % 2 == 0);

    } else {

      FLG = true;
    }

  } else { // num_PLUS == 0

    FLG = (K % 2 == 0);
  }

  MOD_INT ans = 1;

  if (!FLG) { // 答えが負になる

    for (int i = 0; i < N; i++) {

      A[i] = abs(A[i]);
    }
    sort(A, A + N);

    for (int i = 0; i < K; i++) {

      ans *= A[i];
    }
    ans *= -1;

  } else {

    sort(PLUS.begin(), PLUS.end());
    sort(NEG.rbegin(), NEG.rend());

    if (K % 2 == 1) {

      ans *= PLUS.back();
      PLUS.pop_back();
    }
    vector<ll> work;

    while (PLUS.size() >= 2) {

      ll tmp = PLUS.back();
      PLUS.pop_back();
      tmp *= PLUS.back();
      PLUS.pop_back();
      work.push_back(tmp);
    }

    while (NEG.size() >= 2) {

      ll tmp = NEG.back();
      NEG.pop_back();
      tmp *= NEG.back();
      NEG.pop_back();
      work.push_back(tmp);
    }
    sort(work.rbegin(), work.rend());

    for (ll i = 0; i < K / 2; i++) {

      ans *= work[i];
    }
  }

  printf("%lld\n", (ans.value + MOD) % MOD);

  return 0;
}