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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int M = 1e9 + 7;
  int n, k;
  cin >> n >> k;
  vector<pair<long long int, int>> ls;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      ls.push_back(make_pair(a[i], 0));
    else
      ls.push_back(make_pair(-a[i], 1));
  }
  sort(ls.begin(), ls.end(), greater<pair<long long int, int>>());
  long long int ans = 1;
  int cnt = 0;
  vector<long long int> positive;
  vector<long long int> negative;
  for (int i = 0; i < k; i++) {
    ans *= ls[i].first;
    ans %= M;
    cnt += ls[i].second;
    if (ls[i].second == 0)
      positive.push_back(ls[i].first);
    else
      negative.push_back(ls[i].first);
  }
  if (cnt % 2) {
    bool f1 = false;
    long long int next_pos = -1;
    // case1 neg-- pos++
    if (negative.size()) {
      for (int i = k; i < n; i++) {
        if (ls[i].second == 0) {
          next_pos = ls[i].first;
          break;
        }
      }
      if (next_pos != -1)
        f1 = true;
    }
    bool f2 = false;
    long long int next_neg = -1;
    // case2 neg++ pos--
    if (positive.size()) {
      for (int i = k; i < n; i++) {
        if (ls[i].second == 1) {
          next_neg = ls[i].first;
          break;
        }
      }
      if (next_neg != -1)
        f2 = true;
    }
    if (f1 and f2) {
      long long int last_neg = negative[negative.size() - 1];
      long long int last_pos = positive[positive.size() - 1];
      if (next_pos * last_pos >= next_neg * last_neg) {
        ans = next_pos;
        for (int i = 0; i < (int)positive.size(); i++) {
          ans *= positive[i];
          ans %= M;
        }
        for (int i = 0; i < (int)negative.size() - 1; i++) {
          ans *= negative[i];
          ans %= M;
        }
        cout << ans << "\\n";
      } else {
        ans = next_neg;
        for (int i = 0; i < (int)positive.size() - 1; i++) {
          ans *= positive[i];
          ans %= M;
        }
        for (int i = 0; i < (int)negative.size(); i++) {
          ans *= negative[i];
          ans %= M;
        }
        cout << ans << "\\n";
      }
    } else if (f1) {
      ans = next_pos;
      for (int i = 0; i < (int)positive.size(); i++) {
        ans *= positive[i];
        ans %= M;
      }
      for (int i = 0; i < (int)negative.size() - 1; i++) {
        ans *= negative[i];
        ans %= M;
      }
      cout << ans << "\\n";
    } else if (f2) {
      ans = next_neg;
      for (int i = 0; i < (int)positive.size() - 1; i++) {
        ans *= positive[i];
        ans %= M;
      }
      for (int i = 0; i < (int)negative.size(); i++) {
        ans *= negative[i];
        ans %= M;
      }
      cout << ans << "\\n";
    } else {
      sort(a, a + n, greater<long long int>());
      ans = 1;
      for (int i = 0; i < k; i++) {
        ans *= a[i];
        ans %= M;
      }
      cout << (ans + M) % M << "\\n";
    }
  } else
    cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int M = 1e9 + 7;
  int n, k;
  cin >> n >> k;
  vector<pair<long long int, int>> ls;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      ls.push_back(make_pair(a[i], 0));
    else
      ls.push_back(make_pair(-a[i], 1));
  }
  sort(ls.begin(), ls.end(), greater<pair<long long int, int>>());
  long long int ans = 1;
  int cnt = 0;
  vector<long long int> positive;
  vector<long long int> negative;
  for (int i = 0; i < k; i++) {
    ans *= ls[i].first;
    ans %= M;
    cnt += ls[i].second;
    if (ls[i].second == 0)
      positive.push_back(ls[i].first);
    else
      negative.push_back(ls[i].first);
  }
  if (cnt % 2) {
    // case1 neg-- pos++
    long long int next_pos = -1;
    for (int i = k; i < n; i++) {
      if (ls[i].second == 0) {
        next_pos = ls[i].first;
        break;
      }
    }
    // case2 neg++ pos--
    long long int next_neg = -1;
    for (int i = k; i < n; i++) {
      if (ls[i].second == 1) {
        next_neg = ls[i].first;
        break;
      }
    }
    if (positive.size() == 0 and next_pos == -1) {
      sort(a, a + n, greater<long long int>());
      ans = 1;
      for (int i = 0; i < k; i++) {
        ans *= a[i];
        ans %= M;
      }
      cout << (ans + M) % M << "\\n";
    } else if (next_pos == -1) {
      ans = next_neg;
      for (int i = 0; i < positive.size() - 1; i++) {
        ans *= positive[i];
        ans %= M;
      }
      for (int i = 0; i < negative.size(); i++) {
        ans *= negative[i];
        ans %= M;
      }
      cout << ans << "\\n";
    } else if (next_neg == -1) {
      ans = next_pos;
      for (int i = 0; i < positive.size(); i++) {
        ans *= positive[i];
        ans %= M;
      }
      for (int i = 0; i < negative.size() - 1; i++) {
        ans *= negative[i];
        ans %= M;
      }
      cout << ans << "\\n";
    } else {
      long long int last_neg = negative[negative.size() - 1];
      long long int last_pos = -1;
      if (positive.size())
        last_pos = positive[positive.size() - 1];
      if (positive.size() == 0 or (next_pos * 1.0) / (last_neg * 1.0) >
                                      (next_neg * 1.0) / (last_pos * 1.0)) {
        ans = next_neg;
        for (int i = 0; i < positive.size() - 1; i++) {
          ans *= positive[i];
          ans %= M;
        }
        for (int i = 0; i < negative.size(); i++) {
          ans *= negative[i];
          ans %= M;
        }
        cout << ans << "\\n";
      } else {
        ans = next_pos;
        for (int i = 0; i < positive.size(); i++) {
          ans *= positive[i];
          ans %= M;
        }
        for (int i = 0; i < negative.size() - 1; i++) {
          ans *= negative[i];
          ans %= M;
        }
        cout << ans << "\\n";
      }
    }
  } else
    cout << ans << "\\n";
  return 0;
}