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

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long long mod = 1000000007;
  long long N, K;
  cin >> N >> K;
  vector<vector<long long>> A(N, vector<long long>(2));
  for (long long i = 0; i < N; i++) {
    long long a;
    cin >> a;
    if (a >= 0) {
      A[i][0] = a;
      A[i][1] = 0;
    } else {
      A[i][0] = a * (-1);
      A[i][1] = 1;
    }
  }

  sort(A.begin(), A.end(),
       [](const vector<long long> &alpha, const vector<long long> &beta) {
         return alpha[0] > beta[0];
       });

  long long ans = 1;
  long long check = 0;
  for (long long i = 0; i < K; i++) {
    check += A[i][1];
  }

  if (check % 2 == 1) {
    long long num1 = 0;
    bool num1check = false;
    long long num2 = 0;
    bool num2check = false;
    long long num3 = N - 1;
    for (long long i = K; i < N; i++) {
      if (A[i][1] != A[K - 1][1]) {
        num1 = i;
        num1check = true;
        break;
      }
    }
    for (long long i = K - 2; i > -1; i--) {
      if (A[i][1] != A[K - 1][1]) {
        for (long long j = K; j < N; j++) {
          if (A[j][1] != A[i][1]) {
            num2 = i;
            num3 = j;
            num2check = true;
            break;
          }
        }
        break;
      }
    }
    if (num1check == false && num2check == false) {
      for (long long j = N - 1; j > N - K - 1; j--) {
        ans = ans * A[j][0] % mod;
      }
      ans = (mod - ans) % mod;
      cout << ans << endl;
      exit(0);
    }

    if (((A[num1][0] * A[num2][0] >= A[num3][0] * A[K - 1][0]) &&
         num1check == true) ||
        num2check == false) {
      for (long long i = 0; i < K - 1; i++) {
        ans = ans * A[i][0] % mod;
      }
      ans = ans * A[num1][0] % mod;
    } else {
      for (long long i = 0; i < K; i++) {
        if (i != num2) {
          ans = ans * A[i][0] % mod;
        }
      }
      ans = ans * A[num3][0] % mod;
    }
    cout << ans << endl;
    exit(0);
  }
  for (long long i = 0; i < K; i++) {
    ans = ans * A[i][0] % mod;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long long mod = 1000000007;
  long long N, K;
  cin >> N >> K;
  vector<vector<long long>> A(N, vector<long long>(2));
  for (long long i = 0; i < N; i++) {
    long long a;
    cin >> a;
    if (a >= 0) {
      A[i][0] = a;
      A[i][1] = 0;
    } else {
      A[i][0] = a * (-1);
      A[i][1] = 1;
    }
  }

  sort(A.begin(), A.end(),
       [](const vector<long long> &alpha, const vector<long long> &beta) {
         return alpha[0] > beta[0];
       });

  long long ans = 1;
  long long check = 0;
  for (long long i = 0; i < K; i++) {
    check += A[i][1];
  }

  if (check % 2 == 1) {
    long long num1 = 0;
    long long num2 = 0;
    long long num3 = N - 1;
    for (long long i = K; i < N; i++) {
      if (A[i][1] != A[K - 1][1]) {
        num1 = i;
        break;
      }
    }
    for (long long i = K - 2; i > -1; i--) {
      if (A[i][1] != A[K - 1][1]) {
        for (long long j = K; j < N; j++) {
          if (A[j][1] != A[i][1]) {
            num2 = i;
            num3 = j;
            break;
          }
        }
        break;
      }
    }
    if (num1 == 0 && num2 == 0) {
      for (long long j = N - 1; j > N - K - 1; j--) {
        ans = ans * A[j][0] % mod;
      }
      ans = (mod - ans) % mod;
      cout << ans << endl;
      exit(0);
    }

    if ((double)(A[num1][0] / A[K - 1][0]) >=
        (double)(A[num3][0] / A[num2][0])) {
      for (long long i = 0; i < K - 1; i++) {
        ans = ans * A[i][0] % mod;
      }
      ans = ans * A[num1][0] % mod;
    } else {
      for (long long i = 0; i < K; i++) {
        if (i != num2) {
          ans = ans * A[i][0] % mod;
        }
      }
      ans = ans * A[num3][0] % mod;
    }
    cout << ans << endl;
    exit(0);
  }
  for (long long i = 0; i < K; i++) {
    ans = ans * A[i][0] % mod;
  }
  cout << ans << endl;
}