Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);

  int n, K;
  cin >> n >> K;

  vector<long long> A(n);
  bool hasZero = false;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    hasZero = A[i] == 0;
  }
  sort(A.begin(), A.end());

  const long long MOD = 1e9 + 7;

  if (n == K) {
    long long res = 1;
    for (auto x : A) {
      (res *= x) %= MOD;
      if (res < 0) {
        res += MOD;
      }
    }
    cout << res << '\n';
    return 0;
  }

  vector<long long> negatives;
  vector<long long> nonNegatives;
  for (auto x : A) {
    if (x < 0) {
      negatives.push_back(x);
    } else {
      nonNegatives.push_back(x);
    }
  }

  if (negatives.size() == n) {
    long long res = 1;
    if (K % 2 == 1) {
      reverse(A.begin(), A.end());
    }
    for (int i = 0; i < K; ++i) {
      (res *= A[i]) %= MOD;
      if (res < 0) {
        res += MOD;
      }
    }

    cout << res << '\n';
    return 0;
  }

  if (K == n - 1) {
    long long res = 1;
    if (negatives.size() % 2 == 1) {
      for (int i = 0; i + 1 < negatives.size(); ++i) {
        (res *= negatives[i]) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
      for (auto x : nonNegatives) {
        (res *= x) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
    } else {
      reverse(nonNegatives.begin(), nonNegatives.end());
      nonNegatives.pop_back();
      for (auto x : negatives) {
        (res *= x) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
      for (auto x : nonNegatives) {
        (res *= x) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
    }
    cout << res << '\n';
    return 0;
  }

  vector<long long> N;
  vector<long long> P;

  for (int i = 0; i + 1 < negatives.size(); i += 2) {
    long long x = negatives[i];
    long long y = negatives[i + 1];
    N.push_back(x * y);
  }

  long long res = 1;
  if (K % 2 == 1) {
    res = nonNegatives.back();
    nonNegatives.pop_back();
    --K;
  }

  for (int i = (int)nonNegatives.size() - 1; i > 0; i -= 2) {
    long long x = nonNegatives[i];
    long long y = nonNegatives[i - 1];
    P.push_back(x * y);
  }

  vector<long long> B;
  for (auto x : N) {
    B.push_back(x);
  }
  for (auto x : P) {
    B.push_back(x);
  }

  sort(B.begin(), B.end(), greater<long long>());

  for (int i = 0; i < K; i += 2) {
    (B[i] %= MOD);
    if (B[i] < 0) {
      B[i] += MOD;
    }
    (res *= B[i]) %= MOD;
  }

  cout << res << '\n';

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);

  int n, K;
  cin >> n >> K;

  vector<long long> A(n);
  bool hasZero = false;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    hasZero = A[i] == 0;
  }
  sort(A.begin(), A.end());

  const long long MOD = 1e9 + 7;

  if (n == K) {
    long long res = 1;
    for (auto x : A) {
      (res *= x) %= MOD;
      if (res < 0) {
        res += MOD;
      }
    }
    cout << res << '\n';
    return 0;
  }

  vector<long long> negatives;
  vector<long long> nonNegatives;
  for (auto x : A) {
    if (x < 0) {
      negatives.push_back(x);
    } else {
      nonNegatives.push_back(x);
    }
  }

  if (negatives.size() == n) {
    long long res = 1;
    if (K % 2 == 1) {
      reverse(A.begin(), A.end());
    }
    for (int i = 0; i < K; ++i) {
      (res *= A[i]) %= MOD;
      if (res < 0) {
        res += MOD;
      }
    }

    cout << res << '\n';
    return 0;
  }

  if (K == n - 1) {
    long long res = 1;
    if (negatives.size() % 2 == 1) {
      for (int i = 0; i + 1 < negatives.size(); ++i) {
        (res *= negatives[i]) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
      for (auto x : nonNegatives) {
        (res *= x) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
    } else {
      reverse(nonNegatives.begin(), nonNegatives.end());
      nonNegatives.pop_back();
      for (auto x : negatives) {
        (res *= x) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
      for (auto x : nonNegatives) {
        (res *= x) %= MOD;
        if (res < 0) {
          res += MOD;
        }
      }
    }
    cout << res << '\n';
    return 0;
  }

  vector<long long> N;
  vector<long long> P;

  for (int i = 0; i + 1 < negatives.size(); i += 2) {
    long long x = negatives[i];
    long long y = negatives[i + 1];
    N.push_back(x * y);
  }

  long long res = 1;
  if (K % 2 == 1) {
    res = nonNegatives.back();
    nonNegatives.pop_back();
    --K;
  }

  for (int i = (int)nonNegatives.size() - 1; i > 0; i -= 2) {
    long long x = nonNegatives[i];
    long long y = nonNegatives[i - 1];
    P.push_back(x * y);
  }

  vector<long long> B;
  for (auto x : N) {
    B.push_back(x);
  }
  for (auto x : P) {
    B.push_back(x);
  }

  sort(B.begin(), B.end(), greater<long long>());

  for (int i = 0; K > 0; ++i, K -= 2) {
    (B[i] %= MOD);
    if (B[i] < 0) {
      B[i] += MOD;
    }
    (res *= B[i]) %= MOD;
  }

  cout << res << '\n';

  return 0;
}