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

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N, K;
  std::cin >> N >> K;
  int zero(0), pos(0);
  std::vector<std::pair<int, int>> A(N);
  {
    int a;
    for (int i(0); i < N; ++i) {
      std::cin >> a;
      A[i].first = std::abs(a);
      if (A[i].first)
        A[i].second = a / A[i].first;
      else
        ++zero;
      if (A[i].second == 1)
        ++pos;
    }
  }
  std::sort(std::rbegin(A), std::rend(A));
  if ((K & 1) && !pos)
    std::sort(std::begin(A), std::begin(A) + N - zero);
  long long ans(1);
  int sgn(1);
  if (!pos && zero && (K & 1))
    ans = 0;
  else {
    for (int i(0); i < K; ++i)
      sgn *= A[i].second;
    if (!pos || sgn > -1 || N == K) {
      for (int i(0); i < K; ++i) {
        ans *= A[i].first;
        ans %= MOD;
      }
      ans *= sgn;
      if (ans < 0)
        ans += MOD;
    } else {
      long long a(-1), b(-1), c(-1), d(-1);
      int it(NIL);
      for (int j(K); j < N; ++j) {
        if (!A[j].second)
          break;
        if (A[j].second != A[K - 1].second) {
          it = j;
          break;
        }
      }
      if (it != NIL) {
        a = A[K - 1].first;
        b = A[it].first;
      }
      int it2(NIL), it3(NIL);
      for (int j(K - 2); j >= 0; --j) {
        if (A[j].second != A[K - 1].second) {
          it2 = j;
          break;
        }
      }
      for (int j(K); j < N; ++j) {
        if (A[j].second != A[it2].second) {
          it3 = j;
          break;
        }
      }
      if (it2 != NIL && it3 != NIL) {
        c = A[it2].first;
        d = A[it3].first;
      }

      if (a == -1)
        std::swap(A[it2], A[it3]);
      else if (c == -1 || d == -1)
        std::swap(A[it], A[K - 1]);
      else if (b * c < a * d)
        std::swap(A[it2], A[it3]);
      else
        std::swap(A[it], A[K - 1]);
      for (int i(0); i < K; ++i) {
        ans *= A[i].first;
        ans %= MOD;
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N, K;
  std::cin >> N >> K;
  int zero(0), pos(0);
  std::vector<std::pair<int, int>> A(N);
  {
    int a;
    for (int i(0); i < N; ++i) {
      std::cin >> a;
      A[i].first = std::abs(a);
      if (A[i].first)
        A[i].second = a / A[i].first;
      else
        ++zero;
      if (A[i].second == 1)
        ++pos;
    }
  }
  std::sort(std::rbegin(A), std::rend(A));
  if ((K & 1) && !pos)
    std::sort(std::begin(A), std::begin(A) + N - zero);
  long long ans(1);
  int sgn(1);
  if (!pos && zero && (K & 1))
    ans = 0;
  else {
    for (int i(0); i < K; ++i)
      sgn *= A[i].second;
    if (!pos || sgn > -1 || N == K) {
      for (int i(0); i < K; ++i) {
        ans *= A[i].first;
        ans %= MOD;
      }
      ans *= sgn;
      if (ans < 0)
        ans += MOD;
    } else {
      long long a(-1), b(-1), c(-1), d(-1);
      int it(NIL);
      for (int j(K); j < N; ++j) {
        if (!A[j].second)
          break;
        if (A[j].second != A[K - 1].second) {
          it = j;
          break;
        }
      }
      if (it != NIL) {
        a = A[K - 1].first;
        b = A[it].first;
      }
      int it2(NIL), it3(NIL);
      for (int j(K - 2); j >= 0; --j) {
        if (A[j].second != A[K - 1].second) {
          it2 = j;
          break;
        }
      }
      for (int j(K); j < N; ++j) {
        if (A[j].second != A[it2].second) {
          it3 = j;
          break;
        }
      }
      c = A[it2].first;
      d = A[it3].first;
      if (a == -1)
        std::swap(A[it2], A[it3]);
      else if (c == -1 || d == -1)
        std::swap(A[it], A[K - 1]);
      else if (b * c < a * d)
        std::swap(A[it2], A[it3]);
      else
        std::swap(A[it], A[K - 1]);
      for (int i(0); i < K; ++i) {
        ans *= A[i].first;
        ans %= MOD;
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}